Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iroh-store): put_many bug #507

Merged
merged 2 commits into from
Nov 17, 2022
Merged
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
1 change: 1 addition & 0 deletions iroh-store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description = "Implementation of the storage part of iroh"

[dependencies]
anyhow = "1"
ahash = "0.8.0"
async-trait = "0.1.56"
bytecheck = "0.6.7"
bytes = "1.1.0"
Expand Down
36 changes: 34 additions & 2 deletions iroh-store/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{sync::Arc, thread::available_parallelism};

use ahash::AHashSet;
use anyhow::{anyhow, bail, Context, Result};
use bytes::Bytes;
use cid::Cid;
Expand Down Expand Up @@ -366,11 +367,14 @@ impl<'a> WriteStore<'a> {
let mut total_blob_size = 0;

let mut batch = WriteBatch::default();
let mut cid_tracker: AHashSet<Cid> = AHashSet::default();
Copy link
Contributor

Choose a reason for hiding this comment

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

If we had a vec we could use https://docs.rs/ahash/latest/ahash/struct.AHashSet.html#method.with_capacity here to make sure the hashset does not need to rehash.

But with the iterator we could just use the size hint, which is probably not worth it.

Copy link
Contributor

Choose a reason for hiding this comment

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

for (cid, blob, links) in blocks.into_iter() {
if self.has(&cid)? {
return Ok(());
if cid_tracker.contains(&cid) || self.has(&cid)? {
continue;
ramfox marked this conversation as resolved.
Show resolved Hide resolved
}

cid_tracker.insert(cid);

let id = self.next_id();

let id_bytes = id.to_be_bytes();
Expand Down Expand Up @@ -911,4 +915,32 @@ mod tests {
assert_eq!(Vec::<String>::new(), store.consistency_check()?);
Ok(())
}

#[tokio::test]
async fn test_put_many_repeat_ids() -> anyhow::Result<()> {
ramfox marked this conversation as resolved.
Show resolved Hide resolved
let cid1 = Cid::from_str("bafybeib4tddkl4oalrhe7q66rrz5dcpz4qwv5lmpstuqrls3djikw566y4")?;
let cid2 = Cid::from_str("QmcBphfXUFUNLcfAm31WEqYjrjEh19G5x4iAQANSK151DD")?;
let cid3 = Cid::from_str("bafkreieq5jui4j25lacwomsqgjeswwl3y5zcdrresptwgmfylxo2depppq")?;

let blob = Bytes::from(vec![0u8]);

let (store, _dir) = test_store().await?;
let blocks = vec![(cid1, blob.clone(), vec![]), (cid2, blob.clone(), vec![])];
store.put_many(blocks)?;
assert!(store.has(&cid1)?);
assert!(store.has(&cid2)?);

let blocks = vec![
(cid1, blob.clone(), vec![]),
(cid2, blob.clone(), vec![]),
(cid3, blob.clone(), vec![]),
];

store.put_many(blocks)?;
assert!(store.has(&cid1)?);
assert!(store.has(&cid2)?);
assert!(store.has(&cid3)?);

Ok(())
}
}