dbsp 0.292.0

Continuous streaming analytics engine
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
//! A generic cursor implementation merging multiple cursors.

use crate::dynamic::{DataTrait, Factory, WeightTrait};
use crate::trace::cursor::Position;
use crate::utils::binary_heap::BinaryHeap;
use std::marker::PhantomData;
use std::mem::{take, transmute};
use std::{any::TypeId, cmp::Ordering};

use super::{Cursor, Direction};

/// Provides a cursor interface over a list of cursors.
///
/// The `CursorList` tracks the indices of cursors with the minimum key, and
/// the indices of cursors with the minimum key and minimum value.
///
/// # Design
///
/// The implementation uses a binary heap to keep the cursors partially
/// sorted by key and value. This allows us to find the new min/max
/// key/value in logarithmic time compared to linear time for a naive linear scan.
///
/// Why binary heap and not a tournament tree? Tournament trees are generally better
/// for merge sorting, as they require exactly log(n) comparisons to find the new
/// winner vs the worst-case 2 x log(n) comparisons for a binary heap. The problem
/// with tournament trees is that they don't offer an efficient way to peek _all_
/// max/min values, and not just one (you need to remove or update the current max first
/// and pay another log(n) comparison to find the next max). In contrast, our customized
/// binary heap implementation offers an efficient way to peek all max/min values at once.
pub struct CursorList<K, V, T, R: WeightTrait, C>
where
    K: DataTrait + ?Sized,
    V: DataTrait + ?Sized,
    T: 'static,
    R: WeightTrait + ?Sized,
    C: Cursor<K, V, T, R>,
{
    cursors: Vec<C>,

    /// Indexes of cursors that hold the current minimum key.
    current_key: Vec<usize>,

    /// Indexes of cursors in `current_key` in `key_priority_heap`.
    current_key_indexes: Vec<usize>,

    /// A priority heap that keeps cursors partially sorted by key.
    /// The first element is the index of the cursor in `cursors`,
    /// the second element is a pointer to the key. It should be equal
    /// to `cursors[index].key()`.
    ///
    /// # Safety
    ///
    /// We assume that the key reference returned by the cursor remains valid
    /// until the cursor moves to the next key. This is not officially part of the
    /// cursor API and I hope we can find a more strongly typed way to achieve this;
    /// however both in-memory and file-backed batches we have today are well-behaved
    /// in this regard.
    key_priority_heap: Vec<(usize, &'static K)>,

    /// Indexes of cursors that hold the current minimum value.
    current_val: Vec<usize>,

    /// Indexes of cursors in `current_val` in `val_priority_heap`.
    current_val_indexes: Vec<usize>,

    /// A priority heap that keeps cursors partially sorted by value.
    val_priority_heap: Vec<(usize, &'static V)>,

    /// The direction is None after calling `seek_key_exact`. In this case we don't sort the cursors by key.
    /// step_key, seek_key, etc., cannot be called on such a cursor.
    #[cfg(debug_assertions)]
    key_direction: Option<Direction>,
    #[cfg(debug_assertions)]
    val_direction: Direction,
    weight: Box<R>,
    /// Scratch space for use by `peek_all`.
    scratch: Vec<usize>,
    weight_factory: &'static dyn Factory<R>,
    __type: PhantomData<fn(&K, &V, &T, &R)>,
}

impl<K, V, T, R, C> CursorList<K, V, T, R, C>
where
    K: DataTrait + ?Sized,
    V: DataTrait + ?Sized,
    R: WeightTrait + ?Sized,
    C: Cursor<K, V, T, R>,
    T: 'static,
{
    /// Creates a new cursor list from pre-existing cursors.
    pub fn new(weight_factory: &'static dyn Factory<R>, cursors: Vec<C>) -> Self {
        let num_cursors = cursors.len();
        let mut result = Self {
            cursors,
            current_key: Vec::new(),
            current_key_indexes: Vec::new(),
            key_priority_heap: Vec::with_capacity(num_cursors),
            current_val: Vec::new(),
            current_val_indexes: Vec::new(),
            val_priority_heap: Vec::with_capacity(num_cursors),
            #[cfg(debug_assertions)]
            key_direction: Some(Direction::Forward),
            #[cfg(debug_assertions)]
            val_direction: Direction::Forward,
            weight: weight_factory.default_box(),
            weight_factory,
            scratch: Vec::new(),
            __type: PhantomData,
        };

        result.minimize_keys();

        result.skip_zero_weight_keys_forward();

        result
    }

    #[cfg(debug_assertions)]
    fn set_key_direction(&mut self, direction: Option<Direction>) {
        self.key_direction = direction;
    }

    #[cfg(not(debug_assertions))]
    fn set_key_direction(&mut self, _direction: Option<Direction>) {}

    #[cfg(debug_assertions)]
    fn set_val_direction(&mut self, direction: Direction) {
        self.val_direction = direction;
    }

    #[cfg(not(debug_assertions))]
    fn set_val_direction(&mut self, _direction: Direction) {}

    #[cfg(debug_assertions)]
    fn assert_key_direction(&self, direction: Direction) {
        debug_assert_eq!(self.key_direction, Some(direction));
    }

    #[cfg(not(debug_assertions))]
    fn assert_key_direction(&self, _direction: Direction) {}

    #[cfg(debug_assertions)]
    fn assert_val_direction(&self, direction: Direction) {
        debug_assert_eq!(self.val_direction, direction);
    }

    #[cfg(not(debug_assertions))]
    fn assert_val_direction(&self, _direction: Direction) {}

    fn is_zero_weight(&mut self) -> bool {
        if TypeId::of::<T>() == TypeId::of::<()>() {
            debug_assert!(self.key_valid());
            debug_assert!(self.val_valid());
            debug_assert!(self.cursors[self.current_val[0]].val_valid());
            self.weight.as_mut().set_zero();
            for &index in self.current_val.iter() {
                // TODO: use weight_checked
                self.cursors[index].map_times(&mut |_, w| self.weight.add_assign(w));
            }
            self.weight.is_zero()
        } else {
            false
        }
    }

    fn skip_zero_weight_vals_forward(&mut self) {
        self.assert_val_direction(Direction::Forward);

        while self.val_valid() && self.is_zero_weight() {
            for &index in self.current_val.iter() {
                self.cursors[index].step_val();
            }
            self.update_min_vals();
        }
    }

    fn skip_zero_weight_vals_reverse(&mut self) {
        self.assert_val_direction(Direction::Backward);

        while self.val_valid() && self.is_zero_weight() {
            for &index in self.current_val.iter() {
                self.cursors[index].step_val_reverse();
            }
            self.update_max_vals();
        }
    }

    fn skip_zero_weight_keys_forward(&mut self) {
        self.assert_key_direction(Direction::Forward);

        while self.key_valid() {
            self.skip_zero_weight_vals_forward();
            if self.val_valid() {
                break;
            }
            for &index in self.current_key.iter() {
                self.cursors[index].step_key();
            }
            self.set_val_direction(Direction::Forward);
            self.update_min_keys();
        }
    }

    fn skip_zero_weight_keys_reverse(&mut self) {
        self.assert_key_direction(Direction::Backward);

        while self.key_valid() {
            self.skip_zero_weight_vals_forward();
            if self.val_valid() {
                break;
            }
            for &index in self.current_key.iter() {
                self.cursors[index].step_key_reverse();
            }
            self.set_val_direction(Direction::Forward);
            self.update_max_keys();
        }
    }

    /// Find all cursors that point to the maximum (according to `comparator`) key and store the indexes in `current_key`.
    ///
    /// Uses naive linear scan to find the maximum key by performing num_cursors comparisons.
    /// This is a fallback used for small number of cursors where a linear scan is faster than
    /// maintaining the binary heap.
    fn current_keys_linear(&mut self, comparator: impl Fn(&K, &K) -> Ordering) {
        self.current_key.clear();

        // Determine the index of the cursor with minimum key.
        let mut max_key_opt: Option<&K> = None;
        for (index, cursor) in self.cursors.iter().enumerate() {
            if let Some(key) = cursor.get_key() {
                if let Some(max_key_opt) = &mut max_key_opt {
                    match (comparator)(key, max_key_opt) {
                        Ordering::Greater => {
                            *max_key_opt = key;
                            self.current_key.clear();
                            self.current_key.push(index);
                        }
                        Ordering::Equal => {
                            self.current_key.push(index);
                        }
                        _ => (),
                    }
                } else {
                    max_key_opt = Some(key);
                    self.current_key.push(index);
                }
            }
        }
    }

    /// Update current_key to contain the indexes of cursors with the maximum (according to `comparator`) key.
    ///
    /// If `rebuild` is true, assumes that all cursors have moved from their previous positions.
    /// Otherwise, it assumes that only the cursors in `current_key` have moved.
    ///
    /// The implementation uses maximize_keys_linear for a small number of cursors. For larger numbers of cursors,
    /// it uses a binary heap to keep the cursors partially sorted by key.
    fn sort_keys(&mut self, comparator: impl Fn(&K, &K) -> Ordering, rebuild: bool) {
        self.assert_val_direction(Direction::Forward);

        // Use linear scan for small number of cursors.
        //
        // Binary heap cost:
        //
        // if rebuild is true:
        //   cost <= 2*num_cursors + 2.
        // else:
        //   costs <= 2*log2(num_cursors) + 2.
        //
        // The +2 component above is the cost of peek_all assuming a single max element;
        // in general it's 2x the number of max elements.
        //
        // Linear scan cost: num_cursors-1 in both cases.
        //
        // We pick 5 as a break-even point beyond which the binary heap is faster.
        match self.cursors.len() {
            0 => {
                self.current_key.clear();
                return;
            }
            1 => {
                self.current_key.clear();
                // SAFETY: `cursors.len() = 1`.
                if self.cursors[0].key_valid() {
                    self.current_key.push(0);
                }
                return;
            }
            n if n <= 5 => {
                self.current_keys_linear(comparator);
                return;
            }
            _ => {}
        }

        let cmp = |a: &(usize, &'static K), b: &(usize, &'static K)| comparator(a.1, b.1);

        let heap = if rebuild {
            // Build heap from scratch.
            self.key_priority_heap.clear();
            for (i, cursor) in self.cursors.iter().enumerate() {
                if let Some(key) = cursor.get_key() {
                    // SAFETY: We will not access the key after the cursor is moved.
                    self.key_priority_heap
                        .push((i, unsafe { transmute::<&K, &'static K>(key) }))
                }
            }

            BinaryHeap::<(usize, &'static K), _>::from_vec(take(&mut self.key_priority_heap), cmp)
        } else {
            // Start with a previously built heap.
            let mut heap = unsafe {
                BinaryHeap::<(usize, &'static K), _>::from_vec_unchecked(
                    take(&mut self.key_priority_heap),
                    cmp,
                )
            };

            // We may have moved cursors in `current_key` since the last time we built the heap
            // and need to update the heap accordingly.
            // IMPORTANT: we iterate over the indexes returned by peek_all in reverse order, which
            // guarantees the modifying or removing the elements does not affect the indexes of the
            // remaining elements.
            for (pos, i) in self
                .current_key_indexes
                .iter()
                .rev()
                .zip(self.current_key.iter().rev())
            {
                // If the cursor still points to a valid key, update the key in the heap; otherwise,
                // remove the cursor from the heap.
                if let Some(key) = unsafe { self.cursors.get_unchecked(*i).get_key() } {
                    // SAFETY: We know that `index` is in bounds since it's in `current_key`.
                    // SAFETY: We will not access the key after the cursor is moved.
                    unsafe {
                        heap.update_pos_sift_down(*pos, ((*i), transmute::<&K, &'static K>(key)))
                    };
                } else {
                    heap.remove(*pos);
                }
            }

            heap
        };

        self.current_key.clear();
        self.current_key_indexes.clear();

        // Find the new set of cursors with the maximum key and record them in `current_key` and `current_key_indexes`,
        // without removing them from the heap. This is 2x more efficient that removing them and re-inserting during the
        // next call to sort_keys.
        heap.peek_all(
            |pos, &(i, _)| {
                self.current_key.push(i);
                self.current_key_indexes.push(pos);
            },
            &mut self.scratch,
        );

        self.key_priority_heap = heap.into_vec();
    }

    /// Find all cursors that point to the maximum (according to `comparator`) value and store the indexes in `current_val`.
    ///
    /// Uses naive linear scan to find the maximum value by performing current_key.len() comparisons.
    /// This is a fallback used for small number of cursors where a linear scan is faster than
    /// maintaining the binary heap.
    fn current_vals_linear(&mut self, comparator: impl Fn(&V, &V) -> Ordering) {
        self.current_val.clear();

        let mut max_val: Option<&V> = None;
        for &index in self.current_key.iter() {
            // SAFETY: `index` is in bounds since it's in `current_key`.
            if let Some(val) = unsafe { self.cursors.get_unchecked(index).get_val() } {
                if let Some(max_val) = &mut max_val {
                    match (comparator)(val, max_val) {
                        Ordering::Greater => {
                            *max_val = val;
                            self.current_val.clear();
                            self.current_val.push(index);
                        }
                        Ordering::Equal => self.current_val.push(index),
                        _ => (),
                    }
                } else {
                    max_val = Some(val);
                    self.current_val.push(index);
                }
            }
        }
    }

    /// Update `current_val` to contain the indexes of cursors with the maximum (according to `comparator`) values.
    ///
    /// If `rebuild` is true, assumes that all cursors have moved from their previous positions.
    /// Otherwise, it assumes that only the cursors in `current_val` have moved.
    ///
    /// The implementation uses maximize_vals_linear for a small number of cursors. For larger numbers of cursors,
    /// it uses a binary heap to keep the cursors partially sorted by value.
    fn sort_vals(&mut self, comparator: impl Fn(&V, &V) -> Ordering, rebuild: bool) {
        debug_assert!(
            self.current_key
                .iter()
                .all(|i| self.cursors[*i].key_valid())
        );

        match self.current_key.len() {
            0 => {
                self.current_val.clear();
                return;
            }
            1 => {
                self.current_val.clear();
                let i = unsafe { *self.current_key.get_unchecked(0) };
                if unsafe { self.cursors.get_unchecked(i) }.val_valid() {
                    self.current_val.push(i);
                }
                return;
            }
            n if n <= 5 => {
                self.current_vals_linear(comparator);
                return;
            }
            _ => {}
        }

        let cmp = |a: &(usize, &'static V), b: &(usize, &'static V)| comparator(a.1, b.1);

        let heap = if rebuild {
            self.val_priority_heap.clear();
            for i in self.current_key.iter() {
                if let Some(val) = unsafe { self.cursors.get_unchecked(*i).get_val() } {
                    // SAFETY: We will not access the value after the cursor is moved.
                    self.val_priority_heap
                        .push(((*i), unsafe { transmute::<&V, &'static V>(val) }));
                }
            }
            BinaryHeap::<(usize, &'static V), _>::from_vec(take(&mut self.val_priority_heap), cmp)
        } else {
            let mut heap = unsafe {
                BinaryHeap::<(usize, &'static V), _>::from_vec_unchecked(
                    take(&mut self.val_priority_heap),
                    cmp,
                )
            };

            for (pos, i) in self
                .current_val_indexes
                .iter()
                .rev()
                .zip(self.current_val.iter().rev())
            {
                if let Some(val) = unsafe { self.cursors.get_unchecked(*i).get_val() } {
                    // SAFETY: We will not access the value after the cursor is moved.
                    unsafe {
                        heap.update_pos_sift_down(*pos, ((*i), transmute::<&V, &'static V>(val)))
                    };
                } else {
                    heap.remove(*pos);
                }
            }

            heap
        };

        self.current_val.clear();
        self.current_val_indexes.clear();
        heap.peek_all(
            |pos, &(i, _)| {
                self.current_val.push(i);
                self.current_val_indexes.push(pos);
            },
            &mut self.scratch,
        );

        self.val_priority_heap = heap.into_vec();
    }

    /// Sort all cursors by key (ascending); initialize current_key with the indices of cursors with the minimum key.
    ///
    /// Invoked after calling seek_key, seek_key_with, or rewind_keys on all cursors.
    ///
    /// Once finished, it invokes `minimize_vals()` to ensure the value cursor is
    /// in a consistent state as well.
    fn minimize_keys(&mut self) {
        self.assert_key_direction(Direction::Forward);

        self.sort_keys(|c1, c2| c2.cmp(c1), true);
        self.minimize_vals();
    }

    /// Sort all cursors by key (descending); initialize current_key with the indices of cursors with the maximum key.
    ///
    /// Invoked after calling seek_key_reverse, seek_key_with_reverse, or fast_forward_keys on all cursors.
    ///
    /// Once finished, it invokes `minimize_vals()` to ensure the value cursor is
    /// in a consistent state as well.
    fn maximize_keys(&mut self) {
        self.assert_key_direction(Direction::Backward);

        self.sort_keys(|c1, c2| c1.cmp(c2), true);
        self.minimize_vals();
    }

    /// Sort all cursors is self.current_key by values (ascending); initialize current_val with the
    /// indices of cursors with the minimum value.
    fn minimize_vals(&mut self) {
        self.assert_val_direction(Direction::Forward);

        self.sort_vals(|c1, c2| c2.cmp(c1), true);
    }

    /// Sort all cursors is self.current_key by values (descending); initialize current_val with the
    /// indices of cursors with the maximum value.
    fn maximize_vals(&mut self) {
        self.assert_val_direction(Direction::Backward);

        self.sort_vals(|c1, c2| c1.cmp(c2), true);
    }

    /// Update the current_key array after stepping all cursors in self.current_key.
    ///
    /// Assumes that cursors in key_priority_heap are valid and sorted by key (ascending).
    ///
    /// Inserts valid cursors in current_key to key_priority_heap and pops the new min key cursors
    /// from key_priority_heap into current_key.
    fn update_min_keys(&mut self) {
        self.assert_key_direction(Direction::Forward);

        self.sort_keys(|c1, c2| c2.cmp(c1), false);
        self.minimize_vals();
    }

    /// Update the current_key array after stepping all cursors in self.current_key in reverse.
    ///
    /// Assumes that cursors in key_priority_heap are valid and sorted by key (descending).
    ///
    /// Inserts valid cursors in current_key to key_priority_heap and pops the new max key cursors
    /// from key_priority_heap into current_key.
    fn update_max_keys(&mut self) {
        self.assert_key_direction(Direction::Backward);

        self.sort_keys(|c1, c2| c1.cmp(c2), false);
        self.minimize_vals();
    }

    /// Update the current_val array after stepping all cursors in self.current_val with step_val.
    ///
    /// Assumes that cursors in val_priority_heap are valid and sorted by value (ascending).
    ///
    /// Inserts valid cursors in current_val to val_priority_heap and pops the new min value cursors
    /// from val_priority_heap into current_val.
    fn update_min_vals(&mut self) {
        self.assert_val_direction(Direction::Forward);
        self.sort_vals(|c1, c2| c2.cmp(c1), false);
    }

    /// Update the current_val array after stepping all cursors in self.current_val with step_val_reverse.
    ///
    /// Assumes that cursors in val_priority_heap are valid and sorted by value (descending).
    ///
    /// Inserts valid cursors in current_val to val_priority_heap and pops the new max value cursors
    /// from val_priority_heap into current_val.
    fn update_max_vals(&mut self) {
        self.assert_val_direction(Direction::Backward);
        self.sort_vals(|c1, c2| c1.cmp(c2), false);
    }
}

impl<K, V, T, R, C: Cursor<K, V, T, R>> Cursor<K, V, T, R> for CursorList<K, V, T, R, C>
where
    K: DataTrait + ?Sized,
    V: DataTrait + ?Sized,
    R: WeightTrait + ?Sized,
    T: 'static,
{
    fn weight_factory(&self) -> &'static dyn Factory<R> {
        self.weight_factory
    }

    fn key_valid(&self) -> bool {
        !self.current_key.is_empty()
    }

    fn val_valid(&self) -> bool {
        !self.current_val.is_empty()
    }

    fn key(&self) -> &K {
        debug_assert!(self.key_valid());
        debug_assert!(self.cursors[self.current_key[0]].key_valid());
        self.cursors[self.current_key[0]].key()
    }

    fn val(&self) -> &V {
        debug_assert!(self.key_valid());
        debug_assert!(self.val_valid());
        debug_assert!(self.cursors[self.current_val[0]].val_valid());
        self.cursors[self.current_val[0]].val()
    }

    fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R)) {
        debug_assert!(self.key_valid());
        debug_assert!(self.val_valid());
        for &index in self.current_val.iter() {
            self.cursors[index].map_times(logic);
        }
    }

    fn map_times_through(&mut self, upper: &T, logic: &mut dyn FnMut(&T, &R)) {
        debug_assert!(self.key_valid());
        debug_assert!(self.val_valid());
        for &index in self.current_val.iter() {
            self.cursors[index].map_times_through(upper, logic);
        }
    }

    fn weight(&mut self) -> &R
    where
        T: PartialEq<()>,
    {
        self.weight_checked()
    }

    fn weight_checked(&mut self) -> &R {
        if TypeId::of::<T>() == TypeId::of::<()>() {
            debug_assert!(self.key_valid());
            debug_assert!(self.val_valid());
            debug_assert!(self.cursors[self.current_val[0]].val_valid());

            // Weight should already be computed by `is_zero_weight`, which is always
            // called as part of every operation that moves the cursor.
            debug_assert!(!self.weight.is_zero());

            &self.weight
        } else {
            panic!("CursorList::weight_checked called on non-unit timestamp type");
        }
    }

    fn map_values(&mut self, logic: &mut dyn FnMut(&V, &R))
    where
        T: PartialEq<()>,
    {
        debug_assert!(self.key_valid());
        while self.val_valid() {
            let val = self.val();
            logic(val, &self.weight);
            self.step_val();
        }
    }

    fn step_key(&mut self) {
        self.assert_key_direction(Direction::Forward);

        for &index in self.current_key.iter() {
            debug_assert!(self.cursors[index].key_valid());
            self.cursors[index].step_key();
        }

        self.set_val_direction(Direction::Forward);
        self.update_min_keys();
        self.skip_zero_weight_keys_forward();
    }

    fn step_key_reverse(&mut self) {
        self.assert_key_direction(Direction::Backward);

        for &index in self.current_key.iter() {
            debug_assert!(self.cursors[index].key_valid());
            self.cursors[index].step_key_reverse();
        }

        self.set_val_direction(Direction::Forward);
        self.update_max_keys();
        self.skip_zero_weight_keys_reverse();
    }

    fn seek_key(&mut self, key: &K) {
        self.assert_key_direction(Direction::Forward);

        for cursor in self.cursors.iter_mut() {
            cursor.seek_key(key);
        }

        self.set_val_direction(Direction::Forward);
        self.minimize_keys();
        self.skip_zero_weight_keys_forward();
    }

    fn seek_key_exact(&mut self, key: &K, hash: Option<u64>) -> bool {
        self.set_key_direction(None);

        let hash = hash.unwrap_or_else(|| key.default_hash());
        self.current_key.clear();

        let mut result = false;

        for (index, cursor) in self.cursors.iter_mut().enumerate() {
            if cursor.seek_key_exact(key, Some(hash)) {
                self.current_key.push(index);
                result = true;
            }
        }

        self.set_val_direction(Direction::Forward);
        self.minimize_vals();

        if result {
            self.skip_zero_weight_vals_forward();
            self.val_valid()
        } else {
            false
        }
    }

    fn seek_key_with(&mut self, predicate: &dyn Fn(&K) -> bool) {
        self.assert_key_direction(Direction::Forward);

        for cursor in self.cursors.iter_mut() {
            cursor.seek_key_with(&predicate);
        }

        self.set_val_direction(Direction::Forward);
        self.minimize_keys();
        self.skip_zero_weight_keys_forward();
    }

    fn seek_key_with_reverse(&mut self, predicate: &dyn Fn(&K) -> bool) {
        self.assert_key_direction(Direction::Backward);

        for cursor in self.cursors.iter_mut() {
            cursor.seek_key_with_reverse(&predicate);
        }

        self.set_val_direction(Direction::Forward);
        self.maximize_keys();
        self.skip_zero_weight_keys_reverse();
    }

    fn seek_key_reverse(&mut self, key: &K) {
        self.assert_key_direction(Direction::Backward);

        for cursor in self.cursors.iter_mut() {
            cursor.seek_key_reverse(key);
        }

        self.set_val_direction(Direction::Forward);
        self.maximize_keys();
        self.skip_zero_weight_keys_reverse();
    }

    fn step_val(&mut self) {
        self.assert_val_direction(Direction::Forward);

        for &index in self.current_val.iter() {
            debug_assert!(self.cursors[index].key_valid());
            debug_assert!(self.cursors[index].val_valid());
            self.cursors[index].step_val();
        }
        self.update_min_vals();

        self.skip_zero_weight_vals_forward();
    }

    fn seek_val(&mut self, val: &V) {
        self.assert_val_direction(Direction::Forward);

        for &index in self.current_key.iter() {
            debug_assert!(self.cursors[index].key_valid());
            self.cursors[index].seek_val(val);
        }
        self.minimize_vals();
        self.skip_zero_weight_vals_forward();
    }

    fn seek_val_with(&mut self, predicate: &dyn Fn(&V) -> bool) {
        self.assert_val_direction(Direction::Forward);

        for &index in self.current_key.iter() {
            debug_assert!(self.cursors[index].key_valid());
            self.cursors[index].seek_val_with(predicate);
        }
        self.minimize_vals();
        self.skip_zero_weight_vals_forward();
    }

    fn rewind_keys(&mut self) {
        self.set_key_direction(Some(Direction::Forward));

        for cursor in self.cursors.iter_mut() {
            cursor.rewind_keys();
        }

        self.set_val_direction(Direction::Forward);
        self.minimize_keys();
        self.skip_zero_weight_keys_forward();
    }

    fn fast_forward_keys(&mut self) {
        self.set_key_direction(Some(Direction::Backward));

        for cursor in self.cursors.iter_mut() {
            cursor.fast_forward_keys();
        }

        self.set_val_direction(Direction::Forward);
        self.maximize_keys();
        self.skip_zero_weight_keys_reverse();
    }

    fn rewind_vals(&mut self) {
        for &index in self.current_key.iter() {
            self.cursors[index].rewind_vals();
        }

        self.set_val_direction(Direction::Forward);
        self.minimize_vals();
        self.skip_zero_weight_vals_forward();
    }

    fn step_val_reverse(&mut self) {
        self.assert_val_direction(Direction::Backward);

        for &index in self.current_val.iter() {
            debug_assert!(self.cursors[index].key_valid());
            debug_assert!(self.cursors[index].val_valid());
            self.cursors[index].step_val_reverse();
        }
        self.update_max_vals();
        self.skip_zero_weight_vals_reverse();
    }

    fn seek_val_reverse(&mut self, val: &V) {
        self.assert_val_direction(Direction::Backward);

        for &index in self.current_key.iter() {
            debug_assert!(self.cursors[index].key_valid());
            self.cursors[index].seek_val_reverse(val);
        }
        self.maximize_vals();
        self.skip_zero_weight_vals_reverse();
    }

    fn seek_val_with_reverse(&mut self, predicate: &dyn Fn(&V) -> bool) {
        self.assert_val_direction(Direction::Backward);

        for &index in self.current_key.iter() {
            debug_assert!(self.cursors[index].key_valid());
            self.cursors[index].seek_val_with_reverse(predicate);
        }
        self.maximize_vals();
        self.skip_zero_weight_vals_reverse();
    }

    fn fast_forward_vals(&mut self) {
        for &index in self.current_key.iter() {
            debug_assert!(self.cursors[index].key_valid());
            self.cursors[index].fast_forward_vals();
        }

        self.set_val_direction(Direction::Backward);
        self.maximize_vals();
        self.skip_zero_weight_vals_reverse();
    }

    fn position(&self) -> Option<Position> {
        let mut num_keys = 0;
        let mut current_key = 0;

        for cursor in self.cursors.iter() {
            let position = cursor.position().unwrap();
            num_keys += position.total;
            current_key += position.offset;
        }
        Some(Position {
            total: num_keys,
            offset: current_key,
        })
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::IndexedZSetReader;
    use crate::utils::Tup2;
    use crate::{
        dynamic::DowncastTrait,
        indexed_zset,
        trace::{BatchReader, BatchReaderFactories},
    };
    use proptest::{collection::vec, prelude::*};

    pub type TestBatch = crate::OrdIndexedZSet<u64, u64>;

    /// Collect (key, value, weight) tuples from a cursor by iterating forward.
    pub fn cursor_to_tuples<C>(cursor: &mut C) -> Vec<(u64, u64, i64)>
    where
        C: Cursor<crate::dynamic::DynData, crate::dynamic::DynData, (), crate::DynZWeight>,
    {
        let mut result = Vec::new();
        while cursor.key_valid() {
            while cursor.val_valid() {
                let k = *cursor.key().downcast_checked::<u64>();
                let v = *cursor.val().downcast_checked::<u64>();
                let w = *cursor.weight().downcast_checked::<i64>();
                assert_ne!(w, 0);
                result.push((k, v, w));

                cursor.step_val();
            }
            cursor.step_key();
        }
        result
    }

    /// Collect tuples from batches via merge_batches_by_reference (uses cursor_to_tuples).
    pub fn merged_batch_to_tuples(batches: &[TestBatch]) -> Vec<(u64, u64, i64)> {
        if batches.is_empty() {
            return Vec::new();
        }

        let mut tuples = batches.iter().flat_map(|b| b.iter()).collect::<Vec<_>>();
        tuples.sort();

        tuples.dedup_by(|(key1, v1, w1), (key2, v2, w2)| {
            if key1 == key2 && v1 == v2 {
                *w2 += std::mem::replace(w1, 0);
                true
            } else {
                false
            }
        });
        tuples.retain(|(_, _, w)| *w != 0);
        tuples
    }

    #[test]
    fn cursor_list_matches_merge_batches() {
        let batch1: TestBatch = indexed_zset! { 1 => { 1 => 1, 2 => 2 }, 2 => { 1 => 1 } };
        let batch2: TestBatch = indexed_zset! { 1 => { 2 => -1, 3 => 2 } };
        let batch3: TestBatch = indexed_zset! { 2 => { 2 => 1 }, 3 => { 1 => 1 }, 4 => { 1 => 1 } };

        let batches = vec![batch1, batch2, batch3];
        let cursors: Vec<_> = batches.iter().map(|b| b.cursor()).collect();
        let weight_factory = batches[0].factories().weight_factory();
        let mut cursor_list = CursorList::new(weight_factory, cursors);

        let cursor_output = cursor_to_tuples(&mut cursor_list);

        let expected = merged_batch_to_tuples(&batches);

        assert_eq!(cursor_output, expected);
    }

    #[test]
    fn cursor_list_empty_batches() {
        let batch1: TestBatch = indexed_zset! {};
        let batch2: TestBatch = indexed_zset! { 1 => { 1 => 1 } };
        let batch3: TestBatch = indexed_zset! {};

        let batches = vec![batch1, batch2, batch3];
        let cursors: Vec<_> = batches.iter().map(|b| b.cursor()).collect();
        let weight_factory = batches[0].factories().weight_factory();
        let mut cursor_list = CursorList::new(weight_factory, cursors);

        let cursor_output = cursor_to_tuples(&mut cursor_list);
        let expected = merged_batch_to_tuples(&batches);

        assert_eq!(cursor_output, expected);
        assert_eq!(cursor_output, vec![(1, 1, 1)]);
    }

    #[test]
    fn cursor_list_single_batch() {
        let batch: TestBatch = indexed_zset! { 1 => { 1 => 1, 2 => 2 }, 2 => { 1 => -1 } };

        let batches = vec![batch];
        let cursors: Vec<_> = batches.iter().map(|b| b.cursor()).collect();
        let weight_factory = batches[0].factories().weight_factory();
        let mut cursor_list = CursorList::new(weight_factory, cursors);

        let cursor_output = cursor_to_tuples(&mut cursor_list);
        let expected = merged_batch_to_tuples(&batches);

        assert_eq!(cursor_output, expected);
    }

    #[test]
    fn cursor_list_weights_consolidate() {
        // Multiple batches with same (k,v) - weights should sum
        let batch1: TestBatch = indexed_zset! { 1 => { 1 => 2, 2 => 1 } };
        let batch2: TestBatch = indexed_zset! { 1 => { 1 => 3, 2 => -1 } };
        let batch3: TestBatch = indexed_zset! { 1 => { 1 => -1 } };

        let batches = vec![batch1, batch2, batch3];
        let cursors: Vec<_> = batches.iter().map(|b| b.cursor()).collect();
        let weight_factory = batches[0].factories().weight_factory();
        let mut cursor_list = CursorList::new(weight_factory, cursors);

        let cursor_output = cursor_to_tuples(&mut cursor_list);
        let expected = merged_batch_to_tuples(&batches);

        assert_eq!(cursor_output, expected);
        // (1,1): 2+3-1=4, (1,2): 1-1=0 (filtered out)
        assert_eq!(cursor_output, vec![(1, 1, 4)]);
    }

    fn batches(
        (max_key, max_value, weight_min, weight_max, max_batch_size, max_num_batches): (
            u64,
            u64,
            i64,
            i64,
            usize,
            usize,
        ),
    ) -> impl Strategy<Value = Vec<TestBatch>> {
        let tuple_strategy = (0..max_key, 0..max_value, weight_min..=weight_max)
            .prop_map(|(k, v, w)| Tup2(Tup2(k, v), w));
        vec(
            vec(tuple_strategy, 0..max_batch_size)
                .prop_map(|tuples| TestBatch::from_tuples((), tuples)),
            1..=max_num_batches,
        )
    }

    fn test_cursor_list_matches_merge_batches(batches: &[TestBatch]) {
        let cursors: Vec<_> = batches.iter().map(|b| b.cursor()).collect();
        let weight_factory = batches[0].factories().weight_factory();
        let mut cursor_list = CursorList::new(weight_factory, cursors);

        let cursor_output = cursor_to_tuples(&mut cursor_list);
        let expected = merged_batch_to_tuples(&batches);

        assert_eq!(cursor_output, expected);
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        #[test]
        fn proptest_cursor_list_matches_merge_batches1(batches in batches((100, 100, 0, 2, 100, 100))) {
            test_cursor_list_matches_merge_batches(&batches);
        }

        #[test]
        fn proptest_cursor_list_matches_merge_batches2(batches in batches((100, 100, -2, 2, 100, 100))) {
            test_cursor_list_matches_merge_batches(&batches);
        }

        #[test]
        fn proptest_cursor_list_matches_merge_batches3(batches in batches((1, 1, -2, 2, 100, 100))) {
            test_cursor_list_matches_merge_batches(&batches);
        }

        #[test]
        fn proptest_cursor_list_matches_merge_batches4(batches in batches((1000, 1, -2, 2, 100, 100))) {
            test_cursor_list_matches_merge_batches(&batches);
        }

        #[test]
        fn proptest_cursor_list_matches_merge_batches5(batches in batches((1, 1000, -2, 2, 100, 100))) {
            test_cursor_list_matches_merge_batches(&batches);
        }

    }
}