Skip to content

Commit ef995d4

Browse files
committed
address review comments
1 parent 88d86d2 commit ef995d4

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

storage/operation/badgerimpl/iterator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ func newBadgerIterator(db *badger.DB, startPrefix, endPrefix []byte, ops storage
3737
}
3838

3939
// First seeks to the smallest key greater than or equal to the given key.
40-
func (i *badgerIterator) First() {
40+
func (i *badgerIterator) First() bool {
4141
i.iter.Seek(i.lowerBound)
42+
return i.Valid()
4243
}
4344

4445
// Valid returns whether the iterator is positioned at a valid key-value pair.

storage/operation/pebbleimpl/iterator.go

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
type pebbleIterator struct {
12-
iter *pebble.Iterator
12+
*pebble.Iterator
1313
}
1414

1515
var _ storage.Iterator = (*pebbleIterator)(nil)
@@ -33,56 +33,36 @@ func newPebbleIterator(reader pebble.Reader, startPrefix, endPrefix []byte, ops
3333
}
3434

3535
return &pebbleIterator{
36-
iter: iter,
36+
iter,
3737
}, nil
3838
}
3939

40-
// First seeks to the smallest key greater than or equal to the given key.
41-
func (i *pebbleIterator) First() {
42-
i.iter.First()
43-
}
44-
45-
// Valid returns whether the iterator is positioned at a valid key-value pair.
46-
func (i *pebbleIterator) Valid() bool {
47-
return i.iter.Valid()
40+
// IterItem returns the current key-value pair, or nil if done.
41+
func (i *pebbleIterator) IterItem() storage.IterItem {
42+
return pebbleIterItem{i.Iterator}
4843
}
4944

50-
// Next advances the iterator to the next key-value pair.
45+
// Next seeks to the smallest key greater than or equal to the given key.
5146
func (i *pebbleIterator) Next() {
52-
i.iter.Next()
53-
}
54-
55-
// IterItem returns the current key-value pair, or nil if done.
56-
func (i *pebbleIterator) IterItem() storage.IterItem {
57-
return pebbleIterItem{iter: i.iter}
47+
i.Iterator.Next()
5848
}
5949

6050
type pebbleIterItem struct {
61-
iter *pebble.Iterator
51+
*pebble.Iterator
6252
}
6353

6454
var _ storage.IterItem = (*pebbleIterItem)(nil)
6555

66-
func (i pebbleIterItem) Key() []byte {
67-
return i.iter.Key()
68-
}
69-
7056
// KeyCopy returns a copy of the key of the item, writing it to dst slice.
7157
func (i pebbleIterItem) KeyCopy(dst []byte) []byte {
72-
return append(dst[:0], i.iter.Key()...)
58+
return append(dst[:0], i.Key()...)
7359
}
7460

7561
func (i pebbleIterItem) Value(fn func([]byte) error) error {
76-
val, err := i.iter.ValueAndErr()
62+
val, err := i.ValueAndErr()
7763
if err != nil {
7864
return err
7965
}
8066

8167
return fn(val)
8268
}
83-
84-
// Close closes the iterator. Iterator must be closed, otherwise it causes memory leak.
85-
// No errors expected during normal operation
86-
func (i *pebbleIterator) Close() error {
87-
return i.iter.Close()
88-
}

storage/operation/reads.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func IterateKeysByPrefixRange(r storage.Reader, startPrefix []byte, endPrefix []
4545
}
4646
return false, nil
4747
}, nil, nil
48-
}, storage.IteratorOption{IterateKeyOnly: true})
48+
}, storage.IteratorOption{BadgerIterateKeyOnly: true})
4949
}
5050

5151
// IterateKey will iterate over all entries in the database, where the key starts with a prefixes in

storage/operation/reads_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,43 @@ import (
1212
"github.com/onflow/flow-go/storage/operation/dbtest"
1313
)
1414

15+
func TestFirst(t *testing.T) {
16+
dbtest.RunWithStorages(t, func(t *testing.T, r storage.Reader, withWriter dbtest.WithWriter) {
17+
18+
// Create a range of keys around the prefix start/end values
19+
keys := [][]byte{
20+
{0x10, 0x00},
21+
{0x10, 0xff},
22+
}
23+
24+
// Insert the keys into the storage
25+
require.NoError(t, withWriter(func(writer storage.Writer) error {
26+
for _, key := range keys {
27+
value := []byte{0x00} // value are skipped, doesn't matter
28+
err := operation.Upsert(key, value)(writer)
29+
if err != nil {
30+
return err
31+
}
32+
}
33+
return nil
34+
}))
35+
36+
iter, err := r.NewIter([]byte{0x20}, []byte{0x30}, storage.DefaultIteratorOptions())
37+
require.NoError(t, err)
38+
39+
// Check that the iterator is at the first key and return false when matching no key
40+
require.False(t, iter.First())
41+
require.NoError(t, iter.Close())
42+
43+
iter, err = r.NewIter([]byte{0x10}, []byte{0x30}, storage.DefaultIteratorOptions())
44+
require.NoError(t, err)
45+
46+
// Check that the iterator is at the first key and return true when matching the first key
47+
require.True(t, iter.First())
48+
require.NoError(t, iter.Close())
49+
})
50+
}
51+
1552
func TestIterateKeysInPrefixRange(t *testing.T) {
1653
dbtest.RunWithStorages(t, func(t *testing.T, r storage.Reader, withWriter dbtest.WithWriter) {
1754
// Define the prefix range

storage/operations.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ type Iterator interface {
1717
// This method must be called because it's necessary for the badger implementation
1818
// to move the iteration cursor to the first key in the iteration range.
1919
// This method must be called before calling Valid, Next, IterItem, or Close.
20-
First()
20+
// return true if the iterator is pointing to a valid key-value pair after calling First,
21+
// return false otherwise.
22+
First() bool
2123

2224
// Valid returns whether the iterator is positioned at a valid key-value pair.
2325
// If Valid returns false, the iterator is done and must be closed.

0 commit comments

Comments
 (0)