tendermint/db/db.go

65 lines
1.2 KiB
Go
Raw Normal View History

2015-10-22 12:31:02 -07:00
package db
2017-04-18 14:56:05 -07:00
import . "github.com/tendermint/tmlibs/common"
2015-10-22 12:31:02 -07:00
type DB interface {
Get([]byte) []byte
Set([]byte, []byte)
SetSync([]byte, []byte)
Delete([]byte)
DeleteSync([]byte)
Close()
2016-11-29 16:06:36 -08:00
NewBatch() Batch
Iterator() Iterator
2017-09-28 08:35:01 -07:00
IteratorPrefix([]byte) Iterator
2015-10-22 12:31:02 -07:00
// For debugging
Print()
Stats() map[string]string
2015-10-22 12:31:02 -07:00
}
2016-11-29 16:06:36 -08:00
type Batch interface {
Set(key, value []byte)
Delete(key []byte)
Write()
}
type Iterator interface {
Next() bool
Key() []byte
Value() []byte
Release()
Error() error
}
2015-10-22 12:31:02 -07:00
//-----------------------------------------------------------------------------
const (
LevelDBBackendStr = "leveldb" // legacy, defaults to goleveldb.
2016-12-22 12:44:51 -08:00
CLevelDBBackendStr = "cleveldb"
GoLevelDBBackendStr = "goleveldb"
MemDBBackendStr = "memdb"
)
type dbCreator func(name string, dir string) (DB, error)
var backends = map[string]dbCreator{}
func registerDBCreator(backend string, creator dbCreator, force bool) {
_, ok := backends[backend]
if !force && ok {
return
}
backends[backend] = creator
}
2015-10-22 12:31:02 -07:00
2016-05-08 15:00:13 -07:00
func NewDB(name string, backend string, dir string) DB {
db, err := backends[backend](name, dir)
if err != nil {
PanicSanity(Fmt("Error initializing DB: %v", err))
2015-10-22 12:31:02 -07:00
}
return db
2015-10-22 12:31:02 -07:00
}