Skip to content

Commit

Permalink
fix: fixed issue with query if no path element (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer authored Jan 21, 2025
1 parent d29da25 commit f92956f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 43 deletions.
39 changes: 39 additions & 0 deletions grovedb/src/element/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,45 @@ impl Element {
Ok(absolute_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
pub fn get_optional_with_absolute_refs<'db, K: AsRef<[u8]>, S: StorageContext<'db>>(
merk: &Merk<S>,
path: &[&[u8]],
key: K,
allow_cache: bool,
grove_version: &GroveVersion,
) -> CostResult<Option<Element>, Error> {
use crate::error::GroveDbErrorExt;

check_grovedb_v0_with_cost!(
"get_with_absolute_refs",
grove_version
.grovedb_versions
.element
.get_with_absolute_refs
);
let mut cost = OperationCost::default();

let maybe_element = cost_return_on_error!(
&mut cost,
Self::get_optional(merk, key.as_ref(), allow_cache, grove_version)
.add_context(format!("path is {}", path_as_slices_hex_to_ascii(path)))
);

match maybe_element {
None => Ok(None).wrap_with_cost(cost),
Some(element) => {
let absolute_element = cost_return_on_error_no_add!(
&cost,
element.convert_if_reference_to_absolute_reference(path, Some(key.as_ref()))
);
Ok(Some(absolute_element)).wrap_with_cost(cost)
}
}
}

#[cfg(feature = "minimal")]
/// Get an element's value hash from Merk under a key
pub fn get_value_hash<'db, K: AsRef<[u8]>, S: StorageContext<'db>>(
Expand Down
92 changes: 49 additions & 43 deletions grovedb/src/element/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,18 +546,20 @@ impl Element {
subtree,
grove_version,
{
results.push(QueryResultElement::ElementResultItem(
cost_return_on_error!(
&mut cost,
Element::get_with_absolute_refs(
&subtree,
path_vec.as_slice(),
subquery_path_last_key.as_slice(),
allow_cache,
grove_version,
)
),
));
if let Some(element) = cost_return_on_error!(
&mut cost,
Element::get_optional_with_absolute_refs(
&subtree,
path_vec.as_slice(),
subquery_path_last_key.as_slice(),
allow_cache,
grove_version,
)
) {
results.push(QueryResultElement::ElementResultItem(
element,
));
}
}
);
}
Expand All @@ -571,21 +573,23 @@ impl Element {
subtree,
grove_version,
{
results.push(QueryResultElement::KeyElementPairResultItem(
(
subquery_path_last_key.to_vec(),
cost_return_on_error!(
&mut cost,
Element::get_with_absolute_refs(
&subtree,
path_vec.as_slice(),
subquery_path_last_key.as_slice(),
allow_cache,
grove_version,
)
),
),
));
if let Some(element) = cost_return_on_error!(
&mut cost,
Element::get_optional_with_absolute_refs(
&subtree,
path_vec.as_slice(),
subquery_path_last_key.as_slice(),
allow_cache,
grove_version,
)
) {
results.push(
QueryResultElement::KeyElementPairResultItem((
subquery_path_last_key.to_vec(),
element,
)),
);
}
}
);
}
Expand All @@ -599,22 +603,24 @@ impl Element {
subtree,
grove_version,
{
results.push(
QueryResultElement::PathKeyElementTrioResultItem((
path_vec.iter().map(|p| p.to_vec()).collect(),
subquery_path_last_key.to_vec(),
cost_return_on_error!(
&mut cost,
Element::get_with_absolute_refs(
&subtree,
path_vec.as_slice(),
subquery_path_last_key.as_slice(),
allow_cache,
grove_version,
)
),
)),
);
if let Some(element) = cost_return_on_error!(
&mut cost,
Element::get_optional_with_absolute_refs(
&subtree,
path_vec.as_slice(),
subquery_path_last_key.as_slice(),
allow_cache,
grove_version,
)
) {
results.push(
QueryResultElement::PathKeyElementTrioResultItem((
path_vec.iter().map(|p| p.to_vec()).collect(),
subquery_path_last_key.to_vec(),
element,
)),
);
}
}
);
}
Expand Down

0 comments on commit f92956f

Please sign in to comment.