-
Notifications
You must be signed in to change notification settings - Fork 7
/
convenience.go
206 lines (179 loc) · 5.04 KB
/
convenience.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package gophia
import (
"bytes"
"encoding/gob"
"errors"
)
// CursorS returns a Cursor that fetches rows from the database
// from the given key, passed as a string.
// Callers must call Close() on the received Cursor.
func (db *Database) CursorS(order Order, key string) (*Cursor, error) {
return db.Cursor(order, []byte(key))
}
// DeleteS deletes the key from the database.
func (db *Database) DeleteS(key string) error {
return db.Delete([]byte(key))
}
// Each iterates through the key-values in the database, passing each to the each function.
// It is a convenience wrapper around a Cursor iteration.
func (db *Database) Each(order Order, key []byte, each func(key []byte, value []byte)) error {
cur, err := db.Cursor(order, key)
defer cur.Close()
if nil != err {
return err
}
for cur.Fetch() {
each(cur.Key(), cur.Value())
}
return nil
}
// GetAO returns on object value for a byte-array key.
func (db *Database) GetAO(key []byte, out interface{}) error {
buf, err := db.Get(key)
if nil != err {
return err
}
dec := gob.NewDecoder(bytes.NewReader(buf))
return dec.Decode(out)
}
// GetS retrieves an array value for a string key. It is a convenience
// method to simplify working with string keys.
func (db *Database) GetSA(key string) ([]byte, error) {
return db.Get([]byte(key))
}
// GetSO fetches a gob encoded object for a string key.
func (db *Database) GetSO(key string, out interface{}) error {
return db.GetAO([]byte(key), out)
}
// GetSS returns a string value for a string key.
func (db *Database) GetSS(key string) (string, error) {
v, err := db.Get([]byte(key))
if nil != err {
return "", err
}
return string(v), nil
}
// HasS returns true if the database has a value for the string key.
func (db *Database) HasS(key string) (bool, error) {
return db.Has([]byte(key))
}
// KeyLen returns the length of the current key. It is
// a synonym for KeySize()
func (cur *Cursor) KeyLen() int {
return cur.KeySize()
}
// KeyS returns the current key as a string.
func (cur *Cursor) KeyS() string {
return string(cur.Key())
}
// MustHas returns true if the key exists, false otherwise. It panics
// in the even of error.
func (db *Database) MustHas(key []byte) bool {
has, err := db.Has(key)
if nil != err {
panic(err)
}
return has
}
// MustGet returns the value of the key, or panics on an error.
func (db *Database) MustGet(key []byte) []byte {
val, err := db.Get(key)
if nil != err {
panic(err)
}
return val
}
// MustGetSA returns the byte array value for the string key. It panics on
// error.
func (db *Database) MustGetSA(key string) []byte {
value, err := db.Get([]byte(key))
if nil != err {
panic(err)
}
return value
}
// MustGetString returns the byte array value for the string key. It panics
// on error.
// @deprecated Use MustGetSA instead.
func (db *Database) MustGetString(key string) []byte {
return db.MustGetSA(key)
}
// MustGetSS returns the string value for a string key. It panics
// on an error.
func (db *Database) MustGetSS(key string) string {
value, err := db.Get([]byte(key))
if nil != err {
panic(err)
}
return string(value)
}
// MustHasS returns true if the string exists, or false if it does not. It panics
// in the event of error.
func (db *Database) MustHasS(key string) bool {
return db.MustHas([]byte(key))
}
// Next is identical to Fetch. It exists because it
// seems that Next() is more go-idiomatic.
func (cur *Cursor) Next() bool {
return cur.Fetch()
}
// Open opens the database with the given access permissions in the given directory.
func Open(access Access, directory string) (*Database, error) {
env, err := NewEnvironment()
if nil != err {
return nil, err
}
err = env.Dir(access, directory)
if nil != err {
env.Close()
return nil, err
}
db, err := env.Open()
if nil != err {
env.Close()
return nil, err
}
db.env = env
return db, nil
}
// SetAO sets a byte array key to an object value.
func (db *Database) SetAO(key []byte, value interface{}) error {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(value)
if nil != err {
return err
}
return db.Set(key, buf.Bytes())
}
// SetSA sets a string key to a byte array value.
func (db *Database) SetSA(key string, value []byte) error {
return db.Set([]byte(key), value)
}
// SetSS sets a string key to a string value
func (db *Database) SetSS(key, value string) error {
return db.Set([]byte(key), []byte(value))
}
// SetSO sets a string key to an object value.
func (db *Database) SetSO(key string, value interface{}) error {
return db.SetAO([]byte(key), value)
}
// ValueLen returns the length of the current value. It is
// a synonym for ValueSize()
func (cur *Cursor) ValueLen() int {
return cur.ValueSize()
}
// ValueO returns the current value as an object, by gob decoding
// the current value at the cursor.
func (cur *Cursor) ValueO(out interface{}) error {
buf := cur.Value()
if nil == buf {
return errors.New("Value is nil")
}
dec := gob.NewDecoder(bytes.NewReader(buf))
return dec.Decode(out)
}
// ValueS returns the current value as a string.
func (cur *Cursor) ValueS() string {
return string(cur.Value())
}