-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.go
514 lines (437 loc) · 13 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* go-leia
* Copyright (C) 2022 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 (
"context"
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"github.com/piprate/json-gold/ld"
"github.com/tidwall/gjson"
"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 path) drop it first.
AddIndex(index ...Index) error
// DropIndex by path
DropIndex(name string) error
// NewIndex creates a new index from the context of this collection
// If multiple field indexers are given, a compound index is created.
NewIndex(name string, parts ...FieldIndexer) Index
// 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
// returns context errors when the context has been cancelled or deadline has exceeded.
// passing ctx prevents adding too many records to the result set.
Find(ctx context.Context, 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
// ValuesAtPath returns a slice with the values found by the configured valueCollector
ValuesAtPath(document Document, queryPath QueryPath) ([]Scalar, error)
// DocumentCount returns the number of indexed documents
DocumentCount() (int, 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)
var b = make([]byte, len(s))
copy(b, s[:])
return b
}
type collection struct {
name string
db *bbolt.DB
indexList []Index
refMake ReferenceFunc
documentLoader ld.DocumentLoader
collectionType CollectionType
valueCollector valueCollector
}
func (c *collection) NewIndex(name string, parts ...FieldIndexer) Index {
return &index{
name: name,
indexParts: parts,
collection: c,
}
}
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, 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)
if err != nil {
return err
}
}
return nil
}
func (c *collection) Find(ctx context.Context, query Query) ([]Document, error) {
docs := make([]Document, 0)
walker := func(key Reference, value []byte) error {
// stop iteration when needed
if err := ctx.Err(); err != nil {
return err
}
docs = append(docs, 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) {
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 {
// 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 data, err
}
func (c *collection) DocumentCount() (int, error) {
var count int
err := c.db.View(func(tx *bbolt.Tx) error {
bucket := c.documentBucket(tx)
if bucket == nil {
return nil
}
count = bucket.Stats().KeyN
return nil
})
return count, 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())
}
// valueCollector is responsible for going through the document and finding the Scalars that match the Query
type valueCollector func(collection *collection, document Document, queryPath QueryPath) ([]Scalar, error)
// JSONPathValueCollector collects values at a given JSON path expression. Objects are delimited by a dot and lists use an extra # in the expression:
// object.list.#.key
func JSONPathValueCollector(_ *collection, document Document, queryPath QueryPath) ([]Scalar, error) {
jsonPath, ok := queryPath.(jsonPath)
if !ok {
return nil, ErrInvalidQuery
}
if !gjson.ValidBytes(document) {
return nil, ErrInvalidJSON
}
result := gjson.GetBytes(document, string(jsonPath))
return valuesFromResult(result)
}
// JSONLDValueCollector collects values given a list of IRIs that represent the nesting of the objects.
func JSONLDValueCollector(collection *collection, document Document, queryPath QueryPath) ([]Scalar, error) {
iriPath, ok := queryPath.(iriPath)
if !ok {
return nil, ErrInvalidQuery
}
var input interface{}
if err := json.Unmarshal(document, &input); err != nil {
return nil, err
}
options := ld.NewJsonLdOptions("")
options.DocumentLoader = collection.documentLoader
expanded, err := ld.NewJsonLdProcessor().Expand(input, options)
if err != nil {
return nil, err
}
return valuesFromSliceAtPath(expanded, iriPath), nil
}
func valuesFromSliceAtPath(expanded []interface{}, termPath iriPath) []Scalar {
result := make([]Scalar, 0)
for _, sub := range expanded {
switch typedSub := sub.(type) {
case []interface{}:
result = append(result, valuesFromSliceAtPath(typedSub, termPath)...)
case map[string]interface{}:
result = append(result, valuesFromMapAtPath(typedSub, termPath)...)
case string:
result = append(result, MustParseScalar(typedSub))
case bool:
result = append(result, MustParseScalar(typedSub))
case float64:
result = append(result, MustParseScalar(typedSub))
}
}
return result
}
func valuesFromMapAtPath(expanded map[string]interface{}, termPath iriPath) []Scalar {
// JSON-LD in expanded form either has @value, @id, @list or @set
if termPath.IsEmpty() {
if value, ok := expanded["@value"]; ok {
return []Scalar{MustParseScalar(value)}
}
if id, ok := expanded["@id"]; ok {
return []Scalar{MustParseScalar(id)}
}
if list, ok := expanded["@list"]; ok {
castList := list.([]interface{})
return valuesFromSliceAtPath(castList, termPath)
}
}
if list, ok := expanded["@list"]; ok {
castList := list.([]interface{})
return valuesFromSliceAtPath(castList, termPath)
}
if value, ok := expanded[termPath.Head()]; ok {
// the value should now be a slice
next, ok := value.([]interface{})
if !ok {
return nil
}
return valuesFromSliceAtPath(next, termPath.Tail())
}
return nil
}
// ValuesAtPath returns a slice with the values found at the given JSON path query
func (c *collection) ValuesAtPath(document Document, queryPath QueryPath) ([]Scalar, error) {
return c.valueCollector(c, document, queryPath)
}
func valuesFromResult(result gjson.Result) ([]Scalar, error) {
switch result.Type {
case gjson.String:
return []Scalar{StringScalar(result.Str)}, nil
case gjson.True:
return []Scalar{BoolScalar(true)}, nil
case gjson.False:
return []Scalar{BoolScalar(false)}, nil
case gjson.Number:
return []Scalar{Float64Scalar(result.Num)}, nil
case gjson.Null:
return []Scalar{}, nil
default:
if result.IsArray() {
keys := make([]Scalar, 0)
for _, subResult := range result.Array() {
subKeys, err := valuesFromResult(subResult)
if err != nil {
return nil, err
}
keys = append(keys, subKeys...)
}
return keys, nil
}
}
return nil, fmt.Errorf("type at path not supported for indexing: %s", result.String())
}