Skip to content

add linter for codes #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,31 @@ on:
name: CI

jobs:
build:
name: build
check:
name: check
runs-on: ubuntu-latest
env:
CARGO_TARGET_DIR: "./build"
steps:
- uses: actions/checkout@v2
- name: Rust Cache
uses: Swatinem/rust-cache@v1.4.0
- uses: actions/checkout@v3
- name: cpp lint
uses: DoozyX/clang-format-lint-action@v0.16.2
with:
source: '.'
extensions: 'cpp,h'
exclude: 'build'
style: Google
- name: rust cache
uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
build/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: rust lint
run: cargo fmt -- --check && cargo clippy -- -D warnings && cargo check --all-targets --all-features
- name: cmake build
run: cmake -S . -B build && cmake --build build
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug && cmake --build build
70 changes: 36 additions & 34 deletions example/raw.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.

#include <iostream>
#include <tikv/tikv_client.h>

#include <iostream>

int main() {
auto client = tikv_client::RawKVClient({"127.0.0.1:2379"});

const std::uint32_t kTimeoutMs = 10;
client.put("k1", "v1", kTimeoutMs);

auto val = client.get("k1",kTimeoutMs);
if (val) {
std::cout << "get key: \n(k1:" << *val << ")" << std::endl;
} else {
std::cout << "key not found" << std::endl;
}

client.batch_put({{"k2","v2"},{"k3","v3"},{"k4","v4"},{"k5","v5"}}, kTimeoutMs);

const std::uint32_t kLimit = 20;
// scan [k1,k6), limit 20, timeout 10ms
auto kv_pairs = client.scan("k1","k6", kLimit ,kTimeoutMs);
std::cout<<"scan[\"k1\",\"k6\"):"<<std::endl;
for (auto iter = kv_pairs.begin(); iter != kv_pairs.end(); ++iter) {
std::cout << "(" << iter->key << ": " << iter->value << ") ";
}
std::cout << std::endl;

// delete [k3,k5), so [k1,k6) should be [k1,k3) + [k5,k6)
std::cout<<"scan[\"k1\",\"k6\") after delete:"<<std::endl;
client.remove_range("k3","k5",kTimeoutMs);
kv_pairs = client.scan("k1","k6", kLimit ,kTimeoutMs);
for (auto iter = kv_pairs.begin(); iter != kv_pairs.end(); ++iter) {
std::cout << "(" << iter->key << ": " << iter->value << ") ";
}
std::cout << std::endl;

return 0;
auto client = tikv_client::RawKVClient({"127.0.0.1:2379"});

const std::uint32_t kTimeoutMs = 10;
client.put("k1", "v1", kTimeoutMs);

auto val = client.get("k1", kTimeoutMs);
if (val) {
std::cout << "get key: \n(k1:" << *val << ")" << std::endl;
} else {
std::cout << "key not found" << std::endl;
}

client.batch_put({{"k2", "v2"}, {"k3", "v3"}, {"k4", "v4"}, {"k5", "v5"}},
kTimeoutMs);

const std::uint32_t kLimit = 20;
// scan [k1,k6), limit 20, timeout 10ms
auto kv_pairs = client.scan("k1", "k6", kLimit, kTimeoutMs);
std::cout << "scan[\"k1\",\"k6\"):" << std::endl;
for (auto iter = kv_pairs.begin(); iter != kv_pairs.end(); ++iter) {
std::cout << "(" << iter->key << ": " << iter->value << ") ";
}
std::cout << std::endl;

// delete [k3,k5), so [k1,k6) should be [k1,k3) + [k5,k6)
std::cout << "scan[\"k1\",\"k6\") after delete:" << std::endl;
client.remove_range("k3", "k5", kTimeoutMs);
kv_pairs = client.scan("k1", "k6", kLimit, kTimeoutMs);
for (auto iter = kv_pairs.begin(); iter != kv_pairs.end(); ++iter) {
std::cout << "(" << iter->key << ": " << iter->value << ") ";
}
std::cout << std::endl;

return 0;
}
33 changes: 17 additions & 16 deletions example/txn.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.

#include <iostream>
#include <tikv/tikv_client.h>

#include <iostream>

int main() {
auto client = tikv_client::TransactionClient({"127.0.0.1:2379"});
auto txn = client.begin();
auto client = tikv_client::TransactionClient({"127.0.0.1:2379"});
auto txn = client.begin();

txn.put("k1", "v2");
txn.put("k1", "v2");

auto val = txn.get("k1");
if (val) {
std::cout << "get key k1:" << *val << std::endl;
} else {
std::cout << "key not found" << std::endl;
}
auto val = txn.get("k1");
if (val) {
std::cout << "get key k1:" << *val << std::endl;
} else {
std::cout << "key not found" << std::endl;
}

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

txn.commit();
txn.commit();

return 0;
return 0;
}
91 changes: 52 additions & 39 deletions include/tikv_client.h
Original file line number Diff line number Diff line change
@@ -1,62 +1,75 @@
// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.

#ifndef _TIKV_CLIENT_H_
#define _TIKV_CLIENT_H_
#define _TIKV_CLIENT_H_

#include "lib.rs.h"
#include <iostream>
#include <optional>

#include "lib.rs.h"

namespace tikv_client {

struct KvPair final {
std::string key;
std::string value;
std::string key;
std::string value;

KvPair(std::string &&key, std::string &&value);
ffi::KvPair to_ffi();
KvPair(std::string &&key, std::string &&value);
ffi::KvPair to_ffi();
};

class Transaction {
public:
Transaction(::rust::cxxbridge1::Box<tikv_client_glue::Transaction> txn);
std::optional<std::string> get(const std::string &key);
std::optional<std::string> get_for_update(const std::string &key);
std::vector<KvPair> batch_get(const std::vector<std::string> &keys);
std::vector<KvPair> batch_get_for_update(const std::vector<std::string> &keys);
std::vector<KvPair> scan(const std::string &start, Bound start_bound, const std::string &end, Bound end_bound, std::uint32_t limit);
std::vector<std::string> scan_keys(const std::string &start, Bound start_bound, const std::string &end, Bound end_bound, std::uint32_t limit);
void put(const std::string &key, const std::string &value);
void batch_put(const std::vector<KvPair> &kvs);
void remove(const std::string &key);
void commit();
private:
::rust::cxxbridge1::Box<tikv_client_glue::Transaction> _txn;
public:
Transaction(::rust::cxxbridge1::Box<tikv_client_glue::Transaction> txn);
std::optional<std::string> get(const std::string &key);
std::optional<std::string> get_for_update(const std::string &key);
std::vector<KvPair> batch_get(const std::vector<std::string> &keys);
std::vector<KvPair> batch_get_for_update(
const std::vector<std::string> &keys);
std::vector<KvPair> scan(const std::string &start, Bound start_bound,
const std::string &end, Bound end_bound,
std::uint32_t limit);
std::vector<std::string> scan_keys(const std::string &start,
Bound start_bound, const std::string &end,
Bound end_bound, std::uint32_t limit);
void put(const std::string &key, const std::string &value);
void batch_put(const std::vector<KvPair> &kvs);
void remove(const std::string &key);
void commit();

private:
::rust::cxxbridge1::Box<tikv_client_glue::Transaction> _txn;
};

class TransactionClient {
public:
TransactionClient(const std::vector<std::string> &pd_endpoints);
Transaction begin();
Transaction begin_pessimistic();
private:
::rust::cxxbridge1::Box<tikv_client_glue::TransactionClient> _client;
public:
TransactionClient(const std::vector<std::string> &pd_endpoints);
Transaction begin();
Transaction begin_pessimistic();

private:
::rust::cxxbridge1::Box<tikv_client_glue::TransactionClient> _client;
};

class RawKVClient {
public:
RawKVClient(const std::vector<std::string> &pd_endpoints);
std::optional<std::string> get(const std::string &key,const std::uint64_t timeout);
void put(const std::string &key, const std::string &value, const std::uint64_t timeout);
void batch_put(const std::vector<KvPair> &kvs, const std::uint64_t timeout);
void remove(const std::string &key, const std::uint64_t timeout);
void remove_range(const std::string &start_key, const std::string &end_key, const std::uint64_t timeout);
std::vector<KvPair> scan(const std::string &startKey, const std::string &endKey, std::uint32_t limit, const std::uint64_t timeout);

private:
::rust::cxxbridge1::Box<tikv_client_glue::RawKVClient> _client;
public:
RawKVClient(const std::vector<std::string> &pd_endpoints);
std::optional<std::string> get(const std::string &key,
const std::uint64_t timeout);
void put(const std::string &key, const std::string &value,
const std::uint64_t timeout);
void batch_put(const std::vector<KvPair> &kvs, const std::uint64_t timeout);
void remove(const std::string &key, const std::uint64_t timeout);
void remove_range(const std::string &start_key, const std::string &end_key,
const std::uint64_t timeout);
std::vector<KvPair> scan(const std::string &startKey,
const std::string &endKey, std::uint32_t limit,
const std::uint64_t timeout);

private:
::rust::cxxbridge1::Box<tikv_client_glue::RawKVClient> _client;
};

} // namespace tikv_client
} // namespace tikv_client

#endif //_TIKV_CLIENT_H_
#endif //_TIKV_CLIENT_H_
Loading