Skip to content

Commit

Permalink
feat(core): add copy&rename to error_context layer
Browse files Browse the repository at this point in the history
Signed-off-by: suyanhanx <suyanhanx@gmail.com>
  • Loading branch information
suyanhanx committed Apr 19, 2023
1 parent 93dfb29 commit cc930b9
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 69 deletions.
127 changes: 64 additions & 63 deletions bindings/c/include/opendal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
Expand Down Expand Up @@ -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`]
Expand All @@ -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;

/*
Expand All @@ -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;

/*
Expand All @@ -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
Expand All @@ -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`]
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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"
Expand Down
42 changes: 42 additions & 0 deletions core/src/layers/error_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions core/src/layers/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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,
);
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions core/src/raw/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion core/tests/behavior/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down

0 comments on commit cc930b9

Please sign in to comment.