-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add a syntactic sugar method
Signed-off-by: Phoeniix Zhao <Phoenix500526@163.com>
- Loading branch information
1 parent
e104226
commit 70d0524
Showing
2 changed files
with
39 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
use anyhow::Result; | ||
use xline_client::{clients::Xutex, Client, ClientOptions}; | ||
use xline_client::{clients::Xutex, Client, ClientOptions, types::kv::{Compare, PutRequest, TxnOp, CompareResult},}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
// the name and address of all curp members | ||
let curp_members = ["10.0.0.1:2379", "10.0.0.2:2379", "10.0.0.3:2379"]; | ||
|
||
let client = Client::connect(curp_members, ClientOptions::default()) | ||
.await? | ||
.lock_client(); | ||
.await?; | ||
|
||
let mut xutex = Xutex::new(client, "lock-test", None, None).await?; | ||
let lock_client = client.lock_client(); | ||
let kv_client = client.kv_client(); | ||
|
||
let mut xutex = Xutex::new(lock_client, "lock-test", None, None).await?; | ||
// when the `xutex_guard` drop, the lock will be unlocked. | ||
let xutex_guard = xutex.lock().await?; | ||
|
||
println!( | ||
"lock key: {:?}", | ||
String::from_utf8_lossy(xutex_guard.key().as_bytes()) | ||
); | ||
let txn_req = xutex_guard.txn_check_locked_key() | ||
.when([Compare::value("key2", CompareResult::Equal, "value2")]) | ||
.and_then( | ||
[TxnOp::put( | ||
PutRequest::new("key2", "value3").with_prev_kv(true), | ||
)], | ||
) | ||
.or_else(&[]); | ||
|
||
let _resp = kv_client.txn(txn_req).await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters