-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
get.go
324 lines (278 loc) · 7.55 KB
/
get.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package ds
import (
"encoding/binary"
"fmt"
"sort"
"go.etcd.io/bbolt"
)
// GetOptions describes options for getting entries from a DS table
type GetOptions struct {
// Should the results be sorted. Does nothing for unsorted tables
Sorted bool
// If results are to be sorted, should they be from most recent to oldest (true) or invese (false)
Ascending bool
// The maximum number of entries to return. 0 means unlimited
Max int
}
// Get will get a single entry by its primary key. Returns (nil, nil) if nothing found.
func (table *Table) Get(primaryKey interface{}) (interface{}, error) {
table.lock.RLock()
defer table.lock.RUnlock()
if primaryKey == nil {
return nil, nil
}
primaryKeyBytes, err := gobEncode(primaryKey)
if err != nil {
table.log.Error("Error encoding primary key: %s", err.Error())
return nil, err
}
return table.getPrimaryKey(primaryKeyBytes)
}
func (table *Table) getPrimaryKey(key []byte) (interface{}, error) {
var data []byte
err := table.data.View(func(tx *bbolt.Tx) error {
dataBucket := tx.Bucket(dataKey)
data = dataBucket.Get(key)
return nil
})
if err != nil {
return nil, err
}
if data == nil {
return nil, nil
}
value, err := table.gobDecodeValue(data)
if err != nil {
table.log.Error("Error decoding value: %s", err.Error())
return nil, err
}
return value.Interface(), nil
}
// GetIndex will get multiple entries that contain the same value for the specified indexed field.
// Result is not ordered. Use GetIndexSorted to return a sorted slice.
// Returns an empty array if nothing found.
func (table *Table) GetIndex(fieldName string, value interface{}, options *GetOptions) ([]interface{}, error) {
table.lock.RLock()
defer table.lock.RUnlock()
if !table.IsIndexed(fieldName) {
table.log.Error("Field '%s' is not indexed", fieldName)
return nil, fmt.Errorf("field '%s' is not indexed", fieldName)
}
o := GetOptions{}
if options != nil {
o = *options
}
if table.options.DisableSorting && o.Sorted {
table.log.Warn("Requested sorted results from unsorted table")
o.Sorted = false
}
indexValueBytes, err := gobEncode(value)
if err != nil {
table.log.Error("Error encoding index value: %s", err.Error())
return nil, err
}
table.log.Debug("Get Index(%s) == %x", fieldName, indexValueBytes)
var primaryKeysData []byte
err = table.data.View(func(tx *bbolt.Tx) error {
indexBucket := tx.Bucket([]byte(indexPrefix + fieldName))
primaryKeysData = indexBucket.Get(indexValueBytes)
return nil
})
if err != nil {
return nil, err
}
if primaryKeysData == nil {
table.log.Debug("Index value returned no primary keys")
return []interface{}{}, nil
}
keys, err := gobDecodePrimaryKeyList(primaryKeysData)
if err != nil {
table.log.Error("Error decoding primary key list: %s", err.Error())
return nil, err
}
table.log.Debug("Keys matching Index(%s) == %x: %d", fieldName, indexValueBytes, len(keys))
if o.Sorted {
return table.getIndexSorted(keys, o)
}
return table.getIndexUnsorted(keys, o)
}
func (table *Table) getIndexUnsorted(keys [][]byte, options GetOptions) ([]interface{}, error) {
length := len(keys)
if options.Max > 0 && length > options.Max {
length = options.Max
}
var values = make([]interface{}, length)
for i, key := range keys {
if i >= length {
break
}
v, err := table.getPrimaryKey(key)
if err != nil {
table.log.Error("Error getting object: %s", err.Error())
return nil, err
}
values[i] = v
}
return values, nil
}
func (table *Table) getIndexSorted(keys [][]byte, options GetOptions) ([]interface{}, error) {
orderMap := map[uint64][]byte{}
err := table.data.View(func(tx *bbolt.Tx) error {
for _, key := range keys {
index := table.indexForPrimaryKey(tx, key)
if index == nil {
return fmt.Errorf("no index found")
}
orderMap[*index] = key
}
return nil
})
if err != nil {
return nil, err
}
// To store the keys in slice in sorted order
var indexes []uint64
for k := range orderMap {
indexes = append(indexes, k)
}
sort.SliceStable(indexes, func(i int, j int) bool {
if options.Ascending {
return indexes[i] > indexes[j]
}
return indexes[i] < indexes[j]
})
length := len(keys)
if options.Max > 0 && length > options.Max {
length = options.Max
}
var sortedObject = make([]interface{}, length)
for i, key := range indexes {
if i >= length {
break
}
primaryKey := orderMap[key]
o, err := table.getPrimaryKey(primaryKey)
if err != nil {
return nil, err
}
sortedObject[i] = o
}
return sortedObject, nil
}
// GetUnique will get a single entry based on the value of the provided unique field.
// Returns (nil, nil) if nothing found.
func (table *Table) GetUnique(fieldName string, value interface{}) (interface{}, error) {
table.lock.RLock()
defer table.lock.RUnlock()
if !table.IsUnique(fieldName) {
table.log.Error("Field '%s' is not unique", fieldName)
return nil, fmt.Errorf("field '%s' is not unique", fieldName)
}
uniqueValueBytes, err := gobEncode(value)
if err != nil {
table.log.Error("Error encoding unique value: %s", err.Error())
return nil, err
}
var primaryKeyData []byte
err = table.data.View(func(tx *bbolt.Tx) error {
uniqueBucket := tx.Bucket([]byte(uniquePrefix + fieldName))
primaryKeyData = uniqueBucket.Get(uniqueValueBytes)
return nil
})
if err != nil {
return nil, err
}
if primaryKeyData == nil {
table.log.Debug("Unique value returned no primary key")
return nil, nil
}
return table.getPrimaryKey(primaryKeyData)
}
// GetAll will get all of the entries in the table.
func (table *Table) GetAll(options *GetOptions) ([]interface{}, error) {
table.lock.RLock()
defer table.lock.RUnlock()
o := GetOptions{}
if options != nil {
o = *options
}
if o.Sorted && table.options.DisableSorting {
table.log.Warn("Requested sorted results from unsorted table")
o.Sorted = false
}
if o.Sorted {
return table.getAllSorted(o)
}
return table.getAllUnsorted(o)
}
func (table *Table) getAllUnsorted(options GetOptions) ([]interface{}, error) {
var entires []interface{}
i := 0
err := table.data.View(func(tx *bbolt.Tx) error {
dataBucket := tx.Bucket(dataKey)
return dataBucket.ForEach(func(k []byte, v []byte) error {
i++
if options.Max > 0 && i > options.Max {
return nil
}
value, err := table.gobDecodeValue(v)
if err != nil {
table.log.Error("Error decoding value: %s", err.Error())
return err
}
entires = append(entires, value.Interface())
return nil
})
})
if err != nil {
return nil, err
}
return entires, nil
}
func (table *Table) getAllSorted(options GetOptions) ([]interface{}, error) {
// Map index to primary key
orderMap := map[uint64][]byte{}
err := table.data.View(func(tx *bbolt.Tx) error {
bucket := tx.Bucket(insertOrderKey)
return bucket.ForEach(func(k []byte, v []byte) error {
primaryKey := k
index := binary.LittleEndian.Uint64(v)
orderMap[index] = primaryKey
return nil
})
})
if err != nil {
return nil, err
}
if len(orderMap) == 0 {
return []interface{}{}, nil
}
// To store the keys in slice in sorted order
var keys []uint64
for k := range orderMap {
keys = append(keys, k)
}
sort.SliceStable(keys, func(i int, j int) bool {
if options.Ascending {
return keys[i] > keys[j]
}
return keys[i] < keys[j]
})
length := len(keys)
if options.Max > 0 && length > options.Max {
length = options.Max
}
objects := make([]interface{}, length)
for i, index := range keys {
if i >= length {
break
}
primaryKey := orderMap[index]
object, err := table.getPrimaryKey(primaryKey)
if err != nil {
return nil, err
}
objects[i] = object
}
return objects, nil
}