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

refactor(types/operator): rename is_exist to exists #5193

Merged
merged 3 commits into from
Oct 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn main() -> Result<()> {
assert_eq!(op.info().scheme(), Scheme::S3);

let result = op
.is_exist(&uuid::Uuid::new_v4().to_string())
.exists(&uuid::Uuid::new_v4().to_string())
.await
.expect("this operation should never return error");
assert!(!result, "the file must be not exist");
Expand Down
25 changes: 25 additions & 0 deletions core/src/types/operator/blocking_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,30 @@ impl BlockingOperator {
))
}

/// Check if this path exists or not.
///
/// # Example
///
/// ```no_run
/// use anyhow::Result;
/// use opendal::BlockingOperator;
/// fn test(op: BlockingOperator) -> Result<()> {
/// let _ = op.exists("test")?;
///
/// Ok(())
/// }
/// ```
pub fn exists(&self, path: &str) -> Result<bool> {
let r = self.stat(path);
match r {
Ok(_) => Ok(true),
Err(err) => match err.kind() {
ErrorKind::NotFound => Ok(false),
_ => Err(err),
},
}
}

/// Check if this path exists or not.
///
/// # Example
Expand All @@ -280,6 +304,7 @@ impl BlockingOperator {
/// Ok(())
/// }
/// ```
#[deprecated(note = "rename to `exists` for consistence with `std::fs::exists`")]
pub fn is_exist(&self, path: &str) -> Result<bool> {
let r = self.stat(path);
match r {
Expand Down
27 changes: 27 additions & 0 deletions core/src/types/operator/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,32 @@ impl Operator {
)
}

/// Check if this path exists or not.
///
/// # Example
///
/// ```
/// use anyhow::Result;
/// use futures::io;
/// use opendal::Operator;
///
/// async fn test(op: Operator) -> Result<()> {
/// let _ = op.exists("test").await?;
///
/// Ok(())
/// }
/// ```
pub async fn exists(&self, path: &str) -> Result<bool> {
let r = self.stat(path).await;
match r {
Ok(_) => Ok(true),
Err(err) => match err.kind() {
ErrorKind::NotFound => Ok(false),
_ => Err(err),
},
}
}

/// Check if this path exists or not.
///
/// # Example
Expand All @@ -345,6 +371,7 @@ impl Operator {
/// Ok(())
/// }
/// ```
#[deprecated(note = "rename to `exists` for consistence with `std::fs::exists`")]
pub async fn is_exist(&self, path: &str) -> Result<bool> {
let r = self.stat(path).await;
match r {
Expand Down
12 changes: 6 additions & 6 deletions core/tests/behavior/async_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub async fn test_delete_file(op: Operator) -> Result<()> {
op.delete(&path).await?;

// Stat it again to check.
assert!(!op.is_exist(&path).await?);
assert!(!op.exists(&path).await?);

Ok(())
}
Expand Down Expand Up @@ -96,7 +96,7 @@ pub async fn test_delete_with_special_chars(op: Operator) -> Result<()> {
op.delete(&path).await?;

// Stat it again to check.
assert!(!op.is_exist(&path).await?);
assert!(!op.exists(&path).await?);

Ok(())
}
Expand All @@ -121,7 +121,7 @@ pub async fn test_remove_one_file(op: Operator) -> Result<()> {
op.remove(vec![path.clone()]).await?;

// Stat it again to check.
assert!(!op.is_exist(&path).await?);
assert!(!op.exists(&path).await?);

op.write(&format!("/{path}"), content)
.await
Expand All @@ -130,7 +130,7 @@ pub async fn test_remove_one_file(op: Operator) -> Result<()> {
op.remove(vec![path.clone()]).await?;

// Stat it again to check.
assert!(!op.is_exist(&path).await?);
assert!(!op.exists(&path).await?);

Ok(())
}
Expand Down Expand Up @@ -163,7 +163,7 @@ pub async fn test_delete_stream(op: Operator) -> Result<()> {
// Stat it again to check.
for path in expected.iter() {
assert!(
!op.is_exist(&format!("{dir}/{path}")).await?,
!op.exists(&format!("{dir}/{path}")).await?,
"{path} should be removed"
)
}
Expand Down Expand Up @@ -229,7 +229,7 @@ pub async fn test_delete_with_version(op: Operator) -> Result<()> {
let version = meta.version().expect("must have version");

op.delete(path.as_str()).await.expect("delete must success");
assert!(!op.is_exist(path.as_str()).await?);
assert!(!op.exists(path.as_str()).await?);

// After a simple delete, the data can still be accessed using its version.
let meta = op
Expand Down
2 changes: 1 addition & 1 deletion core/tests/behavior/async_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ pub async fn test_remove_all(op: Operator) -> Result<()> {
continue;
}
assert!(
!op.is_exist(&format!("{parent}/{path}")).await?,
!op.exists(&format!("{parent}/{path}")).await?,
"{parent}/{path} should be removed"
)
}
Expand Down
4 changes: 2 additions & 2 deletions core/tests/behavior/async_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ pub async fn test_writer_abort(op: Operator) -> Result<()> {
}

// Aborted writer should not write actual file.
assert!(!op.is_exist(&path).await?);
assert!(!op.exists(&path).await?);
Ok(())
}

Expand All @@ -282,7 +282,7 @@ pub async fn test_writer_abort_with_concurrent(op: Operator) -> Result<()> {
}

// Aborted writer should not write actual file.
assert!(!op.is_exist(&path).await?);
assert!(!op.exists(&path).await?);
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions core/tests/behavior/blocking_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn test_blocking_delete_file(op: BlockingOperator) -> Result<()> {
op.delete(&path)?;

// Stat it again to check.
assert!(!op.is_exist(&path)?);
assert!(!op.exists(&path)?);

Ok(())
}
Expand All @@ -67,7 +67,7 @@ pub fn test_blocking_remove_one_file(op: BlockingOperator) -> Result<()> {
op.remove(vec![path.clone()])?;

// Stat it again to check.
assert!(!op.is_exist(&path)?);
assert!(!op.exists(&path)?);

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion core/tests/behavior/blocking_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub fn test_blocking_remove_all(op: BlockingOperator) -> Result<()> {
continue;
}
assert!(
!op.is_exist(&format!("{parent}/{path}"))?,
!op.exists(&format!("{parent}/{path}"))?,
"{parent}/{path} should be removed"
)
}
Expand Down
4 changes: 2 additions & 2 deletions integrations/dav-server/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl DavFileSystem for OpendalFs {
// During MKCOL processing, a server MUST make the Request-URI a member of its parent collection, unless the Request-URI is "/". If no such ancestor exists, the method MUST fail.
// refer to https://datatracker.ietf.org/doc/html/rfc2518#section-8.3.1
let parent = Path::new(&path).parent().unwrap();
match self.op.is_exist(parent.to_str().unwrap()).await {
match self.op.exists(parent.to_str().unwrap()).await {
Ok(exist) => {
if !exist && parent != Path::new("/") {
return Err(FsError::NotFound);
Expand All @@ -132,7 +132,7 @@ impl DavFileSystem for OpendalFs {

let path = path.as_str();
// check if the given path is exist (MKCOL on existing collection should fail (RFC2518:8.3.1))
let exist = self.op.is_exist(path).await;
let exist = self.op.exists(path).await;
match exist {
Ok(exist) => match exist {
true => Err(FsError::Exists),
Expand Down
2 changes: 1 addition & 1 deletion integrations/parquet/src/async_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mod tests {
writer.write(bytes).await.unwrap();
drop(writer);

let exist = op.is_exist(path).await.unwrap();
let exist = op.exists(path).await.unwrap();
assert!(!exist);
}

Expand Down
Loading