Skip to content

Commit

Permalink
Merge pull request #29 from lovoo/iterator-fix
Browse files Browse the repository at this point in the history
iterator offset key skipping
  • Loading branch information
SamiHiltunen committed Jul 25, 2017
2 parents 4264155 + 38319c5 commit 3e4d6b6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
15 changes: 14 additions & 1 deletion storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func (i *iter) Next() bool {

// find the next non deleted snapshot value
for !i.snapExhausted() {
if string(i.Key()) == offsetKey {
i.current++
continue
}

if val, err := i.Value(); err != nil {
// TODO (franz): sure we want a panic? Not returning the error?
i.storage.log.Panicf("error getting snapshot value in next: %v", err)
Expand All @@ -96,6 +101,10 @@ func (i *iter) Next() bool {

// find next value in db that was not in snapshot
for i.iter.Next() {
if string(i.Key()) == offsetKey {
continue
}

if !i.snapshot.Has(string(i.Key())) {
return true
}
Expand All @@ -114,7 +123,11 @@ func (i *iter) Key() []byte {

func (i *iter) Value() (interface{}, error) {
if i.snapExhausted() {
return i.storage.codec.Decode(i.iter.Value())
dec, err := i.storage.codec.Decode(i.iter.Value())
if err != nil {
return nil, fmt.Errorf("error decoding value (key: %s): %v", string(i.Key()), err)
}
return dec, nil
}

val, enc, err := i.snapshot.Get(string(i.Key()), i.storage.stateCloner(i.storage.codec))
Expand Down
3 changes: 3 additions & 0 deletions storage/storage_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func (i *memiter) exhausted() bool {

func (i *memiter) Next() bool {
i.current++
if string(i.Key()) == offsetKey {
i.current++
}
return !i.exhausted()
}

Expand Down
3 changes: 2 additions & 1 deletion storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ func TestMemIter(t *testing.T) {

found := map[string]string{}

storage.Set(offsetKey, "not-returned")
for k, v := range kv {
storage.Set(k, v)
}

// released iterator should be immidiately exhausted
// released iterator should be immediately exhausted
iter := storage.Iterator()
iter.Release()
ensure.False(t, iter.Next(), "released iterator had a next")
Expand Down

0 comments on commit 3e4d6b6

Please sign in to comment.