github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/database/leveldb/db.go (about)

     1  package db
     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  
    16  	// For debugging
    17  	Print()
    18  	Stats() map[string]string
    19  }
    20  
    21  type Batch interface {
    22  	Set(key, value []byte)
    23  	Delete(key []byte)
    24  	Write()
    25  }
    26  
    27  type Iterator interface {
    28  	Next() bool
    29  
    30  	Key() []byte
    31  	Value() []byte
    32  	Seek([]byte) bool
    33  
    34  	Release()
    35  	Error() error
    36  }
    37  
    38  //-----------------------------------------------------------------------------
    39  
    40  const (
    41  	LevelDBBackendStr   = "leveldb" // legacy, defaults to goleveldb.
    42  	CLevelDBBackendStr  = "cleveldb"
    43  	GoLevelDBBackendStr = "goleveldb"
    44  	MemDBBackendStr     = "memdb"
    45  )
    46  
    47  type dbCreator func(name string, dir string) (DB, error)
    48  
    49  var backends = map[string]dbCreator{}
    50  
    51  func registerDBCreator(backend string, creator dbCreator, force bool) {
    52  	_, ok := backends[backend]
    53  	if !force && ok {
    54  		return
    55  	}
    56  	backends[backend] = creator
    57  }
    58  
    59  func NewDB(name string, backend string, dir string) DB {
    60  	db, err := backends[backend](name, dir)
    61  	if err != nil {
    62  		PanicSanity(Fmt("Error initializing DB: %v", err))
    63  	}
    64  	return db
    65  }