prolly-map 0.3.0

Content-addressed versioned map storage primitives.
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
//! Range iteration operations for Prolly trees
//!
//! This module handles range queries and iteration over key-value pairs
//! within specified bounds. It provides efficient traversal of the tree
//! in lexicographic order with support for start and end bounds.
//!
//! # Overview
//!
//! Range iteration allows traversing key-value pairs in sorted order within
//! a specified key range. The iterator efficiently navigates the tree structure,
//! handling node boundaries transparently.
//!
//! # Iteration Behavior
//!
//! ## Start Bound (Inclusive)
//!
//! The iterator begins at the first key greater than or equal to the start bound.
//! If the start key exists, iteration begins there. If not, iteration begins at
//! the next key in lexicographic order.
//!
//! Use an empty slice (`&[]`) to start from the beginning of the tree.
//!
//! ## End Bound (Exclusive)
//!
//! The iterator stops before reaching the end bound. Keys equal to or greater
//! than the end bound are not yielded.
//!
//! Use `None` to iterate to the end of the tree.
//!
//! # Implementation Details
//!
//! The iterator maintains a stack of (node, index) pairs representing the
//! current position in the tree. This allows efficient traversal across
//! node boundaries without restarting from the root.
//!
//! ## Traversal Algorithm
//!
//! 1. **Initial positioning**: Find the path to the start key
//! 2. **Leaf iteration**: Yield entries from the current leaf
//! 3. **Node advancement**: When a leaf is exhausted, backtrack to find the next leaf
//! 4. **Bound checking**: Stop when the end bound is reached
//!
//! # Example
//!
//! ```rust
//! use prolly::{Prolly, MemStore, Config};
//!
//! let store = MemStore::new();
//! let prolly = Prolly::new(store, Config::default());
//! let mut tree = prolly.create();
//!
//! // Insert some data
//! tree = prolly.put(&tree, b"a".to_vec(), b"1".to_vec()).unwrap();
//! tree = prolly.put(&tree, b"b".to_vec(), b"2".to_vec()).unwrap();
//! tree = prolly.put(&tree, b"c".to_vec(), b"3".to_vec()).unwrap();
//! tree = prolly.put(&tree, b"d".to_vec(), b"4".to_vec()).unwrap();
//!
//! // Iterate over all keys
//! for result in prolly.range(&tree, &[], None).unwrap() {
//!     let (key, val) = result.unwrap();
//!     println!("{:?} -> {:?}", key, val);
//! }
//!
//! // Iterate over range [b, d) - yields "b" and "c"
//! for result in prolly.range(&tree, b"b", Some(b"d")).unwrap() {
//!     let (key, val) = result.unwrap();
//!     println!("{:?} -> {:?}", key, val);
//! }
//! ```
//!
//! # Performance
//!
//! - **Initial seek**: O(log n) to find the starting position
//! - **Per-entry**: O(1) amortized for sequential access within a leaf
//! - **Node transitions**: O(log n) worst case, but typically O(1) amortized
//!
//! The iterator is lazy and only loads nodes as needed, making it memory-efficient
//! for large trees.

use super::cid::Cid;
use super::error::Error;
use super::node::Node;
use super::store::Store;
use super::tree::Tree;

use super::Prolly;
#[cfg(feature = "async-store")]
use super::{store::AsyncStore, AsyncProlly};
#[cfg(feature = "async-store")]
use futures_util::stream::{self, Stream};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

type RangeItem = Result<(Vec<u8>, Vec<u8>), Error>;
type LeafEntry = (Vec<u8>, Vec<u8>);
type OptionalLeafEntry = Result<Option<LeafEntry>, Error>;
pub(crate) const RANGE_CHILD_PREFETCH_PARALLELISM: usize = 16;

/// Stable cursor token for resumable range scans.
///
/// The token is independent of in-memory traversal state: it records the last
/// emitted key, and the next scan resumes strictly after that key. This makes it
/// suitable for checkpointing background indexing or sync jobs for an immutable
/// tree snapshot.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct RangeCursor {
    after_key: Option<Vec<u8>>,
}

impl RangeCursor {
    /// Start scanning from the beginning of the requested range.
    pub fn start() -> Self {
        Self { after_key: None }
    }

    /// Resume scanning strictly after `key`.
    pub fn after_key(key: impl Into<Vec<u8>>) -> Self {
        Self {
            after_key: Some(key.into()),
        }
    }

    /// Return the key this cursor resumes after, if any.
    pub fn after(&self) -> Option<&[u8]> {
        self.after_key.as_deref()
    }

    /// Whether this cursor represents the beginning of a range.
    pub fn is_start(&self) -> bool {
        self.after_key.is_none()
    }
}

/// A bounded page of range-scan results.
///
/// `next_cursor` is `Some` when another call should resume after the last entry
/// in this page. It is `None` when the scan reached the end bound or the end of
/// the tree.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RangePage {
    pub entries: Vec<(Vec<u8>, Vec<u8>)>,
    pub next_cursor: Option<RangeCursor>,
}

/// Stable cursor token for resumable reverse scans.
///
/// The token records the next exclusive upper bound. A start cursor scans from
/// the end of the requested range; a resumed cursor scans keys strictly before
/// `before_key`.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReverseCursor {
    before_key: Option<Vec<u8>>,
}

impl ReverseCursor {
    /// Start scanning from the end of the requested range.
    pub fn end() -> Self {
        Self { before_key: None }
    }

    /// Resume scanning strictly before `key`.
    pub fn before_key(key: impl Into<Vec<u8>>) -> Self {
        Self {
            before_key: Some(key.into()),
        }
    }

    /// Return the key this cursor resumes before, if any.
    pub fn before(&self) -> Option<&[u8]> {
        self.before_key.as_deref()
    }

    /// Whether this cursor represents the end of a range.
    pub fn is_end(&self) -> bool {
        self.before_key.is_none()
    }
}

/// A bounded page of reverse-scan results.
///
/// Entries are returned in descending key order. `next_cursor` is `Some` when
/// another call should resume before the last entry in this page.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ReversePage {
    pub entries: Vec<(Vec<u8>, Vec<u8>)>,
    pub next_cursor: Option<ReverseCursor>,
}

/// Bounded result for a stateless cursor seek.
///
/// `position_key`/`position_value` report where the internal cursor lands for
/// the requested seek key. This is the exact key when `found` is true; otherwise
/// it is the closest leaf entry chosen by cursor navigation. `entries` are the
/// forward window starting at the first key greater than or equal to the seek
/// key, and `next_cursor` resumes after the last emitted entry.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CursorWindow {
    pub position_key: Option<Vec<u8>>,
    pub position_value: Option<Vec<u8>>,
    pub found: bool,
    pub entries: Vec<(Vec<u8>, Vec<u8>)>,
    pub next_cursor: Option<RangeCursor>,
}

/// Backward-compatible name for async range pages.
#[cfg(feature = "async-store")]
pub type AsyncRangePage = RangePage;

/// Backward-compatible name for async reverse range pages.
#[cfg(feature = "async-store")]
pub type AsyncReversePage = ReversePage;

/// Create a range iterator over key-value pairs.
///
/// Returns an iterator that yields `(key, value)` pairs in lexicographic order,
/// starting from `start` (inclusive) up to `end` (exclusive).
///
/// # Arguments
/// * `prolly` - Reference to the Prolly tree manager
/// * `tree` - The tree to iterate over
/// * `start` - The starting key (inclusive). Use `&[]` to start from the beginning.
/// * `end` - Optional ending key (exclusive). Use `None` to iterate to the end.
///
/// # Returns
/// * `Ok(RangeIter)` - An iterator over the range
/// * `Err` on storage or deserialization errors during path finding
pub fn create_range_iter<'a, S: Store>(
    prolly: &'a Prolly<S>,
    tree: &Tree,
    start: &[u8],
    end: Option<&[u8]>,
) -> Result<RangeIter<'a, S>, Error> {
    if end.is_some_and(|end| end <= start) {
        return Ok(RangeIter::new(prolly, Vec::new(), start, end));
    }

    // Find path to start key
    let path = prolly.find_path_arcs(tree, start)?;
    Ok(RangeIter::new(prolly, path, start, end))
}

/// Create a range iterator that starts strictly after `after_key`.
pub fn create_range_after_iter<'a, S: Store>(
    prolly: &'a Prolly<S>,
    tree: &Tree,
    after_key: &[u8],
    end: Option<&[u8]>,
) -> Result<RangeIter<'a, S>, Error> {
    if end.is_some_and(|end| end <= after_key) {
        return Ok(RangeIter::new_after(prolly, Vec::new(), after_key, end));
    }

    let path = prolly.find_path_arcs(tree, after_key)?;
    Ok(RangeIter::new_after(prolly, path, after_key, end))
}

/// Create an async range iterator over key-value pairs.
#[cfg(feature = "async-store")]
pub async fn create_async_range_iter<'a, S>(
    prolly: &'a AsyncProlly<S>,
    tree: &Tree,
    start: &[u8],
    end: Option<&[u8]>,
) -> Result<AsyncRangeIter<'a, S>, Error>
where
    S: AsyncStore,
    S::Error: Send + Sync,
{
    if end.is_some_and(|end| end <= start) {
        return Ok(AsyncRangeIter::new(prolly, Vec::new(), start, end));
    }

    let path = prolly.find_path_arcs(tree, start).await?;
    Ok(AsyncRangeIter::new(prolly, path, start, end))
}

/// Create an async range iterator that starts strictly after `after_key`.
#[cfg(feature = "async-store")]
pub async fn create_async_range_after_iter<'a, S>(
    prolly: &'a AsyncProlly<S>,
    tree: &Tree,
    after_key: &[u8],
    end: Option<&[u8]>,
) -> Result<AsyncRangeIter<'a, S>, Error>
where
    S: AsyncStore,
    S::Error: Send + Sync,
{
    if end.is_some_and(|end| end <= after_key) {
        return Ok(AsyncRangeIter::new_after(
            prolly,
            Vec::new(),
            after_key,
            end,
        ));
    }

    let path = prolly.find_path_arcs(tree, after_key).await?;
    Ok(AsyncRangeIter::new_after(prolly, path, after_key, end))
}

/// Iterator over key-value pairs in a range.
///
/// Created by [`Prolly::range`]. Yields `(key, value)` pairs in lexicographic
/// order.
///
/// Maintains a stack of (node, index) pairs to track the current position in the tree
/// and supports optional end bounds for range queries.
pub struct RangeIter<'a, S: Store> {
    /// Reference to the Prolly tree manager
    prolly: &'a Prolly<S>,
    /// Stack of (node, index) pairs representing the traversal path
    stack: Vec<(Arc<Node>, usize)>,
    /// Optional end bound (exclusive)
    end: Option<Vec<u8>>,
    /// Whether we've started iteration (for positioning at start key)
    started: bool,
    /// The start key for initial positioning
    start_key: Vec<u8>,
    /// Whether to skip an entry equal to the start key.
    skip_start_key: bool,
    /// Last key yielded by this iterator.
    last_key: Option<Vec<u8>>,
}

impl<'a, S: Store> RangeIter<'a, S> {
    /// Create a new range iterator.
    ///
    /// # Arguments
    /// * `prolly` - Reference to the Prolly tree manager
    /// * `stack` - Initial traversal path from find_path
    /// * `start` - The starting key (inclusive)
    /// * `end` - Optional ending key (exclusive)
    pub(crate) fn new(
        prolly: &'a Prolly<S>,
        stack: Vec<(Arc<Node>, usize)>,
        start: &[u8],
        end: Option<&[u8]>,
    ) -> Self {
        Self {
            prolly,
            stack,
            end: end.map(|e| e.to_vec()),
            started: false,
            start_key: start.to_vec(),
            skip_start_key: false,
            last_key: None,
        }
    }

    pub(crate) fn new_after(
        prolly: &'a Prolly<S>,
        stack: Vec<(Arc<Node>, usize)>,
        after_key: &[u8],
        end: Option<&[u8]>,
    ) -> Self {
        Self {
            prolly,
            stack,
            end: end.map(|e| e.to_vec()),
            started: false,
            start_key: after_key.to_vec(),
            skip_start_key: true,
            last_key: None,
        }
    }

    /// Return a resumable cursor for the last key yielded by this iterator.
    ///
    /// If the iterator has not yielded an item yet, this returns
    /// [`RangeCursor::start`].
    pub fn resume_cursor(&self) -> RangeCursor {
        self.last_key
            .clone()
            .map(RangeCursor::after_key)
            .unwrap_or_else(RangeCursor::start)
    }

    /// Position the iterator at the first key >= start_key.
    ///
    /// Called on the first iteration to find the correct starting position
    /// in the leaf node.
    fn position_at_start(&mut self) -> Option<RangeItem> {
        self.started = true;

        // If stack is empty, tree is empty
        if self.stack.is_empty() {
            return None;
        }

        // Find the first key >= start_key in the leaf
        let (node, idx) = self.stack.last_mut()?;

        // If we're at a leaf, find the correct starting position
        if node.leaf {
            // Find first key >= start_key
            let start_idx = match node.search(&self.start_key) {
                Ok(i) if self.skip_start_key => i.saturating_add(1),
                Ok(i) => i,  // Exact match
                Err(i) => i, // First key > start_key
            };

            *idx = start_idx;

            // Check if we're past the end of this node
            if *idx >= node.len() {
                // Need to advance to next node
                return self.advance_to_next_leaf();
            }

            match leaf_entry_before_end(node, *idx, self.end.as_deref()) {
                Ok(Some(entry)) => {
                    *idx += 1;
                    self.last_key = Some(entry.0.clone());
                    return Some(Ok(entry));
                }
                Ok(None) => return None,
                Err(e) => return Some(Err(e)),
            }
        }

        // Not at a leaf - descend to the correct leaf
        self.descend_to_leaf()
    }

    /// Descend from current internal node to the leftmost leaf.
    ///
    /// Follows child pointers until reaching a leaf node, then returns
    /// the first entry from that leaf.
    fn descend_to_leaf(&mut self) -> Option<RangeItem> {
        loop {
            let (node, idx) = self.stack.last()?;

            if node.leaf {
                // We're at a leaf, return the current entry
                if *idx >= node.len() {
                    return self.advance_to_next_leaf();
                }

                match leaf_entry_before_end(node, *idx, self.end.as_deref()) {
                    Ok(Some(entry)) => {
                        if let Some((_, idx)) = self.stack.last_mut() {
                            *idx += 1;
                        }
                        self.last_key = Some(entry.0.clone());
                        return Some(Ok(entry));
                    }
                    Ok(None) => return None,
                    Err(e) => return Some(Err(e)),
                }
            }

            // Internal node - descend to child
            if *idx >= node.len() {
                // No more children, go back up
                return self.advance_to_next_leaf();
            }

            match child_starts_at_or_after_end(self.end.as_deref(), node, *idx) {
                Ok(true) => return None,
                Ok(false) => {}
                Err(e) => return Some(Err(e)),
            }

            match self.load_child_for_descent(node, *idx) {
                Ok(child) => {
                    self.stack.push((child, 0));
                }
                Err(e) => return Some(Err(e)),
            }
        }
    }

    /// Advance to the next leaf node when current leaf is exhausted.
    ///
    /// Pops nodes from the stack until finding a parent with more children,
    /// then descends to the next child's leftmost leaf.
    fn advance_to_next_leaf(&mut self) -> Option<RangeItem> {
        loop {
            // Pop the current node
            self.stack.pop();

            if self.stack.is_empty() {
                return None;
            }

            // Increment parent's index
            if let Some((parent, parent_idx)) = self.stack.last_mut() {
                *parent_idx += 1;

                // Check if parent has more children
                if *parent_idx < parent.len() {
                    match child_starts_at_or_after_end(self.end.as_deref(), parent, *parent_idx) {
                        Ok(true) => return None,
                        Ok(false) => {}
                        Err(e) => return Some(Err(e)),
                    }
                    // Descend to the next child
                    return self.descend_to_leaf();
                }
                if parent.keys.len() != parent.vals.len() {
                    return Some(Err(Error::InvalidNode));
                }
                // Otherwise, continue popping
            }
        }
    }

    fn load_child_for_descent(&self, node: &Node, idx: usize) -> Result<Arc<Node>, Error> {
        let child_cid = child_cid_at(node, idx)?;

        if !self.prolly.store().prefers_batch_reads() {
            return self.prolly.load_arc(&child_cid);
        }

        if let Some(child) = self.prolly.cached_node_arc(&child_cid) {
            return Ok(child);
        }

        let max_child_idx = node
            .len()
            .min(idx.saturating_add(RANGE_CHILD_PREFETCH_PARALLELISM));
        let mut child_cids = Vec::with_capacity(max_child_idx.saturating_sub(idx));
        child_cids.push(child_cid);

        for child_idx in idx + 1..max_child_idx {
            match child_starts_at_or_after_end(self.end.as_deref(), node, child_idx) {
                Ok(true) => break,
                Ok(false) => {}
                Err(_) => break,
            }

            match child_cid_at(node, child_idx) {
                Ok(cid) => child_cids.push(cid),
                Err(_) => break,
            }
        }

        if child_cids.len() == 1 {
            return self.prolly.load_arc(&child_cids[0]);
        }

        let children = self
            .prolly
            .load_many_ordered_with_parallelism(&child_cids, RANGE_CHILD_PREFETCH_PARALLELISM)?;
        children.into_iter().next().ok_or(Error::InvalidNode)
    }
}

/// Iterator implementation for RangeIter.
///
/// Yields `(key, value)` pairs in lexicographic order, handling cursor advancement
/// across node boundaries and checking end bounds for each yielded entry.
impl<'a, S: Store> Iterator for RangeIter<'a, S> {
    type Item = Result<(Vec<u8>, Vec<u8>), Error>;

    fn next(&mut self) -> Option<Self::Item> {
        // First call: position at start key
        if !self.started {
            return self.position_at_start();
        }

        loop {
            let (node, idx) = self.stack.last_mut()?;

            if !node.leaf && node.keys.len() != node.vals.len() {
                return Some(Err(Error::InvalidNode));
            }

            // If we've exhausted this node, advance to next
            if *idx >= node.len() {
                return self.advance_to_next_leaf();
            }

            // If we're at a leaf, yield the current entry
            if node.leaf {
                match leaf_entry_before_end(node, *idx, self.end.as_deref()) {
                    Ok(Some(entry)) => {
                        *idx += 1;
                        self.last_key = Some(entry.0.clone());
                        return Some(Ok(entry));
                    }
                    Ok(None) => return None,
                    Err(e) => return Some(Err(e)),
                }
            }

            // Internal node - descend to child
            match child_starts_at_or_after_end(self.end.as_deref(), node, *idx) {
                Ok(true) => return None,
                Ok(false) => {}
                Err(e) => return Some(Err(e)),
            }

            let child = {
                let (node, idx) = self.stack.last()?;
                self.load_child_for_descent(node, *idx)
            };

            match child {
                Ok(child) => {
                    self.stack.push((child, 0));
                }
                Err(e) => return Some(Err(e)),
            }
        }
    }
}

/// Async iterator over key-value pairs in a range.
///
/// Created by [`AsyncProlly::range`](crate::AsyncProlly::range). Call
/// [`AsyncRangeIter::next`] to lazily read one item at a time, or
/// [`AsyncRangeIter::into_stream`] to adapt it to a `futures_util::Stream`.
#[cfg(feature = "async-store")]
pub struct AsyncRangeIter<'a, S: AsyncStore> {
    prolly: &'a AsyncProlly<S>,
    stack: Vec<(Arc<Node>, usize)>,
    end: Option<Vec<u8>>,
    started: bool,
    start_key: Vec<u8>,
    skip_start_key: bool,
    last_key: Option<Vec<u8>>,
}

#[cfg(feature = "async-store")]
impl<'a, S> AsyncRangeIter<'a, S>
where
    S: AsyncStore,
    S::Error: Send + Sync,
{
    pub(crate) fn new(
        prolly: &'a AsyncProlly<S>,
        stack: Vec<(Arc<Node>, usize)>,
        start: &[u8],
        end: Option<&[u8]>,
    ) -> Self {
        Self {
            prolly,
            stack,
            end: end.map(|e| e.to_vec()),
            started: false,
            start_key: start.to_vec(),
            skip_start_key: false,
            last_key: None,
        }
    }

    pub(crate) fn new_after(
        prolly: &'a AsyncProlly<S>,
        stack: Vec<(Arc<Node>, usize)>,
        after_key: &[u8],
        end: Option<&[u8]>,
    ) -> Self {
        Self {
            prolly,
            stack,
            end: end.map(|e| e.to_vec()),
            started: false,
            start_key: after_key.to_vec(),
            skip_start_key: true,
            last_key: None,
        }
    }

    /// Return the next key-value pair in lexicographic order.
    pub async fn next(&mut self) -> Option<RangeItem> {
        self.position_at_start();

        loop {
            let (node, idx) = self.stack.last_mut()?;

            if !node.leaf && node.keys.len() != node.vals.len() {
                return Some(Err(Error::InvalidNode));
            }

            if *idx >= node.len() {
                match self.advance_to_next_sibling() {
                    Ok(true) => continue,
                    Ok(false) => return None,
                    Err(e) => return Some(Err(e)),
                }
            }

            if node.leaf {
                match leaf_entry_before_end(node, *idx, self.end.as_deref()) {
                    Ok(Some(entry)) => {
                        *idx += 1;
                        self.last_key = Some(entry.0.clone());
                        return Some(Ok(entry));
                    }
                    Ok(None) => return None,
                    Err(e) => return Some(Err(e)),
                }
            }

            match child_starts_at_or_after_end(self.end.as_deref(), node, *idx) {
                Ok(true) => return None,
                Ok(false) => {}
                Err(e) => return Some(Err(e)),
            }

            let child = {
                let (node, idx) = self.stack.last()?;
                self.load_child_for_descent(node, *idx).await
            };

            match child {
                Ok(child) => self.stack.push((child, 0)),
                Err(e) => return Some(Err(e)),
            }
        }
    }

    /// Collect all remaining range entries into memory.
    pub async fn collect(mut self) -> Result<Vec<LeafEntry>, Error> {
        let mut entries = Vec::new();
        while let Some(item) = self.next().await {
            entries.push(item?);
        }
        Ok(entries)
    }

    /// Return a resumable cursor for the last key yielded by this iterator.
    ///
    /// If the iterator has not yielded an item yet, this returns
    /// [`RangeCursor::start`].
    pub fn resume_cursor(&self) -> RangeCursor {
        self.last_key
            .clone()
            .map(RangeCursor::after_key)
            .unwrap_or_else(RangeCursor::start)
    }

    /// Convert this iterator into a `futures_util::Stream`.
    pub fn into_stream(self) -> impl Stream<Item = RangeItem> + 'a {
        stream::unfold(self, |mut iter| async move {
            iter.next().await.map(|item| (item, iter))
        })
    }

    fn position_at_start(&mut self) {
        if self.started {
            return;
        }

        self.started = true;
        let Some((node, idx)) = self.stack.last_mut() else {
            return;
        };

        if node.leaf {
            *idx = match node.search(&self.start_key) {
                Ok(i) if self.skip_start_key => i.saturating_add(1),
                Ok(i) | Err(i) => i,
            };
        }
    }

    fn advance_to_next_sibling(&mut self) -> Result<bool, Error> {
        loop {
            self.stack.pop();
            let Some((parent, parent_idx)) = self.stack.last_mut() else {
                return Ok(false);
            };

            *parent_idx += 1;

            if *parent_idx < parent.len() {
                if child_starts_at_or_after_end(self.end.as_deref(), parent, *parent_idx)? {
                    return Ok(false);
                }
                return Ok(true);
            }

            if parent.keys.len() != parent.vals.len() {
                return Err(Error::InvalidNode);
            }
        }
    }

    async fn load_child_for_descent(&self, node: &Node, idx: usize) -> Result<Arc<Node>, Error> {
        let child_cid = child_cid_at(node, idx)?;

        if !self.prolly.store().prefers_batch_reads() {
            return self.prolly.load_arc(&child_cid).await;
        }

        if let Some(child) = self.prolly.cached_node_arc(&child_cid) {
            return Ok(child);
        }

        let max_child_idx = node
            .len()
            .min(idx.saturating_add(RANGE_CHILD_PREFETCH_PARALLELISM));
        let mut child_cids = Vec::with_capacity(max_child_idx.saturating_sub(idx));
        child_cids.push(child_cid);

        for child_idx in idx + 1..max_child_idx {
            if child_starts_at_or_after_end(self.end.as_deref(), node, child_idx).unwrap_or(true) {
                break;
            }

            match child_cid_at(node, child_idx) {
                Ok(cid) => child_cids.push(cid),
                Err(_) => break,
            }
        }

        if child_cids.len() == 1 {
            return self.prolly.load_arc(&child_cids[0]).await;
        }

        let children = self.prolly.load_child_frontier_ordered(&child_cids).await?;
        children.into_iter().next().ok_or(Error::InvalidNode)
    }
}

fn leaf_entry_before_end(node: &Node, idx: usize, end: Option<&[u8]>) -> OptionalLeafEntry {
    let key = node.keys.get(idx).ok_or(Error::InvalidNode)?;
    if let Some(end) = end {
        if key.as_slice() >= end {
            return Ok(None);
        }
    }

    let val = node.vals.get(idx).ok_or(Error::InvalidNode)?;
    Ok(Some((key.clone(), val.clone())))
}

fn child_starts_at_or_after_end(
    end: Option<&[u8]>,
    node: &Node,
    child_index: usize,
) -> Result<bool, Error> {
    let Some(end) = end else {
        return Ok(false);
    };

    let first_key = node.keys.get(child_index).ok_or(Error::InvalidNode)?;
    Ok(first_key.as_slice() >= end)
}

fn child_cid_at(node: &Node, idx: usize) -> Result<Cid, Error> {
    let child = node.vals.get(idx).ok_or(Error::InvalidNode)?;
    Ok(Cid(child
        .as_slice()
        .try_into()
        .map_err(|_| Error::InvalidNode)?))
}