-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
391 lines (341 loc) · 11 KB
/
index.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
/*
* 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 (
"bytes"
"errors"
"go.etcd.io/bbolt"
)
// Index describes an index. An index is based on a json path and has a path.
// The path is used for storage but also as identifier in search options.
type Index interface {
// Name returns the path of this index
Name() string
// Add indexes the document. It uses a sub-bucket of the given bucket.
// It will only be indexed if the complete index matches.
Add(bucket *bbolt.Bucket, ref Reference, doc Document) error
// Delete document from the index
Delete(bucket *bbolt.Bucket, ref Reference, doc Document) error
// IsMatch determines if this index can be used for the given query. The higher the return value, the more likely it is useful.
// return values lie between 0.0 and 1.0, where 1.0 is the most useful.
IsMatch(query Query) float64
// Iterate over the key/value pairs given a query. Entries that match the query are passed to the iteratorFn.
// it will not filter out double values
Iterate(bucket *bbolt.Bucket, query Query, fn iteratorFn) error
// BucketName returns the bucket path for this index
BucketName() []byte
// QueryPartsOutsideIndex selects the queryParts that are not covered by the index.
QueryPartsOutsideIndex(query Query) []QueryPart
// Depth returns the number of indexed fields
Depth() int
// Keys returns the scalars found in the document at the location specified by the FieldIndexer
Keys(fi FieldIndexer, document Document) ([]Scalar, error)
}
// iteratorFn defines a function that is used as a callback when an IterateIndex query finds results. The function is called for each result entry.
// the key will be the indexed value and the value will contain an Entry
type iteratorFn DocumentWalker
type index struct {
name string
indexParts []FieldIndexer
collection Collection
}
func (i *index) Name() string {
return i.name
}
func (i *index) BucketName() []byte {
return []byte(i.Name())
}
func (i *index) Depth() int {
return len(i.indexParts)
}
func (i *index) Add(bucket *bbolt.Bucket, ref Reference, doc Document) error {
cBucket, _ := bucket.CreateBucketIfNotExists(i.BucketName())
return i.addDocumentR(cBucket, i.indexParts, Key{}, ref, doc)
}
// addDocumentR, like Add but recursive
func (i *index) addDocumentR(bucket *bbolt.Bucket, parts []FieldIndexer, cKey Key, ref Reference, doc Document) error {
// current part
ip := parts[0]
matches, err := i.Keys(ip, doc)
if err != nil {
return err
}
// exit condition
if len(parts) == 1 {
// all matches to be added to current bucket
for _, m := range matches {
key := ComposeKey(cKey, m.Bytes())
_ = addRefToBucket(bucket, key, ref)
}
if len(matches) == 0 {
key := ComposeKey(cKey, []byte{})
_ = addRefToBucket(bucket, key, ref)
}
return nil
}
// continue recursion
for _, m := range matches {
nKey := ComposeKey(cKey, m.Bytes())
if err = i.addDocumentR(bucket, parts[1:], nKey, ref, doc); err != nil {
return err
}
}
// no matches for the document and this part of the index
// add key with an empty byte slice as value
if len(matches) == 0 {
nKey := ComposeKey(cKey, []byte{})
return i.addDocumentR(bucket, parts[1:], nKey, ref, doc)
}
return nil
}
// removeDocumentR, like Delete but recursive
func (i *index) removeDocumentR(bucket *bbolt.Bucket, parts []FieldIndexer, cKey Key, ref Reference, doc Document) error {
// current part
ip := parts[0]
matches, err := i.Keys(ip, doc)
if err != nil {
return err
}
// exit condition
if len(parts) == 1 {
for _, m := range matches {
key := ComposeKey(cKey, m.Bytes())
_ = removeRefFromBucket(bucket, key, ref)
}
return nil
}
// continue recursion
for _, m := range matches {
nKey := ComposeKey(cKey, m.Bytes())
return i.removeDocumentR(bucket, parts[1:], nKey, ref, doc)
}
// no matches for the document and this part of the index
return nil
}
func (i *index) Delete(bucket *bbolt.Bucket, ref Reference, doc Document) error {
cBucket := bucket.Bucket(i.BucketName())
if cBucket == nil {
return nil
}
return i.removeDocumentR(cBucket, i.indexParts, Key{}, ref, doc)
}
// addRefToBucket adds the reference to the correct key in the bucket. It handles multiple reference on the same location
func addRefToBucket(bucket *bbolt.Bucket, key Key, ref Reference) error {
// first check if there's a sub-bucket
subBucket, err := bucket.CreateBucketIfNotExists(key)
if err != nil {
return err
}
return subBucket.Put(ref, []byte{})
}
// removeRefFromBucket removes the reference from the bucket. It handles multiple reference on the same location
func removeRefFromBucket(bucket *bbolt.Bucket, key Key, ref Reference) error {
// first check if there's a sub-bucket
subBucket := bucket.Bucket(key)
if subBucket == nil {
return nil
}
return subBucket.Delete(ref)
}
func (i *index) IsMatch(query Query) float64 {
hitcount := 0
parts := i.matchingParts(query)
outer:
for thc, ip := range i.indexParts {
for _, qp := range parts {
if ip.Equals(qp) {
hitcount++
}
}
// if a miss is encountered, do not continue. You can't skip an index lvl
if hitcount == thc {
break outer
}
}
return float64(hitcount)
}
// matchingParts returns the queryParts that match the index.
// it also sorts them in the right order. If multiple matches exist a index position, the first is returned.
func (i *index) matchingParts(query Query) []QueryPart {
var sorted = make([]QueryPart, len(i.indexParts))
outer:
for _, qp := range query.parts {
for j, ip := range i.indexParts {
if ip.Equals(qp) {
if sorted[j] == nil {
sorted[j] = qp
continue outer
}
}
}
}
// only use till the first nil value
for i, s := range sorted {
if s == nil {
sorted = sorted[:i]
break
}
}
return sorted
}
func (i *index) QueryPartsOutsideIndex(query Query) []QueryPart {
matchingParts := i.matchingParts(query)
resultingParts := make([]QueryPart, 0)
visitedParts := make([]QueryPart, 0)
outer:
for _, qp := range query.parts {
for _, mp := range matchingParts {
if mp.Equals(qp) {
for _, hp := range visitedParts {
if hp.Equals(qp) { // already excluded once
resultingParts = append(resultingParts, qp)
continue outer
}
}
// exclude and continue
visitedParts = append(visitedParts, mp)
continue outer
}
}
// no hit in index parts
resultingParts = append(resultingParts, qp)
}
return resultingParts
}
func (i *index) Iterate(bucket *bbolt.Bucket, query Query, fn iteratorFn) error {
var err error
cBucket := bucket.Bucket(i.BucketName())
if cBucket == nil {
return err
}
// Sort the parts of the Query to conform to the index key building order
sortedQueryParts := i.matchingParts(query)
if len(sortedQueryParts) == 0 {
return errors.New("unable to iterate over index without matching keys")
}
// extract tokenizer and transform to here
matchers := i.matchers(sortedQueryParts)
_, err = findR(cBucket.Cursor(), Key{}, matchers, fn, []byte{}, 0)
return err
}
func (i *index) matchers(sortedQueryParts []QueryPart) []matcher {
// extract tokenizer and transform to here
matchers := make([]matcher, len(sortedQueryParts))
for j, cPart := range sortedQueryParts {
terms := make([]Scalar, 0)
for _, token := range i.indexParts[j].Tokenize(cPart.Seek()) {
seek := i.indexParts[j].Transform(token)
terms = append(terms, seek)
}
matchers[j] = matcher{
queryPart: cPart,
terms: terms,
transform: i.indexParts[j].Transform,
}
}
return matchers
}
func (i *index) Keys(j FieldIndexer, document Document) ([]Scalar, error) {
// first get the raw values from the query path
rawKeys, err := i.collection.ValuesAtPath(document, j.QueryPath())
if err != nil {
return nil, err
}
// run the tokenizer
tokenized := make([]Scalar, 0)
for _, rawKey := range rawKeys {
tokens := j.Tokenize(rawKey)
tokenized = append(tokenized, tokens...)
}
// run the transformer
transformed := make([]Scalar, len(tokenized))
for i, rawKey := range tokenized {
transformed[i] = j.Transform(rawKey)
}
return transformed, nil
}
type matcher struct {
queryPart QueryPart
terms []Scalar
transform Transform
}
func findR(cursor *bbolt.Cursor, searchKey Key, matchers []matcher, fn iteratorFn, lastCursorPosition []byte, depth int) ([]byte, error) {
var err error
returnKey := lastCursorPosition
currentQueryPart := matchers[0].queryPart
//outer:
for _, seekTerm := range matchers[0].terms {
// new location in cursor to skip to
seek := ComposeKey(searchKey, seekTerm.Bytes())
condition := true
// do not go back to prevent infinite loops. The cursor may only go forward.
if bytes.Compare(seek, lastCursorPosition) < 0 {
seek = lastCursorPosition
}
var currentKey []byte
for currentKey, _ = cursor.Seek(seek); currentKey != nil && bytes.HasPrefix(currentKey, searchKey) && condition; {
var newPart []byte
split := Key(currentKey).Split()
if len(split) > depth {
newPart = split[depth]
} // else use nil value, should not happen, but better to prevent panics
// check of current (partial) key still matches with query
condition = currentQueryPart.Condition(newPart, matchers[0].transform)
if condition {
if len(matchers) > 1 {
// (partial) key still matches, continue to next index part
nKey := ComposeKey(searchKey, newPart)
// on success the cursor is moved forward, the latest key is returned, continue with that key
// if keys haven't changed: break
var subKey []byte
subKey, err = findR(cursor, nKey, matchers[1:], fn, currentKey, depth+1)
if bytes.Equal(subKey, currentKey) {
// the nested search could not advance the cursor, so we do it here before continuing the loop
currentKey, _ = cursor.Next()
returnKey = currentKey
continue
}
currentKey = subKey
} else {
// all index parts applied to key construction, retrieve results.
err = iterateOverDocuments(cursor, currentKey, fn)
// this position was a success, hopefully the next as well
currentKey, _ = cursor.Next()
}
if err != nil {
return nil, err
}
}
}
returnKey = currentKey
}
return returnKey, nil
}
func iterateOverDocuments(cursor *bbolt.Cursor, cKey []byte, fn iteratorFn) error {
subBucket := cursor.Bucket().Bucket(cKey)
if subBucket != nil {
subCursor := subBucket.Cursor()
for k, _ := subCursor.Seek([]byte{}); k != nil; k, _ = subCursor.Next() {
if err := fn(cKey, k); err != nil {
return err
}
}
}
return nil
}