Skip to content

Commit

Permalink
kv-service python api (#136)
Browse files Browse the repository at this point in the history
* kv-service python api

* update
  • Loading branch information
NoBugInMyCode authored Mar 9, 2024
1 parent a145249 commit c3953f9
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
19 changes: 19 additions & 0 deletions api/BUILD
Original file line number Diff line number Diff line change
@@ -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 = ["."],
)

32 changes: 32 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -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")
```
1 change: 1 addition & 0 deletions api/ip_address.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5 44.193.63.142 17005
26 changes: 26 additions & 0 deletions api/kv_operation.py
Original file line number Diff line number Diff line change
@@ -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))
50 changes: 50 additions & 0 deletions api/pybind_kv_service.cpp
Original file line number Diff line number Diff line change
@@ -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");
}

0 comments on commit c3953f9

Please sign in to comment.