Skip to content

Commit

Permalink
fix(indexeddb): Abort failed transactions (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique authored Aug 13, 2024
1 parent 4bba613 commit 73cc6d8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ multihash-codetable = { version = "0.1.1", features = ["digest", "sha2"] }
wasm-bindgen-test = "0.3.41"

[features]
indexeddb = ["dep:js-sys", "dep:rexie", "dep:wasm-bindgen"]
indexeddb = ["dep:js-sys", "dep:rexie", "dep:wasm-bindgen", "dep:idb"]
lru = ["dep:lru"]
redb = ["dep:redb", "dep:tokio"]
sled = ["dep:sled", "dep:tokio"]
Expand Down
37 changes: 30 additions & 7 deletions src/indexed_db_blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,25 @@ impl Blockstore for IndexedDbBlockstore {
let tx = self
.db
.transaction(&[BLOCK_STORE], TransactionMode::ReadWrite)?;
let blocks = tx.store(BLOCK_STORE)?;

if !has_key(&blocks, &cid).await? {
blocks.add(&data, Some(&cid)).await?;
let res = async {
let blocks = tx.store(BLOCK_STORE)?;

if !has_key(&blocks, &cid).await? {
blocks.add(&data, Some(&cid)).await?;
}

Ok(())
}
.await;

if res.is_ok() {
tx.commit().await?;
} else {
tx.abort().await?;
}
Ok(())

res
}

async fn remove<const S: usize>(&self, cid: &CidGeneric<S>) -> Result<()> {
Expand All @@ -87,11 +100,21 @@ impl Blockstore for IndexedDbBlockstore {
let tx = self
.db
.transaction(&[BLOCK_STORE], TransactionMode::ReadWrite)?;
let blocks = tx.store(BLOCK_STORE)?;

blocks.delete(cid.into()).await?;
let res = async {
let blocks = tx.store(BLOCK_STORE)?;
blocks.delete(cid.into()).await?;
Ok(())
}
.await;

if res.is_ok() {
tx.commit().await?;
} else {
tx.abort().await?;
}

Ok(())
res
}

async fn has<const S: usize>(&self, cid: &CidGeneric<S>) -> Result<bool> {
Expand Down

0 comments on commit 73cc6d8

Please sign in to comment.