Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): add stat_with API for blocking operator #2915

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions core/src/types/operator/blocking_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,53 @@ impl BlockingOperator {
/// # }
/// ```
pub fn stat(&self, path: &str) -> Result<Metadata> {
self.stat_with(path).call()
}

/// Get current path's metadata **without cache** directly with extra options.
///
/// # Notes
///
/// Use `stat` if you:
///
/// - Want to detect the outside changes of path.
/// - Don't want to read from cached metadata.
///
/// You may want to use `metadata` if you are working with entries
/// returned by [`Lister`]. It's highly possible that metadata
/// you want has already been cached.
///
/// # Examples
///
/// ```
/// # use anyhow::Result;
/// # use opendal::BlockingOperator;
/// use opendal::ErrorKind;
/// #
/// # #[tokio::main]
/// # async fn test(op: BlockingOperator) -> Result<()> {
/// if let Err(e) = op.stat_with("test").if_match("<etag>").call() {
/// if e.kind() == ErrorKind::NotFound {
/// println!("file not exist")
/// }
/// }
/// # Ok(())
/// # }
/// ```
pub fn stat_with(&self, path: &str) -> FunctionStat {
let path = normalize_path(path);

let rp = self.inner().blocking_stat(&path, OpStat::new())?;
let meta = rp.into_metadata();
FunctionStat(OperatorFunction::new(
self.inner().clone(),
path,
OpStat::default(),
|inner, path, args| {
let rp = inner.blocking_stat(&path, args)?;
let meta = rp.into_metadata();

Ok(meta)
Ok(meta)
},
))
}

/// Check if this path exists or not.
Expand Down
31 changes: 31 additions & 0 deletions core/src/types/operator/operator_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,34 @@ impl FunctionReader {
self.0.call()
}
}

/// Function that generated by [`BlockingOperator::stat_with`].
///
/// Users can add more options by public functions provided by this struct.
pub struct FunctionStat(pub(crate) OperatorFunction<OpStat, Metadata>);

impl FunctionStat {
/// Set the If-Match for this operation.
pub fn if_match(mut self, v: &str) -> Self {
self.0 = self.0.map_args(|args| args.with_if_match(v));
self
}

/// Set the If-None-Match for this operation.
pub fn if_none_match(mut self, v: &str) -> Self {
self.0 = self.0.map_args(|args| args.with_if_none_match(v));
self
}

/// Set the version for this operation.
pub fn version(mut self, v: &str) -> Self {
self.0 = self.0.map_args(|args| args.with_version(v));
self
}

/// Call the function to consume all the input and generate a
/// result.
pub fn call(self) -> Result<Metadata> {
self.0.call()
}
}