Skip to content

Commit

Permalink
Merge branch 'feat/bigSumTrees' of github.com:dashevo/grovedb into fe…
Browse files Browse the repository at this point in the history
…at/bigSumTrees
  • Loading branch information
QuantumExplorer committed Jan 16, 2025
2 parents 9e313f4 + 384894f commit cee2f29
Show file tree
Hide file tree
Showing 23 changed files with 205 additions and 169 deletions.
63 changes: 50 additions & 13 deletions grovedb/src/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,22 +397,28 @@ fn merk_proof_node_to_grovedbg(node: Node) -> Result<MerkProofNode, crate::Error
let element = crate::Element::deserialize(&value, GroveVersion::latest())?;
MerkProofNode::KVValueHash(key, element_to_grovedbg(element), hash)
}
Node::KVValueHashFeatureType(key, value, hash, TreeFeatureType::BasicMerkNode) => {
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) => {
Expand Down Expand Up @@ -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,
}
}
}
}

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions grovedb/src/element/constructor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Constructor
//! Functions for setting an element's type
#[cfg(feature = "minimal")]
use crate::element::{BigSumValue, CountValue};
#[cfg(feature = "minimal")]
use crate::{
Expand Down
1 change: 1 addition & 0 deletions grovedb/src/element/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use grovedb_costs::OperationCost;
#[cfg(feature = "minimal")]
use grovedb_costs::{storage_cost::removal::StorageRemovedBytes, CostResult, CostsExt};
#[cfg(feature = "minimal")]
use grovedb_merk::merk::tree_type::TreeType;
#[cfg(feature = "minimal")]
use grovedb_merk::{BatchEntry, Error as MerkError, Merk, MerkOptions, Op};
Expand Down
58 changes: 16 additions & 42 deletions grovedb/src/element/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use grovedb_costs::{
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;
Expand All @@ -18,6 +19,7 @@ use grovedb_version::{
};
use integer_encoding::VarInt;

#[cfg(feature = "minimal")]
use crate::{
element::{CostSize, SUM_ITEM_COST_SIZE},
operations::proof::util::path_as_slices_hex_to_ascii,
Expand All @@ -33,7 +35,6 @@ impl Element {
merk: &Merk<S>,
key: K,
allow_cache: bool,
extra_error_info: Option<impl FnOnce() -> String>,
grove_version: &GroveVersion,
) -> CostResult<Element, Error> {
check_grovedb_v0_with_cost!("get", grove_version.grovedb_versions.element.get);
Expand All @@ -45,19 +46,14 @@ impl Element {
} else {
String::new()
};
let extra_error_info_string = extra_error_info
.map(|callback| format!(" {}", callback()))
.unwrap_or(String::new());
Error::PathKeyNotFound(format!(
"get: key 0x{} {}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,
extra_error_info_string,
))
})
})
Expand Down Expand Up @@ -236,7 +232,6 @@ 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
Expand Down Expand Up @@ -328,6 +323,8 @@ impl Element {
allow_cache: bool,
grove_version: &GroveVersion,
) -> CostResult<Element, Error> {
use crate::error::GroveDbErrorExt;

check_grovedb_v0_with_cost!(
"get_with_absolute_refs",
grove_version
Expand All @@ -339,13 +336,8 @@ impl Element {

let element = cost_return_on_error!(
&mut cost,
Self::get(
merk,
key.as_ref(),
allow_cache,
Some(|| { format!("path is {}", path_as_slices_hex_to_ascii(path)) }),
grove_version
)
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!(
Expand Down Expand Up @@ -437,15 +429,9 @@ mod tests {
.unwrap();

assert_eq!(
Element::get(
&merk,
b"another-key",
true,
None::<fn() -> String>,
grove_version
)
.unwrap()
.expect("expected successful get"),
Element::get(&merk, b"another-key", true, grove_version)
.unwrap()
.expect("expected successful get"),
Element::new_item(b"value".to_vec()),
);

Expand All @@ -455,24 +441,12 @@ mod tests {
.unwrap()
.expect("expected successful insertion 2");

let cost_with_cache = Element::get(
&merk,
b"another-key",
true,
None::<fn() -> String>,
grove_version,
)
.cost_as_result()
.expect("expected to get cost");
let cost_without_cache = Element::get(
&merk,
b"another-key",
false,
None::<fn() -> String>,
grove_version,
)
.cost_as_result()
.expect("expected to get cost");
let cost_with_cache = Element::get(&merk, b"another-key", true, grove_version)
.cost_as_result()
.expect("expected to get cost");
let cost_without_cache = Element::get(&merk, b"another-key", false, grove_version)
.cost_as_result()
.expect("expected to get cost");
assert_ne!(cost_with_cache, cost_without_cache);

assert_eq!(
Expand Down
2 changes: 2 additions & 0 deletions grovedb/src/element/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use grovedb_merk::tree::kv::{
ValueDefinedCostType::{LayeredValueDefinedCost, SpecializedValueDefinedCost},
};

#[cfg(feature = "minimal")]
use grovedb_merk::{
merk::{tree_type::TreeType, NodeType},
MaybeTree,
Expand All @@ -23,6 +24,7 @@ use grovedb_version::{check_grovedb_v0, error::GroveVersionError, version::Grove
#[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};
#[cfg(feature = "minimal")]
use crate::reference_path::path_from_reference_path_type;
Expand Down
48 changes: 12 additions & 36 deletions grovedb/src/element/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,15 +502,9 @@ mod tests {
.expect("expected successful insertion 2");

assert_eq!(
Element::get(
&merk,
b"another-key",
true,
None::<fn() -> String>,
grove_version
)
.unwrap()
.expect("expected successful get"),
Element::get(&merk, b"another-key", true, grove_version)
.unwrap()
.expect("expected successful get"),
Element::new_item(b"value".to_vec()),
);
}
Expand Down Expand Up @@ -541,15 +535,9 @@ mod tests {
assert!(!inserted);
assert_eq!(previous, None);
assert_eq!(
Element::get(
&merk,
b"another-key",
true,
None::<fn() -> String>,
grove_version
)
.unwrap()
.expect("expected successful get"),
Element::get(&merk, b"another-key", true, grove_version)
.unwrap()
.expect("expected successful get"),
Element::new_item(b"value".to_vec()),
);
}
Expand Down Expand Up @@ -592,15 +580,9 @@ mod tests {
let merk = empty_path_merk_read_only(&*storage, grove_version);

assert_eq!(
Element::get(
&merk,
b"another-key",
true,
None::<fn() -> String>,
grove_version
)
.unwrap()
.expect("expected successful get"),
Element::get(&merk, b"another-key", true, grove_version)
.unwrap()
.expect("expected successful get"),
Element::new_item(b"value2".to_vec()),
);
}
Expand All @@ -622,15 +604,9 @@ mod tests {
assert_eq!(previous, None);

assert_eq!(
Element::get(
&merk,
b"another-key",
true,
None::<fn() -> String>,
grove_version
)
.unwrap()
.expect("expected successful get"),
Element::get(&merk, b"another-key", true, grove_version)
.unwrap()
.expect("expected successful get"),
Element::new_item(b"value2".to_vec()),
);
}
Expand Down
1 change: 1 addition & 0 deletions grovedb/src/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod serialize;
use std::fmt;

use bincode::{Decode, Encode};
#[cfg(any(feature = "minimal", feature = "verify"))]
use grovedb_merk::estimated_costs::SUM_AND_COUNT_LAYER_COST_SIZE;
#[cfg(any(feature = "minimal", feature = "verify"))]
use grovedb_merk::estimated_costs::SUM_VALUE_EXTRA_COST;
Expand Down
14 changes: 6 additions & 8 deletions grovedb/src/element/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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;
Expand Down Expand Up @@ -745,6 +746,8 @@ impl Element {
add_element_function: fn(PathQueryPushArgs, &GroveVersion) -> CostResult<(), Error>,
grove_version: &GroveVersion,
) -> CostResult<(), Error> {

Check warning on line 748 in grovedb/src/element/query.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (12/7)

warning: this function has too many arguments (12/7) --> grovedb/src/element/query.rs:735:5 | 735 | / fn query_item( 736 | | storage: &RocksDbStorage, 737 | | item: &QueryItem, 738 | | results: &mut Vec<QueryResultElement>, ... | 747 | | grove_version: &GroveVersion, 748 | | ) -> CostResult<(), Error> { | |______________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
use crate::error::GroveDbErrorExt;

check_grovedb_v0_with_cost!(
"query_item",
grove_version.grovedb_versions.element.query_item
Expand All @@ -766,14 +769,9 @@ impl Element {
subtree,
grove_version,
{
Element::get(
&subtree,
key,
query_options.allow_cache,
Some(|| format!("path is {}", path_as_slices_hex_to_ascii(path))),
grove_version,
)
.unwrap_add_cost(&mut cost)
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)
}
);
match element_res {
Expand Down
Loading

0 comments on commit cee2f29

Please sign in to comment.