-
Notifications
You must be signed in to change notification settings - Fork 5
/
iterator.go
50 lines (42 loc) · 1.08 KB
/
iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package store
import (
"io"
"github.com/ipld/go-storethehash/store/index"
)
// Iterator iterates keys and values. Any write to the store potentially
// invalidates the iterator and may cause values to be missed or seen again.
type Iterator struct {
index *index.Index
indexIter *index.Iterator
}
// NewIterator creates a new store iterator.
func (s *Store) NewIterator() *Iterator {
_ = s.Flush()
return &Iterator{
index: s.index,
indexIter: s.index.NewIterator(),
}
}
// Next returns the next key and value. Returns io.EOF error when done.
func (it *Iterator) Next() ([]byte, []byte, error) {
for {
rec, done, err := it.indexIter.Next()
if err != nil {
return nil, nil, err
}
if done {
return nil, nil, io.EOF
}
// Get the key and value stored in primary to see if it is the same
// (index only stores prefixes).
key, value, err := it.index.Primary.Get(rec.Block)
if err != nil || key == nil {
// Record no longer there, skip.
continue
}
return key, value, nil
}
}
func (it *Iterator) Progress() float64 {
return it.indexIter.Progress()
}