//! Compact keyless database types and helpers for compact sync demonstration. use crate::{Hasher, Key, Value}; use commonware_parallel::Sequential; use commonware_runtime::{buffer::paged::CacheRef, BufferPooler}; use commonware_storage::{ journal::contiguous::variable, merkle::mmr, qmdb::{ self, keyless::fixed::{self, CompactConfig}, sync::compact, }, Context, }; use commonware_utils::{NZUsize, NZU16, NZU64}; use tracing::error; /// Database type alias. pub type Database = fixed::CompactDb; /// Operation type alias. pub type Operation = fixed::Operation; /// Create a database configuration for the compact keyless variant. pub fn create_config(context: &impl BufferPooler) -> CompactConfig { CompactConfig { strategy: Sequential, witness: variable::Config { partition: "compact-keyless-witness".into(), items_per_section: NZU64!(4096), compression: None, codec_config: (), page_cache: CacheRef::from_pooler(context, NZU16!(1024), NZUsize!(64)), write_buffer: NZUsize!(1024), }, commit_codec_config: (), } } impl super::ExampleDatabase for Database where E: Context, { type Family = mmr::Family; type Operation = Operation; fn create_test_operations(count: usize, seed: u64, starting_loc: u64) -> Vec { super::keyless::create_test_operations(count, seed, starting_loc) } async fn add_operations( &mut self, operations: Vec, ) -> Result<(), qmdb::Error> { let Some(last) = operations.last() else { error!("operations must end with a commit"); return Ok(()); }; if !matches!(last, Operation::Commit(..)) { error!("operations must end with a commit"); return Ok(()); } let mut batch = self.new_batch(); for operation in operations { match operation { Operation::Append(value) => { batch = batch.append(value); } Operation::Commit(metadata, floor) => { let merkleized = batch.merkleize(self, metadata, floor).await; self.apply_batch(merkleized)?; self.sync().await?; batch = self.new_batch(); } } } Ok(()) } fn current_floor(&self) -> u64 { *Self::last_commit_loc(self) } fn root(&self) -> Key { Self::root(self) } fn name() -> &'static str { "compact keyless" } } impl super::CompactSyncable for Database where E: Context, { fn target(&self) -> compact::Target { Self::target(self) } }