-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprefixscan.go
108 lines (99 loc) · 2.6 KB
/
prefixscan.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package buckets
import (
"bytes"
"github.com/boltdb/bolt"
)
// A PrefixScanner scans a bucket for keys with a given prefix.
type PrefixScanner struct {
db *DB
BucketName []byte
Prefix []byte
}
// Map applies `do` on each key/value pair for keys with prefix.
func (ps *PrefixScanner) Map(do func(k, v []byte) error) error {
pre := ps.Prefix
return ps.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(ps.BucketName).Cursor()
for k, v := c.Seek(pre); bytes.HasPrefix(k, pre); k, _ = c.Next() {
do(k, v)
}
return nil
})
}
// Count returns a count of the keys with prefix.
func (ps *PrefixScanner) Count() (count int, err error) {
pre := ps.Prefix
err = ps.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(ps.BucketName).Cursor()
for k, _ := c.Seek(pre); bytes.HasPrefix(k, pre); k, _ = c.Next() {
count++
}
return nil
})
if err != nil {
return count, err
}
return count, err
}
// Keys returns a slice of keys with prefix.
func (ps *PrefixScanner) Keys() (keys [][]byte, err error) {
pre := ps.Prefix
err = ps.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(ps.BucketName).Cursor()
for k, _ := c.Seek(pre); bytes.HasPrefix(k, pre); k, _ = c.Next() {
keys = append(keys, k)
}
return nil
})
if err != nil {
return nil, err
}
return keys, err
}
// Values returns a slice of values for keys with prefix.
func (ps *PrefixScanner) Values() (values [][]byte, err error) {
pre := ps.Prefix
err = ps.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(ps.BucketName).Cursor()
for k, v := c.Seek(pre); bytes.HasPrefix(k, pre); k, v = c.Next() {
values = append(values, v)
}
return nil
})
if err != nil {
return nil, err
}
return values, err
}
// Items returns a slice of key/value pairs for keys with prefix.
func (ps *PrefixScanner) Items() (items []Item, err error) {
pre := ps.Prefix
err = ps.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(ps.BucketName).Cursor()
for k, v := c.Seek(pre); bytes.HasPrefix(k, pre); k, v = c.Next() {
items = append(items, Item{k, v})
}
return nil
})
if err != nil {
return nil, err
}
return items, err
}
// ItemMapping returns a map of key/value pairs for keys with prefix.
// This only works with buckets whose keys are byte-sliced strings.
func (ps *PrefixScanner) ItemMapping() (map[string][]byte, error) {
pre := ps.Prefix
items := make(map[string][]byte)
err := ps.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(ps.BucketName).Cursor()
for k, v := c.Seek(pre); bytes.HasPrefix(k, pre); k, v = c.Next() {
items[string(k)] = v
}
return nil
})
if err != nil {
return nil, err
}
return items, err
}