tikv_client/transaction/
snapshot.rs1use derive_new::new;
4use log::debug;
5use std::marker::PhantomData;
6
7use crate::codec::ApiV1TxnCodec;
8use crate::pd::{PdClient, PdRpcClient};
9use crate::request::codec::Codec;
10use crate::BoundRange;
11use crate::Key;
12use crate::KvPair;
13use crate::Result;
14use crate::Transaction;
15use crate::Value;
16
17#[derive(new)]
25pub struct Snapshot<Cod: Codec = ApiV1TxnCodec, PdC: PdClient<Codec = Cod> = PdRpcClient<Cod>> {
26 transaction: Transaction<Cod, PdC>,
27 phantom: PhantomData<Cod>,
28}
29
30impl<Cod: Codec, PdC: PdClient<Codec = Cod>> Snapshot<Cod, PdC> {
31 pub async fn get(&mut self, key: impl Into<Key>) -> Result<Option<Value>> {
33 debug!("invoking get request on snapshot");
34 self.transaction.get(key).await
35 }
36
37 pub async fn key_exists(&mut self, key: impl Into<Key>) -> Result<bool> {
39 debug!("invoking key_exists request on snapshot");
40 self.transaction.key_exists(key).await
41 }
42
43 pub async fn batch_get(
45 &mut self,
46 keys: impl IntoIterator<Item = impl Into<Key>>,
47 ) -> Result<impl Iterator<Item = KvPair>> {
48 debug!("invoking batch_get request on snapshot");
49 self.transaction.batch_get(keys).await
50 }
51
52 pub async fn scan(
54 &mut self,
55 range: impl Into<BoundRange>,
56 limit: u32,
57 ) -> Result<impl Iterator<Item = KvPair>> {
58 debug!("invoking scan request on snapshot");
59 self.transaction.scan(range, limit).await
60 }
61
62 pub async fn scan_keys(
64 &mut self,
65 range: impl Into<BoundRange>,
66 limit: u32,
67 ) -> Result<impl Iterator<Item = Key>> {
68 debug!("invoking scan_keys request on snapshot");
69 self.transaction.scan_keys(range, limit).await
70 }
71
72 pub async fn scan_reverse(
74 &mut self,
75 range: impl Into<BoundRange>,
76 limit: u32,
77 ) -> Result<impl Iterator<Item = KvPair>> {
78 debug!("invoking scan_reverse request on snapshot");
79 self.transaction.scan_reverse(range, limit).await
80 }
81
82 pub async fn scan_keys_reverse(
84 &mut self,
85 range: impl Into<BoundRange>,
86 limit: u32,
87 ) -> Result<impl Iterator<Item = Key>> {
88 debug!("invoking scan_keys_reverse request on snapshot");
89 self.transaction.scan_keys_reverse(range, limit).await
90 }
91}