Skip to content

Commit 4390461

Browse files
author
centos7
committed
rust to cpp
1 parent 2a18c9a commit 4390461

File tree

7 files changed

+528
-0
lines changed

7 files changed

+528
-0
lines changed

Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "client-cpp"
3+
version = "0.1.0"
4+
authors = ["venslu <1262612838@qq.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
[lib]
9+
name = "tikv_client_rust"
10+
crate-type = ["staticlib"]
11+
12+
[dependencies]
13+
anyhow = "1.0"
14+
cxx = "1.0.18"
15+
futures = "0.3"
16+
tikv-client = "0.0.0"
17+
grpcio = { version = "0.6", features = ["openssl"] }
18+
#tikv-client = { git = "https://github.com/tikv/client-rust.git", branch = "master" }

Makefile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
pes_parent_dir:=$(shell pwd)/$(lastword $(MAKEFILE_LIST))
2+
cur_makefile_path := $(shell dirname $(pes_parent_dir))
3+
4+
all: build-lib build-test
5+
6+
build-test: target/tikv-test
7+
8+
build-lib: pre-build target/cxx/tikv_client_glue.o target/cxx/tikv_client_cpp.o target/cxx/libtikv_client.a
9+
10+
pre-build: target/cxx target/cxx/libtikv_client_rust.a target/cxx/tikv_client_glue.cc target/cxx/tikv_client_glue.h
11+
12+
clean:
13+
cargo clean
14+
15+
run: target/tikv-test
16+
RUST_BACKTRACE=1 $(cur_makefile_path)/target/tikv-test
17+
18+
target/cxx:
19+
mkdir -p $(cur_makefile_path)/target/cxx
20+
21+
target/tikv-test: target/cxx/libtikv_client.a example/main.cpp
22+
c++ $(cur_makefile_path)/example/main.cpp -o $(cur_makefile_path)/target/tikv-test -std=c++17 -g -I$(cur_makefile_path)/target/cxx -I$(cur_makefile_path)/include -L$(cur_makefile_path)/target/cxx -ltikv_client -lpthread -ldl -lssl -lcrypto
23+
24+
target/cxx/libtikv_client.a: target/cxx/libtikv_client_rust.a target/cxx/tikv_client_glue.o target/cxx/tikv_client_cpp.o
25+
cd $(cur_makefile_path)/target/cxx && cp libtikv_client_rust.a libtikv_client.a && ar cr libtikv_client.a tikv_client_cpp.o tikv_client_glue.o
26+
27+
target/cxx/tikv_client_cpp.o: src/tikv_client.cpp
28+
c++ -c $(cur_makefile_path)/src/tikv_client.cpp -o $(cur_makefile_path)/target/cxx/tikv_client_cpp.o -std=c++17 -g -I$(cur_makefile_path)/target/cxx -I$(cur_makefile_path)/include
29+
30+
target/cxx/tikv_client_glue.o: target/cxx/tikv_client_glue.cc
31+
c++ -c $(cur_makefile_path)/target/cxx/tikv_client_glue.cc -o $(cur_makefile_path)/target/cxx/tikv_client_glue.o -std=c++17 -I$(cur_makefile_path)/target/cxx
32+
33+
target/cxx/libtikv_client_rust.a: src/lib.rs
34+
cargo build && mv $(cur_makefile_path)/target/debug/libtikv_client_rust.a $(cur_makefile_path)/target/cxx
35+
36+
target/cxx/tikv_client_glue.cc: src/lib.rs
37+
cxxbridge $(cur_makefile_path)/src/lib.rs > $(cur_makefile_path)/target/cxx/tikv_client_glue.cc
38+
39+
target/cxx/tikv_client_glue.h: src/lib.rs
40+
cxxbridge $(cur_makefile_path)/src/lib.rs --header > $(cur_makefile_path)/target/cxx/tikv_client_glue.h

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,22 @@ It's built on top of
66
[TiKV Client in Rust](https://github.com/tikv/client-rust) via [cxx](https://github.com/dtolnay/cxx).
77

88
This client is still in the stage of prove-of-concept and under heavy development.
9+
10+
# compilation process
11+
12+
## all
13+
14+
```bash
15+
make all
16+
```
17+
18+
## build static lib
19+
20+
```bash
21+
make build-lib
22+
```
23+
24+
## output target
25+
26+
- libtikv_client.a : static lib
27+
- tikv-test: execute file for test

example/main.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "tikv_client.h"
2+
#include <iostream>
3+
4+
int main() {
5+
auto client = tikv_client::TransactionClient({"127.0.0.1:2379"});
6+
auto txn = client.begin();
7+
//txn.put("k1", "v2");
8+
auto val = txn.get("k1");
9+
if (val) {
10+
std::cout << "get key k1:" << *val << std::endl;
11+
} else {
12+
std::cout << "key not found" << std::endl;
13+
}
14+
auto kv_pairs = txn.scan("k1", Bound::Included, "", Bound::Unbounded, 10);
15+
for (auto iter = kv_pairs.begin(); iter != kv_pairs.end(); ++iter) {
16+
std::cout << "scan:" << iter->key << ": " << iter->value << std::endl;
17+
}
18+
return 0;
19+
}

include/tikv_client.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef _TIKV_CLIENT_H_
2+
#define _TIKV_CLIENT_H_
3+
4+
#include "tikv_client_glue.h"
5+
#include <iostream>
6+
#include <optional>
7+
8+
namespace tikv_client {
9+
10+
struct KvPair final {
11+
std::string key;
12+
std::string value;
13+
14+
KvPair(std::string &&key, std::string &&value);
15+
};
16+
17+
class Transaction {
18+
public:
19+
Transaction(::rust::cxxbridge1::Box<tikv_client_glue::Transaction> txn);
20+
std::optional<std::string> get(const std::string &key);
21+
std::optional<std::string> get_for_update(const std::string &key);
22+
std::vector<KvPair> batch_get(const std::vector<std::string> &keys);
23+
std::vector<KvPair> batch_get_for_update(const std::vector<std::string> &keys);
24+
std::vector<KvPair> scan(const std::string &start, Bound start_bound, const std::string &end, Bound end_bound, std::uint32_t limit);
25+
std::vector<std::string> scan_keys(const std::string &start, Bound start_bound, const std::string &end, Bound end_bound, std::uint32_t limit);
26+
bool key_may_exist(const std::string &key);
27+
void put(const std::string &key, const std::string &value);
28+
void batch_put(const std::vector<KvPair> &kvs);
29+
bool merge(const std::string &key, const std::string &value);
30+
void remove(const std::string &key);
31+
void commit();
32+
private:
33+
::rust::cxxbridge1::Box<tikv_client_glue::Transaction> _txn;
34+
};
35+
36+
class TransactionClient {
37+
public:
38+
TransactionClient(const std::vector<std::string> &pd_endpoints);
39+
Transaction begin();
40+
Transaction begin_pessimistic();
41+
private:
42+
::rust::cxxbridge1::Box<tikv_client_glue::TransactionClient> _client;
43+
};
44+
45+
}
46+
47+
#endif //_TIKV_CLIENT_H_

0 commit comments

Comments
 (0)