-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement namespace APIs (#71)
Signed-off-by: tison <wander4096@gmail.com> Co-authored-by: David Li <davidli2010@foxmail.com>
- Loading branch information
1 parent
75e96f8
commit f8900b1
Showing
11 changed files
with
404 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! Namespace example | ||
use etcd_client::*; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
let client = Client::connect(["localhost:2379"], None).await?; | ||
let mut kv_client = client.kv_client(); | ||
let mut kv_client_prefix = KvClientPrefix::new(kv_client.clone(), "person/".into()); | ||
|
||
kv_client_prefix.put("Alice", "15", None).await?; | ||
println!("put kv: {{Alice: 15}}"); | ||
|
||
// get prefixed kv | ||
let resp = kv_client.get("person/Alice", None).await?; | ||
if let Some(kv) = resp.kvs().first() { | ||
println!( | ||
"Get prefixed kv: {{{}: {}}}", | ||
kv.key_str()?, | ||
kv.value_str()? | ||
); | ||
} | ||
|
||
// get kv | ||
let resp = kv_client_prefix.get("Alice", None).await?; | ||
if let Some(kv) = resp.kvs().first() { | ||
println!("Get kv: {{{}: {}}}", kv.key_str()?, kv.value_str()?); | ||
} | ||
|
||
// delete kv | ||
let resp = kv_client_prefix | ||
.delete("Alice", Some(DeleteOptions::new().with_prev_key())) | ||
.await?; | ||
if let Some(kv) = resp.prev_kvs().first() { | ||
println!("Delete kv: {{{}: {}}}", kv.key_str()?, kv.value_str()?); | ||
} | ||
|
||
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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::error::Result; | ||
use crate::vec::VecExt; | ||
use crate::{ | ||
DeleteOptions, DeleteResponse, GetOptions, GetResponse, KvClient, PutOptions, PutResponse, Txn, | ||
TxnResponse, | ||
}; | ||
|
||
pub struct KvClientPrefix { | ||
pfx: Vec<u8>, | ||
kv: KvClient, | ||
} | ||
|
||
impl KvClientPrefix { | ||
pub fn new(kv: KvClient, pfx: Vec<u8>) -> Self { | ||
Self { pfx, kv } | ||
} | ||
|
||
#[inline] | ||
fn prefixed_key(&self, key: impl Into<Vec<u8>>) -> Vec<u8> { | ||
let mut key = key.into(); | ||
key.prefix_with(&self.pfx); | ||
key | ||
} | ||
|
||
pub async fn put( | ||
&mut self, | ||
key: impl Into<Vec<u8>>, | ||
value: impl Into<Vec<u8>>, | ||
options: Option<PutOptions>, | ||
) -> Result<PutResponse> { | ||
let key = self.prefixed_key(key); | ||
let mut resp = self.kv.put(key, value, options).await?; | ||
resp.strip_prev_key_prefix(&self.pfx); | ||
Ok(resp) | ||
} | ||
|
||
pub async fn get( | ||
&mut self, | ||
key: impl Into<Vec<u8>>, | ||
mut options: Option<GetOptions>, | ||
) -> Result<GetResponse> { | ||
let key = self.prefixed_key(key); | ||
options = options.map(|mut opts| { | ||
opts.key_range_end_mut().prefix_range_end_with(&self.pfx); | ||
opts | ||
}); | ||
let mut resp = self.kv.get(key, options).await?; | ||
resp.strip_kvs_prefix(&self.pfx); | ||
Ok(resp) | ||
} | ||
|
||
pub async fn delete( | ||
&mut self, | ||
key: impl Into<Vec<u8>>, | ||
mut options: Option<DeleteOptions>, | ||
) -> Result<DeleteResponse> { | ||
let key = self.prefixed_key(key); | ||
options = options.map(|mut opts| { | ||
opts.key_range_end_mut().prefix_range_end_with(&self.pfx); | ||
opts | ||
}); | ||
let mut resp = self.kv.delete(key, options).await?; | ||
resp.strip_prev_kvs_prefix(&self.pfx); | ||
Ok(resp) | ||
} | ||
|
||
pub async fn txn(&mut self, mut txn: Txn) -> Result<TxnResponse> { | ||
txn.prefix_with(&self.pfx); | ||
let mut resp = self.kv.txn(txn).await?; | ||
resp.strip_key_prefix(&self.pfx); | ||
Ok(resp) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use crate::error::Result; | ||
use crate::{LeaseClient, LeaseTimeToLiveOptions, LeaseTimeToLiveResponse}; | ||
|
||
pub struct LeaseClientPrefix { | ||
pfx: Vec<u8>, | ||
lease: LeaseClient, | ||
} | ||
|
||
impl LeaseClientPrefix { | ||
/// Wrap a Lease interface to filter for only keys with a prefix | ||
/// and remove that prefix when fetching attached keys through TimeToLive. | ||
pub fn new(lease: LeaseClient, pfx: Vec<u8>) -> Self { | ||
Self { pfx, lease } | ||
} | ||
|
||
pub async fn time_to_live( | ||
&mut self, | ||
id: i64, | ||
options: Option<LeaseTimeToLiveOptions>, | ||
) -> Result<LeaseTimeToLiveResponse> { | ||
let mut resp = self.lease.time_to_live(id, options).await?; | ||
resp.strip_keys_prefix(&self.pfx); | ||
Ok(resp) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod kv; | ||
mod lease; | ||
|
||
pub use kv::KvClientPrefix; | ||
pub use lease::LeaseClientPrefix; |
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
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
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
Oops, something went wrong.