2015-10-22 12:31:02 -07:00
|
|
|
package db
|
|
|
|
|
2017-12-12 14:00:03 -08:00
|
|
|
import "fmt"
|
|
|
|
|
2017-12-28 18:30:56 -08:00
|
|
|
//----------------------------------------
|
2017-11-09 14:42:32 -08:00
|
|
|
// Main entry
|
2015-10-22 12:31:02 -07:00
|
|
|
|
2016-11-30 20:15:29 -08:00
|
|
|
const (
|
2017-12-12 16:08:22 -08:00
|
|
|
LevelDBBackendStr = "leveldb" // legacy, defaults to goleveldb unless +gcc
|
2016-12-22 12:44:51 -08:00
|
|
|
CLevelDBBackendStr = "cleveldb"
|
|
|
|
GoLevelDBBackendStr = "goleveldb"
|
2016-11-30 20:15:29 -08:00
|
|
|
MemDBBackendStr = "memdb"
|
2017-11-09 14:42:32 -08: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 {
|
2017-12-12 14:00:03 -08:00
|
|
|
panic(fmt.Sprintf("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
|
|
|
}
|