-
Notifications
You must be signed in to change notification settings - Fork 25
/
iterator.go
68 lines (55 loc) · 1.25 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 CouloyDB
import (
"bytes"
"github.com/Kirov7/CouloyDB/meta"
)
type Iterator struct {
options IteratorOptions
IndexIterator meta.Iterator
db *DB
}
func (db *DB) NewIterator(options IteratorOptions) *Iterator {
iterator := db.index.getStrIndex().Iterator(options.Reverse)
return &Iterator{
IndexIterator: iterator,
db: db,
options: options,
}
}
func (it *Iterator) Rewind() {
it.IndexIterator.Rewind()
it.skipToNext()
}
func (it *Iterator) Seek(key []byte) {
it.IndexIterator.Seek(key)
it.skipToNext()
}
func (it *Iterator) Next() {
it.IndexIterator.Next()
it.skipToNext()
}
func (it *Iterator) Valid() bool {
return it.IndexIterator.Valid()
}
func (it *Iterator) Key() []byte {
return it.IndexIterator.Key()
}
func (it *Iterator) Value() ([]byte, error) {
logRecordPos := it.IndexIterator.Value()
return it.db.getValueByPos(logRecordPos)
}
func (it *Iterator) Close() {
it.IndexIterator.Close()
}
func (it *Iterator) skipToNext() {
prefixLen := len(it.options.Prefix)
if prefixLen == 0 {
return
}
for ; it.IndexIterator.Valid(); it.IndexIterator.Next() {
key := it.IndexIterator.Key()
if prefixLen <= len(key) && bytes.Compare(it.options.Prefix, key[:prefixLen]) == 0 {
break
}
}
}