From 0cec8ba8991edf226997df2b429f3fdb0285620d Mon Sep 17 00:00:00 2001 From: yjh Date: Thu, 22 Aug 2024 19:31:18 +0800 Subject: [PATCH] feat: align `fn root` semantics; fix missing root for some services; rm duplicated normalize ops (#5035) * feat: align `fn root` semantics; fix missing root for some services * typo and fmt * chore * fix name * align more roots * fix * fix * fix * fix --- core/src/raw/adapters/kv/backend.rs | 11 +++++++++-- core/src/services/atomicserver/backend.rs | 2 +- core/src/services/azblob/backend.rs | 8 +++++--- core/src/services/azdls/backend.rs | 8 +++++--- core/src/services/azfile/backend.rs | 8 +++++--- core/src/services/cloudflare_kv/backend.rs | 11 +++++++---- core/src/services/cos/backend.rs | 8 +++++--- core/src/services/d1/backend.rs | 11 +++++++---- core/src/services/dbfs/backend.rs | 8 +++++--- core/src/services/dropbox/builder.rs | 7 ++++++- core/src/services/etcd/backend.rs | 11 +++++++---- core/src/services/foundationdb/backend.rs | 2 +- core/src/services/fs/backend.rs | 8 +++++--- core/src/services/gcs/backend.rs | 8 +++++--- core/src/services/gdrive/builder.rs | 7 ++++++- core/src/services/ghac/backend.rs | 8 +++++--- core/src/services/gridfs/backend.rs | 22 +++++++++++++++++----- core/src/services/huggingface/backend.rs | 9 ++++++--- core/src/services/ipfs/backend.rs | 8 +++++--- core/src/services/libsql/backend.rs | 11 +++++++---- core/src/services/memcached/backend.rs | 11 +++++++---- core/src/services/mongodb/backend.rs | 22 ++++++++++++++++------ core/src/services/mysql/backend.rs | 11 +++++++---- core/src/services/obs/backend.rs | 8 +++++--- core/src/services/onedrive/builder.rs | 7 ++++++- core/src/services/postgresql/backend.rs | 11 +++++++---- core/src/services/redis/backend.rs | 13 ++++++++----- core/src/services/rocksdb/backend.rs | 19 +++++++++++++++---- core/src/services/sled/backend.rs | 9 +++++++-- core/src/services/sqlite/backend.rs | 11 +++++++---- core/src/services/surrealdb/backend.rs | 11 +++++++---- core/src/services/swift/backend.rs | 8 +++++--- core/src/services/webhdfs/backend.rs | 8 +++++--- 33 files changed, 221 insertions(+), 104 deletions(-) diff --git a/core/src/raw/adapters/kv/backend.rs b/core/src/raw/adapters/kv/backend.rs index 55de6b5c2c9..a4a878924c7 100644 --- a/core/src/raw/adapters/kv/backend.rs +++ b/core/src/raw/adapters/kv/backend.rs @@ -50,8 +50,15 @@ where } /// Configure root within this backend. - pub fn with_root(mut self, root: &str) -> Self { - self.root = normalize_root(root); + pub fn with_root(self, root: &str) -> Self { + self.with_normalized_root(normalize_root(root)) + } + + /// Configure root within this backend. + /// + /// This method assumes root is normalized. + pub(crate) fn with_normalized_root(mut self, root: String) -> Self { + self.root = root; self } } diff --git a/core/src/services/atomicserver/backend.rs b/core/src/services/atomicserver/backend.rs index eb61f612832..e6e415674ba 100644 --- a/core/src/services/atomicserver/backend.rs +++ b/core/src/services/atomicserver/backend.rs @@ -155,7 +155,7 @@ impl Builder for AtomicserverBuilder { .with_context("service", Scheme::Atomicserver) })?, }) - .with_root(&root)) + .with_normalized_root(root)) } } diff --git a/core/src/services/azblob/backend.rs b/core/src/services/azblob/backend.rs index d97f5b84053..ad59f525418 100644 --- a/core/src/services/azblob/backend.rs +++ b/core/src/services/azblob/backend.rs @@ -150,9 +150,11 @@ impl AzblobBuilder { /// /// All operations will happen under this root. pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()) - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; self } diff --git a/core/src/services/azdls/backend.rs b/core/src/services/azdls/backend.rs index baa68ae14f5..0af18e2819d 100644 --- a/core/src/services/azdls/backend.rs +++ b/core/src/services/azdls/backend.rs @@ -113,9 +113,11 @@ impl AzdlsBuilder { /// /// All operations will happen under this root. pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()) - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; self } diff --git a/core/src/services/azfile/backend.rs b/core/src/services/azfile/backend.rs index 5390e025569..9f6d386b35a 100644 --- a/core/src/services/azfile/backend.rs +++ b/core/src/services/azfile/backend.rs @@ -111,9 +111,11 @@ impl AzfileBuilder { /// /// All operations will happen under this root. pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()) - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; self } diff --git a/core/src/services/cloudflare_kv/backend.rs b/core/src/services/cloudflare_kv/backend.rs index 7184ce0e9ef..f97bca593a5 100644 --- a/core/src/services/cloudflare_kv/backend.rs +++ b/core/src/services/cloudflare_kv/backend.rs @@ -115,9 +115,12 @@ impl CloudflareKvBuilder { /// Set the root within this backend. pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()) - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } } @@ -175,7 +178,7 @@ impl Builder for CloudflareKvBuilder { client, url_prefix, }) - .with_root(&root)) + .with_normalized_root(root)) } } diff --git a/core/src/services/cos/backend.rs b/core/src/services/cos/backend.rs index a900aba741e..478aecb3860 100644 --- a/core/src/services/cos/backend.rs +++ b/core/src/services/cos/backend.rs @@ -97,9 +97,11 @@ impl CosBuilder { /// /// All operations will happen under this root. pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()) - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; self } diff --git a/core/src/services/d1/backend.rs b/core/src/services/d1/backend.rs index e6aaa6da4e5..c30b622a3a0 100644 --- a/core/src/services/d1/backend.rs +++ b/core/src/services/d1/backend.rs @@ -129,9 +129,12 @@ impl D1Builder { /// /// default: "/" pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_owned()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } @@ -231,7 +234,7 @@ impl Builder for D1Builder { key_field, value_field, }) - .with_root(&root)) + .with_normalized_root(root)) } } diff --git a/core/src/services/dbfs/backend.rs b/core/src/services/dbfs/backend.rs index 542722a3e0d..1f5b16ff37a 100644 --- a/core/src/services/dbfs/backend.rs +++ b/core/src/services/dbfs/backend.rs @@ -87,9 +87,11 @@ impl DbfsBuilder { /// /// All operations will happen under this root. pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()) - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; self } diff --git a/core/src/services/dropbox/builder.rs b/core/src/services/dropbox/builder.rs index bef4e2421e5..5b73de42003 100644 --- a/core/src/services/dropbox/builder.rs +++ b/core/src/services/dropbox/builder.rs @@ -88,7 +88,12 @@ impl DropboxBuilder { /// /// Default to `/` if not set. pub fn root(mut self, root: &str) -> Self { - self.config.root = Some(root.to_string()); + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } diff --git a/core/src/services/etcd/backend.rs b/core/src/services/etcd/backend.rs index 3344a951cf0..fa7fb348207 100644 --- a/core/src/services/etcd/backend.rs +++ b/core/src/services/etcd/backend.rs @@ -159,9 +159,12 @@ impl EtcdBuilder { /// /// default: "/" pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_owned()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } @@ -246,7 +249,7 @@ impl Builder for EtcdBuilder { client, options, }) - .with_root(root.as_str())) + .with_normalized_root(root)) } } diff --git a/core/src/services/foundationdb/backend.rs b/core/src/services/foundationdb/backend.rs index 2a83bffdeb3..553b6a06087 100644 --- a/core/src/services/foundationdb/backend.rs +++ b/core/src/services/foundationdb/backend.rs @@ -113,7 +113,7 @@ impl Builder for FoundationdbBuilder { .as_str(), ); - Ok(FoundationdbBackend::new(Adapter { db, _network }).with_root(&root)) + Ok(FoundationdbBackend::new(Adapter { db, _network }).with_normalized_root(root)) } } diff --git a/core/src/services/fs/backend.rs b/core/src/services/fs/backend.rs index 151e35aef6c..af1f163187c 100644 --- a/core/src/services/fs/backend.rs +++ b/core/src/services/fs/backend.rs @@ -62,9 +62,11 @@ pub struct FsBuilder { impl FsBuilder { /// Set root for backend. pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; self } diff --git a/core/src/services/gcs/backend.rs b/core/src/services/gcs/backend.rs index 8861b22202f..df3fab369f5 100644 --- a/core/src/services/gcs/backend.rs +++ b/core/src/services/gcs/backend.rs @@ -124,9 +124,11 @@ impl Debug for GcsBuilder { impl GcsBuilder { /// set the working directory root of backend pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()) - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; self } diff --git a/core/src/services/gdrive/builder.rs b/core/src/services/gdrive/builder.rs index fc3b9b8385c..3628488b819 100644 --- a/core/src/services/gdrive/builder.rs +++ b/core/src/services/gdrive/builder.rs @@ -92,7 +92,12 @@ impl Debug for GdriveBuilder { impl GdriveBuilder { /// Set root path of GoogleDrive folder. pub fn root(mut self, root: &str) -> Self { - self.config.root = Some(root.to_string()); + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } diff --git a/core/src/services/ghac/backend.rs b/core/src/services/ghac/backend.rs index 4698de168f3..5a6fc305451 100644 --- a/core/src/services/ghac/backend.rs +++ b/core/src/services/ghac/backend.rs @@ -117,9 +117,11 @@ pub struct GhacBuilder { impl GhacBuilder { /// set the working directory root of backend pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()) - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; self } diff --git a/core/src/services/gridfs/backend.rs b/core/src/services/gridfs/backend.rs index 6c36776fa42..7abfa0d7976 100644 --- a/core/src/services/gridfs/backend.rs +++ b/core/src/services/gridfs/backend.rs @@ -30,7 +30,7 @@ use tokio::sync::OnceCell; use crate::raw::adapters::kv; use crate::raw::new_std_io_error; -use crate::raw::Access; +use crate::raw::*; use crate::*; /// Config for Grid file system support. @@ -114,9 +114,12 @@ impl GridFsBuilder { /// /// default: "/" pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_owned()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } @@ -176,13 +179,22 @@ impl Builder for GridFsBuilder { }; let chunk_size = self.config.chunk_size.unwrap_or(255); + let root = normalize_root( + self.config + .root + .clone() + .unwrap_or_else(|| "/".to_string()) + .as_str(), + ); + Ok(GridFsBackend::new(Adapter { connection_string: conn, database, bucket, chunk_size, bucket_instance: OnceCell::new(), - })) + }) + .with_normalized_root(root)) } } diff --git a/core/src/services/huggingface/backend.rs b/core/src/services/huggingface/backend.rs index 712c895edf4..1e974215433 100644 --- a/core/src/services/huggingface/backend.rs +++ b/core/src/services/huggingface/backend.rs @@ -159,9 +159,12 @@ impl HuggingfaceBuilder { /// /// All operations will happen under this root. pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } diff --git a/core/src/services/ipfs/backend.rs b/core/src/services/ipfs/backend.rs index 3eeda15b972..5589db79a99 100644 --- a/core/src/services/ipfs/backend.rs +++ b/core/src/services/ipfs/backend.rs @@ -70,9 +70,11 @@ impl IpfsBuilder { /// - `/ipfs/bafybeibozpulxtpv5nhfa2ue3dcjx23ndh3gwr5vwllk7ptoyfwnfjjr4q/` (IPFS with CID v1) /// - `/ipns/opendal.apache.org/` (IPNS) pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()) - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; self } diff --git a/core/src/services/libsql/backend.rs b/core/src/services/libsql/backend.rs index 70bdfb92192..8df0c942663 100644 --- a/core/src/services/libsql/backend.rs +++ b/core/src/services/libsql/backend.rs @@ -137,9 +137,12 @@ impl LibsqlBuilder { /// /// default: "/" pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } @@ -215,7 +218,7 @@ impl Builder for LibsqlBuilder { key_field, value_field, }) - .with_root(&root)) + .with_normalized_root(root)) } } diff --git a/core/src/services/memcached/backend.rs b/core/src/services/memcached/backend.rs index 4f516bfc826..2e8526c0cbf 100644 --- a/core/src/services/memcached/backend.rs +++ b/core/src/services/memcached/backend.rs @@ -78,9 +78,12 @@ impl MemcachedBuilder { /// /// default: "/" pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_owned()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } @@ -172,7 +175,7 @@ impl Builder for MemcachedBuilder { conn, default_ttl: self.config.default_ttl, }) - .with_root(&root)) + .with_normalized_root(root)) } } diff --git a/core/src/services/mongodb/backend.rs b/core/src/services/mongodb/backend.rs index d700b08d382..8c7cb893c15 100644 --- a/core/src/services/mongodb/backend.rs +++ b/core/src/services/mongodb/backend.rs @@ -27,7 +27,7 @@ use serde::Serialize; use tokio::sync::OnceCell; use crate::raw::adapters::kv; -use crate::raw::Access; +use crate::raw::*; use crate::*; /// Config for Mongodb service support. @@ -114,9 +114,12 @@ impl MongodbBuilder { /// /// default: "/" pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_owned()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } @@ -195,7 +198,13 @@ impl Builder for MongodbBuilder { Some(v) => v.clone(), None => "value".to_string(), }; - + let root = normalize_root( + self.config + .root + .clone() + .unwrap_or_else(|| "/".to_string()) + .as_str(), + ); Ok(MongodbBackend::new(Adapter { connection_string: conn, database, @@ -203,7 +212,8 @@ impl Builder for MongodbBuilder { collection_instance: OnceCell::new(), key_field, value_field, - })) + }) + .with_normalized_root(root)) } } diff --git a/core/src/services/mysql/backend.rs b/core/src/services/mysql/backend.rs index e3bd1f02dc0..cdf9c90b689 100644 --- a/core/src/services/mysql/backend.rs +++ b/core/src/services/mysql/backend.rs @@ -108,9 +108,12 @@ impl MysqlBuilder { /// /// default: "/" pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } @@ -195,7 +198,7 @@ impl Builder for MysqlBuilder { key_field, value_field, }) - .with_root(&root)) + .with_normalized_root(root)) } } diff --git a/core/src/services/obs/backend.rs b/core/src/services/obs/backend.rs index 071a0cda016..20433c8854c 100644 --- a/core/src/services/obs/backend.rs +++ b/core/src/services/obs/backend.rs @@ -97,9 +97,11 @@ impl ObsBuilder { /// /// All operations will happen under this root. pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()) - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; self } diff --git a/core/src/services/onedrive/builder.rs b/core/src/services/onedrive/builder.rs index 4f0ae806d64..5b3fb36739b 100644 --- a/core/src/services/onedrive/builder.rs +++ b/core/src/services/onedrive/builder.rs @@ -85,7 +85,12 @@ impl OnedriveBuilder { /// Set root path of OneDrive folder. pub fn root(mut self, root: &str) -> Self { - self.config.root = Some(root.to_string()); + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } diff --git a/core/src/services/postgresql/backend.rs b/core/src/services/postgresql/backend.rs index fa053562a75..e049eae0d1a 100644 --- a/core/src/services/postgresql/backend.rs +++ b/core/src/services/postgresql/backend.rs @@ -133,9 +133,12 @@ impl PostgresqlBuilder { /// /// default: "/" pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_owned()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } @@ -219,7 +222,7 @@ impl Builder for PostgresqlBuilder { key_field, value_field, }) - .with_root(&root)) + .with_normalized_root(root)) } } diff --git a/core/src/services/redis/backend.rs b/core/src/services/redis/backend.rs index 9f840f3ca73..59e3e09571a 100644 --- a/core/src/services/redis/backend.rs +++ b/core/src/services/redis/backend.rs @@ -191,9 +191,12 @@ impl RedisBuilder { /// /// default: "/" pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_owned()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } } @@ -234,7 +237,7 @@ impl Builder for RedisBuilder { conn, default_ttl: self.config.default_ttl, }) - .with_root(&root)) + .with_normalized_root(root)) } else { let endpoint = self .config @@ -259,7 +262,7 @@ impl Builder for RedisBuilder { conn, default_ttl: self.config.default_ttl, }) - .with_root(&root)) + .with_normalized_root(root)) } } } diff --git a/core/src/services/rocksdb/backend.rs b/core/src/services/rocksdb/backend.rs index 130b785bbdf..e117b41aaee 100644 --- a/core/src/services/rocksdb/backend.rs +++ b/core/src/services/rocksdb/backend.rs @@ -67,9 +67,12 @@ impl RocksdbBuilder { /// /// default: "/" pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_owned()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } } @@ -90,7 +93,15 @@ impl Builder for RocksdbBuilder { .set_source(e) })?; - Ok(RocksdbBackend::new(Adapter { db: Arc::new(db) })) + let root = normalize_root( + self.config + .root + .clone() + .unwrap_or_else(|| "/".to_string()) + .as_str(), + ); + + Ok(RocksdbBackend::new(Adapter { db: Arc::new(db) }).with_normalized_root(root)) } } diff --git a/core/src/services/sled/backend.rs b/core/src/services/sled/backend.rs index e04488a15a0..b100c4e6877 100644 --- a/core/src/services/sled/backend.rs +++ b/core/src/services/sled/backend.rs @@ -87,8 +87,13 @@ impl SledBuilder { } /// Set the root for sled. - pub fn root(mut self, path: &str) -> Self { - self.config.root = Some(path.into()); + pub fn root(mut self, root: &str) -> Self { + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } diff --git a/core/src/services/sqlite/backend.rs b/core/src/services/sqlite/backend.rs index 1229ca2eaa8..4e9538509cb 100644 --- a/core/src/services/sqlite/backend.rs +++ b/core/src/services/sqlite/backend.rs @@ -121,9 +121,12 @@ impl SqliteBuilder { /// /// default: "/" pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_owned()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } @@ -204,7 +207,7 @@ impl Builder for SqliteBuilder { key_field, value_field, }) - .with_root(&root)) + .with_normalized_root(root)) } } diff --git a/core/src/services/surrealdb/backend.rs b/core/src/services/surrealdb/backend.rs index b7482f6cdfa..b1129b40366 100644 --- a/core/src/services/surrealdb/backend.rs +++ b/core/src/services/surrealdb/backend.rs @@ -116,9 +116,12 @@ impl SurrealdbBuilder { /// /// default: "/" pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()); - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; + self } @@ -251,7 +254,7 @@ impl Builder for SurrealdbBuilder { key_field, value_field, }) - .with_root(&root)) + .with_normalized_root(root)) } } diff --git a/core/src/services/swift/backend.rs b/core/src/services/swift/backend.rs index e0170db0b94..89a1f83d199 100644 --- a/core/src/services/swift/backend.rs +++ b/core/src/services/swift/backend.rs @@ -123,9 +123,11 @@ impl SwiftBuilder { /// /// All operations will happen under this root. pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()) - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; self } diff --git a/core/src/services/webhdfs/backend.rs b/core/src/services/webhdfs/backend.rs index 2e32b03f7b9..0354f3659d4 100644 --- a/core/src/services/webhdfs/backend.rs +++ b/core/src/services/webhdfs/backend.rs @@ -100,9 +100,11 @@ impl WebhdfsBuilder { /// /// The root will be automatically created if not exists. pub fn root(mut self, root: &str) -> Self { - if !root.is_empty() { - self.config.root = Some(root.to_string()) - } + self.config.root = if root.is_empty() { + None + } else { + Some(root.to_string()) + }; self }