Skip to content

Commit

Permalink
refactor(core!): Make service builder takes ownership (#4922)
Browse files Browse the repository at this point in the history
* refactor(!): Make service builder takes ownership

Signed-off-by: Xuanwo <github@xuanwo.io>

* Fix comments

Signed-off-by: Xuanwo <github@xuanwo.io>

* FIx tests

Signed-off-by: Xuanwo <github@xuanwo.io>

* Fix tests

Signed-off-by: Xuanwo <github@xuanwo.io>

---------

Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo authored Jul 24, 2024
1 parent db66cb0 commit c7cecc4
Show file tree
Hide file tree
Showing 139 changed files with 899 additions and 984 deletions.
3 changes: 1 addition & 2 deletions bin/oay/src/bin/oay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ async fn webdav() -> Result<()> {
},
};

let mut builder = Fs::default();
builder.root("/tmp");
let builder = Fs::default().root("/tmp");

let op = Operator::new(builder)?.finish();

Expand Down
3 changes: 1 addition & 2 deletions bin/oay/src/bin/webdav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ async fn main() -> Result<()> {
},
};

let mut builder = Fs::default();
builder.root("/tmp");
let builder = Fs::default().root("/tmp");

let op = Operator::new(builder)?.finish();

Expand Down
4 changes: 2 additions & 2 deletions bin/oli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ impl Config {

let filename = match fp_str.split_once(['/', '\\']) {
Some((base, filename)) => {
fs_builder.root(if base.is_empty() { "/" } else { base });
fs_builder = fs_builder.root(if base.is_empty() { "/" } else { base });
filename
}
_ => {
fs_builder.root(".");
fs_builder = fs_builder.root(".");
s
}
};
Expand Down
10 changes: 2 additions & 8 deletions bindings/c/include/opendal.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ typedef struct BlockingLister BlockingLister;
*
* fn main() -> Result<()> {
* // Create fs backend builder.
* let mut builder = Fs::default();
* // Set the root for fs, all operations will happen under this root.
* //
* // NOTE: the root must be absolute path.
* builder.root("/tmp");
* let builder = Fs::default().root("/tmp");
*
* // Build an `BlockingOperator` to start operating the storage.
* let _: BlockingOperator = Operator::new(builder)?.finish().blocking();
Expand All @@ -136,9 +132,7 @@ typedef struct BlockingLister BlockingLister;
*
* async fn test() -> Result<()> {
* // Create fs backend builder.
* let mut builder = S3::default();
* builder.bucket("test");
* builder.region("us-east-1");
* let mut builder = S3::default().bucket("test").region("us-east-1");
*
* // Build an `BlockingOperator` with blocking layer to start operating the storage.
* let _: BlockingOperator = Operator::new(builder)?
Expand Down
3 changes: 1 addition & 2 deletions core/benches/vs_fs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ fn main() {
}

fn bench_vs_fs(c: &mut Criterion) {
let mut cfg = services::Fs::default();
cfg.root("/tmp/opendal/");
let cfg = services::Fs::default().root("/tmp/opendal/");
let op = Operator::new(cfg).unwrap().finish().blocking();

let mut group = c.benchmark_group("read");
Expand Down
12 changes: 6 additions & 6 deletions core/benches/vs_s3/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ fn main() {
let region = env::var("OPENDAL_S3_REGION").unwrap();

// Init OpenDAL Operator.
let mut cfg = services::S3::default();
cfg.endpoint(&endpoint);
cfg.access_key_id(&access_key);
cfg.secret_access_key(&secret_key);
cfg.bucket(&bucket);
cfg.region(&region);
let cfg = services::S3::default()
.endpoint(&endpoint)
.access_key_id(&access_key)
.secret_access_key(&secret_key)
.bucket(&bucket)
.region(&region);
let op = Operator::new(cfg).unwrap().finish();

// Init AWS S3 SDK.
Expand Down
4 changes: 2 additions & 2 deletions core/edge/file_write_on_full_disk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use rand::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
let mut builder = Fs::default();
builder.root(&env::var("OPENDAL_FS_ROOT").expect("root must be set for this test"));
let builder =
Fs::default().root(&env::var("OPENDAL_FS_ROOT").expect("root must be set for this test"));
let op = Operator::new(builder)?.finish();

let size = thread_rng().gen_range(512 * 1024 + 1..4 * 1024 * 1024);
Expand Down
12 changes: 6 additions & 6 deletions core/edge/s3_read_on_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub async fn hello_world() -> String {
let mut cfg = S3::default();
cfg.endpoint("http://127.0.0.1:9000");
cfg.access_key_id("minioadmin");
cfg.secret_access_key("minioadmin");
cfg.bucket("test");
cfg.region("us-east-1");
let cfg = S3::default()
.endpoint("http://127.0.0.1:9000")
.access_key_id("minioadmin")
.secret_access_key("minioadmin")
.bucket("test")
.region("us-east-1");

let op = Operator::new(cfg).unwrap().finish();
op.write(
Expand Down
12 changes: 3 additions & 9 deletions core/src/layers/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ use crate::*;
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// // Create fs backend builder.
/// let mut builder = S3::default();
/// builder.bucket("test");
/// builder.region("us-east-1");
/// let mut builder = S3::default().bucket("test").region("us-east-1");
///
/// // Build an `BlockingOperator` with blocking layer to start operating the storage.
/// let _: BlockingOperator = Operator::new(builder)?
Expand Down Expand Up @@ -79,9 +77,7 @@ use crate::*;
///
/// fn blocking_fn() -> Result<BlockingOperator> {
/// // Create fs backend builder.
/// let mut builder = S3::default();
/// builder.bucket("test");
/// builder.region("us-east-1");
/// let mut builder = S3::default().bucket("test").region("us-east-1");
///
/// let handle = tokio::runtime::Handle::try_current().unwrap();
/// let _guard = handle.enter();
Expand Down Expand Up @@ -119,9 +115,7 @@ use crate::*;
///
/// fn main() -> Result<()> {
/// // Create fs backend builder.
/// let mut builder = S3::default();
/// builder.bucket("test");
/// builder.region("us-east-1");
/// let mut builder = S3::default().bucket("test").region("us-east-1");
///
/// // Fetch the `EnterGuard` from global runtime.
/// let _guard = RUNTIME.enter();
Expand Down
9 changes: 3 additions & 6 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
//!
//! fn main() -> Result<()> {
//! // Pick a builder and configure it.
//! let mut builder = services::S3::default();
//! builder.bucket("test");
//! let mut builder = services::S3::default().bucket("test");
//!
//! // Init an operator
//! let op = Operator::new(builder)?.finish();
Expand All @@ -73,8 +72,7 @@
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! // Pick a builder and configure it.
//! let mut builder = services::S3::default();
//! builder.bucket("test");
//! let mut builder = services::S3::default().bucket("test");
//!
//! // Init an operator
//! let op = Operator::new(builder)?
Expand Down Expand Up @@ -107,8 +105,7 @@
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! // Pick a builder and configure it.
//! let mut builder = services::S3::default();
//! builder.bucket("test");
//! let mut builder = services::S3::default().bucket("test");
//!
//! // Init an operator
//! let op = Operator::new(builder)?
Expand Down
14 changes: 7 additions & 7 deletions core/src/services/aliyun_drive/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl AliyunDriveBuilder {
/// Set the root of this backend.
///
/// All operations will happen under this root.
pub fn root(&mut self, root: &str) -> &mut Self {
pub fn root(mut self, root: &str) -> Self {
self.config.root = if root.is_empty() {
None
} else {
Expand All @@ -129,35 +129,35 @@ impl AliyunDriveBuilder {
}

/// Set access_token of this backend.
pub fn access_token(&mut self, access_token: &str) -> &mut Self {
pub fn access_token(mut self, access_token: &str) -> Self {
self.config.access_token = Some(access_token.to_string());

self
}

/// Set client_id of this backend.
pub fn client_id(&mut self, client_id: &str) -> &mut Self {
pub fn client_id(mut self, client_id: &str) -> Self {
self.config.client_id = Some(client_id.to_string());

self
}

/// Set client_secret of this backend.
pub fn client_secret(&mut self, client_secret: &str) -> &mut Self {
pub fn client_secret(mut self, client_secret: &str) -> Self {
self.config.client_secret = Some(client_secret.to_string());

self
}

/// Set refresh_token of this backend.
pub fn refresh_token(&mut self, refresh_token: &str) -> &mut Self {
pub fn refresh_token(mut self, refresh_token: &str) -> Self {
self.config.refresh_token = Some(refresh_token.to_string());

self
}

/// Set drive_type of this backend.
pub fn drive_type(&mut self, drive_type: &str) -> &mut Self {
pub fn drive_type(mut self, drive_type: &str) -> Self {
self.config.drive_type = drive_type.to_string();

self
Expand All @@ -169,7 +169,7 @@ impl AliyunDriveBuilder {
///
/// This API is part of OpenDAL's Raw API. `HttpClient` could be changed
/// during minor updates.
pub fn http_client(&mut self, client: HttpClient) -> &mut Self {
pub fn http_client(mut self, client: HttpClient) -> Self {
self.http_client = Some(client);
self
}
Expand Down
30 changes: 15 additions & 15 deletions core/src/services/aliyun_drive/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ use opendal::Operator;
#[tokio::main]
async fn main() -> Result<()> {
// Create aliyun-drive backend builder.
let mut builder = AliyunDrive::default();
// Set the root for aliyun-drive, all operations will happen under this root.
//
// NOTE: the root must be absolute path.
builder.root("/path/to/dir");
// Set the client_id. This is required.
builder.client_id("client_id");
// Set the client_secret. This is required.
builder.client_secret("client_secret");
// Set the refresh_token. This is required.
builder.refresh_token("refresh_token");
// Set the drive_type. This is required.
//
// Fallback to the default type if no other types found.
builder.drive_type("resource");
let mut builder = AliyunDrive::default()
// Set the root for aliyun-drive, all operations will happen under this root.
//
// NOTE: the root must be absolute path.
.root("/path/to/dir")
// Set the client_id. This is required.
.client_id("client_id")
// Set the client_secret. This is required.
.client_secret("client_secret")
// Set the refresh_token. This is required.
.refresh_token("refresh_token")
// Set the drive_type. This is required.
//
// Fallback to the default type if no other types found.
.drive_type("resource");
let op: Operator = Operator::new(builder)?.finish();
Expand Down
14 changes: 7 additions & 7 deletions core/src/services/alluxio/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl AlluxioBuilder {
/// Set root of this backend.
///
/// All operations will happen under this root.
pub fn root(&mut self, root: &str) -> &mut Self {
pub fn root(mut self, root: &str) -> Self {
self.config.root = if root.is_empty() {
None
} else {
Expand All @@ -104,7 +104,7 @@ impl AlluxioBuilder {
/// endpoint of this backend.
///
/// Endpoint must be full uri, mostly like `http://127.0.0.1:39999`.
pub fn endpoint(&mut self, endpoint: &str) -> &mut Self {
pub fn endpoint(mut self, endpoint: &str) -> Self {
if !endpoint.is_empty() {
// Trim trailing `/` so that we can accept `http://127.0.0.1:39999/`
self.config.endpoint = Some(endpoint.trim_end_matches('/').to_string())
Expand All @@ -119,7 +119,7 @@ impl AlluxioBuilder {
///
/// This API is part of OpenDAL's Raw API. `HttpClient` could be changed
/// during minor updates.
pub fn http_client(&mut self, client: HttpClient) -> &mut Self {
pub fn http_client(mut self, client: HttpClient) -> Self {
self.http_client = Some(client);
self
}
Expand Down Expand Up @@ -270,10 +270,10 @@ mod test {

#[test]
fn test_builder_build() {
let mut builder = AlluxioBuilder::default();
builder.root("/root").endpoint("http://127.0.0.1:39999");

let builder = builder.build();
let builder = AlluxioBuilder::default()
.root("/root")
.endpoint("http://127.0.0.1:39999")
.build();

assert!(builder.is_ok());
}
Expand Down
11 changes: 5 additions & 6 deletions core/src/services/alluxio/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ use opendal::Operator;
#[tokio::main]
async fn main() -> Result<()> {
// create backend builder
let mut builder = Alluxio::default();
// set the storage bucket for OpenDAL
builder.root("/");
// set the endpoint for OpenDAL
builder.endpoint("http://127.0.0.1:39999");
let mut builder = Alluxio::default()
// set the storage bucket for OpenDAL
.root("/")
// set the endpoint for OpenDAL
.endpoint("http://127.0.0.1:39999");
let op: Operator = Operator::new(builder)?.finish();
Expand Down
10 changes: 5 additions & 5 deletions core/src/services/atomicserver/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ impl Debug for AtomicserverBuilder {

impl AtomicserverBuilder {
/// Set the root for Atomicserver.
pub fn root(&mut self, path: &str) -> &mut Self {
pub fn root(mut self, path: &str) -> Self {
self.config.root = Some(path.into());
self
}

/// Set the server address for Atomicserver.
pub fn endpoint(&mut self, endpoint: &str) -> &mut Self {
pub fn endpoint(mut self, endpoint: &str) -> Self {
self.config.endpoint = Some(endpoint.into());
self
}

/// Set the private key for agent used for Atomicserver.
pub fn private_key(&mut self, private_key: &str) -> &mut Self {
pub fn private_key(mut self, private_key: &str) -> Self {
self.config.private_key = Some(private_key.into());
self
}
Expand All @@ -105,13 +105,13 @@ impl AtomicserverBuilder {
/// For example, if the subject URL for the agent being used
/// is ${endpoint}/agents/lTB+W3C/2YfDu9IAVleEy34uCmb56iXXuzWCKBVwdRI=
/// Then the required public key is `lTB+W3C/2YfDu9IAVleEy34uCmb56iXXuzWCKBVwdRI=`
pub fn public_key(&mut self, public_key: &str) -> &mut Self {
pub fn public_key(mut self, public_key: &str) -> Self {
self.config.public_key = Some(public_key.into());
self
}

/// Set the parent resource id (url) that Atomicserver uses to store resources under.
pub fn parent_resource_id(&mut self, parent_resource_id: &str) -> &mut Self {
pub fn parent_resource_id(mut self, parent_resource_id: &str) -> Self {
self.config.parent_resource_id = Some(parent_resource_id.into());
self
}
Expand Down
Loading

0 comments on commit c7cecc4

Please sign in to comment.