Skip to content

Commit

Permalink
iterator offset key skipping
Browse files Browse the repository at this point in the history
* Skips the offset key while iterating over the storage. Returning
  the offset produces errors as users only expect to receive
  objects they've stored in the storage themselves and the stored
  offset can be considered an implementation detail.
  • Loading branch information
SamiHiltunen committed Jul 20, 2017
1 parent 4264155 commit 38319c5
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 38319c5

Please sign in to comment.