Skip to content

Commit

Permalink
Merge branch 'simplify-iterator' of https://github.com/phatngluu/go-k…
Browse files Browse the repository at this point in the history
…ardia into stack-trie
  • Loading branch information
rameight committed Dec 29, 2022
2 parents 080507c + 2d09ee3 commit b3dc2b1
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 340 deletions.
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
}

0 comments on commit b3dc2b1

Please sign in to comment.