You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2023/05/26 09:26:07 UTC
[incubator-opendal] branch main updated: feat(bindings/C): opendal_operator_ptr construction using kvs (#2329)
This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
The following commit(s) were added to refs/heads/main by this push:
new 98bc820d feat(bindings/C): opendal_operator_ptr construction using kvs (#2329)
98bc820d is described below
commit 98bc820d3c2fdd43692e7cfc3d6a1b26aa89830a
Author: xyJi <je...@outlook.com>
AuthorDate: Fri May 26 17:26:01 2023 +0800
feat(bindings/C): opendal_operator_ptr construction using kvs (#2329)
* introduce map construction for opendal_operator_ptr
* use opendal_operator_options
* upd
* use as_ref and is_null
---
bindings/c/include/opendal.h | 66 +++++++++++++++++++-
bindings/c/src/lib.rs | 39 ++++++++++--
bindings/c/src/types.rs | 72 ++++++++++++++++++++++
bindings/c/tests/bdd.cpp | 139 +++++++++++++++++++++----------------------
4 files changed, 237 insertions(+), 79 deletions(-)
diff --git a/bindings/c/include/opendal.h b/bindings/c/include/opendal.h
index 2d4e82fd..4a2f9159 100644
--- a/bindings/c/include/opendal.h
+++ b/bindings/c/include/opendal.h
@@ -111,6 +111,8 @@ typedef enum opendal_code {
*/
typedef struct BlockingOperator BlockingOperator;
+typedef struct HashMap_String__String HashMap_String__String;
+
/*
Metadata carries all metadata associated with an path.
@@ -134,6 +136,11 @@ typedef struct Metadata Metadata;
*/
typedef const struct BlockingOperator *opendal_operator_ptr;
+/*
+ [`opendal_operator_options`] represents a series of string type key-value pairs, it may be used for initialization
+ */
+typedef struct HashMap_String__String *opendal_operator_options;
+
/*
The [`opendal_bytes`] type is a C-compatible substitute for [`Vec`]
in Rust, it will not be deallocated automatically like what
@@ -192,17 +199,37 @@ extern "C" {
#endif // __cplusplus
/*
- Returns a result type [`opendal_result_op`], with operator_ptr. If the construction succeeds
- the error is nullptr, otherwise it contains the error information.
+ Uses an array of key-value pairs to initialize the operator based on provided scheme.
+
+ # Example
+
+ Following is a C example.
+ ```no_run
+ opendal_operator_options options = opendal_operator_options_new();
+ opendal_operator_options_set(&options, "root", "/myroot");
+
+ opendal_operator_ptr ptr = opendal_operator_new("memory", options);
+
+ opendal_operator_options_free(&options);
+
+ // ... your operations
+ ```
# Safety
+ This function is unsafe because it deferences and casts the raw pointers.
+
It is [safe] under two cases below
* The memory pointed to by `scheme` must contain a valid nul terminator at the end of
the string.
* The `scheme` points to NULL, this function simply returns you a null opendal_operator_ptr
+
+ # Returns
+
+ Returns a result type [`opendal_result_op`], with operator_ptr. If the construction succeeds
+ the error is nullptr, otherwise it contains the error information.
*/
-opendal_operator_ptr opendal_operator_new(const char *scheme);
+opendal_operator_ptr opendal_operator_new(const char *scheme, opendal_operator_options options);
/*
Write the data into the path blockingly by operator, returns the error code OPENDAL_OK
@@ -311,6 +338,39 @@ bool opendal_metadata_is_file(const opendal_metadata *self);
*/
bool opendal_metadata_is_dir(const opendal_metadata *self);
+/*
+ Construct a heap-allocated opendal_operator_options
+ */
+opendal_operator_options opendal_operator_options_new(void);
+
+/*
+ Set a Key-Value pair inside opendal_operator_options
+
+ # Safety
+
+ This function is unsafe because it dereferences and casts the raw pointers
+ Make sure the pointer of `key` and `value` point to a valid string.
+
+ # Example
+
+ ```C
+ opendal_operator_options options = opendal_operator_options_new();
+ opendal_operator_options_set(&options, "root", "/myroot");
+
+ // .. use your opendal_operator_options
+
+ opendal_operator_options_free(options);
+ ```
+ */
+void opendal_operator_options_set(opendal_operator_options *self,
+ const char *key,
+ const char *value);
+
+/*
+ Free the allocated memory used by [`opendal_operator_options`]
+ */
+void opendal_operator_options_free(const opendal_operator_options *self);
+
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
diff --git a/bindings/c/src/lib.rs b/bindings/c/src/lib.rs
index c2e8893e..87652af0 100644
--- a/bindings/c/src/lib.rs
+++ b/bindings/c/src/lib.rs
@@ -32,22 +32,47 @@ use result::opendal_result_is_exist;
use result::opendal_result_read;
use result::opendal_result_stat;
use types::opendal_metadata;
+use types::opendal_operator_options;
use crate::types::opendal_bytes;
use crate::types::opendal_operator_ptr;
-/// Returns a result type [`opendal_result_op`], with operator_ptr. If the construction succeeds
-/// the error is nullptr, otherwise it contains the error information.
+/// Uses an array of key-value pairs to initialize the operator based on provided scheme.
+///
+/// # Example
+///
+/// Following is a C example.
+/// ```no_run
+/// opendal_operator_options options = opendal_operator_options_new();
+/// opendal_operator_options_set(&options, "root", "/myroot");
+///
+/// opendal_operator_ptr ptr = opendal_operator_new("memory", options);
+///
+// free the options right away since the options is not used later on
+/// opendal_operator_options_free(&options);
+///
+/// // ... your operations
+/// ```
///
/// # Safety
///
+/// This function is unsafe because it deferences and casts the raw pointers.
+///
/// It is [safe] under two cases below
/// * The memory pointed to by `scheme` must contain a valid nul terminator at the end of
/// the string.
/// * The `scheme` points to NULL, this function simply returns you a null opendal_operator_ptr
+///
+/// # Returns
+///
+/// Returns a result type [`opendal_result_op`], with operator_ptr. If the construction succeeds
+/// the error is nullptr, otherwise it contains the error information.
#[no_mangle]
-pub unsafe extern "C" fn opendal_operator_new(scheme: *const c_char) -> opendal_operator_ptr {
- if scheme.is_null() {
+pub unsafe extern "C" fn opendal_operator_new(
+ scheme: *const c_char,
+ options: opendal_operator_options,
+) -> opendal_operator_ptr {
+ if scheme.is_null() || options.is_null() {
return opendal_operator_ptr::null();
}
@@ -59,8 +84,10 @@ pub unsafe extern "C" fn opendal_operator_new(scheme: *const c_char) -> opendal_
}
};
- // todo: api for map construction
- let map = HashMap::default();
+ let mut map = HashMap::default();
+ for (k, v) in options.as_ref() {
+ map.insert(k.to_string(), v.to_string());
+ }
let op = match scheme {
od::Scheme::Memory => generate_operator!(od::services::Memory, map),
diff --git a/bindings/c/src/types.rs b/bindings/c/src/types.rs
index 5bbebb02..cd4c5f55 100644
--- a/bindings/c/src/types.rs
+++ b/bindings/c/src/types.rs
@@ -17,6 +17,8 @@
use ::opendal as od;
+use std::{collections::HashMap, os::raw::c_char};
+
/// The [`opendal_operator_ptr`] owns a pointer to a [`od::BlockingOperator`].
/// It is also the key struct that OpenDAL's APIs access the real
/// operator's memory. The use of OperatorPtr is zero cost, it
@@ -178,3 +180,73 @@ impl opendal_metadata {
}
}
}
+
+/// [`opendal_operator_options`] represents a series of string type key-value pairs, it may be used for initialization
+#[repr(transparent)]
+pub struct opendal_operator_options {
+ inner: *mut HashMap<String, String>,
+}
+
+impl opendal_operator_options {
+ /// Construct a heap-allocated opendal_operator_options
+ #[no_mangle]
+ pub extern "C" fn opendal_operator_options_new() -> Self {
+ let map: HashMap<String, String> = HashMap::default();
+ Self {
+ inner: Box::leak(Box::new(map)),
+ }
+ }
+
+ /// Set a Key-Value pair inside opendal_operator_options
+ ///
+ /// # Safety
+ ///
+ /// This function is unsafe because it dereferences and casts the raw pointers
+ /// Make sure the pointer of `key` and `value` point to a valid string.
+ ///
+ /// # Example
+ ///
+ /// ```C
+ /// opendal_operator_options options = opendal_operator_options_new();
+ /// opendal_operator_options_set(&options, "root", "/myroot");
+ ///
+ /// // .. use your opendal_operator_options
+ ///
+ /// opendal_operator_options_free(options);
+ /// ```
+ #[no_mangle]
+ pub unsafe extern "C" fn opendal_operator_options_set(
+ &mut self,
+ key: *const c_char,
+ value: *const c_char,
+ ) {
+ let k = unsafe { std::ffi::CStr::from_ptr(key) }
+ .to_str()
+ .unwrap()
+ .to_string();
+ let v = unsafe { std::ffi::CStr::from_ptr(value) }
+ .to_str()
+ .unwrap()
+ .to_string();
+ (*self.inner).insert(k, v);
+ }
+
+ /// Returns a reference to the underlying [`HashMap<String, String>`]
+ pub(crate) fn as_ref(&self) -> &HashMap<String, String> {
+ unsafe { &*(self.inner) }
+ }
+
+ /// Returns whether the underlying HashMap points to NULL
+ pub(crate) fn is_null(&self) -> bool {
+ self.inner.is_null()
+ }
+
+ /// Free the allocated memory used by [`opendal_operator_options`]
+ #[no_mangle]
+ pub extern "C" fn opendal_operator_options_free(&self) {
+ if self.inner.is_null() {
+ return;
+ }
+ let _ = unsafe { Box::from_raw(self.inner as *mut HashMap<String, String>) };
+ }
+}
diff --git a/bindings/c/tests/bdd.cpp b/bindings/c/tests/bdd.cpp
index 27bcea54..60c4e24d 100644
--- a/bindings/c/tests/bdd.cpp
+++ b/bindings/c/tests/bdd.cpp
@@ -25,98 +25,97 @@ extern "C" {
class OpendalBddTest : public ::testing::Test {
protected:
- // Setup code for the fixture
- opendal_operator_ptr p;
- std::string scheme;
- std::string path;
- std::string content;
-
- // the fixture setup is an operator write, which will be
- // run at the beginning of every tests
- void SetUp() override
- {
- // construct the memory operator
- this->scheme = std::string("memory");
- this->path = std::string("test");
- this->content = std::string("Hello, World!");
-
- this->p = opendal_operator_new(scheme.c_str());
-
- EXPECT_TRUE(this->p);
-
- const opendal_bytes data = {
- .data = (uint8_t*)this->content.c_str(),
- .len = this->content.length(),
- };
-
- opendal_code code = opendal_operator_blocking_write(this->p, this->path.c_str(), data);
-
- EXPECT_EQ(code, OPENDAL_OK);
- }
-
- // Teardown code for the fixture, free the operator
- void TearDown() override
- {
- opendal_operator_free(&this->p);
- }
+ // Setup code for the fixture
+ opendal_operator_ptr p;
+ std::string scheme;
+ std::string path;
+ std::string content;
+
+ // the fixture setup is an operator write, which will be
+ // run at the beginning of every tests
+ void SetUp() override {
+ // construct the memory operator
+ this->scheme = std::string("memory");
+ this->path = std::string("test");
+ this->content = std::string("Hello, World!");
+
+ // set options
+ opendal_operator_options options = opendal_operator_options_new();
+ opendal_operator_options_set(&options, "root", "/myroot");
+
+ this->p = opendal_operator_new(scheme.c_str(), options);
+
+ // free the options right away since the options is not used later on
+ opendal_operator_options_free(&options);
+
+ EXPECT_TRUE(this->p);
+
+ const opendal_bytes data = {
+ .data = (uint8_t *)this->content.c_str(),
+ .len = this->content.length(),
+ };
+
+ opendal_code code =
+ opendal_operator_blocking_write(this->p, this->path.c_str(), data);
+
+ EXPECT_EQ(code, OPENDAL_OK);
+ }
+
+ // Teardown code for the fixture, free the operator
+ void TearDown() override { opendal_operator_free(&this->p); }
};
// do nothing, the fixture does the Write Test
-TEST_F(OpendalBddTest, Write)
-{
-}
+TEST_F(OpendalBddTest, Write) {}
// The path must exist
-TEST_F(OpendalBddTest, Exist)
-{
- opendal_result_is_exist r = opendal_operator_is_exist(this->p, this->path.c_str());
+TEST_F(OpendalBddTest, Exist) {
+ opendal_result_is_exist r =
+ opendal_operator_is_exist(this->p, this->path.c_str());
- EXPECT_EQ(r.code, OPENDAL_OK);
- EXPECT_TRUE(r.is_exist);
+ EXPECT_EQ(r.code, OPENDAL_OK);
+ EXPECT_TRUE(r.is_exist);
}
// The entry mode must be file
-TEST_F(OpendalBddTest, EntryMode)
-{
- opendal_result_stat r = opendal_operator_stat(this->p, this->path.c_str());
- EXPECT_EQ(r.code, OPENDAL_OK);
+TEST_F(OpendalBddTest, EntryMode) {
+ opendal_result_stat r = opendal_operator_stat(this->p, this->path.c_str());
+ EXPECT_EQ(r.code, OPENDAL_OK);
- opendal_metadata meta = r.meta;
- EXPECT_TRUE(opendal_metadata_is_file(&meta));
+ opendal_metadata meta = r.meta;
+ EXPECT_TRUE(opendal_metadata_is_file(&meta));
- opendal_metadata_free(&meta);
+ opendal_metadata_free(&meta);
}
// The content length must be consistent
-TEST_F(OpendalBddTest, ContentLength)
-{
- opendal_result_stat r = opendal_operator_stat(this->p, this->path.c_str());
- EXPECT_EQ(r.code, OPENDAL_OK);
+TEST_F(OpendalBddTest, ContentLength) {
+ opendal_result_stat r = opendal_operator_stat(this->p, this->path.c_str());
+ EXPECT_EQ(r.code, OPENDAL_OK);
- opendal_metadata meta = r.meta;
- EXPECT_EQ(opendal_metadata_content_length(&meta), 13);
+ opendal_metadata meta = r.meta;
+ EXPECT_EQ(opendal_metadata_content_length(&meta), 13);
- opendal_metadata_free(&meta);
+ opendal_metadata_free(&meta);
}
// We must read the correct content
-TEST_F(OpendalBddTest, Read)
-{
- struct opendal_result_read r = opendal_operator_blocking_read(this->p, this->path.c_str());
+TEST_F(OpendalBddTest, Read) {
+ struct opendal_result_read r =
+ opendal_operator_blocking_read(this->p, this->path.c_str());
- EXPECT_EQ(r.code, OPENDAL_OK);
- EXPECT_EQ(r.data->len, this->content.length());
+ EXPECT_EQ(r.code, OPENDAL_OK);
+ EXPECT_EQ(r.data->len, this->content.length());
- for (int i = 0; i < r.data->len; i++) {
- EXPECT_EQ(this->content[i], (char)(r.data->data[i]));
- }
+ for (int i = 0; i < r.data->len; i++) {
+ EXPECT_EQ(this->content[i], (char)(r.data->data[i]));
+ }
- // free the bytes's heap memory
- opendal_bytes_free(r.data);
+ // free the bytes's heap memory
+ opendal_bytes_free(r.data);
}
-int main(int argc, char** argv)
-{
- ::testing::InitGoogleTest(&argc, argv);
- return RUN_ALL_TESTS();
+int main(int argc, char **argv) {
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
}