use reifydb_core::interface::store::{SingleVersionContains, SingleVersionGet, SingleVersionRow};
use reifydb_runtime::sync::rwlock::{ArcRwLock, OwnedRwLockReadGuard};
use reifydb_value::{Result, util::hex::encode};
use super::*;
use crate::error::TransactionError;
pub struct KeyReadLock {
pub(super) _guard: OwnedRwLockReadGuard<()>,
}
impl KeyReadLock {
pub(super) fn new(lock: ArcRwLock<()>) -> Self {
Self {
_guard: lock.read(),
}
}
}
pub struct SingleReadTransaction<'a> {
pub(super) inner: &'a SingleTransactionInner,
pub(super) keys: Vec<EncodedKey>,
pub(super) _key_locks: Vec<KeyReadLock>,
}
impl<'a> SingleReadTransaction<'a> {
#[inline]
fn check_key_allowed(&self, key: &EncodedKey) -> Result<()> {
if self.keys.iter().any(|k| k == key) {
Ok(())
} else {
Err(TransactionError::KeyOutOfScope {
key: encode(key),
}
.into())
}
}
pub fn get(&mut self, key: &EncodedKey) -> Result<Option<SingleVersionRow>> {
self.check_key_allowed(key)?;
let store = self.inner.store.read().clone();
SingleVersionGet::get(&store, key)
}
pub fn contains_key(&mut self, key: &EncodedKey) -> Result<bool> {
self.check_key_allowed(key)?;
let store = self.inner.store.read().clone();
SingleVersionContains::contains(&store, key)
}
}