Skip to content

Commit

Permalink
Expose levelDB object (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdeziel authored Feb 9, 2022
1 parent b3f3099 commit 59182a3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
6 changes: 6 additions & 0 deletions engines/leveldb/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions honu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 18 additions & 1 deletion honu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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) {
Expand Down

0 comments on commit 59182a3

Please sign in to comment.