Skip to content

Commit

Permalink
more work
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Jul 8, 2024
1 parent 14e3b42 commit ab8d1e2
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
2 changes: 1 addition & 1 deletion grovedb/src/operations/proof/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ impl GroveDb {
if !done_with_results
&& query.has_subquery_or_matching_in_path_on_key(key) =>
{
println!("found tree {}, query is {:?}", hex_to_ascii(key), query);
println!("found tree {}, query is {}", hex_to_ascii(key), query);
// We only want to check in sub nodes for the proof if the tree has
// elements
let mut lower_path = path.clone();
Expand Down
90 changes: 88 additions & 2 deletions grovedb/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,7 @@ impl PathQuery {
}

Check warning on line 312 in grovedb/src/query/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

manual implementation of `Option::map`

warning: manual implementation of `Option::map` --> grovedb/src/query/mod.rs:308:41 | 308 | / ... if let Some(subquery) = &subquery_branch.subquery { 309 | | ... Some(InternalCowItemsQuery::from_query(subquery)) 310 | | ... } else { 311 | | ... None 312 | | ... } | |_______________________^ help: try: `subquery_branch.subquery.as_ref().map(|subquery| InternalCowItemsQuery::from_query(subquery))` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_map = note: `#[warn(clippy::manual_map)]` on by default
} else {
let last_path_item = path.len() == subquery_path.len();
let has_subquery =
query.default_subquery_branch.subquery.is_some();
let has_subquery = subquery_branch.subquery.is_some();
Some(InternalCowItemsQuery::from_key_when_in_path(
&subquery_path[path_after_top_removed.len()],
last_path_item,
Expand Down Expand Up @@ -422,6 +421,23 @@ pub(crate) enum HasSubquery<'a> {
Conditionally(Cow<'a, IndexMap<QueryItem, SubqueryBranch>>),
}

#[cfg(any(feature = "full", feature = "verify"))]
impl<'a> fmt::Display for HasSubquery<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
HasSubquery::NoSubquery => write!(f, "NoSubquery"),
HasSubquery::Always => write!(f, "Always"),
HasSubquery::Conditionally(map) => {
writeln!(f, "Conditionally {{")?;
for (query_item, subquery_branch) in map.iter() {
writeln!(f, " {query_item}: {subquery_branch},")?;
}
write!(f, "}}")
}
}
}
}

impl<'a> HasSubquery<'a> {
/// Checks to see if we have a subquery on a specific key
pub fn has_subquery_on_key(&self, key: &[u8]) -> bool {
Expand Down Expand Up @@ -450,6 +466,25 @@ pub(crate) struct InternalCowItemsQuery<'a> {
pub in_path: Option<Cow<'a, Key>>,
}

#[cfg(any(feature = "full", feature = "verify"))]
impl<'a> fmt::Display for InternalCowItemsQuery<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "InternalCowItemsQuery {{")?;
writeln!(f, " items: [")?;
for item in self.items.iter() {
writeln!(f, " {item},")?;
}
writeln!(f, " ]")?;
writeln!(f, " has_subquery: {}", self.has_subquery)?;
writeln!(f, " left_to_right: {}", self.left_to_right)?;
match &self.in_path {
Some(path) => writeln!(f, " in_path: Some({})", hex_to_ascii(&path)),

Check warning on line 481 in grovedb/src/query/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> grovedb/src/query/mod.rs:481:75 | 481 | Some(path) => writeln!(f, " in_path: Some({})", hex_to_ascii(&path)), | ^^^^^ help: change this to: `path` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
None => writeln!(f, " in_path: None"),
}?;
write!(f, "}}")
}
}

impl<'a> InternalCowItemsQuery<'a> {
/// Checks to see if we have a subquery on a specific key
pub fn has_subquery_on_key(&self, key: &[u8]) -> bool {

Check warning on line 490 in grovedb/src/query/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

method `has_subquery_on_key` is never used

warning: method `has_subquery_on_key` is never used --> grovedb/src/query/mod.rs:490:12 | 488 | impl<'a> InternalCowItemsQuery<'a> { | ---------------------------------- method in this implementation 489 | /// Checks to see if we have a subquery on a specific key 490 | pub fn has_subquery_on_key(&self, key: &[u8]) -> bool { | ^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -521,6 +556,7 @@ mod tests {
query::{query_item::QueryItem, SubqueryBranch},
Query,
};
use indexmap::IndexMap;

use crate::{
query::{HasSubquery, InternalCowItemsQuery},
Expand Down Expand Up @@ -1352,4 +1388,54 @@ mod tests {
);
}
}

#[test]
fn test_conditional_subquery_refusing_elements() {
let empty_vec: Vec<u8> = vec![];
let zero_vec: Vec<u8> = vec![0];

let mut conditional_subquery_branches = IndexMap::new();
conditional_subquery_branches.insert(
QueryItem::Key(b"".to_vec()),
SubqueryBranch {
subquery_path: Some(vec![zero_vec.clone()]),
subquery: Some(Query::new().into()),
},
);

let path_query = PathQuery {
path: vec![TEST_LEAF.to_vec()],
query: SizedQuery {
query: Query {
items: vec![QueryItem::RangeFull(RangeFull)],
default_subquery_branch: SubqueryBranch {
subquery_path: Some(vec![zero_vec.clone()]),
subquery: None,
},
left_to_right: true,
conditional_subquery_branches: Some(conditional_subquery_branches),
},
limit: Some(100),
offset: None,
},
};

{
let path = vec![TEST_LEAF, empty_vec.as_slice()];

let second = path_query
.query_items_at_path(&path)
.expect("expected query items");

assert_eq!(
second,
InternalCowItemsQuery {
items: Cow::Owned(vec![QueryItem::Key(zero_vec.clone())]),
has_subquery: HasSubquery::NoSubquery,
left_to_right: true,
in_path: Some(Cow::Borrowed(&zero_vec)),
}
);
}
}
}
4 changes: 3 additions & 1 deletion grovedb/src/tests/query_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,11 @@ mod tests {

let subquery = Query::new();

// This conditional subquery expresses that we do not want to get values in ""
// tree
query.add_conditional_subquery(
QueryItem::Key(b"".to_vec()),
Some(vec![b"\0".to_vec()]),
Some(vec![b"\0".to_vec()]), // We want to go into 0 but we don't want to get anything
Some(subquery),
);

Expand Down

0 comments on commit ab8d1e2

Please sign in to comment.