Skip to content

Commit

Permalink
refactor(ops): Promote ops as a parent mod (#553)
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <github@xuanwo.io>

Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo committed Aug 22, 2022
1 parent 7dad75e commit 8266ca8
Show file tree
Hide file tree
Showing 11 changed files with 595 additions and 406 deletions.
395 changes: 1 addition & 394 deletions src/ops.rs → src/ops/bytes_range.rs

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Operations and help utils used by [`Accessor`][crate::Accessor].
//!
//! # Notes
//!
//! Users should not use struct or functions here except the following cases:
//!
//! - Implement a new service support.
//! - Implement a new Layer.

mod operation;
pub use operation::Operation;

mod op_create;
pub use op_create::OpCreate;
mod op_delete;
pub use op_delete::OpDelete;
mod op_list;
pub use op_list::OpList;
mod op_presign;
pub use op_presign::OpPresign;
pub use op_presign::PresignedRequest;
mod op_read;
pub use op_read::OpRead;
mod op_stat;
pub use op_stat::OpStat;
mod op_write;
pub use op_write::OpWrite;

mod bytes_range;
pub use bytes_range::BytesRange;
82 changes: 82 additions & 0 deletions src/ops/op_create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::io::Result;

use anyhow::anyhow;

use crate::error::other;
use crate::error::ObjectError;
use crate::ObjectMode;

/// Args for `create` operation.
///
/// The path must be normalized.
#[derive(Debug, Clone, Default)]
pub struct OpCreate {
path: String,
mode: ObjectMode,
}

impl OpCreate {
/// Create a new `OpCreate`.
///
/// If input path is not match with object mode, an error will be returned.
pub fn new(path: &str, mode: ObjectMode) -> Result<Self> {
match mode {
ObjectMode::FILE => {
if path.ends_with('/') {
return Err(other(ObjectError::new(
"create",
path,
anyhow!("Is a directory"),
)));
}
Ok(Self {
path: path.to_string(),
mode,
})
}
ObjectMode::DIR => {
if !path.ends_with('/') {
return Err(other(ObjectError::new(
"create",
path,
anyhow!("Not a directory"),
)));
}

Ok(Self {
path: path.to_string(),
mode,
})
}
ObjectMode::Unknown => Err(other(ObjectError::new(
"create",
path,
anyhow!("create unknown object mode is not supported"),
))),
}
}

/// Get path from option.
pub fn path(&self) -> &str {
&self.path
}

/// Get object mode from option.
pub fn mode(&self) -> ObjectMode {
self.mode
}
}
37 changes: 37 additions & 0 deletions src/ops/op_delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::io::Result;

/// Args for `delete` operation.
///
/// The path must be normalized.
#[derive(Debug, Clone, Default)]
pub struct OpDelete {
path: String,
}

impl OpDelete {
/// Create a new `OpDelete`.
pub fn new(path: &str) -> Result<Self> {
Ok(Self {
path: path.to_string(),
})
}

/// Get path from option.
pub fn path(&self) -> &str {
&self.path
}
}
52 changes: 52 additions & 0 deletions src/ops/op_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::io::Result;

use anyhow::anyhow;

use crate::error::other;
use crate::error::ObjectError;

/// Args for `list` operation.
///
/// The path must be normalized.
#[derive(Debug, Clone, Default)]
pub struct OpList {
path: String,
}

impl OpList {
/// Create a new `OpList`.
///
/// If input path is not a dir path, an error will be returned.
pub fn new(path: &str) -> Result<Self> {
if !path.ends_with('/') {
return Err(other(ObjectError::new(
"list",
path,
anyhow!("Not a directory"),
)));
}

Ok(Self {
path: path.to_string(),
})
}

/// Get path from option.
pub fn path(&self) -> &str {
&self.path
}
}
89 changes: 89 additions & 0 deletions src/ops/op_presign.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::io::Result;

use time::Duration;

use super::Operation;

/// Args for `presign` operation.
///
/// The path must be normalized.
#[derive(Debug, Clone, Default)]
pub struct OpPresign {
path: String,
op: Operation,
expire: Duration,
}

impl OpPresign {
/// Create a new `OpPresign`.
pub fn new(path: &str, op: Operation, expire: Duration) -> Result<Self> {
Ok(Self {
path: path.to_string(),
op,
expire,
})
}

/// Get path from op.
pub fn path(&self) -> &str {
&self.path
}

/// Get operation from op.
pub fn operation(&self) -> Operation {
self.op
}

/// Get expire from op.
pub fn expire(&self) -> Duration {
self.expire
}
}

/// PresignedRequest is a presigned request return by `presign`.
#[derive(Debug, Clone)]
pub struct PresignedRequest {
method: http::Method,
uri: http::Uri,
headers: http::HeaderMap,
}

impl PresignedRequest {
/// Create a new PresignedRequest
pub fn new(method: http::Method, uri: http::Uri, headers: http::HeaderMap) -> Self {
Self {
method,
uri,
headers,
}
}

/// Return request's method.
pub fn method(&self) -> &http::Method {
&self.method
}

/// Return request's uri.
pub fn uri(&self) -> &http::Uri {
&self.uri
}

/// Return request's header.
pub fn header(&self) -> &http::HeaderMap {
&self.headers
}
}
90 changes: 90 additions & 0 deletions src/ops/op_read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::io::Result;
use std::ops::RangeBounds;

use anyhow::anyhow;

use super::BytesRange;
use crate::error::other;
use crate::error::ObjectError;

/// Args for `read` operation.
///
/// The path must be normalized.
#[derive(Debug, Clone, Default)]
pub struct OpRead {
path: String,
offset: Option<u64>,
size: Option<u64>,
}

impl OpRead {
/// Create a new `OpRead`.
///
/// If input path is not a file path, an error will be returned.
pub fn new(path: &str, range: impl RangeBounds<u64>) -> Result<Self> {
if path.ends_with('/') {
return Err(other(ObjectError::new(
"read",
path,
anyhow!("Is a directory"),
)));
}

let br = BytesRange::from(range);

Ok(Self {
path: path.to_string(),
offset: br.offset(),
size: br.size(),
})
}

pub(crate) fn new_with_offset(
path: &str,
offset: Option<u64>,
size: Option<u64>,
) -> Result<Self> {
if path.ends_with('/') {
return Err(other(ObjectError::new(
"read",
path,
anyhow!("Is a directory"),
)));
}

Ok(Self {
path: path.to_string(),
offset,
size,
})
}

/// Get path from option.
pub fn path(&self) -> &str {
&self.path
}

/// Get offset from option.
pub fn offset(&self) -> Option<u64> {
self.offset
}

/// Get size from option.
pub fn size(&self) -> Option<u64> {
self.size
}
}
Loading

1 comment on commit 8266ca8

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for opendal ready!

✅ Preview
https://opendal-ibulwaa4u-databend.vercel.app

Built with commit 8266ca8.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.