From 6cd5e897f5f65c0f07784b1153f5a8f86e8ce4e8 Mon Sep 17 00:00:00 2001 From: Darius Clark Date: Sat, 28 Sep 2024 14:56:30 -0400 Subject: [PATCH] deprecate `{BlockStore, DataStore}::open` --- CHANGELOG.md | 1 + src/lib.rs | 4 +++- src/repo/blockstore/flatfs.rs | 8 ------- src/repo/blockstore/idb.rs | 4 ---- src/repo/blockstore/memory.rs | 6 ------ src/repo/common_tests.rs | 1 - src/repo/datastore/flatfs.rs | 5 ----- src/repo/datastore/idb.rs | 4 ---- src/repo/datastore/memory.rs | 5 ----- src/repo/mod.rs | 40 ++++++++++++----------------------- 10 files changed, 17 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07722f47c..992522a4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`, `Borrow` and `Into` 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. diff --git a/src/lib.rs b/src/lib.rs index 09c54933b..0f0a145ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1284,7 +1284,9 @@ impl Ipfs { pub async fn publish_ipns>(&self, path: B) -> Result { 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 diff --git a/src/repo/blockstore/flatfs.rs b/src/repo/blockstore/flatfs.rs index 504b57b06..cd8d0eb47 100644 --- a/src/repo/blockstore/flatfs.rs +++ b/src/repo/blockstore/flatfs.rs @@ -42,10 +42,6 @@ impl BlockStore for FsBlockStore { Ok(()) } - async fn open(&self) -> Result<(), Error> { - Ok(()) - } - async fn contains(&self, cid: &Cid) -> Result { let inner = &*self.inner.read().await; inner.contains(cid).await @@ -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); @@ -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); @@ -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(); diff --git a/src/repo/blockstore/idb.rs b/src/repo/blockstore/idb.rs index adc21b146..139680495 100644 --- a/src/repo/blockstore/idb.rs +++ b/src/repo/blockstore/idb.rs @@ -70,10 +70,6 @@ impl BlockStore for IdbBlockStore { Ok(()) } - async fn open(&self) -> Result<(), Error> { - Ok(()) - } - async fn contains(&self, cid: &Cid) -> Result { let database = self.get_db().clone(); let (tx, rx) = oneshot::channel(); diff --git a/src/repo/blockstore/memory.rs b/src/repo/blockstore/memory.rs index c804aa9ae..5d8a47f6e 100644 --- a/src/repo/blockstore/memory.rs +++ b/src/repo/blockstore/memory.rs @@ -48,10 +48,6 @@ impl BlockStore for MemBlockStore { Ok(()) } - async fn open(&self) -> Result<(), Error> { - Ok(()) - } - async fn contains(&self, cid: &Cid) -> Result { let inner = &*self.inner.read().await; Ok(inner.blocks.contains_key(cid)) @@ -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()); @@ -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(); diff --git a/src/repo/common_tests.rs b/src/repo/common_tests.rs index 571d4cd9b..50a275c4f 100644 --- a/src/repo/common_tests.rs +++ b/src/repo/common_tests.rs @@ -22,7 +22,6 @@ impl DSTestContext { let ds = factory(p); ds.init().await.unwrap(); - ds.open().await.unwrap(); DSTestContext { tempdir, diff --git a/src/repo/datastore/flatfs.rs b/src/repo/datastore/flatfs.rs index 0b682811e..8498aae39 100644 --- a/src/repo/datastore/flatfs.rs +++ b/src/repo/datastore/flatfs.rs @@ -185,10 +185,6 @@ impl DataStore for FsDataStore { Ok(()) } - async fn open(&self) -> Result<(), Error> { - Ok(()) - } - async fn contains(&self, key: &[u8]) -> Result { let _g = self.ds_guard.read().await; Ok(self._contains(key)) @@ -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); diff --git a/src/repo/datastore/idb.rs b/src/repo/datastore/idb.rs index a9d6af532..dab549c64 100644 --- a/src/repo/datastore/idb.rs +++ b/src/repo/datastore/idb.rs @@ -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 { let database = self.get_db().to_owned(); diff --git a/src/repo/datastore/memory.rs b/src/repo/datastore/memory.rs index 6d2ea3f44..123a3e70d 100644 --- a/src/repo/datastore/memory.rs +++ b/src/repo/datastore/memory.rs @@ -318,10 +318,6 @@ impl DataStore for MemDataStore { Ok(()) } - async fn open(&self) -> Result<(), Error> { - Ok(()) - } - async fn contains(&self, key: &[u8]) -> Result { let contains = self.inner.lock().await.contains_key(key); Ok(contains) @@ -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()); diff --git a/src/repo/mod.rs b/src/repo/mod.rs index 832aacae5..bc6d4b6b8 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -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; /// Returns a block from the blockstore. @@ -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; /// Returns the value associated with a key from the datastore. @@ -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.