Skip to content

Commit

Permalink
feat(core): add if_not_exist in OpWrite
Browse files Browse the repository at this point in the history
Signed-off-by: Keming <[email protected]>
  • Loading branch information
kemingy committed Nov 10, 2024
1 parent 20b7f34 commit 0b131ec
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/src/raw/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ pub struct OpWrite {
cache_control: Option<String>,
executor: Option<Executor>,
if_none_match: Option<String>,
if_not_exist: Option<bool>,
user_metadata: Option<HashMap<String, String>>,
}

Expand Down Expand Up @@ -697,6 +698,17 @@ impl OpWrite {
self.if_none_match.as_deref()
}

/// Set the If-Not-Exist of the option
pub fn with_if_not_exist(mut self, b: bool) -> Self {
self.if_not_exist = Some(b);
self
}

/// Get If-Not-Exist from option
pub fn if_not_exist(&self) -> Option<bool> {
self.if_not_exist
}

/// Merge given executor into option.
///
/// If executor has already been set, this will do nothing.
Expand Down
1 change: 1 addition & 0 deletions core/src/services/s3/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ impl Access for S3Backend {
write_with_cache_control: true,
write_with_content_type: true,
write_with_if_none_match: true,
write_with_if_not_exist: true,
write_with_user_metadata: true,

// The min multipart size of S3 is 5 MiB.
Expand Down
6 changes: 6 additions & 0 deletions core/src/services/s3/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,12 @@ impl S3Core {
req = req.header(IF_NONE_MATCH, if_none_match);
}

if let Some(if_not_exist) = args.if_not_exist() {
if if_not_exist {
req = req.header(IF_NONE_MATCH, "*");
}
}

// Set body
let req = req.body(body).map_err(new_request_build_error)?;

Expand Down
2 changes: 2 additions & 0 deletions core/src/types/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub struct Capability {
pub write_with_cache_control: bool,
/// If operator supports write with if none match.
pub write_with_if_none_match: bool,
/// If operator supports write with if not exist.
pub write_with_if_not_exist: bool,
/// If operator supports write with user defined metadata
pub write_with_user_metadata: bool,
/// write_multi_max_size is the max size that services support in write_multi.
Expand Down
19 changes: 19 additions & 0 deletions core/src/types/operator/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,25 @@ impl Operator {
/// # }
/// ```
///
/// ## `if_not_exist`
///
/// Set `if_not_exist` for this `write` request. This can be treated as a simplified version
/// of [`OpWrite::if_none_match`].
///
/// This feature is used to write a file only when it doesn't exist. If the file already
/// exists, an error with kind [`ErrorKind::ConditionNotMatch`] will be returned.
///
/// ```no_run
/// # use opendal::{ErrorKind, Result};
/// use opendal::Operator;
/// # async fn test(op: Operator, etag: &str) -> Result<()> {
/// let bs = b"hello, world!".to_vec();
/// let res = op.write_with("path/to/file", bs).if_not_exist(true).await;
/// assert!(res.is_err());
/// assert_eq!(res.unwrap_err().kind(), ErrorKind::ConditionNotMatch);
/// # Ok(())}
/// ```
///
/// # Examples
///
/// ```
Expand Down
5 changes: 5 additions & 0 deletions core/src/types/operator/operator_futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ impl<F: Future<Output = Result<()>>> FutureWrite<F> {
self.map(|(args, options, bs)| (args.with_if_none_match(s), options, bs))
}

/// Set the If-Not-Exist for this operation.
pub fn if_not_exist(self, b: bool) -> Self {
self.map(|(args, options, bs)| (args.with_if_not_exist(b), options, bs))
}

/// Set the user defined metadata of the op
///
/// ## Notes
Expand Down
22 changes: 22 additions & 0 deletions core/tests/behavior/async_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub fn tests(op: &Operator, tests: &mut Vec<Trial>) {
test_write_with_content_type,
test_write_with_content_disposition,
test_write_with_if_none_match,
test_write_with_if_not_exist,
test_write_with_user_metadata,
test_writer_write,
test_writer_write_with_overwrite,
Expand Down Expand Up @@ -646,3 +647,24 @@ pub async fn test_write_with_if_none_match(op: Operator) -> Result<()> {

Ok(())
}

/// Write an file with if_not_exist will get a ConditionNotMatch error if file exists.
pub async fn test_write_with_if_not_exist(op: Operator) -> Result<()> {
if !op.info().full_capability().write_with_if_not_exist {
return Ok(());
}

let (path, content, _) = TEST_FIXTURE.new_file(op.clone());

op.write(&path, content.clone())
.await
.expect("write must succeed");
let res = op
.write_with(&path, content.clone())
.if_not_exist(true)
.await;
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind(), ErrorKind::ConditionNotMatch);

Ok(())
}

0 comments on commit 0b131ec

Please sign in to comment.