github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/database/leveldb/db.go (about)

     1  package leveldb
     2  
     3  import . "github.com/tendermint/tmlibs/common"
     4  
     5  type DB interface {
     6  	Get([]byte) []byte
     7  	Set([]byte, []byte)
     8  	SetSync([]byte, []byte)
     9  	Delete([]byte)
    10  	DeleteSync([]byte)
    11  	Close()
    12  	NewBatch() Batch
    13  	Iterator() Iterator
    14  	IteratorPrefix([]byte) Iterator
    15  	IteratorPrefixWithStart(Prefix, start []byte, isReverse bool) Iterator
    16  
    17  	// For debugging
    18  	Print()
    19  	Stats() map[string]string
    20  }
    21  
    22  type Batch interface {
    23  	Set(key, value []byte)
    24  	Delete(key []byte)
    25  	Write()
    26  }
    27  
    28  type Iterator interface {
    29  	Next() bool
    30  
    31  	Key() []byte
    32  	Value() []byte
    33  	Seek([]byte) bool
    34  
    35  	Release()
    36  	Error() error
    37  }
    38  
    39  //-----------------------------------------------------------------------------
    40  
    41  const (
    42  	LevelDBBackendStr   = "leveldb" // legacy, defaults to goleveldb.
    43  	CLevelDBBackendStr  = "cleveldb"
    44  	GoLevelDBBackendStr = "goleveldb"
    45  	MemDBBackendStr     = "memdb"
    46  )
    47  
    48  type dbCreator func(name string, dir string) (DB, error)
    49  
    50  var backends = map[string]dbCreator{}
    51  
    52  func registerDBCreator(backend string, creator dbCreator, force bool) {
    53  	_, ok := backends[backend]
    54  	if !force && ok {
    55  		return
    56  	}
    57  	backends[backend] = creator
    58  }
    59  
    60  func NewDB(name string, backend string, dir string) DB {
    61  	db, err := backends[backend](name, dir)
    62  	if err != nil {
    63  		PanicSanity(Fmt("Error initializing DB: %v", err))
    64  	}
    65  	return db
    66  }