You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by su...@apache.org on 2023/04/19 12:28:56 UTC

[incubator-opendal] 01/01: feat(core): add copy&rename to error_context layer

This is an automated email from the ASF dual-hosted git repository.

suyanhanx pushed a commit to branch add-copy-rename-error_context
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git

commit cc930b9924b058043f7986d32e6f6744cb298fcb
Author: suyanhanx <su...@gmail.com>
AuthorDate: Wed Apr 19 20:27:41 2023 +0800

    feat(core): add copy&rename to error_context layer
    
    Signed-off-by: suyanhanx <su...@gmail.com>
---
 bindings/c/include/opendal.h     | 127 ++++++++++++++++++++-------------------
 core/src/layers/error_context.rs |  42 +++++++++++++
 core/src/layers/logging.rs       |   6 +-
 core/src/raw/operation.rs        |   4 +-
 core/tests/behavior/README.md    |   2 +-
 5 files changed, 112 insertions(+), 69 deletions(-)

diff --git a/bindings/c/include/opendal.h b/bindings/c/include/opendal.h
index 21d9ba4e..f4ad4704 100644
--- a/bindings/c/include/opendal.h
+++ b/bindings/c/include/opendal.h
@@ -17,65 +17,66 @@
  * under the License.
  */
 
+
 #ifndef _OPENDAL_H
 #define _OPENDAL_H
 
-#include <stdbool.h>
-#include <stddef.h>
 #include <stdint.h>
+#include <stddef.h>
+#include <stdbool.h>
 
 /*
  The error code for opendal APIs in C binding
  */
 typedef enum opendal_code {
-    /*
-     All is well
-     */
-    OPENDAL_OK,
-    /*
-     General error
-     */
-    OPENDAL_ERROR,
-    /*
-     returning it back. For example, s3 returns an internal service error.
-     */
-    OPENDAL_UNEXPECTED,
-    /*
-     Underlying service doesn't support this operation.
-     */
-    OPENDAL_UNSUPPORTED,
-    /*
-     The config for backend is invalid.
-     */
-    OPENDAL_CONFIG_INVALID,
-    /*
-     The given path is not found.
-     */
-    OPENDAL_NOT_FOUND,
-    /*
-     The given path doesn't have enough permission for this operation
-     */
-    OPENDAL_PERMISSION_DENIED,
-    /*
-     The given path is a directory.
-     */
-    OPENDAL_IS_A_DIRECTORY,
-    /*
-     The given path is not a directory.
-     */
-    OPENDAL_NOT_A_DIRECTORY,
-    /*
-     The given path already exists thus we failed to the specified operation on it.
-     */
-    OPENDAL_ALREADY_EXISTS,
-    /*
-     Requests that sent to this path is over the limit, please slow down.
-     */
-    OPENDAL_RATE_LIMITED,
-    /*
-     The given file paths are same.
-     */
-    OPENDAL_IS_SAME_FILE,
+  /*
+   All is well
+   */
+  OPENDAL_OK,
+  /*
+   General error
+   */
+  OPENDAL_ERROR,
+  /*
+   returning it back. For example, s3 returns an internal service error.
+   */
+  OPENDAL_UNEXPECTED,
+  /*
+   Underlying service doesn't support this operation.
+   */
+  OPENDAL_UNSUPPORTED,
+  /*
+   The config for backend is invalid.
+   */
+  OPENDAL_CONFIG_INVALID,
+  /*
+   The given path is not found.
+   */
+  OPENDAL_NOT_FOUND,
+  /*
+   The given path doesn't have enough permission for this operation
+   */
+  OPENDAL_PERMISSION_DENIED,
+  /*
+   The given path is a directory.
+   */
+  OPENDAL_IS_A_DIRECTORY,
+  /*
+   The given path is not a directory.
+   */
+  OPENDAL_NOT_A_DIRECTORY,
+  /*
+   The given path already exists thus we failed to the specified operation on it.
+   */
+  OPENDAL_ALREADY_EXISTS,
+  /*
+   Requests that sent to this path is over the limit, please slow down.
+   */
+  OPENDAL_RATE_LIMITED,
+  /*
+   The given file paths are same.
+   */
+  OPENDAL_IS_SAME_FILE,
 } opendal_code;
 
 /*
@@ -120,7 +121,7 @@ typedef struct BlockingOperator BlockingOperator;
  to check its validity by native boolean operator.
  e.g. you could check by (!ptr) on a [`opendal_operator_ptr`]
  */
-typedef const struct BlockingOperator* opendal_operator_ptr;
+typedef const struct BlockingOperator *opendal_operator_ptr;
 
 /*
  The [`opendal_bytes`] type is a C-compatible substitute for [`Vec`]
@@ -129,8 +130,8 @@ typedef const struct BlockingOperator* opendal_operator_ptr;
  to free the heap memory to avoid memory leak.
  */
 typedef struct opendal_bytes {
-    const uint8_t* data;
-    uintptr_t len;
+  const uint8_t *data;
+  uintptr_t len;
 } opendal_bytes;
 
 /*
@@ -140,8 +141,8 @@ typedef struct opendal_bytes {
  and the error code is NOT OPENDAL_OK.
  */
 typedef struct opendal_result_read {
-    struct opendal_bytes* data;
-    enum opendal_code code;
+  struct opendal_bytes *data;
+  enum opendal_code code;
 } opendal_result_read;
 
 /*
@@ -150,8 +151,8 @@ typedef struct opendal_result_read {
  corresponding error code.
  */
 typedef struct opendal_result_is_exist {
-    bool is_exist;
-    enum opendal_code code;
+  bool is_exist;
+  enum opendal_code code;
 } opendal_result_is_exist;
 
 #ifdef __cplusplus
@@ -169,7 +170,7 @@ extern "C" {
    the string.
  * The `scheme` points to NULL, this function simply returns you a null opendal_operator_ptr
  */
-opendal_operator_ptr opendal_operator_new(const char* scheme);
+opendal_operator_ptr opendal_operator_new(const char *scheme);
 
 /*
  Free the allocated operator pointed by [`opendal_operator_ptr`]
@@ -191,8 +192,8 @@ void opendal_operator_free(opendal_operator_ptr op_ptr);
  * If the `path` points to NULL, this function panics
  */
 enum opendal_code opendal_operator_blocking_write(opendal_operator_ptr op_ptr,
-    const char* path,
-    struct opendal_bytes bytes);
+                                                  const char *path,
+                                                  struct opendal_bytes bytes);
 
 /*
  Read the data out from path into a [`Bytes`] blockingly by operator, returns
@@ -210,7 +211,7 @@ enum opendal_code opendal_operator_blocking_write(opendal_operator_ptr op_ptr,
  * If the `path` points to NULL, this function panics
  */
 struct opendal_result_read opendal_operator_blocking_read(opendal_operator_ptr op_ptr,
-    const char* path);
+                                                          const char *path);
 
 /*
  Check whether the path exists.
@@ -231,12 +232,12 @@ struct opendal_result_read opendal_operator_blocking_read(opendal_operator_ptr o
  * If the `path` points to NULL, this function panics
  */
 struct opendal_result_is_exist opendal_operator_is_exist(opendal_operator_ptr op_ptr,
-    const char* path);
+                                                         const char *path);
 
 /*
  Frees the heap memory used by the [`opendal_bytes`]
  */
-void opendal_bytes_free(const struct opendal_bytes* self);
+void opendal_bytes_free(const struct opendal_bytes *self);
 
 #ifdef __cplusplus
 } // extern "C"
diff --git a/core/src/layers/error_context.rs b/core/src/layers/error_context.rs
index 5417a040..ae7c1c59 100644
--- a/core/src/layers/error_context.rs
+++ b/core/src/layers/error_context.rs
@@ -138,6 +138,30 @@ impl<A: Accessor> LayeredAccessor for ErrorContextAccessor<A> {
             .await
     }
 
+    async fn copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
+        self.inner
+            .copy(from, to, args)
+            .map_err(|err| {
+                err.with_operation(Operation::Copy)
+                    .with_context("service", self.meta.scheme())
+                    .with_context("from", from)
+                    .with_context("to", to)
+            })
+            .await
+    }
+
+    async fn rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> {
+        self.inner
+            .rename(from, to, args)
+            .map_err(|err| {
+                err.with_operation(Operation::Rename)
+                    .with_context("service", self.meta.scheme())
+                    .with_context("from", from)
+                    .with_context("to", to)
+            })
+            .await
+    }
+
     async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
         self.inner
             .stat(path, args)
@@ -284,6 +308,24 @@ impl<A: Accessor> LayeredAccessor for ErrorContextAccessor<A> {
             })
     }
 
+    fn blocking_copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
+        self.inner.blocking_copy(from, to, args).map_err(|err| {
+            err.with_operation(Operation::BlockingCopy)
+                .with_context("service", self.meta.scheme())
+                .with_context("from", from)
+                .with_context("to", to)
+        })
+    }
+
+    fn blocking_rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> {
+        self.inner.blocking_rename(from, to, args).map_err(|err| {
+            err.with_operation(Operation::BlockingRename)
+                .with_context("service", self.meta.scheme())
+                .with_context("from", from)
+                .with_context("to", to)
+        })
+    }
+
     fn blocking_stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
         self.inner.blocking_stat(path, args).map_err(|err| {
             err.with_operation(Operation::BlockingStat)
diff --git a/core/src/layers/logging.rs b/core/src/layers/logging.rs
index abecc48f..d65797f5 100644
--- a/core/src/layers/logging.rs
+++ b/core/src/layers/logging.rs
@@ -827,7 +827,7 @@ impl<A: Accessor> LayeredAccessor for LoggingAccessor<A> {
             target: LOGGING_TARGET,
             "service={} operation={} from={} to={} -> started",
             self.scheme,
-            Operation::BlockingMove,
+            Operation::BlockingRename,
             from,
             to,
         );
@@ -839,7 +839,7 @@ impl<A: Accessor> LayeredAccessor for LoggingAccessor<A> {
                     target: LOGGING_TARGET,
                     "service={} operation={} from={} to={} -> finished",
                     self.scheme,
-                    Operation::BlockingMove,
+                    Operation::BlockingRename,
                     from,
                     to,
                 );
@@ -852,7 +852,7 @@ impl<A: Accessor> LayeredAccessor for LoggingAccessor<A> {
                         lvl,
                         "service={} operation={} from={} to={} -> {}: {err:?}",
                         self.scheme,
-                        Operation::BlockingMove,
+                        Operation::BlockingRename,
                         from,
                         to,
                         self.err_status(&err)
diff --git a/core/src/raw/operation.rs b/core/src/raw/operation.rs
index 1c37cbf4..0600ce26 100644
--- a/core/src/raw/operation.rs
+++ b/core/src/raw/operation.rs
@@ -56,7 +56,7 @@ pub enum Operation {
     /// Operation for [`crate::raw::Accessor::blocking_copy`]
     BlockingCopy,
     /// Operation for [`crate::raw::Accessor::blocking_rename`]
-    BlockingMove,
+    BlockingRename,
     /// Operation for [`crate::raw::Accessor::blocking_stat`]
     BlockingStat,
     /// Operation for [`crate::raw::Accessor::blocking_delete`]
@@ -99,7 +99,7 @@ impl From<Operation> for &'static str {
             Operation::BlockingRead => "blocking_read",
             Operation::BlockingWrite => "blocking_write",
             Operation::BlockingCopy => "blocking_copy",
-            Operation::BlockingMove => "blocking_rename",
+            Operation::BlockingRename => "blocking_rename",
             Operation::BlockingStat => "blocking_stat",
             Operation::BlockingDelete => "blocking_delete",
             Operation::BlockingList => "blocking_list",
diff --git a/core/tests/behavior/README.md b/core/tests/behavior/README.md
index 3b8c9427..82a9f169 100644
--- a/core/tests/behavior/README.md
+++ b/core/tests/behavior/README.md
@@ -38,7 +38,7 @@ Test specific backend(such as `fs`).
 cargo test services_fs
 ```
 
-As `cargo test` only run tests containing the following string in their names, we use `services-fs` to run all tests under `services::fs`.
+As `cargo test` only run tests containing the following string in their names, we use `services_fs` to run all tests under `services::fs`.
 
 To run all tests under `tests/behavior/write.rs` for `fs`, we use `services_fs_write`.