use std::convert::Infallible;
use std::hash::Hash;
use std::marker::PhantomData;
use std::ops::{Bound, ControlFlow};
use armour_core::{CollectionShape, StorageBackend, StorageClass, Typ, ValueModel};
use crate::access::traits::{Access, FallibleRowFn, KeyFn, RowFn};
use crate::armour::inspection::ScanCapability;
use crate::codec::{Codec, InspectionEncoding};
use crate::error::{DbError, DbResult};
use crate::held_ops::HeldOps;
use crate::key::Key;
use crate::seg::{SegMap, SegTree};
pub(crate) struct SegNoShard<'a, K, T> {
never: Infallible,
_marker: PhantomData<&'a (K, T)>,
}
impl<K, T> HeldOps for SegNoShard<'_, K, T> {
type Key = K;
type Val = T;
fn try_contains(&mut self, _key: &K) -> DbResult<bool> {
match self.never {}
}
fn with_value<R>(&mut self, _key: &K, _f: impl FnOnce(&T) -> R) -> DbResult<Option<R>> {
match self.never {}
}
fn put(&mut self, _key: &K, _value: T) -> DbResult<()> {
match self.never {}
}
fn delete(&mut self, _key: &K) -> DbResult<()> {
match self.never {}
}
}
impl<K, T, C, H> Access for SegMap<K, T, C, H>
where
K: Key + Hash + Eq + Send + Sync + 'static,
T: Send + Sync + 'static,
C: Codec<T> + 'static,
H: crate::hook::TypedWriteHook<K, T> + 'static,
{
type Key = K;
type Val = T;
type Shard<'a>
= SegNoShard<'a, K, T>
where
Self: 'a;
const STORAGE: StorageClass = StorageClass {
model: ValueModel::Segmented,
shape: CollectionShape::Map,
backend: StorageBackend::Segmented,
};
const RAW_WIRE_BYTES: bool = true;
const LEN_IS_EXACT: bool = false;
fn effective_projection_limits(&self) -> Option<(usize, usize)> {
Some(SegMap::effective_projection_limits(self))
}
fn encoding(&self, _ty: &Typ) -> InspectionEncoding {
C::INSPECTION_ENCODING
}
fn approx_len(&self) -> u64 {
self.len() as u64
}
fn rpc_exact_count(&self) -> DbResult<u64> {
self.core().count_exact()
}
fn inspection_exact_count(&self) -> DbResult<u64> {
self.core().count_exact()
}
fn scan_capability(&self) -> ScanCapability {
ScanCapability::UnorderedSnapshot
}
fn reversed(&self) -> bool {
false
}
fn with_value<R>(&self, key: &K, f: impl FnOnce(&T) -> R) -> DbResult<Option<R>> {
Ok(self.try_get(key)?.map(|value| f(&value)))
}
fn contains(&self, key: &K) -> DbResult<bool> {
self.try_contains(key)
}
fn stored_bytes(&self, key: &K) -> DbResult<Option<Vec<u8>>> {
self.core().get(key)
}
fn with_first<R>(&self, _f: impl FnOnce(&K, &T) -> R) -> DbResult<Option<R>> {
Err(DbError::NotImplemented)
}
fn with_last<R>(&self, _f: impl FnOnce(&K, &T) -> R) -> DbResult<Option<R>> {
Err(DbError::NotImplemented)
}
fn scan_range(&self, _start: Bound<&K>, _end: Bound<&K>, _f: RowFn<'_, K, T>) -> DbResult<()> {
Err(DbError::NotImplemented)
}
fn try_scan_range(
&self,
_start: Bound<&K>,
_end: Bound<&K>,
_batch: usize,
_f: RowFn<'_, K, T>,
) -> DbResult<()> {
Err(DbError::NotImplemented)
}
fn scan_keys(&self, _start: Bound<&K>, _end: Bound<&K>, _f: KeyFn<'_, K>) -> DbResult<()> {
Err(DbError::NotImplemented)
}
fn scan_all(&self, f: FallibleRowFn<'_, K, T>) {
self.walk_rows(true, &mut |row| match row {
Ok((key, value)) => f(Ok((&key, &value))),
Err(e) => f(Err(e)),
});
}
fn undecodable_rows(&self) -> u64 {
let mut count = 0u64;
self.walk_rows(false, &mut |row| {
if row.is_err() {
count += 1;
}
ControlFlow::Continue(())
});
count
}
fn put(&self, key: &K, value: T) -> DbResult<()> {
SegMap::put(self, key, &value)
}
fn put_ref(&self, key: &K, value: &T) -> DbResult<()> {
SegMap::put(self, key, value)
}
fn delete(&self, key: &K) -> DbResult<()> {
SegMap::delete(self, key)
}
fn atomic_at<R>(
&self,
_key: &K,
_f: impl FnOnce(&mut Self::Shard<'_>) -> DbResult<R>,
) -> DbResult<R> {
Err(DbError::NotImplemented)
}
fn insert_if_absent(&self, key: &K, value: T) -> DbResult<()> {
SegMap::insert(self, key, &value)
}
}
impl<K, T, C, H> crate::access::ManagedOpen for SegMap<K, T, C, H>
where
K: Key + Hash + Eq + Send + Sync + 'static,
T: Send + Sync + 'static,
C: Codec<T> + 'static,
H: crate::hook::TypedWriteHook<K, T> + 'static,
{
type Key = K;
type Val = T;
fn migrate_inner(
&self,
mfn: &crate::armour::migration::TypedMigrationFn<K, T>,
_fire_init: bool,
) -> DbResult<usize> {
self.migrate_rows(mfn)
}
fn flush_for_migration(&self) -> DbResult<()> {
self.durable_flush()
}
fn projections_enabled(&self) -> bool {
self.hook.projection_targets().is_some() || self.hook.counter_targets().is_some()
}
fn reconcile_projections(&self) -> DbResult<()> {
SegMap::reconcile_projections(self)
}
fn backfill_index_target(&self, name: &'static str) -> DbResult<u64> {
SegMap::backfill_index_target(self, name)
}
fn reconcile_counters(&self) -> DbResult<u64> {
SegMap::reconcile_counters(self)
}
fn counter_target_names(&self) -> Vec<&'static str> {
SegMap::counter_target_names(self)
}
fn index_targets(&self) -> Vec<crate::projection::ErasedTarget> {
self.hook
.projection_targets()
.map(|t| t.erased_targets().to_vec())
.unwrap_or_default()
}
fn register_tx_metadata(&self, db: &crate::armour::Db) -> DbResult<()> {
if let Some(targets) = self.hook.projection_targets() {
db.tx_register_projection_targets(targets)?;
}
if let Some(targets) = self.hook.counter_targets() {
db.tx_register_counter_targets(targets)?;
}
if H::EMITS
&& let Some(targets) = self.hook.stream_targets()
{
db.tx_register_stream_targets(targets)?;
}
Ok(())
}
fn replay_init(&self) {}
fn start_background(&self) -> DbResult<()> {
self.core().start_background()
}
fn bind_tx_arc(arc: &std::sync::Arc<Self>) {
let _ = arc.tx_self.set(std::sync::Arc::downgrade(arc));
}
fn replay_pending_tx_log(
&self,
db: &crate::armour::Db,
stored_fingerprint: Option<u64>,
format_plan: Option<&crate::format_migration::plan::MigrationPlan>,
) -> DbResult<usize> {
crate::txn::replay::replay_pending(db, self, stored_fingerprint, format_plan)
}
}
impl<K, T, C, H> Access for SegTree<K, T, C, H>
where
K: Key + Hash + Eq + Send + Sync + 'static,
T: Send + Sync + 'static,
C: Codec<T> + 'static,
H: crate::hook::TypedWriteHook<K, T> + 'static,
{
type Key = K;
type Val = T;
type Shard<'a>
= SegNoShard<'a, K, T>
where
Self: 'a;
const STORAGE: StorageClass = StorageClass {
model: ValueModel::Segmented,
shape: CollectionShape::Tree,
backend: StorageBackend::Segmented,
};
const RAW_WIRE_BYTES: bool = true;
const LEN_IS_EXACT: bool = false;
fn effective_projection_limits(&self) -> Option<(usize, usize)> {
Some(SegTree::effective_projection_limits(self))
}
fn encoding(&self, _ty: &Typ) -> InspectionEncoding {
C::INSPECTION_ENCODING
}
fn approx_len(&self) -> u64 {
self.len() as u64
}
fn rpc_exact_count(&self) -> DbResult<u64> {
self.core().count_exact()
}
fn inspection_exact_count(&self) -> DbResult<u64> {
self.core().count_exact()
}
fn scan_capability(&self) -> ScanCapability {
ScanCapability::Ordered
}
fn reversed(&self) -> bool {
false
}
fn with_value<R>(&self, key: &K, f: impl FnOnce(&T) -> R) -> DbResult<Option<R>> {
Ok(self.try_get(key)?.map(|value| f(&value)))
}
fn contains(&self, key: &K) -> DbResult<bool> {
self.try_contains(key)
}
fn stored_bytes(&self, key: &K) -> DbResult<Option<Vec<u8>>> {
self.core().get(key)
}
fn with_first<R>(&self, f: impl FnOnce(&K, &T) -> R) -> DbResult<Option<R>> {
Ok(self.first()?.map(|(key, value)| f(&key, &value)))
}
fn with_last<R>(&self, f: impl FnOnce(&K, &T) -> R) -> DbResult<Option<R>> {
Ok(self.last()?.map(|(key, value)| f(&key, &value)))
}
fn scan_range(&self, start: Bound<&K>, end: Bound<&K>, f: RowFn<'_, K, T>) -> DbResult<()> {
for row in self.range_bounds(start, end) {
let (key, value) = row?;
if f(&key, &value)?.is_break() {
break;
}
}
Ok(())
}
fn try_scan_range(
&self,
start: Bound<&K>,
end: Bound<&K>,
_batch: usize,
f: RowFn<'_, K, T>,
) -> DbResult<()> {
self.scan_range(start, end, f)
}
fn scan_keys(&self, start: Bound<&K>, end: Bound<&K>, f: KeyFn<'_, K>) -> DbResult<()> {
for key in SegTree::scan_keys(self, crate::seg::SegRange::range(start, end)) {
if f(&key?)?.is_break() {
break;
}
}
Ok(())
}
fn scan_all(&self, f: FallibleRowFn<'_, K, T>) {
self.walk_rows(true, &mut |row| match row {
Ok((key, value)) => f(Ok((&key, &value))),
Err(e) => f(Err(e)),
});
}
fn undecodable_rows(&self) -> u64 {
let mut count = 0u64;
self.walk_rows(false, &mut |row| {
if row.is_err() {
count += 1;
}
ControlFlow::Continue(())
});
count
}
fn put(&self, key: &K, value: T) -> DbResult<()> {
SegTree::put(self, key, &value)
}
fn put_ref(&self, key: &K, value: &T) -> DbResult<()> {
SegTree::put(self, key, value)
}
fn delete(&self, key: &K) -> DbResult<()> {
SegTree::delete(self, key)
}
fn atomic_at<R>(
&self,
_key: &K,
_f: impl FnOnce(&mut Self::Shard<'_>) -> DbResult<R>,
) -> DbResult<R> {
Err(DbError::NotImplemented)
}
fn insert_if_absent(&self, key: &K, value: T) -> DbResult<()> {
SegTree::insert(self, key, &value)
}
}
impl<K, T, C, H> crate::access::ManagedOpen for SegTree<K, T, C, H>
where
K: Key + Hash + Eq + Send + Sync + 'static,
T: Send + Sync + 'static,
C: Codec<T> + 'static,
H: crate::hook::TypedWriteHook<K, T> + 'static,
{
type Key = K;
type Val = T;
fn migrate_inner(
&self,
mfn: &crate::armour::migration::TypedMigrationFn<K, T>,
_fire_init: bool,
) -> DbResult<usize> {
self.migrate_rows(mfn)
}
fn flush_for_migration(&self) -> DbResult<()> {
self.durable_flush()
}
fn projections_enabled(&self) -> bool {
self.hook.projection_targets().is_some() || self.hook.counter_targets().is_some()
}
fn reconcile_projections(&self) -> DbResult<()> {
SegTree::reconcile_projections(self)
}
fn backfill_index_target(&self, name: &'static str) -> DbResult<u64> {
SegTree::backfill_index_target(self, name)
}
fn reconcile_counters(&self) -> DbResult<u64> {
SegTree::reconcile_counters(self)
}
fn counter_target_names(&self) -> Vec<&'static str> {
SegTree::counter_target_names(self)
}
fn index_targets(&self) -> Vec<crate::projection::ErasedTarget> {
self.hook
.projection_targets()
.map(|t| t.erased_targets().to_vec())
.unwrap_or_default()
}
fn index_target_role(&self) -> bool {
self.is_index_target()
}
fn bind_index_state(&self, cell: std::sync::Arc<crate::seg::index_target::IndexStateCell>) {
SegTree::bind_index_state(self, cell);
}
fn register_tx_metadata(&self, db: &crate::armour::Db) -> DbResult<()> {
if let Some(targets) = self.hook.projection_targets() {
db.tx_register_projection_targets(targets)?;
}
if let Some(targets) = self.hook.counter_targets() {
db.tx_register_counter_targets(targets)?;
}
if H::EMITS
&& let Some(targets) = self.hook.stream_targets()
{
db.tx_register_stream_targets(targets)?;
}
Ok(())
}
fn replay_init(&self) {}
fn start_background(&self) -> DbResult<()> {
self.core().start_background()
}
fn bind_tx_arc(arc: &std::sync::Arc<Self>) {
let _ = arc.tx_self.set(std::sync::Arc::downgrade(arc));
}
fn replay_pending_tx_log(
&self,
db: &crate::armour::Db,
stored_fingerprint: Option<u64>,
format_plan: Option<&crate::format_migration::plan::MigrationPlan>,
) -> DbResult<usize> {
crate::txn::replay::replay_pending(db, self, stored_fingerprint, format_plan)
}
}
#[cfg(test)]
#[cfg(feature = "segmented")]
mod tests {
use crate::access::Access;
use crate::access::traits::Step;
use crate::armour::inspection::ScanCapability;
use crate::error::DbError;
use crate::seg::testkit::{TestKey, TextCodec, key_of, meta};
use crate::seg::{SegConfig, SegMap};
use armour_core::{CollectionShape, StorageBackend, StorageClass, ValueModel};
use std::ops::{Bound, ControlFlow};
use crate::MigrateAction;
use crate::access::ManagedOpen;
#[test]
fn the_managed_open_seam_migrates_and_flushes() {
let dir = tempfile::tempdir().unwrap();
let map = open(dir.path());
for i in 0..10u64 {
map.put(&key_of(i), &format!("v{i}")).unwrap();
}
fn upper(_key: &TestKey, value: &str) -> MigrateAction<String> {
MigrateAction::Update(value.to_uppercase())
}
let mfn: crate::armour::migration::TypedMigrationFn<TestKey, String> =
|key, value| upper(key, value);
assert_eq!(ManagedOpen::migrate_inner(&map, &mfn, false).unwrap(), 10);
assert_eq!(map.get(&key_of(3)), Some("V3".to_owned()));
ManagedOpen::flush_for_migration(&map).unwrap();
assert_eq!(map.dirty_shards(), 0);
ManagedOpen::replay_init(&map);
}
type Map = SegMap<TestKey, String, TextCodec>;
fn open(dir: &std::path::Path) -> Map {
let config = SegConfig {
seal_max_keys: 1_000_000,
max_frozen_generations: 8,
..SegConfig::test()
};
SegMap::open(dir, meta(), config, TextCodec).unwrap()
}
fn shadowed(dir: &std::path::Path) -> Map {
let map = open(dir);
for i in 0..20u64 {
map.put(&key_of(i), &format!("v{i}")).unwrap();
}
map.seal().unwrap();
assert!(map.wait_sealed(std::time::Duration::from_secs(20)).unwrap());
map.put(&key_of(1), &"newer".to_owned()).unwrap();
map.delete(&key_of(2)).unwrap();
map
}
#[test]
fn the_storage_class_is_honest() {
assert_eq!(
<Map as Access>::STORAGE,
StorageClass {
model: ValueModel::Segmented,
shape: CollectionShape::Map,
backend: StorageBackend::Segmented,
}
);
const { assert!(<Map as Access>::RAW_WIRE_BYTES) };
const { assert!(!<Map as Access>::LEN_IS_EXACT) };
}
#[test]
fn point_access_goes_through_every_layer() {
let dir = tempfile::tempdir().unwrap();
let map = shadowed(dir.path());
assert_eq!(
Access::with_value(&map, &key_of(1), |v| v.clone()).unwrap(),
Some("newer".to_owned())
);
assert_eq!(
Access::with_value(&map, &key_of(2), |v| v.clone()).unwrap(),
None
);
assert!(Access::contains(&map, &key_of(3)).unwrap());
assert!(!Access::contains(&map, &key_of(2)).unwrap());
assert_eq!(
Access::stored_bytes(&map, &key_of(3)).unwrap(),
Some(b"v3".to_vec())
);
Access::put(&map, &key_of(50), "fifty".to_owned()).unwrap();
Access::put_ref(&map, &key_of(51), &"fifty-one".to_owned()).unwrap();
Access::delete(&map, &key_of(50)).unwrap();
assert_eq!(map.get(&key_of(50)), None);
assert_eq!(map.get(&key_of(51)), Some("fifty-one".to_owned()));
}
#[test]
fn the_length_is_an_upper_bound_and_the_count_is_exact() {
let dir = tempfile::tempdir().unwrap();
let map = shadowed(dir.path());
assert_eq!(map.approx_len(), 21);
assert_eq!(map.rpc_exact_count().unwrap(), 19);
assert_eq!(map.inspection_exact_count().unwrap(), 19);
}
#[test]
fn the_scan_yields_each_visible_row_once() {
let dir = tempfile::tempdir().unwrap();
let map = shadowed(dir.path());
assert_eq!(map.scan_capability(), ScanCapability::UnorderedSnapshot);
let mut rows = std::collections::BTreeMap::new();
map.scan_all(&mut |row| -> Step {
let (key, value) = row.unwrap();
assert!(
rows.insert(*key, value.clone()).is_none(),
"a key came twice"
);
ControlFlow::Continue(())
});
assert_eq!(rows.len(), 19);
assert_eq!(rows[&key_of(1)], "newer");
assert!(!rows.contains_key(&key_of(2)));
assert_eq!(map.undecodable_rows(), 0);
map.core().put(&key_of(90), &[0xff]).unwrap();
assert_eq!(map.undecodable_rows(), 1);
}
#[test]
fn what_an_unordered_map_cannot_do_is_not_implemented() {
let dir = tempfile::tempdir().unwrap();
let map = shadowed(dir.path());
let unbounded = || (Bound::Unbounded, Bound::Unbounded);
assert!(matches!(
map.with_first(|_, _| ()),
Err(DbError::NotImplemented)
));
assert!(matches!(
map.with_last(|_, _| ()),
Err(DbError::NotImplemented)
));
let (start, end) = unbounded();
assert!(matches!(
map.scan_range(start, end, &mut |_, _| Ok(ControlFlow::Continue(()))),
Err(DbError::NotImplemented)
));
let (start, end) = unbounded();
assert!(matches!(
map.try_scan_range(start, end, 8, &mut |_, _| Ok(ControlFlow::Continue(()))),
Err(DbError::NotImplemented)
));
let (start, end) = unbounded();
assert!(matches!(
map.scan_keys(start, end, &mut |_| Ok(ControlFlow::Continue(()))),
Err(DbError::NotImplemented)
));
assert!(matches!(
map.atomic_at(&key_of(1), |_| Ok(())),
Err(DbError::NotImplemented)
));
assert_eq!(
Access::effective_projection_limits(&map),
Some((
crate::projection::DEFAULT_MAX_PROJECTED_ROWS,
crate::projection::DEFAULT_MAX_PROJECTED_BYTES
))
);
}
#[test]
fn tree_ordered_access_propagates_decode_errors_and_preserves_early_stop() {
use crate::seg::SegTree;
use crate::seg::testkit::ordered_key;
let dir = tempfile::tempdir().unwrap();
let tree = SegTree::<TestKey, String, TextCodec>::open(
dir.path(),
meta(),
&[],
SegConfig::test(),
TextCodec,
)
.unwrap();
tree.put(&ordered_key(1), &"good".to_owned()).unwrap();
tree.core().put(&ordered_key(2), &[0xff]).unwrap();
assert_eq!(
tree.with_first(|_, v| v.clone()).unwrap(),
Some("good".to_owned())
);
assert!(tree.with_last(|_, _| ()).is_err());
let mut seen = 0;
assert!(
tree.scan_range(Bound::Unbounded, Bound::Unbounded, &mut |_, _| {
seen += 1;
Ok(ControlFlow::Continue(()))
})
.is_err()
);
assert_eq!(seen, 1);
tree.try_scan_range(Bound::Unbounded, Bound::Unbounded, 1, &mut |_, _| {
Ok(ControlFlow::Break(()))
})
.unwrap();
let mut keys = Vec::new();
Access::scan_keys(&tree, Bound::Unbounded, Bound::Unbounded, &mut |k| {
keys.push(*k);
Ok(ControlFlow::Continue(()))
})
.unwrap();
assert_eq!(keys, vec![ordered_key(1), ordered_key(2)]);
assert_eq!(tree.undecodable_rows(), 1);
let mut errors = 0;
tree.scan_all(&mut |row| {
if row.is_err() {
errors += 1;
}
ControlFlow::Continue(())
});
assert_eq!(errors, 1);
}
}