As we all know, the database used by Hyperledger is ROCKSDB, and if you do not modify it, the data will be stored in the/var/hyperledger/production/db directory.
Now let's analyze the relevant code in the gray section of the diagram.
The code structure diagram is as follows
Start the database, initialize the OPENCHAINDB instance, and open the database. Note that this method does not guarantee the correct behavior of concurrent calls
Func Start () {
Openchaindb.open ()
}
Open opens a database that already exists in Hyperledger
Func (openchaindb *openchaindb) open () {
DbPath: = Getdbpath ()
Missing, err: = Dirmissingorempty (DbPath)
If err! = Nil {
Panic (FMT. Sprintf ("Error while trying to open DB:%s", err))
}
DBLOGGER.DEBUGF ("is DB path [%s] empty [%t]", DbPath, missing)
If missing {
Err = OS. Mkdirall (path. Dir (DbPath), 0755)
If err! = Nil {
Panic (FMT. Sprintf ("Error making directory path [%s]:%s", DbPath, Err))
}
}
OPTs: = Gorocksdb. Newdefaultoptions ()
Defer opts. Destroy ()
Maxlogfilesize: = Viper. GetInt ("Peer.db.maxLogFileSize")
If maxlogfilesize > 0 {
Dblogger.infof ("Setting rocksdb maxlogfilesize to%d", maxlogfilesize)
OPTs. Setmaxlogfilesize (Maxlogfilesize)
}
Keeplogfilenum: = Viper. GetInt ("Peer.db.keepLogFileNum")
If Keeplogfilenum > 0 {
Dblogger.infof ("Setting rocksdb Keeplogfilenum to%d", keeplogfilenum)
OPTs. Setkeeplogfilenum (Keeplogfilenum)
}
Loglevelstr: = Viper. GetString ("Peer.db.loglevel")
LogLevel, OK: = Rocksdbloglevelmap[loglevelstr]
If OK {
Dblogger.infof ("Setting Rocks db Infologlevel to%d", logLevel)
OPTs. Setinfologlevel (LogLevel)
}
OPTs. Setcreateifmissing (missing)
OPTs. Setcreateifmissingcolumnfamilies (True)
Cfnames: = []string{"Default"}
Cfnames = Append (Cfnames, columnfamilies ...)
var cfopts []*gorocksdb. Options
For range Cfnames {
cfopts = Append (cfopts, opts)
}
DB, Cfhandlers, err: = Gorocksdb. Opendbcolumnfamilies (opts, DbPath, Cfnames, cfopts)
If err! = Nil {
Panic (FMT. Sprintf ("Error opening DB:%s", err))
}
Openchaindb.db = DB
OPENCHAINDB.BLOCKCHAINCF = cfhandlers[1]
OPENCHAINDB.STATECF = cfhandlers[2]
OPENCHAINDB.STATEDELTACF = cfhandlers[3]
OPENCHAINDB.INDEXESCF = Cfhandlers[4]
OPENCHAINDB.PERSISTCF = cfhandlers[5]
}
Newdefaultoptions Create a default options.
Func newdefaultoptions () *options {
Return Newnativeoptions (C.rocksdb_options_create ())
}
//options means that all optional options are available when opening a database in open form
Type Options struct {
C *c.rocksdb_options_t
Keep the reference GC.
Env *env
bbto *blockbasedtableoptions
In destroy, we have to make sure we can release their memory.
CCMP *c.rocksdb_comparator_t
CMO *c.rocksdb_mergeoperator_t
CST *c.rocksdb_slicetransform_t
CCF *c.rocksdb_compactionfilter_t
}
Newdefaultoptions Create a default options.
Func newdefaultoptions () *options {
Return Newnativeoptions (C.rocksdb_options_create ())
}
Newnativeoptions create an Options object.
Func newnativeoptions (c *c.rocksdb_options_t) *options {
Return &OPTIONS{C:C}
}
Newnativeoptions create an Options object.
Func newnativeoptions (c *c.rocksdb_options_t) *options {
Return &OPTIONS{C:C}
}
Options means that when opening a database in open, all optional options
Type Options struct {
C *c.rocksdb_options_t
Keep the reference GC.
Env *env
bbto *blockbasedtableoptions
In destroy, we have to make sure we can release their memory.
CCMP *c.rocksdb_comparator_t
CMO *c.rocksdb_mergeoperator_t
CST *c.rocksdb_slicetransform_t
CCF *c.rocksdb_compactionfilter_t
}
Setmaxlogfilesize setting the maximum size of the Infolog file
If the log file is larger than the Max_log_file_size constant, the new log will be created
If Max_log_file_size equals 0, all logs are written to a log
Default Size: 0
Func (OPTs *options) setmaxlogfilesize (value int) {
C.rocksdb_options_set_max_log_file_size (OPTS.C, c.size_t (value))
}
Setkeeplogfilenum setting the maximum saved log information file
Default size: 1000
Func (OPTs *options) setkeeplogfilenum (value int) {
C.rocksdb_options_set_keep_log_file_num (OPTS.C, c.size_t (value))