Struct tikv_client::RawClient
source · pub struct RawClient<Cod = ApiV1RawCodec, PdC = PdRpcClient<Cod>>where
Cod: Codec,
PdC: PdClient<Codec = Cod>,{ /* private fields */ }
Expand description
The TiKV raw Client
is used to interact with TiKV using raw requests.
Raw requests don’t need a wrapping transaction. Each request is immediately processed once executed.
The returned results of raw request methods are Future
s that must be
awaited to execute.
Implementations§
source§impl Client<ApiV1RawCodec, PdRpcClient<ApiV1RawCodec>>
impl Client<ApiV1RawCodec, PdRpcClient<ApiV1RawCodec>>
sourcepub async fn new<S: Into<String>>(pd_endpoints: Vec<S>) -> Result<Self>
pub async fn new<S: Into<String>>(pd_endpoints: Vec<S>) -> Result<Self>
Create a raw Client
and connect to the TiKV cluster.
Because TiKV is managed by a PD cluster, the endpoints for PD must be provided, not the TiKV nodes. It’s important to include more than one PD endpoint (include all endpoints, if possible), this helps avoid having a single point of failure.
Examples
let client = RawClient::new(vec!["192.168.0.100"]).await.unwrap();
sourcepub async fn new_with_config<S: Into<String>>(
pd_endpoints: Vec<S>,
config: Config
) -> Result<Self>
pub async fn new_with_config<S: Into<String>>( pd_endpoints: Vec<S>, config: Config ) -> Result<Self>
Create a raw Client
with a custom configuration, and connect to the TiKV cluster.
Because TiKV is managed by a PD cluster, the endpoints for PD must be provided, not the TiKV nodes. It’s important to include more than one PD endpoint (include all endpoints, if possible), this helps avoid having a single point of failure.
Examples
let client = RawClient::new_with_config(
vec!["192.168.0.100"],
Config::default().with_timeout(Duration::from_secs(60)),
)
.await
.unwrap();
sourcepub fn with_cf(&self, cf: ColumnFamily) -> Self
pub fn with_cf(&self, cf: ColumnFamily) -> Self
Create a new client which is a clone of self
, but which uses an explicit column family for
all requests.
This function returns a new Client
; requests created with the new client will use the
supplied column family. The original Client
can still be used (without the new
column family).
By default, raw clients use the Default
column family.
Examples
let client = RawClient::new(vec!["192.168.0.100"])
.await
.unwrap()
.with_cf(ColumnFamily::Write);
// Fetch a value at "foo" from the Write CF.
let get_request = client.get("foo".to_owned());
source§impl<Cod: Codec> Client<Cod, PdRpcClient<Cod>>
impl<Cod: Codec> Client<Cod, PdRpcClient<Cod>>
sourcepub fn with_backoff(&self, backoff: Backoff) -> Self
pub fn with_backoff(&self, backoff: Backoff) -> Self
Set the Backoff
strategy for retrying requests.
The default strategy is DEFAULT_REGION_BACKOFF
.
See Backoff
for more information.
Examples
let client = RawClient::new(vec!["192.168.0.100"])
.await
.unwrap()
.with_backoff(DEFAULT_REGION_BACKOFF);
// Fetch a value at "foo" from the Write CF.
let get_request = client.get("foo".to_owned());
sourcepub fn with_atomic_for_cas(&self) -> Self
pub fn with_atomic_for_cas(&self) -> Self
Set to use the atomic mode.
The only reason of using atomic mode is the
compare_and_swap
operation. To guarantee
the atomicity of CAS, write operations like put
or
delete
in atomic mode are more expensive. Some
operations are not supported in the mode.
source§impl<Cod: Codec, PdC: PdClient<Codec = Cod>> Client<Cod, PdC>
impl<Cod: Codec, PdC: PdClient<Codec = Cod>> Client<Cod, PdC>
sourcepub async fn get(&self, key: impl Into<Key>) -> Result<Option<Value>>
pub async fn get(&self, key: impl Into<Key>) -> Result<Option<Value>>
Create a new ‘get’ request.
Once resolved this request will result in the fetching of the value associated with the given key.
Retuning Ok(None)
indicates the key does not exist in TiKV.
Examples
let key = "TiKV".to_owned();
let req = client.get(key);
let result: Option<Value> = req.await.unwrap();
sourcepub async fn batch_get(
&self,
keys: impl IntoIterator<Item = impl Into<Key>>
) -> Result<Vec<KvPair>>
pub async fn batch_get( &self, keys: impl IntoIterator<Item = impl Into<Key>> ) -> Result<Vec<KvPair>>
Create a new ‘batch get’ request.
Once resolved this request will result in the fetching of the values associated with the given keys.
Non-existent entries will not appear in the result. The order of the keys is not retained in the result.
Examples
let keys = vec!["TiKV".to_owned(), "TiDB".to_owned()];
let req = client.batch_get(keys);
let result: Vec<KvPair> = req.await.unwrap();
sourcepub async fn put(
&self,
key: impl Into<Key>,
value: impl Into<Value>
) -> Result<()>
pub async fn put( &self, key: impl Into<Key>, value: impl Into<Value> ) -> Result<()>
Create a new ‘put’ request.
Once resolved this request will result in the setting of the value associated with the given key.
Examples
let key = "TiKV".to_owned();
let val = "TiKV".to_owned();
let req = client.put(key, val);
let result: () = req.await.unwrap();
sourcepub async fn batch_put(
&self,
pairs: impl IntoIterator<Item = impl Into<KvPair>>
) -> Result<()>
pub async fn batch_put( &self, pairs: impl IntoIterator<Item = impl Into<KvPair>> ) -> Result<()>
Create a new ‘batch put’ request.
Once resolved this request will result in the setting of the values associated with the given keys.
Examples
let kvpair1 = ("PD".to_owned(), "Go".to_owned());
let kvpair2 = ("TiKV".to_owned(), "Rust".to_owned());
let iterable = vec![kvpair1, kvpair2];
let req = client.batch_put(iterable);
let result: () = req.await.unwrap();
sourcepub async fn delete(&self, key: impl Into<Key>) -> Result<()>
pub async fn delete(&self, key: impl Into<Key>) -> Result<()>
Create a new ‘delete’ request.
Once resolved this request will result in the deletion of the given key.
It does not return an error if the key does not exist in TiKV.
Examples
let key = "TiKV".to_owned();
let req = client.delete(key);
let result: () = req.await.unwrap();
sourcepub async fn batch_delete(
&self,
keys: impl IntoIterator<Item = impl Into<Key>>
) -> Result<()>
pub async fn batch_delete( &self, keys: impl IntoIterator<Item = impl Into<Key>> ) -> Result<()>
Create a new ‘batch delete’ request.
Once resolved this request will result in the deletion of the given keys.
It does not return an error if some of the keys do not exist and will delete the others.
Examples
let keys = vec!["TiKV".to_owned(), "TiDB".to_owned()];
let req = client.batch_delete(keys);
let result: () = req.await.unwrap();
sourcepub async fn delete_range(&self, range: impl Into<BoundRange>) -> Result<()>
pub async fn delete_range(&self, range: impl Into<BoundRange>) -> Result<()>
Create a new ‘delete range’ request.
Once resolved this request will result in the deletion of all keys lying in the given range.
Examples
let inclusive_range = "TiKV"..="TiDB";
let req = client.delete_range(inclusive_range.into_owned());
let result: () = req.await.unwrap();
sourcepub async fn scan(
&self,
range: impl Into<BoundRange>,
limit: u32
) -> Result<Vec<KvPair>>
pub async fn scan( &self, range: impl Into<BoundRange>, limit: u32 ) -> Result<Vec<KvPair>>
Create a new ‘scan’ request.
Once resolved this request will result in a Vec
of key-value pairs that lies in the specified range.
If the number of eligible key-value pairs are greater than limit
,
only the first limit
pairs are returned, ordered by the key.
Examples
let inclusive_range = "TiKV"..="TiDB";
let req = client.scan(inclusive_range.into_owned(), 2);
let result: Vec<KvPair> = req.await.unwrap();
sourcepub async fn scan_keys(
&self,
range: impl Into<BoundRange>,
limit: u32
) -> Result<Vec<Key>>
pub async fn scan_keys( &self, range: impl Into<BoundRange>, limit: u32 ) -> Result<Vec<Key>>
Create a new ‘scan’ request that only returns the keys.
Once resolved this request will result in a Vec
of keys that lies in the specified range.
If the number of eligible keys are greater than limit
,
only the first limit
pairs are returned, ordered by the key.
Examples
let inclusive_range = "TiKV"..="TiDB";
let req = client.scan_keys(inclusive_range.into_owned(), 2);
let result: Vec<Key> = req.await.unwrap();
sourcepub async fn batch_scan(
&self,
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
each_limit: u32
) -> Result<Vec<KvPair>>
pub async fn batch_scan( &self, ranges: impl IntoIterator<Item = impl Into<BoundRange>>, each_limit: u32 ) -> Result<Vec<KvPair>>
Create a new ‘batch scan’ request.
Once resolved this request will result in a set of scanners over the given keys.
Warning: This method is experimental. The each_limit
parameter does not work as expected.
It does not limit the number of results returned of each range,
instead it limits the number of results in each region of each range.
As a result, you may get more than each_limit
key-value pairs for each range.
But you should not miss any entries.
Examples
let inclusive_range1 = "TiDB"..="TiKV";
let inclusive_range2 = "TiKV"..="TiSpark";
let iterable = vec![inclusive_range1.into_owned(), inclusive_range2.into_owned()];
let req = client.batch_scan(iterable, 2);
let result = req.await;
sourcepub async fn batch_scan_keys(
&self,
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
each_limit: u32
) -> Result<Vec<Key>>
pub async fn batch_scan_keys( &self, ranges: impl IntoIterator<Item = impl Into<BoundRange>>, each_limit: u32 ) -> Result<Vec<Key>>
Create a new ‘batch scan’ request that only returns the keys.
Once resolved this request will result in a set of scanners over the given keys.
Warning: This method is experimental.
The each_limit
parameter does not limit the number of results returned of each range,
instead it limits the number of results in each region of each range.
As a result, you may get more than each_limit
key-value pairs for each range,
but you should not miss any entries.
Examples
let inclusive_range1 = "TiDB"..="TiKV";
let inclusive_range2 = "TiKV"..="TiSpark";
let iterable = vec![inclusive_range1.into_owned(), inclusive_range2.into_owned()];
let req = client.batch_scan(iterable, 2);
let result = req.await;
sourcepub async fn compare_and_swap(
&self,
key: impl Into<Key>,
previous_value: impl Into<Option<Value>>,
new_value: impl Into<Value>
) -> Result<(Option<Value>, bool)>
pub async fn compare_and_swap( &self, key: impl Into<Key>, previous_value: impl Into<Option<Value>>, new_value: impl Into<Value> ) -> Result<(Option<Value>, bool)>
Create a new atomic ‘compare and set’ request.
Once resolved this request will result in an atomic `compare and set’ operation for the given key.
If the value retrived is equal to current_value
, new_value
is
written.
Return Value
A tuple is returned if successful: the previous value and whether the value is swapped
pub async fn coprocessor( &self, copr_name: impl Into<String>, copr_version_req: impl Into<String>, ranges: impl IntoIterator<Item = impl Into<BoundRange>>, request_builder: impl Fn(Region, Vec<Range<Key>>) -> Vec<u8> + Send + Sync + 'static ) -> Result<Vec<(Vec<u8>, Vec<Range<Key>>)>>
Trait Implementations§
Auto Trait Implementations§
impl<Cod, PdC> RefUnwindSafe for Client<Cod, PdC>where PdC: RefUnwindSafe,
impl<Cod, PdC> Send for Client<Cod, PdC>
impl<Cod, PdC> Sync for Client<Cod, PdC>
impl<Cod, PdC> Unpin for Client<Cod, PdC>
impl<Cod, PdC> UnwindSafe for Client<Cod, PdC>where PdC: RefUnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request