2015-10-22 12:31:02 -07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
|
|
|
|
. "github.com/tendermint/go-common"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2015-10-22 12:31:02 -07:00
|
|
|
|
|
|
|
// For debugging
|
|
|
|
Print()
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:06:36 -08:00
|
|
|
type Batch interface {
|
|
|
|
Set(key, value []byte)
|
|
|
|
Delete(key []byte)
|
|
|
|
Write()
|
|
|
|
}
|
|
|
|
|
2015-10-22 12:31:02 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Database types
|
|
|
|
const DBBackendMemDB = "memdb"
|
|
|
|
const DBBackendLevelDB = "leveldb"
|
2016-11-29 16:06:36 -08:00
|
|
|
const DBBackendLevelDB2 = "leveldb2"
|
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 {
|
|
|
|
switch backend {
|
2015-10-22 12:31:02 -07:00
|
|
|
case DBBackendMemDB:
|
|
|
|
db := NewMemDB()
|
|
|
|
return db
|
|
|
|
case DBBackendLevelDB:
|
2016-05-08 15:00:13 -07:00
|
|
|
db, err := NewLevelDB(path.Join(dir, name+".db"))
|
2015-10-22 12:31:02 -07:00
|
|
|
if err != nil {
|
|
|
|
PanicCrisis(err)
|
|
|
|
}
|
|
|
|
return db
|
2016-11-29 16:06:36 -08:00
|
|
|
case DBBackendLevelDB2:
|
|
|
|
db, err := NewLevelDB2(path.Join(dir, name+".db"))
|
|
|
|
if err != nil {
|
|
|
|
PanicCrisis(err)
|
|
|
|
}
|
|
|
|
return db
|
2015-10-22 12:31:02 -07:00
|
|
|
default:
|
2016-05-08 15:00:13 -07:00
|
|
|
PanicSanity(Fmt("Unknown DB backend: %v", backend))
|
2015-10-22 12:31:02 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|