Add example for key_exists

Signed-off-by: Andy Lok <andylokandy@hotmail.com>
This commit is contained in:
Andy Lok 2021-01-18 18:18:54 +08:00
parent 25ea196aad
commit c8c7c02dcd
1 changed files with 25 additions and 0 deletions

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;