use crate::{
db::{
data::{DataKey, RawRow, StorageKey},
index::{IndexStore, RawIndexEntry, RawIndexKey},
},
error::InternalError,
traits::{EntityKind, EntityValue},
types::EntityTag,
};
use std::{cell::RefCell, ops::Bound, thread::LocalKey};
#[derive(Clone, Copy)]
pub(in crate::db) struct IndexReadContract<'a> {
store_path: &'a str,
unique: bool,
fields: &'a str,
}
impl<'a> IndexReadContract<'a> {
#[must_use]
pub(in crate::db) const fn new(store_path: &'a str, unique: bool, fields: &'a str) -> Self {
Self {
store_path,
unique,
fields,
}
}
#[must_use]
pub(in crate::db) const fn store_path(self) -> &'a str {
self.store_path
}
#[must_use]
pub(in crate::db) const fn unique(self) -> bool {
self.unique
}
#[must_use]
pub(in crate::db) const fn fields(self) -> &'a str {
self.fields
}
}
pub(in crate::db) trait SealedPrimaryRowReader<E: EntityKind + EntityValue> {}
pub(in crate::db) trait SealedStructuralPrimaryRowReader {}
pub(in crate::db) trait SealedIndexEntryReader<E: EntityKind + EntityValue> {}
pub(in crate::db) trait SealedStructuralIndexEntryReader {}
pub(in crate::db) trait PrimaryRowReader<E: EntityKind + EntityValue>:
SealedPrimaryRowReader<E>
{
fn read_primary_row(&self, key: &DataKey) -> Result<Option<RawRow>, InternalError>;
}
pub(in crate::db) trait StructuralPrimaryRowReader:
SealedStructuralPrimaryRowReader
{
fn read_primary_row_structural(&self, key: &DataKey) -> Result<Option<RawRow>, InternalError>;
}
pub(in crate::db) trait IndexEntryReader<E: EntityKind + EntityValue>:
SealedIndexEntryReader<E>
{
fn read_index_entry(
&self,
index_store: &'static LocalKey<RefCell<IndexStore>>,
key: &RawIndexKey,
) -> Result<Option<RawIndexEntry>, InternalError>;
fn read_index_keys_in_raw_range(
&self,
index_store: &'static LocalKey<RefCell<IndexStore>>,
index: IndexReadContract<'_>,
bounds: (&Bound<RawIndexKey>, &Bound<RawIndexKey>),
limit: usize,
) -> Result<Vec<StorageKey>, InternalError>;
}
pub(in crate::db) trait StructuralIndexEntryReader:
SealedStructuralIndexEntryReader
{
fn read_index_entry_structural(
&self,
index_store: &'static LocalKey<RefCell<IndexStore>>,
key: &RawIndexKey,
) -> Result<Option<RawIndexEntry>, InternalError>;
fn read_index_keys_in_raw_range_structural(
&self,
entity_path: &'static str,
entity_tag: EntityTag,
index_store: &'static LocalKey<RefCell<IndexStore>>,
index: IndexReadContract<'_>,
bounds: (&Bound<RawIndexKey>, &Bound<RawIndexKey>),
limit: usize,
) -> Result<Vec<StorageKey>, InternalError>;
}
impl<E> StructuralIndexEntryReader for dyn IndexEntryReader<E> + '_
where
E: EntityKind + EntityValue,
{
fn read_index_entry_structural(
&self,
index_store: &'static LocalKey<RefCell<IndexStore>>,
key: &RawIndexKey,
) -> Result<Option<RawIndexEntry>, InternalError> {
self.read_index_entry(index_store, key)
}
fn read_index_keys_in_raw_range_structural(
&self,
_entity_path: &'static str,
_entity_tag: EntityTag,
index_store: &'static LocalKey<RefCell<IndexStore>>,
index: IndexReadContract<'_>,
bounds: (&Bound<RawIndexKey>, &Bound<RawIndexKey>),
limit: usize,
) -> Result<Vec<StorageKey>, InternalError> {
self.read_index_keys_in_raw_range(index_store, index, bounds, limit)
}
}
impl<E> StructuralPrimaryRowReader for dyn PrimaryRowReader<E> + '_
where
E: EntityKind + EntityValue,
{
fn read_primary_row_structural(&self, key: &DataKey) -> Result<Option<RawRow>, InternalError> {
self.read_primary_row(key)
}
}
impl<E> SealedStructuralPrimaryRowReader for dyn PrimaryRowReader<E> + '_ where
E: EntityKind + EntityValue
{
}
impl<E> SealedStructuralIndexEntryReader for dyn IndexEntryReader<E> + '_ where
E: EntityKind + EntityValue
{
}