mod models;
mod operations;
#[cfg(feature = "postgres")]
mod postgres;
mod schema;
#[cfg(feature = "sqlite")]
mod sqlite;
use std::collections::HashSet;
use crate::error::InternalError;
use crate::state::merkle::sql::cache::DataCache;
use crate::state::merkle::{
node::Node,
sql::backend::{Backend, Connection},
};
type NodeChanges = Vec<(String, Node, String)>;
#[derive(Default)]
pub struct TreeUpdate {
pub node_changes: NodeChanges,
pub deletions: HashSet<String>,
}
pub trait MerkleRadixStore {
fn get_or_create_tree(
&self,
tree_name: &str,
initial_state_root_hash: &str,
) -> Result<i64, InternalError>;
fn get_tree_id_by_name(&self, tree_name: &str) -> Result<Option<i64>, InternalError>;
fn delete_tree(&self, tree_id: i64) -> Result<(), InternalError>;
fn list_trees(
&self,
) -> Result<Box<dyn ExactSizeIterator<Item = Result<String, InternalError>>>, InternalError>;
fn has_root(&self, tree_id: i64, state_root_hash: &str) -> Result<bool, InternalError>;
fn get_path(
&self,
tree_id: i64,
state_root_hash: &str,
address: &str,
) -> Result<Vec<(String, Node)>, InternalError>;
fn get_entries(
&self,
tree_id: i64,
state_root_hash: &str,
keys: Vec<&str>,
) -> Result<Vec<(String, Vec<u8>)>, InternalError>;
fn list_entries(
&self,
tree_id: i64,
state_root_hash: &str,
prefix: Option<&str>,
) -> Result<Vec<(String, Vec<u8>)>, InternalError>;
fn write_changes(
&self,
tree_id: i64,
state_root_hash: &str,
parent_state_root_hash: &str,
tree_update: TreeUpdate,
) -> Result<(), InternalError>;
fn prune(&self, tree_id: i64, state_root: &str) -> Result<Vec<String>, InternalError>;
fn remove_pruned_entries(&self, tree_id: i64) -> Result<u64, InternalError>;
}
pub struct SqlMerkleRadixStore<'b, B: Backend, C> {
pub backend: &'b B,
_conn: std::marker::PhantomData<C>,
pub cache: Option<&'b DataCache>,
}
impl<'b, B: Backend, C> SqlMerkleRadixStore<'b, B, C>
where
B: Backend,
<B as Backend>::Connection: Connection<ConnectionType = C>,
{
pub fn new(backend: &'b B) -> Self {
Self {
backend,
_conn: std::marker::PhantomData,
cache: None,
}
}
pub(crate) fn new_with_cache(backend: &'b B, cache: &'b DataCache) -> Self {
Self {
backend,
_conn: std::marker::PhantomData,
cache: Some(cache),
}
}
}