This is an automated email from the ASF dual-hosted git repository.
msadoghi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-resilientdb.git
The following commit(s) were added to refs/heads/master by this push:
new c3953f90 kv-service python api (#136)
c3953f90 is described below
commit c3953f90d182bf5260d5738f3cea2c2dced4e6de
Author: Jiazhi Sun <[email protected]>
AuthorDate: Fri Mar 8 21:27:33 2024 -0800
kv-service python api (#136)
* kv-service python api
* update
---
api/BUILD | 19 ++++++++++++++++++
api/README.md | 32 ++++++++++++++++++++++++++++++
api/ip_address.config | 1 +
api/kv_operation.py | 26 ++++++++++++++++++++++++
api/pybind_kv_service.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 128 insertions(+)
diff --git a/api/BUILD b/api/BUILD
new file mode 100644
index 00000000..d69120c1
--- /dev/null
+++ b/api/BUILD
@@ -0,0 +1,19 @@
+package(default_visibility = ["//visibility:public"])
+cc_binary(
+ name = "pybind_kv.so",
+ srcs = ["pybind_kv_service.cpp"],
+ linkshared =1,
+ linkstatic = 1,
+ deps = [
+ "@//common/proto:signature_info_cc_proto",
+ "@//interface/kv:kv_client",
+ "@//platform/config:resdb_config_utils",
+ "@pybind11//:pybind11",
+ ],
+)
+py_library(
+ name = "pybind_kv_so",
+ data = [":pybind_kv.so"],
+ imports = ["."],
+)
+
diff --git a/api/README.md b/api/README.md
new file mode 100644
index 00000000..437d3fc0
--- /dev/null
+++ b/api/README.md
@@ -0,0 +1,32 @@
+# ResilientDB kv-Service Python API(Get and Set Command)
+
+## Description
+This API allows users to directly use kv-service of the ResilientDB in Python
+
+## How to Run
+1. Make sure you have installed bazel5.0 and pybind11
+2. cd to `incubator-resilientdb/api` folder
+3. Run command `bazel build :pybind_kv_so`
+4. From `kv_operation.py` import `get_value` and `set_value` function into
your Python file to use it (Make sure to use same python version when run
`bazel build` command and call the functions)
+
+## Parameters
+### `set_value`:
+1. `key`: The key user want to store in key-value pair. Acceptable types are
`str`, `int`, `float`
+2. `value`: The `key`'s corresponding value in key-value pair. Acceptable
types are `str`, `int`, `float`
+3. `config_path`(optional): The absolute path to user's blockchain config
file(ip addresses). If user does not specify this parameter, system will use
main chain as default. Acceptable type is `str`
+4. `return`: `True` if `value` has been set successfully, otherwise `value`
has not been set successfully.
+### `get_value`:
+1. `key`: The key user want to get in key-value pair. Acceptable types are
`str`, `int`, `float`
+2. `return`: `\n` if the corresponding value of `key` is empty, otherwise is
corresponding value of `key`
+
+
+## Example
+```angular2html
+import sys
+# Your path to ResilientDB api folder
+sys.path.append("/home/ubuntu/Desktop/incubator-resilientdb/api")
+from kv_operation import set_value, get_value
+
+set_value("test", "111222")
+get_value("test")
+```
\ No newline at end of file
diff --git a/api/ip_address.config b/api/ip_address.config
new file mode 100644
index 00000000..e119673e
--- /dev/null
+++ b/api/ip_address.config
@@ -0,0 +1 @@
+5 44.193.63.142 17005
\ No newline at end of file
diff --git a/api/kv_operation.py b/api/kv_operation.py
new file mode 100644
index 00000000..805a9b8a
--- /dev/null
+++ b/api/kv_operation.py
@@ -0,0 +1,26 @@
+import os
+import sys
+current_file_path = os.path.abspath(__file__)
+current_dir = os.path.dirname(current_file_path)
+new_path_dir = os.path.join(current_dir, "bazel-out", "k8-fastbuild", "bin")
+sys.path.insert(0, new_path_dir)
+import pybind_kv
+
+
+def set_value(key: str or int or float, value: str or int or float,
config_path: str = current_dir + "/ip_address.config") -> bool:
+ """
+ :param key: The key you want to set your value to.
+ :param value: The key's corresponding value in key value pair.
+ :param config_path: Default is connect to the main chain, users can
specify the path to connect to their local blockchain.
+ :return: True if value has been set successfully.
+ """
+ return pybind_kv.set(str(key), str(value), os.path.abspath(config_path))
+
+
+def get_value(key: str or int or float, config_path: str = current_dir +
"/ip_address.config") -> str:
+ """
+ :param key: The key of the value you want to get in key value pair.
+ :param config_path: Default is connect to the main chain, users can
specify the path to connect to their local blockchain.
+ :return: A string of the key's corresponding value.
+ """
+ return pybind_kv.get(str(key), os.path.abspath(config_path))
diff --git a/api/pybind_kv_service.cpp b/api/pybind_kv_service.cpp
new file mode 100644
index 00000000..bad50dc7
--- /dev/null
+++ b/api/pybind_kv_service.cpp
@@ -0,0 +1,50 @@
+#include <fcntl.h>
+#include <getopt.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <pybind11/pybind11.h>
+
+#include <fstream>
+#include "common/proto/signature_info.pb.h"
+#include "interface/kv/kv_client.h"
+#include "platform/config/resdb_config_utils.h"
+
+using resdb::GenerateReplicaInfo;
+using resdb::GenerateResDBConfig;
+using resdb::KVClient;
+using resdb::ReplicaInfo;
+using resdb::ResDBConfig;
+
+
+
+
+std::string get(std::string key, std::string config_path) {
+ ResDBConfig config = GenerateResDBConfig(config_path);
+ config.SetClientTimeoutMs(100000);
+ KVClient client(config);
+ auto result_ptr = client.Get(key);
+ if (result_ptr) {
+ return *result_ptr;
+ } else {
+ return "";
+ }
+}
+
+bool set(std::string key, std::string value, std::string config_path) {
+ ResDBConfig config = GenerateResDBConfig(config_path);
+ config.SetClientTimeoutMs(100000);
+ KVClient client(config);
+ int result = client.Set(key, value);
+ if (result == 0) {
+ return true;
+ } else {
+ return false;
+ }
+}
+
+PYBIND11_MODULE(pybind_kv, m) {
+ m.def("get", &get, "A function that gets a value from the key-value
store");
+ m.def("set", &set, "A function that sets a value in the key-value store");
+}
+