forked from lovoo/goka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.go
49 lines (41 loc) · 991 Bytes
/
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
package goka
import (
"github.com/lovoo/goka/storage"
)
// Iterator allows one to iterate over the keys of a view.
type Iterator interface {
Next() bool
Key() string
Value() (interface{}, error)
Release()
Seek(key string) bool
}
type iterator struct {
iter storage.Iterator
codec Codec
}
// Next advances the iterator to the next key.
func (i *iterator) Next() bool {
return i.iter.Next()
}
// Key returns the current key.
func (i *iterator) Key() string {
return string(i.iter.Key())
}
// Value returns the current value decoded by the codec of the storage.
func (i *iterator) Value() (interface{}, error) {
data, err := i.iter.Value()
if err != nil {
return nil, err
} else if data == nil {
return nil, nil
}
return i.codec.Decode(data)
}
// Releases releases the iterator. The iterator is not usable anymore after calling Release.
func (i *iterator) Release() {
i.iter.Release()
}
func (i *iterator) Seek(key string) bool {
return i.iter.Seek([]byte(key))
}