Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Jan 9, 2025
1 parent 3f6705f commit 1ba42f5
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 77 deletions.
35 changes: 28 additions & 7 deletions costs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FeatureSumLength>,
Option<(TreeCostType, FeatureSumLength)>,
Option<(ChildKeyLength, ChildSumLength)>,
Option<(ChildKeyLength, ChildSumLength)>,
)>;
Expand Down Expand Up @@ -199,20 +219,21 @@ 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
paid_value_len += paid_value_len.required_space() as u32;

// 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
Expand Down
4 changes: 2 additions & 2 deletions grovedb/src/batch/estimated_costs/average_case_costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use grovedb_costs::{
cost_return_on_error, cost_return_on_error_no_add, CostResult, CostsExt, OperationCost,
};
#[cfg(feature = "full")]
use grovedb_merk::{
estimated_costs::average_case_costs::{average_case_merk_propagate, EstimatedLayerInformation},
use grovedb_merk::estimated_costs::average_case_costs::{
average_case_merk_propagate, EstimatedLayerInformation,
};
use grovedb_merk::{merk::TreeType, tree::AggregateData, RootHashKeyAndAggregateData};
#[cfg(feature = "full")]
Expand Down
19 changes: 12 additions & 7 deletions grovedb/src/batch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ use crate::batch::estimated_costs::EstimatedCostsType;
use crate::{
batch::{batch_structure::BatchStructure, mode::BatchRunMode},
element::{
MaxReferenceHop, BIG_SUM_TREE_COST_SIZE, COUNT_TREE_COST_SIZE, COUNT_SUM_TREE_COST_SIZE, SUM_ITEM_COST_SIZE,
SUM_TREE_COST_SIZE, TREE_COST_SIZE,
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::{
Expand Down Expand Up @@ -1022,7 +1022,8 @@ where
Element::Tree(..)
| Element::SumTree(..)
| Element::BigSumTree(..)
| Element::CountTree(..) | Element::CountSumTree(..) => Err(Error::InvalidBatchOperation(
| Element::CountTree(..)
| Element::CountSumTree(..) => Err(Error::InvalidBatchOperation(
"references can not point to trees being updated",
))
.wrap_with_cost(cost),
Expand Down Expand Up @@ -1143,7 +1144,8 @@ where
Element::Tree(..)
| Element::SumTree(..)
| Element::BigSumTree(..)
| Element::CountTree(..) | Element::CountSumTree(..) => Err(Error::InvalidBatchOperation(
| Element::CountTree(..)
| Element::CountSumTree(..) => Err(Error::InvalidBatchOperation(
"references can not point to trees being updated",
))
.wrap_with_cost(cost),
Expand Down Expand Up @@ -1173,7 +1175,8 @@ where
Element::Tree(..)
| Element::SumTree(..)
| Element::BigSumTree(..)
| Element::CountTree(..) | Element::CountSumTree(..) => Err(Error::InvalidBatchOperation(
| Element::CountTree(..)
| Element::CountSumTree(..) => Err(Error::InvalidBatchOperation(
"references can not point to trees being updated",
))
.wrap_with_cost(cost),
Expand Down Expand Up @@ -1348,7 +1351,8 @@ where
Element::Tree(..)
| Element::SumTree(..)
| Element::BigSumTree(..)
| Element::CountTree(..) | Element::CountSumTree(..) => {
| Element::CountTree(..)
| Element::CountSumTree(..) => {
let merk_feature_type = cost_return_on_error!(
&mut cost,
element
Expand Down Expand Up @@ -1658,7 +1662,8 @@ where
Element::Tree(..)
| Element::SumTree(..)
| Element::BigSumTree(..)
| Element::CountTree(..) | Element::CountSumTree(..) => {
| Element::CountTree(..)
| Element::CountSumTree(..) => {
let tree_type = new_element.tree_type().unwrap();
let tree_cost_size = match tree_type {
TreeType::NormalTree => TREE_COST_SIZE,
Expand Down
2 changes: 1 addition & 1 deletion grovedb/src/element/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl Element {
| Some(Element::SumTree(_, _, flags))
| Some(Element::BigSumTree(_, _, flags))
| Some(Element::CountTree(_, _, flags))
| Some(Element::CountSumTree(.. , 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;
Expand Down
11 changes: 7 additions & 4 deletions grovedb/src/element/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use grovedb_merk::tree::kv::{
};
use grovedb_merk::{
merk::{NodeType, TreeType},
TreeFeatureType::{BigSummedMerkNode, CountedMerkNode},
TreeFeatureType::{BigSummedMerkNode, CountedMerkNode, CountedSummedMerkNode},
};
#[cfg(feature = "full")]
use grovedb_merk::{
Expand All @@ -20,7 +20,7 @@ use grovedb_merk::{
use grovedb_version::{check_grovedb_v0, error::GroveVersionError, version::GroveVersion};
#[cfg(feature = "full")]
use integer_encoding::VarInt;
use grovedb_merk::TreeFeatureType::CountedSummedMerkNode;

use crate::element::{BIG_SUM_TREE_COST_SIZE, COUNT_SUM_TREE_COST_SIZE, COUNT_TREE_COST_SIZE};
#[cfg(feature = "full")]
use crate::reference_path::path_from_reference_path_type;
Expand Down Expand Up @@ -245,7 +245,10 @@ impl Element {
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 => Ok(CountedSummedMerkNode(self.count_value_or_default(), self.sum_value_or_default())),
TreeType::CountSumTree => Ok(CountedSummedMerkNode(
self.count_value_or_default(),
self.sum_value_or_default(),
)),
}
}

Expand Down Expand Up @@ -416,7 +419,7 @@ impl Element {
key_len, value_len, node_type,
)
}
Element::CountSumTree(.. , flags) => {
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
Expand Down
2 changes: 1 addition & 1 deletion grovedb/src/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ mod serialize;
use std::fmt;

use bincode::{Decode, Encode};
use grovedb_merk::estimated_costs::SUM_AND_COUNT_LAYER_COST_SIZE;
#[cfg(any(feature = "full", feature = "verify"))]
use grovedb_merk::estimated_costs::SUM_VALUE_EXTRA_COST;
#[cfg(feature = "full")]
use grovedb_merk::estimated_costs::{
BIG_SUM_LAYER_COST_SIZE, LAYER_COST_SIZE, SUM_LAYER_COST_SIZE,
};
use grovedb_merk::estimated_costs::SUM_AND_COUNT_LAYER_COST_SIZE;
#[cfg(feature = "full")]
use grovedb_merk::merk::TreeType;
#[cfg(feature = "full")]
Expand Down
3 changes: 2 additions & 1 deletion grovedb/src/operations/get/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,8 @@ impl GroveDb {
Ok(Element::Tree(..))
| Ok(Element::SumTree(..))
| Ok(Element::BigSumTree(..))
| Ok(Element::CountTree(..)) | Ok(Element::CountSumTree(..)) => Ok(()).wrap_with_cost(cost),
| 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),
}
Expand Down
11 changes: 7 additions & 4 deletions grovedb/src/operations/get/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,10 @@ where {
Ok(QueryItemOrSumReturnType::CountValue(count_value))
}
Element::CountSumTree(_, count_value, sum_value, _) => {
Ok(QueryItemOrSumReturnType::CountSumValue(count_value, sum_value))
Ok(QueryItemOrSumReturnType::CountSumValue(
count_value,
sum_value,
))
}
_ => Err(Error::InvalidQuery(
"the reference must result in an item",
Expand All @@ -470,9 +473,9 @@ where {
Element::CountTree(_, count_value, _) => {
Ok(QueryItemOrSumReturnType::CountValue(count_value))
}
Element::CountSumTree(_, count_value, sum_value, _) => {
Ok(QueryItemOrSumReturnType::CountSumValue(count_value, sum_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",
Expand Down
Loading

0 comments on commit 1ba42f5

Please sign in to comment.