/
collection.go
351 lines (293 loc) · 8.19 KB
/
collection.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* go-leia
* Copyright (C) 2021 Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package leia
import (
"crypto/sha1"
"errors"
"go.etcd.io/bbolt"
)
// ErrNoIndex is returned when no index is found to query against
var ErrNoIndex = errors.New("no index found")
// DocumentWalker defines a function that is used as a callback for matching documents.
// The key will be the document Reference (hash) and the value will be the raw document bytes
type DocumentWalker func(key Reference, value []byte) error
// documentCollection is the bucket that stores all the documents for a collection
const documentCollection = "_documents"
func documentCollectionByteRef() []byte {
return []byte(documentCollection)
}
// Collection defines a logical collection of documents and indices within a store.
type Collection interface {
// AddIndex to this collection. It doesn't matter if the index already exists.
// If you want to override an index (by name) drop it first.
AddIndex(index ...Index) error
// DropIndex by name
DropIndex(name string) error
// Add a set of documents to this collection
Add(jsonSet []Document) error
// Get returns the data for the given key or nil if not found
Get(ref Reference) (*Document, error)
// Delete a document
Delete(doc Document) error
// Find queries the collection for documents
// returns ErrNoIndex when no suitable index can be found
Find(query Query) ([]Document, error)
// Reference uses the configured reference function to generate a reference of the function
Reference(doc Document) Reference
// Iterate over documents that match the given query
Iterate(query Query, walker DocumentWalker) error
// IndexIterate is used for iterating over indexed values. The query keys must match exactly with all the FieldIndexer.Name() of an index
// returns ErrNoIndex when no suitable index can be found
IndexIterate(query Query, fn ReferenceScanFn) error
}
// ReferenceFunc is the func type used for creating references.
// references are the key under which a document is stored.
// a ReferenceFunc could be the sha256 func or something that stores document in chronological order.
// The first would be best for random access, the latter for chronological access
type ReferenceFunc func(doc Document) Reference
// default for shasum docs
func defaultReferenceCreator(doc Document) Reference {
s := sha1.Sum(doc.raw)
var b = make([]byte, len(s))
copy(b, s[:])
return b
}
type collection struct {
Name string `json:"name"`
db *bbolt.DB
IndexList []Index `json:"indices"`
refMake ReferenceFunc
}
func (c *collection) AddIndex(indexes ...Index) error {
for _, index := range indexes {
for _, i := range c.IndexList {
if i.Name() == index.Name() {
return nil
}
}
if err := c.db.Update(func(tx *bbolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(c.Name))
if err != nil {
return err
}
// skip existing
if b := bucket.Bucket(index.BucketName()); b != nil {
return nil
}
gBucket, err := bucket.CreateBucketIfNotExists(documentCollectionByteRef())
if err != nil {
return err
}
cur := gBucket.Cursor()
for ref, doc := cur.First(); ref != nil; ref, doc = cur.Next() {
index.Add(bucket, ref, Document{raw: doc})
}
return nil
}); err != nil {
return err
}
c.IndexList = append(c.IndexList, index)
}
return nil
}
func (c *collection) DropIndex(name string) error {
return c.db.Update(func(tx *bbolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(c.Name))
if err != nil {
return err
}
var newIndices = make([]Index, len(c.IndexList))
j := 0
for _, i := range c.IndexList {
if name == i.Name() {
bucket.DeleteBucket(i.BucketName())
} else {
newIndices[j] = i
j++
}
}
c.IndexList = newIndices[:j]
return nil
})
}
func (c *collection) Reference(doc Document) Reference {
return c.refMake(doc)
}
// Add a json document set to the store
// this uses a single transaction per set.
func (c *collection) Add(jsonSet []Document) error {
return c.db.Update(func(tx *bbolt.Tx) error {
return c.add(tx, jsonSet)
})
}
func (c *collection) add(tx *bbolt.Tx, jsonSet []Document) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(c.Name))
if err != nil {
return err
}
docBucket, err := bucket.CreateBucketIfNotExists(documentCollectionByteRef())
if err != nil {
return err
}
for _, doc := range jsonSet {
ref := c.refMake(doc)
// indices
// buckets are cached within tx
for _, i := range c.IndexList {
err = i.Add(bucket, ref, doc)
if err != nil {
return err
}
}
err = docBucket.Put(ref, doc.raw)
if err != nil {
return err
}
}
return nil
}
func (c *collection) Find(query Query) ([]Document, error) {
docs := make([]Document, 0)
walker := func(key Reference, value []byte) error {
docs = append(docs, DocumentFromBytes(value))
return nil
}
if err := c.Iterate(query, walker); err != nil {
return nil, err
}
return docs, nil
}
func (c *collection) Iterate(query Query, fn DocumentWalker) error {
plan, err := c.queryPlan(query)
if err != nil {
return err
}
if err = plan.execute(fn); err != nil {
return err
}
return nil
}
// IndexIterate uses a query to loop over all keys and Entries in an index. It skips the resultScan and collect phase
func (c *collection) IndexIterate(query Query, fn ReferenceScanFn) error {
index := c.findIndex(query)
if index == nil {
return ErrNoIndex
}
plan := indexScanQueryPlan{
queryPlanBase: queryPlanBase{
collection: c,
query: query,
},
index: index,
}
return plan.execute(fn)
}
// Delete a document from the store, this also removes the entries from indices
func (c *collection) Delete(doc Document) error {
// find matching indices and remove hash from that index
return c.db.Update(func(tx *bbolt.Tx) error {
return c.delete(tx, doc)
})
}
func (c *collection) delete(tx *bbolt.Tx, doc Document) error {
bucket := tx.Bucket([]byte(c.Name))
if bucket == nil {
return nil
}
ref := c.refMake(doc)
docBucket := c.documentBucket(tx)
if docBucket == nil {
return nil
}
err := docBucket.Delete(ref)
if err != nil {
return err
}
// indices
for _, i := range c.IndexList {
err = i.Delete(bucket, ref, doc)
if err != nil {
return err
}
}
return nil
}
func (c *collection) queryPlan(query Query) (queryPlan, error) {
if query == nil {
return nil, ErrNoQuery
}
index := c.findIndex(query)
if index == nil {
return fullTableScanQueryPlan{
queryPlanBase: queryPlanBase{
collection: c,
query: query,
},
}, nil
}
return resultScanQueryPlan{
queryPlanBase: queryPlanBase{
collection: c,
query: query,
},
index: index,
}, nil
}
// find a matching index.
// The index may, at most, be one longer than the number of search options.
// The longest index will win.
func (c *collection) findIndex(query Query) Index {
if query == nil {
return nil
}
// first map the indices to the number of matching search options
var cIndex Index
var cMatch float64
for _, i := range c.IndexList {
m := i.IsMatch(query)
if m > cMatch {
cIndex = i
cMatch = m
}
}
return cIndex
}
func (c *collection) Get(key Reference) (*Document, error) {
var err error
var data []byte
err = c.db.View(func(tx *bbolt.Tx) error {
bucket := c.documentBucket(tx)
if bucket == nil {
return nil
}
data = bucket.Get(key)
return nil
})
if data == nil {
return nil, nil
}
return &Document{raw: data}, err
}
func (c *collection) documentBucket(tx *bbolt.Tx) *bbolt.Bucket {
bucket := tx.Bucket([]byte(c.Name))
if bucket == nil {
return nil
}
return bucket.Bucket(documentCollectionByteRef())
}