Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed issue with query if no path element #354

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions grovedb/src/element/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
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())

Check warning on line 44 in grovedb/src/element/get.rs

View workflow job for this annotation

GitHub Actions / clippy

accessing first element with `key.as_ref().get(0)`

warning: accessing first element with `key.as_ref().get(0)` --> grovedb/src/element/get.rs:44:49 | 44 | format!("({} in decimal) ", key.as_ref().get(0).unwrap()) | ^^^^^^^^^^^^^^^^^^^ help: try: `key.as_ref().first()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#get_first = note: `#[warn(clippy::get_first)]` on by default
} else {
String::new()
};
Expand Down Expand Up @@ -347,6 +347,45 @@
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 @@ -174,7 +174,7 @@
}

#[cfg(feature = "minimal")]
impl<'db, 'ctx, 'a> fmt::Display for PathQueryPushArgs<'db, 'ctx, 'a>

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

View workflow job for this annotation

GitHub Actions / clippy

the following explicit lifetimes could be elided: 'a

warning: the following explicit lifetimes could be elided: 'a --> grovedb/src/element/query.rs:177:17 | 177 | impl<'db, 'ctx, 'a> fmt::Display for PathQueryPushArgs<'db, 'ctx, 'a> | ^^ ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes help: elide the lifetimes | 177 - impl<'db, 'ctx, 'a> fmt::Display for PathQueryPushArgs<'db, 'ctx, 'a> 177 + impl<'db, 'ctx> fmt::Display for PathQueryPushArgs<'db, 'ctx, '_> |
where
'db: 'ctx,
{
Expand Down Expand Up @@ -304,16 +304,16 @@
#[cfg(feature = "minimal")]
/// Returns a vector of result elements and the number of skipped items
/// based on given query
pub fn get_query_apply_function(
storage: &RocksDbStorage,
path: &[&[u8]],
sized_query: &SizedQuery,
query_options: QueryOptions,
result_type: QueryResultType,
transaction: TransactionArg,
add_element_function: fn(PathQueryPushArgs, &GroveVersion) -> CostResult<(), Error>,
grove_version: &GroveVersion,
) -> CostResult<(QueryResultElements, u16), Error> {

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

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> grovedb/src/element/query.rs:307:5 | 307 | / pub fn get_query_apply_function( 308 | | storage: &RocksDbStorage, 309 | | path: &[&[u8]], 310 | | sized_query: &SizedQuery, ... | 315 | | grove_version: &GroveVersion, 316 | | ) -> CostResult<(QueryResultElements, u16), Error> { | |______________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
check_grovedb_v0_with_cost!(
"get_query_apply_function",
grove_version
Expand Down Expand Up @@ -546,18 +546,20 @@
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 @@
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 @@
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 Expand Up @@ -731,20 +737,20 @@
/// expensive query.
#[cfg(feature = "minimal")]
// TODO: refactor
fn query_item(
storage: &RocksDbStorage,
item: &QueryItem,
results: &mut Vec<QueryResultElement>,
path: &[&[u8]],
sized_query: &SizedQuery,
transaction: TransactionArg,
limit: &mut Option<u16>,
offset: &mut Option<u16>,
query_options: QueryOptions,
result_type: QueryResultType,
add_element_function: fn(PathQueryPushArgs, &GroveVersion) -> CostResult<(), Error>,
grove_version: &GroveVersion,
) -> CostResult<(), Error> {

Check warning on line 753 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:740:5 | 740 | / fn query_item( 741 | | storage: &RocksDbStorage, 742 | | item: &QueryItem, 743 | | results: &mut Vec<QueryResultElement>, ... | 752 | | grove_version: &GroveVersion, 753 | | ) -> 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!(
Expand Down
Loading