-
Notifications
You must be signed in to change notification settings - Fork 21
/
keyvalues.go
149 lines (126 loc) · 3.53 KB
/
keyvalues.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
//
// Wraps boltdb with methods that assume strings for the keys and values
//
import (
"bytes"
"errors"
"fmt"
"log"
"time"
"github.com/boltdb/bolt"
)
type KeyValueStore struct {
db *bolt.DB
}
type KeyValue struct {
Key string
Value string
}
const (
bucket = "MyBucket"
)
func main() {
db := NewKeyValueStore("bolt.db.deleteme")
err := db.CreateBucket(bucket)
if err != nil {
fmt.Printf("Error creating bucket: %s", err)
}
// Put a string in the bucket
err = db.PutString(bucket, "answer", "42")
if err != nil {
fmt.Printf("Error putting to bucket: %s", err)
}
// Get a known value
var value string
value, err = db.GetString(bucket, "answer")
if err != nil {
fmt.Printf("Error getting from bucket: %s\n", err)
}
fmt.Println("The answer is", value)
// Get a known missing value
value, err = db.GetString(bucket, "bogus-key")
if err != nil {
if err.Error() == "DoesNotExist" {
fmt.Printf("Good work. Bucket %s does not contain key %s\n", bucket, "bogus-key")
} else {
fmt.Printf("Error getting from bucket: %s\n", err)
}
}
// Put a string in the bucket
if err := db.PutString(bucket, "jon1", "this is jon1"); err != nil {
fmt.Printf("Error %s\n", err)
}
if err := db.PutString(bucket, "jon2", "this is jon2"); err != nil {
fmt.Printf("Error %s\n", err)
}
if err := db.PutString(bucket, "jon3", "this is jon3"); err != nil {
fmt.Printf("Error %s\n", err)
}
if err := db.PutString(bucket, "jon3", "this was jon3"); err != nil {
fmt.Printf("Error %s\n", err)
}
kvs, err := db.GetKeyValues(bucket, "jon")
if err != nil {
fmt.Printf("Error getting from bucket: %s\n", err)
}
fmt.Printf("Pulled %d key values with prefix jon\n", len(kvs))
for _, kv := range kvs {
fmt.Printf("Key: %s Value: %s\n", kv.Key, kv.Value)
}
}
func NewKeyValueStore(fileName string) *KeyValueStore {
boltdb, err := bolt.Open(fileName, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
log.Fatal(err)
}
return &KeyValueStore{db: boltdb}
}
func (kvs *KeyValueStore) CreateBucket(name string) error {
return kvs.db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(bucket))
return err
})
}
// PutString is a boltdb wrapper that assumes both the key and value are strings
func (kvs *KeyValueStore) PutString(bucket string, key string, value string) error {
return kvs.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
err := b.Put([]byte(key), []byte(value))
return err
})
}
// GetString is a boltdb wrapper that assumes both the key and value are strings
func (kvs *KeyValueStore) GetString(bucket string, key string) (string, error) {
var value string
err := kvs.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
v := b.Get([]byte(key))
if v == nil {
return errors.New("DoesNotExist")
}
value = string(v)
return nil
})
if err != nil {
return "", err
}
return value, nil
}
// GetKeyValues returns the keys that match the given prefix (with their values)
func (kvs *KeyValueStore) GetKeyValues(bucket string, keyPrefix string) ([]KeyValue, error) {
var returnValue = []KeyValue{}
// Partial key match example
err := kvs.db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
c := tx.Bucket([]byte(bucket)).Cursor()
prefix := []byte(keyPrefix)
for k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {
kv := KeyValue{string(k), string(v)}
//fmt.Printf("key=%s, value=%s\n", k, v)
returnValue = append(returnValue, kv)
}
return nil
})
return returnValue, err
}