Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/homedb/src/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct RangeIterator {
btree: Arc<Btree<DbKey, DbValue>>,
handle: Option<QueryResultHandle<'static, DbKey, DbValue>>,
current_batch: Vec<(DbKey, DbValue)>,
cursor: usize,
start_key: Vec<u8>,
end_key: Vec<u8>,
batch_size: u32,
Expand Down Expand Up @@ -47,6 +48,7 @@ impl RangeIterator {
btree,
handle: if has_more { Some(handle) } else { None },
current_batch: results,
cursor: 0,
start_key,
end_key,
batch_size,
Expand All @@ -64,13 +66,16 @@ impl RangeIterator {
#[maybe_async_cfg::maybe(keep_self, sync(feature = "sync_frontend"), async(feature = "async_frontend"))]
pub async fn next(&mut self) -> Result<Option<(Vec<u8>, Vec<u8>)>> {
loop {
// Try to get from current batch
if !self.current_batch.is_empty() {
let (key, value) = self.current_batch.remove(0);
return Ok(Some((key.into_vec(), value.into_vec())));
if self.cursor < self.current_batch.len() {
let (key, value) = &self.current_batch[self.cursor];
let result = (key.clone().into_vec(), value.clone().into_vec());
self.cursor += 1;
return Ok(Some(result));
}

// Current batch exhausted, try to fetch next batch
// Batch exhausted — drop it and fetch the next one.
self.current_batch.clear();
self.cursor = 0;
match self.handle.take() {
Some(handle) if handle.has_more() => {
let next_handle = self.btree
Expand Down Expand Up @@ -104,6 +109,7 @@ impl RangeIterator {
pub async fn seek(&mut self, key: &[u8]) -> Result<bool> {
self.handle = None;
self.current_batch.clear();
self.cursor = 0;

let (range_start, range_end) = if self.reverse {
(
Expand Down
8 changes: 4 additions & 4 deletions src/homestore/index/btree/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,11 @@ where
) -> Result<QueryResultHandle<K, V>, BtreeError> {
let req = handle.request();
if req.is_sweep_query() {
// Use traversal query for reverse iteration
self.traversal_query_internal(req).await
} else {
// Use sweep query for forward iteration
// Sweep query continues with sweep (forward, sibling-link based)
self.sweep_query_internal(req).await
} else {
// Traversal query continues with traversal (supports reverse, no sibling links)
self.traversal_query_internal(req).await

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did it even work earlier was a wonder :-)

}
}
}
Expand Down