Skip to content

Commit

Permalink
Choose appropriate datastructures for references
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Aug 25, 2023
1 parent cad84c8 commit b3c51ae
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 6 deletions.
3 changes: 1 addition & 2 deletions car-mirror/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,12 @@ pub async fn block_receive(
/// This will error out if
/// - the codec is not supported
/// - the block can't be parsed.
pub fn references(cid: Cid, block: impl AsRef<[u8]>) -> Result<Vec<Cid>> {
pub fn references<E: Extend<Cid>>(cid: Cid, block: impl AsRef<[u8]>, mut refs: E) -> Result<E> {
let codec: IpldCodec = cid
.codec()
.try_into()
.map_err(|_| anyhow!("Unsupported codec in Cid: {cid}"))?;

let mut refs = Vec::new();
<Ipld as References<IpldCodec>>::references(codec, &mut Cursor::new(block), &mut refs)?;
Ok(refs)
}
Expand Down
4 changes: 2 additions & 2 deletions car-mirror/src/dag_walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl DagWalk {
// - skip Raw CIDs. They can't have further links (but needs adjustment to this function's return type)
// - run multiple `get_block` calls concurrently
let block = store.get_block(&cid).await?;
for ref_cid in references(cid, &block)? {
for ref_cid in references(cid, &block, HashSet::new())? {
if !self.visited.contains(&ref_cid) {
self.frontier.push_front(ref_cid);
}
Expand Down Expand Up @@ -112,7 +112,7 @@ impl DagWalk {
/// Skip a node from the traversal for now.
pub fn skip_walking(&mut self, block: (Cid, Bytes)) -> Result<()> {
let (cid, bytes) = block;
let refs = references(cid, bytes)?;
let refs = references(cid, bytes, HashSet::new())?;
self.visited.insert(cid);
self.frontier
.retain(|frontier_cid| !refs.contains(frontier_cid));
Expand Down
2 changes: 1 addition & 1 deletion car-mirror/src/test_utils/blockstore_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn dag_to_dot(

for (cid, ipld) in blocks {
let bytes = encode(&ipld)?;
let refs = references(cid, bytes)?;
let refs = references(cid, bytes, Vec::new())?;
for to_cid in refs {
print_truncated_string(writer, cid.to_string())?;
write!(writer, " -> ")?;
Expand Down
2 changes: 1 addition & 1 deletion car-mirror/src/test_utils/local_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) async fn get_cid_at_approx_path(
let mut working_cid = root;
for nth in path {
let block = store.get_block(&working_cid).await?;
let refs = references(working_cid, block)?;
let refs = references(working_cid, block, Vec::new())?;
if refs.is_empty() {
break;
}
Expand Down

0 comments on commit b3c51ae

Please sign in to comment.