#[cfg(feature = "minimal")]
mod constructor;
#[cfg(feature = "minimal")]
mod delete;
#[cfg(feature = "minimal")]
mod exists;
#[cfg(feature = "minimal")]
mod get;
#[cfg(any(feature = "minimal", feature = "verify"))]
pub(crate) mod helpers;
#[cfg(feature = "minimal")]
mod insert;
#[cfg(any(feature = "minimal", feature = "verify"))]
mod query;
#[cfg(any(feature = "minimal", feature = "verify"))]
pub use query::QueryOptions;
#[cfg(any(feature = "minimal", feature = "verify"))]
mod serialize;
#[cfg(any(feature = "minimal", feature = "verify"))]
use std::fmt;
use bincode::{Decode, Encode};
#[cfg(feature = "minimal")]
use grovedb_merk::estimated_costs::SUM_AND_COUNT_LAYER_COST_SIZE;
#[cfg(feature = "minimal")]
use grovedb_merk::estimated_costs::SUM_VALUE_EXTRA_COST;
#[cfg(feature = "minimal")]
use grovedb_merk::estimated_costs::{
BIG_SUM_LAYER_COST_SIZE, LAYER_COST_SIZE, SUM_LAYER_COST_SIZE,
};
#[cfg(feature = "minimal")]
use grovedb_merk::tree_type::TreeType;
#[cfg(feature = "minimal")]
use grovedb_visualize::visualize_to_vec;
use crate::operations::proof::util::hex_to_ascii;
#[cfg(any(feature = "minimal", feature = "verify"))]
use crate::reference_path::ReferencePathType;
#[cfg(feature = "minimal")]
use crate::OperationCost;
#[cfg(any(feature = "minimal", feature = "verify"))]
pub type ElementFlags = Vec<u8>;
#[cfg(any(feature = "minimal", feature = "verify"))]
pub type MaxReferenceHop = Option<u8>;
#[cfg(feature = "minimal")]
pub const TREE_COST_SIZE: u32 = LAYER_COST_SIZE; #[cfg(feature = "minimal")]
pub const SUM_ITEM_COST_SIZE: u32 = SUM_VALUE_EXTRA_COST + 2; #[cfg(feature = "minimal")]
pub const SUM_TREE_COST_SIZE: u32 = SUM_LAYER_COST_SIZE;
#[cfg(feature = "minimal")]
pub const BIG_SUM_TREE_COST_SIZE: u32 = BIG_SUM_LAYER_COST_SIZE;
#[cfg(feature = "minimal")]
pub const COUNT_TREE_COST_SIZE: u32 = SUM_LAYER_COST_SIZE;
#[cfg(feature = "minimal")]
pub const COUNT_SUM_TREE_COST_SIZE: u32 = SUM_AND_COUNT_LAYER_COST_SIZE;
#[cfg(any(feature = "minimal", feature = "verify"))]
pub type SumValue = i64;
#[cfg(any(feature = "minimal", feature = "verify"))]
pub type BigSumValue = i128;
#[cfg(any(feature = "minimal", feature = "verify"))]
pub type CountValue = u64;
#[cfg(feature = "minimal")]
pub trait CostSize {
fn cost_size(&self) -> u32;
}
#[cfg(feature = "minimal")]
impl CostSize for TreeType {
fn cost_size(&self) -> u32 {
match self {
TreeType::NormalTree => TREE_COST_SIZE,
TreeType::SumTree => SUM_TREE_COST_SIZE,
TreeType::BigSumTree => BIG_SUM_TREE_COST_SIZE,
TreeType::CountTree => COUNT_TREE_COST_SIZE,
TreeType::CountSumTree => COUNT_SUM_TREE_COST_SIZE,
}
}
}
#[cfg(any(feature = "minimal", feature = "verify"))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, Hash)]
#[cfg_attr(not(any(feature = "minimal", feature = "visualize")), derive(Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Element {
Item(Vec<u8>, Option<ElementFlags>),
Reference(ReferencePathType, MaxReferenceHop, Option<ElementFlags>),
Tree(Option<Vec<u8>>, Option<ElementFlags>),
SumItem(SumValue, Option<ElementFlags>),
SumTree(Option<Vec<u8>>, SumValue, Option<ElementFlags>),
BigSumTree(Option<Vec<u8>>, BigSumValue, Option<ElementFlags>),
CountTree(Option<Vec<u8>>, CountValue, Option<ElementFlags>),
CountSumTree(Option<Vec<u8>>, CountValue, SumValue, Option<ElementFlags>),
}
impl fmt::Display for Element {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Element::Item(data, flags) => {
write!(
f,
"Item({}{})",
hex_to_ascii(data),
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
Element::Reference(path, max_hop, flags) => {
write!(
f,
"Reference({}, max_hop: {}{})",
path,
max_hop.map_or("None".to_string(), |h| h.to_string()),
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
Element::Tree(root_key, flags) => {
write!(
f,
"Tree({}{})",
root_key.as_ref().map_or("None".to_string(), hex::encode),
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
Element::SumItem(sum_value, flags) => {
write!(
f,
"SumItem({}{})",
sum_value,
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
Element::SumTree(root_key, sum_value, flags) => {
write!(
f,
"SumTree({}, {}{})",
root_key.as_ref().map_or("None".to_string(), hex::encode),
sum_value,
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
Element::BigSumTree(root_key, sum_value, flags) => {
write!(
f,
"BigSumTree({}, {}{})",
root_key.as_ref().map_or("None".to_string(), hex::encode),
sum_value,
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
Element::CountTree(root_key, count_value, flags) => {
write!(
f,
"CountTree({}, {}{})",
root_key.as_ref().map_or("None".to_string(), hex::encode),
count_value,
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
Element::CountSumTree(root_key, count_value, sum_value, flags) => {
write!(
f,
"CountSumTree({}, {}, {}{})",
root_key.as_ref().map_or("None".to_string(), hex::encode),
count_value,
sum_value,
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
}
}
}
impl Element {
pub fn type_str(&self) -> &str {
match self {
Element::Item(..) => "item",
Element::Reference(..) => "reference",
Element::Tree(..) => "tree",
Element::SumItem(..) => "sum item",
Element::SumTree(..) => "sum tree",
Element::BigSumTree(..) => "big sum tree",
Element::CountTree(..) => "count tree",
Element::CountSumTree(..) => "count sum tree",
}
}
#[cfg(feature = "minimal")]
pub(crate) fn value_hash(
&self,
grove_version: &grovedb_version::version::GroveVersion,
) -> grovedb_costs::CostResult<grovedb_merk::CryptoHash, crate::Error> {
let bytes = grovedb_costs::cost_return_on_error_default!(self.serialize(grove_version));
crate::value_hash(&bytes).map(Result::Ok)
}
}
#[cfg(any(feature = "minimal", feature = "visualize"))]
impl fmt::Debug for Element {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut v = Vec::new();
visualize_to_vec(&mut v, self);
f.write_str(&String::from_utf8_lossy(&v))
}
}