-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpart_index.go
More file actions
638 lines (544 loc) · 17.3 KB
/
part_index.go
File metadata and controls
638 lines (544 loc) · 17.3 KB
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package statedb
import (
"bytes"
"encoding/binary"
"github.com/cilium/statedb/index"
"github.com/cilium/statedb/part"
)
// Index implements the indexing of objects (FromObjects) and
// querying of objects from the index (FromKey).
//
// The objects are indexed in a Persistent Adaptive Radix Tree [part].
type Index[Obj any, Key any] struct {
// Name of the index
Name string
// FromObject extracts key(s) from the object. The key set
// can contain 0, 1 or more keys. Must contain exactly one
// key for primary indices.
FromObject func(obj Obj) index.KeySet
// FromKey converts the index key into a raw key.
// With this we can perform Query() against this index with
// the [Key] type.
FromKey func(key Key) index.Key
// FromString is an optional conversion from string to a raw key.
// If implemented allows script commands to query with this index.
FromString func(key string) (index.Key, error)
// Unique marks the index as unique. Primary index must always be
// unique. A secondary index may be non-unique in which case a single
// key may map to multiple objects.
Unique bool
}
func (i Index[Obj, Key]) isIndexerOf(Obj) {
panic("isIndexerOf")
}
func (i Index[Obj, Key]) isUnique() bool {
return i.Unique
}
// fromString implements Indexer.
func (i Index[Obj, Key]) fromString(s string) (index.Key, error) {
return i.FromString(s)
}
var _ Indexer[struct{}] = &Index[struct{}, bool]{}
// The nolint:unused below are needed due to linter not seeing
// the use-sites due to generics.
//nolint:unused
func (i Index[Key, Obj]) indexName() string {
return i.Name
}
// Query constructs a query against this index from a key.
func (i Index[Obj, Key]) Query(key Key) Query[Obj] {
return Query[Obj]{
index: i.Name,
key: i.FromKey(key),
}
}
func (i Index[Obj, Key]) QueryFromObject(obj Obj) Query[Obj] {
return Query[Obj]{
index: i.Name,
key: i.FromObject(obj).First(),
}
}
// QueryFromKey constructs a query against the index using the given
// user-supplied key. Be careful when using this and prefer [Index.Query]
// over this if possible.
func (i Index[Obj, Key]) QueryFromKey(key index.Key) Query[Obj] {
return Query[Obj]{
index: i.Name,
key: key,
}
}
func (i Index[Obj, Key]) ObjectToKey(obj Obj) index.Key {
return i.FromObject(obj).First()
}
// newTableIndex constructs a new instance of this index type.
func (i Index[Obj, Key]) newTableIndex() tableIndex {
return &partIndex{
tree: part.New[object](),
partIndexTxn: partIndexTxn{
objectToKeys: func(obj object) index.KeySet {
return i.FromObject(obj.data.(Obj))
},
unique: i.Unique,
},
}
}
// partIndex indexes objects in a [part.Tree], e.g. an adaptive radix tree.
type partIndex struct {
tree part.Tree[object]
// partIndexTxn is the current transaction against the index. It's embedded
// here to avoid heap allocations.
partIndexTxn
}
// list implements tableIndex.
func (r *partIndex) list(key index.Key) (tableIndexIterator, <-chan struct{}) {
return partList(r.unique, &r.tree, key)
}
var emptyTableIndexIterator = &singletonTableIndexIterator{}
func partList(unique bool, tree part.Ops[object], key index.Key) (tableIndexIterator, <-chan struct{}) {
if unique {
// Unique index means that there can be only a single matching object.
// Doing a Get() is more efficient than constructing an iterator.
obj, watch, ok := tree.Get(key)
if ok {
return &singletonTableIndexIterator{key, obj}, watch
}
return emptyTableIndexIterator, watch
}
key = encodeNonUniqueBytes(key)
// For a non-unique index we do a prefix search. The keys are of
// form <secondary key><primary key><secondary key length>, and thus the
// iteration will continue until key length mismatches, e.g. we hit a
// longer key sharing the same prefix.
iter, watch := tree.Prefix(key)
return newNonUniquePartIterator(iter, false, key), watch
}
// rootWatch implements tableIndex.
func (r *partIndex) rootWatch() <-chan struct{} {
return r.tree.RootWatch()
}
func (r *partIndex) objectToKey(obj object) index.Key {
return r.objectToKeys(obj).First()
}
func (r *partIndex) commit() (tableIndex, tableIndexTxnNotify) {
return r, nil
}
// get implements tableIndex.
func (r *partIndex) get(ikey index.Key) (iobj object, watch <-chan struct{}, found bool) {
return partGet(r.unique, &r.tree, ikey)
}
func partGet(unique bool, tree part.Ops[object], ikey index.Key) (iobj object, watch <-chan struct{}, found bool) {
searchKey := ikey
if unique {
// On a unique index we can do a direct get rather than a prefix search.
return tree.Get(searchKey)
}
searchKey = encodeNonUniqueBytes(searchKey)
// For a non-unique index we need to do a prefix search.
iter, watch := tree.Prefix(searchKey)
for {
var key []byte
key, iobj, found = iter.Next()
if !found {
break
}
// Check that we have a full match on the key
if nonUniqueKey(key).secondaryLen() == len(searchKey) {
break
}
}
return iobj, watch, found
}
// len implements tableIndex.
func (r *partIndex) len() int {
return r.tree.Len()
}
func (r *partIndex) all() (tableIndexIterator, <-chan struct{}) {
return &r.tree, r.rootWatch()
}
// prefix implements tableIndex.
func (r *partIndex) prefix(ikey index.Key) (tableIndexIterator, <-chan struct{}) {
return partPrefix(r.unique, &r.tree, ikey)
}
func partPrefix(unique bool, tree part.Ops[object], key index.Key) (tableIndexIterator, <-chan struct{}) {
if !unique {
key = encodeNonUniqueBytes(key)
}
iter, watch := tree.Prefix(key)
if unique {
return iter, watch
}
return newNonUniquePartIterator(iter, true, key), watch
}
// lowerBound implements tableIndexTxn.
func (r *partIndex) lowerBound(ikey index.Key) (tableIndexIterator, <-chan struct{}) {
return partLowerBound(r.unique, &r.tree, ikey), r.rootWatch()
}
// lowerBoundNext implements tableIndexTxn.
func (r *partIndex) lowerBoundNext(key index.Key) (func() ([]byte, object, bool), <-chan struct{}) {
if !r.unique {
key = encodeNonUniqueBytes(key)
}
iter := r.tree.LowerBound(key)
if r.unique {
return iter.Next, r.rootWatch()
}
return newNonUniqueLowerBoundPartIterator(iter, key).Next, r.rootWatch()
}
func partLowerBound(unique bool, tree part.Ops[object], key index.Key) tableIndexIterator {
if !unique {
key = encodeNonUniqueBytes(key)
}
iter := tree.LowerBound(key)
if unique {
return &iter
}
return newNonUniqueLowerBoundPartIterator(iter, key)
}
// txn implements tableIndex.
func (r *partIndex) txn() (tableIndexTxn, bool) {
txn := &r.partIndexTxn
txn.tx = r.tree.Txn()
return txn, true
}
var _ tableIndex = &partIndex{}
type partIndexTxn struct {
objectToKeys func(object) index.KeySet
unique bool
tx *part.Txn[object]
}
// all implements tableIndexTxn.
func (r *partIndexTxn) all() (tableIndexIterator, <-chan struct{}) {
snapshot := r.tx.Clone()
return &snapshot, r.rootWatch()
}
// list implements tableIndexTxn.
func (r *partIndexTxn) list(ikey index.Key) (tableIndexIterator, <-chan struct{}) {
snapshot := r.tx.Clone()
return partList(r.unique, &snapshot, ikey)
}
// lowerBound implements tableIndexTxn.
func (r *partIndexTxn) lowerBound(ikey index.Key) (tableIndexIterator, <-chan struct{}) {
snapshot := r.tx.Clone()
return partLowerBound(r.unique, &snapshot, ikey), r.rootWatch()
}
// lowerBoundNext implements tableIndexTxn.
func (r *partIndexTxn) lowerBoundNext(key index.Key) (func() ([]byte, object, bool), <-chan struct{}) {
if !r.unique {
key = encodeNonUniqueBytes(key)
}
snapshot := r.tx.Clone()
iter := snapshot.LowerBound(key)
if r.unique {
return iter.Next, r.rootWatch()
}
return newNonUniqueLowerBoundPartIterator(iter, key).Next, r.rootWatch()
}
// rootWatch implements tableIndexTxn.
func (r *partIndexTxn) rootWatch() <-chan struct{} {
return r.tx.RootWatch()
}
// commit implements tableIndexTxn.
func (r *partIndexTxn) commit() (tableIndex, tableIndexTxnNotify) {
return &partIndex{
tree: r.tx.Commit(),
partIndexTxn: partIndexTxn{
unique: r.unique,
objectToKeys: r.objectToKeys,
},
}, r
}
// delete implements tableIndexTxn.
func (r *partIndexTxn) delete(key index.Key) (old object, hadOld bool) {
return r.tx.Delete(key)
}
// get implements tableIndexTxn.
func (r *partIndexTxn) get(key index.Key) (iobj object, watch <-chan struct{}, ok bool) {
return partGet(r.unique, r.tx, key)
}
// insert implements tableIndexTxn.
func (r *partIndexTxn) insert(key index.Key, obj object) (old object, hadOld bool, watch <-chan struct{}) {
return r.tx.InsertWatch(key, obj)
}
// len implements tableIndexTxn.
func (r *partIndexTxn) len() int {
return r.tx.Len()
}
// modify implements tableIndexTxn.
func (r *partIndexTxn) modify(key index.Key, obj object, mod func(old, new object) object) (old object, newObj object, hadOld bool, watch <-chan struct{}) {
return r.tx.ModifyWatch(key, obj, mod)
}
// notify implements tableIndexTxn.
func (r *partIndexTxn) notify() {
if r.tx != nil {
r.tx.Notify()
r.tx = nil
}
}
// prefix implements tableIndexTxn.
func (r *partIndexTxn) prefix(ikey index.Key) (tableIndexIterator, <-chan struct{}) {
snapshot := r.tx.Clone()
return partPrefix(r.unique, &snapshot, ikey)
}
func (r *partIndexTxn) objectToKey(obj object) index.Key {
return r.objectToKeys(obj).First()
}
// reindex implements tableIndexTxn.
func (r *partIndexTxn) reindex(idKey index.Key, old object, new object) {
unique := r.unique
var newKeys index.KeySet
if new.revision != 0 {
newKeys = r.objectToKeys(new)
newKeys.Foreach(func(newKey index.Key) {
// Non-unique secondary indexes are formed by concatenating them
// with the primary key.
if !unique {
newKey = encodeNonUniqueKey(idKey, newKey)
}
r.tx.Insert(newKey, new)
})
}
if old.revision != 0 {
// The old object existed, remove any obsolete keys
r.objectToKeys(old).Foreach(
func(oldKey index.Key) {
if !newKeys.Exists(oldKey) {
if !unique {
oldKey = encodeNonUniqueKey(idKey, oldKey)
}
_, hadOld := r.tx.Delete(oldKey)
if !unique && !hadOld {
panic("BUG: delete did not find old object")
}
}
},
)
}
}
func (r *partIndexTxn) txn() (tableIndexTxn, bool) {
return r, false
}
var _ tableIndexTxn = &partIndexTxn{}
const (
// nonUniqueSeparator is the byte that delimits the secondary and primary keys.
// It has to be 0x00 for correct ordering, e.g. if secondary prefix is "ab",
// then it must hold that "ab<sep>" < "abc<sep>", which is only possible if sep=0x00.
nonUniqueSeparator = 0x00
// nonUniqueSubstitute is the byte that is used to escape 0x00 and 0x01 in
// order to make sure the non-unique key has only a single 0x00 byte that is
// the separator.
nonUniqueSubstitute = 0x01
)
// appendEncode encodes the 'src' into 'dst'.
func appendEncode(dst, src []byte) (int, []byte) {
n := 0
for _, b := range src {
switch b {
case nonUniqueSeparator:
dst = append(dst, nonUniqueSubstitute, 0x01)
n += 2
case nonUniqueSubstitute:
dst = append(dst, nonUniqueSubstitute, 0x02)
n += 2
default:
dst = append(dst, b)
n++
}
}
return n, dst
}
func encodedLength(src []byte) int {
n := len(src)
for _, b := range src {
if b == nonUniqueSeparator || b == nonUniqueSubstitute {
n++
}
}
return n
}
func encodeNonUniqueBytes(src []byte) []byte {
n := encodedLength(src)
if n == len(src) {
// No substitutions needed.
return src
}
_, out := appendEncode(make([]byte, 0, n), src)
return out
}
// encodeNonUniqueKey constructs the internal key to use with non-unique indexes.
//
// This schema allows looking up from the non-unique index with the secondary key by
// doing a prefix search. The length is used to safe-guard against indexers that don't
// terminate the key properly (e.g. if secondary key is "foo", then we don't want
// "foobar" to match).
func encodeNonUniqueKey(primary, secondary index.Key) []byte {
key := make([]byte, 0,
encodedLength(secondary)+
1 /* delimiter */ +
encodedLength(primary)+
2 /* primary length */)
_, key = appendEncode(key, secondary)
key = append(key, 0x00)
primaryLen, key := appendEncode(key, primary)
return binary.BigEndian.AppendUint16(key, uint16(primaryLen))
}
type nonUniqueKey []byte
func (k nonUniqueKey) primaryLen() int {
// Non-unique key is [<secondary...>, 0x00, <primary...>, <primary length>]
if len(k) <= 3 {
return 0
}
return int(binary.BigEndian.Uint16(k[len(k)-2:]))
}
func (k nonUniqueKey) secondaryLen() int {
return len(k) - k.primaryLen() - 3
}
func (k nonUniqueKey) encodedPrimary() []byte {
primaryLen := k.primaryLen()
return k[len(k)-2-primaryLen : len(k)-2]
}
func (k nonUniqueKey) encodedSecondary() []byte {
return k[:k.secondaryLen()]
}
type nonUniquePartIterator struct {
iter part.Iterator[object]
prefixSearch bool
searchKey []byte
}
// All implements tableIndexIterator.
func (it *nonUniquePartIterator) All(yield func([]byte, object) bool) {
var visited map[string]struct{}
if it.prefixSearch {
// When prefix searching, keep track of objects we've already seen as
// multiple keys in non-unique index may map to a single object.
// When just doing a List() on a non-unique index we will see each object
// only once and do not need to track this.
//
// This of course makes iterating over a non-unique index with a prefix
// (or lowerbound search) about 20x slower than normal!
visited = map[string]struct{}{}
}
for key, iobj := range it.iter.All {
nuk := nonUniqueKey(key)
secondaryLen := nuk.secondaryLen()
switch {
case !it.prefixSearch && secondaryLen != len(it.searchKey):
// This a List(), thus secondary key must match length exactly.
continue
case it.prefixSearch && secondaryLen < len(it.searchKey):
// This is Prefix(), thus key must be equal or longer to search key.
continue
}
if it.prefixSearch {
primary := nuk.encodedPrimary()
// When doing a prefix search on a non-unique index we may see the
// same object multiple times since multiple keys may point it.
// Skip if we've already seen this object.
if _, found := visited[string(primary)]; found {
continue
}
visited[string(primary)] = struct{}{}
}
if !yield(key, iobj) {
return
}
}
}
func (it *nonUniquePartIterator) Next() ([]byte, object, bool) {
panic("not implemented")
}
var _ tableIndexIterator = &nonUniquePartIterator{}
// nonUniqueSeq returns a sequence of objects for a non-unique index.
// Non-unique indexes work by concatenating the secondary key with the
// primary key and then prefix searching for the items:
//
// <secondary>\0<primary><secondary length>
// ^^^^^^^^^^^
//
// Since the primary key can be of any length and we're prefix searching,
// we need to iterate over all objects matching the prefix and only emitting
// those which have the correct secondary key length.
// For example if we search for the key "aaaa", then we might have the following
// matches (_ is just delimiting, not part of the key):
//
// aaaa\0bbb4
// aaa\0abab3
// aaaa\0ccc4
//
// We yield "aaaa\0bbb4", skip "aaa\0abab3" and yield "aaaa\0ccc4".
func newNonUniquePartIterator(iter part.Iterator[object], prefixSearch bool, searchKey []byte) tableIndexIterator {
return &nonUniquePartIterator{
iter: iter,
prefixSearch: prefixSearch,
searchKey: searchKey,
}
}
type nonUniqueLowerBoundPartIterator struct {
iter part.Iterator[object]
searchKey []byte
// Keep track of objects we've already seen as multiple keys in non-unique
// index may map to a single object. Only used by Next().
visited map[string]struct{}
}
// All implements tableIndexIterator.
func (it *nonUniqueLowerBoundPartIterator) All(yield func([]byte, object) bool) {
visited := map[string]struct{}{}
for key, iobj := range it.iter.All {
// With a non-unique index we have a composite key <secondary><primary><secondary len>.
// This means we need to check every key that it's larger or equal to the search key.
// Just seeking to the first one isn't enough as the secondary key length may vary.
nuk := nonUniqueKey(key)
secondary := nuk.encodedSecondary()
if bytes.Compare(secondary, it.searchKey) >= 0 {
primary := nuk.encodedPrimary()
if _, found := visited[string(primary)]; found {
continue
}
visited[string(primary)] = struct{}{}
if !yield(key, iobj) {
return
}
}
}
}
func (it *nonUniqueLowerBoundPartIterator) Next() ([]byte, object, bool) {
if it.visited == nil {
it.visited = map[string]struct{}{}
}
for {
key, obj, ok := it.iter.Next()
if !ok {
return nil, object{}, false
}
// With a non-unique index we have a composite key <secondary><primary><secondary len>.
// This means we need to check every key that it's larger or equal to the search key.
// Just seeking to the first one isn't enough as the secondary key length may vary.
nuk := nonUniqueKey(key)
secondary := nuk.encodedSecondary()
if bytes.Compare(secondary, it.searchKey) >= 0 {
primary := nuk.encodedPrimary()
if _, found := it.visited[string(primary)]; found {
continue
}
it.visited[string(primary)] = struct{}{}
return key, obj, true
}
}
}
func newNonUniqueLowerBoundPartIterator(iter part.Iterator[object], searchKey []byte) *nonUniqueLowerBoundPartIterator {
return &nonUniqueLowerBoundPartIterator{
iter: iter,
searchKey: searchKey,
}
}
type singletonTableIndexIterator struct {
key []byte
obj object
}
func (s *singletonTableIndexIterator) All(yield func([]byte, object) bool) {
if s.key != nil {
yield(s.key, s.obj)
}
}
var _ tableIndexIterator = &singletonTableIndexIterator{}