Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKickliter committed Apr 19, 2024
1 parent 32805dd commit 12b82ad
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
23 changes: 22 additions & 1 deletion src/disktree/iter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
cell::CellStack,
disktree::{dptr::Dp, dtseek::DtSeek, tree::HDR_SZ, varint},
disktree::{dptr::Dp, dtseek::DtSeek, tree::HDR_SZ, varint, DiskTreeMap},
error::{Error, Result},
Cell,
};
Expand Down Expand Up @@ -106,6 +106,27 @@ impl<'a> Iter<'a> {
node_stack,
})
}

pub(crate) fn empty(disktree_buf: &'a [u8]) -> Iter<'a> {
let disktree_csr = Cursor::new(disktree_buf);
let cell_stack = CellStack::new();
let node_stack = Vec::new();
let recycle_bin = Vec::new();
let curr_node = None;
Self {
cell_stack,
curr_node,
disktree_buf,
disktree_csr,
recycle_bin,
node_stack,
}
}

/// Creates a new `Iter` over `cell` and its descendants.
pub(crate) fn subtree(_disktree: &'a DiskTreeMap, _cell: Cell) -> Result<Iter<'a>> {
unimplemented!()
}
}

impl<'a> Iterator for Iter<'a> {
Expand Down
19 changes: 17 additions & 2 deletions src/disktree/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,23 @@ impl DiskTreeMap {
}

/// Returns an iterator visiting the specified `cell` or its descendants.
pub fn subtree_iter(&self, _cell: Cell) -> Result<impl Iterator<Item = Result<(Cell, &[u8])>>> {
Ok(None.into_iter())
pub fn subtree_iter(&self, cell: Cell) -> Result<impl Iterator<Item = Result<(Cell, &[u8])>>> {
let iter = match self.get_raw(cell)? {
None => {
let iter = crate::disktree::iter::Iter::empty((*self.0).as_ref());
None.into_iter().chain(iter)
}
Some((cell, Node::Leaf(range))) => {
let iter = crate::disktree::iter::Iter::empty((*self.0).as_ref());
let val_bytes = &(*self.0).as_ref()[range];
Some(Ok((cell, val_bytes))).into_iter().chain(iter)
}
Some((cell, Node::Parent(_children))) => {
let iter = crate::disktree::iter::Iter::subtree(self, cell)?;
None.into_iter().chain(iter)
}
};
Ok(iter)
}

/// Returns the DPtr to a base (res0) cell dptr.
Expand Down

0 comments on commit 12b82ad

Please sign in to comment.