-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Seek and Prev to Iterator interface (#11)
Although primarily a leveldb iterator interface, having Prev in addition to Next is going to be a helpful utility if we can implement it with other engines. The larger requirement is Seek which is absolutely needed for pagination and other advanced iteration. These are primarily a passthrough for leveldb operations, but I've also added some tests for the empty iterator which is not a passthrough.
- Loading branch information
Showing
4 changed files
with
71 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package iterator_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
. "github.com/rotationalio/honu/iterator" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEmptyIterator(t *testing.T) { | ||
// Check that the empty iterator returns expected values | ||
iter := NewEmptyIterator(nil) | ||
require.False(t, iter.Next()) | ||
require.False(t, iter.Prev()) | ||
require.False(t, iter.Seek([]byte("foo"))) | ||
require.Nil(t, iter.Key()) | ||
require.Nil(t, iter.Value()) | ||
require.NoError(t, iter.Error()) | ||
|
||
obj, err := iter.Object() | ||
require.NoError(t, err) | ||
require.Nil(t, obj) | ||
|
||
// After calling release the empty iterator should still have no error | ||
iter.Release() | ||
require.NoError(t, iter.Error()) | ||
|
||
// However if next is called after release, then the iterator should error | ||
require.False(t, iter.Next()) | ||
require.EqualError(t, iter.Error(), ErrIterReleased.Error()) | ||
|
||
// Check that the empty iterator can be initialized with an error | ||
iter = NewEmptyIterator(errors.New("something bad happened")) | ||
require.EqualError(t, iter.Error(), "something bad happened") | ||
|
||
// Ensure that calling any of the iterator methods do not change the error | ||
require.False(t, iter.Next()) | ||
require.False(t, iter.Prev()) | ||
require.False(t, iter.Seek([]byte("foo"))) | ||
require.Nil(t, iter.Key()) | ||
require.Nil(t, iter.Value()) | ||
|
||
obj, err = iter.Object() | ||
require.NoError(t, err) | ||
require.Nil(t, obj) | ||
|
||
require.EqualError(t, iter.Error(), "something bad happened") | ||
|
||
// Ensure calling Release doesn't change the error | ||
iter.Release() | ||
require.EqualError(t, iter.Error(), "something bad happened") | ||
|
||
// Ensure calling Next after Release doesn't change the error | ||
iter.Next() | ||
require.EqualError(t, iter.Error(), "something bad happened") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters