Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose levelDB object #20

Merged
merged 1 commit into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines +170 to +171
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good type assertion!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add a totalKeys++ to line 110 after the undelete you'll see the count mismatch.

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