2015-10-22 12:31:02 -07:00
|
|
|
package db
|
|
|
|
|
2017-04-18 17:56:05 -04:00
|
|
|
import . "github.com/tendermint/tmlibs/common"
|
2015-10-22 12:31:02 -07:00
|
|
|
|
|
|
|
type DB interface {
|
2017-11-09 17:42:32 -05:00
|
|
|
Get([]byte) []byte // NOTE: returns nil iff never set or deleted.
|
2015-10-22 12:31:02 -07:00
|
|
|
Set([]byte, []byte)
|
|
|
|
SetSync([]byte, []byte)
|
|
|
|
Delete([]byte)
|
|
|
|
DeleteSync([]byte)
|
|
|
|
Close()
|
2016-11-29 16:06:36 -08:00
|
|
|
NewBatch() Batch
|
2017-09-28 17:26:24 +02:00
|
|
|
Iterator() Iterator
|
2015-10-22 12:31:02 -07:00
|
|
|
|
|
|
|
// For debugging
|
|
|
|
Print()
|
2017-11-09 17:42:32 -05:00
|
|
|
|
|
|
|
// Stats returns a map of property values for all keys and the size of the cache.
|
2017-03-23 11:37:46 -04:00
|
|
|
Stats() map[string]string
|
2017-11-09 17:42:32 -05:00
|
|
|
|
|
|
|
// CacheWrap wraps the DB w/ a CacheDB.
|
|
|
|
CacheWrap() interface{}
|
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()
|
|
|
|
}
|
|
|
|
|
2017-11-09 17:42:32 -05:00
|
|
|
/*
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
for itr.Seek(mykey); itr.Valid(); itr.Next() {
|
|
|
|
k, v := itr.Key(); itr.Value()
|
|
|
|
....
|
|
|
|
}
|
|
|
|
*/
|
2017-03-17 13:27:20 -04:00
|
|
|
type Iterator interface {
|
2017-03-23 11:37:46 -04:00
|
|
|
|
2017-11-09 17:42:32 -05:00
|
|
|
// Seek moves the iterator the position of the key given or, if the key
|
|
|
|
// doesn't exist, the next key that does exist in the database. If the key
|
|
|
|
// doesn't exist, and there is no next key, the Iterator becomes invalid.
|
|
|
|
Seek(key []byte)
|
|
|
|
|
|
|
|
// Valid returns false only when an Iterator has iterated past either the
|
|
|
|
// first or the last key in the database.
|
|
|
|
Valid() bool
|
|
|
|
|
|
|
|
// Next moves the iterator to the next sequential key in the database, as
|
|
|
|
// defined by the Comparator in the ReadOptions used to create this Iterator.
|
|
|
|
//
|
|
|
|
// If Valid returns false, this method will panic.
|
|
|
|
Next()
|
|
|
|
|
|
|
|
// Prev moves the iterator to the previous sequential key in the database, as
|
|
|
|
// defined by the Comparator in the ReadOptions used to create this Iterator.
|
|
|
|
//
|
|
|
|
// If Valid returns false, this method will panic.
|
|
|
|
Prev()
|
|
|
|
|
|
|
|
// Key returns the key of the cursor.
|
|
|
|
//
|
|
|
|
// If Valid returns false, this method will panic.
|
2017-03-17 13:27:20 -04:00
|
|
|
Key() []byte
|
2017-11-09 17:42:32 -05:00
|
|
|
|
|
|
|
// Value returns the key of the cursor.
|
|
|
|
//
|
|
|
|
// If Valid returns false, this method will panic.
|
2017-03-17 13:27:20 -04:00
|
|
|
Value() []byte
|
2017-09-28 17:26:24 +02:00
|
|
|
|
2017-11-09 17:42:32 -05:00
|
|
|
// GetError returns an IteratorError from LevelDB if it had one during
|
|
|
|
// iteration.
|
|
|
|
//
|
|
|
|
// This method is safe to call when Valid returns false.
|
|
|
|
GetError() error
|
|
|
|
|
|
|
|
// Close deallocates the given Iterator.
|
|
|
|
Close()
|
2017-03-17 13:27:20 -04:00
|
|
|
}
|
|
|
|
|
2015-10-22 12:31:02 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-11-09 17:42:32 -05:00
|
|
|
// Main entry
|
2015-10-22 12:31:02 -07:00
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
const (
|
2016-11-30 20:22:35 -08:00
|
|
|
LevelDBBackendStr = "leveldb" // legacy, defaults to goleveldb.
|
2016-12-22 15:44:51 -05:00
|
|
|
CLevelDBBackendStr = "cleveldb"
|
|
|
|
GoLevelDBBackendStr = "goleveldb"
|
2016-11-30 20:15:29 -08:00
|
|
|
MemDBBackendStr = "memdb"
|
2017-11-09 17:42:32 -05:00
|
|
|
FSDBBackendStr = "fsdb" // using the filesystem naively
|
2016-11-30 20:15:29 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type dbCreator func(name string, dir string) (DB, error)
|
|
|
|
|
2016-11-30 20:22:35 -08:00
|
|
|
var backends = map[string]dbCreator{}
|
2016-11-30 20:15:29 -08:00
|
|
|
|
|
|
|
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 {
|
2016-11-30 20:15:29 -08:00
|
|
|
db, err := backends[backend](name, dir)
|
|
|
|
if err != nil {
|
|
|
|
PanicSanity(Fmt("Error initializing DB: %v", err))
|
2015-10-22 12:31:02 -07:00
|
|
|
}
|
2016-11-30 20:15:29 -08:00
|
|
|
return db
|
2015-10-22 12:31:02 -07:00
|
|
|
}
|