range-filters 0.1.0

High-performance range filter implementation - DIVA (VLDB 2025 Best Research Paper)
Documentation
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
use crate::Key;
use crate::binary_search_tree::BinarySearchTreeGroup;
use crate::infix_store::InfixStore;
use crate::x_fast_trie::XFastTrie;
use std::fmt;
use std::sync::{Arc, RwLock};

pub struct YFastTrie {
    pub x_fast_trie: XFastTrie,
}

impl YFastTrie {
    pub fn new(no_levels: usize) -> Self {
        Self {
            x_fast_trie: XFastTrie::new(no_levels),
        }
    }

    pub fn new_with_keys(keys: &[Key], no_levels: usize) -> Self {
        if keys.is_empty() {
            return Self::new(no_levels);
        }

        // step 1: sort and dedup keys
        let mut sorted_keys = keys.to_vec();
        sorted_keys.sort();
        sorted_keys.dedup();

        let bst_group_size = no_levels;

        let mut x_fast_trie = XFastTrie::new(no_levels);

        // step 2: partition all keys into BST group chunks of size ~log U (e.g. 64 keys per group for 64 bit keys)
        for chunk_start in (0..sorted_keys.len()).step_by(bst_group_size) {
            let chunk_end = (chunk_start + bst_group_size).min(sorted_keys.len());
            let chunk = &sorted_keys[chunk_start..chunk_end];

            // boundary key is the first key of this chunk
            let boundary_key = chunk[0];

            // step 3: insert boundary key into x-fast trie
            x_fast_trie.insert(boundary_key);

            // step 4: create a balanced BST group with all keys in this chunk
            let bst_group = BinarySearchTreeGroup::new_with_keys(chunk);
            let bst_group_arc = Arc::new(RwLock::new(bst_group));

            // step 5: attach the BST group to the boundary representative
            if let Some(rep_node) = x_fast_trie.lookup(boundary_key) {
                if let Ok(mut rep) = rep_node.write() {
                    rep.bst_group = Some(bst_group_arc);
                }
            }
        }

        Self { x_fast_trie }
    }

    pub fn len(&self) -> usize {
        let mut total = 0;
        if let Some(head) = &self.x_fast_trie.head_rep {
            let mut current = Some(head.clone());
            while let Some(node) = current {
                if let Ok(n) = node.read() {
                    if let Some(bst_group) = &n.bst_group {
                        if let Ok(bst) = bst_group.read() {
                            total += bst.len();
                        }
                    }
                    current = n.right.as_ref().and_then(|w| w.upgrade());
                } else {
                    break;
                }
            }
        }
        total
    }

    pub fn sample_count(&self) -> usize {
        self.x_fast_trie.len()
    }

    pub fn get_infix_store(&self, key: Key) -> Option<Arc<RwLock<InfixStore>>> {
        // find the boundary representative
        let rep_node = self.x_fast_trie.lookup(key)?;
        let rep = rep_node.read().ok()?;

        // get the BST group and call its get_infix_store
        if let Some(bst_group) = &rep.bst_group {
            if let Ok(bst) = bst_group.read() {
                return bst.get_infix_store(key);
            }
        }

        None
    }

    pub fn set_infix_store(&mut self, key: Key, infix_store: InfixStore) {
        // find the boundary representative
        if let Some(rep_node) = self.x_fast_trie.predecessor(key) {
            if let Ok(rep) = rep_node.read() {
                if let Some(bst_group) = &rep.bst_group {
                    if let Ok(mut bst) = bst_group.write() {
                        bst.set_infix_store(key, infix_store);
                    }
                }
            }
        }
    }

    // TODO: add insert method
    // TODO: add next, previous methods
    // TODO: create an iterator for the trie

    pub fn predecessor(&self, key: Key) -> Option<Key> {
        // find the boundary representative
        let rep_node = self.x_fast_trie.predecessor(key)?;
        let rep = rep_node.read().ok()?;

        // search within the BST group
        if let Some(bst_group) = &rep.bst_group {
            if let Ok(bst) = bst_group.read() {
                return bst.predecessor(key);
            }
        }

        Some(rep.key)
    }

    pub fn predecessor_infix_store(&self, key: Key) -> Option<Arc<RwLock<InfixStore>>> {
        // find boundary via x-fast trie
        let rep_node = self.x_fast_trie.predecessor(key)?;
        let rep = rep_node.read().ok()?;

        // get the BST group and call its predecessor_infix_store
        if let Some(bst_group) = &rep.bst_group {
            if let Ok(bst) = bst_group.read() {
                return bst.predecessor_infix_store(key);
            }
        }
        None
    }

    pub fn successor_infix_store(&self, key: Key) -> Option<Arc<RwLock<InfixStore>>> {
        // find the containing bucket via predecessor boundary
        if let Some(rep_node) = self.x_fast_trie.predecessor(key) {
            if let Ok(rep) = rep_node.read() {
                // search within the BST group
                if let Some(bst_group) = &rep.bst_group {
                    if let Ok(bst) = bst_group.read() {
                        if let Some(result) = bst.successor_infix_store(key) {
                            return Some(result);
                        }
                    }
                }

                // key is > all keys in this bucket, try next bucket
                if let Some(next_weak) = &rep.right {
                    if let Some(next_rep) = next_weak.upgrade() {
                        if let Ok(next) = next_rep.read() {
                            if let Some(bst_group) = &next.bst_group {
                                if let Ok(bst) = bst_group.read() {
                                    return bst.get_infix_store(next.key);
                                }
                            }
                        }
                    }
                }
            }
        }

        None
    }

    pub fn successor(&self, key: Key) -> Option<Key> {
        // find the containing bucket via predecessor boundary
        if let Some(rep_node) = self.x_fast_trie.predecessor(key) {
            if let Ok(rep) = rep_node.read() {
                // search within the BST group
                if let Some(bst_group) = &rep.bst_group {
                    if let Ok(bst) = bst_group.read() {
                        if let Some(result) = bst.successor(key) {
                            return Some(result);
                        }
                    }
                }

                // key is > all keys in this bucket, try next bucket
                if let Some(next_weak) = &rep.right {
                    if let Some(next_rep) = next_weak.upgrade() {
                        if let Ok(next) = next_rep.read() {
                            return Some(next.key);
                        }
                    }
                }
            }
        } else {
            // key < first boundary, return first key
            if let Some(head) = &self.x_fast_trie.head_rep {
                if let Ok(head_guard) = head.read() {
                    return Some(head_guard.key);
                }
            }
        }

        None
    }

    pub fn contains(&self, key: Key) -> bool {
        // first check x-fast trie for direct hit
        if self.x_fast_trie.lookup(key).is_some() {
            return true;
        }

        // find the predecessor boundary representative
        if let Some(rep_node) = self.x_fast_trie.predecessor(key) {
            if let Ok(rep) = rep_node.read() {
                // then check if key is in the BST group
                if let Some(bst_group) = &rep.bst_group {
                    if let Ok(bst) = bst_group.read() {
                        return bst.contains(key);
                    }
                }
            }
        }
        false
    }

    pub fn pretty_print(&self) {
        print!("{}", self);
    }

    // helper to collect all keys from BST in sorted order
    fn collect_bst_keys(node: &Option<Box<crate::binary_search_tree::TreeNode>>) -> Vec<Key> {
        let mut keys = Vec::new();
        Self::collect_bst_keys_recursive(node, &mut keys);
        keys
    }

    fn collect_bst_keys_recursive(
        node: &Option<Box<crate::binary_search_tree::TreeNode>>,
        keys: &mut Vec<Key>,
    ) {
        if let Some(n) = node {
            Self::collect_bst_keys_recursive(&n.left, keys);
            keys.push(n.key);
            Self::collect_bst_keys_recursive(&n.right, keys);
        }
    }
}

impl fmt::Display for YFastTrie {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(
            f,
            "\n╔════════════════════════════════════════════════════════╗"
        )?;
        writeln!(
            f,
            "║             Y-FAST TRIE STRUCTURE                      ║"
        )?;
        writeln!(
            f,
            "╚════════════════════════════════════════════════════════╝"
        )?;

        // print stats
        writeln!(f, "\nStats:")?;
        writeln!(f, "  Total keys:        {}", self.len())?;
        writeln!(f, "  Sample count:      {}", self.sample_count())?;
        writeln!(f, "  Levels:            {}", self.x_fast_trie.no_levels)?;

        // print x-fast trie structure
        write!(f, "{}", self.x_fast_trie)?;

        // print BST groups for each representative
        writeln!(
            f,
            "\n╔════════════════════════════════════════════════════════╗"
        )?;
        writeln!(
            f,
            "║             BST GROUPS (per representative)            ║"
        )?;
        writeln!(
            f,
            "╚════════════════════════════════════════════════════════╝\n"
        )?;

        if let Some(head) = &self.x_fast_trie.head_rep {
            let mut current = Some(head.clone());
            let mut bucket_index = 0;

            while let Some(node) = current {
                if let Ok(n) = node.read() {
                    writeln!(f, "Bucket {} (representative: {})", bucket_index, n.key)?;

                    if let Some(bst_group) = &n.bst_group {
                        if let Ok(bst) = bst_group.read() {
                            write!(f, "{}", bst)?;

                            // check for InfixStores attached to keys in this BST
                            let keys = Self::collect_bst_keys(&bst.root);
                            let mut infix_stats = Vec::new();

                            for &key in &keys {
                                if let Some(infix_store_arc) = bst.get_infix_store(key) {
                                    if let Ok(infix_store) = infix_store_arc.read() {
                                        infix_stats.push((
                                            key,
                                            infix_store.elem_count(),
                                            infix_store.remainder_size(),
                                            infix_store.num_slots(),
                                        ));
                                    }
                                }
                            }

                            if !infix_stats.is_empty() {
                                writeln!(f, "  InfixStores:")?;
                                for (key, elem_count, remainder_size, num_slots) in infix_stats {
                                    writeln!(
                                        f,
                                        "    Key {}: {} elements, {} bit remainder, {} slots",
                                        key, elem_count, remainder_size, num_slots
                                    )?;
                                }
                            }
                        }
                    } else {
                        writeln!(f, "  (no BST group attached)")?;
                    }

                    current = n.right.as_ref().and_then(|w| w.upgrade());
                    bucket_index += 1;
                } else {
                    break;
                }
            }
        } else {
            writeln!(f, "  (no buckets)")?;
        }

        writeln!(
            f,
            "\n╔════════════════════════════════════════════════════════╗"
        )?;
        writeln!(
            f,
            "║                    END Y-FAST TRIE                     ║"
        )?;
        writeln!(
            f,
            "╚════════════════════════════════════════════════════════╝\n"
        )?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_single_key() {
        let trie = YFastTrie::new_with_keys(&[42], 8);
        assert!(trie.contains(42));
    }

    #[test]
    fn test_basic_contains() {
        let keys = vec![10, 20, 30, 40, 50, 60, 70, 80];
        let trie = YFastTrie::new_with_keys(&keys, 8);

        for &key in &keys {
            assert!(trie.contains(key), "key {} should be in trie", key);
        }

        assert!(!trie.contains(5));
        assert!(!trie.contains(15));
        assert!(!trie.contains(85));
    }

    #[test]
    fn test_large_set() {
        // create 100 keys: 0, 10, 20, ..., 990
        let keys: Vec<Key> = (0..100).map(|i| i * 10).collect();
        let trie = YFastTrie::new_with_keys(&keys, 16);

        // verify all keys exist
        for &key in &keys {
            assert!(trie.contains(key), "key {} should exist", key);
        }

        // verify non-existent keys
        assert!(!trie.contains(5));
        assert!(!trie.contains(15));
        assert!(!trie.contains(995));
    }

    #[test]
    fn test_boundary_keys() {
        // with bst_group_size=8, these keys create 5 groups with boundaries: 0, 8, 16, 24, 32
        let keys: Vec<Key> = (0..40).collect();
        let trie = YFastTrie::new_with_keys(&keys, 8);

        // verify boundary keys are in x-fast
        assert!(trie.x_fast_trie.lookup(0).is_some());
        assert!(trie.x_fast_trie.lookup(8).is_some());
        assert!(trie.x_fast_trie.lookup(16).is_some());
        assert!(trie.x_fast_trie.lookup(24).is_some());
        assert!(trie.x_fast_trie.lookup(32).is_some());

        // verify non-boundary keys are NOT in x-fast
        assert!(trie.x_fast_trie.lookup(1).is_none());
        assert!(trie.x_fast_trie.lookup(9).is_none());
        assert!(trie.x_fast_trie.lookup(17).is_none());

        // but all keys should be in the trie overall
        for key in 0..40 {
            assert!(trie.contains(key), "key {} should be in trie", key);
        }
    }

    #[test]
    fn test_predecessor() {
        let keys = vec![10, 20, 30, 40, 50];
        let trie = YFastTrie::new_with_keys(&keys, 8);

        // exact matches
        assert_eq!(trie.predecessor(10), Some(10));
        assert_eq!(trie.predecessor(30), Some(30));
        assert_eq!(trie.predecessor(50), Some(50));

        // between keys
        assert_eq!(trie.predecessor(15), Some(10));
        assert_eq!(trie.predecessor(25), Some(20));
        assert_eq!(trie.predecessor(35), Some(30));
        assert_eq!(trie.predecessor(45), Some(40));

        // before first key
        assert_eq!(trie.predecessor(5), None);

        // after last key
        assert_eq!(trie.predecessor(60), Some(50));
    }

    #[test]
    fn test_successor() {
        let keys = vec![10, 20, 30, 40, 50];
        let trie = YFastTrie::new_with_keys(&keys, 8);

        // exact matches
        assert_eq!(trie.successor(10), Some(10));
        assert_eq!(trie.successor(30), Some(30));
        assert_eq!(trie.successor(50), Some(50));

        // between keys
        assert_eq!(trie.successor(15), Some(20));
        assert_eq!(trie.successor(25), Some(30));
        assert_eq!(trie.successor(35), Some(40));
        assert_eq!(trie.successor(45), Some(50));

        // before first key
        assert_eq!(trie.successor(5), Some(10));

        // after last key
        assert_eq!(trie.successor(60), None);
    }

    #[test]
    fn test_predecessor_successor_across_boundaries() {
        // 40 keys create boundaries at: 0, 8, 16, 24, 32
        let keys: Vec<Key> = (0..40).collect();
        let trie = YFastTrie::new_with_keys(&keys, 8);

        // test across BST group boundaries
        assert_eq!(trie.predecessor(7), Some(7));
        assert_eq!(trie.predecessor(8), Some(8));
        assert_eq!(trie.predecessor(9), Some(9));

        assert_eq!(trie.successor(7), Some(7));
        assert_eq!(trie.successor(8), Some(8));
        assert_eq!(trie.successor(9), Some(9));

        // between groups
        assert_eq!(trie.predecessor(15), Some(15));
        assert_eq!(trie.successor(15), Some(15));

        assert_eq!(trie.predecessor(16), Some(16));
        assert_eq!(trie.successor(16), Some(16));

        assert_eq!(trie.predecessor(17), Some(17));
        assert_eq!(trie.successor(17), Some(17));
    }

    #[test]
    fn test_infix_stores() {
        use crate::infix_store::InfixStore;

        // 24 keys: boundaries at 0, 24, 48
        let keys: Vec<Key> = (0..24).map(|i| i * 3).collect();
        let trie = YFastTrie::new_with_keys(&keys, 8);

        // attach infix stores to some keys across buckets
        let store_6 = InfixStore::default();
        let store_12 = InfixStore::default();
        let store_30 = InfixStore::default();

        // manually set infix stores in BST groups
        if let Some(rep) = trie.x_fast_trie.lookup(0) {
            if let Ok(r) = rep.read() {
                if let Some(bst_group) = &r.bst_group {
                    if let Ok(mut bst) = bst_group.write() {
                        bst.set_infix_store(6, store_6);
                    }
                }
            }
        }

        if let Some(rep) = trie.x_fast_trie.lookup(0) {
            if let Ok(r) = rep.read() {
                if let Some(bst_group) = &r.bst_group {
                    if let Ok(mut bst) = bst_group.write() {
                        bst.set_infix_store(12, store_12);
                    }
                }
            }
        }

        if let Some(rep) = trie.x_fast_trie.lookup(24) {
            if let Ok(r) = rep.read() {
                if let Some(bst_group) = &r.bst_group {
                    if let Ok(mut bst) = bst_group.write() {
                        bst.set_infix_store(30, store_30);
                    }
                }
            }
        }

        // get reference stores for comparison
        let ref_store_6 = {
            let rep = trie.x_fast_trie.lookup(0).unwrap();
            let r = rep.read().unwrap();
            let bst_group = r.bst_group.as_ref().unwrap();
            let bst = bst_group.read().unwrap();
            bst.get_infix_store(6).unwrap()
        };

        let ref_store_12 = {
            let rep = trie.x_fast_trie.lookup(0).unwrap();
            let r = rep.read().unwrap();
            let bst_group = r.bst_group.as_ref().unwrap();
            let bst = bst_group.read().unwrap();
            bst.get_infix_store(12).unwrap()
        };

        let ref_store_30 = {
            let rep = trie.x_fast_trie.lookup(24).unwrap();
            let r = rep.read().unwrap();
            let bst_group = r.bst_group.as_ref().unwrap();
            let bst = bst_group.read().unwrap();
            println!("bst: {:?}", bst);
            bst.get_infix_store(30).unwrap()
        };

        assert!(Arc::ptr_eq(
            &trie.predecessor_infix_store(8).unwrap(),
            &ref_store_6
        ));
        assert!(Arc::ptr_eq(
            &trie.predecessor_infix_store(12).unwrap(),
            &ref_store_12
        ));
        assert!(Arc::ptr_eq(
            &trie.predecessor_infix_store(31).unwrap(),
            &ref_store_30
        ));
        // assert!(Arc::ptr_eq(&trie.predecessor_infix_store(100).unwrap(), &ref_store_30));

        assert!(Arc::ptr_eq(
            &trie.successor_infix_store(5).unwrap(),
            &ref_store_6
        ));
        assert!(Arc::ptr_eq(
            &trie.successor_infix_store(10).unwrap(),
            &ref_store_12
        ));
        assert!(Arc::ptr_eq(
            &trie.successor_infix_store(29).unwrap(),
            &ref_store_30
        ));

        // extreme ends
        assert!(trie.predecessor_infix_store(2).is_none());
        assert!(trie.successor_infix_store(1000).is_none());
    }
}