Merge pull request #229 from andylokandy/exist

Add key_exists method
This commit is contained in:
Nick Cameron 2021-01-19 10:23:56 +13:00 committed by GitHub
commit a7c41819c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 0 deletions

View File

@ -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 |

View File

@ -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;

View File

@ -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,

View File

@ -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