Skip to content

Commit 6c9de53

Browse files
committed
lint codes
Signed-off-by: Smityz <smityz@qq.com>
1 parent 755b0e0 commit 6c9de53

File tree

5 files changed

+273
-227
lines changed

5 files changed

+273
-227
lines changed

.github/workflows/ci.yml

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,31 @@ on:
99
name: CI
1010

1111
jobs:
12-
build:
13-
name: build
12+
check:
13+
name: check
1414
runs-on: ubuntu-latest
15+
env:
16+
CARGO_TARGET_DIR: "./build"
1517
steps:
16-
- uses: actions/checkout@v2
17-
- name: Rust Cache
18-
uses: Swatinem/rust-cache@v1.4.0
18+
- uses: actions/checkout@v3
19+
- name: cpp lint
20+
uses: DoozyX/clang-format-lint-action@v0.16.2
21+
with:
22+
source: '.'
23+
extensions: 'cpp,h'
24+
exclude: 'build'
25+
style: Google
26+
- name: rust cache
27+
uses: actions/cache@v3
28+
with:
29+
path: |
30+
~/.cargo/bin/
31+
~/.cargo/registry/index/
32+
~/.cargo/registry/cache/
33+
~/.cargo/git/db/
34+
build/
35+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
36+
- name: rust lint
37+
run: cargo fmt -- --check && cargo clippy -- -D warnings && cargo check --all-targets --all-features
1938
- name: cmake build
20-
run: cmake -S . -B build && cmake --build build
39+
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug && cmake --build build

example/raw.cpp

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.
22

3-
#include <iostream>
43
#include <tikv/tikv_client.h>
54

5+
#include <iostream>
6+
67
int main() {
7-
auto client = tikv_client::RawKVClient({"127.0.0.1:2379"});
8-
9-
const std::uint32_t kTimeoutMs = 10;
10-
client.put("k1", "v1", kTimeoutMs);
11-
12-
auto val = client.get("k1",kTimeoutMs);
13-
if (val) {
14-
std::cout << "get key: \n(k1:" << *val << ")" << std::endl;
15-
} else {
16-
std::cout << "key not found" << std::endl;
17-
}
18-
19-
client.batch_put({{"k2","v2"},{"k3","v3"},{"k4","v4"},{"k5","v5"}}, kTimeoutMs);
20-
21-
const std::uint32_t kLimit = 20;
22-
// scan [k1,k6), limit 20, timeout 10ms
23-
auto kv_pairs = client.scan("k1","k6", kLimit ,kTimeoutMs);
24-
std::cout<<"scan[\"k1\",\"k6\"):"<<std::endl;
25-
for (auto iter = kv_pairs.begin(); iter != kv_pairs.end(); ++iter) {
26-
std::cout << "(" << iter->key << ": " << iter->value << ") ";
27-
}
28-
std::cout << std::endl;
29-
30-
// delete [k3,k5), so [k1,k6) should be [k1,k3) + [k5,k6)
31-
std::cout<<"scan[\"k1\",\"k6\") after delete:"<<std::endl;
32-
client.remove_range("k3","k5",kTimeoutMs);
33-
kv_pairs = client.scan("k1","k6", kLimit ,kTimeoutMs);
34-
for (auto iter = kv_pairs.begin(); iter != kv_pairs.end(); ++iter) {
35-
std::cout << "(" << iter->key << ": " << iter->value << ") ";
36-
}
37-
std::cout << std::endl;
38-
39-
return 0;
8+
auto client = tikv_client::RawKVClient({"127.0.0.1:2379"});
9+
10+
const std::uint32_t kTimeoutMs = 10;
11+
client.put("k1", "v1", kTimeoutMs);
12+
13+
auto val = client.get("k1", kTimeoutMs);
14+
if (val) {
15+
std::cout << "get key: \n(k1:" << *val << ")" << std::endl;
16+
} else {
17+
std::cout << "key not found" << std::endl;
18+
}
19+
20+
client.batch_put({{"k2", "v2"}, {"k3", "v3"}, {"k4", "v4"}, {"k5", "v5"}},
21+
kTimeoutMs);
22+
23+
const std::uint32_t kLimit = 20;
24+
// scan [k1,k6), limit 20, timeout 10ms
25+
auto kv_pairs = client.scan("k1", "k6", kLimit, kTimeoutMs);
26+
std::cout << "scan[\"k1\",\"k6\"):" << std::endl;
27+
for (auto iter = kv_pairs.begin(); iter != kv_pairs.end(); ++iter) {
28+
std::cout << "(" << iter->key << ": " << iter->value << ") ";
29+
}
30+
std::cout << std::endl;
31+
32+
// delete [k3,k5), so [k1,k6) should be [k1,k3) + [k5,k6)
33+
std::cout << "scan[\"k1\",\"k6\") after delete:" << std::endl;
34+
client.remove_range("k3", "k5", kTimeoutMs);
35+
kv_pairs = client.scan("k1", "k6", kLimit, kTimeoutMs);
36+
for (auto iter = kv_pairs.begin(); iter != kv_pairs.end(); ++iter) {
37+
std::cout << "(" << iter->key << ": " << iter->value << ") ";
38+
}
39+
std::cout << std::endl;
40+
41+
return 0;
4042
}

example/txn.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.
22

3-
#include <iostream>
43
#include <tikv/tikv_client.h>
54

5+
#include <iostream>
6+
67
int main() {
7-
auto client = tikv_client::TransactionClient({"127.0.0.1:2379"});
8-
auto txn = client.begin();
8+
auto client = tikv_client::TransactionClient({"127.0.0.1:2379"});
9+
auto txn = client.begin();
910

10-
txn.put("k1", "v2");
11+
txn.put("k1", "v2");
1112

12-
auto val = txn.get("k1");
13-
if (val) {
14-
std::cout << "get key k1:" << *val << std::endl;
15-
} else {
16-
std::cout << "key not found" << std::endl;
17-
}
13+
auto val = txn.get("k1");
14+
if (val) {
15+
std::cout << "get key k1:" << *val << std::endl;
16+
} else {
17+
std::cout << "key not found" << std::endl;
18+
}
1819

19-
auto kv_pairs = txn.scan("k1", Bound::Included, "", Bound::Unbounded, 10);
20-
for (auto iter = kv_pairs.begin(); iter != kv_pairs.end(); ++iter) {
21-
std::cout << "scan:" << iter->key << ": " << iter->value << std::endl;
22-
}
20+
auto kv_pairs = txn.scan("k1", Bound::Included, "", Bound::Unbounded, 10);
21+
for (auto iter = kv_pairs.begin(); iter != kv_pairs.end(); ++iter) {
22+
std::cout << "scan:" << iter->key << ": " << iter->value << std::endl;
23+
}
2324

24-
txn.commit();
25+
txn.commit();
2526

26-
return 0;
27+
return 0;
2728
}

include/tikv_client.h

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,75 @@
11
// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.
22

33
#ifndef _TIKV_CLIENT_H_
4-
#define _TIKV_CLIENT_H_
4+
#define _TIKV_CLIENT_H_
55

6-
#include "lib.rs.h"
76
#include <iostream>
87
#include <optional>
98

9+
#include "lib.rs.h"
10+
1011
namespace tikv_client {
1112

1213
struct KvPair final {
13-
std::string key;
14-
std::string value;
14+
std::string key;
15+
std::string value;
1516

16-
KvPair(std::string &&key, std::string &&value);
17-
ffi::KvPair to_ffi();
17+
KvPair(std::string &&key, std::string &&value);
18+
ffi::KvPair to_ffi();
1819
};
1920

2021
class Transaction {
21-
public:
22-
Transaction(::rust::cxxbridge1::Box<tikv_client_glue::Transaction> txn);
23-
std::optional<std::string> get(const std::string &key);
24-
std::optional<std::string> get_for_update(const std::string &key);
25-
std::vector<KvPair> batch_get(const std::vector<std::string> &keys);
26-
std::vector<KvPair> batch_get_for_update(const std::vector<std::string> &keys);
27-
std::vector<KvPair> scan(const std::string &start, Bound start_bound, const std::string &end, Bound end_bound, std::uint32_t limit);
28-
std::vector<std::string> scan_keys(const std::string &start, Bound start_bound, const std::string &end, Bound end_bound, std::uint32_t limit);
29-
void put(const std::string &key, const std::string &value);
30-
void batch_put(const std::vector<KvPair> &kvs);
31-
void remove(const std::string &key);
32-
void commit();
33-
private:
34-
::rust::cxxbridge1::Box<tikv_client_glue::Transaction> _txn;
22+
public:
23+
Transaction(::rust::cxxbridge1::Box<tikv_client_glue::Transaction> txn);
24+
std::optional<std::string> get(const std::string &key);
25+
std::optional<std::string> get_for_update(const std::string &key);
26+
std::vector<KvPair> batch_get(const std::vector<std::string> &keys);
27+
std::vector<KvPair> batch_get_for_update(
28+
const std::vector<std::string> &keys);
29+
std::vector<KvPair> scan(const std::string &start, Bound start_bound,
30+
const std::string &end, Bound end_bound,
31+
std::uint32_t limit);
32+
std::vector<std::string> scan_keys(const std::string &start,
33+
Bound start_bound, const std::string &end,
34+
Bound end_bound, std::uint32_t limit);
35+
void put(const std::string &key, const std::string &value);
36+
void batch_put(const std::vector<KvPair> &kvs);
37+
void remove(const std::string &key);
38+
void commit();
39+
40+
private:
41+
::rust::cxxbridge1::Box<tikv_client_glue::Transaction> _txn;
3542
};
3643

3744
class TransactionClient {
38-
public:
39-
TransactionClient(const std::vector<std::string> &pd_endpoints);
40-
Transaction begin();
41-
Transaction begin_pessimistic();
42-
private:
43-
::rust::cxxbridge1::Box<tikv_client_glue::TransactionClient> _client;
45+
public:
46+
TransactionClient(const std::vector<std::string> &pd_endpoints);
47+
Transaction begin();
48+
Transaction begin_pessimistic();
49+
50+
private:
51+
::rust::cxxbridge1::Box<tikv_client_glue::TransactionClient> _client;
4452
};
4553

4654
class RawKVClient {
47-
public:
48-
RawKVClient(const std::vector<std::string> &pd_endpoints);
49-
std::optional<std::string> get(const std::string &key,const std::uint64_t timeout);
50-
void put(const std::string &key, const std::string &value, const std::uint64_t timeout);
51-
void batch_put(const std::vector<KvPair> &kvs, const std::uint64_t timeout);
52-
void remove(const std::string &key, const std::uint64_t timeout);
53-
void remove_range(const std::string &start_key, const std::string &end_key, const std::uint64_t timeout);
54-
std::vector<KvPair> scan(const std::string &startKey, const std::string &endKey, std::uint32_t limit, const std::uint64_t timeout);
55-
56-
private:
57-
::rust::cxxbridge1::Box<tikv_client_glue::RawKVClient> _client;
55+
public:
56+
RawKVClient(const std::vector<std::string> &pd_endpoints);
57+
std::optional<std::string> get(const std::string &key,
58+
const std::uint64_t timeout);
59+
void put(const std::string &key, const std::string &value,
60+
const std::uint64_t timeout);
61+
void batch_put(const std::vector<KvPair> &kvs, const std::uint64_t timeout);
62+
void remove(const std::string &key, const std::uint64_t timeout);
63+
void remove_range(const std::string &start_key, const std::string &end_key,
64+
const std::uint64_t timeout);
65+
std::vector<KvPair> scan(const std::string &startKey,
66+
const std::string &endKey, std::uint32_t limit,
67+
const std::uint64_t timeout);
68+
69+
private:
70+
::rust::cxxbridge1::Box<tikv_client_glue::RawKVClient> _client;
5871
};
5972

60-
} // namespace tikv_client
73+
} // namespace tikv_client
6174

62-
#endif //_TIKV_CLIENT_H_
75+
#endif //_TIKV_CLIENT_H_

0 commit comments

Comments
 (0)