use crate::Key;
use commonware_codec::Encode;
use commonware_storage::{
merkle::{self, Location, Proof},
qmdb::{self, operation::Operation},
};
use std::{future::Future, num::NonZeroU64};
pub mod any;
pub mod current;
pub mod immutable;
#[derive(Debug, Clone, Copy)]
pub enum DatabaseType {
Any,
Current,
Immutable,
}
impl std::str::FromStr for DatabaseType {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"any" => Ok(Self::Any),
"current" => Ok(Self::Current),
"immutable" => Ok(Self::Immutable),
_ => Err(format!(
"Invalid database type: '{s}'. Must be 'any', 'current', or 'immutable'",
)),
}
}
}
impl DatabaseType {
pub const fn as_str(&self) -> &'static str {
match self {
Self::Any => "any",
Self::Current => "current",
Self::Immutable => "immutable",
}
}
}
#[allow(clippy::type_complexity)]
pub trait Syncable: Sized {
type Family: merkle::Family;
type Operation: Operation<Self::Family> + Encode + Sync + 'static;
fn create_test_operations(count: usize, seed: u64) -> Vec<Self::Operation>;
fn add_operations(
&mut self,
operations: Vec<Self::Operation>,
) -> impl Future<Output = Result<(), qmdb::Error<Self::Family>>>;
fn root(&self) -> Key;
fn size(&self) -> impl Future<Output = Location<Self::Family>> + Send;
fn inactivity_floor(&self) -> impl Future<Output = Location<Self::Family>> + Send;
fn historical_proof(
&self,
op_count: Location<Self::Family>,
start_loc: Location<Self::Family>,
max_ops: NonZeroU64,
) -> impl Future<
Output = Result<
(Proof<Self::Family, Key>, Vec<Self::Operation>),
qmdb::Error<Self::Family>,
>,
> + Send;
fn pinned_nodes_at(
&self,
loc: Location<Self::Family>,
) -> impl Future<Output = Result<Vec<Key>, qmdb::Error<Self::Family>>> + Send;
fn name() -> &'static str;
}