tikv_client/transaction/
snapshot.rs

1// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
2
3use 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/// A read-only transaction which reads at the given timestamp.
18///
19/// It behaves as if the snapshot was taken at the given timestamp,
20/// i.e. it can read operations happened before the timestamp,
21/// but ignores operations after the timestamp.
22///
23/// See the [Transaction](struct@crate::Transaction) docs for more information on the methods.
24#[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    /// Get the value associated with the given key.
32    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    /// Check whether the key exists.
38    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    /// Get the values associated with the given keys.
44    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    /// Scan a range, return at most `limit` key-value pairs that lying in the range.
53    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    /// Scan a range, return at most `limit` keys that lying in the range.
63    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    /// Similar to scan, but in the reverse direction.
73    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    /// Similar to scan_keys, but in the reverse direction.
83    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}