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

feat: PathQuery depth #328

Merged
merged 6 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion grovedb/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ fn main() {
let grovedbg_zip_path = out_dir.join("grovedbg.zip");

if !grovedbg_zip_path.exists() {
let response = reqwest::blocking::get(format!("https://github.com/dashpay/grovedbg/releases/download/{GROVEDBG_VERSION}/grovedbg-{GROVEDBG_VERSION}.zip"))
let response = reqwest::blocking::get(format!(
"https://github.com/dashpay/grovedbg/releases/download/{GROVEDBG_VERSION}/\
grovedbg-{GROVEDBG_VERSION}.zip"
))
.expect("can't download GroveDBG artifact");

let mut grovedbg_zip = File::create(&grovedbg_zip_path).unwrap();
Expand Down
8 changes: 8 additions & 0 deletions grovedb/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ impl PathQuery {
Self { path, query }
}

/// The max depth od the query, this is the maximum layers we could get back
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
/// from grovedb
pub fn max_depth(&self) -> u32 {
self.query.query.max_depth()
}

/// Gets the path of all terminal keys
pub fn terminal_keys(
&self,
Expand Down Expand Up @@ -1634,6 +1640,8 @@ mod tests {
},
};

assert_eq!(path_query.max_depth(), 4);

{
let path = vec![];
let first = path_query
Expand Down
31 changes: 31 additions & 0 deletions merk/src/proofs/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ pub struct SubqueryBranch {
pub subquery: Option<Box<Query>>,
}

impl SubqueryBranch {
/// Returns the depth of the subquery branch
/// This depth is how many GroveDB layers down we could query at maximum
#[inline]
pub fn max_depth(&self) -> u32 {
let subquery_path_depth = self
.subquery_path
.as_ref()
.map_or(0, |path| path.len() as u32);
let subquery_depth = self.subquery.as_ref().map_or(0, |query| query.max_depth());
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
subquery_path_depth + subquery_depth
}
}

#[cfg(any(feature = "full", feature = "verify"))]
/// `Query` represents one or more keys or ranges of keys, which can be used to
/// resolve a proof which will include all the requested values.
Expand Down Expand Up @@ -476,6 +490,23 @@ impl Query {
// checks if all searched for items are keys
self.items.iter().all(|a| a.is_key())
}

/// Returns the depth of the subquery branch
/// This depth is how many GroveDB layers down we could query at maximum
pub fn max_depth(&self) -> u32 {
let default_subquery_branch_depth = self.default_subquery_branch.max_depth();
let conditional_subquery_branches_max_depth = self
.conditional_subquery_branches
.as_ref()
.map_or(0, |condition_subqueries| {
condition_subqueries
.values()
.map(|conditional_subquery_branch| conditional_subquery_branch.max_depth())
.max()
.unwrap_or_default()
});
1 + default_subquery_branch_depth.max(conditional_subquery_branches_max_depth)
}
}

#[cfg(feature = "full")]
Expand Down
Loading