use std::ops::ControlFlow;
use armour_core::{GetRefs, GetType, StorageBackend, StorageClass};
use crate::access::Access;
use crate::armour::collection::Collection;
use crate::key::Key;
use crate::{CollectionMeta, DbResult};
mod sealed {
pub trait Sealed {}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ScanOutcome {
pub skipped: u64,
pub stopped_at: Option<String>,
}
impl ScanOutcome {
pub fn is_complete(&self) -> bool {
self.skipped == 0 && self.stopped_at.is_none()
}
pub(super) fn merge(&mut self, other: &ScanOutcome) {
self.skipped = self.skipped.max(other.skipped);
if self.stopped_at.is_none() {
self.stopped_at.clone_from(&other.stopped_at);
}
}
}
pub trait IndexTarget: SchemaCollection {
fn put_entry(&self, k: &Self::K, v: &Self::V) -> DbResult<()>;
fn delete_entry(&self, k: &Self::K) -> DbResult<()>;
}
pub trait SchemaCollection: Collection + Send + Sync + 'static + sealed::Sealed {
type K: Key + GetRefs + GetType + 'static;
type V: CollectionMeta + GetRefs + 'static;
const STORAGE: StorageClass;
fn scan(&self, f: &mut dyn FnMut(&Self::K, &Self::V)) -> ScanOutcome;
fn undecodable_rows(&self) -> u64 {
0
}
#[doc(hidden)]
fn effective_projection_limits(&self) -> Option<(usize, usize)> {
None
}
fn contains_key(&self, key: &Self::K) -> DbResult<bool>;
fn with_value(&self, key: &Self::K, f: &mut dyn FnMut(&Self::V)) -> DbResult<bool>;
}
impl<S, K, V> sealed::Sealed for S
where
S: Access<Key = K, Val = V> + Collection,
K: Key + GetRefs + GetType + 'static,
V: CollectionMeta + GetRefs + 'static,
{
}
impl<S, K, V> SchemaCollection for S
where
S: Access<Key = K, Val = V> + Collection,
K: Key + GetRefs + GetType + 'static,
V: CollectionMeta + GetRefs + 'static,
{
type K = K;
type V = V;
const STORAGE: StorageClass = S::STORAGE;
fn scan(&self, f: &mut dyn FnMut(&Self::K, &Self::V)) -> ScanOutcome {
let mut outcome = ScanOutcome::default();
let mut last_error: Option<String> = None;
Access::scan_all(self, &mut |row| {
match row {
Ok((k, v)) => {
last_error = None;
f(k, v);
}
Err(e) => {
outcome.skipped += 1;
last_error = Some(e.to_string());
}
}
ControlFlow::Continue(())
});
if <S as Access>::STORAGE.backend == StorageBackend::Segmented
&& let Some(error) = last_error
{
outcome.skipped -= 1;
outcome.stopped_at = Some(error);
}
outcome
}
fn undecodable_rows(&self) -> u64 {
Access::undecodable_rows(self)
}
fn effective_projection_limits(&self) -> Option<(usize, usize)> {
Access::effective_projection_limits(self)
}
fn contains_key(&self, key: &Self::K) -> DbResult<bool> {
Access::contains(self, key)
}
fn with_value(&self, key: &Self::K, f: &mut dyn FnMut(&Self::V)) -> DbResult<bool> {
Ok(Access::with_value(self, key, |v| f(v))?.is_some())
}
}
impl<S, K, V> IndexTarget for S
where
S: Access<Key = K, Val = V> + Collection,
K: Key + GetRefs + GetType + 'static,
V: CollectionMeta + GetRefs + 'static,
{
fn put_entry(&self, k: &Self::K, v: &Self::V) -> DbResult<()> {
Access::put_ref(self, k, v)
}
fn delete_entry(&self, k: &Self::K) -> DbResult<()> {
Access::delete(self, k)
}
}