Skip to content

Commit

Permalink
feat(!): Allow users to build operator from config
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Jul 24, 2024
1 parent 5e735cc commit 63065e8
Show file tree
Hide file tree
Showing 80 changed files with 854 additions and 1,110 deletions.
4 changes: 2 additions & 2 deletions core/src/docs/internals/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@
//! type Config = DuckConfig;
//!
//! fn from_config(config: Self::Config) -> Self {
//! DuckBuilder { config }
//! DuckBuilder { config: self }
//! }
//!
//! fn build(&mut self) -> Result<Self::Accessor> {
//! fn build(self) -> Result<impl Access> {
//! debug!("backend build started: {:?}", &self);
//!
//! let root = normalize_root(&self.config.root.clone().unwrap_or_default());
Expand Down
18 changes: 9 additions & 9 deletions core/src/layers/immutable_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ mod tests {

use super::*;
use crate::layers::LoggingLayer;
use crate::services::Http;
use crate::services::HttpConfig;
use crate::EntryMode;
use crate::Operator;

Expand All @@ -264,12 +264,12 @@ mod tests {
iil.insert(i.to_string())
}

let op = Http::from_map({
let op = HttpConfig::from_iter({
let mut map = HashMap::new();
map.insert("endpoint".to_string(), "https://xuanwo.io".to_string());
map
})
.and_then(Operator::new)?
.and_then(Operator::from_config)?
.layer(LoggingLayer::default())
.layer(iil)
.finish();
Expand Down Expand Up @@ -302,12 +302,12 @@ mod tests {
iil.insert(i.to_string())
}

let op = Http::from_map({
let op = HttpConfig::from_iter({
let mut map = HashMap::new();
map.insert("endpoint".to_string(), "https://xuanwo.io".to_string());
map
})
.and_then(Operator::new)?
.and_then(Operator::from_config)?
.layer(LoggingLayer::default())
.layer(iil)
.finish();
Expand Down Expand Up @@ -346,12 +346,12 @@ mod tests {
iil.insert(i.to_string())
}

let op = Http::from_map({
let op = HttpConfig::from_iter({
let mut map = HashMap::new();
map.insert("endpoint".to_string(), "https://xuanwo.io".to_string());
map
})
.and_then(Operator::new)?
.and_then(Operator::from_config)?
.layer(LoggingLayer::default())
.layer(iil)
.finish();
Expand Down Expand Up @@ -404,12 +404,12 @@ mod tests {
iil.insert(i.to_string())
}

let op = Http::from_map({
let op = HttpConfig::from_iter({
let mut map = HashMap::new();
map.insert("endpoint".to_string(), "https://xuanwo.io".to_string());
map
})
.and_then(Operator::new)?
.and_then(Operator::from_config)?
.layer(LoggingLayer::default())
.layer(iil)
.finish();
Expand Down
8 changes: 1 addition & 7 deletions core/src/layers/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,15 +773,9 @@ mod tests {

impl Builder for MockBuilder {
const SCHEME: Scheme = Scheme::Custom("mock");
type Accessor = MockService;

type Config = ();

fn from_config(_: Self::Config) -> Self {
Self::default()
}

fn build(&mut self) -> Result<Self::Accessor> {
fn build(self) -> Result<impl Access> {
Ok(MockService {
attempt: self.attempt.clone(),
})
Expand Down
25 changes: 12 additions & 13 deletions core/src/services/aliyun_drive/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use super::lister::AliyunDriveLister;
use super::lister::AliyunDriveParent;
use super::writer::AliyunDriveWriter;
use crate::raw::*;

use crate::*;

/// Aliyun Drive services support.
Expand Down Expand Up @@ -87,6 +88,15 @@ impl Debug for AliyunDriveConfig {
}
}

impl Configurator for AliyunDriveConfig {
fn into_builder(self) -> impl Builder {
AliyunDriveBuilder {
config: self,
http_client: None,
}
}
}

#[doc = include_str!("docs.md")]
#[derive(Default)]
pub struct AliyunDriveBuilder {
Expand Down Expand Up @@ -167,25 +177,15 @@ impl AliyunDriveBuilder {

impl Builder for AliyunDriveBuilder {
const SCHEME: Scheme = Scheme::AliyunDrive;

type Accessor = AliyunDriveBackend;

type Config = AliyunDriveConfig;

fn from_config(config: Self::Config) -> Self {
AliyunDriveBuilder {
config,
http_client: None,
}
}

fn build(&mut self) -> Result<Self::Accessor> {
fn build(self) -> Result<impl Access> {
debug!("backend build started: {:?}", &self);

let root = normalize_root(&self.config.root.clone().unwrap_or_default());
debug!("backend use root {}", &root);

let client = if let Some(client) = self.http_client.take() {
let client = if let Some(client) = self.http_client {
client
} else {
HttpClient::new().map_err(|err| {
Expand Down Expand Up @@ -449,7 +449,6 @@ impl Access for AliyunDriveBackend {
serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
Some(AliyunDriveParent {
file_id: file.file_id,
name: file.name,
path: path.to_string(),
updated_at: file.updated_at,
})
Expand Down
13 changes: 0 additions & 13 deletions core/src/services/aliyun_drive/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,19 +382,6 @@ impl AliyunDriveCore {
self.send(req, token.as_deref()).await
}

pub async fn get(&self, file_id: &str) -> Result<Buffer> {
let (token, drive_id) = self.get_token_and_drive().await?;
let body = serde_json::to_vec(&FileRequest {
drive_id: &drive_id,
file_id,
})
.map_err(new_json_serialize_error)?;
let req = Request::post(format!("{}/adrive/v1.0/openFile/get", self.endpoint))
.body(Buffer::from(body))
.map_err(new_request_build_error)?;
self.send(req, token.as_deref()).await
}

pub async fn upload(&self, upload_url: &str, body: Buffer) -> Result<Buffer> {
let req = Request::put(upload_url)
.body(body)
Expand Down
1 change: 0 additions & 1 deletion core/src/services/aliyun_drive/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub struct AliyunDriveLister {

pub struct AliyunDriveParent {
pub file_id: String,
pub name: String,
pub path: String,
pub updated_at: String,
}
Expand Down
30 changes: 14 additions & 16 deletions core/src/services/alluxio/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ impl Debug for AlluxioConfig {
}
}

impl Configurator for AlluxioConfig {
fn into_builder(self) -> impl Builder {
AlluxioBuilder {
config: self,
http_client: None,
}
}
}

/// [Alluxio](https://www.alluxio.io/) services support.
#[doc = include_str!("docs.md")]
#[derive(Default)]
Expand Down Expand Up @@ -117,18 +126,10 @@ impl AlluxioBuilder {

impl Builder for AlluxioBuilder {
const SCHEME: Scheme = Scheme::Alluxio;
type Accessor = AlluxioBackend;
type Config = AlluxioConfig;

fn from_config(config: Self::Config) -> Self {
AlluxioBuilder {
config,
http_client: None,
}
}

/// Builds the backend and returns the result of AlluxioBackend.
fn build(&mut self) -> Result<Self::Accessor> {
fn build(self) -> Result<impl Access> {
debug!("backend build started: {:?}", &self);

let root = normalize_root(&self.config.root.clone().unwrap_or_default());
Expand All @@ -142,7 +143,7 @@ impl Builder for AlluxioBuilder {
}?;
debug!("backend use endpoint {}", &endpoint);

let client = if let Some(client) = self.http_client.take() {
let client = if let Some(client) = self.http_client {
client
} else {
HttpClient::new().map_err(|err| {
Expand Down Expand Up @@ -259,13 +260,10 @@ mod test {
map.insert("root".to_string(), "/".to_string());
map.insert("endpoint".to_string(), "http://127.0.0.1:39999".to_string());

let builder = AlluxioBuilder::from_map(map).unwrap();
let builder = AlluxioConfig::from_iter(map).unwrap();

assert_eq!(builder.config.root, Some("/".to_string()));
assert_eq!(
builder.config.endpoint,
Some("http://127.0.0.1:39999".to_string())
);
assert_eq!(builder.root, Some("/".to_string()));
assert_eq!(builder.endpoint, Some("http://127.0.0.1:39999".to_string()));
}

#[test]
Expand Down
13 changes: 7 additions & 6 deletions core/src/services/atomicserver/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ impl Debug for AtomicserverConfig {
}
}

impl Configurator for AtomicserverConfig {
fn into_builder(self) -> impl Builder {
AtomicserverBuilder { config: self }
}
}

#[doc = include_str!("docs.md")]
#[derive(Default)]
pub struct AtomicserverBuilder {
Expand Down Expand Up @@ -113,14 +119,9 @@ impl AtomicserverBuilder {

impl Builder for AtomicserverBuilder {
const SCHEME: Scheme = Scheme::Atomicserver;
type Accessor = AtomicserverBackend;
type Config = AtomicserverConfig;

fn from_config(config: Self::Config) -> Self {
Self { config }
}

fn build(&mut self) -> Result<Self::Accessor> {
fn build(self) -> Result<impl Access> {
let root = normalize_root(
self.config
.root
Expand Down
Loading

0 comments on commit 63065e8

Please sign in to comment.