forked from lovoo/goka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
55 lines (45 loc) · 1.17 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
51
52
53
54
55
package storage
import (
"github.com/syndtr/goleveldb/leveldb"
ldbiter "github.com/syndtr/goleveldb/leveldb/iterator"
)
// iterator wraps an Iterator implementation and handles the value decoding and
// offset key skipping.
type iterator struct {
iter ldbiter.Iterator
snap *leveldb.Snapshot
}
// Next advances the iterator to the next key.
func (i *iterator) Next() bool {
next := i.iter.Next()
if string(i.iter.Key()) == offsetKey {
next = i.iter.Next()
}
return next
}
// Err should be called after Next returns false to check for possible
// iteration errors.
func (i *iterator) Err() error {
return i.iter.Error()
}
// Key returns the current key.
func (i *iterator) Key() []byte {
return i.iter.Key()
}
// Value returns the current value decoded by the codec of the storage.
func (i *iterator) Value() ([]byte, error) {
data := i.iter.Value()
if data == nil {
return nil, nil
}
return data, nil
}
// Releases releases the iterator and the associated snapshot. The iterator is
// not usable anymore after calling Release.
func (i *iterator) Release() {
i.iter.Release()
i.snap.Release()
}
func (i *iterator) Seek(key []byte) bool {
return i.iter.Seek(key)
}