Skip to main content

dbsp/trace/
cursor.rs

1//! Traits and types for navigating ordered sequences of `(key, val, time,
2//! diff)` tuples.
3
4pub 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/// Represents approximate position of a cursor as an offset and total.
36#[derive(Debug, Clone, PartialEq, Eq)]
37pub struct Position {
38    pub total: u64,
39    pub offset: u64,
40}
41
42/// A cursor for `(key, val, time, diff)` tuples.
43///
44/// A cursor navigates an ordered collection of `(key, val, time, diff)` tuples
45/// in order by key, then by value within each key.  In the time-diff pairs
46/// associated with each key-value pair, the times may not be ordered or unique
47/// and, in particular, [`CursorList`](`cursor_list::CursorList`) cursors can
48/// contain out-of-order and duplicate timestamps.  Because duplicate times are
49/// possible, it's possible to have multiple diffs even when `T = ()`.
50///
51/// Cursors are not iterators because they allow navigation on multiple levels
52/// (by key and value) and because they support efficient seeking (via
53/// `seek_key` and `seek_val`).
54///
55/// # Visiting keys
56///
57/// A cursor visits keys in forward or reverse order, which is set as a mode.
58/// Initially, a cursor is in the forward mode, positioned on the first key (if
59/// the collection is non-empty).  A cursor in the forward mode can move and
60/// seek forward with, e.g., [`step_key`] and [`seek_key`], but not backward.
61/// The direction can be reversed using [`fast_forward_keys`], after which the
62/// cursor can move and seek backward only, e.g. with [`step_key_reverse`] and
63/// [`seek_key_reverse`].  The client may call [`rewind_keys`] and
64/// [`fast_forward_keys`] as many times as necessary to reposition the cursor to
65/// the first or last key in the forward or reverse mode, respectively.
66///
67/// A cursor can have a valid position on a key or an invalid position after the
68/// last key (in the forward mode) or before the first key (in the reverse
69/// mode).  A cursor for an empty collection of tuples does not have any valid
70/// key positions.
71///
72/// # Visiting values within a key
73///
74/// A cursor also visits values in a forward or reverse order.  Whenever a
75/// cursor moves to a new key, its value mode is reset to forward order and its
76/// value position is set to the first value in the key.  This is true even if
77/// the cursor is visiting keys in reverse order.  In forward order mode, the
78/// cursor can move and seek forward within the values, e.g. with [`step_val`]
79/// and [`seek_val`], but not backward.  The value direction may be reversed
80/// with [`fast_forward_vals`], after which the cursor may move and seek only
81/// backward within the values, e.g. with [`step_val_reverse`] and
82/// [`seek_val_reverse`].  The client may call [`rewind_vals`] and
83/// [`fast_forward_vals`] as many times as necessary to reposition the cursor to
84/// the first or last value in the forward or reverse mode, respectively.
85///
86/// A cursor with a valid key position can have a valid value position on a
87/// value or an invalid value position after the last value (in forward mode) or
88/// before the first value (in reverse mode).
89///
90/// Every key in a nonempty collection has at least one value.
91///
92/// # Example
93///
94/// The following is typical code for iterating through all of the key-value
95/// pairs navigated by a cursor:
96///
97/// ```ignore
98/// let cursor = ...obtain cursor...;
99/// while cursor.key_valid() {
100///     while cursor.val_valid() {
101///         /// Do something with the current key-value pair.
102///         cursor.step_val();
103///     }
104///     cursor.step_key();
105/// }
106/// ```
107///
108/// [`step_key`]: `Self::step_key`
109/// [`seek_key`]: `Self::seek_key`
110/// [`step_key_reverse`]: `Self::step_key_reverse`
111/// [`seek_key_reverse`]: `Self::seek_key_reverse`
112/// [`rewind_keys`]: `Self::rewind_keys`
113/// [`fast_forward_keys`]: `Self::fast_forward_keys`
114/// [`step_val`]: `Self::step_val`
115/// [`seek_val`]: `Self::seek_val`
116/// [`step_val_reverse`]: `Self::step_val_reverse`
117/// [`seek_val_reverse`]: `Self::seek_val_reverse`
118/// [`rewind_vals`]: `Self::rewind_vals`
119/// [`fast_forward_vals`]: `Self::fast_forward_vals`
120/// [`map_times`]: Self::map_times
121/// [`weight`]: Self::weight
122pub trait Cursor<K: ?Sized, V: ?Sized, T, R: ?Sized> {
123    fn weight_factory(&self) -> &'static dyn Factory<R>;
124
125    /// Indicates if the current key is valid.
126    ///
127    /// A value of `false` indicates that the cursor has exhausted all keys.
128    fn key_valid(&self) -> bool;
129
130    /// Indicates if the current value is valid.
131    ///
132    /// A value of `false` indicates that the cursor has exhausted all values
133    /// for this key.
134    fn val_valid(&self) -> bool;
135
136    /// A reference to the current key. Panics if invalid.
137    fn key(&self) -> &K;
138
139    /// A reference to the current value. Panics if invalid.
140    fn val(&self) -> &V;
141
142    /// Returns a reference to the current key, if valid.
143    fn get_key(&self) -> Option<&K> {
144        if self.key_valid() {
145            Some(self.key())
146        } else {
147            None
148        }
149    }
150
151    /// Returns a reference to the current value, if valid.
152    fn get_val(&self) -> Option<&V> {
153        if self.val_valid() {
154            Some(self.val())
155        } else {
156            None
157        }
158    }
159
160    /// Applies `logic` to each pair of time and difference. Intended for
161    /// mutation of the closure's scope.
162    fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R));
163
164    /// Applies `logic` to each pair of time and difference, restricted
165    /// to times `t <= upper`.
166    fn map_times_through(&mut self, upper: &T, logic: &mut dyn FnMut(&T, &R));
167
168    /// Returns the weight associated with the current key/value pair.  This
169    /// concept only makes sense for cursors with unit timestamp type (`T=()`),
170    /// since otherwise there is no singular definition of weight.  This method
171    /// is more convenient, and may be more efficient, than the equivalent call
172    /// to [`Self::map_times`].
173    ///
174    /// If the current key and value are not valid, behavior is unspecified.
175    fn weight(&mut self) -> &R
176    where
177        T: PartialEq<()>;
178
179    /// Returns the weight associated with the current key/value pair is `T` is `()`;
180    /// panics otherwise.
181    ///
182    /// Can be used in contexts where the weight is known to be `()` at runtime, but
183    /// not at compile time. This is more efficient and ergonomic than using [`Self::map_times`]
184    /// to compute the weight.
185    ///
186    /// # Panics
187    ///
188    /// Panics if the `T` is not `()`.
189    fn weight_checked(&mut self) -> &R;
190
191    /// Apply a function to all values associated with the current key.
192    fn map_values(&mut self, logic: &mut dyn FnMut(&V, &R))
193    where
194        T: PartialEq<()>;
195
196    /// Advances the cursor to the next key.
197    fn step_key(&mut self);
198
199    /// Moves the cursor to the previous key.
200    fn step_key_reverse(&mut self);
201
202    /// Advances the cursor to the specified key.  If `key` itself is not
203    /// present, advances to the first key greater than `key`; if there is no
204    /// such key, the cursor becomes invalid.
205    ///
206    /// This has no effect if the cursor is already positioned past `key`, so it
207    /// might be desirable to call [`rewind_keys`](Self::rewind_keys) first.
208    fn seek_key(&mut self, key: &K);
209
210    /// Looks up the specified key and returns true if it is present.
211    ///
212    /// The implementation can be more efficient than `seek_key`.
213    ///
214    /// This method has several constraints.
215    /// - It cannot be mixed with other seek and step methods, specifically,
216    ///   you cannot call `seek_key`, `seek_key_with`, `seek_key_with_reverse`,
217    ///   `seek_key_reverse`, `step_key`, `step_key_reverse` on the cursor if
218    ///   you also use `seek_key_exact` on it without first resetting this cursor
219    ///   using `rewind_keys` or `fast_forward_keys`.
220    ///
221    /// - When calling `seek_key_exact` multiple times on the cursor, every next
222    ///   call must be for a key that is greater than the previous key.
223    fn seek_key_exact(&mut self, key: &K, hash: Option<u64>) -> bool;
224
225    /// Advances the cursor to the first key that satisfies `predicate`.
226    /// Assumes that `predicate` remains true once it turns true.
227    fn seek_key_with(&mut self, predicate: &dyn Fn(&K) -> bool);
228
229    /// Move the cursor backward to the first key that satisfies `predicate`.
230    /// Assumes that `predicate` remains true once it turns true.
231    fn seek_key_with_reverse(&mut self, predicate: &dyn Fn(&K) -> bool);
232
233    /// Moves the cursor backward to the specified key.  If `key` itself is not
234    /// present, moves backward to the first key less than `key`; if there is no
235    /// such key, the cursor becomes invalid.
236    fn seek_key_reverse(&mut self, key: &K);
237
238    /// Advances the cursor to the next value.
239    fn step_val(&mut self);
240
241    /// Moves the cursor to the previous value.
242    fn step_val_reverse(&mut self);
243
244    /// Advances the cursor to the specified value.
245    fn seek_val(&mut self, val: &V);
246
247    /// Moves the cursor back to the specified value.
248    fn seek_val_reverse(&mut self, val: &V);
249
250    /// Move the cursor to the first value (for the current key) that satisfies
251    /// `predicate`.  Assumes that `predicate` remains true once it turns true.
252    fn seek_val_with(&mut self, predicate: &dyn Fn(&V) -> bool);
253
254    /// Move the cursor back to the largest value (for the current key) that
255    /// satisfies `predicate`.  Assumes that `predicate` remains true once
256    /// it turns true.
257    fn seek_val_with_reverse(&mut self, predicate: &dyn Fn(&V) -> bool);
258
259    /// Rewinds the cursor to the first key.
260    fn rewind_keys(&mut self);
261
262    /// Moves the cursor to the last key.
263    fn fast_forward_keys(&mut self);
264
265    /// Rewinds the cursor to the first value for current key.
266    fn rewind_vals(&mut self);
267
268    /// Move the cursor to the last value for the current key.
269    fn fast_forward_vals(&mut self);
270
271    /// Reports whether the current `(key, value)` pair is valid.
272    /// Returns `false` if the cursor has exhausted all pairs.
273    fn keyval_valid(&self) -> bool {
274        self.key_valid() && self.val_valid()
275    }
276
277    /// Returns current `(key, value)` pair.  Panics if invalid.
278    fn keyval(&self) -> (&K, &V) {
279        (self.key(), self.val())
280    }
281
282    /// Moves the cursor to the next `(key, value)` pair.
283    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    /// Moves the cursor to the previous `(key, value)` pair.
291    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    /// Advance the cursor to the specified `(key, value)` pair.
302    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    /// Moves the cursor back to the specified `(key, value)` pair.
320    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
346/// An object that can produce a cursor bounded by its lifetime.
347///
348/// [BatchReader::cursor] produces a cursor bounded by the [BatchReader]'s
349/// lifetime.  Suppose [BatchReader::cursor] could use an existing [Cursor]
350/// implementation, such as [CursorList], but it needs to create some data for
351/// the cursor to own.  Then it's rather inconvenient because it's necessary to
352/// create a new type to own the data and implement the whole broad [Cursor]
353/// trait to forward every call to [CursorList]. Plus, you end up with a
354/// self-referencing type, so you need to use [ouroboros].  See `SpineCursor` in
355/// the async spine for a good example.
356///
357/// Suppose we're building a new interface where we want to return a `dyn
358/// Cursor`, and some of the implementations would suffer from the problem
359/// above.  We can instead return a [CursorFactory].  An implementation of this
360/// trait can own the data that it needs to, and then implement
361/// [CursorFactory::get_cursor] to create and return a cursor whose lifetime is
362/// bounded by the [CursorFactory].  This reduces the boilerplate a great deal
363/// (and we don't need [ouroboros], either).
364///
365/// [BatchReader::cursor] could be retrofitted to this interface, but it's not
366/// clear that it's worth it, especially since it forces using `dyn Cursor`.
367pub 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
380/// A wrapper for [BatchReader] that implements [CursorFactory].
381pub 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
392/// A cursor that can be cloned as a `dyn Cursor` when it is inside a [`Box`].
393///
394/// Rust doesn't have a built-in way to clone boxed trait objects.  This
395/// provides such a way for boxed [`Cursor`]s.
396pub 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/// A wrapper around a `dyn Cursor` to allow choice of implementations at runtime.
582#[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
727/// Specialized cursor for merging.
728///
729/// A [MergeCursor] provides a subset of the operations of a [Cursor], which are
730/// just the operations needed for moving forward and reading data. This makes
731/// it easy to implement specialized versions of merge cursors for filtered and
732/// unfiltered operation, and for prefetching.
733pub 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
835/// A cursor that filters keys and values based on a key filter and a value filter.
836///
837/// This cursor only support simple value filters. Use `FilteredMergeCursorWithSnapshot`
838/// to evaluate more complex filters, such as `GroupFilter::LastN`.
839pub 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
939/// A cursor that filters keys and values based on a key filter and a value filter.
940///
941/// Uses a trace of the spine to evaluate `GroupFilter::LastN` filters.
942pub 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    /// The `snapshot` argument must be the snapshot of the entire spine that the cursor is being created for.
965    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
1074/// State maintained by a `GroupFilter` for a cursor.
1075enum 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        /// The smallest of the top `n` values below the waterline.
1087        min_val: Box<V>,
1088        /// True if there are more than `n` values below the waterline.
1089        /// If true, the cursor will skip values until reaching `min_val`.
1090        min_val_valid: bool,
1091    },
1092    BottomN {
1093        n: usize,
1094        filter: Filter<V>,
1095        /// The largest of the bottom `n` values below the waterline.
1096        max_val: Box<V>,
1097        /// True if there are more than `n` values below the waterline.
1098        /// If true, the cursor will include values `<=max_val`. Otherwise,
1099        /// it will include all values under the cursor.
1100        max_val_valid: bool,
1101    },
1102}
1103
1104impl<V: DataTrait + ?Sized> GroupFilterCursor<V> {
1105    /// Called after the cursor has advanced to a new key.
1106    ///
1107    /// Skip over values that don't satisfy the filter.
1108    /// Return true if the cursor points to the valid values, false otherwise.
1109    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                // Find the last value below the waterline.
1130                trace_cursor.fast_forward_vals();
1131                trace_cursor.seek_val_with_reverse(&|val| !(filter.filter_func())(val));
1132
1133                // Find n'th value below the waterline.
1134                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                    // `trace_cursor` points to the first value below the waterline.
1142                    // Skip values up to this value.
1143                    while cursor.val_valid() && cursor.val() < trace_cursor.val() {
1144                        // println!("skipping: {:?}", cursor.val());
1145                        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                // Find n'th value below the waterline.
1160                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                            // There are at least `n` values below the waterline.
1171                            // `on_step_val` will skip values until reaching `min_val`.
1172                            *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                    // Skip to the next value that belongs to the filtered cursor,
1182                    // which is either the next value that satisfies the filter or `min_val`.
1183                    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                // Find n'th value below the waterline.
1199                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    /// Called after the cursor has advanced to a new value.
1225    ///
1226    /// Skip over values that don't satisfy the filter.
1227    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/// Row is not available yet.
1343///
1344/// This value indicates that data may exist, but that it is not available yet,
1345/// because it is being loaded asynchronously in the background.
1346#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1347pub struct Pending;
1348
1349/// Cursor for non-blocking I/O.
1350///
1351/// Other implementations of cursors do not have a notion of whether data is in
1352/// memory, which means that operations to retrieve data can block on I/O.  A
1353/// `PushCursor` on the other hand, reports [Pending] if the data currently to
1354/// be retrieved is not yet available because of pending I/O.  This allows
1355/// merging using such a cursor to be more efficient.
1356///
1357/// `PushCursor` is optimized for reading all of the data in a batch.
1358pub trait PushCursor<K, V, T, R>
1359where
1360    K: ?Sized,
1361    V: ?Sized,
1362    R: ?Sized,
1363{
1364    /// Returns the current key as `Ok(Some(key))`, or `Ok(None)` if the batch
1365    /// is exhausted, or `Err(Pending)` if this key exists but isn't available
1366    /// yet.
1367    fn key(&self) -> Result<Option<&K>, Pending>;
1368
1369    /// Returns the current value as `Ok(Some(key))`, or `Ok(None)` if the key's
1370    /// values are exhausted, or `Err(Pending)` if this value exists but isn't
1371    /// available yet.
1372    ///
1373    /// It is an error if the batch is exhausted or the current key is not
1374    /// available.  The implementation might panic or return an incorrect value
1375    /// in this case (but it is not undefined behavior).
1376    fn val(&self) -> Result<Option<&V>, Pending>;
1377
1378    /// Applies `logic` to each pair of time and difference for the current
1379    /// key-value pair.
1380    ///
1381    /// When a key-value pair is available, all its time-diff pairs are
1382    /// available.
1383    ///
1384    /// It is an error if the current key and value are not available.  The
1385    /// implementation might panic or pass incorrect time-diff pairs to `logic`
1386    /// in this case (but it is not undefined behavior).
1387    fn map_times(&mut self, logic: &mut dyn FnMut(&T, &R));
1388
1389    /// Returns the weight associated with the current key/value pair.
1390    ///
1391    /// When a key-value pair is available, its weight is available.
1392    ///
1393    /// It is an error if the current key and value are not available.  The
1394    /// implementation might panic or pass incorrect time-diff pairs to `logic`
1395    /// in this case (but it is not undefined behavior).
1396    fn weight(&mut self) -> &R
1397    where
1398        T: PartialEq<()>;
1399
1400    /// Advances to the next key.
1401    ///
1402    /// It is an error if the batch is exhausted or the current key is not
1403    /// available.  The implementation might panic in this case (but it is not
1404    /// undefined behavior).
1405    fn step_key(&mut self);
1406
1407    /// Advances to the next value.
1408    ///
1409    /// It is an error if the batch is exhausted or the current key or value is
1410    /// not available.  The implementation might panic in this case (but it is
1411    /// not undefined behavior).
1412    fn step_val(&mut self);
1413
1414    /// Gives the implementation an opportunity to process I/O results and
1415    /// launch further I/O.  Implementations need this to called periodically.
1416    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}