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

Implement stack trie #241

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions kai/kaidb/dbtest/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestDatabaseSuite(t *testing.T, New func() kaidb.KeyValueStore) {
}
}
// Iterate over the database with the given configs and verify the results
it, idx := db.NewIteratorWithPrefix([]byte(tt.prefix)), 0
it, idx := db.NewIterator([]byte(tt.prefix), nil), 0
for it.Next() {
if len(tt.order) <= idx {
t.Errorf("test %d: prefix=%q more items than expected: checking idx=%d (key %q), expecting len=%d", i, tt.prefix, idx, it.Key(), len(tt.order))
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestDatabaseSuite(t *testing.T, New func() kaidb.KeyValueStore) {
}

{
it := db.NewIterator()
it := db.NewIterator(nil, nil)
got, want := iterateKeys(it), keys
if err := it.Error(); err != nil {
t.Fatal(err)
Expand All @@ -138,7 +138,7 @@ func TestDatabaseSuite(t *testing.T, New func() kaidb.KeyValueStore) {
}

{
it := db.NewIteratorWithPrefix([]byte("1"))
it := db.NewIterator([]byte("1"), nil)
got, want := iterateKeys(it), []string{"1", "10", "11", "12"}
if err := it.Error(); err != nil {
t.Fatal(err)
Expand All @@ -150,7 +150,7 @@ func TestDatabaseSuite(t *testing.T, New func() kaidb.KeyValueStore) {
}

{
it := db.NewIteratorWithPrefix([]byte("5"))
it := db.NewIterator([]byte("5"), nil)
got, want := iterateKeys(it), []string{}
if err := it.Error(); err != nil {
t.Fatal(err)
Expand All @@ -162,7 +162,7 @@ func TestDatabaseSuite(t *testing.T, New func() kaidb.KeyValueStore) {
}

{
it := db.NewIteratorWithStart([]byte("2"))
it := db.NewIterator(nil, []byte("2"))
got, want := iterateKeys(it), []string{"2", "20", "21", "22", "3", "4", "6"}
if err := it.Error(); err != nil {
t.Fatal(err)
Expand All @@ -174,7 +174,7 @@ func TestDatabaseSuite(t *testing.T, New func() kaidb.KeyValueStore) {
}

{
it := db.NewIteratorWithStart([]byte("5"))
it := db.NewIterator(nil, []byte("5"))
got, want := iterateKeys(it), []string{"6"}
if err := it.Error(); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestDatabaseSuite(t *testing.T, New func() kaidb.KeyValueStore) {
}

{
it := db.NewIterator()
it := db.NewIterator(nil, nil)
if got, want := iterateKeys(it), []string{"1", "2", "3", "4"}; !reflect.DeepEqual(got, want) {
t.Errorf("got: %s; want: %s", got, want)
}
Expand All @@ -269,7 +269,7 @@ func TestDatabaseSuite(t *testing.T, New func() kaidb.KeyValueStore) {
}

{
it := db.NewIterator()
it := db.NewIterator(nil, nil)
if got, want := iterateKeys(it), []string{"2", "3", "4", "5", "6"}; !reflect.DeepEqual(got, want) {
t.Errorf("got: %s; want: %s", got, want)
}
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestDatabaseSuite(t *testing.T, New func() kaidb.KeyValueStore) {
t.Fatal(err)
}

it := db.NewIterator()
it := db.NewIterator(nil, nil)
if got := iterateKeys(it); !reflect.DeepEqual(got, want) {
t.Errorf("got: %s; want: %s", got, want)
}
Expand Down
19 changes: 7 additions & 12 deletions kai/kaidb/interator.go → kai/kaidb/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,11 @@ type Iterator interface {

// Iteratee wraps the NewIterator methods of a backing data store.
type Iteratee interface {
// NewIterator creates a binary-alphabetical iterator over the entire keyspace
// contained within the key-value database.
NewIterator() Iterator

// NewIteratorWithStart creates a binary-alphabetical iterator over a subset of
// database content starting at a particular initial key (or after, if it does
// not exist).
NewIteratorWithStart(start []byte) Iterator

// NewIteratorWithPrefix creates a binary-alphabetical iterator over a subset
// of database content with a particular key prefix.
NewIteratorWithPrefix(prefix []byte) Iterator
// NewIterator creates a binary-alphabetical iterator over a subset
// of database content with a particular key prefix, starting at a particular
// initial key (or after, if it does not exist).
//
// Note: This method assumes that the prefix is NOT part of the start, so there's
// no need for the caller to prepend the prefix to the start
NewIterator(prefix []byte, start []byte) Iterator
}