-
Notifications
You must be signed in to change notification settings - Fork 2
/
reader.go
38 lines (34 loc) · 1.06 KB
/
reader.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
package nimbusdb
import (
"strings"
"github.com/google/btree"
)
// KeyReader iterates through each key matching given prefix.
// If prefix is an empty string, all keys are matched.
// The second argument is a callback function which contains the key.
func (db *Db) KeyReader(prefix string, handler func(k []byte)) {
db.keyDir.tree.Ascend(func(it btree.Item) bool {
key := it.(*item).key
if strings.HasPrefix(string(key), prefix) {
handler(key)
}
return true
})
}
// KeyValueReader iterates through each key-value pair matching given key's prefix.
// If prefix is an empty string, all key-value pairs are matched.
// The second argument is a callback function which contains key and the value.
func (db *Db) KeyValueReader(keyPrefix string, handler func(k []byte, v []byte)) (bool, error) {
db.keyDir.tree.Ascend(func(it btree.Item) bool {
key := it.(*item).key
if strings.HasPrefix(string(key), keyPrefix) {
v, err := db.getKeyDir(key)
if err != nil {
return false
}
handler(it.(*item).key, v.value)
}
return true
})
return false, nil
}