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