From 59182a346748edcf011f6b8caa8503ee254f67f9 Mon Sep 17 00:00:00 2001 From: Patrick Deziel <42919891+pdeziel@users.noreply.github.com> Date: Wed, 9 Feb 2022 06:39:19 -0600 Subject: [PATCH] Expose levelDB object (#20) --- engines/leveldb/leveldb.go | 6 ++++++ honu.go | 5 +++++ honu_test.go | 19 ++++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/engines/leveldb/leveldb.go b/engines/leveldb/leveldb.go index f093e42..0b4a678 100644 --- a/engines/leveldb/leveldb.go +++ b/engines/leveldb/leveldb.go @@ -34,6 +34,12 @@ type Transaction struct { ro bool } +// Returns the underlying database object for direct access by the caller, to enable +// backups, etc. +func (db *LevelDBEngine) DB() *leveldb.DB { + return db.ldb +} + // Returns the name of the engine type. func (db *LevelDBEngine) Engine() string { return "leveldb" diff --git a/honu.go b/honu.go index 7add85b..2f9b79f 100644 --- a/honu.go +++ b/honu.go @@ -349,3 +349,8 @@ func (db *DB) Iter(prefix []byte, options ...opts.Option) (i iterator.Iterator, } return iter.Iter(prefix, cfg) } + +// Returns the underlying DB engine for direct access. +func (db *DB) Engine() engine.Engine { + return db.engine +} diff --git a/honu_test.go b/honu_test.go index a58c9b4..56a2908 100644 --- a/honu_test.go +++ b/honu_test.go @@ -10,6 +10,7 @@ import ( "github.com/rotationalio/honu" "github.com/rotationalio/honu/config" + "github.com/rotationalio/honu/engines/leveldb" "github.com/rotationalio/honu/object" "github.com/rotationalio/honu/options" "github.com/stretchr/testify/require" @@ -57,6 +58,7 @@ func TestLevelDBInteractions(t *testing.T) { defer os.RemoveAll(tmpDir) defer db.Close() + totalKeys := 0 for _, namespace := range testNamespaces { // Use a constant key to ensure namespaces // are working correctly. @@ -71,6 +73,7 @@ func TestLevelDBInteractions(t *testing.T) { obj, err := db.Put(key, expectedValue, options.WithNamespace(namespace)) require.NoError(t, err) require.False(t, obj.Tombstone()) + totalKeys++ // Get the version of foo from the database value, err := db.Get(key, options.WithNamespace(namespace)) @@ -136,6 +139,7 @@ func TestLevelDBInteractions(t *testing.T) { value := []byte(pair[1]) _, err := db.Put(key, value, options.WithNamespace(namespace)) require.NoError(t, err) + totalKeys++ } // Iterate over a prefix in the database @@ -147,7 +151,6 @@ func TestLevelDBInteractions(t *testing.T) { require.Equal(t, string(key), pairs[collected+2][0]) value := iter.Value() - fmt.Println(value) require.Equal(t, string(value), string(pairs[collected+2][1])) obj, err := iter.Object() @@ -161,6 +164,20 @@ func TestLevelDBInteractions(t *testing.T) { require.NoError(t, iter.Error()) iter.Release() } + + // Test iteration over all the namespaces + // FIXME: This is skipping the undeleted values + engine, ok := db.Engine().(*leveldb.LevelDBEngine) + require.True(t, ok) + ldb := engine.DB() + iter := ldb.NewIterator(nil, nil) + collected := 0 + for iter.Next() { + collected++ + } + require.Equal(t, totalKeys, collected) + require.NoError(t, iter.Error()) + iter.Release() } func TestUpdate(t *testing.T) {