mirror of https://github.com/tikv/client-rust.git
Merge branch 'master' into not_founds
This commit is contained in:
commit
95a682894d
|
@ -13,6 +13,7 @@ env:
|
|||
global:
|
||||
- RUST_BACKTRACE=1
|
||||
- RUSTFLAGS="-D warnings"
|
||||
- RUSTDOCFLAGS="-D warnings"
|
||||
|
||||
addons:
|
||||
apt:
|
||||
|
@ -42,6 +43,7 @@ script:
|
|||
- if [[ $TRAVIS_OS_NAME == "linux" && $TRAVIS_RUST_VERSION == "stable" ]]; then cargo clippy -- -D clippy::all; fi
|
||||
- cargo build --all
|
||||
- cargo build --examples
|
||||
- if [[ $TRAVIS_OS_NAME == "linux" && $TRAVIS_RUST_VERSION == "nightly" ]]; then cargo doc --exclude tikv-client-proto --document-private-items; fi
|
||||
- if [[ $TRAVIS_RUST_VERSION == "nightly" ]]; then cargo test --all -- --nocapture; fi
|
||||
# For now we only run full integration tests on Linux. Here's why:
|
||||
# * Docker on OS X is not supported by Travis.
|
||||
|
|
|
@ -74,8 +74,10 @@ There are some [examples](examples) which show how to use the client in a Rust p
|
|||
| ----------- | ------------------- | ---------------------- | ------------------------------------------------------------------ |
|
||||
| `put` | `KvPair` | `()` | |
|
||||
| `get` | `Key` | `Option<value>` | |
|
||||
| `key_exists` | `Key` | `bool` | |
|
||||
| `delete` | `Key` | `()` | |
|
||||
| `scan` | `BoundRange` | `Iter<KvPair>` | |
|
||||
| `scan_keys` | `BoundRange` | `Iter<Key>` | |
|
||||
| `batch_get` | `Iter<Key>` | `Iter<KvPair>` | Skip non-existent keys; Does not retain order |
|
||||
| `lock_keys` | `Iter<Key>` | `()` | |
|
||||
| `gc` | `Timestamp` | `bool` | It returns whether the latest safepoint in PD equals the parameter |
|
||||
|
|
|
@ -24,7 +24,8 @@ pub fn parse_args(app_name: &str) -> CommandArgs {
|
|||
.takes_value(true)
|
||||
.multiple(true)
|
||||
.value_delimiter(",")
|
||||
.required(true),
|
||||
.required(false)
|
||||
.default_value("localhost:2379"),
|
||||
)
|
||||
// A cyclic dependency between CA, cert and key is made
|
||||
// to ensure that no security options are missing.
|
||||
|
|
|
@ -29,6 +29,21 @@ async fn get(client: &Client, key: Key) -> Option<Value> {
|
|||
res
|
||||
}
|
||||
|
||||
async fn key_exists(client: &Client, key: Key) -> bool {
|
||||
let mut txn = client
|
||||
.begin_optimistic()
|
||||
.await
|
||||
.expect("Could not begin a transaction");
|
||||
let res = txn
|
||||
.key_exists(key)
|
||||
.await
|
||||
.expect("Could not check key exists");
|
||||
txn.commit()
|
||||
.await
|
||||
.expect("Committing read-only transaction should not fail");
|
||||
res
|
||||
}
|
||||
|
||||
async fn scan(client: &Client, range: impl Into<BoundRange>, limit: u32) {
|
||||
let mut txn = client
|
||||
.begin_optimistic()
|
||||
|
@ -82,6 +97,16 @@ async fn main() {
|
|||
let value1 = get(&txn, key1.clone()).await;
|
||||
println!("{:?}", (key1, value1));
|
||||
|
||||
// check key exists
|
||||
let key1: Key = b"key1".to_vec().into();
|
||||
let key1_exists = key_exists(&txn, key1.clone()).await;
|
||||
let key2: Key = b"key_not_exist".to_vec().into();
|
||||
let key2_exists = key_exists(&txn, key2.clone()).await;
|
||||
println!(
|
||||
"check exists {:?}",
|
||||
vec![(key1, key1_exists), (key2, key2_exists)]
|
||||
);
|
||||
|
||||
// scan
|
||||
let key1: Key = b"key1".to_vec().into();
|
||||
scan(&txn, key1.., 10).await;
|
||||
|
|
|
@ -18,7 +18,7 @@ const SCAN_LOCK_BATCH_SIZE: u32 = 1024; // TODO: cargo-culted value
|
|||
/// The TiKV transactional `Client` is used to interact with TiKV using transactional (MVCC) requests.
|
||||
///
|
||||
/// A [`Transaction`](crate::transaction::Transaction) provides a SQL-like interface.
|
||||
/// It begins with a [`begin`](Client::begin) or [`begin_pessimistic`](Client::begin_pessimistic) request
|
||||
/// It begins with a [`begin_optimistic`](Client::begin_optimistic) or [`begin_pessimistic`](Client::begin_pessimistic) request
|
||||
/// and ends with a `rollback` or `commit` request.
|
||||
/// If a `Transaction` is dropped before it's rolled back or committed, it is automatically rolled back.
|
||||
///
|
||||
|
|
|
@ -23,6 +23,11 @@ impl Snapshot {
|
|||
self.transaction.get(key).await
|
||||
}
|
||||
|
||||
/// Check whether the key exists.
|
||||
pub async fn key_exists(&self, key: impl Into<Key>) -> Result<bool> {
|
||||
self.transaction.key_exists(key).await
|
||||
}
|
||||
|
||||
/// Get the values associated with the given keys.
|
||||
pub async fn batch_get(
|
||||
&self,
|
||||
|
|
|
@ -146,6 +146,24 @@ impl Transaction {
|
|||
}
|
||||
}
|
||||
|
||||
/// Check whether the key exists.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust,no_run
|
||||
/// # use tikv_client::{Value, Config, TransactionClient};
|
||||
/// # use futures::prelude::*;
|
||||
/// # futures::executor::block_on(async {
|
||||
/// # let client = TransactionClient::new(vec!["192.168.0.100", "192.168.0.101"]).await.unwrap();
|
||||
/// let mut txn = client.begin_pessimistic().await.unwrap();
|
||||
/// let exists = txn.key_exists("k1".to_owned()).await.unwrap();
|
||||
/// txn.commit().await.unwrap();
|
||||
/// # });
|
||||
/// ```
|
||||
pub async fn key_exists(&self, key: impl Into<Key>) -> Result<bool> {
|
||||
let key = key.into();
|
||||
Ok(self.scan_keys(key.clone()..=key, 1).await?.next().is_some())
|
||||
}
|
||||
|
||||
/// Create a new 'batch get' request.
|
||||
///
|
||||
/// Once resolved this request will result in the fetching of the values associated with the
|
||||
|
@ -889,7 +907,7 @@ impl Committer {
|
|||
|
||||
#[derive(PartialEq)]
|
||||
enum TransactionStatus {
|
||||
/// The transaction is read-only [`Snapshot`](super::Snapshot::Snapshot), no need to commit or rollback or panic on drop.
|
||||
/// The transaction is read-only [`Snapshot`](super::Snapshot), no need to commit or rollback or panic on drop.
|
||||
ReadOnly,
|
||||
/// The transaction have not been committed or rolled back.
|
||||
Active,
|
||||
|
|
|
@ -80,7 +80,7 @@ impl From<tikv_client_proto::kvrpcpb::KeyError> for Error {
|
|||
}
|
||||
}
|
||||
|
||||
/// A result holding an [`Error`](Error).
|
||||
/// A result holding an [`Error`](enum@Error).
|
||||
pub type Result<T> = result::Result<T, Error>;
|
||||
|
||||
#[macro_export]
|
||||
|
|
Loading…
Reference in New Issue