From c43fb700e39326bf384f6a9077d92ad303f4188e Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 29 Aug 2018 05:44:55 +0100 Subject: [PATCH] New NewGoLevelDBWithOpts() to pass opts down to goleveldb (#2293) Closes: #2292 --- CHANGELOG_PENDING.md | 1 + libs/db/go_level_db.go | 6 +++++- libs/db/go_level_db_test.go | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index 15576a4e0..6f594f690 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -23,6 +23,7 @@ BREAKING CHANGES: FEATURES: - [types] allow genesis file to have 0 validators ([#2015](https://github.com/tendermint/tendermint/issues/2015)) +- [libs] allow passing options through when creating instances of leveldb dbs ([#2292](https://github.com/tendermint/tendermint/issues/2292)) IMPROVEMENTS: - [docs] Lint documentation with `write-good` and `stop-words`. diff --git a/libs/db/go_level_db.go b/libs/db/go_level_db.go index 349e447b2..8a4887921 100644 --- a/libs/db/go_level_db.go +++ b/libs/db/go_level_db.go @@ -28,8 +28,12 @@ type GoLevelDB struct { } func NewGoLevelDB(name string, dir string) (*GoLevelDB, error) { + return NewGoLevelDBWithOpts(name, dir, nil) +} + +func NewGoLevelDBWithOpts(name string, dir string, o *opt.Options) (*GoLevelDB, error) { dbPath := filepath.Join(dir, name+".db") - db, err := leveldb.OpenFile(dbPath, nil) + db, err := leveldb.OpenFile(dbPath, o) if err != nil { return nil, err } diff --git a/libs/db/go_level_db_test.go b/libs/db/go_level_db_test.go index e262fccd3..2b2346588 100644 --- a/libs/db/go_level_db_test.go +++ b/libs/db/go_level_db_test.go @@ -6,9 +6,30 @@ import ( "fmt" "testing" + "github.com/syndtr/goleveldb/leveldb/opt" + + "github.com/stretchr/testify/require" cmn "github.com/tendermint/tendermint/libs/common" ) +func TestNewGoLevelDB(t *testing.T) { + name := fmt.Sprintf("test_%x", cmn.RandStr(12)) + // Test write locks + db, err := NewGoLevelDB(name, "") + require.Nil(t, err) + _, err = NewGoLevelDB(name, "") + require.NotNil(t, err) + db.Close() // Close the db to release the lock + + // Open the db twice in a row to test read-only locks + ro1, err := NewGoLevelDBWithOpts(name, "", &opt.Options{ReadOnly: true}) + defer ro1.Close() + require.Nil(t, err) + ro2, err := NewGoLevelDBWithOpts(name, "", &opt.Options{ReadOnly: true}) + defer ro2.Close() + require.Nil(t, err) +} + func BenchmarkRandomReadsWrites(b *testing.B) { b.StopTimer()