diff --git a/rust/noosphere-api/src/client.rs b/rust/noosphere-api/src/client.rs index 3593b4033..af2915a26 100644 --- a/rust/noosphere-api/src/client.rs +++ b/rust/noosphere-api/src/client.rs @@ -10,7 +10,7 @@ use cid::Cid; use libipld_cbor::DagCborCodec; use noosphere_core::authority::{Author, SphereAction, SphereReference}; -use noosphere_storage::encoding::{block_deserialize, block_serialize}; +use noosphere_storage::{block_deserialize, block_serialize}; use reqwest::{header::HeaderMap, Body, StatusCode}; use ucan::{ builder::UcanBuilder, diff --git a/rust/noosphere-api/src/data.rs b/rust/noosphere-api/src/data.rs index 6bac5df01..69b3dca79 100644 --- a/rust/noosphere-api/src/data.rs +++ b/rust/noosphere-api/src/data.rs @@ -6,7 +6,7 @@ use noosphere_core::{ authority::{SphereAction, SphereReference, SPHERE_SEMANTICS}, data::{Bundle, Did}, }; -use noosphere_storage::encoding::{base64_decode, base64_encode}; +use noosphere_storage::{base64_decode, base64_encode}; use serde::{Deserialize, Deserializer, Serialize}; use ucan::{ capability::{Capability, Resource, With}, diff --git a/rust/noosphere-cli/src/native/commands/config.rs b/rust/noosphere-cli/src/native/commands/config.rs index 1ea5e2934..748cbb734 100644 --- a/rust/noosphere-cli/src/native/commands/config.rs +++ b/rust/noosphere-cli/src/native/commands/config.rs @@ -1,7 +1,7 @@ use anyhow::Result; use noosphere::sphere::GATEWAY_URL; use noosphere_core::data::Did; -use noosphere_storage::interface::KeyValueStore; +use noosphere_storage::KeyValueStore; use serde::{Deserialize, Serialize}; use tokio::sync::OnceCell; use url::Url; diff --git a/rust/noosphere-cli/src/native/commands/save.rs b/rust/noosphere-cli/src/native/commands/save.rs index 757e39cbd..1d2ba9827 100644 --- a/rust/noosphere-cli/src/native/commands/save.rs +++ b/rust/noosphere-cli/src/native/commands/save.rs @@ -3,7 +3,7 @@ use cid::Cid; use libipld_cbor::DagCborCodec; use noosphere_core::{authority::Author, data::Header}; use noosphere_fs::SphereFs; -use noosphere_storage::{interface::BlockStore, memory::MemoryStore}; +use noosphere_storage::{BlockStore, MemoryStore}; use crate::native::workspace::{FileReference, Workspace}; diff --git a/rust/noosphere-cli/src/native/commands/serve/authority.rs b/rust/noosphere-cli/src/native/commands/serve/authority.rs index 478ddbf81..0f466a946 100644 --- a/rust/noosphere-cli/src/native/commands/serve/authority.rs +++ b/rust/noosphere-cli/src/native/commands/serve/authority.rs @@ -11,7 +11,7 @@ use axum::{ use libipld_core::cid::Cid; use noosphere::sphere::SphereContext; use noosphere_core::authority::{SphereAction, SphereReference, SPHERE_SEMANTICS}; -use noosphere_storage::native::NativeStore; +use noosphere_storage::NativeStorage; use tokio::sync::Mutex; use ucan::{capability::Capability, chain::ProofChain, crypto::KeyMaterial, store::UcanJwtStore}; @@ -78,7 +78,7 @@ where // Look for the SphereContext let sphere_context = req .extensions() - .get::>>>() + .get::>>>() .ok_or_else(|| { error!("Could not find DidParser in extensions"); StatusCode::INTERNAL_SERVER_ERROR diff --git a/rust/noosphere-cli/src/native/commands/serve/extractor.rs b/rust/noosphere-cli/src/native/commands/serve/extractor.rs index 390353327..bd34019d3 100644 --- a/rust/noosphere-cli/src/native/commands/serve/extractor.rs +++ b/rust/noosphere-cli/src/native/commands/serve/extractor.rs @@ -8,7 +8,7 @@ use axum::{ }; use libipld_cbor::DagCborCodec; use mime_guess::mime; -use noosphere_storage::encoding::{block_deserialize, block_serialize}; +use noosphere_storage::{block_deserialize, block_serialize}; use serde::{de::DeserializeOwned, Serialize}; #[derive(Debug, Clone, Copy, Default)] diff --git a/rust/noosphere-cli/src/native/commands/serve/gateway.rs b/rust/noosphere-cli/src/native/commands/serve/gateway.rs index ae271b675..d3cc0bbf2 100644 --- a/rust/noosphere-cli/src/native/commands/serve/gateway.rs +++ b/rust/noosphere-cli/src/native/commands/serve/gateway.rs @@ -13,7 +13,7 @@ use ucan::crypto::KeyMaterial; use url::Url; use noosphere_api::route::Route as GatewayRoute; -use noosphere_storage::native::NativeStore; +use noosphere_storage::NativeStorage; use crate::native::commands::serve::{ ipfs::start_ipfs_syndication, @@ -30,7 +30,7 @@ pub struct GatewayScope { pub async fn start_gateway( listener: TcpListener, gateway_scope: GatewayScope, - sphere_context: Arc>>, + sphere_context: Arc>>, ipfs_api: Url, cors_origin: Option, ) -> Result<()> @@ -65,7 +65,7 @@ where ]); } - let (syndication_tx, syndication_task) = start_ipfs_syndication::(ipfs_api); + let (syndication_tx, syndication_task) = start_ipfs_syndication::(ipfs_api); let app = Router::new() .route(&GatewayRoute::Did.to_string(), get(did_route::)) diff --git a/rust/noosphere-cli/src/native/commands/serve/ipfs/kubo.rs b/rust/noosphere-cli/src/native/commands/serve/ipfs/kubo.rs index 112994d98..7d7e1adee 100644 --- a/rust/noosphere-cli/src/native/commands/serve/ipfs/kubo.rs +++ b/rust/noosphere-cli/src/native/commands/serve/ipfs/kubo.rs @@ -101,7 +101,7 @@ mod tests { use cid::Cid; use iroh_car::{CarHeader, CarWriter}; use libipld_cbor::DagCborCodec; - use noosphere_storage::encoding::block_serialize; + use noosphere_storage::block_serialize; use serde::{Deserialize, Serialize}; use url::Url; diff --git a/rust/noosphere-cli/src/native/commands/serve/ipfs/syndication.rs b/rust/noosphere-cli/src/native/commands/serve/ipfs/syndication.rs index f4816d8f7..758f6c9fa 100644 --- a/rust/noosphere-cli/src/native/commands/serve/ipfs/syndication.rs +++ b/rust/noosphere-cli/src/native/commands/serve/ipfs/syndication.rs @@ -9,8 +9,10 @@ use noosphere_core::{ view::{Sphere, Timeline}, }; use noosphere_storage::{ - encoding::{block_deserialize, block_serialize}, - interface::{BlockStore, KeyValueStore, Store}, + BlockStore, + block_deserialize, block_serialize, + KeyValueStore, + Storage, }; use serde::{Deserialize, Serialize}; use tokio::{ @@ -37,7 +39,7 @@ use super::KuboClient; pub struct SyndicationJob where K: KeyMaterial + Clone + 'static, - S: Store, + S: Storage, { /// The revision of the _local_ sphere to discover the _counterpart_ sphere /// from; the counterpart sphere's revision will need to be derived using @@ -68,7 +70,7 @@ pub fn start_ipfs_syndication( ) where K: KeyMaterial + Clone + 'static, - S: Store + 'static, + S: Storage + 'static, { let (tx, rx) = unbounded_channel(); @@ -81,7 +83,7 @@ async fn ipfs_syndication_task( ) -> Result<()> where K: KeyMaterial + Clone + 'static, - S: Store, + S: Storage, { debug!("Syndicating sphere revisions to IPFS API at {}", ipfs_api); @@ -110,10 +112,7 @@ where let sphere = Sphere::at(&revision, context.db()); let links = sphere.try_get_links().await?; - let counterpart_revision = links - .require(&counterpart_identity.to_string()) - .await? - .clone(); + let counterpart_revision = *links.require(&counterpart_identity).await?; let fs = context.fs().await?; @@ -163,7 +162,7 @@ where move |cid| { let filter = filter.clone(); let kubo_client = kubo_client.clone(); - let cid = cid.clone(); + let cid = *cid; async move { // The Bloom filter probabilistically tells us if we diff --git a/rust/noosphere-cli/src/native/commands/serve/route/fetch.rs b/rust/noosphere-cli/src/native/commands/serve/route/fetch.rs index 9bcc36791..6159b67b3 100644 --- a/rust/noosphere-cli/src/native/commands/serve/route/fetch.rs +++ b/rust/noosphere-cli/src/native/commands/serve/route/fetch.rs @@ -11,7 +11,7 @@ use noosphere_core::{ data::Bundle, view::Sphere, }; -use noosphere_storage::{db::SphereDb, native::NativeStore}; +use noosphere_storage::{SphereDb, NativeStorage}; use tokio::sync::Mutex; use ucan::{ capability::{Capability, Resource, With}, @@ -26,7 +26,7 @@ pub async fn fetch_route( authority: GatewayAuthority, Query(FetchParameters { since }): Query, Extension(scope): Extension, - Extension(sphere_context): Extension>>>, + Extension(sphere_context): Extension>>>, ) -> Result where K: KeyMaterial + Clone, @@ -61,7 +61,7 @@ where pub async fn generate_fetch_bundle( scope: &GatewayScope, since: Option<&Cid>, - db: &SphereDb, + db: &SphereDb, ) -> Result> { debug!("Resolving latest local sphere version..."); diff --git a/rust/noosphere-cli/src/native/commands/serve/route/identify.rs b/rust/noosphere-cli/src/native/commands/serve/route/identify.rs index de182be36..03b79deba 100644 --- a/rust/noosphere-cli/src/native/commands/serve/route/identify.rs +++ b/rust/noosphere-cli/src/native/commands/serve/route/identify.rs @@ -4,7 +4,7 @@ use axum::{http::StatusCode, response::IntoResponse, Extension, Json}; use noosphere::sphere::SphereContext; use noosphere_api::data::IdentifyResponse; use noosphere_core::authority::{SphereAction, SphereReference}; -use noosphere_storage::native::NativeStore; +use noosphere_storage::NativeStorage; use tokio::sync::Mutex; use ucan::{ capability::{Capability, Resource, With}, @@ -16,7 +16,7 @@ use crate::native::commands::serve::{authority::GatewayAuthority, gateway::Gatew pub async fn identify_route( authority: GatewayAuthority, Extension(scope): Extension, - Extension(sphere_context): Extension>>>, + Extension(sphere_context): Extension>>>, ) -> Result { debug!("Invoking identify route..."); diff --git a/rust/noosphere-cli/src/native/commands/serve/route/push.rs b/rust/noosphere-cli/src/native/commands/serve/route/push.rs index 59df27cc5..646f09f40 100644 --- a/rust/noosphere-cli/src/native/commands/serve/route/push.rs +++ b/rust/noosphere-cli/src/native/commands/serve/route/push.rs @@ -7,12 +7,13 @@ use axum::{extract::ContentLengthLimit, http::StatusCode, Extension}; use cid::Cid; use noosphere::sphere::SphereContext; use noosphere_api::data::{PushBody, PushResponse}; -use noosphere_core::authority::{Authorization, SphereAction, SphereReference}; -use noosphere_core::data::Bundle; -use noosphere_core::view::{Sphere, SphereMutation, Timeline}; -use noosphere_storage::{db::SphereDb, native::NativeStore}; -use tokio::sync::mpsc::UnboundedSender; -use tokio::sync::Mutex; +use noosphere_core::{ + authority::{Authorization, SphereAction, SphereReference}, + data::Bundle, + view::{Sphere, SphereMutation, Timeline}, +}; +use noosphere_storage::{SphereDb, NativeStorage}; +use tokio::sync::{mpsc::UnboundedSender, Mutex}; use ucan::capability::{Capability, Resource, With}; use ucan::crypto::KeyMaterial; @@ -24,9 +25,9 @@ use crate::native::commands::serve::{ pub async fn push_route( authority: GatewayAuthority, ContentLengthLimit(Cbor(push_body)): ContentLengthLimit, { 1024 * 5000 }>, - Extension(sphere_context_mutex): Extension>>>, + Extension(sphere_context_mutex): Extension>>>, Extension(scope): Extension, - Extension(syndication_tx): Extension>>, + Extension(syndication_tx): Extension>>, ) -> Result, StatusCode> where K: KeyMaterial + Clone, @@ -124,7 +125,7 @@ where // an explicit publish action. Move this to the publish handler when we // have added it to the gateway. if let Err(error) = syndication_tx.send(SyndicationJob { - revision: new_gateway_tip.clone(), + revision: new_gateway_tip, context: sphere_context_mutex.clone(), }) { warn!("Failed to queue IPFS syndication job: {}", error); @@ -141,7 +142,7 @@ async fn update_gateway_sphere( scope: &GatewayScope, key: &K, authority: &Authorization, - db: &mut SphereDb, + db: &mut SphereDb, ) -> Result<(Cid, Bundle)> where K: KeyMaterial + Send, @@ -172,7 +173,7 @@ where async fn incorporate_lineage( scope: &GatewayScope, - db: &mut SphereDb, + db: &mut SphereDb, push_body: &PushBody, ) -> Result<()> { push_body.blocks.load_into(db).await?; diff --git a/rust/noosphere-cli/src/native/commands/status.rs b/rust/noosphere-cli/src/native/commands/status.rs index 0a9f418b9..857a5dbc0 100644 --- a/rust/noosphere-cli/src/native/commands/status.rs +++ b/rust/noosphere-cli/src/native/commands/status.rs @@ -3,7 +3,7 @@ use std::collections::BTreeMap; use crate::native::Workspace; use anyhow::Result; use noosphere_core::data::ContentType; -use noosphere_storage::memory::MemoryStore; +use noosphere_storage::MemoryStore; pub fn status_section( name: &str, diff --git a/rust/noosphere-cli/src/native/commands/sync.rs b/rust/noosphere-cli/src/native/commands/sync.rs index a56d573c9..e267afdd2 100644 --- a/rust/noosphere-cli/src/native/commands/sync.rs +++ b/rust/noosphere-cli/src/native/commands/sync.rs @@ -1,6 +1,6 @@ use crate::native::{commands::serve::tracing::initialize_tracing, workspace::Workspace}; use anyhow::{anyhow, Result}; -use noosphere_storage::memory::MemoryStore; +use noosphere_storage::MemoryStore; pub async fn sync(workspace: &Workspace) -> Result<()> { initialize_tracing(); diff --git a/rust/noosphere-cli/src/native/workspace.rs b/rust/noosphere-cli/src/native/workspace.rs index 48041d244..b32344d9f 100644 --- a/rust/noosphere-cli/src/native/workspace.rs +++ b/rust/noosphere-cli/src/native/workspace.rs @@ -9,9 +9,7 @@ use noosphere_core::{ }; use noosphere_fs::SphereFs; use noosphere_storage::{ - db::SphereDb, - interface::{BlockStore, KeyValueStore, Store}, - native::NativeStore, + BlockStore, SphereDb, KeyValueStore, NativeStorage, Store, }; use pathdiff::diff_paths; use std::{ @@ -40,7 +38,7 @@ const SPHERE_DIRECTORY: &str = ".sphere"; const NOOSPHERE_DIRECTORY: &str = ".noosphere"; // const STORAGE_DIRECTORY: &str = "storage"; -pub type CliSphereContext = SphereContext; +pub type CliSphereContext = SphereContext; /// A delta manifest of changes to the local content space #[derive(Default)] @@ -117,7 +115,7 @@ impl Workspace { /// Get an owned referenced to the [SphereDb] that backs the local sphere. /// Note that this will initialize the [SphereContext] if it has not been /// already. - pub async fn db(&self) -> Result> { + pub async fn db(&self) -> Result> { let context = self.sphere_context().await?; let context = context.lock().await; Ok(context.db().clone()) diff --git a/rust/noosphere-cli/tests/gateway.rs b/rust/noosphere-cli/tests/gateway.rs index 756d55da2..b353827b7 100644 --- a/rust/noosphere-cli/tests/gateway.rs +++ b/rust/noosphere-cli/tests/gateway.rs @@ -2,7 +2,7 @@ use anyhow::anyhow; use noosphere::key::KeyStorage; -use noosphere_storage::interface::BlockStore; +use noosphere_storage::BlockStore; use std::net::TcpListener; use tokio::io::AsyncReadExt; use tokio_stream::StreamExt; diff --git a/rust/noosphere-collections/src/hamt/hamt.rs b/rust/noosphere-collections/src/hamt/hamt.rs index 7b7fd780f..9f4ad8696 100644 --- a/rust/noosphere-collections/src/hamt/hamt.rs +++ b/rust/noosphere-collections/src/hamt/hamt.rs @@ -6,7 +6,7 @@ use anyhow::Result; use libipld_cbor::DagCborCodec; -use noosphere_storage::interface::BlockStore; +use noosphere_storage::BlockStore; use std::borrow::Borrow; use std::marker::PhantomData; use std::pin::Pin; @@ -43,7 +43,7 @@ impl TargetConditionalSendSync for S {} /// /// ``` /// use noosphere_collections::hamt::Hamt; -/// use noosphere_storage::memory::MemoryStore; +/// use noosphere_storage::MemoryStore; /// /// async_std::task::block_on(async { /// let store = MemoryStore::default(); @@ -155,7 +155,7 @@ where /// /// ``` /// use noosphere_collections::hamt::Hamt; - /// use noosphere_storage::memory::MemoryStore; + /// use noosphere_storage::MemoryStore; /// /// async_std::task::block_on(async { /// let store = MemoryStore::default(); @@ -185,7 +185,7 @@ where /// /// ``` /// use noosphere_collections::hamt::Hamt; - /// use noosphere_storage::memory::MemoryStore; + /// use noosphere_storage::MemoryStore; /// /// async_std::task::block_on(async { /// let store = MemoryStore::default(); @@ -223,7 +223,7 @@ where /// /// ``` /// use noosphere_collections::hamt::Hamt; - /// use noosphere_storage::memory::MemoryStore; + /// use noosphere_storage::MemoryStore; /// /// async_std::task::block_on(async { /// let store = MemoryStore::default(); @@ -261,7 +261,7 @@ where /// /// ``` /// use noosphere_collections::hamt::Hamt; - /// use noosphere_storage::memory::MemoryStore; + /// use noosphere_storage::MemoryStore; /// /// async_std::task::block_on(async { /// let store = MemoryStore::default(); @@ -296,7 +296,7 @@ where /// /// ``` /// use noosphere_collections::hamt::Hamt; - /// use noosphere_storage::memory::MemoryStore; + /// use noosphere_storage::MemoryStore; /// /// async_std::task::block_on(async { /// let store = MemoryStore::default(); @@ -336,7 +336,7 @@ where /// /// ``` /// use noosphere_collections::hamt::Hamt; - /// use noosphere_storage::memory::MemoryStore; + /// use noosphere_storage::MemoryStore; /// /// async_std::task::block_on(async { /// let store = MemoryStore::default(); diff --git a/rust/noosphere-collections/src/hamt/node.rs b/rust/noosphere-collections/src/hamt/node.rs index bfcb4b343..46634d32f 100644 --- a/rust/noosphere-collections/src/hamt/node.rs +++ b/rust/noosphere-collections/src/hamt/node.rs @@ -8,7 +8,7 @@ use async_recursion::async_recursion; use async_stream::try_stream; use libipld_cbor::DagCborCodec; -use noosphere_storage::interface::BlockStore; +use noosphere_storage::BlockStore; use std::borrow::Borrow; use std::fmt::Debug; use std::marker::PhantomData; diff --git a/rust/noosphere-collections/src/hamt/test.rs b/rust/noosphere-collections/src/hamt/test.rs index cfbd3ed34..9aaddda62 100644 --- a/rust/noosphere-collections/src/hamt/test.rs +++ b/rust/noosphere-collections/src/hamt/test.rs @@ -7,8 +7,7 @@ use crate::hamt::Hamt; use forest_hash_utils::BytesKey; use serde_bytes::ByteBuf; -use noosphere_storage::memory::MemoryStore; -use noosphere_storage::tracking::{StoreStats, TrackingStore}; +use noosphere_storage::{MemoryStore, StoreStats, TrackingStore}; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::wasm_bindgen_test; diff --git a/rust/noosphere-core/src/authority/author.rs b/rust/noosphere-core/src/authority/author.rs index fa66092cb..90f32281a 100644 --- a/rust/noosphere-core/src/authority/author.rs +++ b/rust/noosphere-core/src/authority/author.rs @@ -6,7 +6,7 @@ use crate::{ data::Did, }; use anyhow::{anyhow, Result}; -use noosphere_storage::{db::SphereDb, interface::Store}; +use noosphere_storage::{SphereDb, Storage}; use ucan::{ capability::{Capability, Resource, With}, chain::ProofChain, @@ -66,7 +66,7 @@ where } /// Determine the level of access that the author has to a given sphere - pub async fn access_to( + pub async fn access_to( &self, sphere_identity: &Did, db: &SphereDb, @@ -107,7 +107,7 @@ where #[cfg(test)] mod tests { - use noosphere_storage::{db::SphereDb, memory::MemoryStorageProvider}; + use noosphere_storage::{MemoryStorage, SphereDb}; use ucan::crypto::KeyMaterial; use crate::{authority::generate_ed25519_key, data::Did, view::Sphere}; @@ -124,9 +124,7 @@ mod tests { #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] async fn it_gives_read_only_access_when_there_is_no_authorization() { let author = Author::anonymous(); - let mut db = SphereDb::new(&MemoryStorageProvider::default()) - .await - .unwrap(); + let mut db = SphereDb::new(&MemoryStorage::default()).await.unwrap(); let (sphere, _, _) = Sphere::try_generate("did:key:foo", &mut db).await.unwrap(); @@ -143,9 +141,7 @@ mod tests { async fn it_gives_read_write_access_if_the_key_is_authorized() { let owner_key = generate_ed25519_key(); let owner_did = Did(owner_key.get_did().await.unwrap()); - let mut db = SphereDb::new(&MemoryStorageProvider::default()) - .await - .unwrap(); + let mut db = SphereDb::new(&MemoryStorage::default()).await.unwrap(); let (sphere, authorization, _) = Sphere::try_generate(&owner_did, &mut db).await.unwrap(); let author = Author { diff --git a/rust/noosphere-core/src/authority/authorization.rs b/rust/noosphere-core/src/authority/authorization.rs index 65c786e39..d259e591a 100644 --- a/rust/noosphere-core/src/authority/authorization.rs +++ b/rust/noosphere-core/src/authority/authorization.rs @@ -3,7 +3,7 @@ use std::{convert::TryFrom, fmt::Display, str::FromStr}; use anyhow::{anyhow, Result}; use cid::Cid; use libipld_core::{ipld::Ipld, raw::RawCodec}; -use noosphere_storage::encoding::block_encode; +use noosphere_storage::block_encode; use ucan::{store::UcanJwtStore, Ucan}; #[cfg(doc)] diff --git a/rust/noosphere-core/src/authority/verification.rs b/rust/noosphere-core/src/authority/verification.rs index f7df117d1..2b97396c8 100644 --- a/rust/noosphere-core/src/authority/verification.rs +++ b/rust/noosphere-core/src/authority/verification.rs @@ -13,18 +13,13 @@ use ucan::{ use crate::data::{ContentType, Header, MemoIpld, SphereIpld}; -use noosphere_storage::{ - db::SphereDb, - encoding::base64_decode, - interface::{BlockStore, Store}, - ucan::UcanStore, -}; +use noosphere_storage::{base64_decode, BlockStore, SphereDb, Storage, UcanStore}; use crate::authority::SPHERE_SEMANTICS; use super::{SphereAction, SphereReference}; -pub async fn verify_sphere_cid( +pub async fn verify_sphere_cid( cid: &Cid, store: &SphereDb, did_parser: &mut DidParser, diff --git a/rust/noosphere-core/src/data/authority.rs b/rust/noosphere-core/src/data/authority.rs index f697ee607..87e09be13 100644 --- a/rust/noosphere-core/src/data/authority.rs +++ b/rust/noosphere-core/src/data/authority.rs @@ -4,11 +4,7 @@ use libipld_cbor::DagCborCodec; use std::hash::Hash; use ucan::{crypto::KeyMaterial, store::UcanJwtStore, Ucan}; -use noosphere_storage::{ - encoding::{base64_decode, base64_encode}, - interface::BlockStore, - ucan::UcanStore, -}; +use noosphere_storage::{base64_decode, base64_encode, BlockStore, UcanStore}; use serde::{Deserialize, Serialize}; use crate::data::{CidKey, VersionedMapIpld}; @@ -108,7 +104,7 @@ pub type RevokedIpld = VersionedMapIpld; #[cfg(test)] mod tests { - use noosphere_storage::{memory::MemoryStore, ucan::UcanStore}; + use noosphere_storage::{MemoryStore, UcanStore}; use ucan::{builder::UcanBuilder, crypto::KeyMaterial, store::UcanJwtStore}; use crate::authority::generate_ed25519_key; diff --git a/rust/noosphere-core/src/data/body_chunk.rs b/rust/noosphere-core/src/data/body_chunk.rs index 22aa5eb81..deb7d688a 100644 --- a/rust/noosphere-core/src/data/body_chunk.rs +++ b/rust/noosphere-core/src/data/body_chunk.rs @@ -4,7 +4,7 @@ use fastcdc::FastCDC; use libipld_cbor::DagCborCodec; use serde::{Deserialize, Serialize}; -use noosphere_storage::interface::BlockStore; +use noosphere_storage::BlockStore; pub const BODY_CHUNK_MAX_SIZE: usize = 1024 * 64 * 8; // ~.5mb/chunk worst case diff --git a/rust/noosphere-core/src/data/bundle.rs b/rust/noosphere-core/src/data/bundle.rs index 8cc89ceb0..2ebfa180e 100644 --- a/rust/noosphere-core/src/data/bundle.rs +++ b/rust/noosphere-core/src/data/bundle.rs @@ -7,10 +7,7 @@ use cid::Cid; use futures::{pin_mut, StreamExt}; use libipld_cbor::DagCborCodec; use libipld_core::{raw::RawCodec, serde::to_ipld}; -use noosphere_storage::{ - encoding::{block_deserialize, block_serialize}, - interface::BlockStore, -}; +use noosphere_storage::{block_deserialize, block_serialize, BlockStore}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use crate::{ @@ -388,7 +385,7 @@ impl TryBundle for AuthorityIpld { mod tests { use libipld_cbor::DagCborCodec; use libipld_core::{ipld::Ipld, raw::RawCodec}; - use noosphere_storage::{interface::BlockStore, memory::MemoryStore}; + use noosphere_storage::{BlockStore, MemoryStore}; use serde_bytes::Bytes; use ucan::crypto::KeyMaterial; diff --git a/rust/noosphere-core/src/data/memo.rs b/rust/noosphere-core/src/data/memo.rs index 89318f35e..0785a5409 100644 --- a/rust/noosphere-core/src/data/memo.rs +++ b/rust/noosphere-core/src/data/memo.rs @@ -8,10 +8,7 @@ use ucan::crypto::KeyMaterial; use crate::{authority::Authorization, data::Header}; -use noosphere_storage::{ - encoding::base64_encode, - interface::{BlockStore, BlockStoreSend}, -}; +use noosphere_storage::{base64_encode, BlockStore, BlockStoreSend}; use super::ContentType; @@ -234,9 +231,7 @@ mod test { use crate::data::MemoIpld; use noosphere_storage::{ - encoding::{block_deserialize, block_encode, block_serialize}, - interface::BlockStore, - memory::MemoryStore, + block_deserialize, block_encode, block_serialize, BlockStore, MemoryStore, }; #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] diff --git a/rust/noosphere-core/src/data/sphere.rs b/rust/noosphere-core/src/data/sphere.rs index b7d265224..5c784d204 100644 --- a/rust/noosphere-core/src/data/sphere.rs +++ b/rust/noosphere-core/src/data/sphere.rs @@ -48,7 +48,7 @@ mod tests { data::{ContentType, Did, Header, MemoIpld, SphereIpld}, }; - use noosphere_storage::{db::SphereDb, interface::BlockStore, memory::MemoryStorageProvider}; + use noosphere_storage::{BlockStore, MemoryStorage, SphereDb}; fn generate_credential() -> Ed25519KeyMaterial { let private_key = Ed25519PrivateKey::new(rand::thread_rng()); @@ -62,9 +62,7 @@ mod tests { let identity_credential = generate_credential(); let identity_did = Did(identity_credential.get_did().await.unwrap()); - let mut store = SphereDb::new(&MemoryStorageProvider::default()) - .await - .unwrap(); + let mut store = SphereDb::new(&MemoryStorage::default()).await.unwrap(); let sphere = SphereIpld { identity: identity_did.clone(), @@ -141,9 +139,7 @@ mod tests { let identity_did = Did(identity_credential.get_did().await.unwrap()); let authorized_did = Did(authorized_credential.get_did().await.unwrap()); - let mut store = SphereDb::new(&MemoryStorageProvider::default()) - .await - .unwrap(); + let mut store = SphereDb::new(&MemoryStorage::default()).await.unwrap(); let sphere = SphereIpld { identity: identity_did.clone(), diff --git a/rust/noosphere-core/src/data/strings.rs b/rust/noosphere-core/src/data/strings.rs index 79bd7e124..125f9d9ea 100644 --- a/rust/noosphere-core/src/data/strings.rs +++ b/rust/noosphere-core/src/data/strings.rs @@ -102,7 +102,7 @@ pub struct Mnemonic(pub String); #[cfg(test)] mod tests { use libipld_cbor::DagCborCodec; - use noosphere_storage::encoding::{block_deserialize, block_serialize}; + use noosphere_storage::{block_deserialize, block_serialize}; use serde::{Deserialize, Serialize}; use crate::data::Did; diff --git a/rust/noosphere-core/src/data/versioned_map.rs b/rust/noosphere-core/src/data/versioned_map.rs index f341a5b7a..9f74dd94e 100644 --- a/rust/noosphere-core/src/data/versioned_map.rs +++ b/rust/noosphere-core/src/data/versioned_map.rs @@ -6,7 +6,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; use std::{fmt::Display, hash::Hash, marker::PhantomData}; use noosphere_collections::hamt::{Hamt, Hash as HamtHash, Sha256}; -use noosphere_storage::interface::BlockStore; +use noosphere_storage::BlockStore; use super::ChangelogIpld; diff --git a/rust/noosphere-core/src/view/authority.rs b/rust/noosphere-core/src/view/authority.rs index e701b1c95..991d5610c 100644 --- a/rust/noosphere-core/src/view/authority.rs +++ b/rust/noosphere-core/src/view/authority.rs @@ -1,7 +1,7 @@ use anyhow::Result; use cid::Cid; use libipld_cbor::DagCborCodec; -use noosphere_storage::interface::BlockStore; +use noosphere_storage::BlockStore; use crate::{ data::AuthorityIpld, diff --git a/rust/noosphere-core/src/view/mutation.rs b/rust/noosphere-core/src/view/mutation.rs index dd27a7f67..cf988c9dd 100644 --- a/rust/noosphere-core/src/view/mutation.rs +++ b/rust/noosphere-core/src/view/mutation.rs @@ -11,7 +11,7 @@ use crate::{ }, }; -use noosphere_storage::interface::BlockStore; +use noosphere_storage::BlockStore; #[derive(Debug)] pub struct SphereRevision { diff --git a/rust/noosphere-core/src/view/sphere.rs b/rust/noosphere-core/src/view/sphere.rs index 23746d3b1..880a6d207 100644 --- a/rust/noosphere-core/src/view/sphere.rs +++ b/rust/noosphere-core/src/view/sphere.rs @@ -22,7 +22,7 @@ use crate::{ view::{Links, SphereMutation, SphereRevision, Timeline}, }; -use noosphere_storage::{interface::BlockStore, ucan::UcanStore}; +use noosphere_storage::{BlockStore, UcanStore}; use super::{AllowedUcans, Authority, Names, RevokedUcans}; @@ -571,11 +571,7 @@ mod tests { view::{Sphere, SphereMutation, Timeline, SPHERE_LIFETIME}, }; - use noosphere_storage::{ - interface::{BlockStore, Store}, - memory::MemoryStore, - ucan::UcanStore, - }; + use noosphere_storage::{BlockStore, MemoryStore, Store, UcanStore}; #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] diff --git a/rust/noosphere-core/src/view/timeline.rs b/rust/noosphere-core/src/view/timeline.rs index 2e711ad02..43ac8b9ef 100644 --- a/rust/noosphere-core/src/view/timeline.rs +++ b/rust/noosphere-core/src/view/timeline.rs @@ -7,7 +7,7 @@ use libipld_cbor::DagCborCodec; use crate::data::MemoIpld; -use noosphere_storage::interface::BlockStore; +use noosphere_storage::BlockStore; // Assumptions: // - network operations are _always_ mediated by a "remote" agent (no client-to-client syncing) diff --git a/rust/noosphere-core/src/view/versioned_map.rs b/rust/noosphere-core/src/view/versioned_map.rs index 6d28bc929..ed664166b 100644 --- a/rust/noosphere-core/src/view/versioned_map.rs +++ b/rust/noosphere-core/src/view/versioned_map.rs @@ -12,7 +12,7 @@ use crate::data::{ }; use noosphere_collections::hamt::Hamt; -use noosphere_storage::interface::BlockStore; +use noosphere_storage::BlockStore; use super::VersionedMapMutation; diff --git a/rust/noosphere-fs/src/decoder.rs b/rust/noosphere-fs/src/decoder.rs index c9a7fd6ed..c6650edd6 100644 --- a/rust/noosphere-fs/src/decoder.rs +++ b/rust/noosphere-fs/src/decoder.rs @@ -3,7 +3,7 @@ use bytes::Bytes; use cid::Cid; use libipld_cbor::DagCborCodec; use noosphere_core::data::BodyChunkIpld; -use noosphere_storage::interface::BlockStore; +use noosphere_storage::BlockStore; use tokio_stream::Stream; /// Helper to easily decode a linked list of `BodyChunkIpld` as a byte stream diff --git a/rust/noosphere-fs/src/fs.rs b/rust/noosphere-fs/src/fs.rs index 9cc7a424b..5f2ca966c 100644 --- a/rust/noosphere-fs/src/fs.rs +++ b/rust/noosphere-fs/src/fs.rs @@ -5,10 +5,7 @@ use noosphere_core::{ data::{BodyChunkIpld, ContentType, Did, Header, MemoIpld}, view::{Sphere, SphereMutation}, }; -use noosphere_storage::{ - db::SphereDb, - interface::{BlockStore, Store}, -}; +use noosphere_storage::{BlockStore, SphereDb, Storage}; use once_cell::sync::OnceCell; use std::str::FromStr; use tokio_util::io::StreamReader; @@ -28,7 +25,7 @@ use crate::{BodyChunkDecoder, SphereFile}; /// interface for operation on sphere content. pub struct SphereFs where - S: Store, + S: Storage, K: KeyMaterial + Clone + 'static, { author: Author, @@ -42,7 +39,7 @@ where impl SphereFs where - S: Store, + S: Storage, K: KeyMaterial + Clone + 'static, { /// Get the DID identity of the sphere that this FS view is reading from and @@ -58,7 +55,7 @@ where } /// Get a data view into the sphere at the current revision - pub fn to_sphere(&self) -> Sphere { + pub fn to_sphere(&self) -> Sphere { Sphere::at(self.revision(), &self.db.to_block_store()) } @@ -310,8 +307,8 @@ pub mod tests { data::{ContentType, Header}, view::Sphere, }; - use noosphere_storage::db::SphereDb; - use noosphere_storage::memory::MemoryStorageProvider; + use noosphere_storage::MemoryStorage; + use noosphere_storage::SphereDb; use tokio::io::AsyncReadExt; use ucan::crypto::KeyMaterial; @@ -325,7 +322,7 @@ pub mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] async fn it_can_unlink_slugs_from_the_content_space() { - let storage_provider = MemoryStorageProvider::default(); + let storage_provider = MemoryStorage::default(); let mut db = SphereDb::new(&storage_provider).await.unwrap(); let owner_key = generate_ed25519_key(); @@ -369,7 +366,7 @@ pub mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] async fn it_does_not_allow_writes_when_an_author_has_read_only_access() { - let storage_provider = MemoryStorageProvider::default(); + let storage_provider = MemoryStorage::default(); let mut db = SphereDb::new(&storage_provider).await.unwrap(); let owner_key = generate_ed25519_key(); @@ -406,7 +403,7 @@ pub mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] async fn it_can_write_a_file_and_read_it_back() { - let storage_provider = MemoryStorageProvider::default(); + let storage_provider = MemoryStorage::default(); let mut db = SphereDb::new(&storage_provider).await.unwrap(); let owner_key = generate_ed25519_key(); @@ -457,7 +454,7 @@ pub mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] async fn it_can_overwrite_a_file_with_new_contents_and_preserve_history() { - let storage_provider = MemoryStorageProvider::default(); + let storage_provider = MemoryStorage::default(); let mut db = SphereDb::new(&storage_provider).await.unwrap(); let owner_key = generate_ed25519_key(); @@ -535,7 +532,7 @@ pub mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] async fn it_throws_an_error_when_saving_without_changes() { - let storage_provider = MemoryStorageProvider::default(); + let storage_provider = MemoryStorage::default(); let mut db = SphereDb::new(&storage_provider).await.unwrap(); let owner_key = generate_ed25519_key(); @@ -566,7 +563,7 @@ pub mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] async fn it_throws_an_error_when_saving_with_empty_mutation_and_empty_headers() { - let storage_provider = MemoryStorageProvider::default(); + let storage_provider = MemoryStorage::default(); let mut db = SphereDb::new(&storage_provider).await.unwrap(); let owner_key = generate_ed25519_key(); diff --git a/rust/noosphere-into/examples/notes-to-html/implementation.rs b/rust/noosphere-into/examples/notes-to-html/implementation.rs index bead2444a..5519d4292 100644 --- a/rust/noosphere-into/examples/notes-to-html/implementation.rs +++ b/rust/noosphere-into/examples/notes-to-html/implementation.rs @@ -12,11 +12,11 @@ use noosphere_core::{ data::{ContentType, Header}, view::Sphere, }; -use noosphere_storage::{db::SphereDb, memory::MemoryStorageProvider}; +use noosphere_storage::{SphereDb, MemoryStorage}; use ucan::crypto::KeyMaterial; pub async fn main() -> Result<()> { - let storage_provider = MemoryStorageProvider::default(); + let storage_provider = MemoryStorage::default(); let mut db = SphereDb::new(&storage_provider).await.unwrap(); let owner_key = generate_ed25519_key(); diff --git a/rust/noosphere-into/src/html/into.rs b/rust/noosphere-into/src/html/into.rs index 35f2da011..c89c7305a 100644 --- a/rust/noosphere-into/src/html/into.rs +++ b/rust/noosphere-into/src/html/into.rs @@ -4,7 +4,7 @@ use anyhow::{anyhow, Result}; use cid::Cid; use noosphere_core::{authority::Author, data::Did, view::Sphere}; use noosphere_fs::SphereFs; -use noosphere_storage::{db::SphereDb, interface::Store}; +use noosphere_storage::{SphereDb, Storage}; use tokio::sync::Mutex; use tokio_stream::StreamExt; @@ -23,7 +23,7 @@ pub async fn sphere_into_html( write_target: &W, ) -> Result<()> where - S: Store + 'static, + S: Storage + 'static, W: WriteTarget + 'static, { let mut next_sphere_cid = match db.get_version(sphere_identity).await? { @@ -171,7 +171,7 @@ pub mod tests { view::Sphere, }; use noosphere_fs::SphereFs; - use noosphere_storage::{db::SphereDb, memory::MemoryStorageProvider}; + use noosphere_storage::{MemoryStorage, SphereDb}; use ucan::crypto::KeyMaterial; #[cfg(target_arch = "wasm32")] @@ -186,10 +186,7 @@ pub mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] async fn it_writes_a_file_from_the_sphere_to_the_target_as_html() { - // let mut sphere_store = MemoryStore::default(); - // let mut block_store = MemoryStore::default(); - - let storage_provider = MemoryStorageProvider::default(); + let storage_provider = MemoryStorage::default(); let mut db = SphereDb::new(&storage_provider).await.unwrap(); let owner_key = generate_ed25519_key(); @@ -274,7 +271,7 @@ pub mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] async fn it_symlinks_a_file_slug_to_the_latest_file_version() { - let storage_provider = MemoryStorageProvider::default(); + let storage_provider = MemoryStorage::default(); let mut db = SphereDb::new(&storage_provider).await.unwrap(); let owner_key = generate_ed25519_key(); diff --git a/rust/noosphere-into/src/html/transform/sphere.rs b/rust/noosphere-into/src/html/transform/sphere.rs index 3b8ab1885..c8e1de519 100644 --- a/rust/noosphere-into/src/html/transform/sphere.rs +++ b/rust/noosphere-into/src/html/transform/sphere.rs @@ -3,7 +3,7 @@ use std::io::Cursor; use anyhow::Result; use noosphere_core::{data::MemoIpld, view::Links}; use noosphere_fs::SphereFs; -use noosphere_storage::interface::Store; +use noosphere_storage::Storage; use tokio::io::AsyncRead; use tokio_stream::StreamExt; use ucan::crypto::KeyMaterial; @@ -15,7 +15,7 @@ use super::TranscludeToHtmlTransformer; /// Transforms a sphere into HTML pub struct SphereToHtmlTransformer<'a, S, K> where - S: Store, + S: Storage, K: KeyMaterial + Clone + 'static, { fs: &'a SphereFs, @@ -24,7 +24,7 @@ where impl<'a, S, K> SphereToHtmlTransformer<'a, S, K> where - S: Store, + S: Storage, K: KeyMaterial + Clone + 'static, { pub fn new(fs: &'a SphereFs) -> Self { @@ -44,7 +44,7 @@ where Ok(Some(Cursor::new(self.html_text(memo, links).await?))) } - async fn html_text(&'a self, memo: MemoIpld, links: Links) -> Result { + async fn html_text(&'a self, memo: MemoIpld, links: Links) -> Result { // Cheating here because the link stream isn't Send + Sync; should fix this let mut link_stream = links.stream().await?; let (html_prefix, html_suffix) = html_document_envelope(memo); diff --git a/rust/noosphere-into/src/html/transform/subtext.rs b/rust/noosphere-into/src/html/transform/subtext.rs index 709bb65ec..7bb65d948 100644 --- a/rust/noosphere-into/src/html/transform/subtext.rs +++ b/rust/noosphere-into/src/html/transform/subtext.rs @@ -6,7 +6,7 @@ use bytes::Bytes; use futures::Stream; use horrorshow::{html, Raw}; use noosphere_fs::{SphereFile, SphereFs}; -use noosphere_storage::interface::Store; +use noosphere_storage::Storage; use subtext::{block::Block, primitive::Entity, Slashlink}; use tokio::io::AsyncRead; use tokio_util::io::StreamReader; @@ -19,7 +19,7 @@ use super::transclude::TranscludeToHtmlTransformer; /// Transforms Subtext files from a sphere into HTML pub struct SubtextToHtmlTransformer<'a, S, K> where - S: Store, + S: Storage, K: KeyMaterial + Clone + 'static, { fs: &'a SphereFs, @@ -28,7 +28,7 @@ where impl<'a, S, K> SubtextToHtmlTransformer<'a, S, K> where - S: Store, + S: Storage, K: KeyMaterial + Clone + 'static, { pub fn new(fs: &'a SphereFs) -> Self { diff --git a/rust/noosphere-into/src/html/transform/transclude.rs b/rust/noosphere-into/src/html/transform/transclude.rs index efa011f33..eaac42345 100644 --- a/rust/noosphere-into/src/html/transform/transclude.rs +++ b/rust/noosphere-into/src/html/transform/transclude.rs @@ -1,7 +1,7 @@ use anyhow::Result; use horrorshow::html; use noosphere_fs::SphereFs; -use noosphere_storage::interface::Store; +use noosphere_storage::Storage; use ucan::crypto::KeyMaterial; use crate::transclude::{Transclude, Transcluder}; @@ -9,7 +9,7 @@ use crate::transclude::{Transclude, Transcluder}; /// Transforms a transclude into HTML pub struct TranscludeToHtmlTransformer<'a, S, K> where - S: Store, + S: Storage, K: KeyMaterial + Clone + 'static, { transcluder: Transcluder<'a, S, K>, @@ -17,7 +17,7 @@ where impl<'a, S, K> TranscludeToHtmlTransformer<'a, S, K> where - S: Store, + S: Storage, K: KeyMaterial + Clone + 'static, { pub fn new(fs: &'a SphereFs) -> Self { diff --git a/rust/noosphere-into/src/transclude.rs b/rust/noosphere-into/src/transclude.rs index ead8b7373..848a486ee 100644 --- a/rust/noosphere-into/src/transclude.rs +++ b/rust/noosphere-into/src/transclude.rs @@ -1,7 +1,7 @@ use anyhow::Result; use noosphere_core::data::Header; use noosphere_fs::SphereFs; -use noosphere_storage::interface::Store; +use noosphere_storage::Storage; use subtext::{block::Block, primitive::Entity}; use tokio_stream::StreamExt; use ucan::crypto::KeyMaterial; @@ -29,7 +29,7 @@ pub enum Transclude { /// for the content that the slug refers to. pub struct Transcluder<'a, S, K> where - S: Store, + S: Storage, K: KeyMaterial + Clone + 'static, { fs: &'a SphereFs, @@ -37,7 +37,7 @@ where impl<'a, S, K> Transcluder<'a, S, K> where - S: Store, + S: Storage, K: KeyMaterial + Clone + 'static, { pub fn new(fs: &'a SphereFs) -> Self { diff --git a/rust/noosphere-ns/src/bin/orb-ns/runner/runner.rs b/rust/noosphere-ns/src/bin/orb-ns/runner/runner.rs index 535bb8b1d..49b007309 100644 --- a/rust/noosphere-ns/src/bin/orb-ns/runner/runner.rs +++ b/rust/noosphere-ns/src/bin/orb-ns/runner/runner.rs @@ -5,12 +5,9 @@ use noosphere_ns::{ dht::{DHTConfig, DHTNode}, Validator, BOOTSTRAP_PEERS, }; -use noosphere_storage::{ - db::SphereDb, - memory::{MemoryStorageProvider, MemoryStore}, -}; +use noosphere_storage::{SphereDb, MemoryStorage}; -type NSNode = DHTNode>; +type NSNode = DHTNode>; /// Runner runs one or many DHT nodes based off of provided /// configuration from a [CLICommand]. @@ -28,7 +25,7 @@ impl Runner { } pub async fn run(&mut self) -> Result<()> { - let store = SphereDb::new(&MemoryStorageProvider::default()).await?; + let store = SphereDb::new(&MemoryStorage::default()).await?; for node_config in self.config.nodes.iter() { let config = DHTConfig { diff --git a/rust/noosphere-ns/src/builder.rs b/rust/noosphere-ns/src/builder.rs index 9978b1828..ae4397c07 100644 --- a/rust/noosphere-ns/src/builder.rs +++ b/rust/noosphere-ns/src/builder.rs @@ -4,7 +4,7 @@ use crate::{ }; use anyhow::{anyhow, Result}; use libp2p::{self, Multiaddr}; -use noosphere_storage::{db::SphereDb, interface::Store}; +use noosphere_storage::{SphereDb, Storage}; use std::net::Ipv4Addr; #[cfg(doc)] @@ -18,7 +18,7 @@ use libp2p::kad::KademliaConfig; /// /// ``` /// use noosphere_core::authority::generate_ed25519_key; -/// use noosphere_storage::{db::SphereDb, memory::{MemoryStore, MemoryStorageProvider}}; +/// use noosphere_storage::{SphereDb, MemoryStorage}; /// use noosphere_ns::{NameSystem, NameSystemBuilder}; /// use ucan_key_support::ed25519::Ed25519KeyMaterial; /// use tokio; @@ -26,7 +26,7 @@ use libp2p::kad::KademliaConfig; /// #[tokio::main] /// async fn main() { /// let key_material = generate_ed25519_key(); -/// let store = SphereDb::new(&MemoryStorageProvider::default()).await.unwrap(); +/// let store = SphereDb::new(&MemoryStorage::default()).await.unwrap(); /// /// let ns = NameSystemBuilder::default() /// .key_material(&key_material) @@ -34,13 +34,13 @@ use libp2p::kad::KademliaConfig; /// .listening_port(30000) /// .build().expect("valid config"); /// -/// assert!(NameSystemBuilder::::default().build().is_err(), +/// assert!(NameSystemBuilder::::default().build().is_err(), /// "key_material and store must be provided."); /// } /// ``` pub struct NameSystemBuilder where - S: Store, + S: Storage, K: DHTKeyMaterial, { bootstrap_peers: Option>, @@ -51,7 +51,7 @@ where impl NameSystemBuilder where - S: Store, + S: Storage, K: DHTKeyMaterial, { /// If bootstrap peers are provided, how often, @@ -153,7 +153,7 @@ where impl Default for NameSystemBuilder where - S: Store, + S: Storage, K: DHTKeyMaterial, { fn default() -> Self { @@ -170,18 +170,13 @@ where mod tests { use super::*; use noosphere_core::authority::generate_ed25519_key; - use noosphere_storage::{ - db::SphereDb, - memory::{MemoryStorageProvider, MemoryStore}, - }; + use noosphere_storage::{MemoryStorage, SphereDb}; use ucan_key_support::ed25519::Ed25519KeyMaterial; #[tokio::test] async fn test_name_system_builder() -> Result<(), anyhow::Error> { let key_material = generate_ed25519_key(); - let store = SphereDb::new(&MemoryStorageProvider::default()) - .await - .unwrap(); + let store = SphereDb::new(&MemoryStorage::default()).await.unwrap(); let bootstrap_peers: Vec = vec![ "/ip4/127.0.0.50/tcp/33333/p2p/12D3KooWH8WgH9mgbMXrKX4veokUznvEn6Ycwg4qaGNi83nLkoUK" .parse()?, @@ -217,14 +212,14 @@ mod tests { assert_eq!(ns.dht_config.replication_interval, 60 * 60 + 1); assert_eq!(ns.dht_config.record_ttl, 60 * 60 * 24 * 3 + 1); - if NameSystemBuilder::::default() + if NameSystemBuilder::::default() .store(&store) .build() .is_ok() { panic!("key_material required."); } - if NameSystemBuilder::::default() + if NameSystemBuilder::::default() .key_material(&key_material) .build() .is_ok() diff --git a/rust/noosphere-ns/src/dht/keys.rs b/rust/noosphere-ns/src/dht/keys.rs index 840db72eb..0894b4e9d 100644 --- a/rust/noosphere-ns/src/dht/keys.rs +++ b/rust/noosphere-ns/src/dht/keys.rs @@ -26,9 +26,7 @@ mod tests { #[test] fn it_converts_to_libp2p_keypair() -> anyhow::Result<()> { let zebra_keys = generate_ed25519_key(); - let keypair = match zebra_keys.to_dht_keypair()? { - libp2p::identity::Keypair::Ed25519(k) => k, - }; + let libp2p::identity::Keypair::Ed25519(keypair) = zebra_keys.to_dht_keypair()?; let zebra_private_key = zebra_keys.1.expect("Has private key"); let dalek_public_key = keypair.public().encode(); let dalek_private_key = keypair.secret(); diff --git a/rust/noosphere-ns/src/name_system.rs b/rust/noosphere-ns/src/name_system.rs index b2cc5b3b8..cfd92e79f 100644 --- a/rust/noosphere-ns/src/name_system.rs +++ b/rust/noosphere-ns/src/name_system.rs @@ -7,7 +7,7 @@ use anyhow::{anyhow, Result}; use futures::future::try_join_all; use libp2p::Multiaddr; use noosphere_core::authority::SUPPORTED_KEYS; -use noosphere_storage::{db::SphereDb, interface::Store}; +use noosphere_storage::{SphereDb, Storage}; use std::collections::HashMap; use ucan::crypto::did::DidParser; @@ -38,7 +38,7 @@ lazy_static! { /// New [NameSystem] instances can be created via [NameSystemBuilder]. pub struct NameSystem where - S: Store + 'static, + S: Storage + 'static, K: DHTKeyMaterial, { /// Bootstrap peers for the DHT network. @@ -58,7 +58,7 @@ where impl NameSystem where - S: Store, + S: Storage, K: DHTKeyMaterial, { /// Internal instantiation function invoked by [NameSystemBuilder]. @@ -256,7 +256,7 @@ where impl Drop for NameSystem where - S: Store, + S: Storage, K: DHTKeyMaterial, { fn drop(&mut self) { diff --git a/rust/noosphere-ns/src/records.rs b/rust/noosphere-ns/src/records.rs index c447e2f2c..f46d28f7d 100644 --- a/rust/noosphere-ns/src/records.rs +++ b/rust/noosphere-ns/src/records.rs @@ -2,7 +2,7 @@ use crate::utils::generate_capability; use anyhow::{anyhow, Error}; use cid::Cid; use noosphere_core::authority::SPHERE_SEMANTICS; -use noosphere_storage::{db::SphereDb, interface::Store}; +use noosphere_storage::{SphereDb, Storage}; use serde_json::Value; use std::{convert::TryFrom, str, str::FromStr}; use ucan::{chain::ProofChain, crypto::did::DidParser, Ucan}; @@ -76,7 +76,7 @@ impl NSRecord { /// Validates the underlying [Ucan] token, ensuring that /// the sphere's owner authorized the publishing of a new /// content address. Returns an `Err` if validation fails. - pub async fn validate( + pub async fn validate( &self, store: &SphereDb, did_parser: &mut DidParser, @@ -210,10 +210,7 @@ impl FromStr for NSRecord { mod test { use super::*; use noosphere_core::authority::{generate_ed25519_key, SUPPORTED_KEYS}; - use noosphere_storage::{ - db::SphereDb, - memory::{MemoryStorageProvider, MemoryStore}, - }; + use noosphere_storage::{SphereDb, MemoryStorage}; use serde_json::json; use std::str::FromStr; @@ -223,7 +220,7 @@ mod test { async fn expect_failure( message: &str, - store: &SphereDb, + store: &SphereDb, did_parser: &mut DidParser, ucan: Ucan, ) { @@ -245,9 +242,7 @@ mod test { let capability = generate_capability(&sphere_identity); let cid_address = "bafy2bzacec4p5h37mjk2n6qi6zukwyzkruebvwdzqpdxzutu4sgoiuhqwne72"; let fact = json!({ "address": cid_address }); - let store = SphereDb::new(&MemoryStorageProvider::default()) - .await - .unwrap(); + let store = SphereDb::new(&MemoryStorage::default()).await.unwrap(); let record = NSRecord::new( UcanBuilder::default() @@ -277,9 +272,7 @@ mod test { let capability = generate_capability(&sphere_identity); let cid_address = "bafy2bzacec4p5h37mjk2n6qi6zukwyzkruebvwdzqpdxzutu4sgoiuhqwne72"; let fact = json!({ "address": cid_address }); - let mut store = SphereDb::new(&MemoryStorageProvider::default()) - .await - .unwrap(); + let mut store = SphereDb::new(&MemoryStorage::default()).await.unwrap(); // First verify that `owner` cannot publish for `sphere` // without delegation. @@ -339,9 +332,7 @@ mod test { let sphere_identity = sphere_key.get_did().await?; let mut did_parser = DidParser::new(SUPPORTED_KEYS); let cid_address = "bafy2bzacec4p5h37mjk2n6qi6zukwyzkruebvwdzqpdxzutu4sgoiuhqwne72"; - let store = SphereDb::new(&MemoryStorageProvider::default()) - .await - .unwrap(); + let store = SphereDb::new(&MemoryStorage::default()).await.unwrap(); let sphere_capability = generate_capability(&sphere_identity); expect_failure( diff --git a/rust/noosphere-ns/src/utils.rs b/rust/noosphere-ns/src/utils.rs index 7c4136490..39fa4e047 100644 --- a/rust/noosphere-ns/src/utils.rs +++ b/rust/noosphere-ns/src/utils.rs @@ -40,7 +40,7 @@ pub fn generate_capability(sphere_did: &str) -> Capability { +pub struct Validator { store: SphereDb, did_parser: DidParser, } impl Validator where - S: Store, + S: Storage, { pub fn new(store: &SphereDb) -> Self { Validator { @@ -25,7 +25,7 @@ where #[async_trait] impl RecordValidator for Validator where - S: Store, + S: Storage, { async fn validate(&mut self, record_value: &[u8]) -> bool { if let Ok(record) = NSRecord::try_from(record_value) { diff --git a/rust/noosphere-ns/tests/ns_test.rs b/rust/noosphere-ns/tests/ns_test.rs index 4e533155f..1447ce148 100644 --- a/rust/noosphere-ns/tests/ns_test.rs +++ b/rust/noosphere-ns/tests/ns_test.rs @@ -10,9 +10,7 @@ use noosphere_ns::{ utils::{generate_capability, generate_fact}, NameSystem, NameSystemBuilder, }; -use noosphere_storage::{ - db::SphereDb, encoding::derive_cid, memory::MemoryStorageProvider, memory::MemoryStore, -}; +use noosphere_storage::{derive_cid, MemoryStorage, SphereDb}; use ucan::{builder::UcanBuilder, crypto::KeyMaterial, store::UcanJwtStore, time::now, Ucan}; use ucan_key_support::ed25519::Ed25519KeyMaterial; @@ -21,7 +19,7 @@ use utils::create_bootstrap_nodes; /// Data related to an owner sphere and a NameSystem running /// on its behalf in it's corresponding gateway. struct NSData { - pub ns: NameSystem, + pub ns: NameSystem, pub owner_key: Ed25519KeyMaterial, pub owner_id: String, pub sphere_id: String, @@ -34,7 +32,7 @@ async fn generate_name_systems_network( ns_count: usize, ) -> Result<( DHTNode, - SphereDb, + SphereDb, Vec, )> { let bootstrap_node = create_bootstrap_nodes(1, DHTNode::::validator()) @@ -43,7 +41,7 @@ async fn generate_name_systems_network( .unwrap(); let bootstrap_addresses = vec![bootstrap_node.p2p_address().unwrap().to_owned()]; - let mut store = SphereDb::new(&MemoryStorageProvider::default()).await?; + let mut store = SphereDb::new(&MemoryStorage::default()).await?; let mut name_systems: Vec = vec![]; for _ in 0..ns_count { @@ -65,7 +63,7 @@ async fn generate_name_systems_network( let _ = &store.write_token(&delegation.encode()?).await?; let ns_key = generate_ed25519_key(); - let ns: NameSystem = NameSystemBuilder::default() + let ns: NameSystem = NameSystemBuilder::default() .key_material(&ns_key) .store(&store) .peer_dialing_interval(1) diff --git a/rust/noosphere-storage/src/block.rs b/rust/noosphere-storage/src/block.rs new file mode 100644 index 000000000..c058b0910 --- /dev/null +++ b/rust/noosphere-storage/src/block.rs @@ -0,0 +1,155 @@ +use std::io::Cursor; + +use anyhow::{anyhow, Result}; +use async_trait::async_trait; +use cid::{ + multihash::{Code, MultihashDigest}, + Cid, +}; +use libipld_core::{ + codec::References, + serde::{from_ipld, to_ipld}, +}; +use libipld_core::{ + codec::{Codec, Decode, Encode}, + ipld::Ipld, +}; +use serde::{de::DeserializeOwned, Serialize}; + +#[cfg(not(target_arch = "wasm32"))] +pub trait BlockStoreSendSync: Send + Sync {} + +#[cfg(not(target_arch = "wasm32"))] +impl BlockStoreSendSync for T where T: Send + Sync {} + +#[cfg(target_arch = "wasm32")] +pub trait BlockStoreSendSync {} + +#[cfg(target_arch = "wasm32")] +impl BlockStoreSendSync for T {} + +#[cfg(not(target_arch = "wasm32"))] +pub trait BlockStoreSend: Send {} + +#[cfg(not(target_arch = "wasm32"))] +impl BlockStoreSend for T where T: Send {} + +#[cfg(target_arch = "wasm32")] +pub trait BlockStoreSend {} + +#[cfg(target_arch = "wasm32")] +impl BlockStoreSend for T {} + +/// An interface for storage backends that are suitable for storing blocks. A +/// block is a chunk of bytes that can be addressed by a +/// [CID](https://docs.ipfs.tech/concepts/content-addressing/#identifier-formats). +/// Any backend that implements this trait should be able to reliably store and +/// retrieve blocks given a [Cid]. +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +pub trait BlockStore: Clone + BlockStoreSendSync { + /// Given a CID and a block, store the links (any [Cid] that is part of the + /// encoded data) in a suitable location for later retrieval. This method is + /// optional, and its default implementation is a no-op. It should be + /// implemented when possible to enable optimized traversal of a DAG given + /// its root. + #[allow(unused_variables)] + async fn put_links(&mut self, cid: &Cid, block: &[u8]) -> Result<()> + where + C: Codec + Default, + Ipld: References, + { + Ok(()) + } + + /// Given a block and its [Cid], persist the block in storage. + async fn put_block(&mut self, cid: &Cid, block: &[u8]) -> Result<()>; + + /// Given the [Cid] of a block, retrieve the block bytes from storage. + async fn get_block(&self, cid: &Cid) -> Result>>; + + /// Given some data structure that implements [Encode] for a given [Codec], + /// encode it as a block and persist it to storage for later retrieval by + /// [Cid]. + async fn put(&mut self, data: T) -> Result + where + C: Codec + Default, + T: Encode + BlockStoreSend, + Ipld: References, + { + let codec = C::default(); + let block = codec.encode(&data)?; + let cid = Cid::new_v1(codec.into(), Code::Blake2b256.digest(&block)); + + self.put_block(&cid, &block).await?; + self.put_links::(&cid, &block).await?; + + Ok(cid) + } + + /// Given the [Cid] of a block that refers to a type that implements + /// [Decode] for some [Codec], retrieve the block, decode it as the type and + /// return the result. + async fn get(&self, cid: &Cid) -> Result> + where + C: Codec + Default, + T: Decode, + { + let codec = C::default(); + let block = self.get_block(cid).await?; + + Ok(match block { + Some(bytes) => Some(T::decode(codec, &mut Cursor::new(bytes))?), + None => None, + }) + } + + /// Given some data structure that implements [Serialize], convert it to an + /// [IPLD](https://ipld.io/docs/)-compatible representation, encode it as a + /// block with the desired [Codec] and persist it to the storage backend by + /// its [Cid] + async fn save(&mut self, data: T) -> Result + where + C: Codec + Default, + T: Serialize + BlockStoreSend, + Ipld: Encode + References, + { + self.put::(to_ipld(data)?).await + } + + /// Given a [Cid] that refers to some data structure that implements + /// [Deserialize], read the block bytes from storage, decode it as + /// [IPLD](https://ipld.io/docs/) using the specified [Codec] and and + /// deserialize it to the intended data structure, returning the result. + async fn load(&self, cid: &Cid) -> Result + where + C: Codec + Default, + T: DeserializeOwned + BlockStoreSend, + u64: From, + Ipld: Decode, + { + let codec = u64::from(C::default()); + + if cid.codec() != codec { + return Err(anyhow!( + "Incorrect codec; expected {}, but CID refers to {}", + codec, + cid.codec() + )); + } + + Ok(match self.get::(cid).await? { + Some(ipld) => from_ipld(ipld)?, + None => return Err(anyhow!("No block found for {}", cid)), + }) + } + + /// Same as load, but returns an error if no block is found locally for the + /// given [Cid] + async fn require_block(&self, cid: &Cid) -> Result> { + match self.get_block(cid).await? { + Some(block) => Ok(block), + None => Err(anyhow!("Block {cid} was required but not found")), + } + } +} diff --git a/rust/noosphere-storage/src/db.rs b/rust/noosphere-storage/src/db.rs index 049b496e8..5ec2d9edc 100644 --- a/rust/noosphere-storage/src/db.rs +++ b/rust/noosphere-storage/src/db.rs @@ -13,10 +13,7 @@ use std::{collections::BTreeSet, fmt::Debug}; use tokio_stream::Stream; use ucan::store::{UcanStore, UcanStoreConditionalSend}; -use crate::{ - interface::{BlockStore, BlockStoreSend, KeyValueStore, StorageProvider, Store}, - memory::MemoryStore, -}; +use crate::{BlockStore, BlockStoreSend, KeyValueStore, MemoryStore, Storage}; use async_stream::try_stream; @@ -40,30 +37,37 @@ pub const METADATA_STORE: &str = "metadata"; pub const SPHERE_DB_STORE_NAMES: &[&str] = &[BLOCK_STORE, LINK_STORE, VERSION_STORE, METADATA_STORE]; +/// A [SphereDb] is a high-level storage primitive for Noosphere's APIs. It +/// takes a [Storage] and implements [BlockStore] and [KeyValueStore], +/// orchestrating writes so that as blocks are stored, links are also extracted +/// and tracked separately, and also hosting metadata information such as sphere +/// version records and other purely local configuration #[derive(Clone)] pub struct SphereDb where - S: Store, + S: Storage, { - block_store: S, - link_store: S, - version_store: S, - metadata_store: S, + block_store: S::BlockStore, + link_store: S::KeyValueStore, + version_store: S::KeyValueStore, + metadata_store: S::KeyValueStore, } impl SphereDb where - S: Store, + S: Storage, { - pub async fn new>(storage_provider: &P) -> Result> { + pub async fn new(storage: &S) -> Result> { Ok(SphereDb { - block_store: storage_provider.get_store(BLOCK_STORE).await?, - link_store: storage_provider.get_store(LINK_STORE).await?, - version_store: storage_provider.get_store(VERSION_STORE).await?, - metadata_store: storage_provider.get_store(METADATA_STORE).await?, + block_store: storage.get_block_store(BLOCK_STORE).await?, + link_store: storage.get_key_value_store(LINK_STORE).await?, + version_store: storage.get_key_value_store(VERSION_STORE).await?, + metadata_store: storage.get_key_value_store(METADATA_STORE).await?, }) } + /// Given a [MemoryStore], store copies of all the blocks found within in + /// the storage that backs this [SphereDb]. pub async fn persist(&mut self, memory_store: &MemoryStore) -> Result<()> { let cids = memory_store.get_stored_cids().await; @@ -85,16 +89,20 @@ where Ok(()) } + /// Record the tip of a local sphere lineage as a [Cid] pub async fn set_version(&mut self, identity: &str, version: &Cid) -> Result<()> { self.version_store .set_key(identity.to_string(), version) .await } + /// Get the most recently recorded tip of a local sphere lineage pub async fn get_version(&self, identity: &str) -> Result> { self.version_store.get_key(identity).await } + /// Get the most recently recorded tip of a local sphere lineage, returning + /// an error if no version has ever been recorded pub async fn require_version(&self, identity: &str) -> Result { self.version_store .get_key(identity) @@ -102,10 +110,18 @@ where .ok_or_else(|| anyhow!("No version was found for sphere {}", identity)) } + /// Get all links referenced by a block given its [Cid] pub async fn get_block_links(&self, cid: &Cid) -> Result>> { self.link_store.get_key(&cid.to_string()).await } + /// Given a [Cid] root and a predicate function, stream all links that are + /// referenced by the root or its descendants (recursively). The predicate + /// function is called with each [Cid] before it is yielded by the stream. + /// If the predicate returns true, the [Cid] is yielded and its referenced + /// links are queued to be yielded later by the stream. If the predicate + /// returns false, the [Cid] is skipped and by extension so are its + /// referenced links. pub fn query_links<'a, F, P>( &'a self, cid: &'a Cid, @@ -137,6 +153,8 @@ where } } + /// Stream all links that are referenced from the given root [Cid] or its + /// DAG descendants (recursively). pub fn stream_links<'a>(&'a self, cid: &'a Cid) -> impl Stream> + 'a { try_stream! { for await cid in self.query_links(cid, |_| async {Ok(true)}) { @@ -145,6 +163,7 @@ where } } + /// Stream all the blocks in the DAG starting at the given root [Cid]. pub fn stream_blocks<'a>( &'a self, cid: &'a Cid, @@ -159,7 +178,9 @@ where } } - pub fn to_block_store(&self) -> S { + /// Get an owned copy of the underlying primitive [BlockStore] for this + /// [SphereDb] + pub fn to_block_store(&self) -> S::BlockStore { self.block_store.clone() } } @@ -168,7 +189,7 @@ where #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] impl BlockStore for SphereDb where - S: Store, + S: Storage, { async fn put_links(&mut self, cid: &Cid, block: &[u8]) -> Result<()> where @@ -198,7 +219,7 @@ where #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] impl KeyValueStore for SphereDb where - S: Store, + S: Storage, { async fn set_key(&mut self, key: K, value: V) -> Result<()> where @@ -228,7 +249,7 @@ where #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] impl UcanStore for SphereDb where - S: Store, + S: Storage, { async fn read>(&self, cid: &Cid) -> Result> { self.get::(cid).await @@ -251,12 +272,7 @@ mod tests { #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure}; - use crate::{ - db::SphereDb, - encoding::{block_encode, derive_cid}, - interface::BlockStore, - memory::MemoryStorageProvider, - }; + use crate::{block_encode, derive_cid, BlockStore, MemoryStorage, SphereDb}; use tokio_stream::StreamExt; @@ -266,7 +282,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] pub async fn it_stores_links_when_a_block_is_saved() { - let storage_provider = MemoryStorageProvider::default(); + let storage_provider = MemoryStorage::default(); let mut db = SphereDb::new(&storage_provider).await.unwrap(); let list1 = vec!["cats", "dogs", "pigeons"]; @@ -287,7 +303,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] pub async fn it_can_stream_all_blocks_in_a_dag() { - let storage_provider = MemoryStorageProvider::default(); + let storage_provider = MemoryStorage::default(); let mut db = SphereDb::new(&storage_provider).await.unwrap(); let list1 = vec!["cats", "dogs", "pigeons"]; @@ -322,7 +338,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] pub async fn it_can_put_a_raw_block_and_read_it_as_a_token() { - let storage_provider = MemoryStorageProvider::default(); + let storage_provider = MemoryStorage::default(); let mut db = SphereDb::new(&storage_provider).await.unwrap(); let (cid, block) = block_encode::(&Ipld::Bytes(b"foobar".to_vec())).unwrap(); diff --git a/rust/noosphere-storage/src/helpers.rs b/rust/noosphere-storage/src/helpers.rs index 6a99cbd79..0e423a831 100644 --- a/rust/noosphere-storage/src/helpers.rs +++ b/rust/noosphere-storage/src/helpers.rs @@ -1,8 +1,8 @@ -use crate::interface::StorageProvider; +use crate::Storage; use anyhow::Result; #[cfg(not(target_arch = "wasm32"))] -use crate::native::{NativeStorageInit, NativeStorageProvider, NativeStore}; +use crate::{NativeStorage, NativeStorageInit, NativeStore}; #[cfg(not(target_arch = "wasm32"))] pub async fn make_disposable_store() -> Result { @@ -14,12 +14,12 @@ pub async fn make_disposable_store() -> Result { .map(String::from) .collect(); let db = sled::open(temp_dir.join(temp_name)).unwrap(); - let provider = NativeStorageProvider::new(NativeStorageInit::Db(db))?; - provider.get_store("foo").await + let provider = NativeStorage::new(NativeStorageInit::Db(db))?; + provider.get_block_store("foo").await } #[cfg(target_arch = "wasm32")] -use crate::web::{WebStorageProvider, WebStore}; +use crate::{WebStorage, WebStore}; #[cfg(target_arch = "wasm32")] pub async fn make_disposable_store() -> Result { @@ -30,6 +30,6 @@ pub async fn make_disposable_store() -> Result { .map(|word| String::from(word)) .collect(); - let provider = WebStorageProvider::new(&temp_name).await?; - provider.get_store(crate::db::BLOCK_STORE).await + let provider = WebStorage::new(&temp_name).await?; + provider.get_block_store(crate::db::BLOCK_STORE).await } diff --git a/rust/noosphere-storage/src/memory.rs b/rust/noosphere-storage/src/implementation/memory.rs similarity index 86% rename from rust/noosphere-storage/src/memory.rs rename to rust/noosphere-storage/src/implementation/memory.rs index 3e812587f..7856570af 100644 --- a/rust/noosphere-storage/src/memory.rs +++ b/rust/noosphere-storage/src/implementation/memory.rs @@ -4,7 +4,8 @@ use async_trait::async_trait; use cid::Cid; use std::{collections::HashMap, sync::Arc}; -use crate::interface::{StorageProvider, Store}; +use crate::storage::Storage; +use crate::store::Store; #[cfg_attr(not(target_arch = "wasm32"), async_trait)] #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] @@ -20,14 +21,12 @@ impl StoreContainsCid for S { } } -#[derive(Default)] -pub struct MemoryStorageProvider { +#[derive(Default, Clone)] +pub struct MemoryStorage { stores: Arc>>, } -#[cfg_attr(not(target_arch = "wasm32"), async_trait)] -#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] -impl StorageProvider for MemoryStorageProvider { +impl MemoryStorage { async fn get_store(&self, name: &str) -> Result { let mut stores = self.stores.lock().await; @@ -42,6 +41,22 @@ impl StorageProvider for MemoryStorageProvider { } } +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +impl Storage for MemoryStorage { + type BlockStore = MemoryStore; + + type KeyValueStore = MemoryStore; + + async fn get_block_store(&self, name: &str) -> Result { + self.get_store(name).await + } + + async fn get_key_value_store(&self, name: &str) -> Result { + self.get_store(name).await + } +} + #[derive(Clone, Default, Debug)] pub struct MemoryStore { pub entries: Arc, Vec>>>, diff --git a/rust/noosphere-storage/src/implementation/mod.rs b/rust/noosphere-storage/src/implementation/mod.rs new file mode 100644 index 000000000..f45005436 --- /dev/null +++ b/rust/noosphere-storage/src/implementation/mod.rs @@ -0,0 +1,17 @@ +mod memory; +mod tracking; + +pub use memory::*; +pub use tracking::*; + +#[cfg(not(target_arch = "wasm32"))] +mod native; + +#[cfg(not(target_arch = "wasm32"))] +pub use native::*; + +#[cfg(target_arch = "wasm32")] +mod web; + +#[cfg(target_arch = "wasm32")] +pub use web::*; diff --git a/rust/noosphere-storage/src/native.rs b/rust/noosphere-storage/src/implementation/native.rs similarity index 76% rename from rust/noosphere-storage/src/native.rs rename to rust/noosphere-storage/src/implementation/native.rs index 535b5e356..5d7eb1b11 100644 --- a/rust/noosphere-storage/src/native.rs +++ b/rust/noosphere-storage/src/implementation/native.rs @@ -1,6 +1,8 @@ use std::path::PathBuf; -use crate::interface::{StorageProvider, Store}; +use crate::storage::Storage; +use crate::store::Store; + use anyhow::Result; use async_trait::async_trait; use sled::{Db, Tree}; @@ -11,11 +13,11 @@ pub enum NativeStorageInit { } #[derive(Clone)] -pub struct NativeStorageProvider { +pub struct NativeStorage { db: Db, } -impl NativeStorageProvider { +impl NativeStorage { pub fn new(init: NativeStorageInit) -> Result { let db: Db = match init { NativeStorageInit::Path(path) => { @@ -25,17 +27,29 @@ impl NativeStorageProvider { NativeStorageInit::Db(db) => db, }; - Ok(NativeStorageProvider { db }) + Ok(NativeStorage { db }) } -} -#[async_trait] -impl StorageProvider for NativeStorageProvider { async fn get_store(&self, name: &str) -> Result { Ok(NativeStore::new(&self.db.open_tree(name)?)) } } +#[async_trait] +impl Storage for NativeStorage { + type BlockStore = NativeStore; + + type KeyValueStore = NativeStore; + + async fn get_block_store(&self, name: &str) -> Result { + self.get_store(name).await + } + + async fn get_key_value_store(&self, name: &str) -> Result { + self.get_store(name).await + } +} + #[derive(Clone)] pub struct NativeStore { db: Tree, diff --git a/rust/noosphere-storage/src/tracking.rs b/rust/noosphere-storage/src/implementation/tracking.rs similarity index 98% rename from rust/noosphere-storage/src/tracking.rs rename to rust/noosphere-storage/src/implementation/tracking.rs index 3e5ccb045..80ce179fc 100644 --- a/rust/noosphere-storage/src/tracking.rs +++ b/rust/noosphere-storage/src/implementation/tracking.rs @@ -3,7 +3,7 @@ use async_std::sync::Mutex; use async_trait::async_trait; use std::sync::Arc; -use crate::interface::Store; +use crate::store::Store; #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct StoreStats { diff --git a/rust/noosphere-storage/src/web.rs b/rust/noosphere-storage/src/implementation/web.rs similarity index 89% rename from rust/noosphere-storage/src/web.rs rename to rust/noosphere-storage/src/implementation/web.rs index 82eec5feb..2509c371a 100644 --- a/rust/noosphere-storage/src/web.rs +++ b/rust/noosphere-storage/src/implementation/web.rs @@ -1,4 +1,5 @@ -use crate::interface::{StorageProvider, Store}; +use crate::store::Store; +use crate::{db::SPHERE_DB_STORE_NAMES, storage::Storage}; use anyhow::{anyhow, Result}; use async_trait::async_trait; use js_sys::Uint8Array; @@ -7,15 +8,15 @@ use rexie::{ }; use std::rc::Rc; use wasm_bindgen::{JsCast, JsValue}; -use crate::db::SPHERE_DB_STORE_NAMES; pub const INDEXEDDB_STORAGE_VERSION: u32 = 1; -pub struct WebStorageProvider { +#[derive(Clone)] +pub struct WebStorage { db: Rc, } -impl WebStorageProvider { +impl WebStorage { pub async fn new(db_name: &str) -> Result { Self::configure(INDEXEDDB_STORAGE_VERSION, db_name, SPHERE_DB_STORE_NAMES).await } @@ -32,12 +33,9 @@ impl WebStorageProvider { .await .map_err(|error| anyhow!("{:?}", error))?; - Ok(WebStorageProvider { db: Rc::new(db) }) + Ok(WebStorage { db: Rc::new(db) }) } -} -#[async_trait(?Send)] -impl StorageProvider for WebStorageProvider { async fn get_store(&self, name: &str) -> Result { if self .db @@ -56,6 +54,21 @@ impl StorageProvider for WebStorageProvider { } } +#[async_trait(?Send)] +impl Storage for WebStorage { + type BlockStore = WebStore; + + type KeyValueStore = WebStore; + + async fn get_block_store(&self, name: &str) -> Result { + self.get_store(name).await + } + + async fn get_key_value_store(&self, name: &str) -> Result { + self.get_store(name).await + } +} + #[derive(Clone)] pub struct WebStore { db: Rc, diff --git a/rust/noosphere-storage/src/interface.rs b/rust/noosphere-storage/src/interface.rs deleted file mode 100644 index 66947d5d0..000000000 --- a/rust/noosphere-storage/src/interface.rs +++ /dev/null @@ -1,256 +0,0 @@ -use std::{fmt::Display, io::Cursor}; - -use anyhow::{anyhow, Result}; -use async_trait::async_trait; -use cid::{ - multihash::{Code, MultihashDigest}, - Cid, -}; -use libipld_cbor::DagCborCodec; -use libipld_core::{ - codec::References, - serde::{from_ipld, to_ipld}, -}; -use libipld_core::{ - codec::{Codec, Decode, Encode}, - ipld::Ipld, -}; -use serde::{de::DeserializeOwned, Serialize}; - -#[cfg_attr(not(target_arch = "wasm32"), async_trait)] -#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] -pub trait StorageProvider { - async fn get_store(&self, name: &str) -> Result; -} - -#[cfg(not(target_arch = "wasm32"))] -pub trait StoreConditionalSendSync: Send + Sync {} - -#[cfg(not(target_arch = "wasm32"))] -impl StoreConditionalSendSync for S where S: Send + Sync {} - -#[cfg(target_arch = "wasm32")] -pub trait StoreConditionalSendSync {} - -#[cfg(target_arch = "wasm32")] -impl StoreConditionalSendSync for S {} - -#[cfg_attr(not(target_arch = "wasm32"), async_trait)] -#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] -pub trait Store: Clone + StoreConditionalSendSync { - /// Read the bytes stored against a given key - async fn read(&self, key: &[u8]) -> Result>>; - - /// Writes bytes to local storage against a given key, and returns the previous - /// value stored against that key if any - async fn write(&mut self, key: &[u8], bytes: &[u8]) -> Result>>; - - /// Remove a value given a key, returning the removed value if any - async fn remove(&mut self, key: &[u8]) -> Result>>; - - /// Flushes pending writes if there are any - async fn flush(&self) -> Result<()> { - Ok(()) - } -} - -#[cfg(not(target_arch = "wasm32"))] -pub trait BlockStoreSendSync: Send + Sync {} - -#[cfg(not(target_arch = "wasm32"))] -impl BlockStoreSendSync for T where T: Send + Sync {} - -#[cfg(target_arch = "wasm32")] -pub trait BlockStoreSendSync {} - -#[cfg(target_arch = "wasm32")] -impl BlockStoreSendSync for T {} - -#[cfg(not(target_arch = "wasm32"))] -pub trait BlockStoreSend: Send {} - -#[cfg(not(target_arch = "wasm32"))] -impl BlockStoreSend for T where T: Send {} - -#[cfg(target_arch = "wasm32")] -pub trait BlockStoreSend {} - -#[cfg(target_arch = "wasm32")] -impl BlockStoreSend for T {} - -#[cfg_attr(not(target_arch = "wasm32"), async_trait)] -#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] -pub trait BlockStore: Clone + BlockStoreSendSync { - #[allow(unused_variables)] - async fn put_links(&mut self, cid: &Cid, block: &[u8]) -> Result<()> - where - C: Codec + Default, - Ipld: References, - { - Ok(()) - } - - async fn put_block(&mut self, cid: &Cid, block: &[u8]) -> Result<()>; - - async fn get_block(&self, cid: &Cid) -> Result>>; - - async fn put(&mut self, data: T) -> Result - where - C: Codec + Default, - T: Encode + BlockStoreSend, - Ipld: References, - { - let codec = C::default(); - let block = codec.encode(&data)?; - let cid = Cid::new_v1(codec.into(), Code::Blake2b256.digest(&block)); - - self.put_block(&cid, &block).await?; - self.put_links::(&cid, &block).await?; - - Ok(cid) - } - - async fn get(&self, cid: &Cid) -> Result> - where - C: Codec + Default, - T: Decode, - { - let codec = C::default(); - let block = self.get_block(cid).await?; - - Ok(match block { - Some(bytes) => Some(T::decode(codec, &mut Cursor::new(bytes))?), - None => None, - }) - } - - async fn save(&mut self, data: T) -> Result - where - C: Codec + Default, - T: Serialize + BlockStoreSend, - Ipld: Encode + References, - { - self.put::(to_ipld(data)?).await - } - - async fn load(&self, cid: &Cid) -> Result - where - C: Codec + Default, - T: DeserializeOwned + BlockStoreSend, - u64: From, - Ipld: Decode, - { - let codec = u64::from(C::default()); - - if cid.codec() != codec { - return Err(anyhow!( - "Incorrect codec; expected {}, but CID refers to {}", - codec, - cid.codec() - )); - } - - Ok(match self.get::(cid).await? { - Some(ipld) => from_ipld(ipld)?, - None => return Err(anyhow!("No block found for {}", cid)), - }) - } - - async fn require_block(&self, cid: &Cid) -> Result> { - match self.get_block(cid).await? { - Some(block) => Ok(block), - None => Err(anyhow!("Block {cid} was required but not found")), - } - } -} - -#[cfg_attr(not(target_arch = "wasm32"), async_trait)] -#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] -impl BlockStore for S -where - S: Store, -{ - async fn put_block(&mut self, cid: &Cid, block: &[u8]) -> Result<()> { - self.write(&cid.to_bytes(), block).await?; - Ok(()) - } - - async fn get_block(&self, cid: &Cid) -> Result>> { - self.read(&cid.to_bytes()).await - } -} - -#[cfg_attr(not(target_arch = "wasm32"), async_trait)] -#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] -pub trait KeyValueStore { - async fn set_key(&mut self, key: K, value: V) -> Result<()> - where - K: AsRef<[u8]> + BlockStoreSend, - V: Serialize + BlockStoreSend; - - async fn get_key(&self, key: K) -> Result> - where - K: AsRef<[u8]> + BlockStoreSend, - V: DeserializeOwned + BlockStoreSend; - - async fn unset_key(&mut self, key: K) -> Result<()> - where - K: AsRef<[u8]> + BlockStoreSend; - - async fn require_key(&self, key: K) -> Result - where - K: AsRef<[u8]> + BlockStoreSend + Display, - V: DeserializeOwned + BlockStoreSend, - { - let required = key.to_string(); - - match self.get_key(key).await? { - Some(value) => Ok(value), - None => Err(anyhow!("No value found for '{required}'")), - } - } -} - -#[cfg_attr(not(target_arch = "wasm32"), async_trait)] -#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] -impl KeyValueStore for S -where - S: Store, -{ - async fn set_key(&mut self, key: K, value: V) -> Result<()> - where - K: AsRef<[u8]> + BlockStoreSend, - V: Serialize + BlockStoreSend, - { - let ipld = to_ipld(value)?; - let codec = DagCborCodec; - let cbor = codec.encode(&ipld)?; - let key_bytes = K::as_ref(&key); - self.write(key_bytes, &cbor).await?; - Ok(()) - } - - async fn unset_key(&mut self, key: K) -> Result<()> - where - K: AsRef<[u8]> + BlockStoreSend, - { - let key_bytes = K::as_ref(&key); - self.remove(key_bytes).await?; - Ok(()) - } - - async fn get_key(&self, key: K) -> Result> - where - K: AsRef<[u8]> + BlockStoreSend, - V: DeserializeOwned + BlockStoreSend, - { - let key_bytes = K::as_ref(&key); - Ok(match self.read(key_bytes).await? { - Some(bytes) => Some(from_ipld(Ipld::decode( - DagCborCodec, - &mut Cursor::new(bytes), - )?)?), - None => None, - }) - } -} diff --git a/rust/noosphere-storage/src/key_value.rs b/rust/noosphere-storage/src/key_value.rs new file mode 100644 index 000000000..e8c5d9fb5 --- /dev/null +++ b/rust/noosphere-storage/src/key_value.rs @@ -0,0 +1,70 @@ +use std::fmt::Display; + +use anyhow::{anyhow, Result}; +use async_trait::async_trait; +use serde::{de::DeserializeOwned, Serialize}; + +#[cfg(not(target_arch = "wasm32"))] +pub trait KeyValueSendSync: Send + Sync {} + +#[cfg(not(target_arch = "wasm32"))] +impl KeyValueSendSync for T where T: Send + Sync {} + +#[cfg(target_arch = "wasm32")] +pub trait KeyValueSendSync {} + +#[cfg(target_arch = "wasm32")] +impl KeyValueSendSync for T {} + +#[cfg(not(target_arch = "wasm32"))] +pub trait KeyValueStoreSend: Send {} + +#[cfg(not(target_arch = "wasm32"))] +impl KeyValueStoreSend for T where T: Send {} + +#[cfg(target_arch = "wasm32")] +pub trait KeyValueStoreSend {} + +#[cfg(target_arch = "wasm32")] +impl KeyValueStoreSend for T {} + +/// A [KeyValueStore] is a construct that is suitable for persisting generic +/// key/value data to a storage backend. +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +pub trait KeyValueStore: Clone + KeyValueSendSync { + /// Given some key that can be realized as bytes, persist a serializable + /// value to storage so that it can later be retrieved by that key + async fn set_key(&mut self, key: K, value: V) -> Result<()> + where + K: AsRef<[u8]> + KeyValueStoreSend, + V: Serialize + KeyValueStoreSend; + + /// Given some key that can be realized as bytes, retrieve some data that + /// can be deserialized as the intended data structure + async fn get_key(&self, key: K) -> Result> + where + K: AsRef<[u8]> + KeyValueStoreSend, + V: DeserializeOwned + KeyValueStoreSend; + + /// Given some key that can be realized as bytes, unset the value stored + /// against that key (if any) + async fn unset_key(&mut self, key: K) -> Result<()> + where + K: AsRef<[u8]> + KeyValueStoreSend; + + /// Same as get_key, but returns an error if no value is found to be stored + /// against the key + async fn require_key(&self, key: K) -> Result + where + K: AsRef<[u8]> + KeyValueStoreSend + Display, + V: DeserializeOwned + KeyValueStoreSend, + { + let required = key.to_string(); + + match self.get_key(key).await? { + Some(value) => Ok(value), + None => Err(anyhow!("No value found for '{required}'")), + } + } +} diff --git a/rust/noosphere-storage/src/lib.rs b/rust/noosphere-storage/src/lib.rs index 93eab2a26..0e6893e67 100644 --- a/rust/noosphere-storage/src/lib.rs +++ b/rust/noosphere-storage/src/lib.rs @@ -1,26 +1,37 @@ +///! This crate contains generic interfaces and concrete implementations to +///! support a common API for data persistance in Noosphere on many different +///! platforms. + #[macro_use] extern crate tracing; -pub mod db; -pub mod encoding; -pub mod interface; -pub mod memory; -pub mod tracking; -pub mod ucan; +mod block; +mod implementation; +mod key_value; + +mod db; +mod encoding; +mod storage; +mod store; +mod ucan; + +pub use crate::ucan::*; +pub use block::*; +pub use db::*; +pub use encoding::*; +pub use implementation::*; +pub use key_value::*; +pub use storage::*; +pub use store::*; pub const BLOCK_STORE: &str = "blocks"; -#[cfg(not(target_arch = "wasm32"))] -pub mod native; -#[cfg(target_arch = "wasm32")] -pub mod web; - #[cfg(test)] pub mod helpers; #[cfg(test)] mod tests { - use crate::{helpers::make_disposable_store, interface::BlockStore}; + use crate::{block::BlockStore, helpers::make_disposable_store}; use libipld_cbor::DagCborCodec; #[cfg(target_arch = "wasm32")] diff --git a/rust/noosphere-storage/src/storage.rs b/rust/noosphere-storage/src/storage.rs new file mode 100644 index 000000000..2a739c5d9 --- /dev/null +++ b/rust/noosphere-storage/src/storage.rs @@ -0,0 +1,25 @@ +use crate::block::BlockStore; +use crate::key_value::KeyValueStore; +use anyhow::Result; +use async_trait::async_trait; + +/// [Storage] is a general trait for composite storage backends. It is often the +/// case that we are able to use a single storage primitive for all forms of +/// storage, but sometimes block storage and generic key/value storage come from +/// different backends. [Storage] provides a composite interface where both +/// cases well accomodated without creating complexity in the signatures of +/// other Noosphere constructs. +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +pub trait Storage: Clone { + type BlockStore: BlockStore; + type KeyValueStore: KeyValueStore; + + /// Get a [BlockStore] where all values stored in it are scoped to the given + /// name + async fn get_block_store(&self, name: &str) -> Result; + + /// Get a [KeyValueStore] where all values stored in it are scoped to the + /// given name + async fn get_key_value_store(&self, name: &str) -> Result; +} diff --git a/rust/noosphere-storage/src/store.rs b/rust/noosphere-storage/src/store.rs new file mode 100644 index 000000000..c02b43d1a --- /dev/null +++ b/rust/noosphere-storage/src/store.rs @@ -0,0 +1,113 @@ +use std::io::Cursor; + +use anyhow::Result; +use async_trait::async_trait; +use cid::Cid; +use libipld_cbor::DagCborCodec; +use libipld_core::{ + codec::{Codec, Decode}, + ipld::Ipld, + serde::{from_ipld, to_ipld}, +}; +use serde::{de::DeserializeOwned, Serialize}; + +use crate::{ + block::BlockStore, + key_value::{KeyValueStore, KeyValueStoreSend}, +}; + +#[cfg(not(target_arch = "wasm32"))] +pub trait StoreConditionalSendSync: Send + Sync {} + +#[cfg(not(target_arch = "wasm32"))] +impl StoreConditionalSendSync for S where S: Send + Sync {} + +#[cfg(target_arch = "wasm32")] +pub trait StoreConditionalSendSync {} + +#[cfg(target_arch = "wasm32")] +impl StoreConditionalSendSync for S {} + +/// A primitive interface for storage backends. A storage backend does not +/// necessarily need to implement this trait to be used in Noosphere, but if it +/// does it automatically benefits from trait implementations for [BlockStore] +/// and [KeyValueStore], making a single [Store] implementation into a universal +/// backend. +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +pub trait Store: Clone + StoreConditionalSendSync { + /// Read the bytes stored against a given key + async fn read(&self, key: &[u8]) -> Result>>; + + /// Writes bytes to local storage against a given key, and returns the previous + /// value stored against that key if any + async fn write(&mut self, key: &[u8], bytes: &[u8]) -> Result>>; + + /// Remove a value given a key, returning the removed value if any + async fn remove(&mut self, key: &[u8]) -> Result>>; + + /// Flushes pending writes if there are any + async fn flush(&self) -> Result<()> { + Ok(()) + } +} + +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +impl BlockStore for S +where + S: Store, +{ + async fn put_block(&mut self, cid: &Cid, block: &[u8]) -> Result<()> { + self.write(&cid.to_bytes(), block).await?; + Ok(()) + } + + async fn get_block(&self, cid: &Cid) -> Result>> { + self.read(&cid.to_bytes()).await + } +} + +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +impl KeyValueStore for S +where + S: Store, +{ + async fn set_key(&mut self, key: K, value: V) -> Result<()> + where + K: AsRef<[u8]> + KeyValueStoreSend, + V: Serialize + KeyValueStoreSend, + { + let ipld = to_ipld(value)?; + let codec = DagCborCodec; + let cbor = codec.encode(&ipld)?; + let key_bytes = K::as_ref(&key); + self.write(key_bytes, &cbor).await?; + Ok(()) + } + + async fn unset_key(&mut self, key: K) -> Result<()> + where + K: AsRef<[u8]> + KeyValueStoreSend, + { + let key_bytes = K::as_ref(&key); + self.remove(key_bytes).await?; + Ok(()) + } + + async fn get_key(&self, key: K) -> Result> + where + K: AsRef<[u8]> + KeyValueStoreSend, + V: DeserializeOwned + KeyValueStoreSend, + { + let key_bytes = K::as_ref(&key); + Ok(match self.read(key_bytes).await? { + Some(bytes) => Some(from_ipld(Ipld::decode( + DagCborCodec, + &mut Cursor::new(bytes), + )?)?), + None => None, + }) + } +} diff --git a/rust/noosphere-storage/src/ucan.rs b/rust/noosphere-storage/src/ucan.rs index 35ad1c5aa..f41c58caf 100644 --- a/rust/noosphere-storage/src/ucan.rs +++ b/rust/noosphere-storage/src/ucan.rs @@ -8,7 +8,7 @@ use libipld_core::{ }; use ucan::store::{UcanStore as UcanStoreTrait, UcanStoreConditionalSend}; -use crate::interface::BlockStore; +use crate::block::BlockStore; pub struct UcanStore(pub S); diff --git a/rust/noosphere/src/ffi/fs.rs b/rust/noosphere/src/ffi/fs.rs index b13ef540d..147811ea7 100644 --- a/rust/noosphere/src/ffi/fs.rs +++ b/rust/noosphere/src/ffi/fs.rs @@ -8,22 +8,22 @@ use tokio::io::{AsyncRead, AsyncReadExt}; use crate::{ ffi::{NsHeaders, NsNoosphereContext}, - platform::{PlatformKeyMaterial, PlatformStore}, + platform::{PlatformKeyMaterial, PlatformStorage}, }; ReprC! { #[ReprC::opaque] pub struct NsSphereFs { - inner: SphereFs + inner: SphereFs } } impl NsSphereFs { - pub fn inner(&self) -> &SphereFs { + pub fn inner(&self) -> &SphereFs { &self.inner } - pub fn inner_mut(&mut self) -> &mut SphereFs { + pub fn inner_mut(&mut self) -> &mut SphereFs { &mut self.inner } } diff --git a/rust/noosphere/src/noosphere.rs b/rust/noosphere/src/noosphere.rs index 0b987c476..c3b1570a9 100644 --- a/rust/noosphere/src/noosphere.rs +++ b/rust/noosphere/src/noosphere.rs @@ -7,7 +7,7 @@ use url::Url; use crate::{ key::KeyStorage, - platform::{PlatformKeyMaterial, PlatformKeyStorage, PlatformStore}, + platform::{PlatformKeyMaterial, PlatformKeyStorage, PlatformStorage}, sphere::{SphereContext, SphereContextBuilder, SphereReceipt}, }; @@ -42,7 +42,7 @@ pub enum NoosphereContextConfiguration { pub struct NoosphereContext { configuration: NoosphereContextConfiguration, sphere_contexts: - Arc>>>>>, + Arc>>>>>, } impl NoosphereContext { @@ -172,7 +172,7 @@ impl NoosphereContext { pub async fn get_sphere_context( &self, sphere_identity: &Did, - ) -> Result>>> { + ) -> Result>>> { let mut contexts = self.sphere_contexts.lock().await; if !contexts.contains_key(sphere_identity) { diff --git a/rust/noosphere/src/platform.rs b/rust/noosphere/src/platform.rs index 65f64b534..124bd0ac6 100644 --- a/rust/noosphere/src/platform.rs +++ b/rust/noosphere/src/platform.rs @@ -8,7 +8,7 @@ target_vendor = "apple" ))] mod inner { - use noosphere_storage::native::{NativeStorageProvider, NativeStore}; + use noosphere_storage::{NativeStorage, NativeStore}; use ucan_key_support::ed25519::Ed25519KeyMaterial; use crate::key::InsecureKeyStorage; @@ -18,22 +18,28 @@ mod inner { pub type PlatformKeyMaterial = Ed25519KeyMaterial; pub type PlatformKeyStorage = InsecureKeyStorage; pub type PlatformStore = NativeStore; - pub type PlatformStorageProvider = NativeStorageProvider; + pub type PlatformStorage = NativeStorage; } #[cfg(target_arch = "wasm32")] mod inner { use crate::key::WebCryptoKeyStorage; - use anyhow::Result; - use noosphere_storage::web::{WebStorageProvider, WebStore}; - use std::path::PathBuf; + + use noosphere_storage::{WebStorage, WebStore}; + use std::sync::Arc; use ucan_key_support::web_crypto::WebCryptoRsaKeyMaterial; pub type PlatformKeyMaterial = Arc; pub type PlatformKeyStorage = WebCryptoKeyStorage; pub type PlatformStore = WebStore; - pub type PlatformStorageProvider = WebStorageProvider; + pub type PlatformStorage = WebStorage; + + #[cfg(test)] + use anyhow::Result; + + #[cfg(test)] + use std::path::PathBuf; #[cfg(test)] pub async fn make_temporary_platform_primitives() -> Result<(PathBuf, PlatformKeyStorage, ())> { @@ -65,7 +71,7 @@ mod inner { )) ))] mod inner { - use noosphere_storage::native::{NativeStorageProvider, NativeStore}; + use noosphere_storage::{NativeStorage, NativeStore}; use ucan_key_support::ed25519::Ed25519KeyMaterial; use crate::key::InsecureKeyStorage; @@ -73,7 +79,7 @@ mod inner { pub type PlatformKeyMaterial = Ed25519KeyMaterial; pub type PlatformKeyStorage = InsecureKeyStorage; pub type PlatformStore = NativeStore; - pub type PlatformStorageProvider = NativeStorageProvider; + pub type PlatformStorage = NativeStorage; #[cfg(test)] use anyhow::Result; diff --git a/rust/noosphere/src/sphere/builder.rs b/rust/noosphere/src/sphere/builder.rs index a826f80e9..836e14cf4 100644 --- a/rust/noosphere/src/sphere/builder.rs +++ b/rust/noosphere/src/sphere/builder.rs @@ -9,13 +9,13 @@ use noosphere_core::{ view::Sphere, }; -use noosphere_storage::{db::SphereDb, interface::KeyValueStore, memory::MemoryStore}; +use noosphere_storage::{KeyValueStore, MemoryStore, SphereDb}; use ucan::crypto::KeyMaterial; use url::Url; use crate::{ key::KeyStorage, - platform::{PlatformKeyMaterial, PlatformKeyStorage, PlatformStore}, + platform::{PlatformKeyMaterial, PlatformKeyStorage, PlatformStorage}, sphere::{ context::SphereContext, metadata::{AUTHORIZATION, IDENTITY, USER_KEY_NAME}, @@ -41,10 +41,10 @@ impl Default for SphereInitialization { /// are possible. pub enum SphereContextBuilderArtifacts { SphereCreated { - context: SphereContext, + context: SphereContext, mnemonic: String, }, - SphereOpened(SphereContext), + SphereOpened(SphereContext), } impl SphereContextBuilderArtifacts { @@ -58,7 +58,7 @@ impl SphereContextBuilderArtifacts { } } -impl From for SphereContext { +impl From for SphereContext { fn from(artifacts: SphereContextBuilderArtifacts) -> Self { match artifacts { SphereContextBuilderArtifacts::SphereCreated { context, .. } => context, @@ -327,7 +327,7 @@ mod tests { use libipld_core::raw::RawCodec; use noosphere_core::authority::Authorization; - use noosphere_storage::encoding::derive_cid; + use noosphere_storage::derive_cid; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::wasm_bindgen_test; diff --git a/rust/noosphere/src/sphere/context.rs b/rust/noosphere/src/sphere/context.rs index 18f8af94f..cf83779ed 100644 --- a/rust/noosphere/src/sphere/context.rs +++ b/rust/noosphere/src/sphere/context.rs @@ -9,10 +9,7 @@ use noosphere_core::{ view::Sphere, }; use noosphere_fs::SphereFs; -use noosphere_storage::{ - db::SphereDb, - interface::{KeyValueStore, Store}, -}; +use noosphere_storage::{KeyValueStore, SphereDb, Storage}; use tokio::sync::OnceCell; use ucan::crypto::{did::DidParser, KeyMaterial}; use url::Url; @@ -33,7 +30,7 @@ use super::{metadata::GATEWAY_URL, GatewaySyncStrategy}; pub struct SphereContext where K: KeyMaterial + Clone + 'static, - S: Store, + S: Storage, { sphere_identity: Did, author: Author, @@ -45,7 +42,7 @@ where impl SphereContext where K: KeyMaterial + Clone + 'static, - S: Store, + S: Storage, { pub fn new(sphere_identity: Did, author: Author, db: SphereDb) -> Self { SphereContext { diff --git a/rust/noosphere/src/sphere/storage.rs b/rust/noosphere/src/sphere/storage.rs index 89061d35d..fb1087c76 100644 --- a/rust/noosphere/src/sphere/storage.rs +++ b/rust/noosphere/src/sphere/storage.rs @@ -1,14 +1,16 @@ use std::{fmt::Display, path::PathBuf}; -use crate::platform::PlatformStorageProvider; +use crate::platform::PlatformStorage; use anyhow::Result; use noosphere_core::data::Did; +#[cfg(doc)] +use noosphere_storage::Storage; + /// [StorageLayout] represents the namespace that should be used depending on /// whether or not a sphere's DID should be included in the namespace. The enum -/// is a convenience that can be directly transformed into a -/// [noosphere_storage::interface::StorageProvider] implementation that is -/// suitable for the current platform +/// is a convenience that can be directly transformed into a [Storage] +/// implementation that is suitable for the current platform pub enum StorageLayout { Scoped(PathBuf, Did), Unscoped(PathBuf), @@ -39,16 +41,16 @@ impl From for PathBuf { #[cfg(not(target_arch = "wasm32"))] impl StorageLayout { - pub async fn to_storage_provider(&self) -> Result { - PlatformStorageProvider::new(noosphere_storage::native::NativeStorageInit::Path( - PathBuf::from(self), - )) + pub async fn to_storage_provider(&self) -> Result { + PlatformStorage::new(noosphere_storage::NativeStorageInit::Path(PathBuf::from( + self, + ))) } } #[cfg(target_arch = "wasm32")] impl StorageLayout { - pub async fn to_storage_provider(&self) -> Result { - PlatformStorageProvider::new(&self.to_string()).await + pub async fn to_storage_provider(&self) -> Result { + PlatformStorage::new(&self.to_string()).await } } diff --git a/rust/noosphere/src/sphere/sync.rs b/rust/noosphere/src/sphere/sync.rs index 522883faf..e96d6c690 100644 --- a/rust/noosphere/src/sphere/sync.rs +++ b/rust/noosphere/src/sphere/sync.rs @@ -4,7 +4,7 @@ use anyhow::{anyhow, Result}; use cid::Cid; use noosphere_api::data::{FetchParameters, FetchResponse, PushBody, PushResponse}; use noosphere_core::{data::Did, view::Sphere}; -use noosphere_storage::{db::SphereDb, interface::Store}; +use noosphere_storage::{SphereDb, Storage}; use ucan::crypto::KeyMaterial; use super::SphereContext; @@ -21,7 +21,7 @@ use super::SphereContext; pub struct GatewaySyncStrategy where K: KeyMaterial + Clone + 'static, - S: Store, + S: Storage, { key_type: PhantomData, store_type: PhantomData, @@ -30,7 +30,7 @@ where impl Default for GatewaySyncStrategy where K: KeyMaterial + Clone + 'static, - S: Store, + S: Storage, { fn default() -> Self { Self { @@ -43,7 +43,7 @@ where impl GatewaySyncStrategy where K: KeyMaterial + Clone + 'static, - S: Store, + S: Storage, { /// Synchronize a local sphere's data with the data in a gateway, and rollback /// if there is an error. @@ -115,9 +115,8 @@ where println!("Local history is already up to date..."); return Ok(( local_sphere_tip.cloned(), - counterpart_sphere_base - .ok_or_else(|| anyhow!("Counterpart sphere history is missing!"))? - .clone(), + *counterpart_sphere_base + .ok_or_else(|| anyhow!("Counterpart sphere history is missing!"))?, )); } };