diff --git a/db/backend_test.go b/db/backend_test.go index 0f4346f2e..0227eb547 100644 --- a/db/backend_test.go +++ b/db/backend_test.go @@ -15,7 +15,7 @@ func cleanupDBDir(dir, name string) { os.RemoveAll(filepath.Join(dir, name) + ".db") } -func testBackendGetSetDelete(t *testing.T, backend string) { +func testBackendGetSetDelete(t *testing.T, backend dbBackendType) { // Default dir, dirname := cmn.Tempdir(fmt.Sprintf("test_backend_%s_", backend)) defer dir.Close() @@ -141,9 +141,9 @@ func TestBackendsNilKeys(t *testing.T) { } } -func TestGoLevelDBBackendStr(t *testing.T) { +func TestGoLevelDBBackend(t *testing.T) { name := cmn.Fmt("test_%x", cmn.RandStr(12)) - db := NewDB(name, GoLevelDBBackendStr, "") + db := NewDB(name, GoLevelDBBackend, "") defer cleanupDBDir("", name) _, ok := db.(*GoLevelDB) diff --git a/db/c_level_db.go b/db/c_level_db.go index f1a5a3aef..a59137883 100644 --- a/db/c_level_db.go +++ b/db/c_level_db.go @@ -14,8 +14,8 @@ func init() { dbCreator := func(name string, dir string) (DB, error) { return NewCLevelDB(name, dir) } - registerDBCreator(LevelDBBackendStr, dbCreator, true) - registerDBCreator(CLevelDBBackendStr, dbCreator, false) + registerDBCreator(LevelDBBackend, dbCreator, true) + registerDBCreator(CLevelDBBackend, dbCreator, false) } var _ DB = (*CLevelDB)(nil) diff --git a/db/c_level_db_test.go b/db/c_level_db_test.go index 89993fbac..34bb72273 100644 --- a/db/c_level_db_test.go +++ b/db/c_level_db_test.go @@ -86,9 +86,9 @@ func bytes2Int64(buf []byte) int64 { } */ -func TestCLevelDBBackendStr(t *testing.T) { +func TestCLevelDBBackend(t *testing.T) { name := cmn.Fmt("test_%x", cmn.RandStr(12)) - db := NewDB(name, LevelDBBackendStr, "") + db := NewDB(name, LevelDBBackend, "") defer cleanupDBDir("", name) _, ok := db.(*CLevelDB) diff --git a/db/common_test.go b/db/common_test.go index 2a5d01818..1a529949f 100644 --- a/db/common_test.go +++ b/db/common_test.go @@ -45,7 +45,7 @@ func checkValuePanics(t *testing.T, itr Iterator) { assert.Panics(t, func() { itr.Key() }, "checkValuePanics expected panic but didn't") } -func newTempDB(t *testing.T, backend string) (db DB) { +func newTempDB(t *testing.T, backend dbBackendType) (db DB) { dir, dirname := cmn.Tempdir("test_go_iterator") db = NewDB("testdb", backend, dirname) dir.Close() diff --git a/db/db.go b/db/db.go index 25ff93ec5..1428c9c42 100644 --- a/db/db.go +++ b/db/db.go @@ -5,19 +5,21 @@ import "fmt" //---------------------------------------- // Main entry +type dbBackendType string + const ( - LevelDBBackendStr = "leveldb" // legacy, defaults to goleveldb unless +gcc - CLevelDBBackendStr = "cleveldb" - GoLevelDBBackendStr = "goleveldb" - MemDBBackendStr = "memdb" - FSDBBackendStr = "fsdb" // using the filesystem naively + LevelDBBackend dbBackendType = "leveldb" // legacy, defaults to goleveldb unless +gcc + CLevelDBBackend dbBackendType = "cleveldb" + GoLevelDBBackend dbBackendType = "goleveldb" + MemDBBackend dbBackendType = "memDB" + FSDBBackend dbBackendType = "fsdb" // using the filesystem naively ) type dbCreator func(name string, dir string) (DB, error) -var backends = map[string]dbCreator{} +var backends = map[dbBackendType]dbCreator{} -func registerDBCreator(backend string, creator dbCreator, force bool) { +func registerDBCreator(backend dbBackendType, creator dbCreator, force bool) { _, ok := backends[backend] if !force && ok { return @@ -25,7 +27,7 @@ func registerDBCreator(backend string, creator dbCreator, force bool) { backends[backend] = creator } -func NewDB(name string, backend string, dir string) DB { +func NewDB(name string, backend dbBackendType, dir string) DB { db, err := backends[backend](name, dir) if err != nil { panic(fmt.Sprintf("Error initializing DB: %v", err)) diff --git a/db/fsdb.go b/db/fsdb.go index 45c3231f6..578c1785a 100644 --- a/db/fsdb.go +++ b/db/fsdb.go @@ -19,7 +19,7 @@ const ( ) func init() { - registerDBCreator(FSDBBackendStr, func(name string, dir string) (DB, error) { + registerDBCreator(FSDBBackend, func(name string, dir string) (DB, error) { dbPath := filepath.Join(dir, name+".db") return NewFSDB(dbPath), nil }, false) diff --git a/db/go_level_db.go b/db/go_level_db.go index 7d60e060f..9fed329bf 100644 --- a/db/go_level_db.go +++ b/db/go_level_db.go @@ -17,8 +17,8 @@ func init() { dbCreator := func(name string, dir string) (DB, error) { return NewGoLevelDB(name, dir) } - registerDBCreator(LevelDBBackendStr, dbCreator, false) - registerDBCreator(GoLevelDBBackendStr, dbCreator, false) + registerDBCreator(LevelDBBackend, dbCreator, false) + registerDBCreator(GoLevelDBBackend, dbCreator, false) } var _ DB = (*GoLevelDB)(nil) diff --git a/db/mem_db.go b/db/mem_db.go index 1e3bee5a5..f2c484fa7 100644 --- a/db/mem_db.go +++ b/db/mem_db.go @@ -7,7 +7,7 @@ import ( ) func init() { - registerDBCreator(MemDBBackendStr, func(name string, dir string) (DB, error) { + registerDBCreator(MemDBBackend, func(name string, dir string) (DB, error) { return NewMemDB(), nil }, false) }