use crate::db::{
data::DataStore,
index::{IndexState, IndexStore},
};
use std::{cell::RefCell, thread::LocalKey};
#[derive(Clone, Copy, Debug)]
pub struct StoreHandle {
data: &'static LocalKey<RefCell<DataStore>>,
index: &'static LocalKey<RefCell<IndexStore>>,
}
impl StoreHandle {
#[must_use]
pub const fn new(
data: &'static LocalKey<RefCell<DataStore>>,
index: &'static LocalKey<RefCell<IndexStore>>,
) -> Self {
Self { data, index }
}
pub fn with_data<R>(&self, f: impl FnOnce(&DataStore) -> R) -> R {
#[cfg(feature = "diagnostics")]
{
crate::db::physical_access::measure_physical_access_operation(|| {
self.data.with_borrow(f)
})
}
#[cfg(not(feature = "diagnostics"))]
{
self.data.with_borrow(f)
}
}
pub fn with_data_mut<R>(&self, f: impl FnOnce(&mut DataStore) -> R) -> R {
self.data.with_borrow_mut(f)
}
pub fn with_index<R>(&self, f: impl FnOnce(&IndexStore) -> R) -> R {
#[cfg(feature = "diagnostics")]
{
crate::db::physical_access::measure_physical_access_operation(|| {
self.index.with_borrow(f)
})
}
#[cfg(not(feature = "diagnostics"))]
{
self.index.with_borrow(f)
}
}
pub fn with_index_mut<R>(&self, f: impl FnOnce(&mut IndexStore) -> R) -> R {
self.index.with_borrow_mut(f)
}
#[must_use]
pub(in crate::db) fn index_state(&self) -> IndexState {
self.with_index(IndexStore::state)
}
pub(in crate::db) fn mark_index_building(&self) {
self.with_index_mut(IndexStore::mark_building);
}
pub(in crate::db) fn mark_index_ready(&self) {
self.with_index_mut(IndexStore::mark_ready);
}
#[must_use]
pub const fn data_store(&self) -> &'static LocalKey<RefCell<DataStore>> {
self.data
}
#[must_use]
pub const fn index_store(&self) -> &'static LocalKey<RefCell<IndexStore>> {
self.index
}
}