reifydb_transaction/single/
read.rs1use std::mem;
5
6use reifydb_core::interface::store::{SingleVersionContains, SingleVersionGet, SingleVersionRow};
7use reifydb_runtime::sync::rwlock::{RwLock, RwLockReadGuard};
8use reifydb_type::{Result, util::hex};
9
10use super::*;
11use crate::error::TransactionError;
12
13pub struct KeyReadLock {
17 pub(super) _guard: RwLockReadGuard<'static, ()>,
18 pub(super) _arc: Arc<RwLock<()>>,
19}
20
21impl KeyReadLock {
22 pub(super) fn new(arc: Arc<RwLock<()>>) -> Self {
32 let guard = arc.read();
34
35 let guard = unsafe { mem::transmute::<RwLockReadGuard<'_, ()>, RwLockReadGuard<'static, ()>>(guard) };
39
40 Self {
41 _arc: arc,
42 _guard: guard,
43 }
44 }
45}
46
47pub struct SingleReadTransaction<'a> {
48 pub(super) inner: &'a SingleTransactionInner,
49 pub(super) keys: Vec<EncodedKey>,
50 pub(super) _key_locks: Vec<KeyReadLock>,
51}
52
53impl<'a> SingleReadTransaction<'a> {
54 #[inline]
55 fn check_key_allowed(&self, key: &EncodedKey) -> Result<()> {
56 if self.keys.iter().any(|k| k == key) {
57 Ok(())
58 } else {
59 Err(TransactionError::KeyOutOfScope {
60 key: hex::encode(key),
61 }
62 .into())
63 }
64 }
65
66 pub fn get(&mut self, key: &EncodedKey) -> Result<Option<SingleVersionRow>> {
67 self.check_key_allowed(key)?;
68 let store = self.inner.store.read().clone();
69 SingleVersionGet::get(&store, key)
70 }
71
72 pub fn contains_key(&mut self, key: &EncodedKey) -> Result<bool> {
73 self.check_key_allowed(key)?;
74 let store = self.inner.store.read().clone();
75 SingleVersionContains::contains(&store, key)
76 }
77}