Skip to content

Commit

Permalink
feat: Implemented basic idb datastore (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 authored Apr 21, 2024
1 parent 0ebb6e6 commit 8c26f65
Show file tree
Hide file tree
Showing 7 changed files with 709 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- refactor: Spawn local task for idb operation instead of using channels. [PR 182](https://github.com/dariusc93/rust-ipfs/pull/182)
- fix: Improve performance by collecting the pinned blocks then compare.
- feat: Add logic to FsDataStore for K/V storage. [PR 183](https://github.com/dariusc93/rust-ipfs/pull/183)
- feat: Implemented basic idb datastore. [PR 185](https://github.com/dariusc93/rust-ipfs/pull/185)

# 0.11.6
- feat: Add RepoInsertPin::provider and RepoInsertPin::providers. [PR 180](https://github.com/dariusc93/rust-ipfs/pull/180)
Expand Down
37 changes: 27 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,39 @@ use libp2p::{

pub(crate) static BITSWAP_ID: AtomicU64 = AtomicU64::new(1);

#[allow(dead_code)]
#[deprecated(note = "Use `StoreageType` instead")]
type StoragePath = StorageType;

#[derive(Default, Debug)]
pub enum StoragePath {
pub enum StorageType {
#[cfg(not(target_arch = "wasm32"))]
Disk(std::path::PathBuf),
#[default]
Memory,
#[cfg(target_arch = "wasm32")]
IndexedDb { namespace: Option<String> },
Custom {
blockstore: Option<Box<dyn BlockStore>>,
datastore: Option<Box<dyn DataStore>>,
lock: Option<Box<dyn Lock>>,
},
}

impl PartialEq for StoragePath {
impl PartialEq for StorageType {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
#[cfg(not(target_arch = "wasm32"))]
(StoragePath::Disk(left_path), StoragePath::Disk(right_path)) => {
(StorageType::Disk(left_path), StorageType::Disk(right_path)) => {
left_path.eq(right_path)
}
(StoragePath::Memory, StoragePath::Memory) => true,
(StoragePath::Custom { .. }, StoragePath::Custom { .. }) => {
#[cfg(target_arch = "wasm32")]
(
StorageType::IndexedDb { namespace: left },
StorageType::IndexedDb { namespace: right },
) => left.eq(right),
(StorageType::Memory, StorageType::Memory) => true,
(StorageType::Custom { .. }, StorageType::Custom { .. }) => {
//Do we really care if they equal?
//TODO: Possibly implement PartialEq/Eq for the traits so we could make sure
// that they do or dont eq each other. For now this will always be true
Expand All @@ -160,7 +171,7 @@ impl PartialEq for StoragePath {
}
}

impl Eq for StoragePath {}
impl Eq for StorageType {}

/// Ipfs node options used to configure the node to be created with [`UninitializedIpfs`].
pub struct IpfsOptions {
Expand All @@ -173,7 +184,7 @@ pub struct IpfsOptions {
///
/// It is **not** recommended to set this to IPFS_PATH without first at least backing up your
/// existing repository.
pub ipfs_path: StoragePath,
pub ipfs_path: StorageType,

/// Nodes used as bootstrap peers.
pub bootstrap: Vec<Multiaddr>,
Expand Down Expand Up @@ -271,7 +282,7 @@ pub enum RepoProvider {
impl Default for IpfsOptions {
fn default() -> Self {
Self {
ipfs_path: StoragePath::Memory,
ipfs_path: StorageType::Memory,
bootstrap: Default::default(),
#[cfg(feature = "beetle_bitswap")]
bitswap_config: Default::default(),
Expand Down Expand Up @@ -552,6 +563,12 @@ impl<C: NetworkBehaviour<ToSwarm = void::Void> + Send> UninitializedIpfs<C> {
])
}

/// Set storage type for the repo.
pub fn set_storage_type(mut self, storage_type: StorageType) -> Self {
self.options.ipfs_path = storage_type;
self
}

/// Adds a listening address
pub fn add_listening_addr(mut self, addr: Multiaddr) -> Self {
if !self.options.listening_addrs.contains(&addr) {
Expand Down Expand Up @@ -744,7 +761,7 @@ impl<C: NetworkBehaviour<ToSwarm = void::Void> + Send> UninitializedIpfs<C> {
#[cfg(not(target_arch = "wasm32"))]
pub fn set_path<P: AsRef<Path>>(mut self, path: P) -> Self {
let path = path.as_ref().to_path_buf();
self.options.ipfs_path = StoragePath::Disk(path);
self.options.ipfs_path = StorageType::Disk(path);
self
}

Expand Down Expand Up @@ -893,7 +910,7 @@ impl<C: NetworkBehaviour<ToSwarm = void::Void> + Send> UninitializedIpfs<C> {
}
None => {
#[cfg(not(target_arch = "wasm32"))]
if let StoragePath::Disk(path) = &options.ipfs_path {
if let StorageType::Disk(path) = &options.ipfs_path {
if !path.is_dir() {
tokio::fs::create_dir_all(path).await?;
}
Expand Down
5 changes: 4 additions & 1 deletion src/repo/blockstore/idb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ pub struct IdbBlockStore {

impl IdbBlockStore {
pub fn new(namespace: Option<String>) -> Self {
let namespace = namespace.unwrap_or_else(|| NAMESPACE.to_string());
let namespace = match namespace {
Some(ns) => format!("{NAMESPACE}-{ns}"),
None => NAMESPACE.to_string()
};

let factory = Factory::new().unwrap();

Expand Down
Loading

0 comments on commit 8c26f65

Please sign in to comment.