reifydb_transaction/single/
read.rs1use reifydb_core::interface::store::{SingleVersionContains, SingleVersionGet, SingleVersionRow};
5use reifydb_runtime::sync::rwlock::{ArcRwLock, OwnedRwLockReadGuard};
6use reifydb_value::{Result, util::hex::encode};
7
8use super::*;
9use crate::error::TransactionError;
10
11pub struct KeyReadLock {
12 pub(super) _guard: OwnedRwLockReadGuard<()>,
13}
14
15impl KeyReadLock {
16 pub(super) fn new(lock: ArcRwLock<()>) -> Self {
17 Self {
18 _guard: lock.read(),
19 }
20 }
21}
22
23pub struct SingleReadTransaction<'a> {
24 pub(super) inner: &'a SingleTransactionInner,
25 pub(super) keys: Vec<EncodedKey>,
26 pub(super) _key_locks: Vec<KeyReadLock>,
27}
28
29impl<'a> SingleReadTransaction<'a> {
30 #[inline]
31 fn check_key_allowed(&self, key: &EncodedKey) -> Result<()> {
32 if self.keys.iter().any(|k| k == key) {
33 Ok(())
34 } else {
35 Err(TransactionError::KeyOutOfScope {
36 key: encode(key),
37 }
38 .into())
39 }
40 }
41
42 pub fn get(&mut self, key: &EncodedKey) -> Result<Option<SingleVersionRow>> {
43 self.check_key_allowed(key)?;
44 let store = self.inner.store.read().clone();
45 SingleVersionGet::get(&store, key)
46 }
47
48 pub fn contains_key(&mut self, key: &EncodedKey) -> Result<bool> {
49 self.check_key_allowed(key)?;
50 let store = self.inner.store.read().clone();
51 SingleVersionContains::contains(&store, key)
52 }
53}