Skip to main content

tikv_client/transaction/
snapshot.rs

1// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
2
3use derive_new::new;
4use log::{debug, trace};
5
6use crate::BoundRange;
7use crate::Key;
8use crate::KvPair;
9use crate::Result;
10use crate::Transaction;
11use crate::Value;
12
13/// A read-only transaction which reads at the given timestamp.
14///
15/// It behaves as if the snapshot was taken at the given timestamp,
16/// i.e. it can read operations happened before the timestamp,
17/// but ignores operations after the timestamp.
18///
19/// See the [Transaction](struct@crate::Transaction) docs for more information on the methods.
20#[derive(new)]
21pub struct Snapshot {
22    transaction: Transaction,
23}
24
25impl Snapshot {
26    /// Get the value associated with the given key.
27    pub async fn get(&mut self, key: impl Into<Key>) -> Result<Option<Value>> {
28        trace!("invoking get request on snapshot");
29        self.transaction.get(key).await
30    }
31
32    /// Check whether the key exists.
33    pub async fn key_exists(&mut self, key: impl Into<Key>) -> Result<bool> {
34        debug!("invoking key_exists request on snapshot");
35        self.transaction.key_exists(key).await
36    }
37
38    /// Get the values associated with the given keys.
39    pub async fn batch_get(
40        &mut self,
41        keys: impl IntoIterator<Item = impl Into<Key>>,
42    ) -> Result<impl Iterator<Item = KvPair>> {
43        debug!("invoking batch_get request on snapshot");
44        self.transaction.batch_get(keys).await
45    }
46
47    /// Scan a range, return at most `limit` key-value pairs that lying in the range.
48    pub async fn scan(
49        &mut self,
50        range: impl Into<BoundRange>,
51        limit: u32,
52    ) -> Result<impl Iterator<Item = KvPair>> {
53        debug!("invoking scan request on snapshot");
54        self.transaction.scan(range, limit).await
55    }
56
57    /// Scan a range, return at most `limit` keys that lying in the range.
58    pub async fn scan_keys(
59        &mut self,
60        range: impl Into<BoundRange>,
61        limit: u32,
62    ) -> Result<impl Iterator<Item = Key>> {
63        debug!("invoking scan_keys request on snapshot");
64        self.transaction.scan_keys(range, limit).await
65    }
66
67    /// Similar to scan, but in the reverse direction.
68    pub async fn scan_reverse(
69        &mut self,
70        range: impl Into<BoundRange>,
71        limit: u32,
72    ) -> Result<impl Iterator<Item = KvPair>> {
73        debug!("invoking scan_reverse request on snapshot");
74        self.transaction.scan_reverse(range, limit).await
75    }
76
77    /// Similar to scan_keys, but in the reverse direction.
78    pub async fn scan_keys_reverse(
79        &mut self,
80        range: impl Into<BoundRange>,
81        limit: u32,
82    ) -> Result<impl Iterator<Item = Key>> {
83        debug!("invoking scan_keys_reverse request on snapshot");
84        self.transaction.scan_keys_reverse(range, limit).await
85    }
86}