-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmod.rs
More file actions
1650 lines (1528 loc) · 80.2 KB
/
mod.rs
File metadata and controls
1650 lines (1528 loc) · 80.2 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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Table indexes with specialized key types.
//!
//! Indexes could be implemented as `MultiMap<AlgebraicValue, RowPointer>` (and once were),
//! but that results in wasted memory and spurious comparisons and branches
//! because the keys must always be homogeneous at a more specific type than `AlgebraicValue`.
//!
//! As an optimization, we hoist the enum out of the keys to the index itself.
//! This is a sizeable improvement for integer keys,
//! as e.g. `u64::cmp` is much faster than `AlgebraicValue::cmp`.
//!
//! This results in some pretty ugly code, where types that would be structs
//! are instead enums with similar-looking variants for each specialized key type,
//! and methods that interact with those enums have matches with similar-looking arms.
//! Some day we may devise a better solution, but this is good enough for now.
//
// I (pgoldman 2024-02-05) suspect, but have not measured, that there's no real reason
// to have a `ProductType` variant, which would apply to multi-column indexes.
// I believe `ProductValue::cmp` to not be meaningfully faster than `AlgebraicValue::cmp`.
// Eventually, we will likely want to compile comparison functions and representations
// for `ProductValue`-keyed indexes which take advantage of type information,
// since we know when creating the index the number and type of all the indexed columns.
// This may involve a bytecode compiler, a tree of closures, or a native JIT.
///
/// We also represent unique indices more compactly than non-unique ones, avoiding the multi-map.
/// Additionally, beyond our btree indices,
/// we support direct unique indices, where key are indices into `Vec`s.
use super::indexes::RowPointer;
use super::table::RowRef;
use crate::{read_column::ReadColumn, static_assert_size};
use core::ops::RangeBounds;
use spacetimedb_primitives::ColList;
use spacetimedb_sats::memory_usage::MemoryUsage;
use spacetimedb_sats::{
algebraic_value::Packed, i256, product_value::InvalidFieldError, sum_value::SumTag, u256, AlgebraicType,
AlgebraicValue, ProductType, F32, F64,
};
mod key_size;
mod multimap;
pub mod unique_direct_fixed_cap_index;
pub mod unique_direct_index;
pub mod uniquemap;
pub use key_size::KeySize;
use spacetimedb_schema::def::IndexAlgorithm;
use unique_direct_fixed_cap_index::{UniqueDirectFixedCapIndex, UniqueDirectFixedCapIndexRangeIter};
use unique_direct_index::{UniqueDirectIndex, UniqueDirectIndexPointIter, UniqueDirectIndexRangeIter};
type BtreeIndex<K> = multimap::MultiMap<K, RowPointer>;
type BtreeIndexPointIter<'a> = multimap::MultiMapPointIter<'a, RowPointer>;
type BtreeIndexRangeIter<'a, K> = multimap::MultiMapRangeIter<'a, K, RowPointer>;
type BtreeUniqueIndex<K> = uniquemap::UniqueMap<K, RowPointer>;
type BtreeUniqueIndexPointIter<'a> = uniquemap::UniqueMapPointIter<'a, RowPointer>;
type BtreeUniqueIndexRangeIter<'a, K> = uniquemap::UniqueMapRangeIter<'a, K, RowPointer>;
/// A point iterator over a [`TypedIndex`], with a specialized key type.
///
/// See module docs for info about specialization.
enum TypedIndexPointIter<'a> {
BTree(BtreeIndexPointIter<'a>),
UniqueBTree(BtreeUniqueIndexPointIter<'a>),
UniqueDirect(UniqueDirectIndexPointIter),
}
impl Iterator for TypedIndexPointIter<'_> {
type Item = RowPointer;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::BTree(this) => this.next().copied(),
Self::UniqueBTree(this) => this.next().copied(),
Self::UniqueDirect(this) => this.next(),
}
}
}
/// An iterator over rows matching a certain [`AlgebraicValue`] on the [`TableIndex`].
pub struct TableIndexPointIter<'a> {
/// The iterator seeking for matching values.
iter: TypedIndexPointIter<'a>,
}
impl Iterator for TableIndexPointIter<'_> {
type Item = RowPointer;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}
/// A ranged iterator over a [`TypedIndex`], with a specialized key type.
///
/// See module docs for info about specialization.
enum TypedIndexRangeIter<'a> {
/// The range itself provided was empty.
RangeEmpty,
// All the non-unique btree index iterators.
BtreeBool(BtreeIndexRangeIter<'a, bool>),
BtreeU8(BtreeIndexRangeIter<'a, u8>),
BtreeI8(BtreeIndexRangeIter<'a, i8>),
BtreeU16(BtreeIndexRangeIter<'a, u16>),
BtreeI16(BtreeIndexRangeIter<'a, i16>),
BtreeU32(BtreeIndexRangeIter<'a, u32>),
BtreeI32(BtreeIndexRangeIter<'a, i32>),
BtreeU64(BtreeIndexRangeIter<'a, u64>),
BtreeI64(BtreeIndexRangeIter<'a, i64>),
BtreeU128(BtreeIndexRangeIter<'a, Packed<u128>>),
BtreeI128(BtreeIndexRangeIter<'a, Packed<i128>>),
BtreeU256(BtreeIndexRangeIter<'a, u256>),
BtreeI256(BtreeIndexRangeIter<'a, i256>),
BtreeF32(BtreeIndexRangeIter<'a, F32>),
BtreeF64(BtreeIndexRangeIter<'a, F64>),
BtreeString(BtreeIndexRangeIter<'a, Box<str>>),
BtreeAV(BtreeIndexRangeIter<'a, AlgebraicValue>),
// All the unique btree index iterators.
UniqueBtreeBool(BtreeUniqueIndexRangeIter<'a, bool>),
UniqueBtreeU8(BtreeUniqueIndexRangeIter<'a, u8>),
UniqueBtreeI8(BtreeUniqueIndexRangeIter<'a, i8>),
UniqueBtreeU16(BtreeUniqueIndexRangeIter<'a, u16>),
UniqueBtreeI16(BtreeUniqueIndexRangeIter<'a, i16>),
UniqueBtreeU32(BtreeUniqueIndexRangeIter<'a, u32>),
UniqueBtreeI32(BtreeUniqueIndexRangeIter<'a, i32>),
UniqueBtreeU64(BtreeUniqueIndexRangeIter<'a, u64>),
UniqueBtreeI64(BtreeUniqueIndexRangeIter<'a, i64>),
UniqueBtreeU128(BtreeUniqueIndexRangeIter<'a, Packed<u128>>),
UniqueBtreeI128(BtreeUniqueIndexRangeIter<'a, Packed<i128>>),
UniqueBtreeU256(BtreeUniqueIndexRangeIter<'a, u256>),
UniqueBtreeI256(BtreeUniqueIndexRangeIter<'a, i256>),
UniqueBtreeF32(BtreeUniqueIndexRangeIter<'a, F32>),
UniqueBtreeF64(BtreeUniqueIndexRangeIter<'a, F64>),
UniqueBtreeString(BtreeUniqueIndexRangeIter<'a, Box<str>>),
UniqueBtreeAV(BtreeUniqueIndexRangeIter<'a, AlgebraicValue>),
UniqueDirect(UniqueDirectIndexRangeIter<'a>),
UniqueDirectU8(UniqueDirectFixedCapIndexRangeIter<'a>),
}
impl Iterator for TypedIndexRangeIter<'_> {
type Item = RowPointer;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::RangeEmpty => None,
Self::BtreeBool(this) => this.next().copied(),
Self::BtreeU8(this) => this.next().copied(),
Self::BtreeI8(this) => this.next().copied(),
Self::BtreeU16(this) => this.next().copied(),
Self::BtreeI16(this) => this.next().copied(),
Self::BtreeU32(this) => this.next().copied(),
Self::BtreeI32(this) => this.next().copied(),
Self::BtreeU64(this) => this.next().copied(),
Self::BtreeI64(this) => this.next().copied(),
Self::BtreeU128(this) => this.next().copied(),
Self::BtreeI128(this) => this.next().copied(),
Self::BtreeU256(this) => this.next().copied(),
Self::BtreeI256(this) => this.next().copied(),
Self::BtreeF32(this) => this.next().copied(),
Self::BtreeF64(this) => this.next().copied(),
Self::BtreeString(this) => this.next().copied(),
Self::BtreeAV(this) => this.next().copied(),
Self::UniqueBtreeBool(this) => this.next().copied(),
Self::UniqueBtreeU8(this) => this.next().copied(),
Self::UniqueBtreeI8(this) => this.next().copied(),
Self::UniqueBtreeU16(this) => this.next().copied(),
Self::UniqueBtreeI16(this) => this.next().copied(),
Self::UniqueBtreeU32(this) => this.next().copied(),
Self::UniqueBtreeI32(this) => this.next().copied(),
Self::UniqueBtreeU64(this) => this.next().copied(),
Self::UniqueBtreeI64(this) => this.next().copied(),
Self::UniqueBtreeU128(this) => this.next().copied(),
Self::UniqueBtreeI128(this) => this.next().copied(),
Self::UniqueBtreeU256(this) => this.next().copied(),
Self::UniqueBtreeI256(this) => this.next().copied(),
Self::UniqueBtreeF32(this) => this.next().copied(),
Self::UniqueBtreeF64(this) => this.next().copied(),
Self::UniqueBtreeString(this) => this.next().copied(),
Self::UniqueBtreeAV(this) => this.next().copied(),
Self::UniqueDirect(this) => this.next(),
Self::UniqueDirectU8(this) => this.next(),
}
}
}
/// An iterator over rows matching a range of [`AlgebraicValue`]s on the [`TableIndex`].
pub struct TableIndexRangeIter<'a> {
/// The iterator seeking for matching values.
iter: TypedIndexRangeIter<'a>,
}
impl Iterator for TableIndexRangeIter<'_> {
type Item = RowPointer;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}
/// An index from a key type determined at runtime to `RowPointer`(s).
///
/// See module docs for info about specialization.
#[derive(Debug, PartialEq, Eq)]
enum TypedIndex {
// All the non-unique btree index types.
BtreeBool(BtreeIndex<bool>),
BtreeU8(BtreeIndex<u8>),
BtreeSumTag(BtreeIndex<u8>),
BtreeI8(BtreeIndex<i8>),
BtreeU16(BtreeIndex<u16>),
BtreeI16(BtreeIndex<i16>),
BtreeU32(BtreeIndex<u32>),
BtreeI32(BtreeIndex<i32>),
BtreeU64(BtreeIndex<u64>),
BtreeI64(BtreeIndex<i64>),
BtreeU128(BtreeIndex<Packed<u128>>),
BtreeI128(BtreeIndex<Packed<i128>>),
BtreeU256(BtreeIndex<u256>),
BtreeI256(BtreeIndex<i256>),
BtreeF32(BtreeIndex<F32>),
BtreeF64(BtreeIndex<F64>),
BtreeString(BtreeIndex<Box<str>>),
BtreeAV(BtreeIndex<AlgebraicValue>),
// All the unique btree index types.
UniqueBtreeBool(BtreeUniqueIndex<bool>),
UniqueBtreeU8(BtreeUniqueIndex<u8>),
UniqueBtreeSumTag(BtreeUniqueIndex<u8>),
UniqueBtreeI8(BtreeUniqueIndex<i8>),
UniqueBtreeU16(BtreeUniqueIndex<u16>),
UniqueBtreeI16(BtreeUniqueIndex<i16>),
UniqueBtreeU32(BtreeUniqueIndex<u32>),
UniqueBtreeI32(BtreeUniqueIndex<i32>),
UniqueBtreeU64(BtreeUniqueIndex<u64>),
UniqueBtreeI64(BtreeUniqueIndex<i64>),
UniqueBtreeU128(BtreeUniqueIndex<Packed<u128>>),
UniqueBtreeI128(BtreeUniqueIndex<Packed<i128>>),
UniqueBtreeU256(BtreeUniqueIndex<u256>),
UniqueBtreeI256(BtreeUniqueIndex<i256>),
UniqueBtreeF32(BtreeUniqueIndex<F32>),
UniqueBtreeF64(BtreeUniqueIndex<F64>),
UniqueBtreeString(BtreeUniqueIndex<Box<str>>),
UniqueBtreeAV(BtreeUniqueIndex<AlgebraicValue>),
// All the unique direct index types.
UniqueDirectU8(UniqueDirectIndex),
UniqueDirectSumTag(UniqueDirectFixedCapIndex),
UniqueDirectU16(UniqueDirectIndex),
UniqueDirectU32(UniqueDirectIndex),
UniqueDirectU64(UniqueDirectIndex),
}
impl MemoryUsage for TypedIndex {
fn heap_usage(&self) -> usize {
match self {
TypedIndex::BtreeBool(this) => this.heap_usage(),
TypedIndex::BtreeU8(this) | TypedIndex::BtreeSumTag(this) => this.heap_usage(),
TypedIndex::BtreeI8(this) => this.heap_usage(),
TypedIndex::BtreeU16(this) => this.heap_usage(),
TypedIndex::BtreeI16(this) => this.heap_usage(),
TypedIndex::BtreeU32(this) => this.heap_usage(),
TypedIndex::BtreeI32(this) => this.heap_usage(),
TypedIndex::BtreeU64(this) => this.heap_usage(),
TypedIndex::BtreeI64(this) => this.heap_usage(),
TypedIndex::BtreeU128(this) => this.heap_usage(),
TypedIndex::BtreeI128(this) => this.heap_usage(),
TypedIndex::BtreeU256(this) => this.heap_usage(),
TypedIndex::BtreeI256(this) => this.heap_usage(),
TypedIndex::BtreeF32(this) => this.heap_usage(),
TypedIndex::BtreeF64(this) => this.heap_usage(),
TypedIndex::BtreeString(this) => this.heap_usage(),
TypedIndex::BtreeAV(this) => this.heap_usage(),
TypedIndex::UniqueBtreeBool(this) => this.heap_usage(),
TypedIndex::UniqueBtreeU8(this) | TypedIndex::UniqueBtreeSumTag(this) => this.heap_usage(),
TypedIndex::UniqueBtreeI8(this) => this.heap_usage(),
TypedIndex::UniqueBtreeU16(this) => this.heap_usage(),
TypedIndex::UniqueBtreeI16(this) => this.heap_usage(),
TypedIndex::UniqueBtreeU32(this) => this.heap_usage(),
TypedIndex::UniqueBtreeI32(this) => this.heap_usage(),
TypedIndex::UniqueBtreeU64(this) => this.heap_usage(),
TypedIndex::UniqueBtreeI64(this) => this.heap_usage(),
TypedIndex::UniqueBtreeU128(this) => this.heap_usage(),
TypedIndex::UniqueBtreeI128(this) => this.heap_usage(),
TypedIndex::UniqueBtreeU256(this) => this.heap_usage(),
TypedIndex::UniqueBtreeI256(this) => this.heap_usage(),
TypedIndex::UniqueBtreeF32(this) => this.heap_usage(),
TypedIndex::UniqueBtreeF64(this) => this.heap_usage(),
TypedIndex::UniqueBtreeString(this) => this.heap_usage(),
TypedIndex::UniqueBtreeAV(this) => this.heap_usage(),
TypedIndex::UniqueDirectSumTag(this) => this.heap_usage(),
TypedIndex::UniqueDirectU8(this)
| TypedIndex::UniqueDirectU16(this)
| TypedIndex::UniqueDirectU32(this)
| TypedIndex::UniqueDirectU64(this) => this.heap_usage(),
}
}
}
fn as_tag(av: &AlgebraicValue) -> Option<&u8> {
av.as_sum().map(|s| &s.tag)
}
impl TypedIndex {
/// Returns a new index with keys being of `key_type` and the index possibly `is_unique`.
fn new(key_type: &AlgebraicType, index_algo: &IndexAlgorithm, is_unique: bool) -> Self {
use TypedIndex::*;
if let IndexAlgorithm::Direct(_) = index_algo {
assert!(is_unique);
return match key_type {
AlgebraicType::U8 => Self::UniqueDirectU8(<_>::default()),
AlgebraicType::U16 => Self::UniqueDirectU16(<_>::default()),
AlgebraicType::U32 => Self::UniqueDirectU32(<_>::default()),
AlgebraicType::U64 => Self::UniqueDirectU64(<_>::default()),
// For a plain enum, use `u8` as the native type.
AlgebraicType::Sum(sum) if sum.is_simple_enum() => {
UniqueDirectSumTag(UniqueDirectFixedCapIndex::new(sum.variants.len()))
}
_ => unreachable!("unexpected key type {key_type:?} for direct index"),
};
}
// If the index is on a single column of a primitive type, string, or plain enum,
// use a homogeneous map with a native key type.
if is_unique {
match key_type {
AlgebraicType::Bool => UniqueBtreeBool(<_>::default()),
AlgebraicType::I8 => UniqueBtreeI8(<_>::default()),
AlgebraicType::U8 => UniqueBtreeU8(<_>::default()),
AlgebraicType::I16 => UniqueBtreeI16(<_>::default()),
AlgebraicType::U16 => UniqueBtreeU16(<_>::default()),
AlgebraicType::I32 => UniqueBtreeI32(<_>::default()),
AlgebraicType::U32 => UniqueBtreeU32(<_>::default()),
AlgebraicType::I64 => UniqueBtreeI64(<_>::default()),
AlgebraicType::U64 => UniqueBtreeU64(<_>::default()),
AlgebraicType::I128 => UniqueBtreeI128(<_>::default()),
AlgebraicType::U128 => UniqueBtreeU128(<_>::default()),
AlgebraicType::I256 => UniqueBtreeI256(<_>::default()),
AlgebraicType::U256 => UniqueBtreeU256(<_>::default()),
AlgebraicType::F32 => UniqueBtreeF32(<_>::default()),
AlgebraicType::F64 => UniqueBtreeF64(<_>::default()),
AlgebraicType::String => UniqueBtreeString(<_>::default()),
// For a plain enum, use `u8` as the native type.
// We use a direct index here
AlgebraicType::Sum(sum) if sum.is_simple_enum() => UniqueBtreeSumTag(<_>::default()),
// The index is either multi-column,
// or we don't care to specialize on the key type,
// so use a map keyed on `AlgebraicValue`.
_ => UniqueBtreeAV(<_>::default()),
}
} else {
match key_type {
AlgebraicType::Bool => BtreeBool(<_>::default()),
AlgebraicType::I8 => BtreeI8(<_>::default()),
AlgebraicType::U8 => BtreeU8(<_>::default()),
AlgebraicType::I16 => BtreeI16(<_>::default()),
AlgebraicType::U16 => BtreeU16(<_>::default()),
AlgebraicType::I32 => BtreeI32(<_>::default()),
AlgebraicType::U32 => BtreeU32(<_>::default()),
AlgebraicType::I64 => BtreeI64(<_>::default()),
AlgebraicType::U64 => BtreeU64(<_>::default()),
AlgebraicType::I128 => BtreeI128(<_>::default()),
AlgebraicType::U128 => BtreeU128(<_>::default()),
AlgebraicType::I256 => BtreeI256(<_>::default()),
AlgebraicType::U256 => BtreeU256(<_>::default()),
AlgebraicType::F32 => BtreeF32(<_>::default()),
AlgebraicType::F64 => BtreeF64(<_>::default()),
AlgebraicType::String => BtreeString(<_>::default()),
// For a plain enum, use `u8` as the native type.
AlgebraicType::Sum(sum) if sum.is_simple_enum() => BtreeSumTag(<_>::default()),
// The index is either multi-column,
// or we don't care to specialize on the key type,
// so use a map keyed on `AlgebraicValue`.
_ => BtreeAV(<_>::default()),
}
}
}
/// Clones the structure of this index but not the indexed elements,
/// so the returned index is empty.
fn clone_structure(&self) -> Self {
use TypedIndex::*;
match self {
BtreeBool(_) => BtreeBool(<_>::default()),
BtreeU8(_) => BtreeU8(<_>::default()),
BtreeSumTag(_) => BtreeSumTag(<_>::default()),
BtreeI8(_) => BtreeI8(<_>::default()),
BtreeU16(_) => BtreeU16(<_>::default()),
BtreeI16(_) => BtreeI16(<_>::default()),
BtreeU32(_) => BtreeU32(<_>::default()),
BtreeI32(_) => BtreeI32(<_>::default()),
BtreeU64(_) => BtreeU64(<_>::default()),
BtreeI64(_) => BtreeI64(<_>::default()),
BtreeU128(_) => BtreeU128(<_>::default()),
BtreeI128(_) => BtreeI128(<_>::default()),
BtreeU256(_) => BtreeU256(<_>::default()),
BtreeI256(_) => BtreeI256(<_>::default()),
BtreeF32(_) => BtreeF32(<_>::default()),
BtreeF64(_) => BtreeF64(<_>::default()),
BtreeString(_) => BtreeString(<_>::default()),
BtreeAV(_) => BtreeAV(<_>::default()),
UniqueBtreeBool(_) => UniqueBtreeBool(<_>::default()),
UniqueBtreeU8(_) => UniqueBtreeU8(<_>::default()),
UniqueBtreeSumTag(_) => UniqueBtreeSumTag(<_>::default()),
UniqueBtreeI8(_) => UniqueBtreeI8(<_>::default()),
UniqueBtreeU16(_) => UniqueBtreeU16(<_>::default()),
UniqueBtreeI16(_) => UniqueBtreeI16(<_>::default()),
UniqueBtreeU32(_) => UniqueBtreeU32(<_>::default()),
UniqueBtreeI32(_) => UniqueBtreeI32(<_>::default()),
UniqueBtreeU64(_) => UniqueBtreeU64(<_>::default()),
UniqueBtreeI64(_) => UniqueBtreeI64(<_>::default()),
UniqueBtreeU128(_) => UniqueBtreeU128(<_>::default()),
UniqueBtreeI128(_) => UniqueBtreeI128(<_>::default()),
UniqueBtreeU256(_) => UniqueBtreeU256(<_>::default()),
UniqueBtreeI256(_) => UniqueBtreeI256(<_>::default()),
UniqueBtreeF32(_) => UniqueBtreeF32(<_>::default()),
UniqueBtreeF64(_) => UniqueBtreeF64(<_>::default()),
UniqueBtreeString(_) => UniqueBtreeString(<_>::default()),
UniqueBtreeAV(_) => UniqueBtreeAV(<_>::default()),
UniqueDirectU8(_) => UniqueDirectU8(<_>::default()),
UniqueDirectSumTag(idx) => UniqueDirectSumTag(idx.clone_structure()),
UniqueDirectU16(_) => UniqueDirectU16(<_>::default()),
UniqueDirectU32(_) => UniqueDirectU32(<_>::default()),
UniqueDirectU64(_) => UniqueDirectU64(<_>::default()),
}
}
/// Returns whether this is a unique index or not.
fn is_unique(&self) -> bool {
use TypedIndex::*;
match self {
BtreeBool(_) | BtreeU8(_) | BtreeSumTag(_) | BtreeI8(_) | BtreeU16(_) | BtreeI16(_) | BtreeU32(_)
| BtreeI32(_) | BtreeU64(_) | BtreeI64(_) | BtreeU128(_) | BtreeI128(_) | BtreeU256(_) | BtreeI256(_)
| BtreeF32(_) | BtreeF64(_) | BtreeString(_) | BtreeAV(_) => false,
UniqueBtreeBool(_)
| UniqueBtreeU8(_)
| UniqueBtreeSumTag(_)
| UniqueBtreeI8(_)
| UniqueBtreeU16(_)
| UniqueBtreeI16(_)
| UniqueBtreeU32(_)
| UniqueBtreeI32(_)
| UniqueBtreeU64(_)
| UniqueBtreeI64(_)
| UniqueBtreeU128(_)
| UniqueBtreeI128(_)
| UniqueBtreeU256(_)
| UniqueBtreeI256(_)
| UniqueBtreeF32(_)
| UniqueBtreeF64(_)
| UniqueBtreeString(_)
| UniqueBtreeAV(_)
| UniqueDirectU8(_)
| UniqueDirectSumTag(_)
| UniqueDirectU16(_)
| UniqueDirectU32(_)
| UniqueDirectU64(_) => true,
}
}
/// Add the row referred to by `row_ref` to the index `self`,
/// which must be keyed at `cols`.
///
/// The returned `usize` is the number of bytes used by the key.
/// [`TableIndex::check_and_insert`] will use this
/// to update the counter for [`TableIndex::num_key_bytes`].
/// We want to store said counter outside of the [`TypedIndex`] enum,
/// but we can only compute the size using type info within the [`TypedIndex`],
/// so we have to return the size across this boundary.
///
/// Returns `Errs(existing_row)` if this index was a unique index that was violated.
/// The index is not inserted to in that case.
///
/// # Safety
///
/// 1. Caller promises that `cols` matches what was given at construction (`Self::new`).
/// 2. Caller promises that the projection of `row_ref`'s type's equals the index's key type.
unsafe fn insert(&mut self, cols: &ColList, row_ref: RowRef<'_>) -> Result<usize, RowPointer> {
fn project_to_singleton_key<T: ReadColumn>(cols: &ColList, row_ref: RowRef<'_>) -> T {
// Extract the column.
let col_pos = cols.as_singleton();
// SAFETY: Caller promised that `cols` matches what was given at construction (`Self::new`).
// In the case of `.clone_structure()`, the structure is preserved,
// so the promise is also preserved.
// This entails, that because we reached here, that `cols` is singleton.
let col_pos = unsafe { col_pos.unwrap_unchecked() }.idx();
// Extract the layout of the column.
let col_layouts = &row_ref.row_layout().product().elements;
// SAFETY:
// - Caller promised that projecting the `row_ref`'s type/layout to `self.indexed_columns`
// gives us the index's key type.
// This entails that each `ColId` in `self.indexed_columns`
// must be in-bounds of `row_ref`'s layout.
let col_layout = unsafe { col_layouts.get_unchecked(col_pos) };
// Read the column in `row_ref`.
// SAFETY:
// - `col_layout` was just derived from the row layout.
// - Caller promised that the type-projection of the row type/layout
// equals the index's key type.
// We've reached here, so the index's key type is compatible with `T`.
// - `self` is a valid row so offsetting to `col_layout` is valid.
unsafe { T::unchecked_read_column(row_ref, col_layout) }
}
fn mm_insert_at_type<T: Ord + ReadColumn + KeySize>(
this: &mut BtreeIndex<T>,
cols: &ColList,
row_ref: RowRef<'_>,
) -> Result<usize, RowPointer> {
let key: T = project_to_singleton_key(cols, row_ref);
let key_size = key.key_size_in_bytes();
this.insert(key, row_ref.pointer());
Ok(key_size)
}
fn um_insert_at_type<T: Ord + ReadColumn + KeySize>(
this: &mut BtreeUniqueIndex<T>,
cols: &ColList,
row_ref: RowRef<'_>,
) -> Result<usize, RowPointer> {
let key: T = project_to_singleton_key(cols, row_ref);
let key_size = key.key_size_in_bytes();
this.insert(key, row_ref.pointer())
.map_err(|ptr| *ptr)
.map(|_| key_size)
}
fn direct_insert_at_type<T: ReadColumn>(
this: &mut UniqueDirectIndex,
cols: &ColList,
row_ref: RowRef<'_>,
to_usize: impl FnOnce(T) -> usize,
) -> Result<usize, RowPointer> {
let key: T = project_to_singleton_key(cols, row_ref);
let key = to_usize(key);
let key_size = key.key_size_in_bytes();
this.insert(key, row_ref.pointer()).map(|_| key_size)
}
fn direct_u8_insert_at_type<T: ReadColumn>(
this: &mut UniqueDirectFixedCapIndex,
cols: &ColList,
row_ref: RowRef<'_>,
to_u8: impl FnOnce(T) -> usize,
) -> Result<usize, RowPointer> {
let key: T = project_to_singleton_key(cols, row_ref);
let key = to_u8(key);
let key_size = key.key_size_in_bytes();
this.insert(key, row_ref.pointer()).map(|_| key_size)
}
match self {
Self::BtreeBool(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeU8(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeSumTag(idx) => {
let SumTag(key) = project_to_singleton_key(cols, row_ref);
let key_size = key.key_size_in_bytes();
idx.insert(key, row_ref.pointer());
Ok(key_size)
}
Self::BtreeI8(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeU16(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeI16(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeU32(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeI32(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeU64(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeI64(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeU128(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeI128(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeU256(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeI256(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeF32(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeF64(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeString(idx) => mm_insert_at_type(idx, cols, row_ref),
Self::BtreeAV(this) => {
// SAFETY: Caller promised that any `col` in `cols` is in-bounds of `row_ref`'s layout.
let key = unsafe { row_ref.project_unchecked(cols) };
let key_size = key.key_size_in_bytes();
this.insert(key, row_ref.pointer());
Ok(key_size)
}
Self::UniqueBtreeBool(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeU8(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeSumTag(idx) => {
let SumTag(key) = project_to_singleton_key(cols, row_ref);
let key_size = key.key_size_in_bytes();
idx.insert(key, row_ref.pointer()).map_err(|ptr| *ptr).map(|_| key_size)
}
Self::UniqueBtreeI8(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeU16(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeI16(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeU32(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeI32(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeU64(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeI64(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeU128(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeI128(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeU256(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeI256(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeF32(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeF64(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeString(idx) => um_insert_at_type(idx, cols, row_ref),
Self::UniqueBtreeAV(this) => {
// SAFETY: Caller promised that any `col` in `cols` is in-bounds of `row_ref`'s layout.
let key = unsafe { row_ref.project_unchecked(cols) };
let key_size = key.key_size_in_bytes();
this.insert(key, row_ref.pointer())
.map_err(|ptr| *ptr)
.map(|_| key_size)
}
Self::UniqueDirectSumTag(idx) => direct_u8_insert_at_type(idx, cols, row_ref, |SumTag(tag)| tag as usize),
Self::UniqueDirectU8(idx) => direct_insert_at_type(idx, cols, row_ref, |k: u8| k as usize),
Self::UniqueDirectU16(idx) => direct_insert_at_type(idx, cols, row_ref, |k: u16| k as usize),
Self::UniqueDirectU32(idx) => direct_insert_at_type(idx, cols, row_ref, |k: u32| k as usize),
Self::UniqueDirectU64(idx) => direct_insert_at_type(idx, cols, row_ref, |k: u64| k as usize),
}
}
/// Remove the row referred to by `row_ref` from the index `self`,
/// which must be keyed at `cols`.
///
/// If `cols` is inconsistent with `self`,
/// or the `row_ref` has a row type other than that used for `self`,
/// this will behave oddly; it may return an error, do nothing,
/// or remove the wrong value from the index.
/// Note, however, that it will not invoke undefined behavior.
///
/// If the row was present and has been deleted, returns `Ok(Some(key_size_in_bytes))`,
/// where `key_size_in_bytes` is the size of the key.
/// [`TableIndex::delete`] will use this
/// to update the counter for [`TableIndex::num_key_bytes`].
/// We want to store said counter outside of the [`TypedIndex`] enum,
/// but we can only compute the size using type info within the [`TypedIndex`],
/// so we have to return the size across this boundary.
// TODO(centril): make this unsafe and use unchecked conversions.
fn delete(&mut self, cols: &ColList, row_ref: RowRef<'_>) -> Result<Option<usize>, InvalidFieldError> {
fn mm_delete_at_type<T: Ord + ReadColumn + KeySize>(
this: &mut BtreeIndex<T>,
cols: &ColList,
row_ref: RowRef<'_>,
) -> Result<Option<usize>, InvalidFieldError> {
let col_pos = cols.as_singleton().unwrap();
let key: T = row_ref.read_col(col_pos).map_err(|_| col_pos)?;
let key_size = key.key_size_in_bytes();
Ok(this.delete(&key, &row_ref.pointer()).then_some(key_size))
}
fn um_delete_at_type<T: Ord + ReadColumn + KeySize>(
this: &mut BtreeUniqueIndex<T>,
cols: &ColList,
row_ref: RowRef<'_>,
) -> Result<Option<usize>, InvalidFieldError> {
let col_pos = cols.as_singleton().unwrap();
let key: T = row_ref.read_col(col_pos).map_err(|_| col_pos)?;
let key_size = key.key_size_in_bytes();
Ok(this.delete(&key).then_some(key_size))
}
fn direct_delete_at_type<T: ReadColumn>(
this: &mut UniqueDirectIndex,
cols: &ColList,
row_ref: RowRef<'_>,
to_usize: impl FnOnce(T) -> usize,
) -> Result<Option<usize>, InvalidFieldError> {
let col_pos = cols.as_singleton().unwrap();
let key: T = row_ref.read_col(col_pos).map_err(|_| col_pos)?;
let key = to_usize(key);
let key_size = key.key_size_in_bytes();
Ok(this.delete(key).then_some(key_size))
}
fn direct_u8_delete_at_type<T: ReadColumn>(
this: &mut UniqueDirectFixedCapIndex,
cols: &ColList,
row_ref: RowRef<'_>,
to_u8: impl FnOnce(T) -> usize,
) -> Result<Option<usize>, InvalidFieldError> {
let col_pos = cols.as_singleton().unwrap();
let key: T = row_ref.read_col(col_pos).map_err(|_| col_pos)?;
let key = to_u8(key);
let key_size = key.key_size_in_bytes();
Ok(this.delete(key).then_some(key_size))
}
match self {
Self::BtreeBool(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeU8(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeSumTag(this) => {
let col_pos = cols.as_singleton().unwrap();
let SumTag(key) = row_ref.read_col(col_pos).map_err(|_| col_pos)?;
let key_size = key.key_size_in_bytes();
Ok(this.delete(&key, &row_ref.pointer()).then_some(key_size))
}
Self::BtreeI8(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeU16(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeI16(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeU32(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeI32(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeU64(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeI64(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeU128(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeI128(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeU256(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeI256(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeF32(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeF64(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeString(this) => mm_delete_at_type(this, cols, row_ref),
Self::BtreeAV(this) => {
let key = row_ref.project(cols)?;
let key_size = key.key_size_in_bytes();
Ok(this.delete(&key, &row_ref.pointer()).then_some(key_size))
}
Self::UniqueBtreeBool(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeU8(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeSumTag(this) => {
let col_pos = cols.as_singleton().unwrap();
let SumTag(key) = row_ref.read_col(col_pos).map_err(|_| col_pos)?;
let key_size = key.key_size_in_bytes();
Ok(this.delete(&key).then_some(key_size))
}
Self::UniqueBtreeI8(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeU16(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeI16(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeU32(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeI32(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeU64(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeI64(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeU128(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeI128(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeU256(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeI256(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeF32(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeF64(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeString(this) => um_delete_at_type(this, cols, row_ref),
Self::UniqueBtreeAV(this) => {
let key = row_ref.project(cols)?;
let key_size = key.key_size_in_bytes();
Ok(this.delete(&key).then_some(key_size))
}
Self::UniqueDirectSumTag(this) => direct_u8_delete_at_type(this, cols, row_ref, |SumTag(k)| k as usize),
Self::UniqueDirectU8(this) => direct_delete_at_type(this, cols, row_ref, |k: u8| k as usize),
Self::UniqueDirectU16(this) => direct_delete_at_type(this, cols, row_ref, |k: u16| k as usize),
Self::UniqueDirectU32(this) => direct_delete_at_type(this, cols, row_ref, |k: u32| k as usize),
Self::UniqueDirectU64(this) => direct_delete_at_type(this, cols, row_ref, |k: u64| k as usize),
}
}
fn seek_point(&self, key: &AlgebraicValue) -> TypedIndexPointIter<'_> {
fn mm_iter_at_type<'a, T: Ord>(
this: &'a BtreeIndex<T>,
key: &AlgebraicValue,
av_as_t: impl Fn(&AlgebraicValue) -> Option<&T>,
) -> BtreeIndexPointIter<'a> {
this.values_in_point(av_as_t(key).expect("key does not conform to key type of index"))
}
fn um_iter_at_type<'a, T: Ord>(
this: &'a BtreeUniqueIndex<T>,
key: &AlgebraicValue,
av_as_t: impl Fn(&AlgebraicValue) -> Option<&T>,
) -> BtreeUniqueIndexPointIter<'a> {
this.values_in_point(av_as_t(key).expect("key does not conform to key type of index"))
}
fn direct_iter_at_type<T>(
this: &UniqueDirectIndex,
key: &AlgebraicValue,
av_as_t: impl Fn(&AlgebraicValue) -> Option<&T>,
to_usize: impl Copy + FnOnce(&T) -> usize,
) -> UniqueDirectIndexPointIter {
let av_as_t = |v| av_as_t(v).expect("key does not conform to key type of index");
this.seek_point(to_usize(av_as_t(key)))
}
use TypedIndex::*;
use TypedIndexPointIter::*;
match self {
BtreeBool(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_bool)),
BtreeU8(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_u8)),
BtreeSumTag(this) => BTree(mm_iter_at_type(this, key, as_tag)),
BtreeI8(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_i8)),
BtreeU16(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_u16)),
BtreeI16(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_i16)),
BtreeU32(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_u32)),
BtreeI32(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_i32)),
BtreeU64(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_u64)),
BtreeI64(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_i64)),
BtreeU128(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_u128)),
BtreeI128(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_i128)),
BtreeU256(this) => BTree(mm_iter_at_type(this, key, |av| av.as_u256().map(|x| &**x))),
BtreeI256(this) => BTree(mm_iter_at_type(this, key, |av| av.as_i256().map(|x| &**x))),
BtreeF32(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_f32)),
BtreeF64(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_f64)),
BtreeString(this) => BTree(mm_iter_at_type(this, key, AlgebraicValue::as_string)),
BtreeAV(this) => BTree(this.values_in_point(key)),
UniqueBtreeBool(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_bool)),
UniqueBtreeU8(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_u8)),
UniqueBtreeSumTag(this) => UniqueBTree(um_iter_at_type(this, key, as_tag)),
UniqueBtreeI8(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_i8)),
UniqueBtreeU16(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_u16)),
UniqueBtreeI16(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_i16)),
UniqueBtreeU32(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_u32)),
UniqueBtreeI32(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_i32)),
UniqueBtreeU64(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_u64)),
UniqueBtreeI64(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_i64)),
UniqueBtreeU128(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_u128)),
UniqueBtreeI128(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_i128)),
UniqueBtreeU256(this) => UniqueBTree(um_iter_at_type(this, key, |av| av.as_u256().map(|x| &**x))),
UniqueBtreeI256(this) => UniqueBTree(um_iter_at_type(this, key, |av| av.as_i256().map(|x| &**x))),
UniqueBtreeF32(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_f32)),
UniqueBtreeF64(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_f64)),
UniqueBtreeString(this) => UniqueBTree(um_iter_at_type(this, key, AlgebraicValue::as_string)),
UniqueBtreeAV(this) => UniqueBTree(this.values_in_point(key)),
UniqueDirectSumTag(this) => {
let key = as_tag(key).expect("key does not conform to key type of index");
UniqueDirect(this.seek_point(*key as usize))
}
UniqueDirectU8(this) => {
UniqueDirect(direct_iter_at_type(this, key, AlgebraicValue::as_u8, |k| *k as usize))
}
UniqueDirectU16(this) => {
UniqueDirect(direct_iter_at_type(this, key, AlgebraicValue::as_u16, |k| *k as usize))
}
UniqueDirectU32(this) => {
UniqueDirect(direct_iter_at_type(this, key, AlgebraicValue::as_u32, |k| *k as usize))
}
UniqueDirectU64(this) => {
UniqueDirect(direct_iter_at_type(this, key, AlgebraicValue::as_u64, |k| *k as usize))
}
}
}
fn seek_range(&self, range: &impl RangeBounds<AlgebraicValue>) -> TypedIndexRangeIter<'_> {
// Copied from `RangeBounds::is_empty` as it's unstable.
// TODO(centril): replace once stable.
fn is_empty<T: PartialOrd>(bounds: &impl RangeBounds<T>) -> bool {
use core::ops::Bound::*;
!match (bounds.start_bound(), bounds.end_bound()) {
(Unbounded, _) | (_, Unbounded) => true,
(Included(start), Excluded(end))
| (Excluded(start), Included(end))
| (Excluded(start), Excluded(end)) => start < end,
(Included(start), Included(end)) => start <= end,
}
}
// Ensure we don't panic inside `BTreeMap::seek_range`.
if is_empty(range) {
return RangeEmpty;
}
fn mm_iter_at_type<'a, T: Ord>(
this: &'a BtreeIndex<T>,
range: &impl RangeBounds<AlgebraicValue>,
av_as_t: impl Fn(&AlgebraicValue) -> Option<&T>,
) -> BtreeIndexRangeIter<'a, T> {
let av_as_t = |v| av_as_t(v).expect("bound does not conform to key type of index");
let start = range.start_bound().map(av_as_t);
let end = range.end_bound().map(av_as_t);
this.values_in_range(&(start, end))
}
fn um_iter_at_type<'a, T: Ord>(
this: &'a BtreeUniqueIndex<T>,
range: &impl RangeBounds<AlgebraicValue>,
av_as_t: impl Fn(&AlgebraicValue) -> Option<&T>,
) -> BtreeUniqueIndexRangeIter<'a, T> {
let av_as_t = |v| av_as_t(v).expect("bound does not conform to key type of index");
let start = range.start_bound().map(av_as_t);
let end = range.end_bound().map(av_as_t);
this.values_in_range(&(start, end))
}
fn direct_iter_at_type<'a, T>(
this: &'a UniqueDirectIndex,
range: &impl RangeBounds<AlgebraicValue>,
av_as_t: impl Fn(&AlgebraicValue) -> Option<&T>,
to_usize: impl Copy + FnOnce(&T) -> usize,
) -> UniqueDirectIndexRangeIter<'a> {
let av_as_t = |v| av_as_t(v).expect("bound does not conform to key type of index");
let start = range.start_bound().map(av_as_t).map(to_usize);
let end = range.end_bound().map(av_as_t).map(to_usize);
this.seek_range(&(start, end))
}
use TypedIndexRangeIter::*;
match self {
Self::BtreeBool(this) => BtreeBool(mm_iter_at_type(this, range, AlgebraicValue::as_bool)),
Self::BtreeU8(this) => BtreeU8(mm_iter_at_type(this, range, AlgebraicValue::as_u8)),
Self::BtreeSumTag(this) => BtreeU8(mm_iter_at_type(this, range, as_tag)),
Self::BtreeI8(this) => BtreeI8(mm_iter_at_type(this, range, AlgebraicValue::as_i8)),
Self::BtreeU16(this) => BtreeU16(mm_iter_at_type(this, range, AlgebraicValue::as_u16)),
Self::BtreeI16(this) => BtreeI16(mm_iter_at_type(this, range, AlgebraicValue::as_i16)),
Self::BtreeU32(this) => BtreeU32(mm_iter_at_type(this, range, AlgebraicValue::as_u32)),
Self::BtreeI32(this) => BtreeI32(mm_iter_at_type(this, range, AlgebraicValue::as_i32)),
Self::BtreeU64(this) => BtreeU64(mm_iter_at_type(this, range, AlgebraicValue::as_u64)),
Self::BtreeI64(this) => BtreeI64(mm_iter_at_type(this, range, AlgebraicValue::as_i64)),
Self::BtreeU128(this) => BtreeU128(mm_iter_at_type(this, range, AlgebraicValue::as_u128)),
Self::BtreeI128(this) => BtreeI128(mm_iter_at_type(this, range, AlgebraicValue::as_i128)),
Self::BtreeU256(this) => BtreeU256(mm_iter_at_type(this, range, |av| av.as_u256().map(|x| &**x))),
Self::BtreeI256(this) => BtreeI256(mm_iter_at_type(this, range, |av| av.as_i256().map(|x| &**x))),
Self::BtreeF32(this) => BtreeF32(mm_iter_at_type(this, range, AlgebraicValue::as_f32)),
Self::BtreeF64(this) => BtreeF64(mm_iter_at_type(this, range, AlgebraicValue::as_f64)),
Self::BtreeString(this) => BtreeString(mm_iter_at_type(this, range, AlgebraicValue::as_string)),
Self::BtreeAV(this) => BtreeAV(this.values_in_range(range)),
Self::UniqueBtreeBool(this) => UniqueBtreeBool(um_iter_at_type(this, range, AlgebraicValue::as_bool)),
Self::UniqueBtreeU8(this) => UniqueBtreeU8(um_iter_at_type(this, range, AlgebraicValue::as_u8)),
Self::UniqueBtreeSumTag(this) => UniqueBtreeU8(um_iter_at_type(this, range, as_tag)),
Self::UniqueBtreeI8(this) => UniqueBtreeI8(um_iter_at_type(this, range, AlgebraicValue::as_i8)),
Self::UniqueBtreeU16(this) => UniqueBtreeU16(um_iter_at_type(this, range, AlgebraicValue::as_u16)),
Self::UniqueBtreeI16(this) => UniqueBtreeI16(um_iter_at_type(this, range, AlgebraicValue::as_i16)),
Self::UniqueBtreeU32(this) => UniqueBtreeU32(um_iter_at_type(this, range, AlgebraicValue::as_u32)),
Self::UniqueBtreeI32(this) => UniqueBtreeI32(um_iter_at_type(this, range, AlgebraicValue::as_i32)),
Self::UniqueBtreeU64(this) => UniqueBtreeU64(um_iter_at_type(this, range, AlgebraicValue::as_u64)),
Self::UniqueBtreeI64(this) => UniqueBtreeI64(um_iter_at_type(this, range, AlgebraicValue::as_i64)),
Self::UniqueBtreeU128(this) => UniqueBtreeU128(um_iter_at_type(this, range, AlgebraicValue::as_u128)),
Self::UniqueBtreeI128(this) => UniqueBtreeI128(um_iter_at_type(this, range, AlgebraicValue::as_i128)),
Self::UniqueBtreeF32(this) => UniqueBtreeF32(um_iter_at_type(this, range, AlgebraicValue::as_f32)),
Self::UniqueBtreeF64(this) => UniqueBtreeF64(um_iter_at_type(this, range, AlgebraicValue::as_f64)),
Self::UniqueBtreeU256(this) => {
UniqueBtreeU256(um_iter_at_type(this, range, |av| av.as_u256().map(|x| &**x)))
}
Self::UniqueBtreeI256(this) => {
UniqueBtreeI256(um_iter_at_type(this, range, |av| av.as_i256().map(|x| &**x)))
}
Self::UniqueBtreeString(this) => UniqueBtreeString(um_iter_at_type(this, range, AlgebraicValue::as_string)),
Self::UniqueBtreeAV(this) => UniqueBtreeAV(this.values_in_range(range)),
Self::UniqueDirectSumTag(this) => {
let av_as_t = |v| as_tag(v).copied().expect("bound does not conform to key type of index") as usize;
let start = range.start_bound().map(av_as_t);
let end = range.end_bound().map(av_as_t);
let iter = this.seek_range(&(start, end));
UniqueDirectU8(iter)
}
Self::UniqueDirectU8(this) => {
UniqueDirect(direct_iter_at_type(this, range, AlgebraicValue::as_u8, |k| *k as usize))
}
Self::UniqueDirectU16(this) => {
UniqueDirect(direct_iter_at_type(this, range, AlgebraicValue::as_u16, |k| {
*k as usize
}))
}
Self::UniqueDirectU32(this) => {
UniqueDirect(direct_iter_at_type(this, range, AlgebraicValue::as_u32, |k| {
*k as usize
}))
}
Self::UniqueDirectU64(this) => {
UniqueDirect(direct_iter_at_type(this, range, AlgebraicValue::as_u64, |k| {
*k as usize
}))
}
}
}
fn clear(&mut self) {
match self {
Self::BtreeBool(this) => this.clear(),
Self::BtreeU8(this) | Self::BtreeSumTag(this) => this.clear(),
Self::BtreeI8(this) => this.clear(),
Self::BtreeU16(this) => this.clear(),
Self::BtreeI16(this) => this.clear(),
Self::BtreeU32(this) => this.clear(),
Self::BtreeI32(this) => this.clear(),
Self::BtreeU64(this) => this.clear(),
Self::BtreeI64(this) => this.clear(),
Self::BtreeU128(this) => this.clear(),
Self::BtreeI128(this) => this.clear(),
Self::BtreeU256(this) => this.clear(),
Self::BtreeI256(this) => this.clear(),
Self::BtreeF32(this) => this.clear(),
Self::BtreeF64(this) => this.clear(),
Self::BtreeString(this) => this.clear(),
Self::BtreeAV(this) => this.clear(),
Self::UniqueBtreeBool(this) => this.clear(),
Self::UniqueBtreeU8(this) | Self::UniqueBtreeSumTag(this) => this.clear(),
Self::UniqueBtreeI8(this) => this.clear(),
Self::UniqueBtreeU16(this) => this.clear(),
Self::UniqueBtreeI16(this) => this.clear(),
Self::UniqueBtreeU32(this) => this.clear(),
Self::UniqueBtreeI32(this) => this.clear(),
Self::UniqueBtreeU64(this) => this.clear(),
Self::UniqueBtreeI64(this) => this.clear(),
Self::UniqueBtreeU128(this) => this.clear(),
Self::UniqueBtreeI128(this) => this.clear(),
Self::UniqueBtreeU256(this) => this.clear(),
Self::UniqueBtreeI256(this) => this.clear(),
Self::UniqueBtreeF32(this) => this.clear(),
Self::UniqueBtreeF64(this) => this.clear(),
Self::UniqueBtreeString(this) => this.clear(),
Self::UniqueBtreeAV(this) => this.clear(),
Self::UniqueDirectSumTag(this) => this.clear(),
Self::UniqueDirectU8(this)
| Self::UniqueDirectU16(this)
| Self::UniqueDirectU32(this)
| Self::UniqueDirectU64(this) => this.clear(),
}