Skip to content

Commit

Permalink
more work
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Jul 10, 2024
1 parent ab3ff66 commit b3f6c29
Show file tree
Hide file tree
Showing 20 changed files with 657 additions and 330 deletions.
15 changes: 9 additions & 6 deletions grovedb/src/batch/estimated_costs/average_case_costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,15 @@ impl<G, SR> TreeCache<G, SR> for AverageCaseTreeCacheKnownPaths {
))
})
);
GroveDb::add_average_case_get_merk_at_path::<RocksDbStorage>(
&mut cost,
path,
layer_should_be_empty,
layer_info.is_sum_tree,
grove_version,
cost_return_on_error_no_add!(
&cost,
GroveDb::add_average_case_get_merk_at_path::<RocksDbStorage>(
&mut cost,
path,
layer_should_be_empty,
layer_info.is_sum_tree,
grove_version,
)
);
self.cached_merks
.insert(path.clone(), layer_info.is_sum_tree);
Expand Down
26 changes: 16 additions & 10 deletions grovedb/src/batch/estimated_costs/worst_case_costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,14 @@ impl<G, SR> TreeCache<G, SR> for WorstCaseTreeCacheKnownPaths {

// Then we have to get the tree
if !self.cached_merks.contains(path) {
GroveDb::add_worst_case_get_merk_at_path::<RocksDbStorage>(
&mut cost,
path,
false,
grove_version,
cost_return_on_error_no_add!(
&cost,
GroveDb::add_worst_case_get_merk_at_path::<RocksDbStorage>(
&mut cost,
path,
false,
grove_version,
)
);
self.cached_merks.insert(path.clone());
}
Expand Down Expand Up @@ -239,11 +242,14 @@ impl<G, SR> TreeCache<G, SR> for WorstCaseTreeCacheKnownPaths {
if let Some(_estimated_layer_info) = self.paths.get(&base_path) {
// Then we have to get the tree
if !self.cached_merks.contains(&base_path) {
GroveDb::add_worst_case_get_merk_at_path::<RocksDbStorage>(
&mut cost,
&base_path,
false,
grove_version,
cost_return_on_error_no_add!(
&cost,
GroveDb::add_worst_case_get_merk_at_path::<RocksDbStorage>(
&mut cost,
&base_path,
false,
grove_version,
)
);
self.cached_merks.insert(base_path);
}
Expand Down
12 changes: 10 additions & 2 deletions grovedb/src/batch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,11 @@ impl GroveDb {
);
cost_return_on_error!(
&mut cost,
Self::apply_batch_structure(batch_structure, batch_apply_options)
Self::apply_batch_structure(
batch_structure,
batch_apply_options,
grove_version
)
);
}

Expand All @@ -2377,7 +2381,11 @@ impl GroveDb {
);
cost_return_on_error!(
&mut cost,
Self::apply_batch_structure(batch_structure, batch_apply_options)
Self::apply_batch_structure(
batch_structure,
batch_apply_options,
grove_version
)
);
}
}
Expand Down
85 changes: 62 additions & 23 deletions grovedb/src/estimated_costs/average_case_costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use grovedb_merk::{
HASH_LENGTH,
};
use grovedb_storage::{worst_case_costs::WorstKeyLength, Storage};
use grovedb_version::{check_v0_with_cost, version::GroveVersion};
use grovedb_version::{
check_v0, check_v0_with_cost, error::GroveVersionError, version::GroveVersion,
};
use integer_encoding::VarInt;

use crate::{
Expand All @@ -33,8 +35,8 @@ impl GroveDb {
merk_should_be_empty: bool,
is_sum_tree: bool,
grove_version: &GroveVersion,
) {
check_v0_with_cost!(
) -> Result<(), Error> {
check_v0!(
"add_average_case_get_merk_at_path",
grove_version
.grovedb_versions
Expand All @@ -55,10 +57,13 @@ impl GroveDb {
key.max_length() as u32,
HASH_LENGTH as u32,
is_sum_tree,
grove_version,
);
}
}
*cost += S::get_storage_context_cost(path.as_vec());

Ok(())
}

/// Add average case for insertion into merk
Expand Down Expand Up @@ -140,7 +145,13 @@ impl GroveDb {
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_layered(
&mut cost,
key_len,
value_len,
in_tree_using_sums,
grove_version,
);
if let Some(input) = propagate_if_input {
add_average_case_merk_propagate(&mut cost, input).map_err(Error::MerkError)
} else {
Expand Down Expand Up @@ -226,13 +237,20 @@ impl GroveDb {
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_layered(
&mut cost,
key_len,
value_len,
in_tree_using_sums,
grove_version,
)
}
_ => add_cost_case_merk_insert(
&mut cost,
key_len,
cost_return_on_error_no_add!(&cost, value.serialized_size()) as u32,
cost_return_on_error_no_add!(&cost, value.serialized_size(grove_version)) as u32,
in_tree_using_sums,
grove_version,
),
};
if let Some(level) = propagate_for_level {
Expand Down Expand Up @@ -281,6 +299,7 @@ impl GroveDb {
key_len,
value_len,
in_tree_using_sums,
grove_version,
)
}
Element::Item(_, flags) | Element::SumItem(_, flags) => {
Expand All @@ -292,21 +311,23 @@ impl GroveDb {
let sum_item_cost_size = if value.is_sum_item() {
SUM_ITEM_COST_SIZE
} else {
cost_return_on_error_no_add!(&cost, value.serialized_size()) as u32
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,
grove_version,
)
}
_ => add_cost_case_merk_replace_same_size(
&mut cost,
key_len,
cost_return_on_error_no_add!(&cost, value.serialized_size()) as u32,
cost_return_on_error_no_add!(&cost, value.serialized_size(grove_version)) as u32,
in_tree_using_sums,
grove_version,
),
};
if let Some(level) = propagate_for_level {
Expand Down Expand Up @@ -347,14 +368,16 @@ impl GroveDb {
});
// Items need to be always the same serialized size for this to work
let item_cost_size =
cost_return_on_error_no_add!(&cost, value.serialized_size()) as u32;
cost_return_on_error_no_add!(&cost, value.serialized_size(grove_version))
as u32;
let value_len = item_cost_size + flags_len;
add_cost_case_merk_patch(
&mut cost,
key_len,
value_len,
change_in_bytes,
in_tree_using_sums,
grove_version,
)
}
_ => {
Expand Down Expand Up @@ -413,8 +436,8 @@ impl GroveDb {
estimated_element_size: u32,
in_parent_tree_using_sums: bool,
grove_version: &GroveVersion,
) {
check_v0_with_cost!(
) -> Result<(), Error> {
check_v0!(
"add_average_case_has_raw_cost",
grove_version
.grovedb_versions
Expand All @@ -427,10 +450,12 @@ impl GroveDb {
key.max_length() as u32,
estimated_element_size,
in_parent_tree_using_sums,
grove_version,
);
cost.seek_count += 1;
cost.storage_loaded_bytes += value_size;
*cost += S::get_storage_context_cost(path.as_vec());
Ok(())
}

/// Adds the average case of checking to see if a tree exists
Expand All @@ -442,8 +467,8 @@ impl GroveDb {
is_sum_tree: bool,
in_parent_tree_using_sums: bool,
grove_version: &GroveVersion,
) {
check_v0_with_cost!(
) -> Result<(), Error> {
check_v0!(
"add_average_case_has_raw_tree_cost",
grove_version
.grovedb_versions
Expand All @@ -464,7 +489,7 @@ impl GroveDb {
estimated_element_size,
in_parent_tree_using_sums,
grove_version,
);
)
}

/// Add average case to get raw cost into merk
Expand All @@ -475,8 +500,8 @@ impl GroveDb {
estimated_element_size: u32,
in_parent_tree_using_sums: bool,
grove_version: &GroveVersion,
) {
check_v0_with_cost!(
) -> Result<(), Error> {
check_v0!(
"add_average_case_get_raw_cost",
grove_version
.grovedb_versions
Expand All @@ -491,7 +516,9 @@ impl GroveDb {
key.max_length() as u32,
estimated_element_size,
in_parent_tree_using_sums,
);
grove_version,
)
.map_err(Error::MerkError)
}

/// adds the average cost of getting a tree
Expand All @@ -503,8 +530,8 @@ impl GroveDb {
is_sum_tree: bool,
in_parent_tree_using_sums: bool,
grove_version: &GroveVersion,
) {
check_v0_with_cost!(
) -> Result<(), Error> {
check_v0!(
"add_average_case_get_raw_tree_cost",
grove_version
.grovedb_versions
Expand All @@ -524,7 +551,9 @@ impl GroveDb {
key.max_length() as u32,
estimated_element_size,
in_parent_tree_using_sums,
);
grove_version,
)
.map_err(Error::MerkError)
}

/// adds the average cost of getting an element knowing there can be
Expand All @@ -537,8 +566,8 @@ impl GroveDb {
estimated_element_size: u32,
estimated_references_sizes: Vec<u32>,
grove_version: &GroveVersion,
) {
check_v0_with_cost!(
) -> Result<(), Error> {
check_v0!(
"add_average_case_get_cost",
grove_version
.grovedb_versions
Expand All @@ -552,10 +581,12 @@ impl GroveDb {
key.max_length() as u32,
estimated_element_size,
in_parent_tree_using_sums,
grove_version,
);
cost.seek_count += 1 + estimated_references_sizes.len() as u16;
cost.storage_loaded_bytes += value_size + estimated_references_sizes.iter().sum::<u32>();
*cost += S::get_storage_context_cost(path.as_vec());
Ok(())
}
}

Expand All @@ -582,6 +613,7 @@ mod test {

#[test]
fn test_get_merk_node_average_case() {
let grove_version = GroveVersion::latest();
// Open a merk and insert 10 elements.
let tmp_dir = TempDir::new().expect("cannot open tempdir");
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
Expand Down Expand Up @@ -636,12 +668,19 @@ 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);
add_average_case_get_merk_node(
&mut cost,
key.max_length() as u32,
60,
false,
grove_version,
);
assert_eq!(cost, node_result.cost);
}

#[test]
fn test_has_raw_average_case() {
let grove_version = GroveVersion::latest();
let tmp_dir = TempDir::new().unwrap();
let db = GroveDb::open(tmp_dir.path()).unwrap();

Expand Down
Loading

0 comments on commit b3f6c29

Please sign in to comment.