From 38319c5a6719e6b4c138f845792c193ed8f057a6 Mon Sep 17 00:00:00 2001 From: Sami Hiltunen Date: Thu, 20 Jul 2017 11:08:41 +0200 Subject: [PATCH] iterator offset key skipping * 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. --- storage/storage.go | 15 ++++++++++++++- storage/storage_mock.go | 3 +++ storage/storage_test.go | 3 ++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/storage/storage.go b/storage/storage.go index 5eba2b85..27b105bf 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -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) @@ -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 } @@ -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)) diff --git a/storage/storage_mock.go b/storage/storage_mock.go index 7fac1c30..1606bcb9 100644 --- a/storage/storage_mock.go +++ b/storage/storage_mock.go @@ -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() } diff --git a/storage/storage_test.go b/storage/storage_test.go index e540821f..5b15c7c0 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -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")