use crate::db::{
data::DataStore,
index::{IndexState, IndexStore},
schema::SchemaStore,
};
use std::{cell::RefCell, thread::LocalKey};
#[derive(Clone, Copy, Debug)]
pub struct StoreHandle {
data: &'static LocalKey<RefCell<DataStore>>,
index: &'static LocalKey<RefCell<IndexStore>>,
schema: &'static LocalKey<RefCell<SchemaStore>>,
}
impl StoreHandle {
#[must_use]
pub const fn new(
data: &'static LocalKey<RefCell<DataStore>>,
index: &'static LocalKey<RefCell<IndexStore>>,
schema: &'static LocalKey<RefCell<SchemaStore>>,
) -> Self {
Self {
data,
index,
schema,
}
}
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)
}
pub fn with_schema<R>(&self, f: impl FnOnce(&SchemaStore) -> R) -> R {
self.schema.with_borrow(f)
}
pub fn with_schema_mut<R>(&self, f: impl FnOnce(&mut SchemaStore) -> R) -> R {
self.schema.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
}
#[must_use]
pub const fn schema_store(&self) -> &'static LocalKey<RefCell<SchemaStore>> {
self.schema
}
}