-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
68 lines (57 loc) · 1.38 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
56
57
58
59
60
61
62
63
64
65
66
67
68
package yojoudb
import (
"github.com/berylyvos/yojoudb/meta"
)
// Iterator is the iterator of db based on IndexIter.
type Iterator struct {
IndexIter meta.Iterator
db *DB
}
func (db *DB) NewIterator(options IteratorOptions) *Iterator {
return &Iterator{
db: db,
IndexIter: db.index.Iterator(meta.IteratorOpt{
Prefix: options.Prefix,
Reverse: options.Reverse,
}),
}
}
// Rewind seeks the first key in the iterator.
func (it *Iterator) Rewind() {
it.IndexIter.Rewind()
}
// Seek moves the iterator to the key which is
// greater(or less when reverse) than or equal
// to the specified key.
func (it *Iterator) Seek(key []byte) {
it.IndexIter.Seek(key)
}
// Next moves the iterator to the next key.
func (it *Iterator) Next() {
it.IndexIter.Next()
}
// Key gets the current key in the iterator.
func (it *Iterator) Key() []byte {
return it.IndexIter.Key()
}
// Value gets the current val in the iterator.
func (it *Iterator) Value() ([]byte, error) {
loc := it.IndexIter.Value()
chunk, err := it.db.dataFiles.Read(loc)
if err != nil {
return nil, err
}
record := decodeLR(chunk)
if record.Type == LRDeleted {
return nil, ErrKeyNotFound
}
return record.Val, nil
}
// Valid checks if the iterator is in valid position.
func (it *Iterator) Valid() bool {
return it.IndexIter.Valid()
}
// Close closes the iterator.
func (it *Iterator) Close() {
it.IndexIter.Close()
}