From 5bb254b443035ab112b0781d654c139e987836ff Mon Sep 17 00:00:00 2001 From: Jordan Santell Date: Tue, 5 Sep 2023 11:44:44 -0700 Subject: [PATCH] chore: Rename NativeStorage -> SledStorage, WebStorage -> IndexedDbStorage. Make storage generic over gateway. --- rust/noosphere-cli/src/native/workspace.rs | 8 ++-- rust/noosphere-cli/tests/helpers/mod.rs | 10 ++-- rust/noosphere-gateway/src/authority.rs | 20 +++++--- rust/noosphere-gateway/src/route/fetch.rs | 2 +- rust/noosphere-gateway/src/route/identify.rs | 4 +- rust/noosphere-gateway/src/route/push.rs | 2 +- rust/noosphere-gateway/src/route/replicate.rs | 2 +- rust/noosphere-storage/src/helpers.rs | 13 +++-- .../implementation/{web.rs => indexed_db.rs} | 48 +++++++++---------- .../src/implementation/mod.rs | 8 ++-- .../src/implementation/{native.rs => sled.rs} | 34 ++++++------- rust/noosphere/src/platform.rs | 18 +++---- rust/noosphere/src/storage.rs | 12 ++--- 13 files changed, 93 insertions(+), 88 deletions(-) rename rust/noosphere-storage/src/implementation/{web.rs => indexed_db.rs} (77%) rename rust/noosphere-storage/src/implementation/{native.rs => sled.rs} (74%) diff --git a/rust/noosphere-cli/src/native/workspace.rs b/rust/noosphere-cli/src/native/workspace.rs index abd0a5ea2..e323129f0 100644 --- a/rust/noosphere-cli/src/native/workspace.rs +++ b/rust/noosphere-cli/src/native/workspace.rs @@ -3,11 +3,11 @@ use anyhow::{anyhow, Result}; use cid::Cid; use directories::ProjectDirs; -use noosphere::sphere::SphereContextBuilder; +use noosphere::{platform::PlatformStorage, sphere::SphereContextBuilder}; use noosphere_core::authority::Author; use noosphere_core::data::{Did, Link, LinkRecord, MemoIpld}; use noosphere_sphere::{SphereContentRead, SphereContext, SphereCursor, COUNTERPART, GATEWAY_URL}; -use noosphere_storage::{KeyValueStore, NativeStorage, SphereDb}; +use noosphere_storage::{KeyValueStore, SphereDb}; use serde_json::Value; use std::path::{Path, PathBuf}; use std::sync::Arc; @@ -24,7 +24,7 @@ use super::paths::SpherePaths; use super::render::SphereRenderer; /// The flavor of [SphereContext] used through the CLI -pub type CliSphereContext = SphereContext; +pub type CliSphereContext = SphereContext; /// Metadata about a given sphere, including the sphere ID, a [Link] /// to it and a corresponding [LinkRecord] (if one is available). @@ -68,7 +68,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/helpers/mod.rs b/rust/noosphere-cli/tests/helpers/mod.rs index e8babb3a3..a3c0f53ce 100644 --- a/rust/noosphere-cli/tests/helpers/mod.rs +++ b/rust/noosphere-cli/tests/helpers/mod.rs @@ -8,19 +8,19 @@ pub use cli::*; use anyhow::Result; use noosphere_ipfs::{IpfsStore, KuboClient}; -use noosphere_storage::{BlockStoreRetry, MemoryStore, NativeStorage, UcanStore}; +use noosphere_storage::{BlockStoreRetry, MemoryStore, UcanStore}; use std::{net::TcpListener, sync::Arc, time::Duration}; use tempfile::TempDir; use noosphere_cli::{ cli::ConfigSetCommand, commands::{key::key_create, sphere::config_set, sphere::sphere_create}, - workspace::Workspace, + workspace::{CliSphereContext, Workspace}, }; use noosphere_core::data::Did; use noosphere_gateway::{start_gateway, GatewayScope}; use noosphere_ns::{helpers::NameSystemNetwork, server::start_name_system_api_server}; -use noosphere_sphere::{HasSphereContext, SphereContext}; +use noosphere_sphere::HasSphereContext; use tokio::{sync::Mutex, task::JoinHandle}; use url::Url; @@ -222,14 +222,14 @@ impl SpherePair { } /// Returns a [SphereContext] for the client sphere. - pub async fn sphere_context(&self) -> Result>>> { + pub async fn sphere_context(&self) -> Result>> { self.client.workspace.sphere_context().await } pub async fn spawn(&self, f: F) -> Result where T: Send + 'static, - F: FnOnce(Arc>>) -> Fut + Send + 'static, + F: FnOnce(Arc>) -> Fut + Send + 'static, Fut: std::future::Future> + Send + 'static, { let context = self.sphere_context().await?; diff --git a/rust/noosphere-gateway/src/authority.rs b/rust/noosphere-gateway/src/authority.rs index 4be66c298..58f384b21 100644 --- a/rust/noosphere-gateway/src/authority.rs +++ b/rust/noosphere-gateway/src/authority.rs @@ -11,8 +11,8 @@ use axum::{ use libipld_core::cid::Cid; use noosphere_core::authority::{SphereAbility, SphereReference, SPHERE_SEMANTICS}; use noosphere_sphere::SphereContext; -use noosphere_storage::NativeStorage; +use noosphere_storage::Storage; use tokio::sync::Mutex; use ucan::{capability::CapabilityView, chain::ProofChain, store::UcanJwtStore}; @@ -23,12 +23,16 @@ use super::GatewayScope; /// represented by a UCAN. Any request handler can use a GatewayAuthority /// to test if a required capability is satisfied by the authorization /// presented by the maker of the request. -pub struct GatewayAuthority { +pub struct GatewayAuthority { proof: ProofChain, scope: GatewayScope, + marker: std::marker::PhantomData, } -impl GatewayAuthority { +impl GatewayAuthority +where + S: Storage + 'static, +{ pub fn try_authorize( &self, capability: &CapabilityView, @@ -52,16 +56,17 @@ impl GatewayAuthority { } #[async_trait] -impl FromRequestParts for GatewayAuthority +impl FromRequestParts for GatewayAuthority where - S: Send + Sync, + St: Send + Sync, + S: Storage + 'static, { type Rejection = StatusCode; - async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, state: &St) -> Result { let sphere_context = parts .extensions - .get::>>>() + .get::>>>() .ok_or_else(|| { error!("Could not find DidParser in extensions"); StatusCode::INTERNAL_SERVER_ERROR @@ -141,6 +146,7 @@ where Ok(GatewayAuthority { scope: gateway_scope.clone(), proof: proof_chain, + marker: std::marker::PhantomData, }) } } diff --git a/rust/noosphere-gateway/src/route/fetch.rs b/rust/noosphere-gateway/src/route/fetch.rs index 37231affb..28f6b295c 100644 --- a/rust/noosphere-gateway/src/route/fetch.rs +++ b/rust/noosphere-gateway/src/route/fetch.rs @@ -19,7 +19,7 @@ use crate::{authority::GatewayAuthority, GatewayScope}; #[instrument(level = "debug", skip(authority, scope, sphere_context, ipfs_client))] pub async fn fetch_route( - authority: GatewayAuthority, + authority: GatewayAuthority, Query(FetchParameters { since }): Query, Extension(scope): Extension, Extension(ipfs_client): Extension, diff --git a/rust/noosphere-gateway/src/route/identify.rs b/rust/noosphere-gateway/src/route/identify.rs index df42026cd..999ccefa1 100644 --- a/rust/noosphere-gateway/src/route/identify.rs +++ b/rust/noosphere-gateway/src/route/identify.rs @@ -8,11 +8,11 @@ use noosphere_storage::Storage; pub async fn identify_route( Extension(scope): Extension, Extension(sphere_context): Extension, - authority: GatewayAuthority, + authority: GatewayAuthority, ) -> Result where C: HasSphereContext, - S: Storage, + S: Storage + 'static, { debug!("Invoking identify route..."); diff --git a/rust/noosphere-gateway/src/route/push.rs b/rust/noosphere-gateway/src/route/push.rs index 7888314b1..a5b9b9be6 100644 --- a/rust/noosphere-gateway/src/route/push.rs +++ b/rust/noosphere-gateway/src/route/push.rs @@ -35,7 +35,7 @@ use crate::{ ) )] pub async fn push_route( - authority: GatewayAuthority, + authority: GatewayAuthority, Extension(sphere_context): Extension, Extension(gateway_scope): Extension, Extension(syndication_tx): Extension>>, diff --git a/rust/noosphere-gateway/src/route/replicate.rs b/rust/noosphere-gateway/src/route/replicate.rs index 860612b77..4f88c76f7 100644 --- a/rust/noosphere-gateway/src/route/replicate.rs +++ b/rust/noosphere-gateway/src/route/replicate.rs @@ -35,7 +35,7 @@ pub type ReplicationCarStreamBody = /// fetch from the gateway. #[instrument(level = "debug", skip(authority, scope, sphere_context,))] pub async fn replicate_route( - authority: GatewayAuthority, + authority: GatewayAuthority, // NOTE: Cannot go from string to CID via serde Path(memo_version): Path, Query(ReplicateParameters { since }): Query, diff --git a/rust/noosphere-storage/src/helpers.rs b/rust/noosphere-storage/src/helpers.rs index 0e423a831..5c531ef3c 100644 --- a/rust/noosphere-storage/src/helpers.rs +++ b/rust/noosphere-storage/src/helpers.rs @@ -2,10 +2,10 @@ use crate::Storage; use anyhow::Result; #[cfg(not(target_arch = "wasm32"))] -use crate::{NativeStorage, NativeStorageInit, NativeStore}; +use crate::{SledStorage, SledStorageInit, SledStore}; #[cfg(not(target_arch = "wasm32"))] -pub async fn make_disposable_store() -> Result { +pub async fn make_disposable_store() -> Result { let temp_dir = std::env::temp_dir(); let temp_name: String = witty_phrase_generator::WPGen::new() .with_words(3) @@ -13,16 +13,15 @@ pub async fn make_disposable_store() -> Result { .into_iter() .map(String::from) .collect(); - let db = sled::open(temp_dir.join(temp_name)).unwrap(); - let provider = NativeStorage::new(NativeStorageInit::Db(db))?; + let provider = SledStorage::new(SledStorageInit::Path(temp_dir.join(temp_name)))?; provider.get_block_store("foo").await } #[cfg(target_arch = "wasm32")] -use crate::{WebStorage, WebStore}; +use crate::{IndexedDbStorage, IndexedDbStore}; #[cfg(target_arch = "wasm32")] -pub async fn make_disposable_store() -> Result { +pub async fn make_disposable_store() -> Result { let temp_name: String = witty_phrase_generator::WPGen::new() .with_words(3) .unwrap() @@ -30,6 +29,6 @@ pub async fn make_disposable_store() -> Result { .map(|word| String::from(word)) .collect(); - let provider = WebStorage::new(&temp_name).await?; + let provider = IndexedDbStorage::new(&temp_name).await?; provider.get_block_store(crate::db::BLOCK_STORE).await } diff --git a/rust/noosphere-storage/src/implementation/web.rs b/rust/noosphere-storage/src/implementation/indexed_db.rs similarity index 77% rename from rust/noosphere-storage/src/implementation/web.rs rename to rust/noosphere-storage/src/implementation/indexed_db.rs index bbaea4ea9..7762009a5 100644 --- a/rust/noosphere-storage/src/implementation/web.rs +++ b/rust/noosphere-storage/src/implementation/indexed_db.rs @@ -12,17 +12,17 @@ use wasm_bindgen::{JsCast, JsValue}; pub const INDEXEDDB_STORAGE_VERSION: u32 = 1; #[derive(Clone)] -pub struct WebStorage { +pub struct IndexedDbStorage { db: Rc, } -impl Debug for WebStorage { +impl Debug for IndexedDbStorage { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("WebStorage").finish() + f.debug_struct("IndexedDbStorage").finish() } } -impl WebStorage { +impl IndexedDbStorage { pub async fn new(db_name: &str) -> Result { Self::configure(INDEXEDDB_STORAGE_VERSION, db_name, SPHERE_DB_STORE_NAMES).await } @@ -39,10 +39,10 @@ impl WebStorage { .await .map_err(|error| anyhow!("{:?}", error))?; - Ok(WebStorage { db: Rc::new(db) }) + Ok(IndexedDbStorage { db: Rc::new(db) }) } - async fn get_store(&self, name: &str) -> Result { + async fn get_store(&self, name: &str) -> Result { if self .db .store_names() @@ -53,7 +53,7 @@ impl WebStorage { return Err(anyhow!("No such store named {}", name)); } - Ok(WebStore { + Ok(IndexedDbStore { db: self.db.clone(), store_name: name.to_string(), }) @@ -61,10 +61,10 @@ impl WebStorage { } #[async_trait(?Send)] -impl Storage for WebStorage { - type BlockStore = WebStore; +impl Storage for IndexedDbStorage { + type BlockStore = IndexedDbStore; - type KeyValueStore = WebStore; + type KeyValueStore = IndexedDbStore; async fn get_block_store(&self, name: &str) -> Result { self.get_store(name).await @@ -76,12 +76,12 @@ impl Storage for WebStorage { } #[derive(Clone)] -pub struct WebStore { +pub struct IndexedDbStore { db: Rc, store_name: String, } -impl WebStore { +impl IndexedDbStore { fn start_transaction(&self, mode: TransactionMode) -> Result<(IdbStore, Transaction)> { let tx = self .db @@ -116,7 +116,7 @@ impl WebStore { } async fn read(key: &JsValue, store: &IdbStore) -> Result>> { - Ok(match WebStore::contains(&key, &store).await? { + Ok(match IndexedDbStore::contains(&key, &store).await? { true => Some( store .get(&key) @@ -132,14 +132,14 @@ impl WebStore { } #[async_trait(?Send)] -impl Store for WebStore { +impl Store for IndexedDbStore { async fn read(&self, key: &[u8]) -> Result>> { let (store, tx) = self.start_transaction(TransactionMode::ReadOnly)?; - let key = WebStore::bytes_to_typed_array(key)?; + let key = IndexedDbStore::bytes_to_typed_array(key)?; - let maybe_dag = WebStore::read(&key, &store).await?; + let maybe_dag = IndexedDbStore::read(&key, &store).await?; - WebStore::finish_transaction(tx).await?; + IndexedDbStore::finish_transaction(tx).await?; Ok(maybe_dag) } @@ -147,17 +147,17 @@ impl Store for WebStore { async fn write(&mut self, key: &[u8], bytes: &[u8]) -> Result>> { let (store, tx) = self.start_transaction(TransactionMode::ReadWrite)?; - let key = WebStore::bytes_to_typed_array(key)?; - let value = WebStore::bytes_to_typed_array(bytes)?; + let key = IndexedDbStore::bytes_to_typed_array(key)?; + let value = IndexedDbStore::bytes_to_typed_array(bytes)?; - let old_bytes = WebStore::read(&key, &store).await?; + let old_bytes = IndexedDbStore::read(&key, &store).await?; store .put(&value, Some(&key)) .await .map_err(|error| anyhow!("{:?}", error))?; - WebStore::finish_transaction(tx).await?; + IndexedDbStore::finish_transaction(tx).await?; Ok(old_bytes) } @@ -165,16 +165,16 @@ impl Store for WebStore { async fn remove(&mut self, key: &[u8]) -> Result>> { let (store, tx) = self.start_transaction(TransactionMode::ReadWrite)?; - let key = WebStore::bytes_to_typed_array(key)?; + let key = IndexedDbStore::bytes_to_typed_array(key)?; - let old_value = WebStore::read(&key, &store).await?; + let old_value = IndexedDbStore::read(&key, &store).await?; store .delete(&key) .await .map_err(|error| anyhow!("{:?}", error))?; - WebStore::finish_transaction(tx).await?; + IndexedDbStore::finish_transaction(tx).await?; Ok(old_value) } diff --git a/rust/noosphere-storage/src/implementation/mod.rs b/rust/noosphere-storage/src/implementation/mod.rs index f45005436..f55567d7a 100644 --- a/rust/noosphere-storage/src/implementation/mod.rs +++ b/rust/noosphere-storage/src/implementation/mod.rs @@ -5,13 +5,13 @@ pub use memory::*; pub use tracking::*; #[cfg(not(target_arch = "wasm32"))] -mod native; +mod sled; #[cfg(not(target_arch = "wasm32"))] -pub use native::*; +pub use self::sled::*; #[cfg(target_arch = "wasm32")] -mod web; +mod indexed_db; #[cfg(target_arch = "wasm32")] -pub use web::*; +pub use indexed_db::*; diff --git a/rust/noosphere-storage/src/implementation/native.rs b/rust/noosphere-storage/src/implementation/sled.rs similarity index 74% rename from rust/noosphere-storage/src/implementation/native.rs rename to rust/noosphere-storage/src/implementation/sled.rs index 62bf1658c..f6a7c785b 100644 --- a/rust/noosphere-storage/src/implementation/native.rs +++ b/rust/noosphere-storage/src/implementation/sled.rs @@ -7,39 +7,39 @@ use anyhow::Result; use async_trait::async_trait; use sled::{Db, Tree}; -pub enum NativeStorageInit { +pub enum SledStorageInit { Path(PathBuf), Db(Db), } #[derive(Clone, Debug)] -pub struct NativeStorage { +pub struct SledStorage { db: Db, } -impl NativeStorage { - pub fn new(init: NativeStorageInit) -> Result { +impl SledStorage { + pub fn new(init: SledStorageInit) -> Result { let db: Db = match init { - NativeStorageInit::Path(path) => { + SledStorageInit::Path(path) => { std::fs::create_dir_all(&path)?; sled::open(path)? } - NativeStorageInit::Db(db) => db, + SledStorageInit::Db(db) => db, }; - Ok(NativeStorage { db }) + Ok(SledStorage { db }) } - async fn get_store(&self, name: &str) -> Result { - Ok(NativeStore::new(&self.db.open_tree(name)?)) + async fn get_store(&self, name: &str) -> Result { + Ok(SledStore::new(&self.db.open_tree(name)?)) } } #[async_trait] -impl Storage for NativeStorage { - type BlockStore = NativeStore; +impl Storage for SledStorage { + type BlockStore = SledStore; - type KeyValueStore = NativeStore; + type KeyValueStore = SledStore; async fn get_block_store(&self, name: &str) -> Result { self.get_store(name).await @@ -51,18 +51,18 @@ impl Storage for NativeStorage { } #[derive(Clone)] -pub struct NativeStore { +pub struct SledStore { db: Tree, } -impl NativeStore { +impl SledStore { pub fn new(db: &Tree) -> Self { - NativeStore { db: db.clone() } + SledStore { db: db.clone() } } } #[async_trait] -impl Store for NativeStore { +impl Store for SledStore { async fn read(&self, key: &[u8]) -> Result>> { Ok(self.db.get(key)?.map(|entry| entry.to_vec())) } @@ -94,7 +94,7 @@ impl Store for NativeStore { } } -impl Drop for NativeStorage { +impl Drop for SledStorage { fn drop(&mut self) { let _ = self.db.flush(); } diff --git a/rust/noosphere/src/platform.rs b/rust/noosphere/src/platform.rs index 3b1fa0839..4579bd8fb 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::NativeStorage; + use noosphere_storage::SledStorage; use ucan_key_support::ed25519::Ed25519KeyMaterial; use crate::key::InsecureKeyStorage; @@ -19,13 +19,13 @@ mod inner { pub type PlatformKeyStorage = InsecureKeyStorage; #[cfg(not(feature = "ipfs-storage"))] - pub type PlatformStorage = NativeStorage; + pub type PlatformStorage = SledStorage; #[cfg(feature = "ipfs-storage")] use noosphere_ipfs::{IpfsStorage, KuboClient}; #[cfg(feature = "ipfs-storage")] - pub type PlatformStorage = IpfsStorage; + pub type PlatformStorage = IpfsStorage; #[cfg(test)] use anyhow::Result; @@ -59,14 +59,14 @@ mod inner { pub type PlatformKeyMaterial = Arc; pub type PlatformKeyStorage = WebCryptoKeyStorage; - use noosphere_storage::WebStorage; + use noosphere_storage::IndexedDbStorage; #[cfg(feature = "ipfs-storage")] pub type PlatformStorage = - noosphere_ipfs::IpfsStorage; + noosphere_ipfs::IpfsStorage; #[cfg(not(feature = "ipfs-storage"))] - pub type PlatformStorage = WebStorage; + pub type PlatformStorage = IndexedDbStorage; #[cfg(test)] use anyhow::Result; @@ -104,7 +104,7 @@ mod inner { )) ))] mod inner { - use noosphere_storage::NativeStorage; + use noosphere_storage::SledStorage; use ucan_key_support::ed25519::Ed25519KeyMaterial; use crate::key::InsecureKeyStorage; @@ -113,13 +113,13 @@ mod inner { pub type PlatformKeyStorage = InsecureKeyStorage; #[cfg(not(feature = "ipfs-storage"))] - pub type PlatformStorage = NativeStorage; + pub type PlatformStorage = SledStorage; #[cfg(feature = "ipfs-storage")] use noosphere_ipfs::{IpfsStorage, KuboClient}; #[cfg(feature = "ipfs-storage")] - pub type PlatformStorage = IpfsStorage; + pub type PlatformStorage = IpfsStorage; #[cfg(test)] use anyhow::Result; diff --git a/rust/noosphere/src/storage.rs b/rust/noosphere/src/storage.rs index 449dbaa12..aed049645 100644 --- a/rust/noosphere/src/storage.rs +++ b/rust/noosphere/src/storage.rs @@ -42,22 +42,22 @@ impl From for PathBuf { } #[cfg(not(target_arch = "wasm32"))] -use noosphere_storage::{NativeStorage, NativeStorageInit}; +use noosphere_storage::{SledStorage, SledStorageInit}; #[cfg(not(target_arch = "wasm32"))] impl StorageLayout { - pub async fn to_storage(&self) -> Result { - NativeStorage::new(NativeStorageInit::Path(PathBuf::from(self))) + pub async fn to_storage(&self) -> Result { + SledStorage::new(SledStorageInit::Path(PathBuf::from(self))) } } #[cfg(target_arch = "wasm32")] -use noosphere_storage::WebStorage; +use noosphere_storage::IndexedDbStorage; #[cfg(target_arch = "wasm32")] impl StorageLayout { - pub async fn to_storage(&self) -> Result { - WebStorage::new(&self.to_string()).await + pub async fn to_storage(&self) -> Result { + IndexedDbStorage::new(&self.to_string()).await } }