Skip to content

Commit

Permalink
deprecate {BlockStore, DataStore}::open
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Sep 28, 2024
1 parent 1f618e2 commit 6cd5e89
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- chore: Provide `BitswapMessage` instead of `bitswap_pb::Message`. [PR 308](https://github.com/dariusc93/rust-ipfs/pull/308)
- refactor: bump msrv to 1.80
- refactor: Add custom error for ipns, added `Borrow<Cid>`, `Borrow<IpfsPath>` and `Into<IpfsPath>` to different function signatures. [PR 309](https://github.com/dariusc93/rust-ipfs/pull/309)
- refactor: deprecate `{BlockStore, DataStore}::open`

# 0.11.21
- chore: Put libp2p-webrtc-websys behind feature.
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,9 @@ impl Ipfs {
pub async fn publish_ipns<B: Borrow<IpfsPath>>(&self, path: B) -> Result<IpfsPath, Error> {
async move {
let ipns = self.ipns();
ipns.publish(None, path, Default::default()).await.map_err(anyhow::Error::from)
ipns.publish(None, path, Default::default())
.await
.map_err(anyhow::Error::from)
}
.instrument(self.span.clone())
.await
Expand Down
8 changes: 0 additions & 8 deletions src/repo/blockstore/flatfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ impl BlockStore for FsBlockStore {
Ok(())
}

async fn open(&self) -> Result<(), Error> {
Ok(())
}

async fn contains(&self, cid: &Cid) -> Result<bool, Error> {
let inner = &*self.inner.read().await;
inner.contains(cid).await
Expand Down Expand Up @@ -335,7 +331,6 @@ mod tests {
let block = Block::new(cid, data).unwrap();

store.init().await.unwrap();
store.open().await.unwrap();

let contains = store.contains(&cid).await.unwrap();
assert!(!contains);
Expand Down Expand Up @@ -373,13 +368,11 @@ mod tests {

let block_store = FsBlockStore::new(tmp.clone());
block_store.init().await.unwrap();
block_store.open().await.unwrap();

assert!(!block_store.contains(block.cid()).await.unwrap());
block_store.put(&block).await.unwrap();

let block_store = FsBlockStore::new(tmp.clone());
block_store.open().await.unwrap();
assert!(block_store.contains(block.cid()).await.unwrap());
assert_eq!(block_store.get(block.cid()).await.unwrap().unwrap(), block);

Expand All @@ -394,7 +387,6 @@ mod tests {

let block_store = FsBlockStore::new(tmp.clone());
block_store.init().await.unwrap();
block_store.open().await.unwrap();

for data in &[b"1", b"2", b"3"] {
let data_slice = data.to_vec();
Expand Down
4 changes: 0 additions & 4 deletions src/repo/blockstore/idb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ impl BlockStore for IdbBlockStore {
Ok(())
}

async fn open(&self) -> Result<(), Error> {
Ok(())
}

async fn contains(&self, cid: &Cid) -> Result<bool, Error> {
let database = self.get_db().clone();
let (tx, rx) = oneshot::channel();
Expand Down
6 changes: 0 additions & 6 deletions src/repo/blockstore/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ impl BlockStore for MemBlockStore {
Ok(())
}

async fn open(&self) -> Result<(), Error> {
Ok(())
}

async fn contains(&self, cid: &Cid) -> Result<bool, Error> {
let inner = &*self.inner.read().await;
Ok(inner.blocks.contains_key(cid))
Expand Down Expand Up @@ -149,7 +145,6 @@ mod tests {
let block = Block::new(cid, data).unwrap();

store.init().await.unwrap();
store.open().await.unwrap();

let contains = store.contains(&cid);
assert!(!contains.await.unwrap());
Expand Down Expand Up @@ -179,7 +174,6 @@ mod tests {
let mem_store = MemBlockStore::new(tmp);

mem_store.init().await.unwrap();
mem_store.open().await.unwrap();

for data in &[b"1", b"2", b"3"] {
let data_slice = data.to_vec();
Expand Down
1 change: 0 additions & 1 deletion src/repo/common_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ impl<T: DataStore> DSTestContext<T> {
let ds = factory(p);

ds.init().await.unwrap();
ds.open().await.unwrap();

DSTestContext {
tempdir,
Expand Down
5 changes: 0 additions & 5 deletions src/repo/datastore/flatfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,6 @@ impl DataStore for FsDataStore {
Ok(())
}

async fn open(&self) -> Result<(), Error> {
Ok(())
}

async fn contains(&self, key: &[u8]) -> Result<bool, Error> {
let _g = self.ds_guard.read().await;
Ok(self._contains(key))
Expand Down Expand Up @@ -779,7 +775,6 @@ mod test {
let value = [5, 6, 7, 8];

store.init().await?;
store.open().await?;

let contains = store.contains(&key).await.unwrap();
assert!(!contains);
Expand Down
4 changes: 0 additions & 4 deletions src/repo/datastore/idb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ impl DataStore for IdbDataStore {
Ok(())
}

async fn open(&self) -> Result<(), Error> {
Ok(())
}

/// Checks if a key is present in the datastore.
async fn contains(&self, key: &[u8]) -> Result<bool, Error> {
let database = self.get_db().to_owned();
Expand Down
5 changes: 0 additions & 5 deletions src/repo/datastore/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,6 @@ impl DataStore for MemDataStore {
Ok(())
}

async fn open(&self) -> Result<(), Error> {
Ok(())
}

async fn contains(&self, key: &[u8]) -> Result<bool, Error> {
let contains = self.inner.lock().await.contains_key(key);
Ok(contains)
Expand Down Expand Up @@ -600,7 +596,6 @@ mod tests {
let value = [5, 6, 7, 8];

store.init().await.unwrap();
store.open().await.unwrap();

let contains = store.contains(&key);
assert!(!contains.await.unwrap());
Expand Down
40 changes: 13 additions & 27 deletions src/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ pub enum BlockRmError {
#[async_trait]
pub trait BlockStore: Debug + Send + Sync {
async fn init(&self) -> Result<(), Error>;
/// FIXME: redundant and never called during initialization, which is expected to happen during [`init`].
async fn open(&self) -> Result<(), Error>;

#[deprecated]
async fn open(&self) -> Result<(), Error> {
Ok(())
}
/// Returns whether a block is present in the blockstore.
async fn contains(&self, cid: &Cid) -> Result<bool, Error>;
/// Returns a block from the blockstore.
Expand All @@ -90,7 +93,10 @@ pub trait BlockStore: Debug + Send + Sync {
/// Generic layer of abstraction for a key-value data store.
pub trait DataStore: PinStore + Debug + Send + Sync {
async fn init(&self) -> Result<(), Error>;
async fn open(&self) -> Result<(), Error>;
#[deprecated]
async fn open(&self) -> Result<(), Error> {
Ok(())
}
/// Checks if a key is present in the datastore.
async fn contains(&self, key: &[u8]) -> Result<bool, Error>;
/// Returns the value associated with a key from the datastore.
Expand Down Expand Up @@ -574,30 +580,10 @@ impl Repo {
log::debug!("lockfile tried");
}

let f1 = self.inner.block_store.init();
let f2 = self.inner.data_store.init();
let (r1, r2) = futures::future::join(f1, f2).await;
let init = &self.inner.initialized;
if r1.is_err() {
r1.map(|_| {
init.store(true, Ordering::SeqCst);
})
} else {
r2.map(|_| {
init.store(true, Ordering::SeqCst);
})
}
}

pub async fn open(&self) -> Result<(), Error> {
let f1 = self.inner.block_store.open();
let f2 = self.inner.data_store.open();
let (r1, r2) = futures::future::join(f1, f2).await;
if r1.is_err() {
r1
} else {
r2
}
self.inner.block_store.init().await?;
self.inner.data_store.init().await?;
self.inner.initialized.store(true, Ordering::SeqCst);
Ok(())
}

/// Puts a block into the block store.
Expand Down

0 comments on commit 6cd5e89

Please sign in to comment.