diff --git a/costs/Cargo.toml b/costs/Cargo.toml index 382404d6..a98dc02d 100644 --- a/costs/Cargo.toml +++ b/costs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "grovedb-costs" -version = "2.2.1" +version = "3.0.0" edition = "2021" license = "MIT" description = "Costs extension crate for GroveDB" diff --git a/costs/src/lib.rs b/costs/src/lib.rs index c3d85530..7e576dc1 100644 --- a/costs/src/lib.rs +++ b/costs/src/lib.rs @@ -72,9 +72,29 @@ pub type ChildrenSizesWithValue = Option<( Option<(ChildKeyLength, ChildSumLength)>, )>; +/// The tree cost type +pub enum TreeCostType { + /// This is for sum trees and count trees + TreeFeatureUsesVarIntCostAs8Bytes, + /// This is for count sum trees + TreeFeatureUsesTwoVarIntsCostAs16Bytes, + /// This is for big sum trees + TreeFeatureUses16Bytes, +} + +impl TreeCostType { + fn cost_size(&self) -> u32 { + match self { + TreeCostType::TreeFeatureUsesVarIntCostAs8Bytes => 8, + TreeCostType::TreeFeatureUsesTwoVarIntsCostAs16Bytes => 16, + TreeCostType::TreeFeatureUses16Bytes => 16, + } + } +} + /// Children sizes starting with if we are in a sum tree pub type ChildrenSizesWithIsSumTree = Option<( - Option, + Option<(TreeCostType, FeatureSumLength)>, Option<(ChildKeyLength, ChildSumLength)>, Option<(ChildKeyLength, ChildSumLength)>, )>; @@ -199,10 +219,14 @@ impl OperationCost { paid_value_len -= right_child_sum_len; } - if let Some(sum_tree_len) = in_sum_tree { + let sum_tree_node_size = if let Some((tree_cost_type, sum_tree_len)) = in_sum_tree { + let cost_size = tree_cost_type.cost_size(); paid_value_len -= sum_tree_len; - paid_value_len += 8; - } + paid_value_len += cost_size; + cost_size + } else { + 0 + }; // This is the moment we need to add the required space (after removing // children) but before adding the parent to child hook @@ -210,9 +234,6 @@ impl OperationCost { // Now we are the parent to child hook - // we need to add the sum tree node size - let sum_tree_node_size = if in_sum_tree.is_some() { 8 } else { 0 }; - // We need to add the cost of a parent // key_len has a hash length already in it from the key prefix // So we need to remove it and then add a hash length diff --git a/grovedb-epoch-based-storage-flags/Cargo.toml b/grovedb-epoch-based-storage-flags/Cargo.toml index 2e32ec2b..83df1463 100644 --- a/grovedb-epoch-based-storage-flags/Cargo.toml +++ b/grovedb-epoch-based-storage-flags/Cargo.toml @@ -2,13 +2,13 @@ name = "grovedb-epoch-based-storage-flags" authors = ["Samuel Westrich "] description = "Epoch based storage flags for GroveDB" -version = "2.2.1" +version = "3.0.0" edition = "2021" license = "MIT" repository = "https://github.com/dashpay/grovedb" [dependencies] -grovedb-costs = { version = "2.2.1", path = "../costs" } +grovedb-costs = { version = "3.0.0", path = "../costs" } hex = { version = "0.4.3" } integer-encoding = { version = "4.0.0" } diff --git a/grovedb-version/Cargo.toml b/grovedb-version/Cargo.toml index 5cbd2dbe..d1e6b08b 100644 --- a/grovedb-version/Cargo.toml +++ b/grovedb-version/Cargo.toml @@ -2,7 +2,7 @@ name = "grovedb-version" authors = ["Samuel Westrich "] description = "Versioning library for Platform" -version = "2.2.1" +version = "3.0.0" edition = "2021" license = "MIT" repository = "https://github.com/dashpay/grovedb" diff --git a/grovedb-version/src/lib.rs b/grovedb-version/src/lib.rs index 48b80a52..f66019d4 100644 --- a/grovedb-version/src/lib.rs +++ b/grovedb-version/src/lib.rs @@ -34,6 +34,23 @@ macro_rules! check_grovedb_v0 { }}; } +#[macro_export] +macro_rules! check_grovedb_v0_or_v1 { + ($method:expr, $version:expr) => {{ + const EXPECTED_VERSION_V0: u16 = 0; + const EXPECTED_VERSION_V1: u16 = 1; + if $version != EXPECTED_VERSION_V0 && $version != EXPECTED_VERSION_V1 { + return Err(GroveVersionError::UnknownVersionMismatch { + method: $method.to_string(), + known_versions: vec![EXPECTED_VERSION_V0, EXPECTED_VERSION_V1], + received: $version, + } + .into()); + } + $version + }}; +} + #[macro_export] macro_rules! check_merk_v0_with_cost { ($method:expr, $version:expr) => {{ diff --git a/grovedb-version/src/version/merk_versions.rs b/grovedb-version/src/version/merk_versions.rs index fac25f91..d0d122da 100644 --- a/grovedb-version/src/version/merk_versions.rs +++ b/grovedb-version/src/version/merk_versions.rs @@ -1,2 +1,12 @@ +use versioned_feature_core::FeatureVersion; + #[derive(Clone, Debug, Default)] -pub struct MerkVersions {} +pub struct MerkVersions { + pub average_case_costs: MerkAverageCaseCostsVersions, +} + +#[derive(Clone, Debug, Default)] +pub struct MerkAverageCaseCostsVersions { + pub add_average_case_merk_propagate: FeatureVersion, + pub sum_tree_estimated_size: FeatureVersion, +} diff --git a/grovedb-version/src/version/mod.rs b/grovedb-version/src/version/mod.rs index 06ac4e12..d795176c 100644 --- a/grovedb-version/src/version/mod.rs +++ b/grovedb-version/src/version/mod.rs @@ -1,11 +1,12 @@ pub mod grovedb_versions; pub mod merk_versions; pub mod v1; +pub mod v2; pub use versioned_feature_core::*; use crate::version::{ - grovedb_versions::GroveDBVersions, merk_versions::MerkVersions, v1::GROVE_V1, + grovedb_versions::GroveDBVersions, merk_versions::MerkVersions, v1::GROVE_V1, v2::GROVE_V2, }; #[derive(Clone, Debug, Default)] @@ -16,6 +17,12 @@ pub struct GroveVersion { } impl GroveVersion { + pub fn first<'a>() -> &'a Self { + GROVE_VERSIONS + .first() + .expect("expected to have a platform version") + } + pub fn latest<'a>() -> &'a Self { GROVE_VERSIONS .last() @@ -23,4 +30,4 @@ impl GroveVersion { } } -pub const GROVE_VERSIONS: &[GroveVersion] = &[GROVE_V1]; +pub const GROVE_VERSIONS: &[GroveVersion] = &[GROVE_V1, GROVE_V2]; diff --git a/grovedb-version/src/version/v1.rs b/grovedb-version/src/version/v1.rs index 97cfb38b..5bf58180 100644 --- a/grovedb-version/src/version/v1.rs +++ b/grovedb-version/src/version/v1.rs @@ -8,7 +8,7 @@ use crate::version::{ GroveDBOperationsWorstCaseVersions, GroveDBPathQueryMethodVersions, GroveDBReplicationVersions, GroveDBVersions, }, - merk_versions::MerkVersions, + merk_versions::{MerkAverageCaseCostsVersions, MerkVersions}, GroveVersion, }; @@ -184,5 +184,10 @@ pub const GROVE_V1: GroveVersion = GroveVersion { apply_chunk: 0, }, }, - merk_versions: MerkVersions {}, + merk_versions: MerkVersions { + average_case_costs: MerkAverageCaseCostsVersions { + add_average_case_merk_propagate: 0, + sum_tree_estimated_size: 0, + }, + }, }; diff --git a/grovedb-version/src/version/v2.rs b/grovedb-version/src/version/v2.rs new file mode 100644 index 00000000..3591ba1a --- /dev/null +++ b/grovedb-version/src/version/v2.rs @@ -0,0 +1,193 @@ +use crate::version::{ + grovedb_versions::{ + GroveDBApplyBatchVersions, GroveDBElementMethodVersions, + GroveDBOperationsAverageCaseVersions, GroveDBOperationsDeleteUpTreeVersions, + GroveDBOperationsDeleteVersions, GroveDBOperationsGetVersions, + GroveDBOperationsInsertVersions, GroveDBOperationsProofVersions, + GroveDBOperationsQueryVersions, GroveDBOperationsVersions, + GroveDBOperationsWorstCaseVersions, GroveDBPathQueryMethodVersions, + GroveDBReplicationVersions, GroveDBVersions, + }, + merk_versions::{MerkAverageCaseCostsVersions, MerkVersions}, + GroveVersion, +}; + +pub const GROVE_V2: GroveVersion = GroveVersion { + protocol_version: 1, + grovedb_versions: GroveDBVersions { + apply_batch: GroveDBApplyBatchVersions { + apply_batch_structure: 0, + apply_body: 0, + continue_partial_apply_body: 0, + apply_operations_without_batching: 0, + apply_batch: 0, + apply_partial_batch: 0, + open_batch_transactional_merk_at_path: 0, + open_batch_merk_at_path: 0, + apply_batch_with_element_flags_update: 0, + apply_partial_batch_with_element_flags_update: 0, + estimated_case_operations_for_batch: 0, + }, + element: GroveDBElementMethodVersions { + delete: 0, + delete_with_sectioned_removal_bytes: 0, + delete_into_batch_operations: 0, + element_at_key_already_exists: 0, + get: 0, + get_optional: 0, + get_from_storage: 0, + get_optional_from_storage: 1, + get_with_absolute_refs: 0, + get_value_hash: 0, + get_specialized_cost: 0, + value_defined_cost: 0, + value_defined_cost_for_serialized_value: 0, + specialized_costs_for_key_value: 0, + required_item_space: 0, + insert: 0, + insert_into_batch_operations: 0, + insert_if_not_exists: 0, + insert_if_not_exists_into_batch_operations: 0, + insert_if_changed_value: 0, + insert_if_changed_value_into_batch_operations: 0, + insert_reference: 0, + insert_reference_into_batch_operations: 0, + insert_subtree: 0, + insert_subtree_into_batch_operations: 0, + get_query: 0, + get_query_values: 0, + get_query_apply_function: 0, + get_path_query: 0, + get_sized_query: 0, + path_query_push: 0, + query_item: 0, + basic_push: 0, + serialize: 0, + serialized_size: 0, + deserialize: 0, + }, + operations: GroveDBOperationsVersions { + get: GroveDBOperationsGetVersions { + get: 0, + get_caching_optional: 0, + follow_reference: 0, + get_raw: 0, + get_raw_caching_optional: 0, + get_raw_optional: 0, + get_raw_optional_caching_optional: 0, + has_raw: 0, + check_subtree_exists_invalid_path: 0, + average_case_for_has_raw: 0, + average_case_for_has_raw_tree: 0, + average_case_for_get_raw: 0, + average_case_for_get: 0, + average_case_for_get_tree: 0, + worst_case_for_has_raw: 0, + worst_case_for_get_raw: 0, + worst_case_for_get: 0, + is_empty_tree: 0, + }, + insert: GroveDBOperationsInsertVersions { + insert: 0, + insert_on_transaction: 0, + insert_without_transaction: 0, + add_element_on_transaction: 0, + add_element_without_transaction: 0, + insert_if_not_exists: 0, + insert_if_not_exists_return_existing_element: 0, + insert_if_changed_value: 0, + }, + delete: GroveDBOperationsDeleteVersions { + delete: 0, + clear_subtree: 0, + delete_with_sectional_storage_function: 0, + delete_if_empty_tree: 0, + delete_if_empty_tree_with_sectional_storage_function: 0, + delete_operation_for_delete_internal: 0, + delete_internal_on_transaction: 0, + delete_internal_without_transaction: 0, + average_case_delete_operation_for_delete: 0, + worst_case_delete_operation_for_delete: 0, + }, + delete_up_tree: GroveDBOperationsDeleteUpTreeVersions { + delete_up_tree_while_empty: 0, + delete_up_tree_while_empty_with_sectional_storage: 0, + delete_operations_for_delete_up_tree_while_empty: 0, + add_delete_operations_for_delete_up_tree_while_empty: 0, + average_case_delete_operations_for_delete_up_tree_while_empty: 0, + worst_case_delete_operations_for_delete_up_tree_while_empty: 0, + }, + query: GroveDBOperationsQueryVersions { + query_encoded_many: 0, + query_many_raw: 0, + get_proved_path_query: 0, + query: 0, + query_item_value: 0, + query_item_value_or_sum: 0, + query_sums: 0, + query_raw: 0, + query_keys_optional: 0, + query_raw_keys_optional: 0, + follow_element: 0, + }, + proof: GroveDBOperationsProofVersions { + prove_query: 0, + prove_query_many: 0, + verify_query_with_options: 0, + verify_query_raw: 0, + verify_layer_proof: 0, + verify_query: 0, + verify_subset_query: 0, + verify_query_with_absence_proof: 0, + verify_subset_query_with_absence_proof: 0, + verify_query_with_chained_path_queries: 0, + }, + average_case: GroveDBOperationsAverageCaseVersions { + add_average_case_get_merk_at_path: 0, + average_case_merk_replace_tree: 1, // changed + average_case_merk_insert_tree: 0, + average_case_merk_delete_tree: 0, + average_case_merk_insert_element: 0, + average_case_merk_replace_element: 0, + average_case_merk_patch_element: 0, + average_case_merk_delete_element: 0, + add_average_case_has_raw_cost: 0, + add_average_case_has_raw_tree_cost: 0, + add_average_case_get_raw_cost: 0, + add_average_case_get_raw_tree_cost: 0, + add_average_case_get_cost: 0, + }, + worst_case: GroveDBOperationsWorstCaseVersions { + add_worst_case_get_merk_at_path: 0, + worst_case_merk_replace_tree: 0, + worst_case_merk_insert_tree: 0, + worst_case_merk_delete_tree: 0, + worst_case_merk_insert_element: 0, + worst_case_merk_replace_element: 0, + worst_case_merk_patch_element: 0, + worst_case_merk_delete_element: 0, + add_worst_case_has_raw_cost: 0, + add_worst_case_get_raw_tree_cost: 0, + add_worst_case_get_raw_cost: 0, + add_worst_case_get_cost: 0, + }, + }, + path_query_methods: GroveDBPathQueryMethodVersions { + terminal_keys: 0, + merge: 0, + query_items_at_path: 0, + }, + replication: GroveDBReplicationVersions { + get_subtrees_metadata: 0, + fetch_chunk: 0, + start_snapshot_syncing: 0, + apply_chunk: 0, + }, + }, + merk_versions: MerkVersions { + average_case_costs: MerkAverageCaseCostsVersions { + add_average_case_merk_propagate: 1, // changed + sum_tree_estimated_size: 1, // changed + }, + }, +}; diff --git a/grovedb/Cargo.toml b/grovedb/Cargo.toml index f977d0f4..5a2d998e 100644 --- a/grovedb/Cargo.toml +++ b/grovedb/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "grovedb" description = "Fully featured database using balanced hierarchical authenticated data structures" -version = "2.2.1" +version = "3.0.0" authors = ["Samuel Westrich ", "Wisdom Ogwu "] edition = "2021" license = "MIT" @@ -11,13 +11,13 @@ readme = "../README.md" documentation = "https://docs.rs/grovedb" [dependencies] -grovedb-costs = { version = "2.2.1", path = "../costs" , optional = true } -grovedbg-types = { version = "2.2.1", path = "../grovedbg-types", optional = true } -grovedb-merk = { version = "2.2.1", path = "../merk", optional = true, default-features = false } -grovedb-path = { version = "2.2.1", path = "../path" } -grovedb-storage = { version = "2.2.1", path = "../storage", optional = true } -grovedb-version = { version = "2.2.1", path = "../grovedb-version" } -grovedb-visualize = { version = "2.2.1", path = "../visualize", optional = true } +grovedb-costs = { version = "3.0.0", path = "../costs" , optional = true } +grovedbg-types = { version = "3.0.0", path = "../grovedbg-types", optional = true } +grovedb-merk = { version = "3.0.0", path = "../merk", optional = true, default-features = false } +grovedb-path = { version = "3.0.0", path = "../path" } +grovedb-storage = { version = "3.0.0", path = "../storage", optional = true } +grovedb-version = { version = "3.0.0", path = "../grovedb-version" } +grovedb-visualize = { version = "3.0.0", path = "../visualize", optional = true } axum = { version = "=0.7.5", features = ["macros"], optional = true } bincode = { version = "2.0.0-rc.3" } @@ -36,19 +36,20 @@ zip-extensions = { version = "0.8.1", optional = true } serde = { version = "1.0.210", features = ["derive"], optional = true } [dev-dependencies] -grovedb-epoch-based-storage-flags = { version = "2.2.1", path = "../grovedb-epoch-based-storage-flags" } +grovedb-epoch-based-storage-flags = { version = "3.0.0", path = "../grovedb-epoch-based-storage-flags" } criterion = "0.5.1" hex = "0.4.3" pretty_assertions = "1.4.0" rand = "0.8.5" +assert_matches = "1.5.0" [[bench]] name = "insertion_benchmark" harness = false [features] -default = ["full"] +default = ["full", "estimated_costs"] proof_debug = ["grovedb-merk/proof_debug"] serde = ["dep:serde", "grovedb-merk/serde", "indexmap/serde"] full = [ diff --git a/grovedb/src/batch/batch_structure.rs b/grovedb/src/batch/batch_structure.rs index 03694379..59e428c6 100644 --- a/grovedb/src/batch/batch_structure.rs +++ b/grovedb/src/batch/batch_structure.rs @@ -17,7 +17,7 @@ use intmap::IntMap; #[cfg(feature = "minimal")] use crate::{ batch::{key_info::KeyInfo, GroveOp, KeyInfoPath, QualifiedGroveDbOp, TreeCache}, - Element, ElementFlags, Error, + ElementFlags, Error, }; #[cfg(feature = "minimal")] @@ -124,17 +124,14 @@ where | GroveOp::InsertOrReplace { element } | GroveOp::Replace { element } | GroveOp::Patch { element, .. } => { - if let Element::Tree(..) = element { - cost_return_on_error!(&mut cost, merk_tree_cache.insert(&op, false)); - } else if let Element::SumTree(..) = element { - cost_return_on_error!(&mut cost, merk_tree_cache.insert(&op, true)); + if let Some(tree_type) = element.tree_type() { + cost_return_on_error!(&mut cost, merk_tree_cache.insert(&op, tree_type)); } Ok(()) } - GroveOp::RefreshReference { .. } - | GroveOp::Delete - | GroveOp::DeleteTree - | GroveOp::DeleteSumTree => Ok(()), + GroveOp::RefreshReference { .. } | GroveOp::Delete | GroveOp::DeleteTree(_) => { + Ok(()) + } GroveOp::ReplaceTreeRootKey { .. } | GroveOp::InsertTreeWithRootHash { .. } => { Err(Error::InvalidBatchOperation( "replace and insert tree hash are internal operations only", diff --git a/grovedb/src/batch/estimated_costs/average_case_costs.rs b/grovedb/src/batch/estimated_costs/average_case_costs.rs index 8d4c076a..310c5863 100644 --- a/grovedb/src/batch/estimated_costs/average_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/average_case_costs.rs @@ -10,12 +10,11 @@ use std::{ use grovedb_costs::{ cost_return_on_error, cost_return_on_error_no_add, CostResult, CostsExt, OperationCost, }; -use grovedb_merk::RootHashKeyAndSum; #[cfg(feature = "minimal")] -use grovedb_merk::{ - estimated_costs::average_case_costs::{average_case_merk_propagate, EstimatedLayerInformation}, - IsSumTree, +use grovedb_merk::estimated_costs::average_case_costs::{ + average_case_merk_propagate, EstimatedLayerInformation, }; +use grovedb_merk::{tree::AggregateData, tree_type::TreeType, RootHashKeyAndAggregateData}; #[cfg(feature = "minimal")] use grovedb_storage::rocksdb_storage::RocksDbStorage; use grovedb_version::version::GroveVersion; @@ -44,7 +43,7 @@ impl GroveOp { propagate: bool, grove_version: &GroveVersion, ) -> CostResult<(), Error> { - let in_tree_using_sums = layer_element_estimates.is_sum_tree; + let in_tree_type = layer_element_estimates.tree_type; let propagate_if_input = || { if propagate { Some(layer_element_estimates) @@ -53,28 +52,32 @@ impl GroveOp { } }; match self { - GroveOp::ReplaceTreeRootKey { sum, .. } => GroveDb::average_case_merk_replace_tree( - key, - layer_element_estimates, - sum.is_some(), - propagate, - grove_version, - ), - GroveOp::InsertTreeWithRootHash { flags, sum, .. } => { - GroveDb::average_case_merk_insert_tree( + GroveOp::ReplaceTreeRootKey { aggregate_data, .. } => { + GroveDb::average_case_merk_replace_tree( key, - flags, - sum.is_some(), - in_tree_using_sums, - propagate_if_input(), + layer_element_estimates, + aggregate_data.parent_tree_type(), + propagate, grove_version, ) } + GroveOp::InsertTreeWithRootHash { + flags, + aggregate_data, + .. + } => GroveDb::average_case_merk_insert_tree( + key, + flags, + aggregate_data.parent_tree_type(), + in_tree_type, + propagate_if_input(), + grove_version, + ), GroveOp::InsertOrReplace { element } | GroveOp::InsertOnly { element } => { GroveDb::average_case_merk_insert_element( key, element, - in_tree_using_sums, + in_tree_type, propagate_if_input(), grove_version, ) @@ -91,14 +94,14 @@ impl GroveOp { *max_reference_hop, flags.clone(), ), - in_tree_using_sums, + in_tree_type, propagate_if_input(), grove_version, ), GroveOp::Replace { element } => GroveDb::average_case_merk_replace_element( key, element, - in_tree_using_sums, + in_tree_type, propagate_if_input(), grove_version, ), @@ -109,7 +112,7 @@ impl GroveOp { key, element, *change_in_bytes, - in_tree_using_sums, + in_tree_type, propagate_if_input(), grove_version, ), @@ -119,16 +122,9 @@ impl GroveOp { propagate, grove_version, ), - GroveOp::DeleteTree => GroveDb::average_case_merk_delete_tree( + GroveOp::DeleteTree(tree_type) => GroveDb::average_case_merk_delete_tree( key, - false, - layer_element_estimates, - propagate, - grove_version, - ), - GroveOp::DeleteSumTree => GroveDb::average_case_merk_delete_tree( - key, - true, + *tree_type, layer_element_estimates, propagate, grove_version, @@ -142,7 +138,7 @@ impl GroveOp { #[derive(Default)] pub(in crate::batch) struct AverageCaseTreeCacheKnownPaths { paths: HashMap, - cached_merks: HashMap, + cached_merks: HashMap, } #[cfg(feature = "minimal")] @@ -167,7 +163,7 @@ impl fmt::Debug for AverageCaseTreeCacheKnownPaths { #[cfg(feature = "minimal")] impl TreeCache for AverageCaseTreeCacheKnownPaths { - fn insert(&mut self, op: &QualifiedGroveDbOp, is_sum_tree: bool) -> CostResult<(), Error> { + fn insert(&mut self, op: &QualifiedGroveDbOp, tree_type: TreeType) -> CostResult<(), Error> { let mut average_case_cost = OperationCost::default(); let mut inserted_path = op.path.clone(); inserted_path.push(op.key.clone()); @@ -175,7 +171,7 @@ impl TreeCache for AverageCaseTreeCacheKnownPaths { // empty at this point. // There is however a hash call that creates the prefix average_case_cost.hash_node_calls += 1; - self.cached_merks.insert(inserted_path, is_sum_tree); + self.cached_merks.insert(inserted_path, tree_type); Ok(()).wrap_with_cost(average_case_cost) } @@ -192,7 +188,7 @@ impl TreeCache for AverageCaseTreeCacheKnownPaths { _flags_update: &mut G, _split_removal_bytes: &mut SR, grove_version: &GroveVersion, - ) -> CostResult { + ) -> CostResult { let mut cost = OperationCost::default(); let layer_element_estimates = cost_return_on_error_no_add!( @@ -238,12 +234,11 @@ impl TreeCache for AverageCaseTreeCacheKnownPaths { &mut cost, path, layer_should_be_empty, - layer_info.is_sum_tree, + layer_info.tree_type, grove_version, ) ); - self.cached_merks - .insert(path.clone(), layer_info.is_sum_tree); + self.cached_merks.insert(path.clone(), layer_info.tree_type); } for (key, op) in ops_at_path_by_key.into_iter() { @@ -255,9 +250,10 @@ impl TreeCache for AverageCaseTreeCacheKnownPaths { cost_return_on_error!( &mut cost, - average_case_merk_propagate(layer_element_estimates).map_err(Error::MerkError) + average_case_merk_propagate(layer_element_estimates, grove_version) + .map_err(Error::MerkError) ); - Ok(([0u8; 32], None, None)).wrap_with_cost(cost) + Ok(([0u8; 32], None, AggregateData::NoAggregateData)).wrap_with_cost(cost) } fn update_base_merk_root_key( @@ -279,12 +275,12 @@ impl TreeCache for AverageCaseTreeCacheKnownPaths { estimated_layer_info .estimated_layer_count .estimated_to_be_empty(), - estimated_layer_info.is_sum_tree, + estimated_layer_info.tree_type, grove_version ) ); self.cached_merks - .insert(base_path, estimated_layer_info.is_sum_tree); + .insert(base_path, estimated_layer_info.tree_type); } } Ok(()).wrap_with_cost(cost) @@ -300,11 +296,14 @@ mod tests { storage_cost::{removal::StorageRemovedBytes::NoStorageRemoval, StorageCost}, OperationCost, }; - use grovedb_merk::estimated_costs::average_case_costs::{ - EstimatedLayerCount::{ApproximateElements, EstimatedLevel, PotentiallyAtMaxElements}, - EstimatedLayerInformation, - EstimatedLayerSizes::{AllItems, AllSubtrees}, - EstimatedSumTrees::{NoSumTrees, SomeSumTrees}, + use grovedb_merk::{ + estimated_costs::average_case_costs::{ + EstimatedLayerCount::{ApproximateElements, EstimatedLevel, PotentiallyAtMaxElements}, + EstimatedLayerInformation, + EstimatedLayerSizes::{AllItems, AllSubtrees}, + EstimatedSumTrees::{NoSumTrees, SomeSumTrees}, + }, + tree_type::TreeType, }; use grovedb_version::version::GroveVersion; @@ -332,7 +331,7 @@ mod tests { paths.insert( KeyInfoPath(vec![]), EstimatedLayerInformation { - is_sum_tree: false, + tree_type: TreeType::NormalTree, estimated_layer_count: ApproximateElements(0), estimated_layer_sizes: AllSubtrees(4, NoSumTrees, None), }, @@ -401,7 +400,7 @@ mod tests { paths.insert( KeyInfoPath(vec![]), EstimatedLayerInformation { - is_sum_tree: false, + tree_type: TreeType::NormalTree, estimated_layer_count: EstimatedLevel(0, true), estimated_layer_sizes: AllSubtrees(4, NoSumTrees, Some(3)), }, @@ -409,7 +408,7 @@ mod tests { paths.insert( KeyInfoPath(vec![KeyInfo::KnownKey(b"key1".to_vec())]), EstimatedLayerInformation { - is_sum_tree: false, + tree_type: TreeType::NormalTree, estimated_layer_count: EstimatedLevel(0, true), estimated_layer_sizes: AllSubtrees(4, NoSumTrees, None), }, @@ -468,7 +467,7 @@ mod tests { paths.insert( KeyInfoPath(vec![]), EstimatedLayerInformation { - is_sum_tree: false, + tree_type: TreeType::NormalTree, estimated_layer_count: EstimatedLevel(0, true), estimated_layer_sizes: AllItems(4, 3, None), }, @@ -541,7 +540,7 @@ mod tests { paths.insert( KeyInfoPath(vec![]), EstimatedLayerInformation { - is_sum_tree: false, + tree_type: TreeType::NormalTree, estimated_layer_count: EstimatedLevel(1, false), estimated_layer_sizes: AllSubtrees(1, NoSumTrees, None), }, @@ -627,7 +626,7 @@ mod tests { paths.insert( KeyInfoPath(vec![]), EstimatedLayerInformation { - is_sum_tree: false, + tree_type: TreeType::NormalTree, estimated_layer_count: EstimatedLevel(0, false), estimated_layer_sizes: AllSubtrees(1, NoSumTrees, None), }, @@ -636,7 +635,7 @@ mod tests { paths.insert( KeyInfoPath(vec![KeyInfo::KnownKey(b"0".to_vec())]), EstimatedLayerInformation { - is_sum_tree: false, + tree_type: TreeType::NormalTree, estimated_layer_count: EstimatedLevel(0, true), estimated_layer_sizes: AllSubtrees(4, NoSumTrees, None), }, @@ -707,12 +706,15 @@ mod tests { paths.insert( KeyInfoPath(vec![]), EstimatedLayerInformation { - is_sum_tree: false, + tree_type: TreeType::NormalTree, estimated_layer_count: EstimatedLevel(1, false), estimated_layer_sizes: AllSubtrees( 1, SomeSumTrees { sum_trees_weight: 1, + big_sum_trees_weight: 0, + count_trees_weight: 0, + count_sum_trees_weight: 0, non_sum_trees_weight: 1, }, None, @@ -722,7 +724,7 @@ mod tests { paths.insert( KeyInfoPath::from_known_owned_path(vec![vec![7]]), EstimatedLayerInformation { - is_sum_tree: true, + tree_type: TreeType::SumTree, estimated_layer_count: PotentiallyAtMaxElements, estimated_layer_sizes: AllItems(32, 8, None), }, @@ -785,7 +787,7 @@ mod tests { paths.insert( KeyInfoPath(vec![]), EstimatedLayerInformation { - is_sum_tree: false, + tree_type: TreeType::NormalTree, estimated_layer_count: EstimatedLevel(1, false), estimated_layer_sizes: AllSubtrees(4, NoSumTrees, None), }, @@ -794,7 +796,7 @@ mod tests { paths.insert( KeyInfoPath(vec![KeyInfo::KnownKey(b"0".to_vec())]), EstimatedLayerInformation { - is_sum_tree: false, + tree_type: TreeType::NormalTree, estimated_layer_count: EstimatedLevel(0, true), estimated_layer_sizes: AllSubtrees(4, NoSumTrees, None), }, diff --git a/grovedb/src/batch/estimated_costs/worst_case_costs.rs b/grovedb/src/batch/estimated_costs/worst_case_costs.rs index 5e4f6e42..b48109ad 100644 --- a/grovedb/src/batch/estimated_costs/worst_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/worst_case_costs.rs @@ -14,7 +14,7 @@ use grovedb_costs::{ use grovedb_merk::estimated_costs::worst_case_costs::{ worst_case_merk_propagate, WorstCaseLayerInformation, }; -use grovedb_merk::RootHashKeyAndSum; +use grovedb_merk::{tree::AggregateData, tree_type::TreeType, RootHashKeyAndAggregateData}; #[cfg(feature = "minimal")] use grovedb_storage::rocksdb_storage::RocksDbStorage; use grovedb_version::version::GroveVersion; @@ -36,7 +36,7 @@ impl GroveOp { fn worst_case_cost( &self, key: &KeyInfo, - is_in_parent_sum_tree: bool, + in_parent_tree_type: TreeType, worst_case_layer_element_estimates: &WorstCaseLayerInformation, propagate: bool, grove_version: &GroveVersion, @@ -49,29 +49,33 @@ impl GroveOp { } }; match self { - GroveOp::ReplaceTreeRootKey { sum, .. } => GroveDb::worst_case_merk_replace_tree( - key, - sum.is_some(), - is_in_parent_sum_tree, - worst_case_layer_element_estimates, - propagate, - grove_version, - ), - GroveOp::InsertTreeWithRootHash { flags, sum, .. } => { - GroveDb::worst_case_merk_insert_tree( + GroveOp::ReplaceTreeRootKey { aggregate_data, .. } => { + GroveDb::worst_case_merk_replace_tree( key, - flags, - sum.is_some(), - is_in_parent_sum_tree, - propagate_if_input(), + aggregate_data.parent_tree_type(), + in_parent_tree_type, + worst_case_layer_element_estimates, + propagate, grove_version, ) } + GroveOp::InsertTreeWithRootHash { + flags, + aggregate_data, + .. + } => GroveDb::worst_case_merk_insert_tree( + key, + flags, + aggregate_data.parent_tree_type(), + in_parent_tree_type, + propagate_if_input(), + grove_version, + ), GroveOp::InsertOrReplace { element } | GroveOp::InsertOnly { element } => { GroveDb::worst_case_merk_insert_element( key, element, - is_in_parent_sum_tree, + in_parent_tree_type, propagate_if_input(), grove_version, ) @@ -88,14 +92,14 @@ impl GroveOp { *max_reference_hop, flags.clone(), ), - is_in_parent_sum_tree, + in_parent_tree_type, propagate_if_input(), grove_version, ), GroveOp::Replace { element } => GroveDb::worst_case_merk_replace_element( key, element, - is_in_parent_sum_tree, + in_parent_tree_type, propagate_if_input(), grove_version, ), @@ -105,7 +109,7 @@ impl GroveOp { } => GroveDb::worst_case_merk_replace_element( key, element, - is_in_parent_sum_tree, + in_parent_tree_type, propagate_if_input(), grove_version, ), @@ -115,16 +119,9 @@ impl GroveOp { propagate, grove_version, ), - GroveOp::DeleteTree => GroveDb::worst_case_merk_delete_tree( - key, - false, - worst_case_layer_element_estimates, - propagate, - grove_version, - ), - GroveOp::DeleteSumTree => GroveDb::worst_case_merk_delete_tree( + GroveOp::DeleteTree(tree_type) => GroveDb::worst_case_merk_delete_tree( key, - true, + *tree_type, worst_case_layer_element_estimates, propagate, grove_version, @@ -163,7 +160,7 @@ impl fmt::Debug for WorstCaseTreeCacheKnownPaths { #[cfg(feature = "minimal")] impl TreeCache for WorstCaseTreeCacheKnownPaths { - fn insert(&mut self, op: &QualifiedGroveDbOp, _is_sum_tree: bool) -> CostResult<(), Error> { + fn insert(&mut self, op: &QualifiedGroveDbOp, _tree_type: TreeType) -> CostResult<(), Error> { let mut worst_case_cost = OperationCost::default(); let mut inserted_path = op.path.clone(); inserted_path.push(op.key.clone()); @@ -188,7 +185,7 @@ impl TreeCache for WorstCaseTreeCacheKnownPaths { _flags_update: &mut G, _split_removal_bytes: &mut SR, grove_version: &GroveVersion, - ) -> CostResult { + ) -> CostResult { let mut cost = OperationCost::default(); let worst_case_layer_element_estimates = cost_return_on_error_no_add!( @@ -208,7 +205,7 @@ impl TreeCache for WorstCaseTreeCacheKnownPaths { GroveDb::add_worst_case_get_merk_at_path::( &mut cost, path, - false, + TreeType::NormalTree, grove_version, ) ); @@ -220,7 +217,7 @@ impl TreeCache for WorstCaseTreeCacheKnownPaths { &mut cost, op.worst_case_cost( &key, - false, + TreeType::NormalTree, worst_case_layer_element_estimates, false, grove_version @@ -232,7 +229,7 @@ impl TreeCache for WorstCaseTreeCacheKnownPaths { &mut cost, worst_case_merk_propagate(worst_case_layer_element_estimates).map_err(Error::MerkError) ); - Ok(([0u8; 32], None, None)).wrap_with_cost(cost) + Ok(([0u8; 32], None, AggregateData::NoAggregateData)).wrap_with_cost(cost) } fn update_base_merk_root_key( @@ -251,7 +248,7 @@ impl TreeCache for WorstCaseTreeCacheKnownPaths { GroveDb::add_worst_case_get_merk_at_path::( &mut cost, &base_path, - false, + TreeType::NormalTree, grove_version, ) ); diff --git a/grovedb/src/batch/just_in_time_reference_update.rs b/grovedb/src/batch/just_in_time_reference_update.rs index f4385b89..06081eb2 100644 --- a/grovedb/src/batch/just_in_time_reference_update.rs +++ b/grovedb/src/batch/just_in_time_reference_update.rs @@ -10,6 +10,7 @@ use grovedb_costs::{ }; use grovedb_merk::{ tree::{kv::KV, value_hash, TreeNode}, + tree_type::TreeType, CryptoHash, Merk, }; use grovedb_storage::StorageContext; @@ -31,7 +32,7 @@ where new_element: &mut Element, old_element: Element, old_serialized_element: &[u8], - is_in_sum_tree: bool, + in_tree_type: TreeType, flags_update: &mut G, split_removal_bytes: &mut SR, grove_version: &GroveVersion, @@ -79,7 +80,7 @@ where let old_storage_cost = KV::node_value_byte_cost_size( key.len() as u32, old_serialized_element.len() as u32, - is_in_sum_tree, + in_tree_type.inner_node_type(), ); let original_new_element = new_element.clone(); @@ -99,10 +100,14 @@ where KV::node_value_byte_cost_size( key.len() as u32, serialized_with_old_flags.len() as u32, - is_in_sum_tree, + in_tree_type.inner_node_type(), ) } else { - KV::node_value_byte_cost_size(key.len() as u32, serialized.len() as u32, is_in_sum_tree) + KV::node_value_byte_cost_size( + key.len() as u32, + serialized.len() as u32, + in_tree_type.inner_node_type(), + ) }; let mut i = 0; @@ -153,7 +158,7 @@ where new_storage_cost = KV::node_value_byte_cost_size( key.len() as u32, new_serialized_bytes.len() as u32, - is_in_sum_tree, + in_tree_type.inner_node_type(), ); if serialization_to_use == new_serialized_bytes { diff --git a/grovedb/src/batch/mod.rs b/grovedb/src/batch/mod.rs index 7f73fea0..3d35525c 100644 --- a/grovedb/src/batch/mod.rs +++ b/grovedb/src/batch/mod.rs @@ -50,10 +50,10 @@ use grovedb_costs::{ use grovedb_merk::{ tree::{ kv::ValueDefinedCostType::{LayeredValueDefinedCost, SpecializedValueDefinedCost}, - value_hash, NULL_HASH, + value_hash, AggregateData, NULL_HASH, }, - CryptoHash, Error as MerkError, Merk, MerkType, Op, RootHashKeyAndSum, - TreeFeatureType::{BasicMerkNode, SummedMerkNode}, + tree_type::TreeType, + CryptoHash, Error as MerkError, Merk, MerkType, Op, RootHashKeyAndAggregateData, }; use grovedb_path::SubtreePath; use grovedb_storage::{ @@ -74,7 +74,10 @@ pub use crate::batch::batch_structure::{OpsByLevelPath, OpsByPath}; use crate::batch::estimated_costs::EstimatedCostsType; use crate::{ batch::{batch_structure::BatchStructure, mode::BatchRunMode}, - element::{MaxReferenceHop, SUM_ITEM_COST_SIZE, SUM_TREE_COST_SIZE, TREE_COST_SIZE}, + element::{ + MaxReferenceHop, BIG_SUM_TREE_COST_SIZE, COUNT_SUM_TREE_COST_SIZE, COUNT_TREE_COST_SIZE, + SUM_ITEM_COST_SIZE, SUM_TREE_COST_SIZE, TREE_COST_SIZE, + }, operations::{get::MAX_REFERENCE_HOPS, proof::util::hex_to_ascii}, reference_path::{ path_from_reference_path_type, path_from_reference_qualified_path_type, ReferencePathType, @@ -91,8 +94,8 @@ pub enum GroveOp { hash: [u8; 32], /// Root key root_key: Option>, - /// Sum - sum: Option, + /// Aggregate data + aggregate_data: AggregateData, }, /// Inserts an element that is known to not yet exist InsertOnly { @@ -124,8 +127,8 @@ pub enum GroveOp { root_key: Option>, /// Flags flags: Option, - /// Sum - sum: Option, + /// Aggregate Data such as sum + aggregate_data: AggregateData, }, /// Refresh the reference with information provided /// Providing this information is necessary to be able to calculate @@ -142,16 +145,14 @@ pub enum GroveOp { /// Delete Delete, /// Delete tree - DeleteTree, - /// Delete sum tree - DeleteSumTree, + DeleteTree(TreeType), } impl GroveOp { fn to_u8(&self) -> u8 { match self { - GroveOp::DeleteTree => 0, - GroveOp::DeleteSumTree => 1, + GroveOp::DeleteTree(_) => 0, + // 1 used to be used for the DeleteSumTree GroveOp::Delete => 2, GroveOp::InsertTreeWithRootHash { .. } => 3, GroveOp::ReplaceTreeRootKey { .. } => 4, @@ -378,8 +379,7 @@ impl fmt::Debug for QualifiedGroveDbOp { ) } GroveOp::Delete => "Delete".to_string(), - GroveOp::DeleteTree => "Delete Tree".to_string(), - GroveOp::DeleteSumTree => "Delete Sum Tree".to_string(), + GroveOp::DeleteTree(tree_type) => format!("Delete Tree {}", tree_type), GroveOp::ReplaceTreeRootKey { .. } => "Replace Tree Hash and Root Key".to_string(), GroveOp::InsertTreeWithRootHash { .. } => "Insert Tree Hash and Root Key".to_string(), }; @@ -509,16 +509,12 @@ impl QualifiedGroveDbOp { } /// A delete tree op using a known owned path and known key - pub fn delete_tree_op(path: Vec>, key: Vec, is_sum_tree: bool) -> Self { + pub fn delete_tree_op(path: Vec>, key: Vec, tree_type: TreeType) -> Self { let path = KeyInfoPath::from_known_owned_path(path); Self { path, key: KnownKey(key), - op: if is_sum_tree { - GroveOp::DeleteSumTree - } else { - GroveOp::DeleteTree - }, + op: GroveOp::DeleteTree(tree_type), } } @@ -532,15 +528,11 @@ impl QualifiedGroveDbOp { } /// A delete tree op - pub fn delete_estimated_tree_op(path: KeyInfoPath, key: KeyInfo, is_sum_tree: bool) -> Self { + pub fn delete_estimated_tree_op(path: KeyInfoPath, key: KeyInfo, tree_type: TreeType) -> Self { Self { path, key, - op: if is_sum_tree { - GroveOp::DeleteSumTree - } else { - GroveOp::DeleteTree - }, + op: GroveOp::DeleteTree(tree_type), } } @@ -681,7 +673,7 @@ impl fmt::Debug for TreeCacheMerkByPath { } trait TreeCache { - fn insert(&mut self, op: &QualifiedGroveDbOp, is_sum_tree: bool) -> CostResult<(), Error>; + fn insert(&mut self, op: &QualifiedGroveDbOp, tree_type: TreeType) -> CostResult<(), Error>; fn get_batch_run_mode(&self) -> BatchRunMode; @@ -695,7 +687,7 @@ trait TreeCache { flags_update: &mut G, split_removal_bytes: &mut SR, grove_version: &GroveVersion, - ) -> CostResult; + ) -> CostResult; fn update_base_merk_root_key( &mut self, @@ -863,10 +855,10 @@ where /// /// # Returns /// - /// * `Ok((Element, Vec, bool))` - Returns the deserialized `Element` - /// and the serialized counterpart if the retrieval and deserialization - /// are successful, wrapped in the associated cost. Also returns if the - /// merk of the element is a sum tree as a bool. + /// * `Ok((Element, Vec, TreeType))` - Returns the deserialized + /// `Element` and the serialized counterpart if the retrieval and + /// deserialization are successful, wrapped in the associated cost. Also + /// returns if the merk of the element is a sum tree as a TreeType. /// * `Err(Error)` - Returns an error if any issue occurs during the /// retrieval or deserialization of the referenced element. /// @@ -883,7 +875,7 @@ where key: &[u8], reference_path: &[Vec], grove_version: &GroveVersion, - ) -> CostResult, bool)>, Error> { + ) -> CostResult, TreeType)>, Error> { let mut cost = OperationCost::default(); let merk = match self.merks.entry(reference_path.to_vec()) { @@ -905,7 +897,7 @@ where .map_err(|e| Error::CorruptedData(e.to_string())) ); - let is_sum_tree = merk.is_sum_tree; + let tree_type = merk.tree_type; if let Some(referenced_element) = referenced_element { let element = cost_return_on_error_no_add!( @@ -915,7 +907,7 @@ where }) ); - Ok(Some((element, referenced_element, is_sum_tree))).wrap_with_cost(cost) + Ok(Some((element, referenced_element, tree_type))).wrap_with_cost(cost) } else { Ok(None).wrap_with_cost(cost) } @@ -1027,7 +1019,11 @@ where grove_version, ) } - Element::Tree(..) | Element::SumTree(..) => Err(Error::InvalidBatchOperation( + Element::Tree(..) + | Element::SumTree(..) + | Element::BigSumTree(..) + | Element::CountTree(..) + | Element::CountSumTree(..) => Err(Error::InvalidBatchOperation( "references can not point to trees being updated", )) .wrap_with_cost(cost), @@ -1145,12 +1141,14 @@ where grove_version, ) } - Element::Tree(..) | Element::SumTree(..) => { - Err(Error::InvalidBatchOperation( - "references can not point to trees being updated", - )) - .wrap_with_cost(cost) - } + Element::Tree(..) + | Element::SumTree(..) + | Element::BigSumTree(..) + | Element::CountTree(..) + | Element::CountSumTree(..) => Err(Error::InvalidBatchOperation( + "references can not point to trees being updated", + )) + .wrap_with_cost(cost), } } GroveOp::InsertOnly { element } => match element { @@ -1174,7 +1172,11 @@ where grove_version, ) } - Element::Tree(..) | Element::SumTree(..) => Err(Error::InvalidBatchOperation( + Element::Tree(..) + | Element::SumTree(..) + | Element::BigSumTree(..) + | Element::CountTree(..) + | Element::CountSumTree(..) => Err(Error::InvalidBatchOperation( "references can not point to trees being updated", )) .wrap_with_cost(cost), @@ -1200,12 +1202,10 @@ where grove_version, ) } - GroveOp::Delete | GroveOp::DeleteTree | GroveOp::DeleteSumTree => { - Err(Error::InvalidBatchOperation( - "references can not point to something currently being deleted", - )) - .wrap_with_cost(cost) - } + GroveOp::Delete | GroveOp::DeleteTree(_) => Err(Error::InvalidBatchOperation( + "references can not point to something currently being deleted", + )) + .wrap_with_cost(cost), } } else { self.process_reference( @@ -1232,7 +1232,7 @@ where F: FnMut(&[Vec], bool) -> CostResult, Error>, S: StorageContext<'db>, { - fn insert(&mut self, op: &QualifiedGroveDbOp, is_sum_tree: bool) -> CostResult<(), Error> { + fn insert(&mut self, op: &QualifiedGroveDbOp, tree_type: TreeType) -> CostResult<(), Error> { let mut cost = OperationCost::default(); let mut inserted_path = op.path.to_path(); @@ -1240,7 +1240,7 @@ where if let HashMapEntry::Vacant(e) = self.merks.entry(inserted_path.clone()) { let mut merk = cost_return_on_error!(&mut cost, (self.get_merk_fn)(&inserted_path, true)); - merk.is_sum_tree = is_sum_tree; + merk.tree_type = tree_type; e.insert(merk); } @@ -1277,14 +1277,14 @@ where flags_update: &mut G, split_removal_bytes: &mut SR, grove_version: &GroveVersion, - ) -> CostResult { + ) -> CostResult { let mut cost = OperationCost::default(); // todo: fix this let p = path.to_path(); let path = &p; // This also populates Merk trees cache - let is_sum_tree = { + let in_tree_type = { let merk = match self.merks.entry(path.to_vec()) { HashMapEntry::Occupied(o) => o.into_mut(), HashMapEntry::Vacant(v) => v.insert(cost_return_on_error!( @@ -1292,7 +1292,7 @@ where (self.get_merk_fn)(path, false) )), }; - merk.is_sum_tree + merk.tree_type }; let mut batch_operations: Vec<(Vec, Op)> = vec![]; @@ -1306,7 +1306,7 @@ where let merk_feature_type = cost_return_on_error!( &mut cost, element - .get_feature_type(is_sum_tree) + .get_feature_type(in_tree_type) .wrap_with_cost(OperationCost::default()) ); let path_reference = cost_return_on_error!( @@ -1348,11 +1348,15 @@ where ) ); } - Element::Tree(..) | Element::SumTree(..) => { + Element::Tree(..) + | Element::SumTree(..) + | Element::BigSumTree(..) + | Element::CountTree(..) + | Element::CountSumTree(..) => { let merk_feature_type = cost_return_on_error!( &mut cost, element - .get_feature_type(is_sum_tree) + .get_feature_type(in_tree_type) .wrap_with_cost(OperationCost::default()) ); cost_return_on_error!( @@ -1371,7 +1375,7 @@ where let merk_feature_type = cost_return_on_error!( &mut cost, element - .get_feature_type(is_sum_tree) + .get_feature_type(in_tree_type) .wrap_with_cost(OperationCost::default()) ); if batch_apply_options.validate_insertion_does_not_override { @@ -1450,11 +1454,7 @@ where .wrap_with_cost(cost); }; - let merk_feature_type = if is_sum_tree { - SummedMerkNode(0) - } else { - BasicMerkNode - }; + let merk_feature_type = in_tree_type.empty_tree_feature_type(); let path_reference = cost_return_on_error!( &mut cost, @@ -1501,32 +1501,20 @@ where Element::delete_into_batch_operations( key_info.get_key(), false, - is_sum_tree, /* we are in a sum tree, this might or might not be a - * sum item */ + in_tree_type, /* we are in a sum tree, this might or might not be a + * sum item */ &mut batch_operations, grove_version ) ); } - GroveOp::DeleteTree => { + GroveOp::DeleteTree(tree_type) => { cost_return_on_error!( &mut cost, Element::delete_into_batch_operations( key_info.get_key(), true, - false, - &mut batch_operations, - grove_version - ) - ); - } - GroveOp::DeleteSumTree => { - cost_return_on_error!( - &mut cost, - Element::delete_into_batch_operations( - key_info.get_key(), - true, - true, + tree_type, &mut batch_operations, grove_version ) @@ -1535,7 +1523,7 @@ where GroveOp::ReplaceTreeRootKey { hash, root_key, - sum, + aggregate_data, } => { let merk = self.merks.get(path).expect("the Merk is cached"); cost_return_on_error!( @@ -1545,7 +1533,7 @@ where key_info.get_key(), root_key, hash, - sum, + aggregate_data, &mut batch_operations, grove_version ) @@ -1555,16 +1543,40 @@ where hash, root_key, flags, - sum, + aggregate_data, } => { - let element = match sum { - None => Element::new_tree_with_flags(root_key, flags), - Some(sum_value) => Element::new_sum_tree_with_flags_and_sum_value( - root_key, sum_value, flags, - ), + let element = match aggregate_data { + AggregateData::NoAggregateData => { + Element::new_tree_with_flags(root_key, flags) + } + AggregateData::Sum(sum_value) => { + Element::new_sum_tree_with_flags_and_sum_value( + root_key, sum_value, flags, + ) + } + AggregateData::BigSum(sum_value) => { + Element::new_big_sum_tree_with_flags_and_sum_value( + root_key, sum_value, flags, + ) + } + AggregateData::Count(count_value) => { + Element::new_count_tree_with_flags_and_count_value( + root_key, + count_value, + flags, + ) + } + AggregateData::CountAndSum(count_value, sum_value) => { + Element::new_count_sum_tree_with_flags_and_sum_and_count_value( + root_key, + count_value, + sum_value, + flags, + ) + } }; let merk_feature_type = - cost_return_on_error_no_add!(&cost, element.get_feature_type(is_sum_tree)); + cost_return_on_error_no_add!(&cost, element.get_feature_type(in_tree_type)); cost_return_on_error!( &mut cost, @@ -1590,8 +1602,13 @@ where &[], Some(batch_apply_options.as_merk_options()), &|key, value| { - Element::specialized_costs_for_key_value(key, value, is_sum_tree, grove_version) - .map_err(|e| MerkError::ClientCorruptionError(e.to_string())) + Element::specialized_costs_for_key_value( + key, + value, + in_tree_type.inner_node_type(), + grove_version, + ) + .map_err(|e| MerkError::ClientCorruptionError(e.to_string())) }, Some(&Element::value_defined_cost_for_serialized_value), &|old_value, new_value| { @@ -1642,11 +1659,18 @@ where // we need to give back the value defined cost in the case that the // new element is a tree match new_element { - Element::Tree(..) | Element::SumTree(..) => { - let tree_cost_size = if new_element.is_sum_tree() { - SUM_TREE_COST_SIZE - } else { - TREE_COST_SIZE + Element::Tree(..) + | Element::SumTree(..) + | Element::BigSumTree(..) + | Element::CountTree(..) + | Element::CountSumTree(..) => { + let tree_type = new_element.tree_type().unwrap(); + let tree_cost_size = match tree_type { + 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, }; let tree_value_cost = tree_cost_size + flags_len @@ -1690,7 +1714,7 @@ where .map_err(|e| Error::CorruptedData(e.to_string())) ); let r = merk - .root_hash_key_and_sum() + .root_hash_key_and_aggregate_data() .add_cost(cost) .map_err(Error::MerkError); @@ -1776,7 +1800,7 @@ impl GroveDb { ); } } else { - let (root_hash, calculated_root_key, sum_value) = cost_return_on_error!( + let (root_hash, calculated_root_key, aggregate_data) = cost_return_on_error!( &mut cost, merk_tree_cache.execute_ops_on_path( &path, @@ -1806,7 +1830,7 @@ impl GroveDb { GroveOp::ReplaceTreeRootKey { hash: root_hash, root_key: calculated_root_key, - sum: sum_value, + aggregate_data, } .into(), ); @@ -1817,11 +1841,11 @@ impl GroveDb { GroveOp::ReplaceTreeRootKey { hash, root_key, - sum, + aggregate_data: aggregate_data_entry, } => { *hash = root_hash; *root_key = calculated_root_key; - *sum = sum_value; + *aggregate_data_entry = aggregate_data; } GroveOp::InsertTreeWithRootHash { .. } => { return Err(Error::CorruptedCodeExecution( @@ -1839,7 +1863,8 @@ impl GroveDb { hash: root_hash, root_key: calculated_root_key, flags: flags.clone(), - sum: None, + aggregate_data: + AggregateData::NoAggregateData, } .into(); } else if let Element::SumTree(.., flags) = @@ -1850,7 +1875,40 @@ impl GroveDb { hash: root_hash, root_key: calculated_root_key, flags: flags.clone(), - sum: sum_value, + aggregate_data, + } + .into(); + } else if let Element::BigSumTree(.., flags) = + element + { + *mutable_occupied_entry = + GroveOp::InsertTreeWithRootHash { + hash: root_hash, + root_key: calculated_root_key, + flags: flags.clone(), + aggregate_data, + } + .into(); + } else if let Element::CountTree(.., flags) = + element + { + *mutable_occupied_entry = + GroveOp::InsertTreeWithRootHash { + hash: root_hash, + root_key: calculated_root_key, + flags: flags.clone(), + aggregate_data, + } + .into(); + } else if let Element::CountSumTree(.., flags) = + element + { + *mutable_occupied_entry = + GroveOp::InsertTreeWithRootHash { + hash: root_hash, + root_key: calculated_root_key, + flags: flags.clone(), + aggregate_data, } .into(); } else { @@ -1867,9 +1925,7 @@ impl GroveDb { )) .wrap_with_cost(cost); } - GroveOp::Delete - | GroveOp::DeleteTree - | GroveOp::DeleteSumTree => { + GroveOp::Delete | GroveOp::DeleteTree(_) => { if calculated_root_key.is_some() { return Err(Error::InvalidBatchOperation( "modification of tree when it will be \ @@ -1889,7 +1945,7 @@ impl GroveDb { GroveOp::ReplaceTreeRootKey { hash: root_hash, root_key: calculated_root_key, - sum: sum_value, + aggregate_data, }, ); ops_at_level_above.insert(parent_path, ops_on_path); @@ -1901,7 +1957,7 @@ impl GroveDb { GroveOp::ReplaceTreeRootKey { hash: root_hash, root_key: calculated_root_key, - sum: sum_value, + aggregate_data, } .into(), ); @@ -2160,7 +2216,12 @@ impl GroveDb { if let Some((parent_path, parent_key)) = path.derive_parent() { if new_merk { // TODO: can this be a sum tree - Ok(Merk::open_empty(storage, MerkType::LayeredMerk, false)).wrap_with_cost(cost) + Ok(Merk::open_empty( + storage, + MerkType::LayeredMerk, + TreeType::NormalTree, + )) + .wrap_with_cost(cost) } else { let parent_storage = self .db @@ -2183,12 +2244,11 @@ impl GroveDb { } ) ); - let is_sum_tree = element.is_sum_tree(); - if let Element::Tree(root_key, _) | Element::SumTree(root_key, ..) = element { + if let Some((root_key, tree_type)) = element.root_key_and_tree_type_owned() { Merk::open_layered_with_root_key( storage, root_key, - is_sum_tree, + tree_type, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -2204,11 +2264,16 @@ impl GroveDb { } } } else if new_merk { - Ok(Merk::open_empty(storage, MerkType::BaseMerk, false)).wrap_with_cost(cost) + Ok(Merk::open_empty( + storage, + MerkType::BaseMerk, + TreeType::NormalTree, + )) + .wrap_with_cost(cost) } else { Merk::open_base( storage, - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -2244,7 +2309,8 @@ impl GroveDb { } else { MerkType::LayeredMerk }; - Ok(Merk::open_empty(storage, merk_type, false)).wrap_with_cost(local_cost) + Ok(Merk::open_empty(storage, merk_type, TreeType::NormalTree)) + .wrap_with_cost(local_cost) } else if let Some((base_path, last)) = path.derive_parent() { let parent_storage = self .db @@ -2254,12 +2320,11 @@ impl GroveDb { &mut local_cost, Element::get_from_storage(&parent_storage, last, grove_version) ); - let is_sum_tree = element.is_sum_tree(); - if let Element::Tree(root_key, _) | Element::SumTree(root_key, ..) = element { + if let Some((root_key, tree_type)) = element.root_key_and_tree_type_owned() { Merk::open_layered_with_root_key( storage, root_key, - is_sum_tree, + tree_type, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -2276,7 +2341,7 @@ impl GroveDb { } else { Merk::open_base( storage, - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) diff --git a/grovedb/src/batch/single_deletion_cost_tests.rs b/grovedb/src/batch/single_deletion_cost_tests.rs index c84a2e47..6e784e57 100644 --- a/grovedb/src/batch/single_deletion_cost_tests.rs +++ b/grovedb/src/batch/single_deletion_cost_tests.rs @@ -7,6 +7,7 @@ mod tests { Identifier, StorageRemovalPerEpochByIdentifier, StorageRemovedBytes::SectionedStorageRemoval, }; + use grovedb_merk::tree_type::TreeType; use grovedb_version::version::GroveVersion; use intmap::IntMap; @@ -75,7 +76,7 @@ mod tests { let ops = vec![QualifiedGroveDbOp::delete_tree_op( vec![], b"key1".to_vec(), - false, + TreeType::NormalTree, )]; let batch_cost = db .apply_batch(ops, None, Some(&tx), grove_version) @@ -219,7 +220,7 @@ mod tests { let ops = vec![QualifiedGroveDbOp::delete_tree_op( vec![], b"key1".to_vec(), - false, + TreeType::NormalTree, )]; let batch_cost = db .apply_batch(ops, None, None, grove_version) @@ -368,7 +369,7 @@ mod tests { let ops = vec![QualifiedGroveDbOp::delete_tree_op( vec![], b"key1".to_vec(), - false, + TreeType::NormalTree, )]; let batch_cost = db .apply_batch(ops, None, Some(&tx), grove_version) @@ -467,7 +468,7 @@ mod tests { let ops = vec![QualifiedGroveDbOp::delete_tree_op( vec![], b"key1".to_vec(), - false, + TreeType::NormalTree, )]; let batch_cost = db .apply_batch_with_element_flags_update( @@ -642,7 +643,7 @@ mod tests { let ops = vec![QualifiedGroveDbOp::delete_tree_op( vec![], b"key1".to_vec(), - false, + TreeType::NormalTree, )]; let batch_cost = db .apply_batch(ops, None, None, grove_version) diff --git a/grovedb/src/batch/single_sum_item_deletion_cost_tests.rs b/grovedb/src/batch/single_sum_item_deletion_cost_tests.rs index 7db03b7f..4763af16 100644 --- a/grovedb/src/batch/single_sum_item_deletion_cost_tests.rs +++ b/grovedb/src/batch/single_sum_item_deletion_cost_tests.rs @@ -2,6 +2,7 @@ #[cfg(feature = "minimal")] mod tests { + use grovedb_merk::tree_type::TreeType; use grovedb_version::version::GroveVersion; use crate::{ @@ -46,7 +47,7 @@ mod tests { let ops = vec![QualifiedGroveDbOp::delete_tree_op( vec![], b"key1".to_vec(), - false, + TreeType::NormalTree, )]; let batch_cost = db .apply_batch(ops, None, Some(&tx), grove_version) @@ -153,7 +154,7 @@ mod tests { let ops = vec![QualifiedGroveDbOp::delete_tree_op( vec![], b"key1".to_vec(), - false, + TreeType::NormalTree, )]; let batch_cost = db .apply_batch(ops, None, Some(&tx), grove_version) diff --git a/grovedb/src/debugger.rs b/grovedb/src/debugger.rs index 1920ff81..696a3a6a 100644 --- a/grovedb/src/debugger.rs +++ b/grovedb/src/debugger.rs @@ -397,22 +397,28 @@ fn merk_proof_node_to_grovedbg(node: Node) -> Result { - let element = crate::Element::deserialize(&value, GroveVersion::latest())?; - MerkProofNode::KVValueHashFeatureType( - key, - element_to_grovedbg(element), - hash, - grovedbg_types::TreeFeatureType::BasicMerkNode, - ) - } - Node::KVValueHashFeatureType(key, value, hash, TreeFeatureType::SummedMerkNode(sum)) => { + Node::KVValueHashFeatureType(key, value, hash, feature_type) => { let element = crate::Element::deserialize(&value, GroveVersion::latest())?; + let node_feature_type = match feature_type { + TreeFeatureType::BasicMerkNode => grovedbg_types::TreeFeatureType::BasicMerkNode, + TreeFeatureType::SummedMerkNode(sum) => { + grovedbg_types::TreeFeatureType::SummedMerkNode(sum) + } + TreeFeatureType::BigSummedMerkNode(sum) => { + grovedbg_types::TreeFeatureType::BigSummedMerkNode(sum) + } + TreeFeatureType::CountedMerkNode(count) => { + grovedbg_types::TreeFeatureType::CountedMerkNode(count) + } + TreeFeatureType::CountedSummedMerkNode(count, sum) => { + grovedbg_types::TreeFeatureType::CountedSummedMerkNode(count, sum) + } + }; MerkProofNode::KVValueHashFeatureType( key, element_to_grovedbg(element), hash, - grovedbg_types::TreeFeatureType::SummedMerkNode(sum), + node_feature_type, ) } Node::KVRefValueHash(key, value, hash) => { @@ -597,6 +603,28 @@ fn element_to_grovedbg(element: crate::Element) -> grovedbg_types::Element { sum, element_flags, }, + crate::Element::BigSumTree(root_key, sum, element_flags) => { + grovedbg_types::Element::BigSumTree { + root_key, + sum, + element_flags, + } + } + crate::Element::CountTree(root_key, count, element_flags) => { + grovedbg_types::Element::CountTree { + root_key, + count, + element_flags, + } + } + crate::Element::CountSumTree(root_key, count, sum, element_flags) => { + grovedbg_types::Element::CountSumTree { + root_key, + count, + sum, + element_flags, + } + } } } @@ -628,8 +656,17 @@ fn node_to_update( right_merk_hash, feature_type: match feature_type { TreeFeatureType::BasicMerkNode => grovedbg_types::TreeFeatureType::BasicMerkNode, - TreeFeatureType::SummedMerkNode(x) => { - grovedbg_types::TreeFeatureType::SummedMerkNode(x) + TreeFeatureType::SummedMerkNode(sum) => { + grovedbg_types::TreeFeatureType::SummedMerkNode(sum) + } + TreeFeatureType::BigSummedMerkNode(sum) => { + grovedbg_types::TreeFeatureType::BigSummedMerkNode(sum) + } + TreeFeatureType::CountedMerkNode(count) => { + grovedbg_types::TreeFeatureType::CountedMerkNode(count) + } + TreeFeatureType::CountedSummedMerkNode(count, sum) => { + grovedbg_types::TreeFeatureType::CountedSummedMerkNode(count, sum) } }, value_hash, diff --git a/grovedb/src/element/constructor.rs b/grovedb/src/element/constructor.rs index 1d5049cd..a6bf00bb 100644 --- a/grovedb/src/element/constructor.rs +++ b/grovedb/src/element/constructor.rs @@ -1,6 +1,8 @@ //! Constructor //! Functions for setting an element's type +#[cfg(feature = "minimal")] +use crate::element::{BigSumValue, CountValue}; #[cfg(feature = "minimal")] use crate::{ element::{MaxReferenceHop, SumValue}, @@ -28,12 +30,48 @@ impl Element { Element::new_sum_tree(Default::default()) } + #[cfg(feature = "minimal")] + /// Set element to default empty big sum tree without flags + pub fn empty_big_sum_tree() -> Self { + Element::new_big_sum_tree(Default::default()) + } + + #[cfg(feature = "minimal")] + /// Set element to default empty count tree without flags + pub fn empty_count_tree() -> Self { + Element::new_count_tree(Default::default()) + } + + #[cfg(feature = "minimal")] + /// Set element to default empty count sum tree without flags + pub fn empty_count_sum_tree() -> Self { + Element::new_count_sum_tree(Default::default()) + } + #[cfg(feature = "minimal")] /// Set element to default empty sum tree with flags pub fn empty_sum_tree_with_flags(flags: Option) -> Self { Element::new_sum_tree_with_flags(Default::default(), flags) } + #[cfg(feature = "minimal")] + /// Set element to default empty sum tree with flags + pub fn empty_big_sum_tree_with_flags(flags: Option) -> Self { + Element::new_big_sum_tree_with_flags(Default::default(), flags) + } + + #[cfg(feature = "minimal")] + /// Set element to default empty count tree with flags + pub fn empty_count_tree_with_flags(flags: Option) -> Self { + Element::new_count_tree_with_flags(Default::default(), flags) + } + + #[cfg(feature = "minimal")] + /// Set element to default empty count sum tree with flags + pub fn empty_count_sum_tree_with_flags(flags: Option) -> Self { + Element::new_count_sum_tree_with_flags(Default::default(), flags) + } + #[cfg(feature = "minimal")] /// Set element to an item without flags pub fn new_item(item_value: Vec) -> Self { @@ -131,4 +169,80 @@ impl Element { ) -> Self { Element::SumTree(maybe_root_key, sum_value, flags) } + + #[cfg(feature = "minimal")] + /// Set element to a big sum tree without flags + pub fn new_big_sum_tree(maybe_root_key: Option>) -> Self { + Element::BigSumTree(maybe_root_key, 0, None) + } + + #[cfg(feature = "minimal")] + /// Set element to a big sum tree with flags + pub fn new_big_sum_tree_with_flags( + maybe_root_key: Option>, + flags: Option, + ) -> Self { + Element::BigSumTree(maybe_root_key, 0, flags) + } + + #[cfg(feature = "minimal")] + /// Set element to a big sum tree with flags and sum value + pub fn new_big_sum_tree_with_flags_and_sum_value( + maybe_root_key: Option>, + big_sum_value: BigSumValue, + flags: Option, + ) -> Self { + Element::BigSumTree(maybe_root_key, big_sum_value, flags) + } + + #[cfg(feature = "minimal")] + /// Set element to a count tree without flags + pub fn new_count_tree(maybe_root_key: Option>) -> Self { + Element::CountTree(maybe_root_key, 0, None) + } + + #[cfg(feature = "minimal")] + /// Set element to a count tree with flags + pub fn new_count_tree_with_flags( + maybe_root_key: Option>, + flags: Option, + ) -> Self { + Element::CountTree(maybe_root_key, 0, flags) + } + + #[cfg(feature = "minimal")] + /// Set element to a count tree with flags and sum value + pub fn new_count_tree_with_flags_and_count_value( + maybe_root_key: Option>, + count_value: CountValue, + flags: Option, + ) -> Self { + Element::CountTree(maybe_root_key, count_value, flags) + } + + #[cfg(feature = "minimal")] + /// Set element to a count sum tree without flags + pub fn new_count_sum_tree(maybe_root_key: Option>) -> Self { + Element::CountSumTree(maybe_root_key, 0, 0, None) + } + + #[cfg(feature = "minimal")] + /// Set element to a count sum tree with flags + pub fn new_count_sum_tree_with_flags( + maybe_root_key: Option>, + flags: Option, + ) -> Self { + Element::CountSumTree(maybe_root_key, 0, 0, flags) + } + + #[cfg(feature = "minimal")] + /// Set element to a count sum tree with flags and sum value + pub fn new_count_sum_tree_with_flags_and_sum_and_count_value( + maybe_root_key: Option>, + count_value: CountValue, + sum_value: SumValue, + flags: Option, + ) -> Self { + Element::CountSumTree(maybe_root_key, count_value, sum_value, flags) + } } diff --git a/grovedb/src/element/delete.rs b/grovedb/src/element/delete.rs index 8c9b3511..17095d72 100644 --- a/grovedb/src/element/delete.rs +++ b/grovedb/src/element/delete.rs @@ -6,6 +6,8 @@ use grovedb_costs::OperationCost; #[cfg(feature = "minimal")] use grovedb_costs::{storage_cost::removal::StorageRemovedBytes, CostResult, CostsExt}; #[cfg(feature = "minimal")] +use grovedb_merk::tree_type::TreeType; +#[cfg(feature = "minimal")] use grovedb_merk::{BatchEntry, Error as MerkError, Merk, MerkOptions, Op}; #[cfg(feature = "minimal")] use grovedb_storage::StorageContext; @@ -27,25 +29,37 @@ impl Element { key: K, merk_options: Option, is_layered: bool, - is_sum: bool, + in_tree_type: TreeType, grove_version: &GroveVersion, ) -> CostResult<(), Error> { check_grovedb_v0_with_cost!("delete", grove_version.grovedb_versions.element.delete); - let op = match (is_sum, is_layered) { - (true, true) => Op::DeleteLayeredMaybeSpecialized, - (true, false) => Op::DeleteMaybeSpecialized, - (false, true) => Op::DeleteLayered, - (false, false) => Op::Delete, + let op = match (in_tree_type, is_layered) { + (TreeType::NormalTree, true) => Op::DeleteLayered, + (TreeType::NormalTree, false) => Op::Delete, + (TreeType::SumTree, true) + | (TreeType::BigSumTree, true) + | (TreeType::CountTree, true) + | (TreeType::CountSumTree, true) => Op::DeleteLayeredMaybeSpecialized, + (TreeType::SumTree, false) + | (TreeType::BigSumTree, false) + | (TreeType::CountTree, false) + | (TreeType::CountSumTree, false) => Op::DeleteMaybeSpecialized, }; let batch = [(key, op)]; - let uses_sum_nodes = merk.is_sum_tree; + // todo not sure we get it again, we need to see if this is necessary + let tree_type = merk.tree_type; merk.apply_with_specialized_costs::<_, Vec>( &batch, &[], merk_options, &|key, value| { - Self::specialized_costs_for_key_value(key, value, uses_sum_nodes, grove_version) - .map_err(|e| MerkError::ClientCorruptionError(e.to_string())) + Self::specialized_costs_for_key_value( + key, + value, + tree_type.inner_node_type(), + grove_version, + ) + .map_err(|e| MerkError::ClientCorruptionError(e.to_string())) }, Some(&Element::value_defined_cost_for_serialized_value), grove_version, @@ -60,7 +74,7 @@ impl Element { key: K, merk_options: Option, is_layered: bool, - is_in_sum_tree: bool, + in_tree_type: TreeType, sectioned_removal: &mut impl FnMut( &Vec, u32, @@ -78,21 +92,33 @@ impl Element { .element .delete_with_sectioned_removal_bytes ); - let op = match (is_in_sum_tree, is_layered) { - (true, true) => Op::DeleteLayeredMaybeSpecialized, - (true, false) => Op::DeleteMaybeSpecialized, - (false, true) => Op::DeleteLayered, - (false, false) => Op::Delete, + let op = match (in_tree_type, is_layered) { + (TreeType::NormalTree, true) => Op::DeleteLayered, + (TreeType::NormalTree, false) => Op::Delete, + (TreeType::SumTree, true) + | (TreeType::BigSumTree, true) + | (TreeType::CountTree, true) + | (TreeType::CountSumTree, true) => Op::DeleteLayeredMaybeSpecialized, + (TreeType::SumTree, false) + | (TreeType::BigSumTree, false) + | (TreeType::CountTree, false) + | (TreeType::CountSumTree, false) => Op::DeleteMaybeSpecialized, }; let batch = [(key, op)]; - let uses_sum_nodes = merk.is_sum_tree; + // todo not sure we get it again, we need to see if this is necessary + let tree_type = merk.tree_type; merk.apply_with_costs_just_in_time_value_update::<_, Vec>( &batch, &[], merk_options, &|key, value| { - Self::specialized_costs_for_key_value(key, value, uses_sum_nodes, grove_version) - .map_err(|e| MerkError::ClientCorruptionError(e.to_string())) + Self::specialized_costs_for_key_value( + key, + value, + tree_type.inner_node_type(), + grove_version, + ) + .map_err(|e| MerkError::ClientCorruptionError(e.to_string())) }, Some(&Element::value_defined_cost_for_serialized_value), &|_, _| Ok(None), @@ -108,7 +134,7 @@ impl Element { pub fn delete_into_batch_operations>( key: K, is_layered: bool, - is_sum: bool, + in_tree_type: TreeType, batch_operations: &mut Vec>, grove_version: &GroveVersion, ) -> CostResult<(), Error> { @@ -119,11 +145,17 @@ impl Element { .element .delete_into_batch_operations ); - let op = match (is_sum, is_layered) { - (true, true) => Op::DeleteLayeredMaybeSpecialized, - (true, false) => Op::DeleteMaybeSpecialized, - (false, true) => Op::DeleteLayered, - (false, false) => Op::Delete, + let op = match (in_tree_type, is_layered) { + (TreeType::NormalTree, true) => Op::DeleteLayered, + (TreeType::NormalTree, false) => Op::Delete, + (TreeType::SumTree, true) + | (TreeType::BigSumTree, true) + | (TreeType::CountTree, true) + | (TreeType::CountSumTree, true) => Op::DeleteLayeredMaybeSpecialized, + (TreeType::SumTree, false) + | (TreeType::BigSumTree, false) + | (TreeType::CountTree, false) + | (TreeType::CountSumTree, false) => Op::DeleteMaybeSpecialized, }; let entry = (key, op); batch_operations.push(entry); diff --git a/grovedb/src/element/get.rs b/grovedb/src/element/get.rs index 3c66b049..40868c77 100644 --- a/grovedb/src/element/get.rs +++ b/grovedb/src/element/get.rs @@ -5,19 +5,24 @@ use grovedb_costs::{ cost_return_on_error, cost_return_on_error_no_add, CostResult, CostsExt, OperationCost, }; -use grovedb_merk::tree::kv::KV; #[cfg(feature = "minimal")] use grovedb_merk::Merk; #[cfg(feature = "minimal")] use grovedb_merk::{ed::Decode, tree::TreeNodeInner}; #[cfg(feature = "minimal")] +use grovedb_merk::{merk::NodeType, tree::kv::KV}; +#[cfg(feature = "minimal")] use grovedb_storage::StorageContext; use grovedb_version::{ check_grovedb_v0_with_cost, error::GroveVersionError, version::GroveVersion, }; use integer_encoding::VarInt; -use crate::element::{SUM_ITEM_COST_SIZE, SUM_TREE_COST_SIZE, TREE_COST_SIZE}; +#[cfg(feature = "minimal")] +use crate::{ + element::{CostSize, SUM_ITEM_COST_SIZE}, + operations::proof::util::path_as_slices_hex_to_ascii, +}; #[cfg(feature = "minimal")] use crate::{Element, Error, Hash}; @@ -35,13 +40,19 @@ impl Element { Self::get_optional(merk, key.as_ref(), allow_cache, grove_version).map(|result| { let value = result?; value.ok_or_else(|| { + let key_single_byte = if key.as_ref().len() == 1 { + format!("({} in decimal) ", key.as_ref().get(0).unwrap()) + } else { + String::new() + }; Error::PathKeyNotFound(format!( - "get: key \"{}\" not found in Merk that has a root key [{}] and is of type {}", + "get: key 0x{} {}not found in Merk that has a root key [{}] and is of type {}", hex::encode(key), + key_single_byte, merk.root_key() .map(hex::encode) .unwrap_or("None".to_string()), - merk.merk_type + merk.merk_type, )) }) }) @@ -118,13 +129,32 @@ impl Element { key: K, grove_version: &GroveVersion, ) -> CostResult, Error> { - check_grovedb_v0_with_cost!( - "get_optional_from_storage", - grove_version - .grovedb_versions - .element - .get_optional_from_storage - ); + match grove_version + .grovedb_versions + .element + .get_optional_from_storage + { + 0 => Self::get_optional_from_storage_v0(storage, key, grove_version), + 1 => Self::get_optional_from_storage_v1(storage, key, grove_version), + version => Err(Error::VersionError( + GroveVersionError::UnknownVersionMismatch { + method: "get_optional_from_storage".to_string(), + known_versions: vec![0, 1], + received: version, + }, + )) + .wrap_with_cost(OperationCost::default()), + } + } + + #[cfg(feature = "minimal")] + /// Get an element directly from storage under a key + /// Merk does not need to be loaded + fn get_optional_from_storage_v0<'db, K: AsRef<[u8]>, S: StorageContext<'db>>( + storage: &S, + key: K, + grove_version: &GroveVersion, + ) -> CostResult, Error> { let mut cost = OperationCost::default(); let key_ref = key.as_ref(); let node_value_opt = cost_return_on_error!( @@ -162,7 +192,7 @@ impl Element { cost.storage_loaded_bytes = KV::value_byte_cost_size_for_key_and_value_lengths( key_ref.len() as u32, value.as_ref().unwrap().len() as u32, - false, + NodeType::NormalNode, ) as u64 } Some(Element::SumItem(_, flags)) => { @@ -172,15 +202,18 @@ impl Element { flags_len + flags_len.required_space() as u32 }); let value_len = cost_size + flags_len; - cost.storage_loaded_bytes = - KV::node_value_byte_cost_size(key_ref.len() as u32, value_len, false) as u64 + cost.storage_loaded_bytes = KV::node_value_byte_cost_size( + key_ref.len() as u32, + value_len, + NodeType::NormalNode, + ) as u64 } - Some(Element::Tree(_, flags)) | Some(Element::SumTree(_, _, flags)) => { - let tree_cost_size = if element.as_ref().unwrap().is_sum_tree() { - SUM_TREE_COST_SIZE - } else { - TREE_COST_SIZE - }; + Some(Element::Tree(_, flags)) + | Some(Element::SumTree(_, _, flags)) + | Some(Element::BigSumTree(_, _, flags)) + | Some(Element::CountTree(_, _, flags)) + | Some(Element::CountSumTree(.., flags)) => { + let tree_cost_size = element.as_ref().unwrap().tree_type().unwrap().cost_size(); let flags_len = flags.as_ref().map_or(0, |flags| { let flags_len = flags.len() as u32; flags_len + flags_len.required_space() as u32 @@ -190,7 +223,7 @@ impl Element { KV::layered_value_byte_cost_size_for_key_and_value_lengths( key_ref.len() as u32, value_len, - false, + NodeType::NormalNode, ) as u64 } None => {} @@ -198,6 +231,87 @@ impl Element { Ok(element).wrap_with_cost(cost) } + #[cfg(feature = "minimal")] + /// Get an element directly from storage under a key + /// Merk does not need to be loaded + fn get_optional_from_storage_v1<'db, K: AsRef<[u8]>, S: StorageContext<'db>>( + storage: &S, + key: K, + grove_version: &GroveVersion, + ) -> CostResult, Error> { + let mut cost = OperationCost::default(); + let key_ref = key.as_ref(); + let node_value_opt = cost_return_on_error!( + &mut cost, + storage + .get(key_ref) + .map_err(|e| Error::CorruptedData(e.to_string())) + ); + let maybe_tree_inner: Option = cost_return_on_error_no_add!( + &cost, + node_value_opt + .map(|node_value| { + Decode::decode(node_value.as_slice()) + .map_err(|e| Error::CorruptedData(e.to_string())) + }) + .transpose() + ); + + let Some((value, tree_feature_type)) = + maybe_tree_inner.map(|tree_inner| tree_inner.value_as_owned_with_feature()) + else { + return Ok(None).wrap_with_cost(cost); + }; + let node_type = tree_feature_type.node_type(); + let element = cost_return_on_error_no_add!( + &cost, + Self::deserialize(value.as_slice(), grove_version).map_err(|_| { + Error::CorruptedData(String::from("unable to deserialize element")) + }) + ); + match &element { + Element::Item(..) | Element::Reference(..) => { + // while the loaded item might be a sum item, it is given for free + // as it would be very hard to know in advance + cost.storage_loaded_bytes = KV::value_byte_cost_size_for_key_and_value_lengths( + key_ref.len() as u32, + value.len() as u32, + node_type, + ) as u64 + } + Element::SumItem(_, flags) => { + let cost_size = SUM_ITEM_COST_SIZE; + let flags_len = flags.as_ref().map_or(0, |flags| { + let flags_len = flags.len() as u32; + flags_len + flags_len.required_space() as u32 + }); + let value_len = cost_size + flags_len; + cost.storage_loaded_bytes = + KV::node_value_byte_cost_size(key_ref.len() as u32, value_len, node_type) as u64 + // this is changed to sum node in v1 + } + Element::Tree(_, flags) + | Element::SumTree(_, _, flags) + | Element::BigSumTree(_, _, flags) + | Element::CountTree(_, _, flags) + | Element::CountSumTree(.., flags) => { + let tree_cost_size = element.tree_type().unwrap().cost_size(); + let flags_len = flags.as_ref().map_or(0, |flags| { + let flags_len = flags.len() as u32; + flags_len + flags_len.required_space() as u32 + }); + let value_len = tree_cost_size + flags_len; + cost.storage_loaded_bytes = + KV::layered_value_byte_cost_size_for_key_and_value_lengths( + key_ref.len() as u32, + value_len, + node_type, + ) as u64 + } + } + Ok(Some(element)).wrap_with_cost(cost) + } + #[cfg(feature = "minimal")] /// Get an element from Merk under a key; path should be resolved and proper /// Merk should be loaded by this moment @@ -208,6 +322,8 @@ impl Element { allow_cache: bool, grove_version: &GroveVersion, ) -> CostResult { + use crate::error::GroveDbErrorExt; + check_grovedb_v0_with_cost!( "get_with_absolute_refs", grove_version @@ -220,6 +336,7 @@ impl Element { let element = cost_return_on_error!( &mut cost, Self::get(merk, key.as_ref(), allow_cache, grove_version) + .add_context(format!("path is {}", path_as_slices_hex_to_ascii(path))) ); let absolute_element = cost_return_on_error_no_add!( @@ -262,6 +379,7 @@ impl Element { #[cfg(feature = "minimal")] #[cfg(test)] mod tests { + use grovedb_merk::tree_type::TreeType; use grovedb_path::SubtreePath; use grovedb_storage::{rocksdb_storage::test_utils::TempStorage, Storage, StorageBatch}; @@ -277,7 +395,7 @@ mod tests { .unwrap(); let mut merk = Merk::open_base( ctx, - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -302,7 +420,7 @@ mod tests { .unwrap(); let mut merk = Merk::open_base( ctx, - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) diff --git a/grovedb/src/element/helpers.rs b/grovedb/src/element/helpers.rs index af74c6f8..d5cf0321 100644 --- a/grovedb/src/element/helpers.rs +++ b/grovedb/src/element/helpers.rs @@ -1,33 +1,40 @@ //! Helpers //! Implements helper functions in Element -#[cfg(feature = "minimal")] -use grovedb_merk::tree::kv::{ - ValueDefinedCostType, - ValueDefinedCostType::{LayeredValueDefinedCost, SpecializedValueDefinedCost}, -}; +#[cfg(any(feature = "minimal", feature = "verify"))] +use grovedb_merk::tree_type::{MaybeTree, TreeType}; #[cfg(feature = "minimal")] use grovedb_merk::{ - tree::{kv::KV, TreeNode}, + merk::NodeType, + tree::{ + kv::{ + ValueDefinedCostType, + ValueDefinedCostType::{LayeredValueDefinedCost, SpecializedValueDefinedCost}, + KV, + }, + TreeNode, + }, TreeFeatureType, - TreeFeatureType::{BasicMerkNode, SummedMerkNode}, + TreeFeatureType::{ + BasicMerkNode, BigSummedMerkNode, CountedMerkNode, CountedSummedMerkNode, SummedMerkNode, + }, }; #[cfg(feature = "minimal")] use grovedb_version::{check_grovedb_v0, error::GroveVersionError, version::GroveVersion}; #[cfg(feature = "minimal")] use integer_encoding::VarInt; +#[cfg(feature = "minimal")] +use crate::element::{ + BIG_SUM_TREE_COST_SIZE, COUNT_SUM_TREE_COST_SIZE, COUNT_TREE_COST_SIZE, SUM_ITEM_COST_SIZE, + SUM_TREE_COST_SIZE, TREE_COST_SIZE, +}; #[cfg(feature = "minimal")] use crate::reference_path::path_from_reference_path_type; #[cfg(any(feature = "minimal", feature = "verify"))] use crate::reference_path::ReferencePathType; -#[cfg(feature = "minimal")] -use crate::{ - element::{SUM_ITEM_COST_SIZE, SUM_TREE_COST_SIZE, TREE_COST_SIZE}, - ElementFlags, -}; #[cfg(any(feature = "minimal", feature = "verify"))] -use crate::{Element, Error}; +use crate::{Element, ElementFlags, Error}; impl Element { #[cfg(any(feature = "minimal", feature = "verify"))] @@ -40,6 +47,41 @@ impl Element { } } + #[cfg(any(feature = "minimal", feature = "verify"))] + /// Decoded the integer value in the CountTree element type, returns 1 for + /// everything else + pub fn count_value_or_default(&self) -> u64 { + match self { + Element::CountTree(_, count_value, _) => *count_value, + _ => 1, + } + } + + #[cfg(any(feature = "minimal", feature = "verify"))] + /// Decoded the integer value in the CountTree element type, returns 1 for + /// everything else + pub fn count_sum_value_or_default(&self) -> (u64, i64) { + match self { + Element::SumItem(sum_value, _) | Element::SumTree(_, sum_value, _) => (1, *sum_value), + Element::CountTree(_, count_value, _) => (*count_value, 0), + Element::CountSumTree(_, count_value, sum_value, _) => (*count_value, *sum_value), + _ => (1, 0), + } + } + + #[cfg(any(feature = "minimal", feature = "verify"))] + /// Decoded the integer value in the SumItem element type, returns 0 for + /// everything else + pub fn big_sum_value_or_default(&self) -> i128 { + match self { + Element::SumItem(sum_value, _) | Element::SumTree(_, sum_value, _) => { + *sum_value as i128 + } + Element::BigSumTree(_, sum_value, _) => *sum_value, + _ => 0, + } + } + #[cfg(any(feature = "minimal", feature = "verify"))] /// Decoded the integer value in the SumItem element type pub fn as_sum_item_value(&self) -> Result { @@ -109,6 +151,79 @@ impl Element { matches!(self, Element::SumTree(..)) } + #[cfg(any(feature = "minimal", feature = "verify"))] + /// Check if the element is a tree and return the root_tree info and tree + /// type + pub fn root_key_and_tree_type_owned(self) -> Option<(Option>, TreeType)> { + match self { + Element::Tree(root_key, _) => Some((root_key, TreeType::NormalTree)), + Element::SumTree(root_key, ..) => Some((root_key, TreeType::SumTree)), + Element::BigSumTree(root_key, ..) => Some((root_key, TreeType::BigSumTree)), + Element::CountTree(root_key, ..) => Some((root_key, TreeType::CountTree)), + Element::CountSumTree(root_key, ..) => Some((root_key, TreeType::CountSumTree)), + _ => None, + } + } + + #[cfg(any(feature = "minimal", feature = "verify"))] + /// Check if the element is a tree and return the root_tree info and the + /// tree type + pub fn root_key_and_tree_type(&self) -> Option<(&Option>, TreeType)> { + match self { + Element::Tree(root_key, _) => Some((root_key, TreeType::NormalTree)), + Element::SumTree(root_key, ..) => Some((root_key, TreeType::SumTree)), + Element::BigSumTree(root_key, ..) => Some((root_key, TreeType::BigSumTree)), + Element::CountTree(root_key, ..) => Some((root_key, TreeType::CountTree)), + Element::CountSumTree(root_key, ..) => Some((root_key, TreeType::CountSumTree)), + _ => None, + } + } + + #[cfg(any(feature = "minimal", feature = "verify"))] + /// Check if the element is a tree and return the flags and the tree type + pub fn tree_flags_and_type(&self) -> Option<(&Option, TreeType)> { + match self { + Element::Tree(_, flags) => Some((flags, TreeType::NormalTree)), + Element::SumTree(_, _, flags) => Some((flags, TreeType::SumTree)), + Element::BigSumTree(_, _, flags) => Some((flags, TreeType::BigSumTree)), + Element::CountTree(_, _, flags) => Some((flags, TreeType::CountTree)), + Element::CountSumTree(.., flags) => Some((flags, TreeType::CountSumTree)), + _ => None, + } + } + + #[cfg(any(feature = "minimal", feature = "verify"))] + /// Check if the element is a tree and return the tree type + pub fn tree_type(&self) -> Option { + match self { + Element::Tree(..) => Some(TreeType::NormalTree), + Element::SumTree(..) => Some(TreeType::SumTree), + Element::BigSumTree(..) => Some(TreeType::BigSumTree), + Element::CountTree(..) => Some(TreeType::CountTree), + Element::CountSumTree(..) => Some(TreeType::CountSumTree), + _ => None, + } + } + + #[cfg(any(feature = "minimal", feature = "verify"))] + /// Check if the element is a tree and return the tree type + pub fn maybe_tree_type(&self) -> MaybeTree { + match self { + Element::Tree(..) => MaybeTree::Tree(TreeType::NormalTree), + Element::SumTree(..) => MaybeTree::Tree(TreeType::SumTree), + Element::BigSumTree(..) => MaybeTree::Tree(TreeType::BigSumTree), + Element::CountTree(..) => MaybeTree::Tree(TreeType::CountTree), + Element::CountSumTree(..) => MaybeTree::Tree(TreeType::CountSumTree), + _ => MaybeTree::NotTree, + } + } + + #[cfg(any(feature = "minimal", feature = "verify"))] + /// Check if the element is a big sum tree + pub fn is_big_sum_tree(&self) -> bool { + matches!(self, Element::BigSumTree(..)) + } + #[cfg(any(feature = "minimal", feature = "verify"))] /// Check if the element is a tree but not a sum tree pub fn is_basic_tree(&self) -> bool { @@ -118,7 +233,14 @@ impl Element { #[cfg(any(feature = "minimal", feature = "verify"))] /// Check if the element is a tree pub fn is_any_tree(&self) -> bool { - matches!(self, Element::SumTree(..) | Element::Tree(..)) + matches!( + self, + Element::SumTree(..) + | Element::Tree(..) + | Element::BigSumTree(..) + | Element::CountTree(..) + | Element::CountSumTree(..) + ) } #[cfg(any(feature = "minimal", feature = "verify"))] @@ -147,10 +269,16 @@ impl Element { #[cfg(feature = "minimal")] /// Get the tree feature type - pub fn get_feature_type(&self, parent_is_sum_tree: bool) -> Result { - match parent_is_sum_tree { - true => Ok(SummedMerkNode(self.sum_value_or_default())), - false => Ok(BasicMerkNode), + pub fn get_feature_type(&self, parent_tree_type: TreeType) -> Result { + match parent_tree_type { + TreeType::NormalTree => Ok(BasicMerkNode), + TreeType::SumTree => Ok(SummedMerkNode(self.sum_value_or_default())), + TreeType::BigSumTree => Ok(BigSummedMerkNode(self.big_sum_value_or_default())), + TreeType::CountTree => Ok(CountedMerkNode(self.count_value_or_default())), + TreeType::CountSumTree => { + let v = self.count_sum_value_or_default(); + Ok(CountedSummedMerkNode(v.0, v.1)) + } } } @@ -162,7 +290,10 @@ impl Element { | Element::Item(_, flags) | Element::Reference(_, _, flags) | Element::SumTree(.., flags) - | Element::SumItem(_, flags) => flags, + | Element::BigSumTree(.., flags) + | Element::CountTree(.., flags) + | Element::SumItem(_, flags) + | Element::CountSumTree(.., flags) => flags, } } @@ -174,7 +305,10 @@ impl Element { | Element::Item(_, flags) | Element::Reference(_, _, flags) | Element::SumTree(.., flags) - | Element::SumItem(_, flags) => flags, + | Element::BigSumTree(.., flags) + | Element::CountTree(.., flags) + | Element::SumItem(_, flags) + | Element::CountSumTree(.., flags) => flags, } } @@ -186,7 +320,10 @@ impl Element { | Element::Item(_, flags) | Element::Reference(_, _, flags) | Element::SumTree(.., flags) - | Element::SumItem(_, flags) => flags, + | Element::BigSumTree(.., flags) + | Element::CountTree(.., flags) + | Element::SumItem(_, flags) + | Element::CountSumTree(.., flags) => flags, } } @@ -198,7 +335,10 @@ impl Element { | Element::Item(_, flags) | Element::Reference(_, _, flags) | Element::SumTree(.., flags) - | Element::SumItem(_, flags) => *flags = new_flags, + | Element::BigSumTree(.., flags) + | Element::CountTree(.., flags) + | Element::SumItem(_, flags) + | Element::CountSumTree(.., flags) => *flags = new_flags, } } @@ -252,7 +392,7 @@ impl Element { pub fn specialized_costs_for_key_value( key: &Vec, value: &[u8], - is_sum_node: bool, + node_type: NodeType, grove_version: &GroveVersion, ) -> Result { check_grovedb_v0!( @@ -273,9 +413,7 @@ impl Element { let value_len = TREE_COST_SIZE + flags_len; let key_len = key.len() as u32; KV::layered_value_byte_cost_size_for_key_and_value_lengths( - key_len, - value_len, - is_sum_node, + key_len, value_len, node_type, ) } Element::SumTree(_, _sum_value, flags) => { @@ -286,9 +424,40 @@ impl Element { let value_len = SUM_TREE_COST_SIZE + flags_len; let key_len = key.len() as u32; KV::layered_value_byte_cost_size_for_key_and_value_lengths( - key_len, - value_len, - is_sum_node, + key_len, value_len, node_type, + ) + } + Element::BigSumTree(_, _sum_value, flags) => { + let flags_len = flags.map_or(0, |flags| { + let flags_len = flags.len() as u32; + flags_len + flags_len.required_space() as u32 + }); + let value_len = BIG_SUM_TREE_COST_SIZE + flags_len; + let key_len = key.len() as u32; + KV::layered_value_byte_cost_size_for_key_and_value_lengths( + key_len, value_len, node_type, + ) + } + Element::CountTree(_, _count_value, flags) => { + let flags_len = flags.map_or(0, |flags| { + let flags_len = flags.len() as u32; + flags_len + flags_len.required_space() as u32 + }); + let value_len = COUNT_TREE_COST_SIZE + flags_len; + let key_len = key.len() as u32; + KV::layered_value_byte_cost_size_for_key_and_value_lengths( + key_len, value_len, node_type, + ) + } + Element::CountSumTree(.., flags) => { + let flags_len = flags.map_or(0, |flags| { + let flags_len = flags.len() as u32; + flags_len + flags_len.required_space() as u32 + }); + let value_len = COUNT_SUM_TREE_COST_SIZE + flags_len; + let key_len = key.len() as u32; + KV::layered_value_byte_cost_size_for_key_and_value_lengths( + key_len, value_len, node_type, ) } Element::SumItem(.., flags) => { @@ -298,9 +467,9 @@ impl Element { }); let value_len = SUM_ITEM_COST_SIZE + flags_len; let key_len = key.len() as u32; - KV::node_value_byte_cost_size(key_len, value_len, is_sum_node) + KV::node_value_byte_cost_size(key_len, value_len, node_type) } - _ => KV::node_value_byte_cost_size(key.len() as u32, value.len() as u32, is_sum_node), + _ => KV::node_value_byte_cost_size(key.len() as u32, value.len() as u32, node_type), }; Ok(cost) } @@ -315,7 +484,10 @@ impl Element { match self { Element::Tree(..) => Ok(TREE_COST_SIZE), Element::SumTree(..) => Ok(SUM_TREE_COST_SIZE), + Element::BigSumTree(..) => Ok(BIG_SUM_TREE_COST_SIZE), Element::SumItem(..) => Ok(SUM_ITEM_COST_SIZE), + Element::CountTree(..) => Ok(COUNT_TREE_COST_SIZE), + Element::CountSumTree(..) => Ok(COUNT_SUM_TREE_COST_SIZE), _ => Err(Error::CorruptedCodeExecution( "trying to get tree cost from non tree element", )), @@ -337,6 +509,9 @@ impl Element { match self { Element::Tree(..) => Some(LayeredValueDefinedCost(cost)), Element::SumTree(..) => Some(LayeredValueDefinedCost(cost)), + Element::BigSumTree(..) => Some(LayeredValueDefinedCost(cost)), + Element::CountTree(..) => Some(LayeredValueDefinedCost(cost)), + Element::CountSumTree(..) => Some(LayeredValueDefinedCost(cost)), Element::SumItem(..) => Some(SpecializedValueDefinedCost(cost)), _ => None, } diff --git a/grovedb/src/element/insert.rs b/grovedb/src/element/insert.rs index 5b47acc4..942a6fd5 100644 --- a/grovedb/src/element/insert.rs +++ b/grovedb/src/element/insert.rs @@ -32,13 +32,13 @@ impl Element { let serialized = cost_return_on_error_default!(self.serialize(grove_version)); - if !merk.is_sum_tree && self.is_sum_item() { + if !merk.tree_type.allows_sum_item() && self.is_sum_item() { return Err(Error::InvalidInput("cannot add sum item to non sum tree")) .wrap_with_cost(Default::default()); } let merk_feature_type = - cost_return_on_error_default!(self.get_feature_type(merk.is_sum_tree)); + cost_return_on_error_default!(self.get_feature_type(merk.tree_type)); let batch_operations = if matches!(self, SumItem(..)) { let value_cost = cost_return_on_error_default!(self.get_specialized_cost(grove_version)); @@ -55,15 +55,20 @@ impl Element { } else { [(key, Op::Put(serialized, merk_feature_type))] }; - let uses_sum_nodes = merk.is_sum_tree; + let tree_type = merk.tree_type; merk.apply_with_specialized_costs::<_, Vec>( &batch_operations, &[], options, &|key, value| { // it is possible that a normal item was being replaced with a - Self::specialized_costs_for_key_value(key, value, uses_sum_nodes, grove_version) - .map_err(|e| MerkError::ClientCorruptionError(e.to_string())) + Self::specialized_costs_for_key_value( + key, + value, + tree_type.inner_node_type(), + grove_version, + ) + .map_err(|e| MerkError::ClientCorruptionError(e.to_string())) }, Some(&Element::value_defined_cost_for_serialized_value), grove_version, @@ -306,7 +311,7 @@ impl Element { let mut cost = OperationCost::default(); let merk_feature_type = cost_return_on_error!( &mut cost, - self.get_feature_type(merk.is_sum_tree) + self.get_feature_type(merk.tree_type) .wrap_with_cost(OperationCost::default()) ); @@ -314,14 +319,19 @@ impl Element { key, Op::PutCombinedReference(serialized, referenced_value, merk_feature_type), )]; - let uses_sum_nodes = merk.is_sum_tree; + let tree_type = merk.tree_type; merk.apply_with_specialized_costs::<_, Vec>( &batch_operations, &[], options, &|key, value| { - Self::specialized_costs_for_key_value(key, value, uses_sum_nodes, grove_version) - .map_err(|e| MerkError::ClientCorruptionError(e.to_string())) + Self::specialized_costs_for_key_value( + key, + value, + tree_type.inner_node_type(), + grove_version, + ) + .map_err(|e| MerkError::ClientCorruptionError(e.to_string())) }, Some(&Element::value_defined_cost_for_serialized_value), grove_version, @@ -387,7 +397,7 @@ impl Element { let cost = OperationCost::default(); let merk_feature_type = - cost_return_on_error_no_add!(&cost, self.get_feature_type(merk.is_sum_tree)); + cost_return_on_error_no_add!(&cost, self.get_feature_type(merk.tree_type)); let tree_cost = cost_return_on_error_no_add!(&cost, self.get_specialized_cost(grove_version)); @@ -401,14 +411,19 @@ impl Element { key, Op::PutLayeredReference(serialized, cost, subtree_root_hash, merk_feature_type), )]; - let uses_sum_nodes = merk.is_sum_tree; + let tree_type = merk.tree_type; merk.apply_with_specialized_costs::<_, Vec>( &batch_operations, &[], options, &|key, value| { - Self::specialized_costs_for_key_value(key, value, uses_sum_nodes, grove_version) - .map_err(|e| MerkError::ClientCorruptionError(e.to_string())) + Self::specialized_costs_for_key_value( + key, + value, + tree_type.inner_node_type(), + grove_version, + ) + .map_err(|e| MerkError::ClientCorruptionError(e.to_string())) }, Some(&Element::value_defined_cost_for_serialized_value), grove_version, diff --git a/grovedb/src/element/mod.rs b/grovedb/src/element/mod.rs index c8ecbbc8..06978897 100644 --- a/grovedb/src/element/mod.rs +++ b/grovedb/src/element/mod.rs @@ -24,10 +24,16 @@ mod serialize; use std::fmt; use bincode::{Decode, Encode}; -#[cfg(any(feature = "minimal", feature = "verify"))] +#[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::{LAYER_COST_SIZE, SUM_LAYER_COST_SIZE}; +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; @@ -49,7 +55,7 @@ pub type MaxReferenceHop = Option; #[cfg(feature = "minimal")] /// The cost of a tree pub const TREE_COST_SIZE: u32 = LAYER_COST_SIZE; // 3 -#[cfg(any(feature = "minimal", feature = "verify"))] +#[cfg(feature = "minimal")] /// The cost of a sum item /// /// It is 11 because we have 9 bytes for the sum value @@ -60,10 +66,48 @@ pub const SUM_ITEM_COST_SIZE: u32 = SUM_VALUE_EXTRA_COST + 2; // 11 /// The cost of a sum tree pub const SUM_TREE_COST_SIZE: u32 = SUM_LAYER_COST_SIZE; // 12 +#[cfg(feature = "minimal")] +/// The cost of a big sum tree +pub const BIG_SUM_TREE_COST_SIZE: u32 = BIG_SUM_LAYER_COST_SIZE; // 19 + +#[cfg(feature = "minimal")] +/// The cost of a count tree +pub const COUNT_TREE_COST_SIZE: u32 = SUM_LAYER_COST_SIZE; // 12 + +#[cfg(feature = "minimal")] +/// The cost of a count tree +pub const COUNT_SUM_TREE_COST_SIZE: u32 = SUM_AND_COUNT_LAYER_COST_SIZE; // 21 + #[cfg(any(feature = "minimal", feature = "verify"))] /// int 64 sum value pub type SumValue = i64; +#[cfg(any(feature = "minimal", feature = "verify"))] +/// int 128 sum value +pub type BigSumValue = i128; + +#[cfg(any(feature = "minimal", feature = "verify"))] +/// int 64 count value +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"))] /// Variants of GroveDB stored entities /// @@ -85,6 +129,15 @@ pub enum Element { /// Same as Element::Tree but underlying Merk sums value of it's summable /// nodes SumTree(Option>, SumValue, Option), + /// Same as Element::Tree but underlying Merk sums value of it's summable + /// nodes in big form i128 + /// The big sum tree is valuable if you have a big sum tree of sum trees + BigSumTree(Option>, BigSumValue, Option), + /// Same as Element::Tree but underlying Merk counts value of its countable + /// nodes + CountTree(Option>, CountValue, Option), + /// Combines Element::SumTree and Element::CountTree + CountSumTree(Option>, CountValue, SumValue, Option), } impl fmt::Display for Element { @@ -142,6 +195,40 @@ impl fmt::Display for Element { .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)) + ) + } } } } @@ -154,6 +241,9 @@ impl Element { 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", } } diff --git a/grovedb/src/element/query.rs b/grovedb/src/element/query.rs index f1975aad..68e57056 100644 --- a/grovedb/src/element/query.rs +++ b/grovedb/src/element/query.rs @@ -15,6 +15,8 @@ use grovedb_merk::proofs::query::SubqueryBranch; #[cfg(feature = "minimal")] use grovedb_merk::proofs::Query; #[cfg(feature = "minimal")] +use grovedb_merk::tree_type::TreeType; +#[cfg(feature = "minimal")] use grovedb_path::SubtreePath; #[cfg(feature = "minimal")] use grovedb_storage::{rocksdb_storage::RocksDbStorage, RawIterator, StorageContext}; @@ -26,6 +28,8 @@ use grovedb_version::{ #[cfg(feature = "minimal")] use crate::operations::proof::util::hex_to_ascii; #[cfg(any(feature = "minimal", feature = "verify"))] +use crate::operations::proof::util::path_as_slices_hex_to_ascii; +#[cfg(any(feature = "minimal", feature = "verify"))] use crate::Element; #[cfg(feature = "minimal")] use crate::{ @@ -741,6 +745,8 @@ impl Element { add_element_function: fn(PathQueryPushArgs, &GroveVersion) -> CostResult<(), Error>, grove_version: &GroveVersion, ) -> CostResult<(), Error> { + use crate::error::GroveDbErrorExt; + check_grovedb_v0_with_cost!( "query_item", grove_version.grovedb_versions.element.query_item @@ -763,6 +769,7 @@ impl Element { grove_version, { Element::get(&subtree, key, query_options.allow_cache, grove_version) + .add_context(format!("path is {}", path_as_slices_hex_to_ascii(path))) .unwrap_add_cost(&mut cost) } ); diff --git a/grovedb/src/error.rs b/grovedb/src/error.rs index 4618e6ac..2ab1937f 100644 --- a/grovedb/src/error.rs +++ b/grovedb/src/error.rs @@ -2,6 +2,8 @@ use std::convert::Infallible; +use grovedb_costs::CostResult; + /// GroveDB Errors #[cfg(any(feature = "minimal", feature = "verify"))] #[derive(Debug, thiserror::Error)] @@ -158,6 +160,50 @@ pub enum Error { CyclicError(&'static str), } +impl Error { + pub fn add_context(&mut self, append: impl AsRef) { + match self { + Self::MissingReference(s) + | Self::InternalError(s) + | Self::InvalidProof(s) + | Self::PathKeyNotFound(s) + | Self::PathNotFound(s) + | Self::PathParentLayerNotFound(s) + | Self::CorruptedReferencePathKeyNotFound(s) + | Self::CorruptedReferencePathNotFound(s) + | Self::CorruptedReferencePathParentLayerNotFound(s) + | Self::InvalidParentLayerPath(s) + | Self::InvalidPath(s) + | Self::CorruptedPath(s) + | Self::CorruptedData(s) + | Self::CorruptedStorage(s) + | Self::DeleteUpTreeStopHeightMoreThanInitialPathSize(s) + | Self::JustInTimeElementFlagsClientError(s) + | Self::SplitRemovalBytesClientError(s) + | Self::ClientReturnedNonClientError(s) + | Self::PathNotFoundInCacheForEstimatedCosts(s) + | Self::NotSupported(s) => { + s.push_str(", "); + s.push_str(append.as_ref()); + } + _ => {} + } + } +} + +pub trait GroveDbErrorExt { + fn add_context(self, append: impl AsRef) -> Self; +} + +impl GroveDbErrorExt for CostResult { + fn add_context(self, append: impl AsRef) -> Self { + self.map_err(|mut e| { + e.add_context(append.as_ref()); + e + }) + } +} + impl From for Error { fn from(_value: Infallible) -> Self { Self::Infallible diff --git a/grovedb/src/estimated_costs/average_case_costs.rs b/grovedb/src/estimated_costs/average_case_costs.rs index 74cfe807..e779d9b8 100644 --- a/grovedb/src/estimated_costs/average_case_costs.rs +++ b/grovedb/src/estimated_costs/average_case_costs.rs @@ -13,6 +13,7 @@ use grovedb_merk::{ }, }, tree::TreeNode, + tree_type::TreeType, HASH_LENGTH, }; use grovedb_storage::{worst_case_costs::WorstKeyLength, Storage}; @@ -23,7 +24,7 @@ use integer_encoding::VarInt; use crate::{ batch::{key_info::KeyInfo, KeyInfoPath}, - element::{SUM_ITEM_COST_SIZE, SUM_TREE_COST_SIZE, TREE_COST_SIZE}, + element::{CostSize, SUM_ITEM_COST_SIZE}, Element, ElementFlags, Error, GroveDb, }; @@ -33,7 +34,7 @@ impl GroveDb { cost: &mut OperationCost, path: &KeyInfoPath, merk_should_be_empty: bool, - is_sum_tree: bool, + in_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result<(), Error> { check_grovedb_v0!( @@ -56,7 +57,7 @@ impl GroveDb { cost.storage_loaded_bytes += TreeNode::average_case_encoded_tree_size( key.max_length() as u32, HASH_LENGTH as u32, - is_sum_tree, + in_tree_type.inner_node_type(), ) as u64; } } @@ -69,19 +70,51 @@ impl GroveDb { pub(crate) fn average_case_merk_replace_tree( key: &KeyInfo, estimated_layer_information: &EstimatedLayerInformation, - _is_sum_tree: bool, + replacing_tree_type: TreeType, propagate: bool, grove_version: &GroveVersion, ) -> CostResult<(), Error> { - check_grovedb_v0_with_cost!( - "average_case_merk_replace_tree", - grove_version - .grovedb_versions - .operations - .average_case - .average_case_merk_replace_tree - ); + match grove_version + .grovedb_versions + .operations + .average_case + .average_case_merk_replace_tree + { + 0 => Self::average_case_merk_replace_tree_v0( + key, + estimated_layer_information, + replacing_tree_type, + propagate, + grove_version, + ), + 1 => Self::average_case_merk_replace_tree_v1( + key, + estimated_layer_information, + replacing_tree_type, + propagate, + grove_version, + ), + version => Err(Error::VersionError( + GroveVersionError::UnknownVersionMismatch { + method: "average_case_merk_replace_tree".to_string(), + known_versions: vec![0, 1], + received: version, + }, + )) + .wrap_with_cost(OperationCost::default()), + } + } + /// Add average case for insertion into merk + fn average_case_merk_replace_tree_v0( + key: &KeyInfo, + estimated_layer_information: &EstimatedLayerInformation, + _replacing_tree_type: TreeType, + propagate: bool, + grove_version: &GroveVersion, + ) -> CostResult<(), Error> { + // In v0 we used the estimated layer information tree type (which is the parent) + // in order to figure out the cost let mut cost = OperationCost::default(); let key_len = key.max_length() as u32; let flags_size = cost_return_on_error_no_add!( @@ -93,20 +126,52 @@ impl GroveDb { ) .map(|f| f + f.required_space() as u32) .unwrap_or_default(); - let tree_cost_size = if estimated_layer_information.is_sum_tree { - SUM_TREE_COST_SIZE + let tree_cost_size = estimated_layer_information.tree_type.cost_size(); // this was wrong + let layer_extra_size = tree_cost_size + flags_size; + add_average_case_merk_replace_layered( + &mut cost, + key_len, + layer_extra_size, + estimated_layer_information.tree_type.inner_node_type(), + ); + if propagate { + add_average_case_merk_propagate(&mut cost, estimated_layer_information, grove_version) + .map_err(Error::MerkError) } else { - TREE_COST_SIZE - }; + Ok(()) + } + .wrap_with_cost(cost) + } + + /// Add average case for insertion into merk + fn average_case_merk_replace_tree_v1( + key: &KeyInfo, + estimated_layer_information: &EstimatedLayerInformation, + replacing_tree_type: TreeType, + propagate: bool, + grove_version: &GroveVersion, + ) -> CostResult<(), Error> { + let mut cost = OperationCost::default(); + let key_len = key.max_length() as u32; + let flags_size = cost_return_on_error_no_add!( + &cost, + estimated_layer_information + .estimated_layer_sizes + .layered_flags_size() + .map_err(Error::MerkError) + ) + .map(|f| f + f.required_space() as u32) + .unwrap_or_default(); + let tree_cost_size = replacing_tree_type.cost_size(); let layer_extra_size = tree_cost_size + flags_size; add_average_case_merk_replace_layered( &mut cost, key_len, layer_extra_size, - estimated_layer_information.is_sum_tree, + estimated_layer_information.tree_type.inner_node_type(), ); if propagate { - add_average_case_merk_propagate(&mut cost, estimated_layer_information) + add_average_case_merk_propagate(&mut cost, estimated_layer_information, grove_version) .map_err(Error::MerkError) } else { Ok(()) @@ -118,8 +183,8 @@ impl GroveDb { pub fn average_case_merk_insert_tree( key: &KeyInfo, flags: &Option, - is_sum_tree: bool, - in_tree_using_sums: bool, + tree_type: TreeType, + in_parent_tree_type: TreeType, propagate_if_input: Option<&EstimatedLayerInformation>, grove_version: &GroveVersion, ) -> CostResult<(), Error> { @@ -138,15 +203,12 @@ impl GroveDb { let flags_len = flags.len() as u32; flags_len + flags_len.required_space() as u32 }); - let tree_cost_size = if is_sum_tree { - SUM_TREE_COST_SIZE - } else { - TREE_COST_SIZE - }; + let tree_cost_size = tree_type.cost_size(); let value_len = tree_cost_size + flags_len; - add_cost_case_merk_insert_layered(&mut cost, key_len, value_len, in_tree_using_sums); + add_cost_case_merk_insert_layered(&mut cost, key_len, value_len, in_parent_tree_type); if let Some(input) = propagate_if_input { - add_average_case_merk_propagate(&mut cost, input).map_err(Error::MerkError) + add_average_case_merk_propagate(&mut cost, input, grove_version) + .map_err(Error::MerkError) } else { Ok(()) } @@ -156,7 +218,7 @@ impl GroveDb { /// Add average case for insertion into merk pub fn average_case_merk_delete_tree( key: &KeyInfo, - is_sum_tree: bool, + tree_type: TreeType, estimated_layer_information: &EstimatedLayerInformation, propagate: bool, grove_version: &GroveVersion, @@ -181,15 +243,11 @@ impl GroveDb { ) .map(|f| f + f.required_space() as u32) .unwrap_or_default(); - let tree_cost_size = if is_sum_tree { - SUM_TREE_COST_SIZE - } else { - TREE_COST_SIZE - }; + let tree_cost_size = tree_type.cost_size(); let layer_extra_size = tree_cost_size + flags_size; add_average_case_merk_delete_layered(&mut cost, key_len, layer_extra_size); if propagate { - add_average_case_merk_propagate(&mut cost, estimated_layer_information) + add_average_case_merk_propagate(&mut cost, estimated_layer_information, grove_version) .map_err(Error::MerkError) } else { Ok(()) @@ -203,7 +261,7 @@ impl GroveDb { pub fn average_case_merk_insert_element( key: &KeyInfo, value: &Element, - in_tree_using_sums: bool, + in_tree_type: TreeType, propagate_for_level: Option<&EstimatedLayerInformation>, grove_version: &GroveVersion, ) -> CostResult<(), Error> { @@ -218,29 +276,25 @@ impl GroveDb { let mut cost = OperationCost::default(); let key_len = key.max_length() as u32; - match value { - Element::Tree(_, flags) | Element::SumTree(_, _, flags) => { - let flags_len = flags.as_ref().map_or(0, |flags| { - let flags_len = flags.len() as u32; - flags_len + flags_len.required_space() as u32 - }); - let tree_cost_size = if value.is_sum_tree() { - SUM_TREE_COST_SIZE - } else { - TREE_COST_SIZE - }; - let value_len = tree_cost_size + flags_len; - add_cost_case_merk_insert_layered(&mut cost, key_len, value_len, in_tree_using_sums) - } - _ => add_cost_case_merk_insert( + if let Some((flags, tree_type)) = value.tree_flags_and_type() { + let flags_len = flags.as_ref().map_or(0, |flags| { + let flags_len = flags.len() as u32; + flags_len + flags_len.required_space() as u32 + }); + let tree_cost_size = tree_type.cost_size(); + let value_len = tree_cost_size + flags_len; + add_cost_case_merk_insert_layered(&mut cost, key_len, value_len, in_tree_type) + } else { + add_cost_case_merk_insert( &mut cost, key_len, cost_return_on_error_no_add!(&cost, value.serialized_size(grove_version)) as u32, - in_tree_using_sums, - ), - }; + in_tree_type, + ) + } if let Some(level) = propagate_for_level { - add_average_case_merk_propagate(&mut cost, level).map_err(Error::MerkError) + add_average_case_merk_propagate(&mut cost, level, grove_version) + .map_err(Error::MerkError) } else { Ok(()) } @@ -253,7 +307,7 @@ impl GroveDb { pub fn average_case_merk_replace_element( key: &KeyInfo, value: &Element, - in_tree_using_sums: bool, + in_tree_type: TreeType, propagate_for_level: Option<&EstimatedLayerInformation>, grove_version: &GroveVersion, ) -> CostResult<(), Error> { @@ -269,23 +323,17 @@ impl GroveDb { let mut cost = OperationCost::default(); let key_len = key.max_length() as u32; match value { - Element::Tree(_, flags) | Element::SumTree(_, _, flags) => { + Element::Tree(_, flags) + | Element::SumTree(_, _, flags) + | Element::BigSumTree(_, _, flags) + | Element::CountTree(_, _, flags) => { let flags_len = flags.as_ref().map_or(0, |flags| { let flags_len = flags.len() as u32; flags_len + flags_len.required_space() as u32 }); - let tree_cost_size = if value.is_sum_tree() { - SUM_TREE_COST_SIZE - } else { - TREE_COST_SIZE - }; + let tree_cost_size = value.tree_type().unwrap().cost_size(); let value_len = tree_cost_size + flags_len; - add_cost_case_merk_replace_layered( - &mut cost, - key_len, - value_len, - in_tree_using_sums, - ) + add_cost_case_merk_replace_layered(&mut cost, key_len, value_len, in_tree_type) } Element::Item(_, flags) | Element::SumItem(_, flags) => { let flags_len = flags.as_ref().map_or(0, |flags| { @@ -299,22 +347,18 @@ impl GroveDb { cost_return_on_error_no_add!(&cost, value.serialized_size(grove_version)) as u32 }; let value_len = sum_item_cost_size + flags_len; - add_cost_case_merk_replace_same_size( - &mut cost, - key_len, - value_len, - in_tree_using_sums, - ) + add_cost_case_merk_replace_same_size(&mut cost, key_len, value_len, in_tree_type) } _ => add_cost_case_merk_replace_same_size( &mut cost, key_len, cost_return_on_error_no_add!(&cost, value.serialized_size(grove_version)) as u32, - in_tree_using_sums, + in_tree_type, ), }; if let Some(level) = propagate_for_level { - add_average_case_merk_propagate(&mut cost, level).map_err(Error::MerkError) + add_average_case_merk_propagate(&mut cost, level, grove_version) + .map_err(Error::MerkError) } else { Ok(()) } @@ -328,7 +372,7 @@ impl GroveDb { key: &KeyInfo, value: &Element, change_in_bytes: i32, - in_tree_using_sums: bool, + in_tree_type: TreeType, propagate_for_level: Option<&EstimatedLayerInformation>, grove_version: &GroveVersion, ) -> CostResult<(), Error> { @@ -359,7 +403,7 @@ impl GroveDb { key_len, value_len, change_in_bytes, - in_tree_using_sums, + in_tree_type, ) } _ => { @@ -368,7 +412,8 @@ impl GroveDb { } }; if let Some(level) = propagate_for_level { - add_average_case_merk_propagate(&mut cost, level).map_err(Error::MerkError) + add_average_case_merk_propagate(&mut cost, level, grove_version) + .map_err(Error::MerkError) } else { Ok(()) } @@ -397,12 +442,12 @@ impl GroveDb { &cost, estimated_layer_information .estimated_layer_sizes - .value_with_feature_and_flags_size() + .value_with_feature_and_flags_size(grove_version) .map_err(Error::MerkError) ); add_average_case_merk_delete(&mut cost, key_len, value_size); if propagate { - add_average_case_merk_propagate(&mut cost, estimated_layer_information) + add_average_case_merk_propagate(&mut cost, estimated_layer_information, grove_version) .map_err(Error::MerkError) } else { Ok(()) @@ -416,7 +461,7 @@ impl GroveDb { path: &KeyInfoPath, key: &KeyInfo, estimated_element_size: u32, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result<(), Error> { check_grovedb_v0!( @@ -431,7 +476,7 @@ impl GroveDb { let value_size = TreeNode::average_case_encoded_tree_size( key.max_length() as u32, estimated_element_size, - in_parent_tree_using_sums, + in_parent_tree_type.inner_node_type(), ); cost.seek_count += 1; cost.storage_loaded_bytes += value_size as u64; @@ -445,8 +490,8 @@ impl GroveDb { path: &KeyInfoPath, key: &KeyInfo, estimated_flags_size: u32, - is_sum_tree: bool, - in_parent_tree_using_sums: bool, + tree_type: TreeType, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result<(), Error> { check_grovedb_v0!( @@ -458,17 +503,13 @@ impl GroveDb { .add_average_case_has_raw_tree_cost ); - let estimated_element_size = if is_sum_tree { - SUM_TREE_COST_SIZE + estimated_flags_size - } else { - TREE_COST_SIZE + estimated_flags_size - }; + let estimated_element_size = tree_type.cost_size() + estimated_flags_size; Self::add_average_case_has_raw_cost::( cost, path, key, estimated_element_size, - in_parent_tree_using_sums, + in_parent_tree_type, grove_version, ) } @@ -479,7 +520,7 @@ impl GroveDb { _path: &KeyInfoPath, key: &KeyInfo, estimated_element_size: u32, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result<(), Error> { check_grovedb_v0!( @@ -496,7 +537,7 @@ impl GroveDb { cost, key.max_length() as u32, estimated_element_size, - in_parent_tree_using_sums, + in_parent_tree_type.inner_node_type(), ) .map_err(Error::MerkError) } @@ -507,8 +548,8 @@ impl GroveDb { _path: &KeyInfoPath, key: &KeyInfo, estimated_flags_size: u32, - is_sum_tree: bool, - in_parent_tree_using_sums: bool, + tree_type: TreeType, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result<(), Error> { check_grovedb_v0!( @@ -520,17 +561,13 @@ impl GroveDb { .add_average_case_get_raw_tree_cost ); - let estimated_element_size = if is_sum_tree { - SUM_TREE_COST_SIZE + estimated_flags_size - } else { - TREE_COST_SIZE + estimated_flags_size - }; + let estimated_element_size = tree_type.cost_size() + estimated_flags_size; cost.seek_count += 1; add_average_case_get_merk_node( cost, key.max_length() as u32, estimated_element_size, - in_parent_tree_using_sums, + in_parent_tree_type.inner_node_type(), ) .map_err(Error::MerkError) } @@ -541,7 +578,7 @@ impl GroveDb { cost: &mut OperationCost, path: &KeyInfoPath, key: &KeyInfo, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, estimated_element_size: u32, estimated_references_sizes: Vec, grove_version: &GroveVersion, @@ -559,7 +596,7 @@ impl GroveDb { let value_size: u32 = TreeNode::average_case_encoded_tree_size( key.max_length() as u32, estimated_element_size, - in_parent_tree_using_sums, + in_parent_tree_type.inner_node_type(), ); cost.seek_count += 1 + estimated_references_sizes.len() as u32; cost.storage_loaded_bytes += value_size as u64 @@ -579,7 +616,7 @@ mod test { use grovedb_costs::OperationCost; use grovedb_merk::{ estimated_costs::average_case_costs::add_average_case_get_merk_node, - test_utils::make_batch_seq, tree::kv::ValueDefinedCostType, Merk, + test_utils::make_batch_seq, tree::kv::ValueDefinedCostType, tree_type::TreeType, Merk, }; use grovedb_storage::{ rocksdb_storage::RocksDbStorage, worst_case_costs::WorstKeyLength, Storage, StorageBatch, @@ -606,7 +643,7 @@ mod test { storage .get_storage_context(EMPTY_PATH, Some(&batch)) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -626,7 +663,7 @@ mod test { // Reopen merk: this time, only root node is loaded to memory let merk = Merk::open_base( storage.get_storage_context(EMPTY_PATH, None).unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -650,8 +687,13 @@ mod test { // (this will be the max_element_size) let mut cost = OperationCost::default(); let key = KnownKey(8_u64.to_be_bytes().to_vec()); - add_average_case_get_merk_node(&mut cost, key.max_length() as u32, 60, false) - .expect("expected to add cost"); + add_average_case_get_merk_node( + &mut cost, + key.max_length() as u32, + 60, + TreeType::NormalTree.inner_node_type(), + ) + .expect("expected to add cost"); assert_eq!(cost, node_result.cost); } @@ -716,7 +758,7 @@ mod test { &path, &key, elem.serialized_size(grove_version).expect("expected size") as u32, - false, + TreeType::NormalTree, GroveVersion::latest(), ) .expect("expected to add cost"); diff --git a/grovedb/src/estimated_costs/worst_case_costs.rs b/grovedb/src/estimated_costs/worst_case_costs.rs index 4c409b99..c6f889f9 100644 --- a/grovedb/src/estimated_costs/worst_case_costs.rs +++ b/grovedb/src/estimated_costs/worst_case_costs.rs @@ -15,6 +15,7 @@ use grovedb_merk::{ }, }, tree::TreeNode, + tree_type::TreeType, HASH_LENGTH, }; use grovedb_storage::{worst_case_costs::WorstKeyLength, Storage}; @@ -25,7 +26,7 @@ use integer_encoding::VarInt; use crate::{ batch::{key_info::KeyInfo, KeyInfoPath}, - element::{SUM_ITEM_COST_SIZE, SUM_TREE_COST_SIZE, TREE_COST_SIZE}, + element::{CostSize, SUM_ITEM_COST_SIZE, SUM_TREE_COST_SIZE, TREE_COST_SIZE}, Element, ElementFlags, Error, GroveDb, }; @@ -36,7 +37,7 @@ impl GroveDb { pub fn add_worst_case_get_merk_at_path<'db, S: Storage<'db>>( cost: &mut OperationCost, path: &KeyInfoPath, - is_sum_tree: bool, + tree_type: TreeType, grove_version: &GroveVersion, ) -> Result<(), Error> { check_grovedb_v0!( @@ -55,7 +56,7 @@ impl GroveDb { cost.storage_loaded_bytes += TreeNode::worst_case_encoded_tree_size( key.max_length() as u32, HASH_LENGTH as u32, - is_sum_tree, + tree_type.inner_node_type(), // todo This is probably wrong ) as u64; } } @@ -66,8 +67,8 @@ impl GroveDb { /// Add worst case for insertion into merk pub(crate) fn worst_case_merk_replace_tree( key: &KeyInfo, - is_sum_tree: bool, - is_in_parent_sum_tree: bool, + tree_type: TreeType, + in_parent_tree_type: TreeType, worst_case_layer_information: &WorstCaseLayerInformation, propagate: bool, grove_version: &GroveVersion, @@ -83,17 +84,13 @@ impl GroveDb { let mut cost = OperationCost::default(); let key_len = key.max_length() as u32; - let tree_cost = if is_sum_tree { - SUM_TREE_COST_SIZE - } else { - TREE_COST_SIZE - }; + let tree_cost = tree_type.cost_size(); let layer_extra_size = tree_cost + WORST_CASE_FLAGS_LEN; add_worst_case_merk_replace_layered( &mut cost, key_len, layer_extra_size, - is_in_parent_sum_tree, + in_parent_tree_type.inner_node_type(), ); if propagate { add_worst_case_merk_propagate(&mut cost, worst_case_layer_information) @@ -108,8 +105,8 @@ impl GroveDb { pub fn worst_case_merk_insert_tree( key: &KeyInfo, flags: &Option, - is_sum_tree: bool, - is_in_parent_sum_tree: bool, + tree_type: TreeType, + in_parent_tree_type: TreeType, propagate_if_input: Option<&WorstCaseLayerInformation>, grove_version: &GroveVersion, ) -> CostResult<(), Error> { @@ -128,13 +125,9 @@ impl GroveDb { let flags_len = flags.len() as u32; flags_len + flags_len.required_space() as u32 }); - let tree_cost = if is_sum_tree { - SUM_TREE_COST_SIZE - } else { - TREE_COST_SIZE - }; + let tree_cost = tree_type.cost_size(); let value_len = tree_cost + flags_len; - add_cost_case_merk_insert_layered(&mut cost, key_len, value_len, is_in_parent_sum_tree); + add_cost_case_merk_insert_layered(&mut cost, key_len, value_len, in_parent_tree_type); if let Some(input) = propagate_if_input { add_worst_case_merk_propagate(&mut cost, input).map_err(Error::MerkError) } else { @@ -146,7 +139,7 @@ impl GroveDb { /// Add worst case for insertion into merk pub fn worst_case_merk_delete_tree( key: &KeyInfo, - is_sum_tree: bool, + tree_type: TreeType, worst_case_layer_information: &WorstCaseLayerInformation, propagate: bool, grove_version: &GroveVersion, @@ -162,11 +155,7 @@ impl GroveDb { let mut cost = OperationCost::default(); let key_len = key.max_length() as u32; - let tree_cost = if is_sum_tree { - SUM_TREE_COST_SIZE - } else { - TREE_COST_SIZE - }; + let tree_cost = tree_type.cost_size(); let layer_extra_size = tree_cost + WORST_CASE_FLAGS_LEN; add_worst_case_merk_delete_layered(&mut cost, key_len, layer_extra_size); if propagate { @@ -184,7 +173,7 @@ impl GroveDb { pub fn worst_case_merk_insert_element( key: &KeyInfo, value: &Element, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, propagate_for_level: Option<&WorstCaseLayerInformation>, grove_version: &GroveVersion, ) -> CostResult<(), Error> { @@ -200,29 +189,28 @@ impl GroveDb { let mut cost = OperationCost::default(); let key_len = key.max_length() as u32; match value { - Element::Tree(_, flags) | Element::SumTree(_, _, flags) => { + Element::Tree(_, flags) + | Element::SumTree(_, _, flags) + | Element::BigSumTree(_, _, flags) + | Element::CountTree(_, _, flags) => { let flags_len = flags.as_ref().map_or(0, |flags| { let flags_len = flags.len() as u32; flags_len + flags_len.required_space() as u32 }); - let tree_cost_size = if value.is_sum_tree() { - SUM_TREE_COST_SIZE - } else { - TREE_COST_SIZE - }; + let tree_cost_size = value.tree_type().unwrap().cost_size(); let value_len = tree_cost_size + flags_len; add_cost_case_merk_insert_layered( &mut cost, key_len, value_len, - in_parent_tree_using_sums, + in_parent_tree_type, ) } _ => add_cost_case_merk_insert( &mut cost, key_len, cost_return_on_error_no_add!(&cost, value.serialized_size(grove_version)) as u32, - in_parent_tree_using_sums, + in_parent_tree_type, ), }; if let Some(level) = propagate_for_level { @@ -239,7 +227,7 @@ impl GroveDb { pub fn worst_case_merk_replace_element( key: &KeyInfo, value: &Element, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, propagate_for_level: Option<&WorstCaseLayerInformation>, grove_version: &GroveVersion, ) -> CostResult<(), Error> { @@ -270,7 +258,7 @@ impl GroveDb { &mut cost, key_len, value_len, - in_parent_tree_using_sums, + in_parent_tree_type, ) } Element::SumItem(_, flags) => { @@ -283,14 +271,14 @@ impl GroveDb { &mut cost, key_len, value_len, - in_parent_tree_using_sums, + in_parent_tree_type, ) } _ => add_cost_case_merk_replace( &mut cost, key_len, cost_return_on_error_no_add!(&cost, value.serialized_size(grove_version)) as u32, - in_parent_tree_using_sums, + in_parent_tree_type, ), }; if let Some(level) = propagate_for_level { @@ -308,7 +296,7 @@ impl GroveDb { key: &KeyInfo, value: &Element, change_in_bytes: i32, - in_tree_using_sums: bool, + in_parent_tree_type: TreeType, propagate_for_level: Option<&WorstCaseLayerInformation>, grove_version: &GroveVersion, ) -> CostResult<(), Error> { @@ -339,7 +327,7 @@ impl GroveDb { key_len, value_len, change_in_bytes, - in_tree_using_sums, + in_parent_tree_type, ) } _ => { @@ -389,7 +377,7 @@ impl GroveDb { path: &KeyInfoPath, key: &KeyInfo, max_element_size: u32, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result<(), Error> { check_grovedb_v0!( @@ -404,7 +392,7 @@ impl GroveDb { let value_size = TreeNode::worst_case_encoded_tree_size( key.max_length() as u32, max_element_size, - in_parent_tree_using_sums, + in_parent_tree_type.inner_node_type(), ); cost.seek_count += 1; cost.storage_loaded_bytes += value_size as u64; @@ -417,8 +405,8 @@ impl GroveDb { cost: &mut OperationCost, _path: &KeyInfoPath, key: &KeyInfo, - is_sum_tree: bool, - in_parent_tree_using_sums: bool, + tree_type: TreeType, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result<(), Error> { check_grovedb_v0!( @@ -431,16 +419,12 @@ impl GroveDb { ); cost.seek_count += 1; - let tree_cost_size = if is_sum_tree { - SUM_TREE_COST_SIZE - } else { - TREE_COST_SIZE - }; + let tree_cost_size = tree_type.cost_size(); add_worst_case_get_merk_node( cost, key.max_length() as u32, tree_cost_size, - in_parent_tree_using_sums, + in_parent_tree_type.inner_node_type(), ) .map_err(Error::MerkError) } @@ -451,7 +435,7 @@ impl GroveDb { _path: &KeyInfoPath, key: &KeyInfo, max_element_size: u32, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result<(), Error> { check_grovedb_v0!( @@ -468,7 +452,7 @@ impl GroveDb { cost, key.max_length() as u32, max_element_size, - in_parent_tree_using_sums, + in_parent_tree_type.inner_node_type(), ) .map_err(Error::MerkError) } @@ -479,7 +463,7 @@ impl GroveDb { path: &KeyInfoPath, key: &KeyInfo, max_element_size: u32, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, max_references_sizes: Vec, grove_version: &GroveVersion, ) -> Result<(), Error> { @@ -496,7 +480,7 @@ impl GroveDb { let value_size: u32 = TreeNode::worst_case_encoded_tree_size( key.max_length() as u32, max_element_size, - in_parent_tree_using_sums, + in_parent_tree_type.inner_node_type(), ); cost.seek_count += 1 + max_references_sizes.len() as u32; cost.storage_loaded_bytes += @@ -513,8 +497,10 @@ mod test { use grovedb_costs::OperationCost; use grovedb_merk::{ estimated_costs::worst_case_costs::add_worst_case_get_merk_node, + merk::NodeType, test_utils::{empty_path_merk, empty_path_merk_read_only, make_batch_seq}, tree::kv::ValueDefinedCostType, + tree_type::TreeType, }; use grovedb_storage::{ rocksdb_storage::{test_utils::TempStorage, RocksDbStorage}, @@ -569,7 +555,7 @@ mod test { // (this will be the max_element_size) let mut cost = OperationCost::default(); let key = KnownKey(8_u64.to_be_bytes().to_vec()); - add_worst_case_get_merk_node(&mut cost, key.max_length() as u32, 60, false) + add_worst_case_get_merk_node(&mut cost, key.max_length() as u32, 60, NodeType::NormalNode) .expect("no issue with version"); assert_eq!(cost, node_result.cost); } @@ -635,7 +621,7 @@ mod test { &path, &key, elem.serialized_size(grove_version).expect("expected size") as u32, - false, + TreeType::NormalTree, GroveVersion::latest(), ) .expect("expected to add cost"); diff --git a/grovedb/src/lib.rs b/grovedb/src/lib.rs index 8e0b088a..7b4e9c00 100644 --- a/grovedb/src/lib.rs +++ b/grovedb/src/lib.rs @@ -161,7 +161,7 @@ use std::{collections::HashMap, option::Option::None, path::Path}; use debugger::start_visualizer; #[cfg(any(feature = "minimal", feature = "verify"))] pub use element::Element; -#[cfg(feature = "minimal")] +#[cfg(any(feature = "minimal", feature = "verify"))] pub use element::ElementFlags; #[cfg(feature = "minimal")] use grovedb_costs::{ @@ -181,6 +181,10 @@ pub use grovedb_merk::proofs::Query; #[cfg(feature = "minimal")] use grovedb_merk::tree::kv::ValueDefinedCostType; #[cfg(feature = "minimal")] +pub use grovedb_merk::tree::AggregateData; +#[cfg(any(feature = "minimal", feature = "verify"))] +pub use grovedb_merk::tree_type::{MaybeTree, TreeType}; +#[cfg(feature = "minimal")] use grovedb_merk::{ self, tree::{combine_hash, value_hash}, @@ -256,7 +260,7 @@ type VerificationIssues = HashMap>, (CryptoHash, CryptoHash, CryptoH type OpenedMerkForReplication<'tx> = ( Merk>, Option>, - bool, + TreeType, ); #[cfg(feature = "minimal")] @@ -318,12 +322,11 @@ impl GroveDb { } ) ); - let is_sum_tree = element.is_sum_tree(); - if let Element::Tree(root_key, _) | Element::SumTree(root_key, ..) = element { + if let Some((root_key, tree_type)) = element.root_key_and_tree_type_owned() { Merk::open_layered_with_root_key( storage, root_key, - is_sum_tree, + tree_type, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -340,7 +343,7 @@ impl GroveDb { } else { Merk::open_base( storage, - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -353,7 +356,7 @@ impl GroveDb { &'db self, prefix: SubtreePrefix, root_key: Option>, - is_sum_tree: bool, + tree_type: TreeType, tx: &'db Transaction, batch: Option<&'db StorageBatch>, grove_version: &GroveVersion, @@ -367,7 +370,7 @@ impl GroveDb { Merk::open_layered_with_root_key( storage, root_key, - is_sum_tree, + tree_type, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -380,7 +383,7 @@ impl GroveDb { } else { Merk::open_base( storage, - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -421,13 +424,12 @@ impl GroveDb { )) }) .unwrap()?; - let is_sum_tree = element.is_sum_tree(); - if let Element::Tree(root_key, _) | Element::SumTree(root_key, ..) = element { + if let Some((root_key, tree_type)) = element.root_key_and_tree_type_owned() { Ok(( Merk::open_layered_with_root_key( storage, root_key.clone(), - is_sum_tree, + tree_type, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -436,7 +438,7 @@ impl GroveDb { }) .unwrap()?, root_key, - is_sum_tree, + tree_type, )) } else { Err(Error::CorruptedPath( @@ -447,14 +449,14 @@ impl GroveDb { Ok(( Merk::open_base( storage, - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) .map_err(|_| Error::CorruptedData("cannot open a the root subtree".to_owned())) .unwrap()?, None, - false, + TreeType::NormalTree, )) } } @@ -494,12 +496,11 @@ impl GroveDb { } ) ); - let is_sum_tree = element.is_sum_tree(); - if let Element::Tree(root_key, _) | Element::SumTree(root_key, ..) = element { + if let Some((root_key, tree_type)) = element.root_key_and_tree_type_owned() { Merk::open_layered_with_root_key( storage, root_key, - is_sum_tree, + tree_type, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -516,7 +517,7 @@ impl GroveDb { } else { Merk::open_base( storage, - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -529,7 +530,7 @@ impl GroveDb { &'db self, prefix: SubtreePrefix, root_key: Option>, - is_sum_tree: bool, + tree_type: TreeType, batch: Option<&'db StorageBatch>, grove_version: &GroveVersion, ) -> CostResult>, Error> { @@ -542,7 +543,7 @@ impl GroveDb { Merk::open_layered_with_root_key( storage, root_key, - is_sum_tree, + tree_type, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -555,7 +556,7 @@ impl GroveDb { } else { Merk::open_base( storage, - false, + tree_type, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -651,9 +652,11 @@ impl GroveDb { grove_version, ) ); - let (root_hash, root_key, sum) = cost_return_on_error!( + let (root_hash, root_key, aggregate_data) = cost_return_on_error!( &mut cost, - child_tree.root_hash_key_and_sum().map_err(Error::MerkError) + child_tree + .root_hash_key_and_aggregate_data() + .map_err(Error::MerkError) ); cost_return_on_error!( &mut cost, @@ -662,7 +665,7 @@ impl GroveDb { parent_key, root_key, root_hash, - sum, + aggregate_data, grove_version, ) ); @@ -705,9 +708,11 @@ impl GroveDb { grove_version ) ); - let (root_hash, root_key, sum) = cost_return_on_error!( + let (root_hash, root_key, aggregate_data) = cost_return_on_error!( &mut cost, - child_tree.root_hash_key_and_sum().map_err(Error::MerkError) + child_tree + .root_hash_key_and_aggregate_data() + .map_err(Error::MerkError) ); cost_return_on_error!( &mut cost, @@ -716,7 +721,7 @@ impl GroveDb { parent_key, root_key, root_hash, - sum, + aggregate_data, grove_version, ) ); @@ -758,7 +763,9 @@ impl GroveDb { ); let (root_hash, root_key, sum) = cost_return_on_error!( &mut cost, - child_tree.root_hash_key_and_sum().map_err(Error::MerkError) + child_tree + .root_hash_key_and_aggregate_data() + .map_err(Error::MerkError) ); cost_return_on_error!( &mut cost, @@ -783,7 +790,7 @@ impl GroveDb { key: K, maybe_root_key: Option>, root_tree_hash: Hash, - sum: Option, + aggregate_data: AggregateData, grove_version: &GroveVersion, ) -> CostResult<(), Error> { let key_ref = key.as_ref(); @@ -795,7 +802,47 @@ impl GroveDb { } else if let Element::SumTree(.., flag) = element { let tree = Element::new_sum_tree_with_flags_and_sum_value( maybe_root_key, - sum.unwrap_or_default(), + aggregate_data.as_sum_i64(), + flag, + ); + tree.insert_subtree( + parent_tree, + key.as_ref(), + root_tree_hash, + None, + grove_version, + ) + } else if let Element::BigSumTree(.., flag) = element { + let tree = Element::new_big_sum_tree_with_flags_and_sum_value( + maybe_root_key, + aggregate_data.as_summed_i128(), + flag, + ); + tree.insert_subtree( + parent_tree, + key.as_ref(), + root_tree_hash, + None, + grove_version, + ) + } else if let Element::CountTree(.., flag) = element { + let tree = Element::new_count_tree_with_flags_and_count_value( + maybe_root_key, + aggregate_data.as_count_u64(), + flag, + ); + tree.insert_subtree( + parent_tree, + key.as_ref(), + root_tree_hash, + None, + grove_version, + ) + } else if let Element::CountSumTree(.., flag) = element { + let tree = Element::new_count_sum_tree_with_flags_and_sum_and_count_value( + maybe_root_key, + aggregate_data.as_count_u64(), + aggregate_data.as_sum_i64(), flag, ); tree.insert_subtree( @@ -825,7 +872,7 @@ impl GroveDb { key: K, maybe_root_key: Option>, root_tree_hash: Hash, - sum: Option, + aggregate_data: AggregateData, batch_operations: &mut Vec>, grove_version: &GroveVersion, ) -> CostResult<(), Error> { @@ -836,7 +883,7 @@ impl GroveDb { let tree = Element::new_tree_with_flags(maybe_root_key, flag); let merk_feature_type = cost_return_on_error!( &mut cost, - tree.get_feature_type(parent_tree.is_sum_tree) + tree.get_feature_type(parent_tree.tree_type) .wrap_with_cost(OperationCost::default()) ); tree.insert_subtree_into_batch_operations( @@ -850,12 +897,70 @@ impl GroveDb { } else if let Element::SumTree(.., flag) = element { let tree = Element::new_sum_tree_with_flags_and_sum_value( maybe_root_key, - sum.unwrap_or_default(), + aggregate_data.as_sum_i64(), + flag, + ); + let merk_feature_type = cost_return_on_error!( + &mut cost, + tree.get_feature_type(parent_tree.tree_type) + .wrap_with_cost(OperationCost::default()) + ); + tree.insert_subtree_into_batch_operations( + key, + root_tree_hash, + true, + batch_operations, + merk_feature_type, + grove_version, + ) + } else if let Element::BigSumTree(.., flag) = element { + let tree = Element::new_big_sum_tree_with_flags_and_sum_value( + maybe_root_key, + aggregate_data.as_summed_i128(), + flag, + ); + let merk_feature_type = cost_return_on_error!( + &mut cost, + tree.get_feature_type(parent_tree.tree_type) + .wrap_with_cost(OperationCost::default()) + ); + tree.insert_subtree_into_batch_operations( + key, + root_tree_hash, + true, + batch_operations, + merk_feature_type, + grove_version, + ) + } else if let Element::CountTree(.., flag) = element { + let tree = Element::new_count_tree_with_flags_and_count_value( + maybe_root_key, + aggregate_data.as_count_u64(), + flag, + ); + let merk_feature_type = cost_return_on_error!( + &mut cost, + tree.get_feature_type(parent_tree.tree_type) + .wrap_with_cost(OperationCost::default()) + ); + tree.insert_subtree_into_batch_operations( + key, + root_tree_hash, + true, + batch_operations, + merk_feature_type, + grove_version, + ) + } else if let Element::CountSumTree(.., flag) = element { + let tree = Element::new_count_sum_tree_with_flags_and_sum_and_count_value( + maybe_root_key, + aggregate_data.as_count_u64(), + aggregate_data.as_sum_i64(), flag, ); let merk_feature_type = cost_return_on_error!( &mut cost, - tree.get_feature_type(parent_tree.is_sum_tree) + tree.get_feature_type(parent_tree.tree_type) .wrap_with_cost(OperationCost::default()) ); tree.insert_subtree_into_batch_operations( @@ -1095,7 +1200,11 @@ impl GroveDb { while let Some((key, element_value)) = element_iterator.next_kv().unwrap() { let element = raw_decode(&element_value, grove_version)?; match element { - Element::SumTree(..) | Element::Tree(..) => { + Element::SumTree(..) + | Element::Tree(..) + | Element::BigSumTree(..) + | Element::CountTree(..) + | Element::CountSumTree(..) => { let (kv_value, element_value_hash) = merk .get_value_and_value_hash( &key, @@ -1239,7 +1348,11 @@ impl GroveDb { while let Some((key, element_value)) = element_iterator.next_kv().unwrap() { let element = raw_decode(&element_value, grove_version)?; match element { - Element::SumTree(..) | Element::Tree(..) => { + Element::SumTree(..) + | Element::Tree(..) + | Element::BigSumTree(..) + | Element::CountTree(..) + | Element::CountSumTree(..) => { let (kv_value, element_value_hash) = merk .get_value_and_value_hash( &key, diff --git a/grovedb/src/operations/delete/average_case.rs b/grovedb/src/operations/delete/average_case.rs index b828ce0c..6e8b0158 100644 --- a/grovedb/src/operations/delete/average_case.rs +++ b/grovedb/src/operations/delete/average_case.rs @@ -8,6 +8,7 @@ use grovedb_merk::{ average_case_costs::EstimatedLayerInformation, worst_case_costs::add_average_case_cost_for_is_empty_tree_except, }, + tree_type::TreeType, HASH_LENGTH_U32, }; use grovedb_storage::{worst_case_costs::WorstKeyLength, Storage}; @@ -65,7 +66,7 @@ impl GroveDb { except_keys_count, key_len, estimated_element_size, - is_sum_tree, + tree_type, ) = cost_return_on_error_no_add!( &cost, if height == path_len - 1 { @@ -74,7 +75,7 @@ impl GroveDb { &cost, layer_info .estimated_layer_sizes - .value_with_feature_and_flags_size() + .value_with_feature_and_flags_size(grove_version) .map_err(Error::MerkError) ); Ok(( @@ -84,7 +85,7 @@ impl GroveDb { 0, key.max_length() as u32, estimated_value_len, - layer_info.is_sum_tree, + layer_info.tree_type, )) } else { Err(Error::InvalidParameter( @@ -99,7 +100,7 @@ impl GroveDb { &cost, layer_info .estimated_layer_sizes - .subtree_with_feature_and_flags_size() + .subtree_with_feature_and_flags_size(grove_version) .map_err(Error::MerkError) ); Ok(( @@ -109,7 +110,7 @@ impl GroveDb { 1, last_key.max_length() as u32, estimated_value_len, - layer_info.is_sum_tree, + layer_info.tree_type, )) } else { Err(Error::InvalidParameter("intermediate layer info missing")) @@ -121,7 +122,7 @@ impl GroveDb { Self::average_case_delete_operation_for_delete::( &KeyInfoPath::from_vec(path_at_level.to_vec()), key_at_level, - is_sum_tree, + tree_type, validate, check_if_tree, except_keys_count, @@ -139,7 +140,7 @@ impl GroveDb { pub fn average_case_delete_operation_for_delete<'db, S: Storage<'db>>( path: &KeyInfoPath, key: &KeyInfo, - parent_tree_is_sum_tree: bool, + in_parent_tree_type: TreeType, validate: bool, check_if_tree: bool, except_keys_count: u16, @@ -163,7 +164,7 @@ impl GroveDb { &mut cost, path, false, - parent_tree_is_sum_tree, + in_parent_tree_type, grove_version, ) ); @@ -176,7 +177,7 @@ impl GroveDb { path, key, estimated_key_element_size.1, - parent_tree_is_sum_tree, + in_parent_tree_type, grove_version, ) ); diff --git a/grovedb/src/operations/delete/delete_up_tree.rs b/grovedb/src/operations/delete/delete_up_tree.rs index 2255f29d..2b167374 100644 --- a/grovedb/src/operations/delete/delete_up_tree.rs +++ b/grovedb/src/operations/delete/delete_up_tree.rs @@ -5,6 +5,7 @@ use grovedb_costs::{ storage_cost::removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, CostResult, CostsExt, OperationCost, }; +use grovedb_merk::MaybeTree; use grovedb_path::SubtreePath; use grovedb_version::{ check_grovedb_v0_with_cost, error::GroveVersionError, version::GroveVersion, @@ -169,7 +170,7 @@ impl GroveDb { path: SubtreePath, key: &[u8], options: &DeleteUpTreeOptions, - is_known_to_be_subtree_with_sum: Option<(bool, bool)>, + is_known_to_be_subtree: Option, mut current_batch_operations: Vec, transaction: TransactionArg, grove_version: &GroveVersion, @@ -186,7 +187,7 @@ impl GroveDb { path, key, options, - is_known_to_be_subtree_with_sum, + is_known_to_be_subtree, &mut current_batch_operations, transaction, grove_version, @@ -201,7 +202,7 @@ impl GroveDb { path: SubtreePath, key: &[u8], options: &DeleteUpTreeOptions, - is_known_to_be_subtree_with_sum: Option<(bool, bool)>, + is_known_to_be_subtree: Option, current_batch_operations: &mut Vec, transaction: TransactionArg, grove_version: &GroveVersion, @@ -234,7 +235,7 @@ impl GroveDb { path.clone(), key, &options.to_delete_options(), - is_known_to_be_subtree_with_sum, + is_known_to_be_subtree, current_batch_operations, transaction, grove_version, diff --git a/grovedb/src/operations/delete/mod.rs b/grovedb/src/operations/delete/mod.rs index 9e24ad3e..8bf2e0ea 100644 --- a/grovedb/src/operations/delete/mod.rs +++ b/grovedb/src/operations/delete/mod.rs @@ -18,7 +18,8 @@ use grovedb_costs::{ storage_cost::removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, CostResult, CostsExt, OperationCost, }; -use grovedb_merk::{proofs::Query, KVIterator}; +#[cfg(feature = "minimal")] +use grovedb_merk::{proofs::Query, KVIterator, MaybeTree}; #[cfg(feature = "minimal")] use grovedb_merk::{Error as MerkError, Merk, MerkOptions}; use grovedb_path::SubtreePath; @@ -511,7 +512,7 @@ impl GroveDb { path: SubtreePath, key: &[u8], options: &DeleteOptions, - is_known_to_be_subtree_with_sum: Option<(bool, bool)>, + is_known_to_be_subtree: Option, current_batch_operations: &[QualifiedGroveDbOp], transaction: TransactionArg, grove_version: &GroveVersion, @@ -544,28 +545,24 @@ impl GroveDb { ) ); } - let (is_subtree, is_subtree_with_sum) = match is_known_to_be_subtree_with_sum { + let tree_type = match is_known_to_be_subtree { None => { let element = cost_return_on_error!( &mut cost, self.get_raw(path.clone(), key.as_ref(), transaction, grove_version) ); - match element { - Element::Tree(..) => (true, false), - Element::SumTree(..) => (true, true), - _ => (false, false), - } + element.maybe_tree_type() } Some(x) => x, }; - if is_subtree { + if let MaybeTree::Tree(tree_type) = tree_type { let subtree_merk_path = path.derive_owned_with_child(key); let subtree_merk_path_vec = subtree_merk_path.to_vec(); let batch_deleted_keys = current_batch_operations .iter() .filter_map(|op| match op.op { - GroveOp::Delete | GroveOp::DeleteTree | GroveOp::DeleteSumTree => { + GroveOp::Delete | GroveOp::DeleteTree(_) => { // todo: to_path clones (best to figure out how to compare without // cloning) if op.path.to_path() == subtree_merk_path_vec { @@ -595,7 +592,7 @@ impl GroveDb { // If there is any current batch operation that is inserting something in this // tree then it is not empty either is_empty &= !current_batch_operations.iter().any(|op| match op.op { - GroveOp::Delete | GroveOp::DeleteTree | GroveOp::DeleteSumTree => false, + GroveOp::Delete | GroveOp::DeleteTree(_) => false, // todo: fix for to_path (it clones) _ => op.path.to_path() == subtree_merk_path_vec, }); @@ -613,7 +610,7 @@ impl GroveDb { Ok(Some(QualifiedGroveDbOp::delete_tree_op( path.to_vec(), key.to_vec(), - is_subtree_with_sum, + tree_type, ))) } else { Err(Error::NotSupported( @@ -711,8 +708,8 @@ impl GroveDb { grove_version ) ); - let uses_sum_tree = subtree_to_delete_from.is_sum_tree; - if element.is_any_tree() { + let uses_sum_tree = subtree_to_delete_from.tree_type; + if let Some(tree_type) = element.tree_type() { let subtree_merk_path = path.derive_owned_with_child(key); let subtree_merk_path_ref = SubtreePath::from(&subtree_merk_path); @@ -771,7 +768,7 @@ impl GroveDb { Merk::open_layered_with_root_key( storage, subtree_to_delete_from.root_key(), - element.is_sum_tree(), + tree_type, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -905,7 +902,7 @@ impl GroveDb { &mut cost, self.open_non_transactional_merk_at_path(path.clone(), Some(batch), grove_version) ); - let uses_sum_tree = subtree_to_delete_from.is_sum_tree; + let uses_sum_tree = subtree_to_delete_from.tree_type; if element.is_any_tree() { let subtree_merk_path = path.derive_owned_with_child(key); let subtree_of_tree_we_are_deleting = cost_return_on_error!( diff --git a/grovedb/src/operations/delete/worst_case.rs b/grovedb/src/operations/delete/worst_case.rs index effcb5fe..89454149 100644 --- a/grovedb/src/operations/delete/worst_case.rs +++ b/grovedb/src/operations/delete/worst_case.rs @@ -5,6 +5,7 @@ use grovedb_costs::{ }; use grovedb_merk::{ estimated_costs::worst_case_costs::add_worst_case_cost_for_is_empty_tree_except, tree::kv::KV, + tree_type::TreeType, }; use grovedb_storage::{worst_case_costs::WorstKeyLength, Storage}; use grovedb_version::{ @@ -26,7 +27,7 @@ impl GroveDb { key: &KeyInfo, stop_path_height: Option, validate: bool, - intermediate_tree_info: IntMap, + intermediate_tree_info: IntMap, max_element_size: u32, grove_version: &GroveVersion, ) -> CostResult, Error> { @@ -59,13 +60,12 @@ impl GroveDb { check_if_tree, except_keys_count, max_element_size, - is_sum_tree, + tree_type, ) = cost_return_on_error_no_add!( &cost, if height == path_len { - if let Some((is_in_sum_tree, _)) = intermediate_tree_info.get(height as u64) - { - Ok((used_path, key, true, 0, max_element_size, *is_in_sum_tree)) + if let Some((tree_type, _)) = intermediate_tree_info.get(height as u64) { + Ok((used_path, key, true, 0, max_element_size, *tree_type)) } else { Err(Error::InvalidParameter( "intermediate flag size missing for height at path length", @@ -74,25 +74,19 @@ impl GroveDb { } else { let (last_key, smaller_path) = used_path.split_last().unwrap(); used_path = smaller_path; - if let Some((is_in_sum_tree, flags_size_at_level)) = + if let Some((tree_type, flags_size_at_level)) = intermediate_tree_info.get(height as u64) { // the worst case is that we are only in sum trees + // Todo the worst case is actually now big sum trees let value_len = SUM_TREE_COST_SIZE + flags_size_at_level; let max_tree_size = KV::layered_node_byte_cost_size_for_key_and_value_lengths( last_key.max_length() as u32, value_len, - *is_in_sum_tree, + tree_type.inner_node_type(), ); - Ok(( - used_path, - last_key, - false, - 1, - max_tree_size, - *is_in_sum_tree, - )) + Ok((used_path, last_key, false, 1, max_tree_size, *tree_type)) } else { Err(Error::InvalidParameter("intermediate flag size missing")) } @@ -103,7 +97,7 @@ impl GroveDb { Self::worst_case_delete_operation_for_delete::( &KeyInfoPath::from_vec(path_at_level.to_vec()), key_at_level, - is_sum_tree, + tree_type, validate, check_if_tree, except_keys_count, @@ -121,7 +115,7 @@ impl GroveDb { pub fn worst_case_delete_operation_for_delete<'db, S: Storage<'db>>( path: &KeyInfoPath, key: &KeyInfo, - parent_tree_is_sum_tree: bool, + in_parent_tree_type: TreeType, validate: bool, check_if_tree: bool, except_keys_count: u16, @@ -144,7 +138,7 @@ impl GroveDb { GroveDb::add_worst_case_get_merk_at_path::( &mut cost, path, - parent_tree_is_sum_tree, + in_parent_tree_type, grove_version, ) ); @@ -157,7 +151,7 @@ impl GroveDb { path, key, max_element_size, - parent_tree_is_sum_tree, + in_parent_tree_type, grove_version, ) ); diff --git a/grovedb/src/operations/get/average_case.rs b/grovedb/src/operations/get/average_case.rs index 0cb44462..d920ad01 100644 --- a/grovedb/src/operations/get/average_case.rs +++ b/grovedb/src/operations/get/average_case.rs @@ -3,6 +3,8 @@ #[cfg(feature = "minimal")] use grovedb_costs::OperationCost; #[cfg(feature = "minimal")] +use grovedb_merk::tree_type::TreeType; +#[cfg(feature = "minimal")] use grovedb_storage::rocksdb_storage::RocksDbStorage; use grovedb_version::{check_grovedb_v0, error::GroveVersionError, version::GroveVersion}; @@ -21,7 +23,7 @@ impl GroveDb { path: &KeyInfoPath, key: &KeyInfo, estimated_element_size: u32, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result { check_grovedb_v0!( @@ -38,7 +40,7 @@ impl GroveDb { path, key, estimated_element_size, - in_parent_tree_using_sums, + in_parent_tree_type, grove_version, )?; Ok(cost) @@ -50,8 +52,8 @@ impl GroveDb { path: &KeyInfoPath, key: &KeyInfo, estimated_flags_size: u32, - is_sum_tree: bool, - in_parent_tree_using_sums: bool, + tree_type: TreeType, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result { check_grovedb_v0!( @@ -68,8 +70,8 @@ impl GroveDb { path, key, estimated_flags_size, - is_sum_tree, - in_parent_tree_using_sums, + tree_type, + in_parent_tree_type, grove_version, )?; Ok(cost) @@ -81,7 +83,7 @@ impl GroveDb { path: &KeyInfoPath, key: &KeyInfo, estimated_element_size: u32, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result { check_grovedb_v0!( @@ -98,7 +100,7 @@ impl GroveDb { path, key, estimated_element_size, - in_parent_tree_using_sums, + in_parent_tree_type, grove_version, )?; Ok(cost) @@ -108,7 +110,7 @@ impl GroveDb { pub fn average_case_for_get( path: &KeyInfoPath, key: &KeyInfo, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, estimated_element_size: u32, estimated_references_sizes: Vec, grove_version: &GroveVersion, @@ -126,7 +128,7 @@ impl GroveDb { &mut cost, path, key, - in_parent_tree_using_sums, + in_parent_tree_type, estimated_element_size, estimated_references_sizes, grove_version, @@ -139,8 +141,8 @@ impl GroveDb { path: &KeyInfoPath, key: &KeyInfo, estimated_flags_size: u32, - is_sum_tree: bool, - in_parent_tree_using_sums: bool, + tree_type: TreeType, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result { check_grovedb_v0!( @@ -157,8 +159,8 @@ impl GroveDb { path, key, estimated_flags_size, - is_sum_tree, - in_parent_tree_using_sums, + tree_type, + in_parent_tree_type, grove_version, )?; Ok(cost) diff --git a/grovedb/src/operations/get/mod.rs b/grovedb/src/operations/get/mod.rs index 4b1d0ccd..d52fe4b1 100644 --- a/grovedb/src/operations/get/mod.rs +++ b/grovedb/src/operations/get/mod.rs @@ -22,6 +22,8 @@ use grovedb_version::{ check_grovedb_v0_with_cost, error::GroveVersionError, version::GroveVersion, }; +#[cfg(feature = "minimal")] +use crate::error::GroveDbErrorExt; #[cfg(feature = "minimal")] use crate::{ reference_path::{path_from_reference_path_type, path_from_reference_qualified_path_type}, @@ -295,7 +297,7 @@ impl GroveDb { let merk_to_get_from = cost_return_on_error!( &mut cost, - self.open_transactional_merk_at_path(path, transaction, None, grove_version) + self.open_transactional_merk_at_path(path.clone(), transaction, None, grove_version) .map_err(|e| match e { Error::InvalidParentLayerPath(s) => { Error::PathParentLayerNotFound(s) @@ -304,7 +306,9 @@ impl GroveDb { }) ); - Element::get(&merk_to_get_from, key, allow_cache, grove_version).add_cost(cost) + Element::get(&merk_to_get_from, key, allow_cache, grove_version) + .add_context(format!("path is {}", path)) + .add_cost(cost) } /// Get tree item without following references @@ -353,7 +357,7 @@ impl GroveDb { let merk_to_get_from = cost_return_on_error!( &mut cost, - self.open_non_transactional_merk_at_path(path, None, grove_version) + self.open_non_transactional_merk_at_path(path.clone(), None, grove_version) .map_err(|e| match e { Error::InvalidParentLayerPath(s) => { Error::PathParentLayerNotFound(s) @@ -362,7 +366,9 @@ impl GroveDb { }) ); - Element::get(&merk_to_get_from, key, allow_cache, grove_version).add_cost(cost) + Element::get(&merk_to_get_from, key, allow_cache, grove_version) + .add_context(format!("path is {}", path)) + .add_cost(cost) } /// Get tree item without following references @@ -445,6 +451,7 @@ impl GroveDb { ); Element::get(&merk_to_get_from, parent_key, true, grove_version) + .add_context(format!("path is {}", path)) } else { let merk_to_get_from = cost_return_on_error!( &mut cost, @@ -452,10 +459,15 @@ impl GroveDb { ); Element::get(&merk_to_get_from, parent_key, true, grove_version) + .add_context(format!("path is {}", path)) } .unwrap_add_cost(&mut cost); match element { - Ok(Element::Tree(..)) | Ok(Element::SumTree(..)) => Ok(()).wrap_with_cost(cost), + Ok(Element::Tree(..)) + | Ok(Element::SumTree(..)) + | Ok(Element::BigSumTree(..)) + | Ok(Element::CountTree(..)) + | Ok(Element::CountSumTree(..)) => Ok(()).wrap_with_cost(cost), Ok(_) | Err(Error::PathKeyNotFound(_)) => Err(error_fn()).wrap_with_cost(cost), Err(e) => Err(e).wrap_with_cost(cost), } diff --git a/grovedb/src/operations/get/query.rs b/grovedb/src/operations/get/query.rs index 0e97f7fb..c1b75468 100644 --- a/grovedb/src/operations/get/query.rs +++ b/grovedb/src/operations/get/query.rs @@ -14,7 +14,8 @@ use integer_encoding::VarInt; #[cfg(feature = "minimal")] use crate::element::SumValue; use crate::{ - element::QueryOptions, operations::proof::ProveOptions, + element::{BigSumValue, CountValue, QueryOptions}, + operations::proof::ProveOptions, query_result_type::PathKeyOptionalElementTrio, }; #[cfg(feature = "minimal")] @@ -32,6 +33,12 @@ pub enum QueryItemOrSumReturnType { ItemData(Vec), /// A sum item or a sum tree value SumValue(SumValue), + /// A big sum tree value + BigSumValue(BigSumValue), + /// A count value + CountValue(CountValue), + /// A count and sum value + CountSumValue(CountValue, SumValue), } #[cfg(feature = "minimal")] @@ -222,7 +229,12 @@ where { )), } } - Element::Item(..) | Element::SumItem(..) | Element::SumTree(..) => Ok(element), + Element::Item(..) + | Element::SumItem(..) + | Element::SumTree(..) + | Element::BigSumTree(..) + | Element::CountTree(..) + | Element::CountSumTree(..) => Ok(element), Element::Tree(..) => Err(Error::InvalidQuery("path_queries can not refer to trees")), } } @@ -341,7 +353,11 @@ where { } Element::Item(item, _) => Ok(item), Element::SumItem(item, _) => Ok(item.encode_var_vec()), - Element::Tree(..) | Element::SumTree(..) => Err(Error::InvalidQuery( + Element::Tree(..) + | Element::SumTree(..) + | Element::BigSumTree(..) + | Element::CountTree(..) + | Element::CountSumTree(..) => Err(Error::InvalidQuery( "path_queries can only refer to items and references", )), } @@ -422,6 +438,18 @@ where { Element::SumTree(_, sum_value, _) => { Ok(QueryItemOrSumReturnType::SumValue(sum_value)) } + Element::BigSumTree(_, big_sum_value, _) => { + Ok(QueryItemOrSumReturnType::BigSumValue(big_sum_value)) + } + Element::CountTree(_, count_value, _) => { + Ok(QueryItemOrSumReturnType::CountValue(count_value)) + } + Element::CountSumTree(_, count_value, sum_value, _) => { + Ok(QueryItemOrSumReturnType::CountSumValue( + count_value, + sum_value, + )) + } _ => Err(Error::InvalidQuery( "the reference must result in an item", )), @@ -439,6 +467,15 @@ where { Element::SumTree(_, sum_value, _) => { Ok(QueryItemOrSumReturnType::SumValue(sum_value)) } + Element::BigSumTree(_, big_sum_value, _) => { + Ok(QueryItemOrSumReturnType::BigSumValue(big_sum_value)) + } + Element::CountTree(_, count_value, _) => { + Ok(QueryItemOrSumReturnType::CountValue(count_value)) + } + Element::CountSumTree(_, count_value, sum_value, _) => Ok( + QueryItemOrSumReturnType::CountSumValue(count_value, sum_value), + ), Element::Tree(..) => Err(Error::InvalidQuery( "path_queries can only refer to items, sum items, references and sum \ trees", @@ -520,12 +557,15 @@ where { } } Element::SumItem(item, _) => Ok(item), - Element::Tree(..) | Element::SumTree(..) | Element::Item(..) => { - Err(Error::InvalidQuery( - "path_queries over sum items can only refer to sum items and \ - references", - )) - } + Element::Tree(..) + | Element::SumTree(..) + | Element::BigSumTree(..) + | Element::CountTree(..) + | Element::CountSumTree(..) + | Element::Item(..) => Err(Error::InvalidQuery( + "path_queries over sum items can only refer to sum items and \ + references", + )), } } _ => Err(Error::CorruptedCodeExecution( diff --git a/grovedb/src/operations/get/worst_case.rs b/grovedb/src/operations/get/worst_case.rs index 591d6150..e6382dd8 100644 --- a/grovedb/src/operations/get/worst_case.rs +++ b/grovedb/src/operations/get/worst_case.rs @@ -3,6 +3,8 @@ #[cfg(feature = "minimal")] use grovedb_costs::OperationCost; #[cfg(feature = "minimal")] +use grovedb_merk::tree_type::TreeType; +#[cfg(feature = "minimal")] use grovedb_storage::rocksdb_storage::RocksDbStorage; use grovedb_version::{check_grovedb_v0, error::GroveVersionError, version::GroveVersion}; @@ -20,7 +22,7 @@ impl GroveDb { path: &KeyInfoPath, key: &KeyInfo, max_element_size: u32, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result { check_grovedb_v0!( @@ -37,7 +39,7 @@ impl GroveDb { path, key, max_element_size, - in_parent_tree_using_sums, + in_parent_tree_type, grove_version, )?; Ok(cost) @@ -48,7 +50,7 @@ impl GroveDb { path: &KeyInfoPath, key: &KeyInfo, max_element_size: u32, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result { check_grovedb_v0!( @@ -65,7 +67,7 @@ impl GroveDb { path, key, max_element_size, - in_parent_tree_using_sums, + in_parent_tree_type, grove_version, )?; Ok(cost) @@ -77,7 +79,7 @@ impl GroveDb { key: &KeyInfo, max_element_size: u32, max_references_sizes: Vec, - in_parent_tree_using_sums: bool, + in_parent_tree_type: TreeType, grove_version: &GroveVersion, ) -> Result { check_grovedb_v0!( @@ -94,7 +96,7 @@ impl GroveDb { path, key, max_element_size, - in_parent_tree_using_sums, + in_parent_tree_type, max_references_sizes, grove_version, )?; diff --git a/grovedb/src/operations/insert/mod.rs b/grovedb/src/operations/insert/mod.rs index 57a59380..e362a761 100644 --- a/grovedb/src/operations/insert/mod.rs +++ b/grovedb/src/operations/insert/mod.rs @@ -317,7 +317,10 @@ impl GroveDb { ) ); } - Element::Tree(ref value, _) | Element::SumTree(ref value, ..) => { + Element::Tree(ref value, _) + | Element::SumTree(ref value, ..) + | Element::BigSumTree(ref value, ..) + | Element::CountTree(ref value, ..) => { if value.is_some() { return Err(Error::InvalidCodeExecution( "a tree should be empty at the moment of insertion when not using batches", @@ -450,7 +453,10 @@ impl GroveDb { ) ); } - Element::Tree(ref value, _) | Element::SumTree(ref value, ..) => { + Element::Tree(ref value, _) + | Element::SumTree(ref value, ..) + | Element::BigSumTree(ref value, ..) + | Element::CountTree(ref value, ..) => { if value.is_some() { return Err(Error::InvalidCodeExecution( "a tree should be empty at the moment of insertion when not using batches", @@ -1593,6 +1599,87 @@ mod tests { ); } + #[test] + fn test_one_insert_item_cost_under_count_tree() { + let grove_version = GroveVersion::latest(); + let db = make_empty_grovedb(); + let tx = db.start_transaction(); + + db.insert( + EMPTY_PATH, + b"tree", + Element::empty_count_tree(), + None, + Some(&tx), + grove_version, + ) + .unwrap() + .unwrap(); + + let cost = db + .insert( + [b"tree".as_slice()].as_ref(), + b"key1", + Element::new_item(b"test".to_vec()), + None, + Some(&tx), + grove_version, + ) + .cost_as_result() + .unwrap(); + + // Explanation for 152 storage_written_bytes + + // Key -> 37 bytes + // 32 bytes for the key prefix + // 4 bytes for the key + // 1 byte for key_size (required space for 36) + + // Value -> 81 + // 1 for the enum type item + // 1 for size of test bytes + // 4 for test bytes + // 1 for the flag option (but no flags) + // 32 for node hash + // 32 for value hash (trees have this for free) + // 9 for Count node + // 1 byte for the value_size (required space for 1) + + // Parent Hook -> 48 + // Key Bytes 4 + // Hash Size 32 + // Key Length 1 + // Count Merk 9 + // Child Heights 2 + + // Total 37 + 81 + 48 = 166 + + // Explanation for replaced bytes + + // Replaced parent Value -> 86 + // 1 for the flag option (but no flags) + // 1 for the enum type + // 1 for an empty option + // 1 for the count merk + // 9 for the count + // 32 for node hash + // 40 for the parent hook + // 2 byte for the value_size + assert_eq!( + cost, + OperationCost { + seek_count: 5, // todo: verify this + storage_cost: StorageCost { + added_bytes: 166, + replaced_bytes: 87, + removed_bytes: NoStorageRemoval + }, + storage_loaded_bytes: 162, // todo: verify this + hash_node_calls: 8, // todo: verify this + } + ); + } + #[test] fn test_one_insert_item_with_apple_flags_cost() { let grove_version = GroveVersion::latest(); diff --git a/grovedb/src/operations/is_empty_tree.rs b/grovedb/src/operations/is_empty_tree.rs index a007d219..4dec3abf 100644 --- a/grovedb/src/operations/is_empty_tree.rs +++ b/grovedb/src/operations/is_empty_tree.rs @@ -2,6 +2,7 @@ #[cfg(feature = "minimal")] use grovedb_costs::{cost_return_on_error, CostResult, CostsExt, OperationCost}; +use grovedb_merk::tree_type::TreeType; use grovedb_path::SubtreePath; #[cfg(feature = "minimal")] use grovedb_version::error::GroveVersionError; diff --git a/grovedb/src/operations/proof/generate.rs b/grovedb/src/operations/proof/generate.rs index d8bfc209..aff48637 100644 --- a/grovedb/src/operations/proof/generate.rs +++ b/grovedb/src/operations/proof/generate.rs @@ -313,7 +313,10 @@ impl GroveDb { } has_a_result_at_level |= true; } - Ok(Element::Tree(Some(_), _)) | Ok(Element::SumTree(Some(_), ..)) + Ok(Element::Tree(Some(_), _)) + | Ok(Element::SumTree(Some(_), ..)) + | Ok(Element::BigSumTree(Some(_), ..)) + | Ok(Element::CountTree(Some(_), ..)) if !done_with_results && query.has_subquery_or_matching_in_path_on_key(key) => { diff --git a/grovedb/src/operations/proof/verify.rs b/grovedb/src/operations/proof/verify.rs index 1ac09c8b..dca00bb9 100644 --- a/grovedb/src/operations/proof/verify.rs +++ b/grovedb/src/operations/proof/verify.rs @@ -308,7 +308,11 @@ impl GroveDb { println!("lower layer had key {}", hex_to_ascii(key)); } match element { - Element::Tree(Some(_), _) | Element::SumTree(Some(_), ..) => { + Element::Tree(Some(_), _) + | Element::SumTree(Some(_), ..) + | Element::BigSumTree(Some(_), ..) + | Element::CountTree(Some(_), ..) + | Element::CountSumTree(Some(_), ..) => { path.push(key); let lower_hash = Self::verify_layer_proof( lower_layer, @@ -337,6 +341,9 @@ impl GroveDb { } Element::Tree(None, _) | Element::SumTree(None, ..) + | Element::BigSumTree(None, ..) + | Element::CountTree(None, ..) + | Element::CountSumTree(None, ..) | Element::SumItem(..) | Element::Item(..) | Element::Reference(..) => { diff --git a/grovedb/src/replication.rs b/grovedb/src/replication.rs index 0996c324..000f97b2 100644 --- a/grovedb/src/replication.rs +++ b/grovedb/src/replication.rs @@ -2,7 +2,7 @@ mod state_sync_session; use std::pin::Pin; -use grovedb_merk::{tree::hash::CryptoHash, ChunkProducer}; +use grovedb_merk::{tree::hash::CryptoHash, tree_type::TreeType, ChunkProducer}; use grovedb_path::SubtreePath; use grovedb_version::{check_grovedb_v0, error::GroveVersionError, version::GroveVersion}; @@ -16,7 +16,7 @@ use crate::{Error, GroveDb, TransactionArg}; /// - `Option>`: The root key, which may be `None` if not present. /// - `bool`: Indicates whether the tree is a sum tree. /// - `Vec`: The chunk ID representing traversal instructions. -pub type ChunkIdentifier = (crate::SubtreePrefix, Option>, bool, Vec); +pub type ChunkIdentifier = (crate::SubtreePrefix, Option>, TreeType, Vec); pub const CURRENT_STATE_SYNC_VERSION: u16 = 1; @@ -82,7 +82,7 @@ impl GroveDb { } let root_app_hash = self.root_hash(transaction, grove_version).value?; - let (chunk_prefix, root_key, is_sum_tree, chunk_id) = + let (chunk_prefix, root_key, tree_type, chunk_id) = utils::decode_global_chunk_id(global_chunk_id, &root_app_hash)?; // TODO: Refactor this by writing fetch_chunk_inner (as only merk constructor @@ -92,7 +92,7 @@ impl GroveDb { .open_transactional_merk_by_prefix( chunk_prefix, root_key, - is_sum_tree, + tree_type, tx, None, grove_version, @@ -138,7 +138,7 @@ impl GroveDb { .open_non_transactional_merk_by_prefix( chunk_prefix, root_key, - is_sum_tree, + tree_type, None, grove_version, ) @@ -257,6 +257,7 @@ pub(crate) mod utils { use grovedb_merk::{ ed::Encode, proofs::{Decoder, Op}, + tree_type::TreeType, }; use crate::{replication::ChunkIdentifier, Error}; @@ -324,7 +325,7 @@ pub(crate) mod utils { if global_chunk_id == app_hash { let root_chunk_prefix_key: crate::SubtreePrefix = [0u8; 32]; - return Ok((root_chunk_prefix_key, None, false, vec![])); + return Ok((root_chunk_prefix_key, None, TreeType::NormalTree, vec![])); } let (chunk_prefix_key, remaining) = global_chunk_id.split_at(chunk_prefix_length); @@ -350,6 +351,8 @@ pub(crate) mod utils { } let (is_sum_tree, chunk_id) = remaining.split_at(is_sum_tree_length); + let tree_type = is_sum_tree[0].try_into()?; + let subtree_prefix: crate::SubtreePrefix = chunk_prefix_key .try_into() .map_err(|_| Error::CorruptedData("unable to construct subtree".to_string()))?; @@ -358,11 +361,11 @@ pub(crate) mod utils { Ok(( subtree_prefix, Some(root_key.to_vec()), - is_sum_tree[0] != 0, + tree_type, chunk_id.to_vec(), )) } else { - Ok((subtree_prefix, None, is_sum_tree[0] != 0, chunk_id.to_vec())) + Ok((subtree_prefix, None, tree_type, chunk_id.to_vec())) } } @@ -381,7 +384,7 @@ pub(crate) mod utils { pub fn encode_global_chunk_id( subtree_prefix: [u8; blake3::OUT_LEN], root_key_opt: Option>, - is_sum_tree: bool, + tree_type: TreeType, chunk_id: Vec, ) -> Vec { let mut res = vec![]; @@ -395,11 +398,7 @@ pub(crate) mod utils { res.push(0u8); } - let mut is_sum_tree_v = 0u8; - if is_sum_tree { - is_sum_tree_v = 1u8; - } - res.push(is_sum_tree_v); + res.push(tree_type as u8); res.extend(chunk_id.to_vec()); diff --git a/grovedb/src/replication/state_sync_session.rs b/grovedb/src/replication/state_sync_session.rs index 1ce41c4b..a8831f07 100644 --- a/grovedb/src/replication/state_sync_session.rs +++ b/grovedb/src/replication/state_sync_session.rs @@ -7,6 +7,7 @@ use std::{ use grovedb_merk::{ tree::{kv::ValueDefinedCostType, value_hash}, + tree_type::TreeType, CryptoHash, Restorer, }; use grovedb_path::SubtreePath; @@ -37,8 +38,8 @@ struct SubtreeStateSyncInfo<'db> { /// Tree root key root_key: Option>, - /// Is Sum tree? - is_sum_tree: bool, + /// The type of tree + tree_type: TreeType, /// Path of current tree current_path: Vec>, @@ -130,7 +131,7 @@ impl<'tx> SubtreeStateSyncInfo<'tx> { SubtreeStateSyncInfo { restorer, root_key: None, - is_sum_tree: false, + tree_type: TreeType::NormalTree, pending_chunks: Default::default(), current_path: vec![], num_processed_chunks: 0, @@ -246,14 +247,14 @@ impl<'db> MultiStateSyncSession<'db> { &*(tx as *const _) }; - if let Ok((merk, root_key, is_sum_tree)) = + if let Ok((merk, root_key, tree_type)) = db.open_merk_for_replication(path.clone(), transaction_ref, grove_version) { let restorer = Restorer::new(merk, hash, actual_hash); let mut sync_info = SubtreeStateSyncInfo::new(restorer); sync_info.pending_chunks.insert(vec![]); sync_info.root_key = root_key.clone(); - sync_info.is_sum_tree = is_sum_tree; + sync_info.tree_type = tree_type; sync_info.current_path = path.to_vec(); self.as_mut() .current_prefixes() @@ -261,7 +262,7 @@ impl<'db> MultiStateSyncSession<'db> { Ok(encode_global_chunk_id( chunk_prefix, root_key, - is_sum_tree, + tree_type, vec![], )) } else { @@ -367,7 +368,7 @@ impl<'db> MultiStateSyncSession<'db> { next_chunk_ids.push(encode_global_chunk_id( chunk_prefix, subtree_state_sync.root_key.clone(), - subtree_state_sync.is_sum_tree, + subtree_state_sync.tree_type, local_chunk_id.clone(), )); } diff --git a/grovedb/src/tests/count_sum_tree_tests.rs b/grovedb/src/tests/count_sum_tree_tests.rs new file mode 100644 index 00000000..f171aee0 --- /dev/null +++ b/grovedb/src/tests/count_sum_tree_tests.rs @@ -0,0 +1,556 @@ +//! Count sum tree tests + +#[cfg(test)] +mod count_sum_tree_tests { + use grovedb_merk::{ + tree::{kv::ValueDefinedCostType, AggregateData}, + TreeFeatureType, + }; + use grovedb_storage::StorageBatch; + use grovedb_version::version::GroveVersion; + + use crate::{ + batch::QualifiedGroveDbOp, + tests::{make_test_grovedb, TEST_LEAF}, + Element, + }; + + #[test] + fn test_count_sum_tree_behaves_like_regular_tree() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + + // Insert a CountSumTree + db.insert( + [TEST_LEAF].as_ref(), + b"count_sum_key", + Element::new_count_sum_tree(None), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert CountSumTree"); + + // Fetch the CountSumTree + let count_sum_tree = db + .get([TEST_LEAF].as_ref(), b"count_sum_key", None, grove_version) + .unwrap() + .expect("should get CountSumTree"); + assert!(matches!(count_sum_tree, Element::CountSumTree(..))); + + // Insert items into the CountSumTree + db.insert( + [TEST_LEAF, b"count_sum_key"].as_ref(), + b"item1", + Element::new_item(vec![1]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item1"); + + db.insert( + [TEST_LEAF, b"count_sum_key"].as_ref(), + b"item2", + Element::new_sum_item(3), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item2"); + + db.insert( + [TEST_LEAF, b"count_sum_key"].as_ref(), + b"item3", + Element::new_sum_item(5), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item3"); + + // Test proper item retrieval + let item1 = db + .get( + [TEST_LEAF, b"count_sum_key"].as_ref(), + b"item1", + None, + grove_version, + ) + .unwrap() + .expect("should get item1"); + assert_eq!(item1, Element::new_item(vec![1])); + + let item2 = db + .get( + [TEST_LEAF, b"count_sum_key"].as_ref(), + b"item2", + None, + grove_version, + ) + .unwrap() + .expect("should get item2"); + assert_eq!(item2, Element::new_sum_item(3)); + + let item3 = db + .get( + [TEST_LEAF, b"count_sum_key"].as_ref(), + b"item3", + None, + grove_version, + ) + .unwrap() + .expect("should get item3"); + assert_eq!(item3, Element::new_sum_item(5)); + + // Test aggregate data (count and sum) + let batch = StorageBatch::new(); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"count_sum_key"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open CountSumTree"); + + let aggregate_data = merk + .aggregate_data() + .expect("expected to get aggregate data"); + + // Assuming AggregateData::CountAndSum is implemented + assert_eq!(aggregate_data, AggregateData::CountAndSum(3, 8)); // 3 items: 1, 3, 5 + } + + #[test] + fn test_count_sum_tree_item_behaves_like_regular_item() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + + // Insert a CountSumTree with flags + db.insert( + [TEST_LEAF].as_ref(), + b"count_sum_key2", + Element::new_count_sum_tree_with_flags(None, None), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert CountSumTree with flags"); + + // Insert count and sum items + db.insert( + [TEST_LEAF, b"count_sum_key2"].as_ref(), + b"count_item", + Element::new_item(vec![2]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert count_item"); + + db.insert( + [TEST_LEAF, b"count_sum_key2"].as_ref(), + b"sum_item", + Element::new_sum_item(4), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert sum_item"); + + // Test aggregate data + let batch = StorageBatch::new(); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"count_sum_key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open CountSumTree with flags"); + + let aggregate_data = merk + .aggregate_data() + .expect("expected to get aggregate data"); + + assert_eq!(aggregate_data, AggregateData::CountAndSum(2, 4)); + } + + #[test] + fn test_homogenous_node_type_in_count_sum_trees_and_regular_trees() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + + // Insert a CountSumTree with initial sum and count values + db.insert( + [TEST_LEAF].as_ref(), + b"count_sum_key3", + Element::new_count_sum_tree_with_flags_and_sum_and_count_value(None, 0, 0, None), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert CountSumTree with sum and count values"); + + // Add count and sum items + db.insert( + [TEST_LEAF, b"count_sum_key3"].as_ref(), + b"item1", + Element::new_item(vec![10]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item1"); + + db.insert( + [TEST_LEAF, b"count_sum_key3"].as_ref(), + b"item2", + Element::new_sum_item(20), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item2"); + + // Add regular items + db.insert( + [TEST_LEAF, b"count_sum_key3"].as_ref(), + b"item3", + Element::new_item(vec![30]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item3"); + + db.insert( + [TEST_LEAF, b"count_sum_key3"].as_ref(), + b"item4", + Element::new_item(vec![40]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item4"); + + // Open merk and check all elements in it + let batch = StorageBatch::new(); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"count_sum_key3"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open CountSumTree"); + + // Verify feature types + let feature_type_item1 = merk + .get_feature_type( + b"item1", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected feature type"); + assert_eq!( + feature_type_item1, + TreeFeatureType::CountedSummedMerkNode(1, 0) + ); + + let feature_type_item2 = merk + .get_feature_type( + b"item2", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected feature type"); + assert_eq!( + feature_type_item2, + TreeFeatureType::CountedSummedMerkNode(1, 20) + ); + + let feature_type_item3 = merk + .get_feature_type( + b"item3", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected feature type"); + assert_eq!( + feature_type_item3, + TreeFeatureType::CountedSummedMerkNode(1, 0) + ); + + let feature_type_item4 = merk + .get_feature_type( + b"item4", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected feature type"); + assert_eq!( + feature_type_item4, + TreeFeatureType::CountedSummedMerkNode(1, 0) + ); + + // Verify aggregate data + let aggregate_data = merk + .aggregate_data() + .expect("expected to get aggregate data"); + assert_eq!(aggregate_data, AggregateData::CountAndSum(4, 20)); // 2 count, 10 + 20 sum + } + + #[test] + fn test_count_sum_tree_feature() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + + // Insert a regular tree + db.insert( + [TEST_LEAF].as_ref(), + b"regular_key", + Element::new_tree(None), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert regular tree"); + + let batch = StorageBatch::new(); + + // Aggregate data should be None for regular tree + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"regular_key"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open regular tree"); + assert_eq!( + merk.aggregate_data() + .expect("expected to get aggregate data"), + AggregateData::NoAggregateData + ); + + // Insert a CountSumTree + db.insert( + [TEST_LEAF].as_ref(), + b"count_sum_key4", + Element::new_count_sum_tree_with_flags_and_sum_and_count_value(None, 0, 0, None), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert CountSumTree"); + + let count_sum_tree = db + .get([TEST_LEAF].as_ref(), b"count_sum_key4", None, grove_version) + .unwrap() + .expect("should retrieve CountSumTree"); + assert!(matches!(count_sum_tree, Element::CountSumTree(..))); + // Note: Directly accessing count_sum_value_or_default is not shown in original + // code. Assuming you have a method like this to extract count and sum + // from the Element. If not, rely on aggregate_data as below. + + // Add count and sum items + db.insert( + [TEST_LEAF, b"count_sum_key4"].as_ref(), + b"count_item1", + Element::new_item(vec![1]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert count_item1"); + + db.insert( + [TEST_LEAF, b"count_sum_key4"].as_ref(), + b"sum_item1", + Element::new_sum_item(5), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert sum_item1"); + + // Verify aggregate data + let batch = StorageBatch::new(); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"count_sum_key4"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open CountSumTree"); + + let aggregate_data = merk + .aggregate_data() + .expect("expected to get aggregate data"); + assert_eq!(aggregate_data, AggregateData::CountAndSum(2, 5)); + } + + #[test] + fn test_count_sum_tree_with_batches() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + + // Prepare a batch of operations + let ops = vec![ + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec()], + b"count_sum_key6".to_vec(), + Element::new_count_sum_tree(None), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"count_sum_key6".to_vec()], + b"a".to_vec(), + Element::new_item(vec![10]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"count_sum_key6".to_vec()], + b"b".to_vec(), + Element::new_sum_item(20), + ), + ]; + + // Apply the batch + db.apply_batch(ops, None, None, grove_version) + .unwrap() + .expect("should apply batch"); + + // Open the CountSumTree and verify aggregate data + let batch = StorageBatch::new(); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"count_sum_key6"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open CountSumTree"); + + let aggregate_data = merk + .aggregate_data() + .expect("expected to get aggregate data"); + assert_eq!(aggregate_data, AggregateData::CountAndSum(2, 20)); + } + + #[test] + fn test_count_sum_tree_propagation() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + + // Insert a parent CountSumTree + db.insert( + [TEST_LEAF].as_ref(), + b"parent_count_sum", + Element::new_count_sum_tree(None), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert parent CountSumTree"); + + // Insert a child CountSumTree within the parent + db.insert( + [TEST_LEAF, b"parent_count_sum"].as_ref(), + b"child_count_sum", + Element::new_count_sum_tree(None), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert child CountSumTree"); + + // Insert items into the child CountSumTree + db.insert( + [TEST_LEAF, b"parent_count_sum", b"child_count_sum"].as_ref(), + b"item1", + Element::new_item(vec![5]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item1 into child"); + + db.insert( + [TEST_LEAF, b"parent_count_sum", b"child_count_sum"].as_ref(), + b"item2", + Element::new_sum_item(15), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item2 into child"); + + // Verify aggregate data of child + let batch = StorageBatch::new(); + let child_merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"parent_count_sum", b"child_count_sum"] + .as_ref() + .into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open child CountSumTree"); + + let child_aggregate = child_merk + .aggregate_data() + .expect("expected to get aggregate data"); + assert_eq!(child_aggregate, AggregateData::CountAndSum(2, 15)); + + // Verify aggregate data of parent + let parent_batch = StorageBatch::new(); + let parent_merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"parent_count_sum"].as_ref().into(), + Some(&parent_batch), + grove_version, + ) + .unwrap() + .expect("should open parent CountSumTree"); + + let parent_aggregate = parent_merk + .aggregate_data() + .expect("expected to get aggregate data"); + assert_eq!(parent_aggregate, AggregateData::CountAndSum(2, 15)); + } +} diff --git a/grovedb/src/tests/count_tree_tests.rs b/grovedb/src/tests/count_tree_tests.rs new file mode 100644 index 00000000..e4dffc06 --- /dev/null +++ b/grovedb/src/tests/count_tree_tests.rs @@ -0,0 +1,852 @@ +//! Count tree tests + +#[cfg(test)] +mod tests { + use assert_matches::assert_matches; + use grovedb_merk::{ + proofs::Query, + tree::{kv::ValueDefinedCostType, AggregateData}, + TreeFeatureType::{BasicMerkNode, CountedMerkNode}, + }; + use grovedb_storage::StorageBatch; + use grovedb_version::version::GroveVersion; + + use crate::{ + batch::QualifiedGroveDbOp, + reference_path::ReferencePathType, + tests::{make_test_grovedb, TEST_LEAF}, + Element, GroveDb, PathQuery, + }; + + #[test] + fn test_count_tree_behaves_like_regular_tree() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + db.insert( + [TEST_LEAF].as_ref(), + b"key", + Element::empty_count_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + + // Can fetch count tree + let count_tree = db + .get([TEST_LEAF].as_ref(), b"key", None, grove_version) + .unwrap() + .expect("should get count tree"); + assert!(matches!(count_tree, Element::CountTree(..))); + + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"innerkey", + Element::new_item(vec![1]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"innerkey2", + Element::new_item(vec![3]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"innerkey3", + Element::empty_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + + // Test proper item retrieval + let item = db + .get( + [TEST_LEAF, b"key"].as_ref(), + b"innerkey", + None, + grove_version, + ) + .unwrap() + .expect("should get item"); + assert_eq!(item, Element::new_item(vec![1])); + + // Test proof generation + let mut query = Query::new(); + query.insert_key(b"innerkey2".to_vec()); + + let path_query = PathQuery::new_unsized(vec![TEST_LEAF.to_vec(), b"key".to_vec()], query); + let proof = db + .prove_query(&path_query, None, grove_version) + .unwrap() + .expect("should generate proof"); + let (root_hash, result_set) = GroveDb::verify_query_raw(&proof, &path_query, grove_version) + .expect("should verify proof"); + assert_eq!( + root_hash, + db.grove_db.root_hash(None, grove_version).unwrap().unwrap() + ); + assert_eq!(result_set.len(), 1); + assert_eq!( + Element::deserialize(&result_set[0].value, grove_version) + .expect("should deserialize element"), + Element::new_item(vec![3]) + ); + } + + #[test] + fn test_homogenous_node_type_in_count_trees_and_regular_trees() { + let grove_version = GroveVersion::latest(); + // All elements in a count tree must have a count feature type + let db = make_test_grovedb(grove_version); + db.insert( + [TEST_LEAF].as_ref(), + b"key", + Element::empty_count_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + // Add count items + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"item1", + Element::new_item(vec![30]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"item2", + Element::new_item(vec![10]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + // Add regular items + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"item3", + Element::new_item(vec![10]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"item4", + Element::new_item(vec![15]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + + let batch = StorageBatch::new(); + + // Open merk and check all elements in it + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + let feature_type_node_1 = merk + .get_feature_type( + b"item1", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected feature type"); + let feature_type_node_2 = merk + .get_feature_type( + b"item2", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected feature type"); + let feature_type_node_3 = merk + .get_feature_type( + b"item3", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected feature type"); + let feature_type_node_4 = merk + .get_feature_type( + b"item4", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected feature type"); + + assert_eq!(feature_type_node_1, CountedMerkNode(1)); + assert_eq!(feature_type_node_2, CountedMerkNode(1)); + assert_eq!(feature_type_node_3, CountedMerkNode(1)); + assert_eq!(feature_type_node_4, CountedMerkNode(1)); + + // Perform the same test on regular trees + let db = make_test_grovedb(grove_version); + db.insert( + [TEST_LEAF].as_ref(), + b"key", + Element::empty_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"item1", + Element::new_item(vec![30]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"item2", + Element::new_item(vec![10]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert!(matches!( + merk.get_feature_type( + b"item1", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(BasicMerkNode) + )); + assert!(matches!( + merk.get_feature_type( + b"item2", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(BasicMerkNode) + )); + assert_eq!( + merk.aggregate_data().expect("expected to get count"), + AggregateData::NoAggregateData + ); + } + + #[test] + fn test_count_tree_feature() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + db.insert( + [TEST_LEAF].as_ref(), + b"key", + Element::empty_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + + let batch = StorageBatch::new(); + + // Sum should be non for non count tree + // TODO: change interface to retrieve element directly + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get count"), + AggregateData::NoAggregateData + ); + + // Add count tree + db.insert( + [TEST_LEAF].as_ref(), + b"key2", + Element::empty_count_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert count tree"); + let count_tree = db + .get([TEST_LEAF].as_ref(), b"key2", None, grove_version) + .unwrap() + .expect("should retrieve tree"); + assert_eq!(count_tree.count_value_or_default(), 0); + + // Add count items to the count tree + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item1", + Element::new_item(vec![30]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + // TODO: change interface to retrieve element directly + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get count"), + AggregateData::Count(1) + ); + + // Add more count items + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item2", + Element::new_item(vec![3]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item3", + Element::new_item(vec![3]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get count"), + AggregateData::Count(3) + ); + + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item4", + Element::new_item(vec![29]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get count"), + AggregateData::Count(4) + ); + + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item2", + Element::new_item(vec![10]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item3", + Element::new_item(vec![3]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get count"), + AggregateData::Count(4) + ); + + db.delete( + [TEST_LEAF, b"key2"].as_ref(), + b"item4", + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to delete"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get count"), + AggregateData::Count(3) + ); + } + + #[test] + fn test_count_tree_propagation() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + // Tree + // count_key: CountTree + // / \ + // countitem3 tree2: CountTree + // + // tree2 : CountTree + // / + // item1 item2 item3 ref1 + db.insert( + [TEST_LEAF].as_ref(), + b"count_key", + Element::empty_count_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"count_key"].as_ref(), + b"tree2", + Element::empty_count_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"count_key"].as_ref(), + b"countitem3", + Element::new_item(vec![3]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"count_key", b"tree2"].as_ref(), + b"item1", + Element::new_item(vec![2]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"count_key", b"tree2"].as_ref(), + b"item2", + Element::new_item(vec![5]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"count_key", b"tree2"].as_ref(), + b"item3", + Element::new_item(vec![10]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"count_key", b"tree2"].as_ref(), + b"ref1", + Element::new_reference(ReferencePathType::AbsolutePathReference(vec![ + TEST_LEAF.to_vec(), + b"count_key".to_vec(), + b"tree2".to_vec(), + b"item1".to_vec(), + ])), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + + let count_tree = db + .get([TEST_LEAF].as_ref(), b"count_key", None, grove_version) + .unwrap() + .expect("should fetch tree"); + assert_eq!(count_tree.count_value_or_default(), 5); + + let batch = StorageBatch::new(); + + // Assert node feature types + let test_leaf_merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + let root_tree_feature_type = test_leaf_merk + .get_feature_type( + b"count_key", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("tree feature type"); + + assert_matches!(root_tree_feature_type, BasicMerkNode); + + let parent_count_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"count_key"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + let count_tree_feature_type = parent_count_tree + .get_feature_type( + b"tree2", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("tree feature type"); + assert_matches!(count_tree_feature_type, CountedMerkNode(4)); + + let child_count_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"count_key", b"tree2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + let count_tree_feature_type = child_count_tree + .get_feature_type( + b"item1", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("tree feature type"); + + assert_matches!(count_tree_feature_type, CountedMerkNode(1)); + + let count_tree_feature_type = child_count_tree + .get_feature_type( + b"item2", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("tree feature type"); + + assert_matches!(count_tree_feature_type, CountedMerkNode(1)); + + let count_tree_feature_type = child_count_tree + .get_feature_type( + b"item3", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("tree feature type"); + + assert_matches!(count_tree_feature_type, CountedMerkNode(1)); + + let count_tree_feature_type = child_count_tree + .get_feature_type( + b"ref1", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("tree feature type"); + + assert_matches!(count_tree_feature_type, CountedMerkNode(1)); + } + + #[test] + fn test_count_tree_with_batches() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + let ops = vec![ + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec()], + b"key1".to_vec(), + Element::empty_count_tree(), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"a".to_vec(), + Element::new_item(vec![214]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"b".to_vec(), + Element::new_item(vec![10]), + ), + ]; + db.apply_batch(ops, None, None, grove_version) + .unwrap() + .expect("should apply batch"); + + let batch = StorageBatch::new(); + let count_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key1"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + + let tree_feature_type_a = count_tree + .get_feature_type( + b"a", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected tree feature type"); + + let tree_feature_type_b = count_tree + .get_feature_type( + b"a", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected tree feature type"); + + assert_matches!(tree_feature_type_a, CountedMerkNode(1)); + assert_matches!(tree_feature_type_b, CountedMerkNode(1)); + + // Create new batch to use existing tree + let ops = vec![QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"c".to_vec(), + Element::new_item(vec![10]), + )]; + db.apply_batch(ops, None, None, grove_version) + .unwrap() + .expect("should apply batch"); + + let batch = StorageBatch::new(); + let count_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key1"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + let tree_feature_type_c = count_tree + .get_feature_type( + b"c", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected tree feature type"); + assert_matches!(tree_feature_type_c, CountedMerkNode(1)); + assert_eq!( + count_tree.aggregate_data().expect("expected to get count"), + AggregateData::Count(3) + ); + + // Test propagation + // Add a new count tree with its own count items, should affect count of + // original tree + let ops = vec![ + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"d".to_vec(), + Element::empty_count_tree(), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"d".to_vec()], + b"first".to_vec(), + Element::new_item(vec![2]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"d".to_vec()], + b"second".to_vec(), + Element::new_item(vec![4]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"e".to_vec(), + Element::empty_count_tree(), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"e".to_vec()], + b"first".to_vec(), + Element::new_item(vec![3]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"e".to_vec()], + b"second".to_vec(), + Element::new_item(vec![4]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"e".to_vec()], + b"third".to_vec(), + Element::empty_count_tree(), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![ + TEST_LEAF.to_vec(), + b"key1".to_vec(), + b"e".to_vec(), + b"third".to_vec(), + ], + b"a".to_vec(), + Element::new_item(vec![5]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![ + TEST_LEAF.to_vec(), + b"key1".to_vec(), + b"e".to_vec(), + b"third".to_vec(), + ], + b"b".to_vec(), + Element::new_item(vec![5]), + ), + ]; + db.apply_batch(ops, None, None, grove_version) + .unwrap() + .expect("should apply batch"); + + let batch = StorageBatch::new(); + let count_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key1"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + count_tree.aggregate_data().expect("expected to get count"), + AggregateData::Count(9) + ); + } +} diff --git a/grovedb/src/tests/mod.rs b/grovedb/src/tests/mod.rs index 41263669..a7f01eb7 100644 --- a/grovedb/src/tests/mod.rs +++ b/grovedb/src/tests/mod.rs @@ -6,6 +6,8 @@ mod query_tests; mod sum_tree_tests; +mod count_sum_tree_tests; +mod count_tree_tests; mod tree_hashes_tests; use std::{ @@ -3213,14 +3215,14 @@ mod tests { let storage = db.db.get_storage_context(EMPTY_PATH, None).unwrap(); let root_merk = Merk::open_base( storage, - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) .unwrap() .expect("expected to get root merk"); let (_, root_key, _) = root_merk - .root_hash_key_and_sum() + .root_hash_key_and_aggregate_data() .unwrap() .expect("expected to get root hash, key and sum"); assert!(root_key.is_some()) @@ -3318,7 +3320,7 @@ mod tests { let subtree = Merk::open_layered_with_root_key( subtree_storage, Some(b"key3".to_vec()), - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -3367,7 +3369,7 @@ mod tests { let subtree = Merk::open_layered_with_root_key( subtree_storage, Some(b"key4".to_vec()), - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) @@ -3387,7 +3389,7 @@ mod tests { let subtree = Merk::open_layered_with_root_key( subtree_storage, Some(b"key3".to_vec()), - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), grove_version, ) diff --git a/grovedb/src/tests/sum_tree_tests.rs b/grovedb/src/tests/sum_tree_tests.rs index b255f653..777fcb45 100644 --- a/grovedb/src/tests/sum_tree_tests.rs +++ b/grovedb/src/tests/sum_tree_tests.rs @@ -1,730 +1,285 @@ //! Sum tree tests -use grovedb_merk::{ - proofs::Query, - tree::kv::ValueDefinedCostType, - TreeFeatureType::{BasicMerkNode, SummedMerkNode}, -}; -use grovedb_storage::StorageBatch; -use grovedb_version::version::GroveVersion; - -use crate::{ - batch::QualifiedGroveDbOp, - reference_path::ReferencePathType, - tests::{make_test_grovedb, TEST_LEAF}, - Element, Error, GroveDb, PathQuery, -}; - -#[test] -fn test_sum_tree_behaves_like_regular_tree() { - let grove_version = GroveVersion::latest(); - let db = make_test_grovedb(grove_version); - db.insert( - [TEST_LEAF].as_ref(), - b"key", - Element::empty_sum_tree(), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert tree"); - - // Can fetch sum tree - let sum_tree = db - .get([TEST_LEAF].as_ref(), b"key", None, grove_version) - .unwrap() - .expect("should get tree"); - assert!(matches!(sum_tree, Element::SumTree(..))); - - db.insert( - [TEST_LEAF, b"key"].as_ref(), - b"innerkey", - Element::new_item(vec![1]), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - db.insert( - [TEST_LEAF, b"key"].as_ref(), - b"innerkey2", - Element::new_item(vec![3]), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - db.insert( - [TEST_LEAF, b"key"].as_ref(), - b"innerkey3", - Element::empty_tree(), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - - // Test proper item retrieval - let item = db - .get( +#[cfg(test)] +mod tests { + use grovedb_merk::{ + proofs::Query, + tree::{kv::ValueDefinedCostType, AggregateData}, + TreeFeatureType::{BasicMerkNode, BigSummedMerkNode, SummedMerkNode}, + }; + use grovedb_storage::StorageBatch; + use grovedb_version::version::GroveVersion; + + use crate::{ + batch::QualifiedGroveDbOp, + element::SumValue, + reference_path::ReferencePathType, + tests::{make_test_grovedb, TEST_LEAF}, + Element, Error, GroveDb, PathQuery, + }; + + #[test] + fn test_sum_tree_behaves_like_regular_tree() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + db.insert( + [TEST_LEAF].as_ref(), + b"key", + Element::empty_sum_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + + // Can fetch sum tree + let sum_tree = db + .get([TEST_LEAF].as_ref(), b"key", None, grove_version) + .unwrap() + .expect("should get tree"); + assert!(matches!(sum_tree, Element::SumTree(..))); + + db.insert( [TEST_LEAF, b"key"].as_ref(), b"innerkey", + Element::new_item(vec![1]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"innerkey2", + Element::new_item(vec![3]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"innerkey3", + Element::empty_tree(), + None, None, grove_version, ) .unwrap() - .expect("should get item"); - assert_eq!(item, Element::new_item(vec![1])); + .expect("should insert item"); - // Test proof generation - let mut query = Query::new(); - query.insert_key(b"innerkey2".to_vec()); + // Test proper item retrieval + let item = db + .get( + [TEST_LEAF, b"key"].as_ref(), + b"innerkey", + None, + grove_version, + ) + .unwrap() + .expect("should get item"); + assert_eq!(item, Element::new_item(vec![1])); - let path_query = PathQuery::new_unsized(vec![TEST_LEAF.to_vec(), b"key".to_vec()], query); - let proof = db - .prove_query(&path_query, None, grove_version) - .unwrap() - .expect("should generate proof"); - let (root_hash, result_set) = - GroveDb::verify_query_raw(&proof, &path_query, grove_version).expect("should verify proof"); - assert_eq!( - root_hash, - db.grove_db.root_hash(None, grove_version).unwrap().unwrap() - ); - assert_eq!(result_set.len(), 1); - assert_eq!( - Element::deserialize(&result_set[0].value, grove_version) - .expect("should deserialize element"), - Element::new_item(vec![3]) - ); -} + // Test proof generation + let mut query = Query::new(); + query.insert_key(b"innerkey2".to_vec()); -#[test] -fn test_sum_item_behaves_like_regular_item() { - let grove_version = GroveVersion::latest(); - let db = make_test_grovedb(grove_version); - db.insert( - [TEST_LEAF].as_ref(), - b"sumkey", - Element::empty_sum_tree(), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert tree"); - db.insert( - [TEST_LEAF, b"sumkey"].as_ref(), - b"k1", - Element::new_item(vec![1]), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert tree"); - db.insert( - [TEST_LEAF, b"sumkey"].as_ref(), - b"k2", - Element::new_sum_item(5), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert tree"); - db.insert( - [TEST_LEAF, b"sumkey"].as_ref(), - b"k3", - Element::empty_tree(), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert tree"); - - // Test proper item retrieval - let item = db - .get([TEST_LEAF, b"sumkey"].as_ref(), b"k2", None, grove_version) - .unwrap() - .expect("should get item"); - assert_eq!(item, Element::new_sum_item(5)); - - // Test proof generation - let mut query = Query::new(); - query.insert_key(b"k2".to_vec()); - - let path_query = PathQuery::new_unsized(vec![TEST_LEAF.to_vec(), b"sumkey".to_vec()], query); - let proof = db - .prove_query(&path_query, None, grove_version) - .unwrap() - .expect("should generate proof"); - let (root_hash, result_set) = - GroveDb::verify_query_raw(&proof, &path_query, grove_version).expect("should verify proof"); - assert_eq!( - root_hash, - db.grove_db.root_hash(None, grove_version).unwrap().unwrap() - ); - assert_eq!(result_set.len(), 1); - let element_from_proof = Element::deserialize(&result_set[0].value, grove_version) - .expect("should deserialize element"); - assert_eq!(element_from_proof, Element::new_sum_item(5)); - assert_eq!(element_from_proof.sum_value_or_default(), 5); -} + let path_query = PathQuery::new_unsized(vec![TEST_LEAF.to_vec(), b"key".to_vec()], query); + let proof = db + .prove_query(&path_query, None, grove_version) + .unwrap() + .expect("should generate proof"); + let (root_hash, result_set) = GroveDb::verify_query_raw(&proof, &path_query, grove_version) + .expect("should verify proof"); + assert_eq!( + root_hash, + db.grove_db.root_hash(None, grove_version).unwrap().unwrap() + ); + assert_eq!(result_set.len(), 1); + assert_eq!( + Element::deserialize(&result_set[0].value, grove_version) + .expect("should deserialize element"), + Element::new_item(vec![3]) + ); + } -#[test] -fn test_cannot_insert_sum_item_in_regular_tree() { - let grove_version = GroveVersion::latest(); - let db = make_test_grovedb(grove_version); - db.insert( - [TEST_LEAF].as_ref(), - b"sumkey", - Element::empty_tree(), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert tree"); - assert!(matches!( + #[test] + fn test_sum_item_behaves_like_regular_item() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + db.insert( + [TEST_LEAF].as_ref(), + b"sumkey", + Element::empty_sum_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); db.insert( [TEST_LEAF, b"sumkey"].as_ref(), b"k1", + Element::new_item(vec![1]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"sumkey"].as_ref(), + b"k2", Element::new_sum_item(5), None, None, - grove_version + grove_version, ) - .unwrap(), - Err(Error::InvalidInput("cannot add sum item to non sum tree")) - )); -} - -#[test] -fn test_homogenous_node_type_in_sum_trees_and_regular_trees() { - let grove_version = GroveVersion::latest(); - // All elements in a sum tree must have a summed feature type - let db = make_test_grovedb(grove_version); - db.insert( - [TEST_LEAF].as_ref(), - b"key", - Element::empty_sum_tree(), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert tree"); - // Add sum items - db.insert( - [TEST_LEAF, b"key"].as_ref(), - b"item1", - Element::new_sum_item(30), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - db.insert( - [TEST_LEAF, b"key"].as_ref(), - b"item2", - Element::new_sum_item(10), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - // Add regular items - db.insert( - [TEST_LEAF, b"key"].as_ref(), - b"item3", - Element::new_item(vec![10]), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - db.insert( - [TEST_LEAF, b"key"].as_ref(), - b"item4", - Element::new_item(vec![15]), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - - let batch = StorageBatch::new(); - - // Open merk and check all elements in it - let merk = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key"].as_ref().into(), - Some(&batch), - grove_version, - ) - .unwrap() - .expect("should open tree"); - assert!(matches!( - merk.get_feature_type( - b"item1", - true, - None::<&fn(&[u8], &GroveVersion) -> Option>, - grove_version + .unwrap() + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"sumkey"].as_ref(), + b"k3", + Element::empty_tree(), + None, + None, + grove_version, ) .unwrap() - .expect("node should exist"), - Some(SummedMerkNode(30)) - )); - assert!(matches!( - merk.get_feature_type( - b"item2", - true, - None::<&fn(&[u8], &GroveVersion) -> Option>, - grove_version + .expect("should insert tree"); + + // Test proper item retrieval + let item = db + .get([TEST_LEAF, b"sumkey"].as_ref(), b"k2", None, grove_version) + .unwrap() + .expect("should get item"); + assert_eq!(item, Element::new_sum_item(5)); + + // Test proof generation + let mut query = Query::new(); + query.insert_key(b"k2".to_vec()); + + let path_query = + PathQuery::new_unsized(vec![TEST_LEAF.to_vec(), b"sumkey".to_vec()], query); + let proof = db + .prove_query(&path_query, None, grove_version) + .unwrap() + .expect("should generate proof"); + let (root_hash, result_set) = GroveDb::verify_query_raw(&proof, &path_query, grove_version) + .expect("should verify proof"); + assert_eq!( + root_hash, + db.grove_db.root_hash(None, grove_version).unwrap().unwrap() + ); + assert_eq!(result_set.len(), 1); + let element_from_proof = Element::deserialize(&result_set[0].value, grove_version) + .expect("should deserialize element"); + assert_eq!(element_from_proof, Element::new_sum_item(5)); + assert_eq!(element_from_proof.sum_value_or_default(), 5); + } + + #[test] + fn test_cannot_insert_sum_item_in_regular_tree() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + db.insert( + [TEST_LEAF].as_ref(), + b"sumkey", + Element::empty_tree(), + None, + None, + grove_version, ) .unwrap() - .expect("node should exist"), - Some(SummedMerkNode(10)) - )); - assert!(matches!( - merk.get_feature_type( - b"item3", - true, - None::<&fn(&[u8], &GroveVersion) -> Option>, - grove_version + .expect("should insert tree"); + assert!(matches!( + db.insert( + [TEST_LEAF, b"sumkey"].as_ref(), + b"k1", + Element::new_sum_item(5), + None, + None, + grove_version + ) + .unwrap(), + Err(Error::InvalidInput("cannot add sum item to non sum tree")) + )); + } + + #[test] + fn test_homogenous_node_type_in_sum_trees_and_regular_trees() { + let grove_version = GroveVersion::latest(); + // All elements in a sum tree must have a summed feature type + let db = make_test_grovedb(grove_version); + db.insert( + [TEST_LEAF].as_ref(), + b"key", + Element::empty_sum_tree(), + None, + None, + grove_version, ) .unwrap() - .expect("node should exist"), - Some(SummedMerkNode(0)) - )); - assert!(matches!( - merk.get_feature_type( - b"item4", - true, - None::<&fn(&[u8], &GroveVersion) -> Option>, - grove_version - ) - .unwrap() - .expect("node should exist"), - Some(SummedMerkNode(0)) - )); - assert_eq!(merk.sum().expect("expected to get sum"), Some(40)); - - // Perform the same test on regular trees - let db = make_test_grovedb(grove_version); - db.insert( - [TEST_LEAF].as_ref(), - b"key", - Element::empty_tree(), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert tree"); - db.insert( - [TEST_LEAF, b"key"].as_ref(), - b"item1", - Element::new_item(vec![30]), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - db.insert( - [TEST_LEAF, b"key"].as_ref(), - b"item2", - Element::new_item(vec![10]), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - - let merk = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key"].as_ref().into(), - Some(&batch), - grove_version, - ) - .unwrap() - .expect("should open tree"); - assert!(matches!( - merk.get_feature_type( + .expect("should insert tree"); + // Add sum items + db.insert( + [TEST_LEAF, b"key"].as_ref(), b"item1", - true, - Some(&Element::value_defined_cost_for_serialized_value), - grove_version + Element::new_sum_item(30), + None, + None, + grove_version, ) .unwrap() - .expect("node should exist"), - Some(BasicMerkNode) - )); - assert!(matches!( - merk.get_feature_type( + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), b"item2", - true, - Some(&Element::value_defined_cost_for_serialized_value), - grove_version + Element::new_sum_item(10), + None, + None, + grove_version, ) .unwrap() - .expect("node should exist"), - Some(BasicMerkNode) - )); - assert_eq!(merk.sum().expect("expected to get sum"), None); -} - -#[test] -fn test_sum_tree_feature() { - let grove_version = GroveVersion::latest(); - let db = make_test_grovedb(grove_version); - db.insert( - [TEST_LEAF].as_ref(), - b"key", - Element::empty_tree(), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert tree"); - - let batch = StorageBatch::new(); - - // Sum should be non for non sum tree - // TODO: change interface to retrieve element directly - let merk = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key"].as_ref().into(), - Some(&batch), - grove_version, - ) - .unwrap() - .expect("should open tree"); - assert_eq!(merk.sum().expect("expected to get sum"), None); - - // Add sum tree - db.insert( - [TEST_LEAF].as_ref(), - b"key2", - Element::empty_sum_tree(), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert sum tree"); - let sum_tree = db - .get([TEST_LEAF].as_ref(), b"key2", None, grove_version) - .unwrap() - .expect("should retrieve tree"); - assert_eq!(sum_tree.sum_value_or_default(), 0); - - // Add sum items to the sum tree - db.insert( - [TEST_LEAF, b"key2"].as_ref(), - b"item1", - Element::new_sum_item(30), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - // TODO: change interface to retrieve element directly - let merk = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key2"].as_ref().into(), - Some(&batch), - grove_version, - ) - .unwrap() - .expect("should open tree"); - assert_eq!(merk.sum().expect("expected to get sum"), Some(30)); - - // Add more sum items - db.insert( - [TEST_LEAF, b"key2"].as_ref(), - b"item2", - Element::new_sum_item(-10), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - db.insert( - [TEST_LEAF, b"key2"].as_ref(), - b"item3", - Element::new_sum_item(50), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - let merk = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key2"].as_ref().into(), - Some(&batch), - grove_version, - ) - .unwrap() - .expect("should open tree"); - assert_eq!(merk.sum().expect("expected to get sum"), Some(70)); // 30 - 10 + 50 = 70 - - // Add non sum items, result should remain the same - db.insert( - [TEST_LEAF, b"key2"].as_ref(), - b"item4", - Element::new_item(vec![29]), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - let merk = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key2"].as_ref().into(), - Some(&batch), - grove_version, - ) - .unwrap() - .expect("should open tree"); - assert_eq!(merk.sum().expect("expected to get sum"), Some(70)); - - // Update existing sum items - db.insert( - [TEST_LEAF, b"key2"].as_ref(), - b"item2", - Element::new_sum_item(10), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - db.insert( - [TEST_LEAF, b"key2"].as_ref(), - b"item3", - Element::new_sum_item(-100), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - let merk = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key2"].as_ref().into(), - Some(&batch), - grove_version, - ) - .unwrap() - .expect("should open tree"); - assert_eq!(merk.sum().expect("expected to get sum"), Some(-60)); // 30 + 10 - 100 = -60 - - // We can not replace a normal item with a sum item, so let's delete it first - db.delete( - [TEST_LEAF, b"key2"].as_ref(), - b"item4", - None, - None, - grove_version, - ) - .unwrap() - .expect("expected to delete"); - // Use a large value - db.insert( - [TEST_LEAF, b"key2"].as_ref(), - b"item4", - Element::new_sum_item(10000000), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - let merk = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key2"].as_ref().into(), - Some(&batch), - grove_version, - ) - .unwrap() - .expect("should open tree"); - assert_eq!(merk.sum().expect("expected to get sum"), Some(9999940)); // 30 + - // 10 - - // 100 + - // 10000000 - - // TODO: Test out overflows -} - -#[test] -fn test_sum_tree_propagation() { - let grove_version = GroveVersion::latest(); - let db = make_test_grovedb(grove_version); - // Tree - // SumTree - // SumTree - // Item1 - // SumItem1 - // SumItem2 - // SumItem3 - db.insert( - [TEST_LEAF].as_ref(), - b"key", - Element::empty_sum_tree(), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert tree"); - db.insert( - [TEST_LEAF, b"key"].as_ref(), - b"tree2", - Element::empty_sum_tree(), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert tree"); - db.insert( - [TEST_LEAF, b"key"].as_ref(), - b"sumitem3", - Element::new_sum_item(20), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert tree"); - db.insert( - [TEST_LEAF, b"key", b"tree2"].as_ref(), - b"item1", - Element::new_item(vec![2]), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - db.insert( - [TEST_LEAF, b"key", b"tree2"].as_ref(), - b"sumitem1", - Element::new_sum_item(5), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - db.insert( - [TEST_LEAF, b"key", b"tree2"].as_ref(), - b"sumitem2", - Element::new_sum_item(10), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - db.insert( - [TEST_LEAF, b"key", b"tree2"].as_ref(), - b"item2", - Element::new_reference(ReferencePathType::AbsolutePathReference(vec![ - TEST_LEAF.to_vec(), - b"key".to_vec(), - b"tree2".to_vec(), - b"sumitem1".to_vec(), - ])), - None, - None, - grove_version, - ) - .unwrap() - .expect("should insert item"); - - let sum_tree = db - .get([TEST_LEAF].as_ref(), b"key", None, grove_version) - .unwrap() - .expect("should fetch tree"); - assert_eq!(sum_tree.sum_value_or_default(), 35); - - let batch = StorageBatch::new(); - - // Assert node feature types - let test_leaf_merk = db - .open_non_transactional_merk_at_path( - [TEST_LEAF].as_ref().into(), - Some(&batch), - grove_version, - ) - .unwrap() - .expect("should open tree"); - assert!(matches!( - test_leaf_merk - .get_feature_type( - b"key", - true, - Some(&Element::value_defined_cost_for_serialized_value), - grove_version - ) - .unwrap() - .expect("node should exist"), - Some(BasicMerkNode) - )); - - let parent_sum_tree = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key"].as_ref().into(), - Some(&batch), + .expect("should insert item"); + // Add regular items + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"item3", + Element::new_item(vec![10]), + None, + None, grove_version, ) .unwrap() - .expect("should open tree"); - assert!(matches!( - parent_sum_tree - .get_feature_type( - b"tree2", - true, - Some(&Element::value_defined_cost_for_serialized_value), - grove_version - ) - .unwrap() - .expect("node should exist"), - Some(SummedMerkNode(15)) /* 15 because the child sum tree has one sum item of - * value 5 and - * another of value 10 */ - )); - - let child_sum_tree = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key", b"tree2"].as_ref().into(), - Some(&batch), + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"item4", + Element::new_item(vec![15]), + None, + None, grove_version, ) .unwrap() - .expect("should open tree"); - assert!(matches!( - child_sum_tree - .get_feature_type( + .expect("should insert item"); + + let batch = StorageBatch::new(); + + // Open merk and check all elements in it + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert!(matches!( + merk.get_feature_type( b"item1", true, None::<&fn(&[u8], &GroveVersion) -> Option>, @@ -732,213 +287,1383 @@ fn test_sum_tree_propagation() { ) .unwrap() .expect("node should exist"), - Some(SummedMerkNode(0)) - )); - assert!(matches!( - child_sum_tree - .get_feature_type( - b"sumitem1", + Some(SummedMerkNode(30)) + )); + assert!(matches!( + merk.get_feature_type( + b"item2", true, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version ) .unwrap() .expect("node should exist"), - Some(SummedMerkNode(5)) - )); - assert!(matches!( - child_sum_tree - .get_feature_type( - b"sumitem2", + Some(SummedMerkNode(10)) + )); + assert!(matches!( + merk.get_feature_type( + b"item3", true, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version ) .unwrap() .expect("node should exist"), - Some(SummedMerkNode(10)) - )); - - // TODO: should references take the sum of the referenced element?? - assert!(matches!( - child_sum_tree - .get_feature_type( - b"item2", + Some(SummedMerkNode(0)) + )); + assert!(matches!( + merk.get_feature_type( + b"item4", true, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version ) .unwrap() .expect("node should exist"), - Some(SummedMerkNode(0)) - )); -} + Some(SummedMerkNode(0)) + )); + assert_eq!( + merk.aggregate_data() + .expect("expected to get sum") + .as_sum_i64(), + 40 + ); -#[test] -fn test_sum_tree_with_batches() { - let grove_version = GroveVersion::latest(); - let db = make_test_grovedb(grove_version); - let ops = vec![ - QualifiedGroveDbOp::insert_or_replace_op( - vec![TEST_LEAF.to_vec()], - b"key1".to_vec(), - Element::empty_sum_tree(), - ), - QualifiedGroveDbOp::insert_or_replace_op( - vec![TEST_LEAF.to_vec(), b"key1".to_vec()], - b"a".to_vec(), - Element::new_item(vec![214]), - ), - QualifiedGroveDbOp::insert_or_replace_op( - vec![TEST_LEAF.to_vec(), b"key1".to_vec()], - b"b".to_vec(), - Element::new_sum_item(10), - ), - ]; - db.apply_batch(ops, None, None, grove_version) + // Perform the same test on regular trees + let db = make_test_grovedb(grove_version); + db.insert( + [TEST_LEAF].as_ref(), + b"key", + Element::empty_tree(), + None, + None, + grove_version, + ) .unwrap() - .expect("should apply batch"); - - let batch = StorageBatch::new(); - let sum_tree = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key1"].as_ref().into(), - Some(&batch), + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"item1", + Element::new_item(vec![30]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"item2", + Element::new_item(vec![10]), + None, + None, grove_version, ) .unwrap() - .expect("should open tree"); + .expect("should insert item"); - assert!(matches!( - sum_tree - .get_feature_type( - b"a", + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert!(matches!( + merk.get_feature_type( + b"item1", true, Some(&Element::value_defined_cost_for_serialized_value), grove_version ) .unwrap() .expect("node should exist"), - Some(SummedMerkNode(0)) - )); - assert!(matches!( - sum_tree - .get_feature_type( - b"b", + Some(BasicMerkNode) + )); + assert!(matches!( + merk.get_feature_type( + b"item2", true, Some(&Element::value_defined_cost_for_serialized_value), grove_version ) .unwrap() .expect("node should exist"), - Some(SummedMerkNode(10)) - )); + Some(BasicMerkNode) + )); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::NoAggregateData + ); + } - // Create new batch to use existing tree - let ops = vec![QualifiedGroveDbOp::insert_or_replace_op( - vec![TEST_LEAF.to_vec(), b"key1".to_vec()], - b"c".to_vec(), - Element::new_sum_item(10), - )]; - db.apply_batch(ops, None, None, grove_version) + #[test] + fn test_sum_tree_feature() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + db.insert( + [TEST_LEAF].as_ref(), + b"key", + Element::empty_tree(), + None, + None, + grove_version, + ) .unwrap() - .expect("should apply batch"); + .expect("should insert tree"); + + let batch = StorageBatch::new(); + + // Sum should be non for non sum tree + // TODO: change interface to retrieve element directly + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::NoAggregateData + ); - let batch = StorageBatch::new(); - let sum_tree = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key1"].as_ref().into(), - Some(&batch), + // Add sum tree + db.insert( + [TEST_LEAF].as_ref(), + b"key2", + Element::empty_sum_tree(), + None, + None, grove_version, ) .unwrap() - .expect("should open tree"); - assert!(matches!( - sum_tree - .get_feature_type( - b"c", - true, - None::<&fn(&[u8], &GroveVersion) -> Option>, - grove_version + .expect("should insert sum tree"); + let sum_tree = db + .get([TEST_LEAF].as_ref(), b"key2", None, grove_version) + .unwrap() + .expect("should retrieve tree"); + assert_eq!(sum_tree.sum_value_or_default(), 0); + + // Add sum items to the sum tree + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item1", + Element::new_sum_item(30), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + // TODO: change interface to retrieve element directly + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, ) .unwrap() - .expect("node should exist"), - Some(SummedMerkNode(10)) - )); - assert_eq!(sum_tree.sum().expect("expected to get sum"), Some(20)); - - // Test propagation - // Add a new sum tree with its own sum items, should affect sum of original - // tree - let ops = vec![ - QualifiedGroveDbOp::insert_or_replace_op( - vec![TEST_LEAF.to_vec(), b"key1".to_vec()], - b"d".to_vec(), - Element::empty_sum_tree(), - ), - QualifiedGroveDbOp::insert_or_replace_op( - vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"d".to_vec()], - b"first".to_vec(), - Element::new_sum_item(4), - ), - QualifiedGroveDbOp::insert_or_replace_op( - vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"d".to_vec()], - b"second".to_vec(), - Element::new_item(vec![4]), - ), - QualifiedGroveDbOp::insert_or_replace_op( - vec![TEST_LEAF.to_vec(), b"key1".to_vec()], - b"e".to_vec(), - Element::empty_sum_tree(), - ), - QualifiedGroveDbOp::insert_or_replace_op( - vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"e".to_vec()], - b"first".to_vec(), - Element::new_sum_item(12), - ), - QualifiedGroveDbOp::insert_or_replace_op( - vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"e".to_vec()], - b"second".to_vec(), - Element::new_item(vec![4]), - ), - QualifiedGroveDbOp::insert_or_replace_op( - vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"e".to_vec()], - b"third".to_vec(), - Element::empty_sum_tree(), - ), - QualifiedGroveDbOp::insert_or_replace_op( - vec![ - TEST_LEAF.to_vec(), - b"key1".to_vec(), - b"e".to_vec(), - b"third".to_vec(), - ], - b"a".to_vec(), - Element::new_sum_item(5), - ), - QualifiedGroveDbOp::insert_or_replace_op( - vec![ - TEST_LEAF.to_vec(), - b"key1".to_vec(), - b"e".to_vec(), - b"third".to_vec(), - ], - b"b".to_vec(), - Element::new_item(vec![5]), - ), - ]; - db.apply_batch(ops, None, None, grove_version) + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(30) + ); + + // Add more sum items + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item2", + Element::new_sum_item(-10), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item3", + Element::new_sum_item(50), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(70) + ); // 30 - 10 + 50 = 70 + + // Add non sum items, result should remain the same + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item4", + Element::new_item(vec![29]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(70) + ); + + // Update existing sum items + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item2", + Element::new_sum_item(10), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item3", + Element::new_sum_item(-100), + None, + None, + grove_version, + ) .unwrap() - .expect("should apply batch"); + .expect("should insert item"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(-60) + ); // 30 + 10 - 100 = -60 - let batch = StorageBatch::new(); - let sum_tree = db - .open_non_transactional_merk_at_path( - [TEST_LEAF, b"key1"].as_ref().into(), - Some(&batch), + // We can not replace a normal item with a sum item, so let's delete it first + db.delete( + [TEST_LEAF, b"key2"].as_ref(), + b"item4", + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to delete"); + // Use a large value + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item4", + Element::new_sum_item(10000000), + None, + None, grove_version, ) .unwrap() - .expect("should open tree"); - assert_eq!(sum_tree.sum().expect("expected to get sum"), Some(41)); + .expect("should insert item"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(9999940) + ); // 30 + + // 10 - + // 100 + + // 10000000 + } + + #[test] + fn test_sum_tree_overflow() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + db.insert( + [TEST_LEAF].as_ref(), + b"key", + Element::empty_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + + let batch = StorageBatch::new(); + + // Sum should be non for non sum tree + // TODO: change interface to retrieve element directly + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::NoAggregateData + ); + + // Add sum tree + db.insert( + [TEST_LEAF].as_ref(), + b"key2", + Element::empty_sum_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert sum tree"); + let sum_tree = db + .get([TEST_LEAF].as_ref(), b"key2", None, grove_version) + .unwrap() + .expect("should retrieve tree"); + assert_eq!(sum_tree.sum_value_or_default(), 0); + + // Add sum items to the sum tree + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item1", + Element::new_sum_item(SumValue::MAX), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + // TODO: change interface to retrieve element directly + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(SumValue::MAX) + ); + + // Subtract 10 from Max should work + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item2", + Element::new_sum_item(-10), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(SumValue::MAX - 10) + ); + + // Add 20 from Max should overflow + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item3", + Element::new_sum_item(20), + None, + None, + grove_version, + ) + .unwrap() + .expect_err("should not be able to insert item"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(SumValue::MAX - 10) + ); + + // Add non sum items, result should remain the same + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item4", + Element::new_item(vec![29]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(SumValue::MAX - 10) + ); + + // Update existing sum item will overflow + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item2", + Element::new_sum_item(10), // we are replacing -10 with 10 + None, + None, + grove_version, + ) + .unwrap() + .expect_err("should not be able to insert item"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(SumValue::MAX - 10) + ); + + // Update existing sum item will overflow + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item2", + Element::new_sum_item(SumValue::MIN), // we are replacing -10 with SumValue::MIN + None, + None, + grove_version, + ) + .unwrap() + .expect("should be able to insert item"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(-1) + ); + + db.insert( + [TEST_LEAF, b"key2"].as_ref(), + b"item3", + Element::new_sum_item(-40), + None, + None, + grove_version, + ) + .unwrap() + .expect("should be able to insert item"); + + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(-41) + ); + + // Deleting item1 should make us overflow + db.delete( + [TEST_LEAF, b"key2"].as_ref(), + b"item1", + None, + None, + grove_version, + ) + .unwrap() + .expect_err("expected not be able to delete"); + let merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + merk.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(-41) + ); + } + + #[test] + fn test_sum_tree_propagation() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + // Tree + // SumTree + // SumTree + // Item1 + // SumItem1 + // SumItem2 + // SumItem3 + db.insert( + [TEST_LEAF].as_ref(), + b"key", + Element::empty_sum_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"tree2", + Element::empty_sum_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"key"].as_ref(), + b"sumitem3", + Element::new_sum_item(20), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"key", b"tree2"].as_ref(), + b"item1", + Element::new_item(vec![2]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key", b"tree2"].as_ref(), + b"sumitem1", + Element::new_sum_item(5), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key", b"tree2"].as_ref(), + b"sumitem2", + Element::new_sum_item(10), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"key", b"tree2"].as_ref(), + b"item2", + Element::new_reference(ReferencePathType::AbsolutePathReference(vec![ + TEST_LEAF.to_vec(), + b"key".to_vec(), + b"tree2".to_vec(), + b"sumitem1".to_vec(), + ])), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + + let sum_tree = db + .get([TEST_LEAF].as_ref(), b"key", None, grove_version) + .unwrap() + .expect("should fetch tree"); + assert_eq!(sum_tree.sum_value_or_default(), 35); + + let batch = StorageBatch::new(); + + // Assert node feature types + let test_leaf_merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert!(matches!( + test_leaf_merk + .get_feature_type( + b"key", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(BasicMerkNode) + )); + + let parent_sum_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert!(matches!( + parent_sum_tree + .get_feature_type( + b"tree2", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(15)) /* 15 because the child sum tree has one sum item of + * value 5 and + * another of value 10 */ + )); + + let child_sum_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key", b"tree2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert!(matches!( + child_sum_tree + .get_feature_type( + b"item1", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(0)) + )); + assert!(matches!( + child_sum_tree + .get_feature_type( + b"sumitem1", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(5)) + )); + assert!(matches!( + child_sum_tree + .get_feature_type( + b"sumitem2", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(10)) + )); + + // TODO: should references take the sum of the referenced element?? + assert!(matches!( + child_sum_tree + .get_feature_type( + b"item2", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(0)) + )); + } + + #[test] + fn test_big_sum_tree_propagation() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + // Tree + // BigSumTree + // SumTree1 + // SumItem1 + // SumItem2 + // SumTree2 + // SumItem3 + // SumItem4 + db.insert( + [TEST_LEAF].as_ref(), + b"big_sum_tree", + Element::empty_big_sum_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"big_sum_tree"].as_ref(), + b"sum_tree_1", + Element::empty_sum_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"big_sum_tree"].as_ref(), + b"sum_tree_2", + Element::empty_sum_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert tree"); + db.insert( + [TEST_LEAF, b"big_sum_tree", b"sum_tree_1"].as_ref(), + b"item1", + Element::new_item(vec![2]), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"big_sum_tree", b"sum_tree_1"].as_ref(), + b"sum_item_1", + Element::new_sum_item(SumValue::MAX - 40), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"big_sum_tree", b"sum_tree_1"].as_ref(), + b"sum_item_2", + Element::new_sum_item(30), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + db.insert( + [TEST_LEAF, b"big_sum_tree", b"sum_tree_1"].as_ref(), + b"ref_1", + Element::new_reference(ReferencePathType::AbsolutePathReference(vec![ + TEST_LEAF.to_vec(), + b"big_sum_tree".to_vec(), + b"sum_tree_1".to_vec(), + b"sum_item_1".to_vec(), + ])), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + + db.insert( + [TEST_LEAF, b"big_sum_tree", b"sum_tree_2"].as_ref(), + b"sum_item_3", + Element::new_sum_item(SumValue::MAX - 50), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + + let sum_tree = db + .get([TEST_LEAF].as_ref(), b"big_sum_tree", None, grove_version) + .unwrap() + .expect("should fetch tree"); + assert_eq!( + sum_tree.big_sum_value_or_default(), + (SumValue::MAX - 10) as i128 + (SumValue::MAX - 50) as i128 + ); + + db.insert( + [TEST_LEAF, b"big_sum_tree"].as_ref(), + b"sum_item_4", + Element::new_sum_item(SumValue::MAX - 70), + None, + None, + grove_version, + ) + .unwrap() + .expect("should insert item"); + + let sum_tree = db + .get([TEST_LEAF].as_ref(), b"big_sum_tree", None, grove_version) + .unwrap() + .expect("should fetch tree"); + assert_eq!( + sum_tree.big_sum_value_or_default(), + (SumValue::MAX - 10) as i128 + + (SumValue::MAX - 50) as i128 + + (SumValue::MAX - 70) as i128 + ); + + let batch = StorageBatch::new(); + + // Assert node feature types + let test_leaf_merk = db + .open_non_transactional_merk_at_path( + [TEST_LEAF].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert!(matches!( + test_leaf_merk + .get_feature_type( + b"big_sum_tree", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(BasicMerkNode) + )); + + let parent_sum_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"big_sum_tree"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + let feature_type = parent_sum_tree + .get_feature_type( + b"sum_tree_1", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected feature type"); + assert_eq!( + feature_type, + BigSummedMerkNode((SumValue::MAX - 10) as i128) + ); + + let feature_type = parent_sum_tree + .get_feature_type( + b"sum_item_4", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version, + ) + .unwrap() + .expect("node should exist") + .expect("expected feature type"); + assert_eq!( + feature_type, + BigSummedMerkNode((SumValue::MAX - 70) as i128) + ); + + let child_sum_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"big_sum_tree", b"sum_tree_1"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + child_sum_tree + .get_feature_type( + b"item1", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(0)) + ); + assert_eq!( + child_sum_tree + .get_feature_type( + b"sum_item_1", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(SumValue::MAX - 40)) + ); + assert_eq!( + child_sum_tree + .get_feature_type( + b"sum_item_2", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(30)) + ); + + assert_eq!( + child_sum_tree + .get_feature_type( + b"ref_1", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(0)) + ); + + let child_sum_tree_2 = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"big_sum_tree", b"sum_tree_2"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + + assert_eq!( + child_sum_tree_2 + .get_feature_type( + b"sum_item_3", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(SumValue::MAX - 50)) + ); + } + + #[test] + fn test_sum_tree_with_batches() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + let ops = vec![ + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec()], + b"key1".to_vec(), + Element::empty_sum_tree(), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"a".to_vec(), + Element::new_item(vec![214]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"b".to_vec(), + Element::new_sum_item(10), + ), + ]; + db.apply_batch(ops, None, None, grove_version) + .unwrap() + .expect("should apply batch"); + + let batch = StorageBatch::new(); + let sum_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key1"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + + assert_eq!( + sum_tree + .get_feature_type( + b"a", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(0)) + ); + assert_eq!( + sum_tree + .get_feature_type( + b"b", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(10)) + ); + + // Create new batch to use existing tree + let ops = vec![QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"c".to_vec(), + Element::new_sum_item(10), + )]; + db.apply_batch(ops, None, None, grove_version) + .unwrap() + .expect("should apply batch"); + + let batch = StorageBatch::new(); + let sum_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key1"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + sum_tree + .get_feature_type( + b"c", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(SummedMerkNode(10)) + ); + assert_eq!( + sum_tree.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(20) + ); + + // Test propagation + // Add a new sum tree with its own sum items, should affect sum of original + // tree + let ops = vec![ + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"d".to_vec(), + Element::empty_sum_tree(), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"d".to_vec()], + b"first".to_vec(), + Element::new_sum_item(4), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"d".to_vec()], + b"second".to_vec(), + Element::new_item(vec![4]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"e".to_vec(), + Element::empty_sum_tree(), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"e".to_vec()], + b"first".to_vec(), + Element::new_sum_item(12), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"e".to_vec()], + b"second".to_vec(), + Element::new_item(vec![4]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"e".to_vec()], + b"third".to_vec(), + Element::empty_sum_tree(), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![ + TEST_LEAF.to_vec(), + b"key1".to_vec(), + b"e".to_vec(), + b"third".to_vec(), + ], + b"a".to_vec(), + Element::new_sum_item(5), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![ + TEST_LEAF.to_vec(), + b"key1".to_vec(), + b"e".to_vec(), + b"third".to_vec(), + ], + b"b".to_vec(), + Element::new_item(vec![5]), + ), + ]; + db.apply_batch(ops, None, None, grove_version) + .unwrap() + .expect("should apply batch"); + + let batch = StorageBatch::new(); + let sum_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key1"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + sum_tree.aggregate_data().expect("expected to get sum"), + AggregateData::Sum(41) + ); + } + + #[test] + fn test_big_sum_tree_with_batches() { + let grove_version = GroveVersion::latest(); + let db = make_test_grovedb(grove_version); + let ops = vec![ + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec()], + b"key1".to_vec(), + Element::empty_big_sum_tree(), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"a".to_vec(), + Element::new_item(vec![214]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"b".to_vec(), + Element::new_sum_item(10), + ), + ]; + db.apply_batch(ops, None, None, grove_version) + .unwrap() + .expect("should apply batch"); + + let batch = StorageBatch::new(); + let sum_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key1"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + + assert_eq!( + sum_tree + .get_feature_type( + b"a", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(BigSummedMerkNode(0)) + ); + assert_eq!( + sum_tree + .get_feature_type( + b"b", + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(BigSummedMerkNode(10)) + ); + + // Create new batch to use existing tree + let ops = vec![QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"c".to_vec(), + Element::new_sum_item(10), + )]; + db.apply_batch(ops, None, None, grove_version) + .unwrap() + .expect("should apply batch"); + + let batch = StorageBatch::new(); + let sum_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key1"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + sum_tree + .get_feature_type( + b"c", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version + ) + .unwrap() + .expect("node should exist"), + Some(BigSummedMerkNode(10)) + ); + assert_eq!( + sum_tree.aggregate_data().expect("expected to get sum"), + AggregateData::BigSum(20) + ); + + // Test propagation + // Add a new sum tree with its own sum items, should affect sum of original + // tree + let ops = vec![ + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"d".to_vec(), + Element::empty_sum_tree(), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"d".to_vec()], + b"first".to_vec(), + Element::new_sum_item(4), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"d".to_vec()], + b"second".to_vec(), + Element::new_item(vec![4]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec()], + b"e".to_vec(), + Element::empty_sum_tree(), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"e".to_vec()], + b"first".to_vec(), + Element::new_sum_item(12), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"e".to_vec()], + b"second".to_vec(), + Element::new_item(vec![4]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec(), b"key1".to_vec(), b"e".to_vec()], + b"third".to_vec(), + Element::empty_sum_tree(), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![ + TEST_LEAF.to_vec(), + b"key1".to_vec(), + b"e".to_vec(), + b"third".to_vec(), + ], + b"a".to_vec(), + Element::new_sum_item(5), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![ + TEST_LEAF.to_vec(), + b"key1".to_vec(), + b"e".to_vec(), + b"third".to_vec(), + ], + b"b".to_vec(), + Element::new_item(vec![5]), + ), + ]; + db.apply_batch(ops, None, None, grove_version) + .unwrap() + .expect("should apply batch"); + + let batch = StorageBatch::new(); + let sum_tree = db + .open_non_transactional_merk_at_path( + [TEST_LEAF, b"key1"].as_ref().into(), + Some(&batch), + grove_version, + ) + .unwrap() + .expect("should open tree"); + assert_eq!( + sum_tree.aggregate_data().expect("expected to get sum"), + AggregateData::BigSum(41) + ); + } } diff --git a/grovedb/src/util.rs b/grovedb/src/util.rs index b9b624a4..20ec46d8 100644 --- a/grovedb/src/util.rs +++ b/grovedb/src/util.rs @@ -28,7 +28,7 @@ macro_rules! storage_context_with_parent_optional_tx { $transaction:ident, $storage:ident, $root_key:ident, - $is_sum_tree:ident, + $tree_type:ident, $grove_version:ident, { $($body:tt)* } ) => { @@ -54,24 +54,14 @@ macro_rules! storage_context_with_parent_optional_tx { ) }) ); - match element { - Element::Tree(root_key, _) => { - let $root_key = root_key; - let $is_sum_tree = false; - $($body)* - } - Element::SumTree(root_key, ..) => { - let $root_key = root_key; - let $is_sum_tree = true; - $($body)* - } - _ => { - return Err(Error::CorruptedData( + let Some(($root_key, $tree_type)) = element.root_key_and_tree_type_owned() else + { + return Err(Error::CorruptedData( "parent is not a tree" .to_owned(), )).wrap_with_cost($cost); - } - } + }; + $($body)* } else { return Err(Error::CorruptedData( "path is empty".to_owned(), @@ -95,24 +85,14 @@ macro_rules! storage_context_with_parent_optional_tx { ) }) ); - match element { - Element::Tree(root_key, _) => { - let $root_key = root_key; - let $is_sum_tree = false; - $($body)* - } - Element::SumTree(root_key, ..) => { - let $root_key = root_key; - let $is_sum_tree = true; - $($body)* - } - _ => { - return Err(Error::CorruptedData( + let Some(($root_key, $tree_type)) = element.root_key_and_tree_type_owned() else + { + return Err(Error::CorruptedData( "parent is not a tree" .to_owned(), )).wrap_with_cost($cost); - } - } + }; + $($body)* } else { return Err(Error::CorruptedData( "path is empty".to_owned(), @@ -134,7 +114,7 @@ macro_rules! storage_context_with_parent_optional_tx_internal_error { $transaction:ident, $storage:ident, $root_key:ident, - $is_sum_tree:ident, + $tree_type:ident, $grove_version:ident, { $($body:tt)* } ) => { @@ -162,24 +142,15 @@ macro_rules! storage_context_with_parent_optional_tx_internal_error { }).unwrap_add_cost(&mut $cost); match result { Ok(element) => { - match element { - Element::Tree(root_key, _) => { - let $root_key = root_key; - let $is_sum_tree = false; - $($body)* - } - Element::SumTree(root_key, ..) => { - let $root_key = root_key; - let $is_sum_tree = true; - $($body)* - } - _ => { - return Err(Error::CorruptedData( - "parent is not a tree" - .to_owned(), - )).wrap_with_cost($cost); - } - } + let Some(($root_key, $tree_type)) + = element.root_key_and_tree_type_owned() else + { + return Err(Error::CorruptedData( + "parent is not a tree" + .to_owned(), + )).wrap_with_cost($cost); + }; + $($body)* }, Err(e) => Err(e), } @@ -210,24 +181,15 @@ macro_rules! storage_context_with_parent_optional_tx_internal_error { }).unwrap_add_cost(&mut $cost); match result { Ok(element) => { - match element { - Element::Tree(root_key, _) => { - let $root_key = root_key; - let $is_sum_tree = false; - $($body)* - } - Element::SumTree(root_key, ..) => { - let $root_key = root_key; - let $is_sum_tree = true; - $($body)* - } - _ => { - return Err(Error::CorruptedData( - "parent is not a tree" - .to_owned(), - )).wrap_with_cost($cost); - } - } + let Some(($root_key, $tree_type)) + = element.root_key_and_tree_type_owned() else + { + return Err(Error::CorruptedData( + "parent is not a tree" + .to_owned(), + )).wrap_with_cost($cost); + }; + $($body)* }, Err(e) => Err(e), } @@ -293,7 +255,7 @@ macro_rules! merk_optional_tx { &mut $cost, ::grovedb_merk::Merk::open_base( storage.unwrap_add_cost(&mut $cost), - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), $grove_version, ).map(|merk_res| @@ -315,7 +277,7 @@ macro_rules! merk_optional_tx { $transaction, storage, root_key, - is_sum_tree, + tree_type, $grove_version, { #[allow(unused_mut)] @@ -324,7 +286,7 @@ macro_rules! merk_optional_tx { ::grovedb_merk::Merk::open_layered_with_root_key( storage, root_key, - is_sum_tree, + tree_type, Some(&Element::value_defined_cost_for_serialized_value), $grove_version, ).map(|merk_res| @@ -367,7 +329,7 @@ macro_rules! merk_optional_tx_internal_error { &mut $cost, ::grovedb_merk::Merk::open_base( storage.unwrap_add_cost(&mut $cost), - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), $grove_version ).map(|merk_res| @@ -389,7 +351,7 @@ macro_rules! merk_optional_tx_internal_error { $transaction, storage, root_key, - is_sum_tree, + tree_type, $grove_version, { #[allow(unused_mut)] @@ -398,7 +360,7 @@ macro_rules! merk_optional_tx_internal_error { ::grovedb_merk::Merk::open_layered_with_root_key( storage, root_key, - is_sum_tree, + tree_type, Some(&Element::value_defined_cost_for_serialized_value), $grove_version, ).map(|merk_res| @@ -438,7 +400,7 @@ macro_rules! merk_optional_tx_path_not_empty { $transaction, storage, root_key, - is_sum_tree, + tree_type, $grove_version, { #[allow(unused_mut)] @@ -447,7 +409,7 @@ macro_rules! merk_optional_tx_path_not_empty { ::grovedb_merk::Merk::open_layered_with_root_key( storage, root_key, - is_sum_tree, + tree_type, Some(&Element::value_defined_cost_for_serialized_value), $grove_version, ).map(|merk_res| @@ -489,7 +451,7 @@ macro_rules! root_merk_optional_tx { &mut $cost, ::grovedb_merk::Merk::open_base( storage.unwrap_add_cost(&mut $cost), - false, + TreeType::NormalTree, Some(&Element::value_defined_cost_for_serialized_value), $grove_version, ).map(|merk_res| diff --git a/grovedb/src/visualize.rs b/grovedb/src/visualize.rs index 39cf3432..8fdccc7e 100644 --- a/grovedb/src/visualize.rs +++ b/grovedb/src/visualize.rs @@ -95,6 +95,39 @@ impl Visualize for Element { drawer = root_key.as_deref().visualize(drawer)?; drawer.write(format!(" {value}").as_bytes())?; + if let Some(f) = flags { + if !f.is_empty() { + drawer = f.visualize(drawer)?; + } + } + } + Element::BigSumTree(root_key, value, flags) => { + drawer.write(b"big_sum_tree: ")?; + drawer = root_key.as_deref().visualize(drawer)?; + drawer.write(format!(" {value}").as_bytes())?; + + if let Some(f) = flags { + if !f.is_empty() { + drawer = f.visualize(drawer)?; + } + } + } + Element::CountTree(root_key, value, flags) => { + drawer.write(b"count_tree: ")?; + drawer = root_key.as_deref().visualize(drawer)?; + drawer.write(format!(" {value}").as_bytes())?; + + if let Some(f) = flags { + if !f.is_empty() { + drawer = f.visualize(drawer)?; + } + } + } + Element::CountSumTree(root_key, count_value, sum_value, flags) => { + drawer.write(b"count_sum_tree: ")?; + drawer = root_key.as_deref().visualize(drawer)?; + drawer.write(format!("count: {count_value}, sum {sum_value}").as_bytes())?; + if let Some(f) = flags { if !f.is_empty() { drawer = f.visualize(drawer)?; diff --git a/grovedbg-types/Cargo.toml b/grovedbg-types/Cargo.toml index 7c5eb549..357ce04a 100644 --- a/grovedbg-types/Cargo.toml +++ b/grovedbg-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "grovedbg-types" -version = "2.2.1" +version = "3.0.0" edition = "2021" description = "Common type definitions for data exchange over GroveDBG protocol" authors = ["Evgeny Fomin "] diff --git a/grovedbg-types/src/lib.rs b/grovedbg-types/src/lib.rs index dd9fc007..fb9c6d90 100644 --- a/grovedbg-types/src/lib.rs +++ b/grovedbg-types/src/lib.rs @@ -125,6 +125,28 @@ pub enum Element { #[serde_as(as = "Option")] element_flags: Option>, }, + BigSumTree { + #[serde_as(as = "Option")] + root_key: Option, + sum: i128, + #[serde_as(as = "Option")] + element_flags: Option>, + }, + CountTree { + #[serde_as(as = "Option")] + root_key: Option, + count: u64, + #[serde_as(as = "Option")] + element_flags: Option>, + }, + CountSumTree { + #[serde_as(as = "Option")] + root_key: Option, + count: u64, + sum: i64, + #[serde_as(as = "Option")] + element_flags: Option>, + }, Item { #[serde_as(as = "Base64")] value: Vec, @@ -261,6 +283,9 @@ pub enum MerkProofNode { pub enum TreeFeatureType { BasicMerkNode, SummedMerkNode(i64), + BigSummedMerkNode(i128), + CountedMerkNode(u64), + CountedSummedMerkNode(u64, i64), } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] diff --git a/merk/Cargo.toml b/merk/Cargo.toml index 903c61d7..07d0b930 100644 --- a/merk/Cargo.toml +++ b/merk/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "grovedb-merk" description = "Merkle key/value store adapted for GroveDB" -version = "2.2.1" +version = "3.0.0" authors = ["Samuel Westrich ", "Wisdom Ogwu ", "Matt Bell "] edition = "2021" license = "MIT" @@ -11,11 +11,11 @@ readme = "README.md" documentation = "https://docs.rs/grovedb-merk" [dependencies] -grovedb-costs = { version = "2.2.1" , path = "../costs" } -grovedb-path = { version = "2.2.1", path = "../path" } -grovedb-storage = { version = "2.2.1", path = "../storage", optional = true } -grovedb-version = { version = "2.2.1", path = "../grovedb-version" } -grovedb-visualize = { version = "2.2.1", path = "../visualize" } +grovedb-costs = { version = "3.0.0" , path = "../costs" } +grovedb-path = { version = "3.0.0", path = "../path" } +grovedb-storage = { version = "3.0.0", path = "../storage", optional = true } +grovedb-version = { version = "3.0.0", path = "../grovedb-version" } +grovedb-visualize = { version = "3.0.0", path = "../visualize" } bincode = { version = "2.0.0-rc.3" } hex = "0.4.3" @@ -24,6 +24,7 @@ integer-encoding = "4.0.0" thiserror = "2.0.11" serde = { version = "1.0.210", features = ["derive"], optional = true } rand = { version = "0.8.5", features = ["small_rng"], optional = true } +byteorder = { version = "1.5.0" } [dependencies.colored] version = "3.0.0" diff --git a/merk/src/error.rs b/merk/src/error.rs index 8fdc1cfc..6eb24385 100644 --- a/merk/src/error.rs +++ b/merk/src/error.rs @@ -117,6 +117,12 @@ pub enum Error { #[error(transparent)] /// Version error VersionError(grovedb_version::error::GroveVersionError), + + #[error("big sum tree under normal sum tree error {0}")] + BigSumTreeUnderNormalSumTree(String), + + #[error("unknown tree type {0}")] + UnknownTreeType(String), } impl From for Error { diff --git a/merk/src/estimated_costs/average_case_costs.rs b/merk/src/estimated_costs/average_case_costs.rs index 6e877efe..3b535767 100644 --- a/merk/src/estimated_costs/average_case_costs.rs +++ b/merk/src/estimated_costs/average_case_costs.rs @@ -3,6 +3,8 @@ #[cfg(feature = "minimal")] use grovedb_costs::{CostResult, CostsExt, OperationCost}; #[cfg(feature = "minimal")] +use grovedb_version::{check_grovedb_v0_or_v1, error::GroveVersionError, version::GroveVersion}; +#[cfg(feature = "minimal")] use integer_encoding::VarInt; #[cfg(feature = "minimal")] @@ -12,6 +14,7 @@ use crate::{ tree::{kv::KV, Link, TreeNode}, HASH_BLOCK_SIZE, HASH_BLOCK_SIZE_U32, HASH_LENGTH, HASH_LENGTH_U32, }; +use crate::{merk::NodeType, tree_type::TreeType}; #[cfg(feature = "minimal")] /// Average key size @@ -38,26 +41,88 @@ pub enum EstimatedSumTrees { SomeSumTrees { /// Sum trees weight sum_trees_weight: Weight, + /// Big Sum trees weight + big_sum_trees_weight: Weight, + /// Count trees weight + count_trees_weight: Weight, + /// Count Sum trees weight + count_sum_trees_weight: Weight, /// Non sum trees weight non_sum_trees_weight: Weight, }, /// All sum trees AllSumTrees, + /// All big sum trees + AllBigSumTrees, + /// All count trees + AllCountTrees, + /// All count sum trees + AllCountSumTrees, } -#[cfg(feature = "minimal")] #[cfg(feature = "minimal")] impl EstimatedSumTrees { - fn estimated_size(&self) -> Result { + fn estimated_size(&self, grove_version: &GroveVersion) -> Result { + let version = check_grovedb_v0_or_v1!( + "EstimatedSumTrees::estimated_size", + grove_version + .merk_versions + .average_case_costs + .sum_tree_estimated_size + ); match self { EstimatedSumTrees::NoSumTrees => Ok(0), EstimatedSumTrees::SomeSumTrees { sum_trees_weight, + big_sum_trees_weight, + count_trees_weight, + count_sum_trees_weight, non_sum_trees_weight, - } => (*non_sum_trees_weight as u32 * 9) - .checked_div(*sum_trees_weight as u32 + *non_sum_trees_weight as u32) - .ok_or(Error::DivideByZero("weights add up to 0")), - EstimatedSumTrees::AllSumTrees => Ok(8), + } => { + // Example calculation including new weights + let total_weight = *sum_trees_weight as u32 + + *big_sum_trees_weight as u32 + + *count_trees_weight as u32 + + *count_sum_trees_weight as u32 + + *non_sum_trees_weight as u32; + if total_weight == 0 { + return Err(Error::DivideByZero("weights add up to 0")); + }; + if version == 0 { + Ok((*non_sum_trees_weight as u32 * 9) + / (*sum_trees_weight as u32 + *non_sum_trees_weight as u32)) + } else if version == 1 { + let estimated_size = (*sum_trees_weight as u32 + * TreeType::SumTree.inner_node_type().cost()) + .checked_add( + *big_sum_trees_weight as u32 + * TreeType::BigSumTree.inner_node_type().cost(), + ) + .and_then(|sum| { + sum.checked_add( + *count_trees_weight as u32 + * TreeType::CountTree.inner_node_type().cost(), + ) + }) + .and_then(|sum| { + sum.checked_add( + *count_sum_trees_weight as u32 + * TreeType::CountSumTree.inner_node_type().cost(), + ) + }) + .ok_or(Error::Overflow("Estimated size calculation overflowed"))?; + + Ok(estimated_size / total_weight) + } else { + Err(Error::CorruptedCodeExecution("we already checked versions")) + } + } + EstimatedSumTrees::AllSumTrees => Ok(TreeType::SumTree.inner_node_type().cost()), + EstimatedSumTrees::AllBigSumTrees => Ok(TreeType::BigSumTree.inner_node_type().cost()), + EstimatedSumTrees::AllCountTrees => Ok(TreeType::CountTree.inner_node_type().cost()), + EstimatedSumTrees::AllCountSumTrees => { + Ok(TreeType::CountSumTree.inner_node_type().cost()) + } } } } @@ -126,19 +191,26 @@ impl EstimatedLayerSizes { /// Returns the size of a subtree's feature and flags /// This only takes into account subtrees in the estimated layer info /// Only should be used when it is known to be a subtree - pub fn subtree_with_feature_and_flags_size(&self) -> Result { + pub fn subtree_with_feature_and_flags_size( + &self, + grove_version: &GroveVersion, + ) -> Result { match self { EstimatedLayerSizes::AllSubtrees(_, estimated_sum_trees, flags_size) => { // 1 for enum type // 1 for empty // 1 for flags size - Ok(estimated_sum_trees.estimated_size()? + flags_size.unwrap_or_default() + 3) + Ok(estimated_sum_trees.estimated_size(grove_version)? + + flags_size.unwrap_or_default() + + 3) } EstimatedLayerSizes::Mix { subtrees_size, .. } => match subtrees_size { None => Err(Error::WrongEstimatedCostsElementTypeForLevel( "this layer is a mix but doesn't have subtrees", )), - Some((_, est, fs, _)) => Ok(est.estimated_size()? + fs.unwrap_or_default() + 3), + Some((_, est, fs, _)) => { + Ok(est.estimated_size(grove_version)? + fs.unwrap_or_default() + 3) + } }, _ => Err(Error::WrongEstimatedCostsElementTypeForLevel( "this layer needs to have trees", @@ -147,7 +219,10 @@ impl EstimatedLayerSizes { } /// Returns the size of a value's feature and flags - pub fn value_with_feature_and_flags_size(&self) -> Result { + pub fn value_with_feature_and_flags_size( + &self, + grove_version: &GroveVersion, + ) -> Result { match self { EstimatedLayerSizes::AllItems(_, average_value_size, flags_size) => { // 1 for enum type @@ -166,7 +241,9 @@ impl EstimatedLayerSizes { // 1 for enum type // 1 for empty // 1 for flags size - Ok(estimated_sum_trees.estimated_size()? + flags_size.unwrap_or_default() + 3) + Ok(estimated_sum_trees.estimated_size(grove_version)? + + flags_size.unwrap_or_default() + + 3) } EstimatedLayerSizes::Mix { subtrees_size, @@ -186,7 +263,7 @@ impl EstimatedLayerSizes { let (subtree_size, subtree_weight) = match subtrees_size { None => None, Some((_, est, fs, weight)) => Some(( - est.estimated_size()? + fs.unwrap_or_default() + 3, + est.estimated_size(grove_version)? + fs.unwrap_or_default() + 3, *weight as u32, )), } @@ -234,8 +311,8 @@ pub type EstimatedToBeEmpty = bool; #[derive(Clone, Copy, PartialEq, Eq, Debug)] /// Information on an estimated layer pub struct EstimatedLayerInformation { - /// Is sum tree? - pub is_sum_tree: bool, + /// The kind of tree we are in + pub tree_type: TreeType, /// Estimated layer count pub estimated_layer_count: EstimatedLayerCount, /// Estimated layer sizes @@ -291,13 +368,13 @@ impl TreeNode { pub fn average_case_encoded_tree_size( not_prefixed_key_len: u32, estimated_element_size: u32, - is_sum_node: bool, + node_type: NodeType, ) -> u32 { // two option values for the left and right link // the actual left and right link encoding size // the encoded kv node size - 2 + (2 * Link::encoded_link_size(not_prefixed_key_len, is_sum_node)) - + KV::encoded_kv_node_size(estimated_element_size, is_sum_node) + 2 + (2 * Link::encoded_link_size(not_prefixed_key_len, node_type)) + + KV::encoded_kv_node_size(estimated_element_size, node_type) } } @@ -307,7 +384,7 @@ pub fn add_average_case_get_merk_node( cost: &mut OperationCost, not_prefixed_key_len: u32, approximate_element_size: u32, - is_sum_tree: bool, + node_type: NodeType, ) -> Result<(), Error> { // Worst case scenario, the element is not already in memory. // One direct seek has to be performed to read the node from storage. @@ -318,7 +395,7 @@ pub fn add_average_case_get_merk_node( cost.storage_loaded_bytes += TreeNode::average_case_encoded_tree_size( not_prefixed_key_len, approximate_element_size, - is_sum_tree, + node_type, ) as u64; Ok(()) } @@ -340,11 +417,11 @@ pub fn add_average_case_merk_replace_layered( cost: &mut OperationCost, key_len: u32, value_len: u32, - is_sum_node: bool, + node_type: NodeType, ) { cost.seek_count += 1; cost.storage_cost.replaced_bytes = - KV::layered_value_byte_cost_size_for_key_and_value_lengths(key_len, value_len, is_sum_node); + KV::layered_value_byte_cost_size_for_key_and_value_lengths(key_len, value_len, node_type); // first lets add the value hash cost.hash_node_calls += 1 + ((value_len - 1) / HASH_BLOCK_SIZE_U32); @@ -394,9 +471,12 @@ pub fn add_average_case_merk_root_hash(cost: &mut OperationCost) { #[cfg(feature = "minimal")] /// Average case cost of propagating a merk -pub fn average_case_merk_propagate(input: &EstimatedLayerInformation) -> CostResult<(), Error> { +pub fn average_case_merk_propagate( + input: &EstimatedLayerInformation, + grove_version: &GroveVersion, +) -> CostResult<(), Error> { let mut cost = OperationCost::default(); - add_average_case_merk_propagate(&mut cost, input).wrap_with_cost(cost) + add_average_case_merk_propagate(&mut cost, input, grove_version).wrap_with_cost(cost) } #[cfg(feature = "minimal")] @@ -404,16 +484,310 @@ pub fn average_case_merk_propagate(input: &EstimatedLayerInformation) -> CostRes pub fn add_average_case_merk_propagate( cost: &mut OperationCost, input: &EstimatedLayerInformation, + grove_version: &GroveVersion, +) -> Result<(), Error> { + match grove_version + .merk_versions + .average_case_costs + .add_average_case_merk_propagate + { + 0 => add_average_case_merk_propagate_v0(cost, input, grove_version), + 1 => add_average_case_merk_propagate_v1(cost, input, grove_version), + version => Err(Error::VersionError( + GroveVersionError::UnknownVersionMismatch { + method: "add_average_case_merk_propagate".to_string(), + known_versions: vec![0, 1], + received: version, + }, + )), + } +} +#[cfg(feature = "minimal")] +/// Add average case cost for propagating a merk +fn add_average_case_merk_propagate_v1( + cost: &mut OperationCost, + input: &EstimatedLayerInformation, + grove_version: &GroveVersion, +) -> Result<(), Error> { + let mut nodes_updated = 0; + // Propagation requires to recompute and write hashes up to the root + let EstimatedLayerInformation { + tree_type, + estimated_layer_count, + estimated_layer_sizes, + } = input; + let levels = estimated_layer_count.estimate_levels(); + nodes_updated += levels; + + if levels > 1 { + // we can get about 1 rotation, if there are more than 2 levels + nodes_updated += 1; + } + cost.seek_count += nodes_updated as u32; + + cost.hash_node_calls += nodes_updated * 2; + + cost.storage_cost.replaced_bytes += match estimated_layer_sizes { + EstimatedLayerSizes::AllSubtrees( + average_key_size, + estimated_sum_trees, + average_flags_size, + ) => { + // it is normal to have LAYER_COST_SIZE here, as we add estimated sum tree + // additions right after + let value_len = LAYER_COST_SIZE + + average_flags_size + .map_or(0, |flags_len| flags_len + flags_len.required_space() as u32); + // in order to simplify calculations we get the estimated size and remove the + // cost for the basic merk + let sum_tree_addition = estimated_sum_trees.estimated_size(grove_version)?; + nodes_updated + * (KV::layered_value_byte_cost_size_for_key_and_value_lengths( + *average_key_size as u32, + value_len, + tree_type.inner_node_type(), + ) + sum_tree_addition) + } + EstimatedLayerSizes::AllItems(average_key_size, average_item_size, average_flags_size) + | EstimatedLayerSizes::AllReference( + average_key_size, + average_item_size, + average_flags_size, + ) => { + let flags_len = average_flags_size.unwrap_or(0); + let average_value_len = average_item_size + flags_len; + nodes_updated + * KV::value_byte_cost_size_for_key_and_raw_value_lengths( + *average_key_size as u32, + average_value_len, + tree_type.inner_node_type(), + ) + } + EstimatedLayerSizes::Mix { + subtrees_size, + items_size, + references_size, + } => { + let total_weight = subtrees_size + .as_ref() + .map(|(_, _, _, weight)| *weight as u32) + .unwrap_or_default() + + items_size + .as_ref() + .map(|(_, _, _, weight)| *weight as u32) + .unwrap_or_default() + + references_size + .as_ref() + .map(|(_, _, _, weight)| *weight as u32) + .unwrap_or_default(); + if total_weight == 0 { + 0 + } else { + let weighted_nodes_updated = (nodes_updated as u64) + .checked_mul(total_weight as u64) + .ok_or(Error::Overflow("overflow for weights average cost"))?; + let tree_node_updates_cost = match subtrees_size { + None => 0, + Some((average_key_size, estimated_sum_trees, average_flags_size, weight)) => { + let flags_len = average_flags_size.unwrap_or(0); + let value_len = LAYER_COST_SIZE + flags_len; + let sum_tree_addition = + estimated_sum_trees.estimated_size(grove_version)?; + let cost = KV::layered_value_byte_cost_size_for_key_and_value_lengths( + *average_key_size as u32, + value_len, + tree_type.inner_node_type(), + ) + sum_tree_addition; + (*weight as u64) + .checked_mul(cost as u64) + .ok_or(Error::Overflow("overflow for mixed tree nodes updates"))? + } + }; + let item_node_updates_cost = match items_size { + None => 0, + Some((average_key_size, average_value_size, average_flags_size, weight)) => { + let flags_len = average_flags_size.unwrap_or(0); + let value_len = average_value_size + flags_len; + let cost = KV::value_byte_cost_size_for_key_and_raw_value_lengths( + *average_key_size as u32, + value_len, + tree_type.inner_node_type(), + ); + (*weight as u64) + .checked_mul(cost as u64) + .ok_or(Error::Overflow("overflow for mixed item nodes updates"))? + } + }; + let reference_node_updates_cost = match references_size { + None => 0, + Some((average_key_size, average_value_size, average_flags_size, weight)) => { + let flags_len = average_flags_size.unwrap_or(0); + let value_len = average_value_size + flags_len; + let cost = KV::value_byte_cost_size_for_key_and_raw_value_lengths( + *average_key_size as u32, + value_len, + tree_type.inner_node_type(), + ); + (*weight as u64) + .checked_mul(cost as u64) + .ok_or(Error::Overflow("overflow for mixed item nodes updates"))? + } + }; + + let total_updates_cost = tree_node_updates_cost + .checked_add(item_node_updates_cost) + .and_then(|c| c.checked_add(reference_node_updates_cost)) + .ok_or(Error::Overflow("overflow for mixed item adding parts"))?; + let total_replaced_bytes = total_updates_cost / weighted_nodes_updated; + if total_replaced_bytes > u32::MAX as u64 { + return Err(Error::Overflow( + "overflow for total replaced bytes more than u32 max", + )); + } + total_replaced_bytes as u32 + } + } + }; + cost.storage_loaded_bytes += match estimated_layer_sizes { + EstimatedLayerSizes::AllSubtrees( + average_key_size, + estimated_sum_trees, + average_flags_size, + ) => { + let flags_len = average_flags_size.unwrap_or(0); + let value_len = LAYER_COST_SIZE + flags_len; + let sum_tree_addition = estimated_sum_trees.estimated_size(grove_version)?; + nodes_updated + * KV::layered_node_byte_cost_size_for_key_and_value_lengths( + *average_key_size as u32, + value_len + sum_tree_addition, + tree_type.inner_node_type(), + ) + } + EstimatedLayerSizes::AllItems(average_key_size, average_item_size, average_flags_size) + | EstimatedLayerSizes::AllReference( + average_key_size, + average_item_size, + average_flags_size, + ) => { + let flags_len = average_flags_size.unwrap_or(0); + let average_value_len = average_item_size + flags_len; + nodes_updated + * KV::node_byte_cost_size_for_key_and_raw_value_lengths( + *average_key_size as u32, + average_value_len, + tree_type.inner_node_type(), + ) + } + EstimatedLayerSizes::Mix { + subtrees_size, + items_size, + references_size, + } => { + let total_weight = subtrees_size + .as_ref() + .map(|(_, _, _, weight)| *weight as u32) + .unwrap_or_default() + + items_size + .as_ref() + .map(|(_, _, _, weight)| *weight as u32) + .unwrap_or_default() + + references_size + .as_ref() + .map(|(_, _, _, weight)| *weight as u32) + .unwrap_or_default(); + if total_weight == 0 { + 0 + } else { + let weighted_nodes_updated = (nodes_updated as u64) + .checked_mul(total_weight as u64) + .ok_or(Error::Overflow("overflow for weights average cost"))?; + let tree_node_updates_cost = subtrees_size + .as_ref() + .map( + |(average_key_size, estimated_sum_trees, average_flags_size, weight)| { + let flags_len = average_flags_size.unwrap_or(0); + let value_len = LAYER_COST_SIZE + flags_len; + let sum_tree_addition = + estimated_sum_trees.estimated_size(grove_version)?; + let cost = KV::layered_node_byte_cost_size_for_key_and_value_lengths( + *average_key_size as u32, + value_len + sum_tree_addition, + tree_type.inner_node_type(), + ); + (*weight as u64) + .checked_mul(cost as u64) + .ok_or(Error::Overflow("overflow for mixed tree nodes updates")) + }, + ) + .unwrap_or(Ok(0))?; + let item_node_updates_cost = items_size + .as_ref() + .map( + |(average_key_size, average_value_size, average_flags_size, weight)| { + let flags_len = average_flags_size.unwrap_or(0); + let value_len = average_value_size + flags_len; + let cost = KV::node_byte_cost_size_for_key_and_raw_value_lengths( + *average_key_size as u32, + value_len, + tree_type.inner_node_type(), + ); + (*weight as u64) + .checked_mul(cost as u64) + .ok_or(Error::Overflow("overflow for mixed item nodes updates")) + }, + ) + .unwrap_or(Ok(0))?; + let reference_node_updates_cost = references_size + .as_ref() + .map( + |(average_key_size, average_value_size, average_flags_size, weight)| { + let flags_len = average_flags_size.unwrap_or(0); + let value_len = average_value_size + flags_len; + let cost = KV::node_byte_cost_size_for_key_and_raw_value_lengths( + *average_key_size as u32, + value_len, + TreeType::NormalTree.inner_node_type(), + ); + (*weight as u64) + .checked_mul(cost as u64) + .ok_or(Error::Overflow("overflow for mixed item nodes updates")) + }, + ) + .unwrap_or(Ok(0))?; + + let total_updates_cost = tree_node_updates_cost + .checked_add(item_node_updates_cost) + .and_then(|c| c.checked_add(reference_node_updates_cost)) + .ok_or(Error::Overflow("overflow for mixed item adding parts"))?; + let total_loaded_bytes = total_updates_cost / weighted_nodes_updated; + if total_loaded_bytes > u32::MAX as u64 { + return Err(Error::Overflow( + "overflow for total replaced bytes more than u32 max", + )); + } + total_loaded_bytes as u32 + } + } + } as u64; + Ok(()) +} + +#[cfg(feature = "minimal")] +/// Add average case cost for propagating a merk +fn add_average_case_merk_propagate_v0( + cost: &mut OperationCost, + input: &EstimatedLayerInformation, + grove_version: &GroveVersion, ) -> Result<(), Error> { let mut nodes_updated = 0; // Propagation requires to recompute and write hashes up to the root let EstimatedLayerInformation { - is_sum_tree, + tree_type, estimated_layer_count, estimated_layer_sizes, } = input; let levels = estimated_layer_count.estimate_levels(); - let in_sum_tree = *is_sum_tree; nodes_updated += levels; if levels > 1 { @@ -437,12 +811,12 @@ pub fn add_average_case_merk_propagate( .map_or(0, |flags_len| flags_len + flags_len.required_space() as u32); // in order to simplify calculations we get the estimated size and remove the // cost for the basic merk - let sum_tree_addition = estimated_sum_trees.estimated_size()?; + let sum_tree_addition = estimated_sum_trees.estimated_size(grove_version)?; nodes_updated * (KV::layered_value_byte_cost_size_for_key_and_value_lengths( *average_key_size as u32, value_len, - *is_sum_tree, + tree_type.inner_node_type(), ) + sum_tree_addition) } EstimatedLayerSizes::AllItems(average_key_size, average_item_size, average_flags_size) @@ -457,7 +831,7 @@ pub fn add_average_case_merk_propagate( * KV::value_byte_cost_size_for_key_and_raw_value_lengths( *average_key_size as u32, average_value_len, - in_sum_tree, + tree_type.inner_node_type(), ) } EstimatedLayerSizes::Mix { @@ -488,11 +862,12 @@ pub fn add_average_case_merk_propagate( Some((average_key_size, estimated_sum_trees, average_flags_size, weight)) => { let flags_len = average_flags_size.unwrap_or(0); let value_len = LAYER_COST_SIZE + flags_len; - let sum_tree_addition = estimated_sum_trees.estimated_size()?; + let sum_tree_addition = + estimated_sum_trees.estimated_size(grove_version)?; let cost = KV::layered_value_byte_cost_size_for_key_and_value_lengths( *average_key_size as u32, value_len, - in_sum_tree, + tree_type.inner_node_type(), ) + sum_tree_addition; (*weight as u64) .checked_mul(cost as u64) @@ -507,7 +882,7 @@ pub fn add_average_case_merk_propagate( let cost = KV::value_byte_cost_size_for_key_and_raw_value_lengths( *average_key_size as u32, value_len, - in_sum_tree, + tree_type.inner_node_type(), ); (*weight as u64) .checked_mul(cost as u64) @@ -522,7 +897,7 @@ pub fn add_average_case_merk_propagate( let cost = KV::value_byte_cost_size_for_key_and_raw_value_lengths( *average_key_size as u32, value_len, - in_sum_tree, + tree_type.inner_node_type(), ); (*weight as u64) .checked_mul(cost as u64) @@ -552,12 +927,12 @@ pub fn add_average_case_merk_propagate( ) => { let flags_len = average_flags_size.unwrap_or(0); let value_len = LAYER_COST_SIZE + flags_len; - let sum_tree_addition = estimated_sum_trees.estimated_size()?; + let sum_tree_addition = estimated_sum_trees.estimated_size(grove_version)?; nodes_updated * KV::layered_node_byte_cost_size_for_key_and_value_lengths( *average_key_size as u32, value_len + sum_tree_addition, - in_sum_tree, + tree_type.inner_node_type(), ) } EstimatedLayerSizes::AllItems(average_key_size, average_item_size, average_flags_size) @@ -572,7 +947,7 @@ pub fn add_average_case_merk_propagate( * KV::node_byte_cost_size_for_key_and_raw_value_lengths( *average_key_size as u32, average_value_len, - in_sum_tree, + tree_type.inner_node_type(), ) } EstimatedLayerSizes::Mix { @@ -604,11 +979,12 @@ pub fn add_average_case_merk_propagate( |(average_key_size, estimated_sum_trees, average_flags_size, weight)| { let flags_len = average_flags_size.unwrap_or(0); let value_len = LAYER_COST_SIZE + flags_len; - let sum_tree_addition = estimated_sum_trees.estimated_size()?; + let sum_tree_addition = + estimated_sum_trees.estimated_size(grove_version)?; let cost = KV::layered_node_byte_cost_size_for_key_and_value_lengths( *average_key_size as u32, value_len + sum_tree_addition, - in_sum_tree, + tree_type.inner_node_type(), ); (*weight as u64) .checked_mul(cost as u64) @@ -625,7 +1001,7 @@ pub fn add_average_case_merk_propagate( let cost = KV::node_byte_cost_size_for_key_and_raw_value_lengths( *average_key_size as u32, value_len, - in_sum_tree, + tree_type.inner_node_type(), ); (*weight as u64) .checked_mul(cost as u64) @@ -642,7 +1018,7 @@ pub fn add_average_case_merk_propagate( let cost = KV::node_byte_cost_size_for_key_and_raw_value_lengths( *average_key_size as u32, value_len, - false, + tree_type.inner_node_type(), // this was changed in v1 ); (*weight as u64) .checked_mul(cost as u64) diff --git a/merk/src/estimated_costs/mod.rs b/merk/src/estimated_costs/mod.rs index 7648246b..0ef4d18c 100644 --- a/merk/src/estimated_costs/mod.rs +++ b/merk/src/estimated_costs/mod.rs @@ -5,6 +5,10 @@ use grovedb_costs::OperationCost; #[cfg(feature = "minimal")] use integer_encoding::VarInt; +#[cfg(feature = "minimal")] +use crate::merk::NodeType; +#[cfg(feature = "minimal")] +use crate::tree_type::TreeType; #[cfg(feature = "minimal")] use crate::{tree::kv::KV, HASH_BLOCK_SIZE_U32, HASH_LENGTH_U32}; @@ -26,17 +30,36 @@ pub const LAYER_COST_SIZE: u32 = 3; /// The cost of a sum value pub const SUM_VALUE_EXTRA_COST: u32 = 9; +#[cfg(any(feature = "minimal", feature = "verify"))] +/// The cost of a count value +pub const COUNT_VALUE_EXTRA_COST: u32 = 9; + +#[cfg(any(feature = "minimal", feature = "verify"))] +/// The cost of a big sum value +pub const BIG_SUM_VALUE_EXTRA_COST: u32 = 16; + #[cfg(feature = "minimal")] /// The cost of a summed subtree layer /// This is the layer size + 9 for the encoded value pub const SUM_LAYER_COST_SIZE: u32 = LAYER_COST_SIZE + SUM_VALUE_EXTRA_COST; +#[cfg(feature = "minimal")] +/// The cost of a summed subtree layer +/// This is the layer size + 9 for the encoded value +pub const SUM_AND_COUNT_LAYER_COST_SIZE: u32 = + LAYER_COST_SIZE + SUM_VALUE_EXTRA_COST + COUNT_VALUE_EXTRA_COST; + +#[cfg(feature = "minimal")] +/// The cost of a summed subtree layer +/// This is the layer size + 16 for the encoded value +pub const BIG_SUM_LAYER_COST_SIZE: u32 = LAYER_COST_SIZE + BIG_SUM_VALUE_EXTRA_COST; + #[cfg(feature = "minimal")] impl KV { - fn encoded_kv_node_size(element_size: u32, is_sum_node: bool) -> u32 { + fn encoded_kv_node_size(element_size: u32, node_type: NodeType) -> u32 { // We always charge 8 bytes for the sum node (even though // it could theoretically be 9 bytes - let sum_node_feature_size = if is_sum_node { 9 } else { 1 }; + let sum_node_feature_size = node_type.feature_len(); // KV holds the state of a node // 32 bytes to encode the hash of the node // 32 bytes to encode the value hash @@ -51,13 +74,13 @@ pub fn add_cost_case_merk_insert( cost: &mut OperationCost, key_len: u32, value_len: u32, - in_tree_using_sums: bool, + in_tree_type: TreeType, ) { cost.seek_count += 1; cost.storage_cost.added_bytes += KV::node_byte_cost_size_for_key_and_raw_value_lengths( key_len, value_len, - in_tree_using_sums, + in_tree_type.inner_node_type(), ); // .. and hash computation for the inserted element itself // first lets add the value hash @@ -75,13 +98,13 @@ pub fn add_cost_case_merk_insert_layered( cost: &mut OperationCost, key_len: u32, value_len: u32, - in_tree_using_sums: bool, + in_tree_type: TreeType, ) { cost.seek_count += 1; cost.storage_cost.added_bytes += KV::layered_node_byte_cost_size_for_key_and_value_lengths( key_len, value_len, - in_tree_using_sums, + in_tree_type.inner_node_type(), ); // .. and hash computation for the inserted element itself // first lets add the value hash @@ -101,11 +124,11 @@ pub fn add_cost_case_merk_replace( cost: &mut OperationCost, key_len: u32, value_len: u32, - in_tree_using_sums: bool, + in_tree_type: TreeType, ) { cost.seek_count += 1; cost.storage_cost.added_bytes += - KV::node_value_byte_cost_size(key_len, value_len, in_tree_using_sums); + KV::node_value_byte_cost_size(key_len, value_len, in_tree_type.inner_node_type()); cost.storage_cost.replaced_bytes += KV::node_key_byte_cost_size(key_len); // .. and hash computation for the inserted element itself // first lets add the value hash @@ -124,13 +147,13 @@ pub fn add_cost_case_merk_replace_same_size( cost: &mut OperationCost, key_len: u32, value_len: u32, - in_tree_using_sums: bool, + in_tree_type: TreeType, ) { cost.seek_count += 1; cost.storage_cost.replaced_bytes += KV::node_byte_cost_size_for_key_and_raw_value_lengths( key_len, value_len, - in_tree_using_sums, + in_tree_type.inner_node_type(), ); // .. and hash computation for the inserted element itself // first lets add the value hash @@ -148,13 +171,13 @@ pub fn add_cost_case_merk_replace_layered( cost: &mut OperationCost, key_len: u32, value_len: u32, - in_tree_using_sums: bool, + in_tree_type: TreeType, ) { cost.seek_count += 1; cost.storage_cost.replaced_bytes += KV::layered_node_byte_cost_size_for_key_and_value_lengths( key_len, value_len, - in_tree_using_sums, + in_tree_type.inner_node_type(), ); // .. and hash computation for the inserted element itself // first lets add the value hash @@ -176,7 +199,7 @@ pub fn add_cost_case_merk_patch( key_len: u32, value_len: u32, change_in_bytes: i32, - in_tree_using_sums: bool, + in_tree_type: TreeType, ) { cost.seek_count += 1; if change_in_bytes >= 0 { @@ -185,12 +208,12 @@ pub fn add_cost_case_merk_patch( let old_byte_size = KV::node_byte_cost_size_for_key_and_raw_value_lengths( key_len, value_len - change_in_bytes as u32, - in_tree_using_sums, + in_tree_type.inner_node_type(), ); let new_byte_size = KV::node_byte_cost_size_for_key_and_raw_value_lengths( key_len, value_len, - in_tree_using_sums, + in_tree_type.inner_node_type(), ); cost.storage_cost.replaced_bytes += old_byte_size; @@ -199,7 +222,7 @@ pub fn add_cost_case_merk_patch( cost.storage_cost.replaced_bytes += KV::node_byte_cost_size_for_key_and_raw_value_lengths( key_len, value_len, - in_tree_using_sums, + in_tree_type.inner_node_type(), ); } diff --git a/merk/src/estimated_costs/worst_case_costs.rs b/merk/src/estimated_costs/worst_case_costs.rs index 3cbd6399..a9ef19e8 100644 --- a/merk/src/estimated_costs/worst_case_costs.rs +++ b/merk/src/estimated_costs/worst_case_costs.rs @@ -33,6 +33,8 @@ use std::cmp::Ordering; #[cfg(feature = "minimal")] use grovedb_costs::{CostResult, CostsExt, OperationCost}; +#[cfg(feature = "minimal")] +use crate::merk::NodeType; #[cfg(feature = "minimal")] use crate::{ error::Error, @@ -57,13 +59,13 @@ impl TreeNode { pub fn worst_case_encoded_tree_size( not_prefixed_key_len: u32, max_element_size: u32, - is_sum_node: bool, + node_type: NodeType, ) -> u32 { // two option values for the left and right link // the actual left and right link encoding size // the encoded kv node size - 2 + (2 * Link::encoded_link_size(not_prefixed_key_len, is_sum_node)) - + KV::encoded_kv_node_size(max_element_size, is_sum_node) + 2 + (2 * Link::encoded_link_size(not_prefixed_key_len, node_type)) + + KV::encoded_kv_node_size(max_element_size, node_type) } } @@ -73,7 +75,7 @@ pub fn add_worst_case_get_merk_node( cost: &mut OperationCost, not_prefixed_key_len: u32, max_element_size: u32, - is_sum_node: bool, + node_type: NodeType, ) -> Result<(), Error> { // Worst case scenario, the element is not already in memory. // One direct seek has to be performed to read the node from storage. @@ -82,7 +84,7 @@ pub fn add_worst_case_get_merk_node( // To write a node to disk, the left link, right link and kv nodes are encoded. // worst case, the node has both the left and right link present. cost.storage_loaded_bytes += - TreeNode::worst_case_encoded_tree_size(not_prefixed_key_len, max_element_size, is_sum_node) + TreeNode::worst_case_encoded_tree_size(not_prefixed_key_len, max_element_size, node_type) as u64; Ok(()) } @@ -104,10 +106,10 @@ pub fn add_worst_case_merk_insert( cost: &mut OperationCost, key_len: u32, value_len: u32, - is_sum_node: bool, + node_type: NodeType, ) { cost.storage_cost.added_bytes += - KV::node_byte_cost_size_for_key_and_raw_value_lengths(key_len, value_len, is_sum_node); + KV::node_byte_cost_size_for_key_and_raw_value_lengths(key_len, value_len, node_type); // .. and hash computation for the inserted element itself // todo: verify this cost.hash_node_calls += 1 + ((value_len - 1) / HASH_BLOCK_SIZE_U32); @@ -119,12 +121,12 @@ pub fn add_worst_case_merk_replace_layered( cost: &mut OperationCost, key_len: u32, value_len: u32, - is_sum_node: bool, + node_type: NodeType, ) { // todo: verify this cost.hash_node_calls += 1 + ((value_len - 1) / HASH_BLOCK_SIZE_U32); cost.storage_cost.replaced_bytes = - KV::layered_value_byte_cost_size_for_key_and_value_lengths(key_len, value_len, is_sum_node); + KV::layered_value_byte_cost_size_for_key_and_value_lengths(key_len, value_len, node_type); // 37 + 35 + key_len } diff --git a/merk/src/lib.rs b/merk/src/lib.rs index 0291314b..51d15afb 100644 --- a/merk/src/lib.rs +++ b/merk/src/lib.rs @@ -65,6 +65,8 @@ pub mod error; #[cfg(any(feature = "minimal", feature = "verify"))] pub mod estimated_costs; +#[cfg(any(feature = "minimal", feature = "verify"))] +pub mod tree_type; #[cfg(feature = "minimal")] mod visualize; @@ -79,12 +81,16 @@ pub use tree::{ }; #[cfg(any(feature = "minimal", feature = "verify"))] pub use tree::{CryptoHash, TreeFeatureType}; +#[cfg(any(feature = "minimal", feature = "verify"))] +pub use tree_type::MaybeTree; +#[cfg(any(feature = "minimal", feature = "verify"))] +pub use tree_type::TreeType; #[cfg(feature = "minimal")] pub use crate::merk::{ defaults::ROOT_KEY_KEY, prove::{ProofConstructionResult, ProofWithoutEncodingResult}, - IsSumTree, KVIterator, Merk, MerkType, RootHashKeyAndSum, + KVIterator, Merk, MerkType, RootHashKeyAndAggregateData, }; #[cfg(feature = "minimal")] pub use crate::visualize::VisualizeableMerk; diff --git a/merk/src/merk/apply.rs b/merk/src/merk/apply.rs index 9c5c9ec9..e524bac6 100644 --- a/merk/src/merk/apply.rs +++ b/merk/src/merk/apply.rs @@ -11,6 +11,7 @@ use grovedb_storage::StorageContext; use grovedb_version::version::GroveVersion; use crate::{ + merk::NodeType, tree::{ kv::{ValueDefinedCostType, KV}, AuxMerkBatch, Walker, @@ -64,7 +65,7 @@ where KB: AsRef<[u8]>, KA: AsRef<[u8]>, { - let use_sum_nodes = self.is_sum_tree; + let node_type: NodeType = self.tree_type.inner_node_type(); self.apply_with_costs_just_in_time_value_update( batch, aux, @@ -73,7 +74,7 @@ where Ok(KV::layered_value_byte_cost_size_for_key_and_value_lengths( key.len() as u32, value.len() as u32, - use_sum_nodes, + node_type, )) }, None::<&fn(&[u8], &GroveVersion) -> Option>, diff --git a/merk/src/merk/committer.rs b/merk/src/merk/committer.rs index 9fb02987..49e4fbdc 100644 --- a/merk/src/merk/committer.rs +++ b/merk/src/merk/committer.rs @@ -44,7 +44,8 @@ impl Commit for MerkCommitter { let right_child_sizes = tree.child_ref_and_sum_size(false); self.batch.push(( tree.key().to_vec(), - tree.feature_type().sum_length(), + tree.feature_type() + .tree_feature_specialized_type_and_length(), Some((buf, left_child_sizes, right_child_sizes)), storage_costs, )); diff --git a/merk/src/merk/mod.rs b/merk/src/merk/mod.rs index ee0deccc..27d679c1 100644 --- a/merk/src/merk/mod.rs +++ b/merk/src/merk/mod.rs @@ -52,7 +52,7 @@ use committer::MerkCommitter; use grovedb_costs::{ cost_return_on_error, cost_return_on_error_default, cost_return_on_error_no_add, storage_cost::key_value_cost::KeyValueStorageCost, ChildrenSizesWithValue, CostContext, - CostResult, CostsExt, FeatureSumLength, OperationCost, + CostResult, CostsExt, FeatureSumLength, OperationCost, TreeCostType, }; use grovedb_storage::{self, Batch, RawIterator, StorageContext}; use grovedb_version::version::GroveVersion; @@ -70,8 +70,10 @@ use crate::{ Query, }, tree::{ - kv::ValueDefinedCostType, AuxMerkBatch, CryptoHash, Op, RefWalker, TreeNode, NULL_HASH, + kv::ValueDefinedCostType, AggregateData, AuxMerkBatch, CryptoHash, Op, RefWalker, TreeNode, + NULL_HASH, }, + tree_type::TreeType, Error::{CostsError, EdError, StorageError}, Link, MerkType::{BaseMerk, LayeredMerk, StandaloneMerk}, @@ -105,16 +107,13 @@ impl KeyUpdates { /// Type alias for simple function signature pub type BatchValue = ( Vec, - Option, + Option<(TreeCostType, FeatureSumLength)>, ChildrenSizesWithValue, KeyValueStorageCost, ); -/// A bool type -pub type IsSumTree = bool; - /// Root hash key and sum -pub type RootHashKeyAndSum = (CryptoHash, Option>, Option); +pub type RootHashKeyAndAggregateData = (CryptoHash, Option>, AggregateData); /// KVIterator allows you to lazily iterate over each kv pair of a subtree pub struct KVIterator<'a, I: RawIterator> { @@ -243,6 +242,38 @@ impl MerkType { } } +#[cfg(any(feature = "minimal", feature = "verify"))] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum NodeType { + NormalNode, + SumNode, + BigSumNode, + CountNode, + CountSumNode, +} + +impl NodeType { + pub const fn feature_len(&self) -> u32 { + match self { + NodeType::NormalNode => 1, + NodeType::SumNode => 9, + NodeType::BigSumNode => 17, + NodeType::CountNode => 9, + NodeType::CountSumNode => 17, + } + } + + pub const fn cost(&self) -> u32 { + match self { + NodeType::NormalNode => 0, + NodeType::SumNode => 8, + NodeType::BigSumNode => 16, + NodeType::CountNode => 8, + NodeType::CountSumNode => 16, + } + } +} + /// A handle to a Merkle key/value store backed by RocksDB. pub struct Merk { pub(crate) tree: Cell>, @@ -251,8 +282,8 @@ pub struct Merk { pub storage: S, /// Merk type pub merk_type: MerkType, - /// Is sum tree? - pub is_sum_tree: bool, + /// The tree type + pub tree_type: TreeType, } impl fmt::Debug for Merk { @@ -265,7 +296,7 @@ impl fmt::Debug for Merk { pub type UseTreeMutResult = CostResult< Vec<( Vec, - Option, + Option<(TreeCostType, FeatureSumLength)>, ChildrenSizesWithValue, KeyValueStorageCost, )>, @@ -295,11 +326,11 @@ where res } - /// Returns the total sum value in the Merk tree - pub fn sum(&self) -> Result, Error> { + /// Returns the total aggregate data in the Merk tree + pub fn aggregate_data(&self) -> Result { self.use_tree(|tree| match tree { - None => Ok(None), - Some(tree) => tree.sum(), + None => Ok(AggregateData::NoAggregateData), + Some(tree) => tree.aggregate_data(), }) } @@ -315,13 +346,16 @@ where } /// Returns the root hash and non-prefixed key of the tree. - pub fn root_hash_key_and_sum(&self) -> CostResult { + pub fn root_hash_key_and_aggregate_data( + &self, + ) -> CostResult { self.use_tree(|tree| match tree { - None => Ok((NULL_HASH, None, None)).wrap_with_cost(Default::default()), + None => Ok((NULL_HASH, None, AggregateData::NoAggregateData)) + .wrap_with_cost(Default::default()), Some(tree) => { - let sum = cost_return_on_error_default!(tree.sum()); + let aggregate_data = cost_return_on_error_default!(tree.aggregate_data()); tree.hash() - .map(|hash| Ok((hash, Some(tree.key().to_vec()), sum))) + .map(|hash| Ok((hash, Some(tree.key().to_vec()), aggregate_data))) } }) } @@ -663,21 +697,28 @@ where skip_sum_checks: bool, grove_version: &GroveVersion, ) { - let (hash, key, sum) = match link { - Link::Reference { hash, key, sum, .. } => { - (hash.to_owned(), key.to_owned(), sum.to_owned()) - } + let (hash, key, aggregate_data) = match link { + Link::Reference { + hash, + key, + aggregate_data, + .. + } => (hash.to_owned(), key.to_owned(), aggregate_data.to_owned()), Link::Modified { tree, .. } => ( tree.hash().unwrap(), tree.key().to_vec(), - tree.sum().unwrap(), + tree.aggregate_data().unwrap(), ), Link::Loaded { hash, child_heights: _, - sum, + aggregate_data, tree, - } => (hash.to_owned(), tree.key().to_vec(), sum.to_owned()), + } => ( + hash.to_owned(), + tree.key().to_vec(), + aggregate_data.to_owned(), + ), _ => todo!(), }; @@ -711,7 +752,7 @@ where } // Need to skip this when restoring a sum tree - if !skip_sum_checks && node.sum().unwrap() != sum { + if !skip_sum_checks && node.aggregate_data().unwrap() != aggregate_data { bad_link_map.insert(instruction_id.to_vec(), hash); parent_keys.insert(instruction_id.to_vec(), parent_key.to_vec()); return; @@ -762,10 +803,9 @@ mod test { use super::{Merk, RefWalker}; use crate::{ - merk::source::MerkSource, test_utils::*, tree::kv::ValueDefinedCostType, Op, - TreeFeatureType::BasicMerkNode, + merk::source::MerkSource, test_utils::*, tree::kv::ValueDefinedCostType, + tree_type::TreeType, Op, TreeFeatureType::BasicMerkNode, }; - // TODO: Close and then reopen test fn assert_invariants(merk: &TempMerk) { @@ -991,7 +1031,7 @@ mod test { storage .get_storage_context(SubtreePath::empty(), None) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1017,7 +1057,7 @@ mod test { storage .get_storage_context(SubtreePath::empty(), None) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1073,7 +1113,7 @@ mod test { storage .get_storage_context(SubtreePath::empty(), Some(&batch)) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1092,7 +1132,7 @@ mod test { storage .get_storage_context(SubtreePath::empty(), None) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1113,7 +1153,7 @@ mod test { storage .get_storage_context(SubtreePath::empty(), None) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1153,7 +1193,7 @@ mod test { storage .get_storage_context(SubtreePath::empty(), Some(&batch)) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1174,7 +1214,7 @@ mod test { storage .get_storage_context(SubtreePath::empty(), None) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1189,7 +1229,7 @@ mod test { storage .get_storage_context(SubtreePath::empty(), None) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1213,7 +1253,7 @@ mod test { storage .get_storage_context(SubtreePath::empty(), Some(&batch)) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1277,7 +1317,7 @@ mod test { storage .get_storage_context(SubtreePath::empty(), None) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) diff --git a/merk/src/merk/open.rs b/merk/src/merk/open.rs index c8646afa..a3d4c16e 100644 --- a/merk/src/merk/open.rs +++ b/merk/src/merk/open.rs @@ -6,6 +6,7 @@ use grovedb_version::version::GroveVersion; use crate::{ tree::kv::ValueDefinedCostType, + tree_type::TreeType, Error, Merk, MerkType, MerkType::{BaseMerk, LayeredMerk, StandaloneMerk}, }; @@ -15,20 +16,20 @@ where S: StorageContext<'db>, { /// Open empty tree - pub fn open_empty(storage: S, merk_type: MerkType, is_sum_tree: bool) -> Self { + pub fn open_empty(storage: S, merk_type: MerkType, tree_type: TreeType) -> Self { Self { tree: Cell::new(None), root_tree_key: Cell::new(None), storage, merk_type, - is_sum_tree, + tree_type, } } /// Open standalone tree pub fn open_standalone( storage: S, - is_sum_tree: bool, + tree_type: TreeType, value_defined_cost_fn: Option< impl Fn(&[u8], &GroveVersion) -> Option, >, @@ -39,7 +40,7 @@ where root_tree_key: Cell::new(None), storage, merk_type: StandaloneMerk, - is_sum_tree, + tree_type, }; merk.load_base_root(value_defined_cost_fn, grove_version) @@ -49,7 +50,7 @@ where /// Open base tree pub fn open_base( storage: S, - is_sum_tree: bool, + tree_type: TreeType, value_defined_cost_fn: Option< impl Fn(&[u8], &GroveVersion) -> Option, >, @@ -60,7 +61,7 @@ where root_tree_key: Cell::new(None), storage, merk_type: BaseMerk, - is_sum_tree, + tree_type, }; merk.load_base_root(value_defined_cost_fn, grove_version) @@ -71,7 +72,7 @@ where pub fn open_layered_with_root_key( storage: S, root_key: Option>, - is_sum_tree: bool, + tree_type: TreeType, value_defined_cost_fn: Option< impl Fn(&[u8], &GroveVersion) -> Option, >, @@ -82,7 +83,7 @@ where root_tree_key: Cell::new(root_key), storage, merk_type: LayeredMerk, - is_sum_tree, + tree_type, }; merk.load_root(value_defined_cost_fn, grove_version) @@ -101,7 +102,10 @@ mod test { use grovedb_version::version::GroveVersion; use tempfile::TempDir; - use crate::{tree::kv::ValueDefinedCostType, Merk, Op, TreeFeatureType::BasicMerkNode}; + use crate::{ + tree::kv::ValueDefinedCostType, tree_type::TreeType, Merk, Op, + TreeFeatureType::BasicMerkNode, + }; #[test] fn test_reopen_root_hash() { @@ -116,7 +120,7 @@ mod test { storage .get_storage_context(SubtreePath::from(test_prefix.as_ref()), Some(&batch)) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -143,7 +147,7 @@ mod test { storage .get_storage_context(SubtreePath::from(test_prefix.as_ref()), None) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -162,7 +166,7 @@ mod test { storage .get_storage_context(SubtreePath::empty(), Some(&batch)) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ); @@ -192,7 +196,7 @@ mod test { storage .get_storage_context(SubtreePath::empty(), None) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ); diff --git a/merk/src/merk/restore.rs b/merk/src/merk/restore.rs index 1082e80b..0c1784fd 100644 --- a/merk/src/merk/restore.rs +++ b/merk/src/merk/restore.rs @@ -48,6 +48,7 @@ use crate::{ Node, Op, }, tree::{combine_hash, kv::ValueDefinedCostType, RefWalker, TreeNode}, + tree_type::TreeType, CryptoHash, Error, Error::{CostsError, StorageError}, Link, Merk, @@ -315,11 +316,16 @@ impl<'db, S: StorageContext<'db>> Restorer { .expect("rewrite is only called when traversal_instruction is not empty"); let updated_key = chunk_tree.key(); - let updated_sum = chunk_tree.sum(); + let updated_sum = chunk_tree.aggregate_data(); - if let Some(Link::Reference { key, sum, .. }) = parent.link_mut(*is_left) { + if let Some(Link::Reference { + key, + aggregate_data, + .. + }) = parent.link_mut(*is_left) + { *key = updated_key.to_vec(); - *sum = updated_sum; + *aggregate_data = updated_sum; } let parent_bytes = parent.encode(); @@ -449,7 +455,7 @@ impl<'db, S: StorageContext<'db>> Restorer { if !self .merk - .verify(self.merk.is_sum_tree, grove_version) + .verify(self.merk.tree_type == TreeType::NormalTree, grove_version) .0 .is_empty() { @@ -561,6 +567,7 @@ mod tests { chunk::tests::traverse_get_node_hash, error::ChunkError::InvalidChunkProof, }, test_utils::{make_batch_seq, TempMerk}, + tree_type::TreeType, Error::ChunkRestoringError, Merk, PanicSource, }; @@ -682,7 +689,7 @@ mod tests { storage .get_immediate_storage_context(SubtreePath::empty(), &tx) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -932,7 +939,7 @@ mod tests { storage .get_immediate_storage_context(SubtreePath::empty(), &tx) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -951,7 +958,7 @@ mod tests { storage .get_immediate_storage_context(SubtreePath::empty(), &tx) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1024,7 +1031,7 @@ mod tests { storage .get_immediate_storage_context(SubtreePath::empty(), &tx) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1093,7 +1100,7 @@ mod tests { storage .get_immediate_storage_context(SubtreePath::empty(), &tx) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1175,7 +1182,7 @@ mod tests { storage .get_immediate_storage_context(SubtreePath::empty(), &tx) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1277,7 +1284,7 @@ mod tests { storage .get_immediate_storage_context(SubtreePath::empty(), &tx) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1359,7 +1366,7 @@ mod tests { storage .get_immediate_storage_context(SubtreePath::empty(), &tx) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) @@ -1409,7 +1416,7 @@ mod tests { storage .get_immediate_storage_context(SubtreePath::empty(), &tx) .unwrap(), - false, + TreeType::NormalTree, None::<&fn(&[u8], &GroveVersion) -> Option>, grove_version, ) diff --git a/merk/src/merk/source.rs b/merk/src/merk/source.rs index dd71e74e..7c7568be 100644 --- a/merk/src/merk/source.rs +++ b/merk/src/merk/source.rs @@ -4,6 +4,7 @@ use grovedb_version::version::GroveVersion; use crate::{ tree::{kv::ValueDefinedCostType, Fetch, TreeNode}, + tree_type::TreeType, Error, Link, Merk, }; @@ -14,7 +15,7 @@ where pub(in crate::merk) fn source(&self) -> MerkSource { MerkSource { storage: &self.storage, - is_sum_tree: self.is_sum_tree, + tree_type: self.tree_type, } } } @@ -22,14 +23,14 @@ where #[derive(Debug)] pub struct MerkSource<'s, S> { storage: &'s S, - is_sum_tree: bool, + tree_type: TreeType, } impl<'s, S> Clone for MerkSource<'s, S> { fn clone(&self) -> Self { MerkSource { storage: self.storage, - is_sum_tree: self.is_sum_tree, + tree_type: self.tree_type, } } } diff --git a/merk/src/proofs/tree.rs b/merk/src/proofs/tree.rs index 4b2037fe..dafd09aa 100644 --- a/merk/src/proofs/tree.rs +++ b/merk/src/proofs/tree.rs @@ -18,8 +18,8 @@ use crate::{error::Error, tree::CryptoHash}; #[cfg(feature = "minimal")] use crate::{ proofs::chunk::chunk::{LEFT, RIGHT}, + tree::AggregateData, Link, - TreeFeatureType::SummedMerkNode, }; #[cfg(any(feature = "minimal", feature = "verify"))] @@ -36,24 +36,22 @@ pub struct Child { impl Child { #[cfg(feature = "minimal")] pub fn as_link(&self) -> Link { - let (key, sum) = match &self.tree.node { - Node::KV(key, _) | Node::KVValueHash(key, ..) => (key.as_slice(), None), + let (key, aggregate_data) = match &self.tree.node { + Node::KV(key, _) | Node::KVValueHash(key, ..) => { + (key.as_slice(), AggregateData::NoAggregateData) + } Node::KVValueHashFeatureType(key, _, _, feature_type) => { - let sum_value = match feature_type { - SummedMerkNode(sum) => Some(*sum), - _ => None, - }; - (key.as_slice(), sum_value) + (key.as_slice(), (*feature_type).into()) } // for the connection between the trunk and leaf chunks, we don't // have the child key so we must first write in an empty one. once // the leaf gets verified, we can write in this key to its parent - _ => (&[] as &[u8], None), + _ => (&[] as &[u8], AggregateData::NoAggregateData), }; Link::Reference { hash: self.hash, - sum, + aggregate_data, child_heights: ( self.tree.child_heights.0 as u8, self.tree.child_heights.1 as u8, @@ -294,12 +292,9 @@ impl Tree { } #[cfg(feature = "minimal")] - pub(crate) fn sum(&self) -> Option { + pub(crate) fn aggregate_data(&self) -> AggregateData { match self.node { - Node::KVValueHashFeatureType(.., feature_type) => match feature_type { - SummedMerkNode(sum) => Some(sum), - _ => None, - }, + Node::KVValueHashFeatureType(.., feature_type) => feature_type.into(), _ => panic!("Expected node to be type KVValueHashFeatureType"), } } @@ -527,6 +522,7 @@ where #[cfg(test)] mod test { use super::{super::*, Tree as ProofTree, *}; + use crate::TreeFeatureType::SummedMerkNode; fn make_7_node_prooftree() -> ProofTree { let make_node = |i| -> super::super::tree::Tree { Node::KV(vec![i], vec![]).into() }; @@ -639,7 +635,7 @@ mod test { left_link, Link::Reference { hash: tree.left.as_ref().map(|node| node.hash).unwrap(), - sum: None, + aggregate_data: AggregateData::NoAggregateData, child_heights: (0, 0), key: vec![1] } @@ -649,7 +645,7 @@ mod test { right_link, Link::Reference { hash: tree.right.as_ref().map(|node| node.hash).unwrap(), - sum: None, + aggregate_data: AggregateData::NoAggregateData, child_heights: (0, 0), key: vec![3] } @@ -688,7 +684,7 @@ mod test { left_link, Link::Reference { hash: tree.left.as_ref().map(|node| node.hash).unwrap(), - sum: Some(3), + aggregate_data: AggregateData::Sum(3), child_heights: (0, 0), key: vec![1] } @@ -698,7 +694,7 @@ mod test { right_link, Link::Reference { hash: tree.right.as_ref().map(|node| node.hash).unwrap(), - sum: Some(1), + aggregate_data: AggregateData::Sum(1), child_heights: (0, 0), key: vec![3] } diff --git a/merk/src/test_utils/mod.rs b/merk/src/test_utils/mod.rs index 45beda4f..76eec948 100644 --- a/merk/src/test_utils/mod.rs +++ b/merk/src/test_utils/mod.rs @@ -44,6 +44,7 @@ use crate::{ kv::{ValueDefinedCostType, KV}, BatchEntry, MerkBatch, NoopCommit, Op, PanicSource, TreeNode, Walker, }, + tree_type::TreeType, Merk, TreeFeatureType::{BasicMerkNode, SummedMerkNode}, }; @@ -80,7 +81,7 @@ pub fn apply_memonly_unchecked( batch: &MerkBatch>, grove_version: &GroveVersion, ) -> TreeNode { - let is_sum_node = tree.is_sum_node(); + let node_type = tree.node_type(); let walker = Walker::::new(tree, PanicSource {}); let mut tree = Walker::::apply_to( Some(walker), @@ -90,7 +91,7 @@ pub fn apply_memonly_unchecked( Ok(KV::layered_value_byte_cost_size_for_key_and_value_lengths( key.len() as u32, value.len() as u32, - is_sum_node, + node_type, )) }, None::<&fn(&[u8], &GroveVersion) -> Option>, @@ -108,12 +109,12 @@ pub fn apply_memonly_unchecked( .expect("apply failed") .0 .expect("expected tree"); - let is_sum_node = tree.is_sum_node(); + let node_type = tree.node_type(); tree.commit(&mut NoopCommit {}, &|key, value| { Ok(KV::layered_value_byte_cost_size_for_key_and_value_lengths( key.len() as u32, value.len() as u32, - is_sum_node, + node_type, )) }) .unwrap() @@ -138,7 +139,7 @@ pub fn apply_memonly( pub fn apply_to_memonly( maybe_tree: Option, batch: &MerkBatch>, - is_sum_tree: bool, + tree_type: TreeType, grove_version: &GroveVersion, ) -> Option { let maybe_walker = maybe_tree.map(|tree| Walker::::new(tree, PanicSource {})); @@ -150,7 +151,7 @@ pub fn apply_to_memonly( Ok(KV::layered_value_byte_cost_size_for_key_and_value_lengths( key.len() as u32, value.len() as u32, - is_sum_tree, + tree_type.inner_node_type(), )) }, None::<&fn(&[u8], &GroveVersion) -> Option>, @@ -168,12 +169,12 @@ pub fn apply_to_memonly( .expect("apply failed") .0 .map(|mut tree| { - let is_sum_node = tree.is_sum_node(); + let node_type = tree.node_type(); tree.commit(&mut NoopCommit {}, &|key, value| { Ok(KV::layered_value_byte_cost_size_for_key_and_value_lengths( key.len() as u32, value.len() as u32, - is_sum_node, + node_type, )) }) .unwrap() @@ -320,7 +321,7 @@ where storage .get_storage_context(SubtreePath::empty(), Some(batch)) .unwrap(), - false, + TreeType::NormalTree, None:: Option>, grove_version, ) @@ -340,7 +341,7 @@ where storage .get_storage_context(SubtreePath::empty(), None) .unwrap(), - false, + TreeType::NormalTree, None:: Option>, grove_version, ) diff --git a/merk/src/test_utils/temp_merk.rs b/merk/src/test_utils/temp_merk.rs index 9a059712..a9b3b26e 100644 --- a/merk/src/test_utils/temp_merk.rs +++ b/merk/src/test_utils/temp_merk.rs @@ -40,9 +40,9 @@ use grovedb_storage::{ }; use grovedb_version::version::GroveVersion; -use crate::tree::kv::ValueDefinedCostType; #[cfg(feature = "minimal")] use crate::Merk; +use crate::{tree::kv::ValueDefinedCostType, tree_type::TreeType}; #[cfg(feature = "minimal")] /// Wraps a Merk instance and deletes it from disk it once it goes out of scope. @@ -66,7 +66,7 @@ impl TempMerk { let merk = Merk::open_base( context, - false, + TreeType::NormalTree, None:: Option>, grove_version, ) @@ -93,7 +93,7 @@ impl TempMerk { .unwrap(); self.merk = Merk::open_base( context, - false, + TreeType::NormalTree, None:: Option>, grove_version, ) diff --git a/merk/src/tree/encoding.rs b/merk/src/tree/encoding.rs index 1e1a3bea..2ef07cf5 100644 --- a/merk/src/tree/encoding.rs +++ b/merk/src/tree/encoding.rs @@ -147,7 +147,10 @@ impl TreeNode { #[cfg(test)] mod tests { use super::{super::Link, *}; - use crate::TreeFeatureType::{BasicMerkNode, SummedMerkNode}; + use crate::{ + tree::AggregateData, + TreeFeatureType::{BasicMerkNode, SummedMerkNode}, + }; #[test] fn encode_leaf_tree() { @@ -196,7 +199,7 @@ mod tests { [55; 32], Some(Link::Loaded { hash: [66; 32], - sum: None, + aggregate_data: AggregateData::NoAggregateData, child_heights: (123, 124), tree: TreeNode::new(vec![2], vec![3], None, BasicMerkNode).unwrap(), }), @@ -225,7 +228,7 @@ mod tests { [55; 32], Some(Link::Uncommitted { hash: [66; 32], - sum: Some(10), + aggregate_data: AggregateData::Sum(10), child_heights: (123, 124), tree: TreeNode::new(vec![2], vec![3], None, BasicMerkNode).unwrap(), }), @@ -254,7 +257,7 @@ mod tests { [55; 32], Some(Link::Reference { hash: [66; 32], - sum: None, + aggregate_data: AggregateData::NoAggregateData, child_heights: (123, 124), key: vec![2], }), @@ -328,7 +331,7 @@ mod tests { key, child_heights, hash, - sum: _, + aggregate_data: _, }) = tree.link(true) { assert_eq!(*key, [2]); diff --git a/merk/src/tree/kv.rs b/merk/src/tree/kv.rs index 7de707db..8ad5349b 100644 --- a/merk/src/tree/kv.rs +++ b/merk/src/tree/kv.rs @@ -12,7 +12,11 @@ use integer_encoding::VarInt; #[cfg(feature = "minimal")] use super::hash::{CryptoHash, HASH_LENGTH, NULL_HASH}; -use crate::tree::kv::ValueDefinedCostType::{LayeredValueDefinedCost, SpecializedValueDefinedCost}; +#[cfg(feature = "minimal")] +use crate::{ + merk::NodeType, + tree::kv::ValueDefinedCostType::{LayeredValueDefinedCost, SpecializedValueDefinedCost}, +}; #[cfg(feature = "minimal")] use crate::{ tree::{ @@ -21,7 +25,6 @@ use crate::{ }, Link, HASH_LENGTH_U32, HASH_LENGTH_U32_X2, }; - // TODO: maybe use something similar to Vec but without capacity field, // (should save 16 bytes per entry). also, maybe a shorter length // field to save even more. also might be possible to combine key @@ -275,16 +278,16 @@ impl KV { pub fn node_value_byte_cost_size( not_prefixed_key_len: u32, raw_value_len: u32, - is_sum_node: bool, + node_type: NodeType, ) -> u32 { // Sum trees are either 1 or 9 bytes. While they might be more or less on disk, // costs can not take advantage of the varint aspect of the feature. - let feature_len = if is_sum_node { 9 } else { 1 }; + let feature_len = node_type.feature_len(); let value_size = raw_value_len + HASH_LENGTH_U32_X2 + feature_len; // The node will be a child of another node which stores it's key and hash // That will be added during propagation - let parent_to_child_cost = Link::encoded_link_size(not_prefixed_key_len, is_sum_node); + let parent_to_child_cost = Link::encoded_link_size(not_prefixed_key_len, node_type); value_size + value_size.required_space() as u32 + parent_to_child_cost } @@ -294,10 +297,10 @@ impl KV { pub fn node_byte_cost_size_for_key_and_raw_value_lengths( not_prefixed_key_len: u32, raw_value_len: u32, - is_sum_node: bool, + node_type: NodeType, ) -> u32 { let node_value_size = - Self::node_value_byte_cost_size(not_prefixed_key_len, raw_value_len, is_sum_node); + Self::node_value_byte_cost_size(not_prefixed_key_len, raw_value_len, node_type); let node_key_size = Self::node_key_byte_cost_size(not_prefixed_key_len); // Each node stores the key and value, the value hash and node hash node_value_size + node_key_size @@ -308,11 +311,11 @@ impl KV { pub fn layered_node_byte_cost_size_for_key_and_value_lengths( not_prefixed_key_len: u32, value_len: u32, - is_sum_node: bool, // this means the node is contained in a sumtree + node_type: NodeType, ) -> u32 { // Sum trees are either 1 or 9 bytes. While they might be more or less on disk, // costs can not take advantage of the varint aspect of the feature. - let feature_len = if is_sum_node { 9 } else { 1 }; + let feature_len = node_type.feature_len(); // Each node stores the key and value, and the node hash // the value hash on a layered node is not stored directly in the node @@ -326,7 +329,7 @@ impl KV { let node_size = node_value_size + node_key_size; // The node will be a child of another node which stores it's key and hash // That will be added during propagation - let parent_to_child_cost = Link::encoded_link_size(not_prefixed_key_len, is_sum_node); + let parent_to_child_cost = Link::encoded_link_size(not_prefixed_key_len, node_type); node_size + parent_to_child_cost } @@ -336,11 +339,12 @@ impl KV { pub fn layered_value_byte_cost_size_for_key_and_value_lengths( not_prefixed_key_len: u32, value_len: u32, - is_sum_node: bool, + node_type: NodeType, ) -> u32 { - // Sum trees are either 1 or 9 bytes. While they might be more or less on disk, + // Sum trees are either 1 or 9 bytes, or 16 bytes for the big sum trees. + // While they might be more or less on disk, // costs can not take advantage of the varint aspect of the feature. - let feature_len = if is_sum_node { 9 } else { 1 }; + let feature_len = node_type.feature_len(); // Each node stores the key and value, and the node hash // the value hash on a layered node is not stored directly in the node // The required space is set to 2. However in reality it could be 1 or 2. @@ -352,7 +356,7 @@ impl KV { let node_value_size = value_len + feature_len + HASH_LENGTH_U32 + 2; // The node will be a child of another node which stores it's key and hash // That will be added during propagation - let parent_to_child_cost = Link::encoded_link_size(not_prefixed_key_len, is_sum_node); + let parent_to_child_cost = Link::encoded_link_size(not_prefixed_key_len, node_type); node_value_size + parent_to_child_cost } @@ -362,7 +366,7 @@ impl KV { pub fn value_byte_cost_size_for_key_and_value_lengths( not_prefixed_key_len: u32, value_len: u32, - is_sum_node: bool, + node_type: NodeType, ) -> u32 { // encoding a reference encodes the key last and doesn't encode the size of the // key. so no need for a varint required space calculation for the @@ -371,7 +375,7 @@ impl KV { // however we do need the varint required space for the cost of the key in // rocks_db let parent_to_child_reference_len = - Link::encoded_link_size(not_prefixed_key_len, is_sum_node); + Link::encoded_link_size(not_prefixed_key_len, node_type); value_len + value_len.required_space() as u32 + parent_to_child_reference_len } @@ -381,14 +385,14 @@ impl KV { pub(crate) fn value_byte_cost_size_for_key_and_raw_value_lengths( not_prefixed_key_len: u32, raw_value_len: u32, - is_sum_node: bool, + node_type: NodeType, ) -> u32 { - let sum_tree_len = if is_sum_node { 9 } else { 1 }; // 1 for option, 0 or 9 for sum feature + let sum_tree_len = node_type.feature_len(); // 1 for option, 0 or 9 for sum feature let value_len = raw_value_len + HASH_LENGTH_U32_X2 + sum_tree_len; Self::value_byte_cost_size_for_key_and_value_lengths( not_prefixed_key_len, value_len, - is_sum_node, + node_type, ) } @@ -400,7 +404,7 @@ impl KV { Self::value_byte_cost_size_for_key_and_value_lengths( key_len, value_len, - self.feature_type.is_sum_feature(), + self.feature_type.node_type(), ) } @@ -415,13 +419,9 @@ impl KV { #[inline] pub(crate) fn layered_value_byte_cost_size(&self, value_cost: u32) -> u32 { let key_len = self.key.len() as u32; - let is_sum_node = self.feature_type.is_sum_feature(); + let node_type = self.feature_type.node_type(); - Self::layered_value_byte_cost_size_for_key_and_value_lengths( - key_len, - value_cost, - is_sum_node, - ) + Self::layered_value_byte_cost_size_for_key_and_value_lengths(key_len, value_cost, node_type) } /// This function is used to calculate the cost of groveDB sum item nodes @@ -431,9 +431,9 @@ impl KV { #[inline] pub(crate) fn specialized_value_byte_cost_size(&self, value_cost: u32) -> u32 { let key_len = self.key.len() as u32; - let is_sum_node = self.feature_type.is_sum_feature(); + let node_type = self.feature_type.node_type(); - Self::node_value_byte_cost_size(key_len, value_cost, is_sum_node) + Self::node_value_byte_cost_size(key_len, value_cost, node_type) } /// Costs based on predefined types (Trees, SumTrees, SumItems) that behave diff --git a/merk/src/tree/link.rs b/merk/src/tree/link.rs index 6c372d87..5e45b6fd 100644 --- a/merk/src/tree/link.rs +++ b/merk/src/tree/link.rs @@ -3,6 +3,8 @@ #[cfg(feature = "minimal")] use std::io::{Read, Write}; +#[cfg(feature = "minimal")] +use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; #[cfg(feature = "minimal")] use ed::{Decode, Encode, Result, Terminated}; #[cfg(feature = "minimal")] @@ -11,8 +13,11 @@ use integer_encoding::{VarInt, VarIntReader, VarIntWriter}; #[cfg(feature = "minimal")] use super::{hash::CryptoHash, TreeNode}; #[cfg(feature = "minimal")] +use crate::merk::NodeType; +#[cfg(feature = "minimal")] +use crate::tree::tree_feature_type::AggregateData; +#[cfg(feature = "minimal")] use crate::HASH_LENGTH_U32; - // TODO: optimize memory footprint #[cfg(feature = "minimal")] @@ -30,8 +35,8 @@ pub enum Link { child_heights: (u8, u8), /// Key key: Vec, - /// Sum - sum: Option, + /// Aggregate data like Sum + aggregate_data: AggregateData, }, /// Represents a tree node which has been modified since the `Tree`'s last @@ -57,8 +62,8 @@ pub enum Link { child_heights: (u8, u8), /// Tree tree: TreeNode, - /// Sum - sum: Option, + /// Aggregate data like Sum + aggregate_data: AggregateData, }, /// Represents a tree node which has not been modified, has an up-to-date @@ -70,8 +75,8 @@ pub enum Link { child_heights: (u8, u8), /// Tree tree: TreeNode, - /// Sum - sum: Option, + /// Aggregate data like Sum + aggregate_data: AggregateData, }, } @@ -160,12 +165,12 @@ impl Link { /// of variant `Link::Modified` since we have not yet recomputed the tree's /// hash. #[inline] - pub const fn sum(&self) -> Option { + pub const fn aggregate_data(&self) -> AggregateData { match self { Link::Modified { .. } => panic!("Cannot get hash from modified link"), - Link::Reference { sum, .. } => *sum, - Link::Uncommitted { sum, .. } => *sum, - Link::Loaded { sum, .. } => *sum, + Link::Reference { aggregate_data, .. } => *aggregate_data, + Link::Uncommitted { aggregate_data, .. } => *aggregate_data, + Link::Loaded { aggregate_data, .. } => *aggregate_data, } } @@ -213,12 +218,12 @@ impl Link { Link::Uncommitted { .. } => panic!("Cannot prune Uncommitted tree"), Link::Loaded { hash, - sum, + aggregate_data, child_heights, tree, } => Self::Reference { hash, - sum, + aggregate_data, child_heights, key: tree.take_key(), }, @@ -251,8 +256,8 @@ impl Link { // Costs for operations within a single merk #[inline] /// Encoded link size - pub const fn encoded_link_size(not_prefixed_key_len: u32, is_sum_tree: bool) -> u32 { - let sum_tree_cost = if is_sum_tree { 8 } else { 0 }; + pub const fn encoded_link_size(not_prefixed_key_len: u32, node_type: NodeType) -> u32 { + let sum_tree_cost = node_type.cost(); // Links are optional values that represent the right or left node for a given // 1 byte to represent key_length (this is a u8) // key_length to represent the actual key @@ -269,9 +274,13 @@ impl Link { debug_assert!(self.key().len() < 256, "Key length must be less than 256"); Ok(match self { - Link::Reference { key, sum, .. } => match sum { - None => key.len() + 36, // 1 + HASH_LENGTH + 2 + 1, - Some(_sum_value) => { + Link::Reference { + key, + aggregate_data, + .. + } => match aggregate_data { + AggregateData::NoAggregateData => key.len() + 36, // 1 + HASH_LENGTH + 2 + 1, + AggregateData::Count(_) | AggregateData::Sum(_) => { // 1 for key len // key_len for keys // 32 for hash @@ -282,14 +291,36 @@ impl Link { // sum_len for sum vale key.len() + 44 // 1 + 32 + 2 + 1 + 8 } + AggregateData::BigSum(_) | AggregateData::CountAndSum(..) => { + // 1 for key len + // key_len for keys + // 32 for hash + // 2 for child heights + // 1 to represent presence of sum value + // if above is 1, then + // 1 for sum len + // sum_len for sum vale + key.len() + 52 // 1 + 32 + 2 + 1 + 16 + } }, Link::Modified { .. } => panic!("No encoding for Link::Modified"), - Link::Uncommitted { tree, sum, .. } | Link::Loaded { tree, sum, .. } => match sum { - None => tree.key().len() + 36, // 1 + 32 + 2 + 1, - Some(sum_value) => { - let _encoded_sum_value = sum_value.encode_var_vec(); + Link::Uncommitted { + tree, + aggregate_data, + .. + } + | Link::Loaded { + tree, + aggregate_data, + .. + } => match aggregate_data { + AggregateData::NoAggregateData => tree.key().len() + 36, // 1 + 32 + 2 + 1, + AggregateData::Count(_) | AggregateData::Sum(_) => { tree.key().len() + 44 // 1 + 32 + 2 + 1 + 8 } + AggregateData::BigSum(_) | AggregateData::CountAndSum(..) => { + tree.key().len() + 52 // 1 + 32 + 2 + 1 + 16 + } }, }) } @@ -299,25 +330,25 @@ impl Link { impl Encode for Link { #[inline] fn encode_into(&self, out: &mut W) -> Result<()> { - let (hash, sum, key, (left_height, right_height)) = match self { + let (hash, aggregate_data, key, (left_height, right_height)) = match self { Link::Reference { hash, - sum, + aggregate_data, key, child_heights, - } => (hash, sum, key.as_slice(), child_heights), + } => (hash, aggregate_data, key.as_slice(), child_heights), Link::Loaded { hash, - sum, + aggregate_data, tree, child_heights, - } => (hash, sum, tree.key(), child_heights), + } => (hash, aggregate_data, tree.key(), child_heights), Link::Uncommitted { hash, - sum, + aggregate_data, tree, child_heights, - } => (hash, sum, tree.key(), child_heights), + } => (hash, aggregate_data, tree.key(), child_heights), Link::Modified { .. } => panic!("No encoding for Link::Modified"), }; @@ -331,13 +362,26 @@ impl Encode for Link { out.write_all(&[*left_height, *right_height])?; - match sum { - None => { + match aggregate_data { + AggregateData::NoAggregateData => { out.write_all(&[0])?; } - Some(sum_value) => { + AggregateData::Sum(sum_value) => { out.write_all(&[1])?; - out.write_varint(sum_value.to_owned())?; + out.write_varint(*sum_value)?; + } + AggregateData::BigSum(big_sum_value) => { + out.write_all(&[2])?; + out.write_i128::(*big_sum_value)?; + } + AggregateData::Count(count_value) => { + out.write_all(&[3])?; + out.write_varint(*count_value)?; + } + AggregateData::CountAndSum(count_value, sum_value) => { + out.write_all(&[4])?; + out.write_varint(*count_value)?; + out.write_varint(*sum_value)?; } } @@ -349,9 +393,13 @@ impl Encode for Link { debug_assert!(self.key().len() < 256, "Key length must be less than 256"); Ok(match self { - Link::Reference { key, sum, .. } => match sum { - None => key.len() + 36, // 1 + 32 + 2 + 1 - Some(sum_value) => { + Link::Reference { + key, + aggregate_data, + .. + } => match aggregate_data { + AggregateData::NoAggregateData => key.len() + 36, // 1 + 32 + 2 + 1 + AggregateData::Sum(sum_value) => { let encoded_sum_value = sum_value.encode_var_vec(); // 1 for key len // key_len for keys @@ -363,14 +411,63 @@ impl Encode for Link { // sum_len for sum vale key.len() + encoded_sum_value.len() + 36 // 1 + 32 + 2 + 1 } + AggregateData::BigSum(_) => { + // 1 for key len + // key_len for keys + // 32 for hash + // 2 for child heights + // 1 to represent presence of sum value + // if above is 1, then + // 1 for sum len + // sum_len for sum vale + key.len() + 52 // 1 + 32 + 2 + 1 + 16 + } + AggregateData::Count(count) => { + let encoded_count_value = count.encode_var_vec(); + // 1 for key len + // key_len for keys + // 32 for hash + // 2 for child heights + // 1 to represent presence of sum value + // if above is 1, then + // 1 for sum len + // sum_len for sum vale + key.len() + encoded_count_value.len() + 36 // 1 + 32 + 2 + 1 + } + AggregateData::CountAndSum(count, sum) => { + let encoded_sum_value = sum.encode_var_vec(); + let encoded_count_value = count.encode_var_vec(); + key.len() + encoded_sum_value.len() + encoded_count_value.len() + 36 + } }, Link::Modified { .. } => panic!("No encoding for Link::Modified"), - Link::Uncommitted { tree, sum, .. } | Link::Loaded { tree, sum, .. } => match sum { - None => tree.key().len() + 36, // 1 + 32 + 2 + 1 - Some(sum_value) => { + Link::Uncommitted { + tree, + aggregate_data, + .. + } + | Link::Loaded { + tree, + aggregate_data, + .. + } => match aggregate_data { + AggregateData::NoAggregateData => tree.key().len() + 36, // 1 + 32 + 2 + 1 + AggregateData::Sum(sum_value) => { let encoded_sum_value = sum_value.encode_var_vec(); tree.key().len() + encoded_sum_value.len() + 36 // 1 + 32 + 2 + 1 } + AggregateData::BigSum(_) => { + tree.key().len() + 52 // 1 + 32 + 2 + 1 + 16 + } + AggregateData::Count(count_value) => { + let encoded_count_value = count_value.encode_var_vec(); + tree.key().len() + encoded_count_value.len() + 36 // 1 + 32 + 2 + 1 + } + AggregateData::CountAndSum(count, sum) => { + let encoded_sum_value = sum.encode_var_vec(); + let encoded_count_value = count.encode_var_vec(); + tree.key().len() + encoded_sum_value.len() + encoded_count_value.len() + 36 + } }, }) } @@ -383,7 +480,7 @@ impl Link { Self::Reference { key: Vec::with_capacity(64), hash: Default::default(), - sum: None, + aggregate_data: AggregateData::NoAggregateData, child_heights: (0, 0), } } @@ -407,7 +504,7 @@ impl Decode for Link { } if let Link::Reference { - ref mut sum, + ref mut aggregate_data, ref mut key, ref mut hash, ref mut child_heights, @@ -423,14 +520,27 @@ impl Decode for Link { child_heights.0 = read_u8(&mut input)?; child_heights.1 = read_u8(&mut input)?; - let has_sum = read_u8(&mut input)?; - *sum = match has_sum { - 0 => None, + let aggregate_data_byte = read_u8(&mut input)?; + *aggregate_data = match aggregate_data_byte { + 0 => AggregateData::NoAggregateData, 1 => { let encoded_sum: i64 = input.read_varint()?; - Some(encoded_sum) + AggregateData::Sum(encoded_sum) } - _ => return Err(ed::Error::UnexpectedByte(55)), + 2 => { + let encoded_big_sum: i128 = input.read_i128::()?; + AggregateData::BigSum(encoded_big_sum) + } + 3 => { + let encoded_count: u64 = input.read_varint()?; + AggregateData::Count(encoded_count) + } + 4 => { + let encoded_count: u64 = input.read_varint()?; + let encoded_sum: i64 = input.read_varint()?; + AggregateData::CountAndSum(encoded_count, encoded_sum) + } + byte => return Err(ed::Error::UnexpectedByte(byte)), }; } else { unreachable!() @@ -487,7 +597,7 @@ mod test { #[test] fn types() { let hash = NULL_HASH; - let sum = None; + let aggregate_data = AggregateData::NoAggregateData; let child_heights = (0, 0); let pending_writes = 1; let key = vec![0]; @@ -495,7 +605,7 @@ mod test { let reference = Link::Reference { hash, - sum, + aggregate_data, child_heights, key, }; @@ -506,13 +616,13 @@ mod test { }; let uncommitted = Link::Uncommitted { hash, - sum, + aggregate_data, child_heights, tree: tree(), }; let loaded = Link::Loaded { hash, - sum, + aggregate_data, child_heights, tree: tree(), }; @@ -578,7 +688,7 @@ mod test { fn uncommitted_into_reference() { Link::Uncommitted { hash: [1; 32], - sum: None, + aggregate_data: AggregateData::NoAggregateData, child_heights: (1, 1), tree: TreeNode::new(vec![0], vec![1], None, BasicMerkNode).unwrap(), } @@ -589,7 +699,7 @@ mod test { fn encode_link() { let link = Link::Reference { key: vec![1, 2, 3], - sum: None, + aggregate_data: AggregateData::NoAggregateData, child_heights: (123, 124), hash: [55; 32], }; @@ -610,7 +720,7 @@ mod test { fn encode_link_with_sum() { let link = Link::Reference { key: vec![1, 2, 3], - sum: Some(50), + aggregate_data: AggregateData::Sum(50), child_heights: (123, 124), hash: [55; 32], }; @@ -629,12 +739,59 @@ mod test { ); } + #[test] + fn encode_link_with_count() { + let link = Link::Reference { + key: vec![1, 2, 3], + aggregate_data: AggregateData::Count(50), + child_heights: (123, 124), + hash: [55; 32], + }; + assert_eq!(link.encoding_length().unwrap(), 40); + + let mut bytes = vec![]; + link.encode_into(&mut bytes).unwrap(); + + assert_eq!(link.encoding_length().unwrap(), bytes.len()); + assert_eq!( + bytes, + vec![ + 3, 1, 2, 3, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, + 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 123, 124, 3, 50, + ] + ); + } + + #[test] + fn encode_link_with_big_sum() { + let link = Link::Reference { + key: vec![1, 2, 3], + aggregate_data: AggregateData::BigSum(50), + child_heights: (123, 124), + hash: [55; 32], + }; + assert_eq!(link.encoding_length().unwrap(), 55); + + let mut bytes = vec![]; + link.encode_into(&mut bytes).unwrap(); + + assert_eq!(link.encoding_length().unwrap(), bytes.len()); + assert_eq!( + bytes, + vec![ + 3, 1, 2, 3, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, + 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 123, 124, 2, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50 + ] + ); + } + #[test] #[should_panic] fn encode_link_long_key() { let link = Link::Reference { key: vec![123; 300], - sum: None, + aggregate_data: AggregateData::NoAggregateData, child_heights: (123, 124), hash: [55; 32], }; @@ -649,6 +806,6 @@ mod test { 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 123, 124, 0, ]; let link = Link::decode(bytes.as_slice()).expect("expected to decode a link"); - assert_eq!(link.sum(), None); + assert_eq!(link.aggregate_data(), AggregateData::NoAggregateData); } } diff --git a/merk/src/tree/mod.rs b/merk/src/tree/mod.rs index e87865a2..460edbce 100644 --- a/merk/src/tree/mod.rs +++ b/merk/src/tree/mod.rs @@ -59,11 +59,15 @@ use kv::KV; pub use link::Link; #[cfg(feature = "minimal")] pub use ops::{AuxMerkBatch, BatchEntry, MerkBatch, Op, PanicSource}; +#[cfg(feature = "minimal")] +pub use tree_feature_type::AggregateData; #[cfg(any(feature = "minimal", feature = "verify"))] pub use tree_feature_type::TreeFeatureType; #[cfg(feature = "minimal")] pub use walk::{Fetch, RefWalker, Walker}; +#[cfg(feature = "minimal")] +use crate::merk::NodeType; #[cfg(feature = "minimal")] use crate::tree::hash::HASH_LENGTH_X2; #[cfg(feature = "minimal")] @@ -91,6 +95,11 @@ impl TreeNodeInner { self.kv.value } + /// Get the value as owned of the key value struct + pub fn value_as_owned_with_feature(self) -> (Vec, TreeFeatureType) { + (self.kv.value, self.kv.feature_type) + } + /// Get the value as slice of the key value struct pub fn value_as_slice(&self) -> &[u8] { self.kv.value.as_slice() @@ -155,9 +164,9 @@ impl TreeNode { } } - /// Is sum node? - pub fn is_sum_node(&self) -> bool { - self.inner.kv.feature_type.is_sum_feature() + /// the node type + pub fn node_type(&self) -> NodeType { + self.inner.kv.feature_type.node_type() } pub fn storage_cost_for_update(current_value_byte_cost: u32, old_cost: u32) -> StorageCost { @@ -250,7 +259,7 @@ impl TreeNode { KV::value_byte_cost_size_for_key_and_value_lengths( key_len, value_len as u32, - self.inner.kv.feature_type.is_sum_feature(), + self.inner.kv.feature_type.node_type(), ) } else { self.inner.kv.value_byte_cost_size() @@ -447,9 +456,15 @@ impl TreeNode { ( // 36 = 32 Hash + 1 key length + 2 child heights + 1 feature type link.key().len() as u32 + 36, - link.sum() - .map(|s| s.encode_var_vec().len() as u32) - .unwrap_or_default(), + match link.aggregate_data() { + AggregateData::NoAggregateData => 0, + AggregateData::Sum(s) => s.encode_var_vec().len() as u32, + AggregateData::BigSum(_) => 16 as u32, + AggregateData::Count(c) => c.encode_var_vec().len() as u32, + AggregateData::CountAndSum(c, s) => { + s.encode_var_vec().len() as u32 + c.encode_var_vec().len() as u32 + } + }, ) }) } @@ -490,9 +505,49 @@ impl TreeNode { /// Returns the sum of the root node's child on the given side, if any. If /// there is no child, returns 0. #[inline] - pub fn child_sum(&self, left: bool) -> i64 { + pub fn child_aggregate_sum_data_as_i64(&self, left: bool) -> Result { + match self.link(left) { + Some(link) => match link.aggregate_data() { + AggregateData::NoAggregateData => Ok(0), + AggregateData::Sum(s) => Ok(s), + AggregateData::BigSum(_) => Err(Error::BigSumTreeUnderNormalSumTree( + "for aggregate data as i64".to_string(), + )), + AggregateData::Count(_) => Ok(0), + AggregateData::CountAndSum(_, s) => Ok(s), + }, + _ => Ok(0), + } + } + + /// Returns the sum of the root node's child on the given side, if any. If + /// there is no child, returns 0. + #[inline] + pub fn child_aggregate_count_data_as_u64(&self, left: bool) -> Result { match self.link(left) { - Some(link) => link.sum().unwrap_or_default(), + Some(link) => match link.aggregate_data() { + AggregateData::NoAggregateData => Ok(0), + AggregateData::Sum(_) => Ok(0), + AggregateData::BigSum(_) => Ok(0), + AggregateData::Count(c) => Ok(c), + AggregateData::CountAndSum(c, _) => Ok(c), + }, + _ => Ok(0), + } + } + + /// Returns the sum of the root node's child on the given side, if any. If + /// there is no child, returns 0. + #[inline] + pub fn child_aggregate_sum_data_as_i128(&self, left: bool) -> i128 { + match self.link(left) { + Some(link) => match link.aggregate_data() { + AggregateData::NoAggregateData => 0, + AggregateData::Sum(s) => s as i128, + AggregateData::BigSum(s) => s, + AggregateData::Count(_) => 0, + AggregateData::CountAndSum(_, s) => s as i128, + }, _ => 0, } } @@ -510,14 +565,52 @@ impl TreeNode { /// Computes and returns the hash of the root node. #[inline] - pub fn sum(&self) -> Result, Error> { + pub fn aggregate_data(&self) -> Result { match self.inner.kv.feature_type { - TreeFeatureType::BasicMerkNode => Ok(None), - TreeFeatureType::SummedMerkNode(value) => value - .checked_add(self.child_sum(true)) - .and_then(|a| a.checked_add(self.child_sum(false))) - .ok_or(Overflow("sum is overflowing")) - .map(Some), + TreeFeatureType::BasicMerkNode => Ok(AggregateData::NoAggregateData), + TreeFeatureType::SummedMerkNode(value) => { + let left = self.child_aggregate_sum_data_as_i64(true)?; + let right = self.child_aggregate_sum_data_as_i64(false)?; + value + .checked_add(left) + .and_then(|a| a.checked_add(right)) + .ok_or(Overflow("sum is overflowing")) + .map(AggregateData::Sum) + } + TreeFeatureType::BigSummedMerkNode(value) => value + .checked_add(self.child_aggregate_sum_data_as_i128(true)) + .and_then(|a| a.checked_add(self.child_aggregate_sum_data_as_i128(false))) + .ok_or(Overflow("big sum is overflowing")) + .map(AggregateData::BigSum), + TreeFeatureType::CountedMerkNode(value) => { + let left = self.child_aggregate_count_data_as_u64(true)?; + let right = self.child_aggregate_count_data_as_u64(false)?; + value + .checked_add(left) + .and_then(|a| a.checked_add(right)) + .ok_or(Overflow("count is overflowing")) + .map(AggregateData::Count) + } + TreeFeatureType::CountedSummedMerkNode(count_value, sum_value) => { + let left_count = self.child_aggregate_count_data_as_u64(true)?; + let right_count = self.child_aggregate_count_data_as_u64(false)?; + let left_sum = self.child_aggregate_sum_data_as_i64(true)?; + let right_sum = self.child_aggregate_sum_data_as_i64(false)?; + let aggregated_count_value = count_value + .checked_add(left_count) + .and_then(|a| a.checked_add(right_count)) + .ok_or(Overflow("count is overflowing"))?; + + let aggregated_sum_value = sum_value + .checked_add(left_sum) + .and_then(|a| a.checked_add(right_sum)) + .ok_or(Overflow("count is overflowing"))?; + + Ok(AggregateData::CountAndSum( + aggregated_count_value, + aggregated_sum_value, + )) + } } } @@ -936,13 +1029,13 @@ impl TreeNode { { // println!("key is {}", std::str::from_utf8(tree.key()).unwrap()); cost_return_on_error!(&mut cost, tree.commit(c, old_specialized_cost,)); - let sum = cost_return_on_error_default!(tree.sum()); + let aggregate_data = cost_return_on_error_default!(tree.aggregate_data()); self.inner.left = Some(Link::Loaded { hash: tree.hash().unwrap_add_cost(&mut cost), tree, child_heights, - sum, + aggregate_data, }); } else { unreachable!() @@ -959,12 +1052,12 @@ impl TreeNode { { // println!("key is {}", std::str::from_utf8(tree.key()).unwrap()); cost_return_on_error!(&mut cost, tree.commit(c, old_specialized_cost,)); - let sum = cost_return_on_error_default!(tree.sum()); + let aggregate_data = cost_return_on_error_default!(tree.aggregate_data()); self.inner.right = Some(Link::Loaded { hash: tree.hash().unwrap_add_cost(&mut cost), tree, child_heights, - sum, + aggregate_data, }); } else { unreachable!() @@ -1001,13 +1094,13 @@ impl TreeNode { { // TODO: return Err instead of panic? let link = self.link(left).expect("Expected link"); - let (child_heights, hash, sum) = match link { + let (child_heights, hash, aggregate_data) = match link { Link::Reference { child_heights, hash, - sum, + aggregate_data, .. - } => (child_heights, hash, sum), + } => (child_heights, hash, aggregate_data), _ => panic!("Expected Some(Link::Reference)"), }; @@ -1021,7 +1114,7 @@ impl TreeNode { tree, hash: *hash, child_heights: *child_heights, - sum: *sum, + aggregate_data: *aggregate_data, }); Ok(()).wrap_with_cost(cost) } @@ -1041,7 +1134,7 @@ pub const fn side_to_str(left: bool) -> &'static str { #[cfg(test)] mod test { - use super::{commit::NoopCommit, hash::NULL_HASH, TreeNode}; + use super::{commit::NoopCommit, hash::NULL_HASH, AggregateData, TreeNode}; use crate::tree::{ tree_feature_type::TreeFeatureType::SummedMerkNode, TreeFeatureType::BasicMerkNode, }; @@ -1250,6 +1343,10 @@ mod test { .unwrap() .expect("commit failed"); - assert_eq!(Some(8), tree.sum().expect("expected to get sum from tree")); + assert_eq!( + AggregateData::Sum(8), + tree.aggregate_data() + .expect("expected to get sum from tree") + ); } } diff --git a/merk/src/tree/ops.rs b/merk/src/tree/ops.rs index 66fcb716..2e2cf3fd 100644 --- a/merk/src/tree/ops.rs +++ b/merk/src/tree/ops.rs @@ -1100,7 +1100,7 @@ mod test { None, Some(Link::Loaded { hash: [123; 32], - sum: None, + aggregate_data: AggregateData::NoAggregateData, child_heights: (0, 0), tree: TreeNode::new(b"foo2".to_vec(), b"bar2".to_vec(), None, BasicMerkNode) .unwrap(), diff --git a/merk/src/tree/tree_feature_type.rs b/merk/src/tree/tree_feature_type.rs index bb815dca..579e5032 100644 --- a/merk/src/tree/tree_feature_type.rs +++ b/merk/src/tree/tree_feature_type.rs @@ -3,15 +3,25 @@ #[cfg(any(feature = "minimal", feature = "verify"))] use std::io::{Read, Write}; +#[cfg(any(feature = "minimal", feature = "verify"))] +use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; #[cfg(feature = "minimal")] use ed::Terminated; #[cfg(any(feature = "minimal", feature = "verify"))] use ed::{Decode, Encode}; #[cfg(any(feature = "minimal", feature = "verify"))] +use grovedb_costs::TreeCostType; +#[cfg(any(feature = "minimal", feature = "verify"))] use integer_encoding::{VarInt, VarIntReader, VarIntWriter}; +#[cfg(feature = "minimal")] +use crate::merk::NodeType; #[cfg(any(feature = "minimal", feature = "verify"))] -use crate::tree::tree_feature_type::TreeFeatureType::{BasicMerkNode, SummedMerkNode}; +use crate::tree::tree_feature_type::TreeFeatureType::{ + BasicMerkNode, BigSummedMerkNode, CountedMerkNode, CountedSummedMerkNode, SummedMerkNode, +}; +#[cfg(feature = "minimal")] +use crate::tree_type::TreeType; #[cfg(any(feature = "minimal", feature = "verify"))] #[derive(Copy, Clone, PartialEq, Eq, Debug)] @@ -21,23 +31,121 @@ pub enum TreeFeatureType { BasicMerkNode, /// Summed Merk Tree Node SummedMerkNode(i64), + /// Big Summed Merk Tree Node + BigSummedMerkNode(i128), + /// Counted Merk Tree None + CountedMerkNode(u64), + /// Counted and summed Merk Tree None + CountedSummedMerkNode(u64, i64), } #[cfg(feature = "minimal")] impl TreeFeatureType { - #[inline] - /// Get length of encoded SummedMerk - pub fn sum_length(&self) -> Option { + pub fn node_type(&self) -> NodeType { match self { - BasicMerkNode => None, - SummedMerkNode(m) => Some(m.encode_var_vec().len() as u32), + BasicMerkNode => NodeType::NormalNode, + SummedMerkNode(_) => NodeType::SumNode, + BigSummedMerkNode(_) => NodeType::BigSumNode, + CountedMerkNode(_) => NodeType::CountNode, + CountedSummedMerkNode(..) => NodeType::CountSumNode, + } + } +} + +#[cfg(feature = "minimal")] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum AggregateData { + NoAggregateData, + Sum(i64), + BigSum(i128), + Count(u64), + CountAndSum(u64, i64), +} + +#[cfg(feature = "minimal")] +impl AggregateData { + pub fn parent_tree_type(&self) -> TreeType { + match self { + AggregateData::NoAggregateData => TreeType::NormalTree, + AggregateData::Sum(_) => TreeType::SumTree, + AggregateData::BigSum(_) => TreeType::BigSumTree, + AggregateData::Count(_) => TreeType::CountTree, + AggregateData::CountAndSum(..) => TreeType::CountSumTree, + } + } + + pub fn as_sum_i64(&self) -> i64 { + match self { + AggregateData::NoAggregateData => 0, + AggregateData::Sum(s) => *s, + AggregateData::BigSum(i) => { + let max = i64::MAX as i128; + if *i > max { + i64::MAX + } else { + *i as i64 + } + } + AggregateData::Count(_) => 0, + AggregateData::CountAndSum(_, s) => *s, + } + } + + pub fn as_count_u64(&self) -> u64 { + match self { + AggregateData::NoAggregateData => 0, + AggregateData::Sum(_) => 0, + AggregateData::BigSum(_) => 0, + AggregateData::Count(c) => *c, + AggregateData::CountAndSum(c, _) => *c, + } + } + + pub fn as_summed_i128(&self) -> i128 { + match self { + AggregateData::NoAggregateData => 0, + AggregateData::Sum(s) => *s as i128, + AggregateData::BigSum(i) => *i, + AggregateData::Count(_) => 0, + AggregateData::CountAndSum(_, s) => *s as i128, } } +} + +#[cfg(feature = "minimal")] +impl From for AggregateData { + fn from(value: TreeFeatureType) -> Self { + match value { + BasicMerkNode => AggregateData::NoAggregateData, + SummedMerkNode(val) => AggregateData::Sum(val), + BigSummedMerkNode(val) => AggregateData::BigSum(val), + CountedMerkNode(val) => AggregateData::Count(val), + CountedSummedMerkNode(count, sum) => AggregateData::CountAndSum(count, sum), + } + } +} +#[cfg(feature = "minimal")] +impl TreeFeatureType { #[inline] - /// Is sum feature? - pub fn is_sum_feature(&self) -> bool { - matches!(self, SummedMerkNode(_)) + /// Get length of encoded SummedMerk + pub fn tree_feature_specialized_type_and_length(&self) -> Option<(TreeCostType, u32)> { + match self { + BasicMerkNode => None, + SummedMerkNode(m) => Some(( + TreeCostType::TreeFeatureUsesVarIntCostAs8Bytes, + m.encode_var_vec().len() as u32, + )), + BigSummedMerkNode(_) => Some((TreeCostType::TreeFeatureUses16Bytes, 16)), + CountedMerkNode(m) => Some(( + TreeCostType::TreeFeatureUsesVarIntCostAs8Bytes, + m.encode_var_vec().len() as u32, + )), + CountedSummedMerkNode(count, sum) => Some(( + TreeCostType::TreeFeatureUsesTwoVarIntsCostAs16Bytes, + count.encode_var_vec().len() as u32 + sum.encode_var_vec().len() as u32, + )), + } } #[inline] @@ -46,6 +154,9 @@ impl TreeFeatureType { match self { BasicMerkNode => 1, SummedMerkNode(_sum) => 9, + BigSummedMerkNode(_) => 17, + CountedMerkNode(_) => 9, + CountedSummedMerkNode(..) => 17, } } } @@ -53,6 +164,7 @@ impl TreeFeatureType { #[cfg(feature = "minimal")] impl Terminated for TreeFeatureType {} +#[cfg(any(feature = "minimal", feature = "verify"))] impl Encode for TreeFeatureType { #[inline] fn encode_into(&self, dest: &mut W) -> ed::Result<()> { @@ -63,7 +175,23 @@ impl Encode for TreeFeatureType { } SummedMerkNode(sum) => { dest.write_all(&[1])?; - dest.write_varint(sum.to_owned())?; + dest.write_varint(*sum)?; + Ok(()) + } + BigSummedMerkNode(sum) => { + dest.write_all(&[2])?; + dest.write_i128::(*sum)?; + Ok(()) + } + CountedMerkNode(count) => { + dest.write_all(&[3])?; + dest.write_varint(*count)?; + Ok(()) + } + CountedSummedMerkNode(count, sum) => { + dest.write_all(&[4])?; + dest.write_varint(*count)?; + dest.write_varint(*sum)?; Ok(()) } } @@ -79,6 +207,18 @@ impl Encode for TreeFeatureType { // encoded_sum.len() for the length of the encoded vector Ok(1 + encoded_sum.len()) } + BigSummedMerkNode(_) => Ok(17), + CountedMerkNode(count) => { + let encoded_sum = count.encode_var_vec(); + // 1 for the enum type + // encoded_sum.len() for the length of the encoded vector + Ok(1 + encoded_sum.len()) + } + CountedSummedMerkNode(count, sum) => { + let encoded_lengths = count.encode_var_vec().len() + sum.encode_var_vec().len(); + // 1 for the enum type + Ok(1 + encoded_lengths) + } } } } @@ -95,6 +235,19 @@ impl Decode for TreeFeatureType { let encoded_sum: i64 = input.read_varint()?; Ok(SummedMerkNode(encoded_sum)) } + [2] => { + let encoded_sum: i128 = input.read_i128::()?; + Ok(BigSummedMerkNode(encoded_sum)) + } + [3] => { + let encoded_count: u64 = input.read_varint()?; + Ok(CountedMerkNode(encoded_count)) + } + [4] => { + let encoded_count: u64 = input.read_varint()?; + let encoded_sum: i64 = input.read_varint()?; + Ok(CountedSummedMerkNode(encoded_count, encoded_sum)) + } _ => Err(ed::Error::UnexpectedByte(55)), } } diff --git a/merk/src/tree/walk/mod.rs b/merk/src/tree/walk/mod.rs index 3a1998c9..834643a1 100644 --- a/merk/src/tree/walk/mod.rs +++ b/merk/src/tree/walk/mod.rs @@ -403,7 +403,7 @@ mod test { use grovedb_version::version::GroveVersion; use super::{super::NoopCommit, *}; - use crate::tree::{TreeFeatureType::BasicMerkNode, TreeNode}; + use crate::tree::{AggregateData, TreeFeatureType::BasicMerkNode, TreeNode}; #[derive(Clone)] struct MockSource {} @@ -491,7 +491,7 @@ mod test { hash: Default::default(), key: b"foo".to_vec(), child_heights: (0, 0), - sum: None, + aggregate_data: AggregateData::NoAggregateData, }), None, BasicMerkNode, diff --git a/merk/src/tree_type.rs b/merk/src/tree_type.rs new file mode 100644 index 00000000..6432f232 --- /dev/null +++ b/merk/src/tree_type.rs @@ -0,0 +1,81 @@ +use std::fmt; + +#[cfg(feature = "minimal")] +use crate::merk::NodeType; +use crate::{Error, TreeFeatureType}; + +#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] +pub enum MaybeTree { + Tree(TreeType), + NotTree, +} + +#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] +pub enum TreeType { + NormalTree = 0, + SumTree = 1, + BigSumTree = 2, + CountTree = 3, + CountSumTree = 4, +} + +impl TryFrom for TreeType { + type Error = Error; + + fn try_from(value: u8) -> Result { + match value { + 0 => Ok(TreeType::NormalTree), + 1 => Ok(TreeType::SumTree), + 2 => Ok(TreeType::BigSumTree), + 3 => Ok(TreeType::CountTree), + 4 => Ok(TreeType::CountSumTree), + n => Err(Error::UnknownTreeType(format!("got {}, max is 4", n))), // Error handling + } + } +} + +impl fmt::Display for TreeType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let s = match *self { + TreeType::NormalTree => "Normal Tree", + TreeType::SumTree => "Sum Tree", + TreeType::BigSumTree => "Big Sum Tree", + TreeType::CountTree => "Count Tree", + TreeType::CountSumTree => "Count Sum Tree", + }; + write!(f, "{}", s) + } +} + +impl TreeType { + pub fn allows_sum_item(&self) -> bool { + match self { + TreeType::NormalTree => false, + TreeType::SumTree => true, + TreeType::BigSumTree => true, + TreeType::CountTree => false, + TreeType::CountSumTree => true, + } + } + + #[cfg(feature = "minimal")] + pub const fn inner_node_type(&self) -> NodeType { + match self { + TreeType::NormalTree => NodeType::NormalNode, + TreeType::SumTree => NodeType::SumNode, + TreeType::BigSumTree => NodeType::BigSumNode, + TreeType::CountTree => NodeType::CountNode, + TreeType::CountSumTree => NodeType::CountSumNode, + } + } + + pub fn empty_tree_feature_type(&self) -> TreeFeatureType { + match self { + TreeType::NormalTree => TreeFeatureType::BasicMerkNode, + TreeType::SumTree => TreeFeatureType::SummedMerkNode(0), + TreeType::BigSumTree => TreeFeatureType::BigSummedMerkNode(0), + TreeType::CountTree => TreeFeatureType::CountedMerkNode(0), + TreeType::CountSumTree => TreeFeatureType::CountedSummedMerkNode(0, 0), + } + } +} diff --git a/node-grove/Cargo.toml b/node-grove/Cargo.toml index bd91146b..3654bd5b 100644 --- a/node-grove/Cargo.toml +++ b/node-grove/Cargo.toml @@ -10,8 +10,8 @@ exclude = ["index.node"] crate-type = ["cdylib"] [dependencies] -grovedb = { version = "2.2.1", path = "../grovedb", features = ["full", "estimated_costs"] } -grovedb-version = { version = "2.2.1", path = "../grovedb-version" } +grovedb = { version = "3.0.0", path = "../grovedb", features = ["full", "estimated_costs"] } +grovedb-version = { version = "3.0.0", path = "../grovedb-version" } [dependencies.neon] version = "0.10.1" diff --git a/node-grove/src/converter.rs b/node-grove/src/converter.rs index a822faad..7ba3850c 100644 --- a/node-grove/src/converter.rs +++ b/node-grove/src/converter.rs @@ -38,6 +38,9 @@ fn element_to_string(element: Element) -> String { Element::Reference(..) => "reference".to_string(), Element::Tree(..) => "tree".to_string(), Element::SumTree(..) => "sum_tree".to_string(), + Element::BigSumTree(..) => "big_sum_tree".to_string(), + Element::CountTree(..) => "count_tree".to_string(), + Element::CountSumTree(..) => "count_sum_tree".to_string(), } } @@ -92,6 +95,9 @@ pub fn element_to_js_object<'a, C: Context<'a>>( Element::Reference(..) => nested_vecs_to_js(vec![], cx)?, Element::Tree(..) => nested_vecs_to_js(vec![], cx)?, Element::SumTree(..) => nested_vecs_to_js(vec![], cx)?, + Element::BigSumTree(..) => nested_vecs_to_js(vec![], cx)?, + Element::CountTree(..) => nested_vecs_to_js(vec![], cx)?, + Element::CountSumTree(..) => nested_vecs_to_js(vec![], cx)?, }; js_object.set(cx, "value", js_value)?; diff --git a/path/Cargo.toml b/path/Cargo.toml index f5f89be7..91738bfe 100644 --- a/path/Cargo.toml +++ b/path/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "grovedb-path" -version = "2.2.1" +version = "3.0.0" edition = "2021" license = "MIT" description = "Path extension crate for GroveDB" @@ -9,3 +9,4 @@ documentation = "https://docs.rs/grovedb-path" repository = "https://github.com/dashpay/grovedb" [dependencies] +hex = "0.4.3" diff --git a/path/src/subtree_path.rs b/path/src/subtree_path.rs index 437f911a..ae8cd900 100644 --- a/path/src/subtree_path.rs +++ b/path/src/subtree_path.rs @@ -34,7 +34,10 @@ //! combined with it's various `From` implementations it can cover slices, owned //! subtree paths and other path references if use as generic [Into]. -use std::hash::{Hash, Hasher}; +use std::{ + fmt::{Display, Formatter}, + hash::{Hash, Hasher}, +}; use crate::{ subtree_path_builder::{SubtreePathBuilder, SubtreePathRelative}, @@ -48,6 +51,51 @@ pub struct SubtreePath<'b, B> { pub(crate) ref_variant: SubtreePathInner<'b, B>, } +fn hex_to_ascii(hex_value: &[u8]) -> String { + // Define the set of allowed characters + const ALLOWED_CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\ + abcdefghijklmnopqrstuvwxyz\ + 0123456789_-/\\[]@"; + + // Check if all characters in hex_value are allowed + if hex_value.iter().all(|&c| ALLOWED_CHARS.contains(&c)) { + // Try to convert to UTF-8 + String::from_utf8(hex_value.to_vec()) + .unwrap_or_else(|_| format!("0x{}", hex::encode(hex_value))) + } else { + // Hex encode and prepend "0x" + format!("0x{}", hex::encode(hex_value)) + } +} + +impl<'b, B: AsRef<[u8]>> Display for SubtreePath<'b, B> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match &self.ref_variant { + SubtreePathInner::Slice(slice) => { + let ascii_path = slice + .iter() + .map(|e| hex_to_ascii(e.as_ref())) + .collect::>() + .join("/"); + write!(f, "{}", ascii_path) + } + SubtreePathInner::SubtreePath(subtree_path) => { + let ascii_path = subtree_path + .to_vec() + .into_iter() + .map(|a| hex_to_ascii(a.as_slice())) + .collect::>() + .join("/"); + write!(f, "{}", ascii_path) + } + SubtreePathInner::SubtreePathIter(iter) => { + let ascii_path = iter.clone().map(hex_to_ascii).collect::>().join("/"); + write!(f, "{}", ascii_path) + } + } + } +} + /// Wrapped inner representation of subtree path ref. #[derive(Debug)] pub(crate) enum SubtreePathInner<'b, B> { diff --git a/path/src/util/compact_bytes.rs b/path/src/util/compact_bytes.rs index 1e4362cb..c44b6dd9 100644 --- a/path/src/util/compact_bytes.rs +++ b/path/src/util/compact_bytes.rs @@ -66,7 +66,7 @@ impl CompactBytes { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub(crate) struct CompactBytesIter<'a> { bytes: &'a CompactBytes, offset_back: usize, diff --git a/storage/Cargo.toml b/storage/Cargo.toml index e2db3bb6..409ddaed 100644 --- a/storage/Cargo.toml +++ b/storage/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "grovedb-storage" -version = "2.2.1" +version = "3.0.0" edition = "2021" license = "MIT" description = "Storage extension crate for GroveDB" @@ -9,9 +9,9 @@ documentation = "https://docs.rs/grovedb-storage" repository = "https://github.com/dashpay/grovedb" [dependencies] -grovedb-costs = { version = "2.2.1", path = "../costs" } -grovedb-path = { version = "2.2.1", path = "../path" } -grovedb-visualize = { version = "2.2.1", path = "../visualize" } +grovedb-costs = { version = "3.0.0", path = "../costs" } +grovedb-path = { version = "3.0.0", path = "../path" } +grovedb-visualize = { version = "3.0.0", path = "../visualize" } blake3 = { version = "1.5.1", optional = true } hex = "0.4.3" diff --git a/visualize/Cargo.toml b/visualize/Cargo.toml index 233341a2..60b09efd 100644 --- a/visualize/Cargo.toml +++ b/visualize/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "grovedb-visualize" -version = "2.2.1" +version = "3.0.0" edition = "2021" license = "MIT" description = "Debug prints extension crate for GroveDB"