1pub mod cursor_empty;
5pub mod cursor_group;
6pub mod cursor_list;
7pub mod cursor_pair;
8pub mod cursor_with_polarity;
9mod reverse;
10pub mod saturating_cursor;
11
12use std::{fmt::Debug, marker::PhantomData};
13
14pub use cursor_empty::CursorEmpty;
15pub use cursor_group::CursorGroup;
16pub use cursor_list::CursorList;
17pub use cursor_pair::CursorPair;
18pub use cursor_with_polarity::CursorWithPolarity;
19pub use saturating_cursor::SaturatingCursor;
20
21pub use reverse::ReverseKeyCursor;
22use size_of::SizeOf;
23
24use crate::dynamic::{DataTrait, Factory};
25
26use super::BatchReader;
27use super::{Filter, GroupFilter};
28
29#[derive(Debug, PartialEq, Eq, Copy, Clone)]
30enum Direction {
31 Forward,
32 Backward,
33}
34
35#[derive(Debug, Clone, PartialEq, Eq)]
37pub struct Position {
38 pub total: u64,
39 pub offset: u64,
40}
41
42pub trait Cursor<K: ?Sized, V: ?Sized, T, R: ?Sized> {
123 fn weight_factory(&self) -> &'static dyn Factory<R>;
124
125 fn key_valid(&self) -> bool;
129
130 fn val_valid(&self) -> bool;
135
136 fn key(&self) -> &K;
138
139 fn val(&self) -> &V;
141
142 fn get_key(&self) -> Option<&K> {
144 if self.key_valid() {
145 Some(self.key())
146 } else {
147 None
148 }
149 }
150
151 fn get_val(&self) -> Option<&V> {
153 if self.val_valid() {
154 Some(self.val())
155 } else {
156 None
157 }
158 }
159
160 fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R));
163
164 fn map_times_through(&mut self, upper: &T, logic: &mut dyn FnMut(&T, &R));
167
168 fn weight(&mut self) -> &R
176 where
177 T: PartialEq<()>;
178
179 fn weight_checked(&mut self) -> &R;
190
191 fn map_values(&mut self, logic: &mut dyn FnMut(&V, &R))
193 where
194 T: PartialEq<()>;
195
196 fn step_key(&mut self);
198
199 fn step_key_reverse(&mut self);
201
202 fn seek_key(&mut self, key: &K);
209
210 fn seek_key_exact(&mut self, key: &K, hash: Option<u64>) -> bool;
224
225 fn seek_key_with(&mut self, predicate: &dyn Fn(&K) -> bool);
228
229 fn seek_key_with_reverse(&mut self, predicate: &dyn Fn(&K) -> bool);
232
233 fn seek_key_reverse(&mut self, key: &K);
237
238 fn step_val(&mut self);
240
241 fn step_val_reverse(&mut self);
243
244 fn seek_val(&mut self, val: &V);
246
247 fn seek_val_reverse(&mut self, val: &V);
249
250 fn seek_val_with(&mut self, predicate: &dyn Fn(&V) -> bool);
253
254 fn seek_val_with_reverse(&mut self, predicate: &dyn Fn(&V) -> bool);
258
259 fn rewind_keys(&mut self);
261
262 fn fast_forward_keys(&mut self);
264
265 fn rewind_vals(&mut self);
267
268 fn fast_forward_vals(&mut self);
270
271 fn keyval_valid(&self) -> bool {
274 self.key_valid() && self.val_valid()
275 }
276
277 fn keyval(&self) -> (&K, &V) {
279 (self.key(), self.val())
280 }
281
282 fn step_keyval(&mut self) {
284 self.step_val();
285 while self.key_valid() && !self.val_valid() {
286 self.step_key();
287 }
288 }
289
290 fn step_keyval_reverse(&mut self) {
292 self.step_val_reverse();
293 while self.key_valid() && !self.val_valid() {
294 self.step_key_reverse();
295 if self.key_valid() {
296 self.fast_forward_vals();
297 }
298 }
299 }
300
301 fn seek_keyval(&mut self, key: &K, val: &V)
303 where
304 K: PartialEq,
305 {
306 if self.get_key() != Some(key) {
307 self.seek_key(key);
308 }
309
310 if self.get_key() == Some(key) {
311 self.seek_val(val);
312 }
313
314 while self.key_valid() && !self.val_valid() {
315 self.step_key();
316 }
317 }
318
319 fn seek_keyval_reverse(&mut self, key: &K, val: &V)
321 where
322 K: PartialEq,
323 {
324 if self.get_key() != Some(key) {
325 self.seek_key_reverse(key);
326 if self.key_valid() {
327 self.fast_forward_vals();
328 }
329 }
330
331 if self.get_key() == Some(key) {
332 self.seek_val_reverse(val);
333 }
334
335 while self.key_valid() && !self.val_valid() {
336 self.step_key_reverse();
337 if self.key_valid() {
338 self.fast_forward_vals();
339 }
340 }
341 }
342
343 fn position(&self) -> Option<Position>;
344}
345
346pub trait CursorFactory<K: ?Sized, V: ?Sized, T, R: ?Sized> {
368 fn get_cursor<'a>(&'a self) -> Box<dyn Cursor<K, V, T, R> + 'a>;
369}
370
371impl<K: ?Sized, V: ?Sized, T, R: ?Sized, B> CursorFactory<K, V, T, R> for &B
372where
373 B: BatchReader<Key = K, Val = V, Time = T, R = R>,
374{
375 fn get_cursor<'a>(&'a self) -> Box<dyn Cursor<K, V, T, R> + 'a> {
376 Box::new(self.cursor())
377 }
378}
379
380pub struct CursorFactoryWrapper<B>(pub B);
382
383impl<K: ?Sized, V: ?Sized, T, R: ?Sized, B> CursorFactory<K, V, T, R> for CursorFactoryWrapper<B>
384where
385 B: BatchReader<Key = K, Val = V, Time = T, R = R>,
386{
387 fn get_cursor<'a>(&'a self) -> Box<dyn Cursor<K, V, T, R> + 'a> {
388 Box::new(self.0.cursor())
389 }
390}
391
392pub trait ClonableCursor<'s, K, V, T, R>: Cursor<K, V, T, R> + Debug
397where
398 K: ?Sized,
399 V: ?Sized,
400 R: ?Sized,
401{
402 fn clone_boxed(&self) -> Box<dyn ClonableCursor<'s, K, V, T, R> + Send + 's>;
403}
404
405impl<'s, K, V, T, R, C> ClonableCursor<'s, K, V, T, R> for C
406where
407 K: ?Sized,
408 V: ?Sized,
409 R: ?Sized,
410 C: Cursor<K, V, T, R> + Debug + Clone + Send + 's,
411{
412 fn clone_boxed(&self) -> Box<dyn ClonableCursor<'s, K, V, T, R> + Send + 's> {
413 Box::new(self.clone())
414 }
415}
416
417impl<K, V, T, R, C> Cursor<K, V, T, R> for Box<C>
418where
419 K: ?Sized,
420 V: ?Sized,
421 R: ?Sized,
422 C: Cursor<K, V, T, R> + ?Sized,
423{
424 fn weight_factory(&self) -> &'static dyn Factory<R> {
425 (**self).weight_factory()
426 }
427
428 fn key_valid(&self) -> bool {
429 (**self).key_valid()
430 }
431
432 fn val_valid(&self) -> bool {
433 (**self).val_valid()
434 }
435
436 fn key(&self) -> &K {
437 (**self).key()
438 }
439
440 fn val(&self) -> &V {
441 (**self).val()
442 }
443
444 fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R)) {
445 (**self).map_times(logic)
446 }
447
448 fn map_times_through(&mut self, upper: &T, logic: &mut dyn FnMut(&T, &R)) {
449 (**self).map_times_through(upper, logic)
450 }
451
452 fn weight(&mut self) -> &R
453 where
454 T: PartialEq<()>,
455 {
456 (**self).weight()
457 }
458
459 fn weight_checked(&mut self) -> &R {
460 (**self).weight_checked()
461 }
462
463 fn map_values(&mut self, logic: &mut dyn FnMut(&V, &R))
464 where
465 T: PartialEq<()>,
466 {
467 (**self).map_values(logic)
468 }
469
470 fn step_key(&mut self) {
471 (**self).step_key()
472 }
473
474 fn step_key_reverse(&mut self) {
475 (**self).step_key_reverse()
476 }
477
478 fn seek_key(&mut self, key: &K) {
479 (**self).seek_key(key)
480 }
481
482 fn seek_key_exact(&mut self, key: &K, hash: Option<u64>) -> bool {
483 (**self).seek_key_exact(key, hash)
484 }
485
486 fn seek_key_with(&mut self, predicate: &dyn Fn(&K) -> bool) {
487 (**self).seek_key_with(predicate)
488 }
489
490 fn seek_key_with_reverse(&mut self, predicate: &dyn Fn(&K) -> bool) {
491 (**self).seek_key_with_reverse(predicate)
492 }
493
494 fn seek_key_reverse(&mut self, key: &K) {
495 (**self).seek_key_reverse(key)
496 }
497
498 fn step_val(&mut self) {
499 (**self).step_val()
500 }
501
502 fn step_val_reverse(&mut self) {
503 (**self).step_val_reverse()
504 }
505
506 fn seek_val(&mut self, val: &V) {
507 (**self).seek_val(val)
508 }
509
510 fn seek_val_reverse(&mut self, val: &V) {
511 (**self).seek_val_reverse(val)
512 }
513
514 fn seek_val_with(&mut self, predicate: &dyn Fn(&V) -> bool) {
515 (**self).seek_val_with(predicate)
516 }
517
518 fn seek_val_with_reverse(&mut self, predicate: &dyn Fn(&V) -> bool) {
519 (**self).seek_val_with_reverse(predicate)
520 }
521
522 fn rewind_keys(&mut self) {
523 (**self).rewind_keys()
524 }
525
526 fn fast_forward_keys(&mut self) {
527 (**self).fast_forward_keys()
528 }
529
530 fn rewind_vals(&mut self) {
531 (**self).rewind_vals()
532 }
533
534 fn fast_forward_vals(&mut self) {
535 (**self).fast_forward_vals()
536 }
537
538 fn get_key(&self) -> Option<&K> {
539 (**self).get_key()
540 }
541
542 fn get_val(&self) -> Option<&V> {
543 (**self).get_val()
544 }
545
546 fn keyval_valid(&self) -> bool {
547 (**self).keyval_valid()
548 }
549
550 fn keyval(&self) -> (&K, &V) {
551 (**self).keyval()
552 }
553
554 fn step_keyval(&mut self) {
555 (**self).step_keyval()
556 }
557
558 fn step_keyval_reverse(&mut self) {
559 (**self).step_keyval_reverse()
560 }
561
562 fn seek_keyval(&mut self, key: &K, val: &V)
563 where
564 K: PartialEq,
565 {
566 (**self).seek_keyval(key, val)
567 }
568
569 fn seek_keyval_reverse(&mut self, key: &K, val: &V)
570 where
571 K: PartialEq,
572 {
573 (**self).seek_keyval_reverse(key, val)
574 }
575
576 fn position(&self) -> Option<Position> {
577 (**self).position()
578 }
579}
580
581#[derive(Debug, SizeOf)]
583pub struct DelegatingCursor<'s, K, V, T, R>(
584 pub Box<dyn ClonableCursor<'s, K, V, T, R> + Send + 's>,
585)
586where
587 K: ?Sized,
588 V: ?Sized,
589 R: ?Sized;
590
591impl<K, V, T, R> Clone for DelegatingCursor<'_, K, V, T, R>
592where
593 K: ?Sized,
594 V: ?Sized,
595 R: ?Sized,
596{
597 fn clone(&self) -> Self {
598 Self(self.0.clone_boxed())
599 }
600}
601
602impl<K, V, T, R> Cursor<K, V, T, R> for DelegatingCursor<'_, K, V, T, R>
603where
604 K: ?Sized,
605 V: ?Sized,
606 R: ?Sized,
607{
608 fn weight_factory(&self) -> &'static dyn Factory<R> {
609 self.0.weight_factory()
610 }
611
612 fn key(&self) -> &K {
613 self.0.key()
614 }
615
616 fn val(&self) -> &V {
617 self.0.val()
618 }
619
620 fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R)) {
621 self.0.map_times(logic)
622 }
623
624 fn map_times_through(&mut self, upper: &T, logic: &mut dyn FnMut(&T, &R)) {
625 self.0.map_times_through(upper, logic)
626 }
627
628 fn map_values(&mut self, logic: &mut dyn FnMut(&V, &R))
629 where
630 T: PartialEq<()>,
631 {
632 self.0.map_values(logic)
633 }
634
635 fn weight(&mut self) -> &R
636 where
637 T: PartialEq<()>,
638 {
639 self.0.weight()
640 }
641
642 fn weight_checked(&mut self) -> &R {
643 self.0.weight_checked()
644 }
645
646 fn key_valid(&self) -> bool {
647 self.0.key_valid()
648 }
649
650 fn val_valid(&self) -> bool {
651 self.0.val_valid()
652 }
653
654 fn step_key(&mut self) {
655 self.0.step_key()
656 }
657
658 fn step_key_reverse(&mut self) {
659 self.0.step_key_reverse()
660 }
661
662 fn seek_key(&mut self, key: &K) {
663 self.0.seek_key(key)
664 }
665
666 fn seek_key_exact(&mut self, key: &K, hash: Option<u64>) -> bool {
667 self.0.seek_key_exact(key, hash)
668 }
669
670 fn seek_key_with(&mut self, predicate: &dyn Fn(&K) -> bool) {
671 self.0.seek_key_with(predicate)
672 }
673
674 fn seek_key_with_reverse(&mut self, predicate: &dyn Fn(&K) -> bool) {
675 self.0.seek_key_with_reverse(predicate)
676 }
677
678 fn seek_key_reverse(&mut self, key: &K) {
679 self.0.seek_key_reverse(key)
680 }
681
682 fn step_val(&mut self) {
683 self.0.step_val()
684 }
685
686 fn seek_val(&mut self, val: &V) {
687 self.0.seek_val(val)
688 }
689
690 fn seek_val_with(&mut self, predicate: &dyn Fn(&V) -> bool) {
691 self.0.seek_val_with(predicate)
692 }
693
694 fn rewind_keys(&mut self) {
695 self.0.rewind_keys()
696 }
697
698 fn fast_forward_keys(&mut self) {
699 self.0.fast_forward_keys()
700 }
701
702 fn rewind_vals(&mut self) {
703 self.0.rewind_vals()
704 }
705
706 fn step_val_reverse(&mut self) {
707 self.0.step_val_reverse()
708 }
709
710 fn seek_val_reverse(&mut self, val: &V) {
711 self.0.seek_val_reverse(val)
712 }
713
714 fn seek_val_with_reverse(&mut self, predicate: &dyn Fn(&V) -> bool) {
715 self.0.seek_val_with_reverse(predicate)
716 }
717
718 fn fast_forward_vals(&mut self) {
719 self.0.fast_forward_vals()
720 }
721
722 fn position(&self) -> Option<Position> {
723 self.0.position()
724 }
725}
726
727pub trait MergeCursor<K, V, T, R>
734where
735 K: ?Sized,
736 V: ?Sized,
737 R: ?Sized,
738{
739 fn key_valid(&self) -> bool;
740 fn val_valid(&self) -> bool;
741 fn get_key(&self) -> Option<&K> {
742 self.key_valid().then(|| self.key())
743 }
744 fn get_val(&self) -> Option<&V> {
745 self.val_valid().then(|| self.val())
746 }
747 fn key(&self) -> &K;
748 fn val(&self) -> &V;
749 fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R));
750 fn weight(&mut self) -> &R
751 where
752 T: PartialEq<()>;
753 fn step_key(&mut self);
754 fn step_val(&mut self);
755
756 fn has_mut(&self) -> bool {
757 false
758 }
759 fn key_mut(&mut self) -> &mut K {
760 unimplemented!("used key_mut() on batch that does not support it")
761 }
762 fn val_mut(&mut self) -> &mut V {
763 unimplemented!("used val_mut() on batch that does not support it")
764 }
765 fn weight_mut(&mut self) -> &mut R
766 where
767 T: PartialEq<()>,
768 {
769 unimplemented!("used weight_mut() on batch that does not support it")
770 }
771}
772
773impl<K, V, T, R, C> MergeCursor<K, V, T, R> for Box<C>
774where
775 C: MergeCursor<K, V, T, R> + ?Sized,
776 K: ?Sized,
777 V: ?Sized,
778 R: ?Sized,
779{
780 fn key_valid(&self) -> bool {
781 (**self).key_valid()
782 }
783
784 fn val_valid(&self) -> bool {
785 (**self).val_valid()
786 }
787
788 fn key(&self) -> &K {
789 (**self).key()
790 }
791
792 fn val(&self) -> &V {
793 (**self).val()
794 }
795
796 fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R)) {
797 (**self).map_times(logic)
798 }
799
800 fn weight(&mut self) -> &R
801 where
802 T: PartialEq<()>,
803 {
804 (**self).weight()
805 }
806
807 fn step_key(&mut self) {
808 (**self).step_key()
809 }
810
811 fn step_val(&mut self) {
812 (**self).step_val()
813 }
814
815 fn has_mut(&self) -> bool {
816 (**self).has_mut()
817 }
818
819 fn key_mut(&mut self) -> &mut K {
820 (**self).key_mut()
821 }
822
823 fn val_mut(&mut self) -> &mut V {
824 (**self).val_mut()
825 }
826
827 fn weight_mut(&mut self) -> &mut R
828 where
829 T: PartialEq<()>,
830 {
831 (**self).weight_mut()
832 }
833}
834
835pub struct FilteredMergeCursor<K, V, T, R, C>
840where
841 K: ?Sized,
842 V: ?Sized + 'static,
843 R: ?Sized,
844 C: Cursor<K, V, T, R>,
845{
846 cursor: C,
847 key_filter: Option<Filter<K>>,
848 value_filter: Option<Filter<V>>,
849 phantom: PhantomData<(T, Box<R>)>,
850}
851
852impl<K, V, T, R, C> FilteredMergeCursor<K, V, T, R, C>
853where
854 K: ?Sized + 'static,
855 V: DataTrait + ?Sized,
856 R: ?Sized + 'static,
857 C: Cursor<K, V, T, R>,
858 T: 'static,
859{
860 pub fn new(
861 mut cursor: C,
862 key_filter: Option<Filter<K>>,
863 value_filter: Option<Filter<V>>,
864 ) -> Self {
865 Self::skip_filtered_keys(&mut cursor, &key_filter, &value_filter);
866 Self {
867 cursor,
868 key_filter,
869 value_filter,
870 phantom: PhantomData,
871 }
872 }
873
874 fn skip_filtered_keys(
875 cursor: &mut C,
876 key_filter: &Option<Filter<K>>,
877 value_filter: &Option<Filter<V>>,
878 ) {
879 while let Some(key) = cursor.get_key() {
880 if Filter::include(key_filter, key) && Self::skip_filtered_values(cursor, value_filter)
881 {
882 return;
883 } else {
884 cursor.step_key();
885 }
886 }
887 }
888
889 fn skip_filtered_values(cursor: &mut C, value_filter: &Option<Filter<V>>) -> bool {
890 while let Some(val) = cursor.get_val() {
891 if Filter::include(value_filter, val) {
892 return true;
893 }
894 cursor.step_val();
895 }
896 false
897 }
898}
899
900impl<K, V, T, R, C> MergeCursor<K, V, T, R> for FilteredMergeCursor<K, V, T, R, C>
901where
902 K: ?Sized + 'static,
903 V: DataTrait + ?Sized,
904 R: ?Sized + 'static,
905 T: 'static,
906 C: Cursor<K, V, T, R>,
907{
908 fn key_valid(&self) -> bool {
909 self.cursor.key_valid()
910 }
911 fn val_valid(&self) -> bool {
912 self.cursor.val_valid()
913 }
914 fn key(&self) -> &K {
915 self.cursor.key()
916 }
917 fn val(&self) -> &V {
918 self.cursor.val()
919 }
920 fn step_key(&mut self) {
921 self.cursor.step_key();
922 Self::skip_filtered_keys(&mut self.cursor, &self.key_filter, &self.value_filter);
923 }
924 fn step_val(&mut self) {
925 self.cursor.step_val();
926 Self::skip_filtered_values(&mut self.cursor, &self.value_filter);
927 }
928 fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R)) {
929 self.cursor.map_times(logic);
930 }
931 fn weight(&mut self) -> &R
932 where
933 T: PartialEq<()>,
934 {
935 self.cursor.weight()
936 }
937}
938
939pub struct FilteredMergeCursorWithSnapshot<'a, K, V, T, R, C>
943where
944 K: ?Sized,
945 V: ?Sized + 'static,
946 R: ?Sized,
947 C: Cursor<K, V, T, R>,
948{
949 cursor: C,
950 trace_cursor: Box<dyn Cursor<K, V, T, R> + Send + 'a>,
951 key_filter: Option<Filter<K>>,
952 value_filter: GroupFilterCursor<V>,
953 phantom: PhantomData<(T, Box<R>)>,
954}
955
956impl<'a, K, V, T, R, C> FilteredMergeCursorWithSnapshot<'a, K, V, T, R, C>
957where
958 K: ?Sized + 'static,
959 V: DataTrait + ?Sized,
960 R: ?Sized + 'static,
961 C: Cursor<K, V, T, R>,
962 T: 'static,
963{
964 pub fn new<S>(
966 cursor: C,
967 key_filter: Option<Filter<K>>,
968 value_filter: GroupFilter<V>,
969 snapshot: &'a S,
970 ) -> Self
971 where
972 S: BatchReader<Key = K, Val = V, Time = T, R = R>,
973 {
974 let trace_cursor = Box::new(snapshot.cursor());
975 let value_filter = value_filter.new_cursor();
976 let mut result = Self {
977 cursor,
978 trace_cursor,
979 key_filter,
980 value_filter,
981 phantom: PhantomData,
982 };
983 result.skip_filtered_keys();
984
985 result
986 }
987
988 fn skip_filtered_keys(&mut self) {
989 while let Some(key) = self.cursor.get_key() {
990 if Filter::include(&self.key_filter, key)
991 && self
992 .value_filter
993 .on_step_key(&mut self.cursor, &mut self.trace_cursor)
994 {
995 return;
996 } else {
997 self.cursor.step_key();
998 }
999 }
1000 }
1001
1002 fn skip_filtered_values(&mut self) {
1003 self.value_filter
1004 .on_step_val(&mut self.cursor, &mut self.trace_cursor);
1005 }
1006}
1007
1008impl<'a, K, V, T, R, C> MergeCursor<K, V, T, R>
1009 for FilteredMergeCursorWithSnapshot<'a, K, V, T, R, C>
1010where
1011 K: ?Sized + 'static,
1012 V: DataTrait + ?Sized,
1013 R: ?Sized + 'static,
1014 T: 'static,
1015 C: Cursor<K, V, T, R>,
1016{
1017 fn key_valid(&self) -> bool {
1018 self.cursor.key_valid()
1019 }
1020 fn val_valid(&self) -> bool {
1021 self.cursor.val_valid()
1022 }
1023 fn key(&self) -> &K {
1024 self.cursor.key()
1025 }
1026 fn val(&self) -> &V {
1027 self.cursor.val()
1028 }
1029 fn step_key(&mut self) {
1030 self.cursor.step_key();
1031 self.skip_filtered_keys();
1032 }
1033 fn step_val(&mut self) {
1034 self.cursor.step_val();
1035 self.skip_filtered_values();
1036 }
1037 fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R)) {
1038 self.cursor.map_times(logic);
1039 }
1040 fn weight(&mut self) -> &R
1041 where
1042 T: PartialEq<()>,
1043 {
1044 self.cursor.weight()
1045 }
1046}
1047
1048impl<V: DataTrait + ?Sized> GroupFilter<V> {
1049 fn new_cursor(&self) -> GroupFilterCursor<V> {
1050 match self {
1051 Self::Simple(filter) => GroupFilterCursor::Simple {
1052 filter: filter.clone(),
1053 },
1054 Self::LastN(n, filter) => GroupFilterCursor::LastN {
1055 n: *n,
1056 filter: filter.clone(),
1057 },
1058 Self::TopN(n, filter, val_factory) => GroupFilterCursor::TopN {
1059 n: *n,
1060 filter: filter.clone(),
1061 min_val: val_factory.default_box(),
1062 min_val_valid: false,
1063 },
1064 Self::BottomN(n, filter, val_factory) => GroupFilterCursor::BottomN {
1065 n: *n,
1066 filter: filter.clone(),
1067 max_val: val_factory.default_box(),
1068 max_val_valid: false,
1069 },
1070 }
1071 }
1072}
1073
1074enum GroupFilterCursor<V: ?Sized> {
1076 Simple {
1077 filter: Filter<V>,
1078 },
1079 LastN {
1080 n: usize,
1081 filter: Filter<V>,
1082 },
1083 TopN {
1084 n: usize,
1085 filter: Filter<V>,
1086 min_val: Box<V>,
1088 min_val_valid: bool,
1091 },
1092 BottomN {
1093 n: usize,
1094 filter: Filter<V>,
1095 max_val: Box<V>,
1097 max_val_valid: bool,
1101 },
1102}
1103
1104impl<V: DataTrait + ?Sized> GroupFilterCursor<V> {
1105 fn on_step_key<K: ?Sized, T, R: ?Sized>(
1110 &mut self,
1111 cursor: &mut dyn Cursor<K, V, T, R>,
1112 trace_cursor: &mut dyn Cursor<K, V, T, R>,
1113 ) -> bool {
1114 if !trace_cursor.seek_key_exact(cursor.key(), None) {
1115 return false;
1116 }
1117
1118 match self {
1119 Self::Simple { filter } => {
1120 while let Some(val) = cursor.get_val() {
1121 if (filter.filter_func())(val) {
1122 return true;
1123 }
1124 cursor.step_val();
1125 }
1126 false
1127 }
1128 Self::LastN { n, filter } => {
1129 trace_cursor.fast_forward_vals();
1131 trace_cursor.seek_val_with_reverse(&|val| !(filter.filter_func())(val));
1132
1133 let mut count = 1;
1135 while count < *n && trace_cursor.val_valid() {
1136 trace_cursor.step_val_reverse();
1137 count += 1;
1138 }
1139
1140 if trace_cursor.val_valid() {
1141 while cursor.val_valid() && cursor.val() < trace_cursor.val() {
1144 cursor.step_val();
1146 }
1147 }
1148
1149 cursor.val_valid()
1150 }
1151 Self::TopN {
1152 n,
1153 filter,
1154 min_val,
1155 min_val_valid,
1156 } => {
1157 trace_cursor.fast_forward_vals();
1158
1159 let mut above_waterline = 0;
1161 let mut below_waterline = 0;
1162 *min_val_valid = false;
1163 while trace_cursor.val_valid() {
1164 if (filter.filter_func())(trace_cursor.val()) {
1165 above_waterline += 1;
1166 } else {
1167 below_waterline += 1;
1168 if below_waterline == *n {
1169 trace_cursor.val().clone_to(min_val);
1170 *min_val_valid = true;
1173 break;
1174 }
1175 }
1176
1177 trace_cursor.step_val_reverse();
1178 }
1179
1180 if below_waterline + above_waterline > 0 {
1181 if *min_val_valid {
1184 cursor
1185 .seek_val_with(&|val| (filter.filter_func())(val) || val >= &**min_val)
1186 }
1187 cursor.val_valid()
1188 } else {
1189 false
1190 }
1191 }
1192 Self::BottomN {
1193 n,
1194 filter,
1195 max_val,
1196 max_val_valid,
1197 } => {
1198 let mut above_waterline = 0;
1200 let mut below_waterline = 0;
1201 *max_val_valid = false;
1202
1203 while trace_cursor.val_valid() {
1204 if (filter.filter_func())(trace_cursor.val()) {
1205 above_waterline += 1;
1206 } else {
1207 below_waterline += 1;
1208
1209 if below_waterline == *n {
1210 *max_val_valid = true;
1211 trace_cursor.val().clone_to(max_val);
1212 break;
1213 }
1214 }
1215
1216 trace_cursor.step_val();
1217 }
1218
1219 below_waterline + above_waterline > 0
1220 }
1221 }
1222 }
1223
1224 fn on_step_val<K: ?Sized, T, R: ?Sized>(
1228 &mut self,
1229 cursor: &mut dyn Cursor<K, V, T, R>,
1230 _trace_cursor: &mut dyn Cursor<K, V, T, R>,
1231 ) {
1232 match self {
1233 Self::Simple { filter } => {
1234 while let Some(val) = cursor.get_val() {
1235 if (filter.filter_func())(val) {
1236 return;
1237 }
1238 cursor.step_val();
1239 }
1240 }
1241 Self::LastN { .. } => {}
1242 Self::TopN {
1243 filter,
1244 min_val,
1245 min_val_valid,
1246 ..
1247 } => {
1248 while let Some(val) = cursor.get_val() {
1249 if (filter.filter_func())(val) || !*min_val_valid {
1250 return;
1251 }
1252 if *val >= **min_val {
1253 *min_val_valid = false;
1254 return;
1255 }
1256 cursor.step_val();
1257 }
1258 }
1259 Self::BottomN {
1260 filter,
1261 max_val,
1262 max_val_valid,
1263 ..
1264 } => {
1265 while let Some(val) = cursor.get_val() {
1266 if (filter.filter_func())(val) || !*max_val_valid {
1267 return;
1268 }
1269 if *val <= **max_val {
1270 return;
1271 }
1272
1273 cursor.step_val();
1274 }
1275 }
1276 }
1277 }
1278}
1279
1280pub struct UnfilteredMergeCursor<K, V, T, R, C>
1281where
1282 K: ?Sized,
1283 V: ?Sized,
1284 R: ?Sized,
1285 C: Cursor<K, V, T, R>,
1286{
1287 cursor: C,
1288 phantom: PhantomData<(Box<K>, Box<V>, T, Box<R>)>,
1289}
1290
1291impl<K, V, T, R, C> UnfilteredMergeCursor<K, V, T, R, C>
1292where
1293 K: ?Sized,
1294 V: ?Sized,
1295 R: ?Sized,
1296 C: Cursor<K, V, T, R>,
1297{
1298 pub fn new(cursor: C) -> Self {
1299 Self {
1300 cursor,
1301 phantom: PhantomData,
1302 }
1303 }
1304}
1305
1306impl<K, V, T, R, C> MergeCursor<K, V, T, R> for UnfilteredMergeCursor<K, V, T, R, C>
1307where
1308 K: ?Sized,
1309 V: ?Sized,
1310 R: ?Sized,
1311 C: Cursor<K, V, T, R>,
1312{
1313 fn key_valid(&self) -> bool {
1314 self.cursor.key_valid()
1315 }
1316 fn val_valid(&self) -> bool {
1317 self.cursor.val_valid()
1318 }
1319 fn val(&self) -> &V {
1320 self.cursor.val()
1321 }
1322 fn key(&self) -> &K {
1323 self.cursor.key()
1324 }
1325 fn step_key(&mut self) {
1326 self.cursor.step_key()
1327 }
1328 fn step_val(&mut self) {
1329 self.cursor.step_val()
1330 }
1331 fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R)) {
1332 self.cursor.map_times(logic);
1333 }
1334 fn weight(&mut self) -> &R
1335 where
1336 T: PartialEq<()>,
1337 {
1338 self.cursor.weight()
1339 }
1340}
1341
1342#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1347pub struct Pending;
1348
1349pub trait PushCursor<K, V, T, R>
1359where
1360 K: ?Sized,
1361 V: ?Sized,
1362 R: ?Sized,
1363{
1364 fn key(&self) -> Result<Option<&K>, Pending>;
1368
1369 fn val(&self) -> Result<Option<&V>, Pending>;
1377
1378 fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R));
1388
1389 fn weight(&mut self) -> &R
1397 where
1398 T: PartialEq<()>;
1399
1400 fn step_key(&mut self);
1406
1407 fn step_val(&mut self);
1413
1414 fn run(&mut self);
1417}
1418
1419impl<K, V, T, R, C> PushCursor<K, V, T, R> for Box<C>
1420where
1421 C: PushCursor<K, V, T, R> + ?Sized,
1422 K: ?Sized,
1423 V: ?Sized,
1424 R: ?Sized,
1425{
1426 fn key(&self) -> Result<Option<&K>, Pending> {
1427 (**self).key()
1428 }
1429
1430 fn val(&self) -> Result<Option<&V>, Pending> {
1431 (**self).val()
1432 }
1433
1434 fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R)) {
1435 (**self).map_times(logic);
1436 }
1437
1438 fn weight(&mut self) -> &R
1439 where
1440 T: PartialEq<()>,
1441 {
1442 (**self).weight()
1443 }
1444
1445 fn step_key(&mut self) {
1446 (**self).step_key();
1447 }
1448
1449 fn step_val(&mut self) {
1450 (**self).step_val();
1451 }
1452
1453 fn run(&mut self) {
1454 (**self).run();
1455 }
1456}
1457
1458pub struct DefaultPushCursor<K, V, T, R, C>
1459where
1460 K: ?Sized,
1461 V: ?Sized,
1462 R: ?Sized,
1463 C: Cursor<K, V, T, R>,
1464{
1465 cursor: C,
1466 phantom: PhantomData<(Box<K>, Box<V>, T, Box<R>)>,
1467}
1468
1469impl<K, V, T, R, C> DefaultPushCursor<K, V, T, R, C>
1470where
1471 K: ?Sized,
1472 V: ?Sized,
1473 R: ?Sized,
1474 C: Cursor<K, V, T, R>,
1475{
1476 pub fn new(cursor: C) -> Self {
1477 Self {
1478 cursor,
1479 phantom: PhantomData,
1480 }
1481 }
1482}
1483
1484impl<K, V, T, R, C> PushCursor<K, V, T, R> for DefaultPushCursor<K, V, T, R, C>
1485where
1486 K: ?Sized,
1487 V: ?Sized,
1488 R: ?Sized,
1489 C: Cursor<K, V, T, R>,
1490{
1491 fn key(&self) -> Result<Option<&K>, Pending> {
1492 Ok(self.cursor.get_key())
1493 }
1494
1495 fn val(&self) -> Result<Option<&V>, Pending> {
1496 Ok(self.cursor.get_val())
1497 }
1498
1499 fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R)) {
1500 self.cursor.map_times(logic);
1501 }
1502
1503 fn weight(&mut self) -> &R
1504 where
1505 T: PartialEq<()>,
1506 {
1507 self.cursor.weight()
1508 }
1509
1510 fn step_key(&mut self) {
1511 self.cursor.step_key();
1512 }
1513
1514 fn step_val(&mut self) {
1515 self.cursor.step_val();
1516 }
1517
1518 fn run(&mut self) {}
1519}