//! Test trait implementations for the Any QMDB. use super::{ batch::{MerkleizedBatch, UnmerkleizedBatch}, db::Db, operation::{Operation, Update, update}, traits::{ BatchableDb, DbAny, MerkleizedBatch as MerkleizedBatchTrait, Provable, UnmerkleizedBatch as UnmerkleizedBatchTrait, }, }; use crate::{ Context, index::{Ordered as OrderedIndex, Unordered as UnorderedIndex}, journal::contiguous::Mutable, merkle::{Family, Location, Proof}, qmdb::{Error, floor::Policy, operation::Key, value::ValueEncoding}, }; use commonware_codec::Codec; use commonware_cryptography::{Digest, Hasher}; use commonware_parallel::Strategy; use commonware_runtime::Handle; use core::{borrow::Borrow, num::NonZeroU64}; use std::{future::Future, ops::Range, sync::Arc}; impl UnmerkleizedBatchTrait, S, N>> for UnmerkleizedBatch, S> where F: Family, K: Key, V: ValueEncoding, H: Hasher, E: Context, C: Mutable>>, I: UnorderedIndex>, S: Strategy, Operation>: Codec, { type Family = F; type Key = K; type Value = V::Value; type Metadata = V::Value; type Merkleized = Arc, S>>; fn with_metadata(self, metadata: impl Into>) -> Self { Self::with_metadata(self, metadata) } fn write(self, key: K, value: Option) -> Self { Self::write(self, key, value) } fn merkleize_with + Send>( self, db: &Db, S, N>, policy: &mut P, ) -> impl Future>> { self.merkleize_with(db, policy) } } impl UnmerkleizedBatchTrait, S, N>> for UnmerkleizedBatch, S> where F: Family, K: Key, V: ValueEncoding, H: Hasher, E: Context, C: Mutable>>, I: OrderedIndex>, S: Strategy, Operation>: Codec, { type Family = F; type Key = K; type Value = V::Value; type Metadata = V::Value; type Merkleized = Arc, S>>; fn with_metadata(self, metadata: impl Into>) -> Self { Self::with_metadata(self, metadata) } fn write(self, key: K, value: Option) -> Self { Self::write(self, key, value) } fn merkleize_with + Send>( self, db: &Db, S, N>, policy: &mut P, ) -> impl Future>> { self.merkleize_with(db, policy) } } impl MerkleizedBatchTrait for Arc> where Operation: Codec, { type Digest = D; fn root(&self) -> D { MerkleizedBatch::root(self) } } impl BatchableDb for Db where F: Family, E: Context, C: Mutable>, I: UnorderedIndex> + 'static, H: Hasher, U: update::Update, S: Strategy, Operation: Codec, UnmerkleizedBatch: UnmerkleizedBatchTrait< Self, Family = F, Key = U::Key, Value = U::Value, Metadata = U::Value, Merkleized = Arc>, >, { type Family = F; type Key = U::Key; type Value = U::Value; type Merkleized = Arc>; type Batch = UnmerkleizedBatch; fn new_batch(&self) -> Self::Batch { self.new_batch() } fn apply_batch( self, batch: Self::Merkleized, ) -> impl Future>> { self.apply_batch(batch) } } impl DbAny for Db where F: Family, E: Context, C: Mutable>, I: UnorderedIndex>, H: Hasher, U: Update, S: Strategy, Operation: Codec, Self: BatchableDb, { type Digest = H::Digest; async fn get(&self, key: &U::Key) -> Result, Error> { Self::get(self, key).await } async fn get_many + Sync>( &self, keys: &[Q], ) -> Result>, Error> { Self::get_many(self, keys).await } fn root(&self) -> H::Digest { Self::root(self) } fn bounds(&self) -> Range> { Self::bounds(self) } async fn get_metadata(&self) -> Result, Error> { Self::get_metadata(self).await } async fn prune(self, loc: Location) -> Result> { Self::prune(self, loc).await } async fn start_sync(self) -> Result<(Self, Handle<()>), Error> { Self::start_sync(self).await } async fn commit(self) -> Result> { Self::commit(self).await } async fn sync(self) -> Result> { Self::sync(self).await } async fn destroy(self) -> Result<(), Error> { Self::destroy(self).await } fn inactivity_floor(&self) -> Location { Self::inactivity_floor(self) } fn sync_boundary(&self) -> Location { Self::sync_boundary(self) } } impl Provable for Db where F: Family, E: Context, C: Mutable>, I: UnorderedIndex>, H: Hasher, U: Update, S: Strategy, Operation: Codec, Self: BatchableDb, { type Operation = Operation; async fn historical_proof( &self, historical_size: Location, start_loc: Location, max_ops: NonZeroU64, ) -> Result<(Proof, Vec>), Error> { Self::historical_proof(self, historical_size, start_loc, max_ops).await } }