Skip to content

Commit

Permalink
refactor: Use list for check instead of stat (#399)
Browse files Browse the repository at this point in the history
* refactor: Use list for check instead of stat

Signed-off-by: Xuanwo <[email protected]>

* Fix format

Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo authored Jun 27, 2022
1 parent 4b9d0e6 commit 836b615
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/io_util/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};
use std::io::ErrorKind;
use std::ops::Deref;

use percent_encoding::utf8_percent_encode;
use percent_encoding::AsciiSet;
use percent_encoding::NON_ALPHANUMERIC;

/// HttpClient that used across opendal.
///
/// NOTE: we could change or support more underlying http backend.
Expand Down
17 changes: 10 additions & 7 deletions src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

#[cfg(feature = "retry")]
use std::fmt::Debug;
use std::io::ErrorKind;
use std::io::Result;
use std::sync::Arc;

#[cfg(feature = "retry")]
use backon::Backoff;
use futures::StreamExt;
use futures::TryStreamExt;
use log::debug;

Expand Down Expand Up @@ -156,8 +158,7 @@ impl Operator {

/// Check if this operator can work correctly.
///
/// We will send a real `stat` request to path and return any errors
/// we met.
/// We will send a `list` request to path and return any errors we met.
///
/// ```
/// # use std::sync::Arc;
Expand All @@ -170,15 +171,17 @@ impl Operator {
/// # async fn main() -> Result<()> {
/// # let accessor = fs::Backend::build().finish().await?;
/// let op = Operator::new(accessor);
/// op.check(".opendal").await?;
/// op.check().await?;
/// # Ok(())
/// # }
/// ```
pub async fn check(&self, path: &str) -> Result<()> {
// The existences of `.opendal` doesn't matters.
let _ = self.object(path).is_exist().await?;
pub async fn check(&self) -> Result<()> {
let mut ds = self.object("/").list().await?;

Ok(())
match ds.next().await {
Some(Err(e)) if e.kind() != ErrorKind::NotFound => Err(e),
_ => Ok(()),
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/behavior/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ cfg_if::cfg_if! {
}
}

/// Create file with file path should succeed.
/// Check should be OK.
async fn test_check(op: Operator) -> Result<()> {
op.check(".opendal").await.expect("operator check is ok");
op.check().await.expect("operator check is ok");

Ok(())
}
Expand Down

1 comment on commit 836b615

@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-hwyg4ts20-databend.vercel.app

Built with commit 836b615.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.