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: early limit reduction #273

Merged
merged 3 commits into from
Oct 11, 2023
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
15 changes: 1 addition & 14 deletions grovedb/src/batch/key_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,7 @@ impl PartialEq<&[u8]> for KeyInfo {
#[cfg(feature = "full")]
impl PartialOrd<Self> for KeyInfo {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match self.as_slice().partial_cmp(other.as_slice()) {
None => None,
Some(ord) => match ord {
Ordering::Less => Some(Ordering::Less),
Ordering::Equal => {
let other_len = other.max_length();
match self.max_length().partial_cmp(&other_len) {
None => Some(Ordering::Equal),
Some(ord) => Some(ord),
}
}
Ordering::Greater => Some(Ordering::Greater),
},
}
Some(self.cmp(other))
}
}

Expand Down
16 changes: 8 additions & 8 deletions grovedb/src/batch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,19 @@ pub enum Op {

impl PartialOrd for Op {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (self, other) {
(Op::Delete, Op::Insert { .. }) => Some(Ordering::Less),
(Op::Delete, Op::Replace { .. }) => Some(Ordering::Less),
(Op::Insert { .. }, Op::Delete) => Some(Ordering::Greater),
(Op::Replace { .. }, Op::Delete) => Some(Ordering::Greater),
_ => Some(Ordering::Equal),
}
Some(self.cmp(other))
}
}

impl Ord for Op {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).expect("all ops have order")
match (self, other) {
(Op::Delete, Op::Insert { .. }) => Ordering::Less,
(Op::Delete, Op::Replace { .. }) => Ordering::Less,
(Op::Insert { .. }, Op::Delete) => Ordering::Greater,
(Op::Replace { .. }, Op::Delete) => Ordering::Greater,
_ => Ordering::Equal,
}
}
}

Expand Down
16 changes: 13 additions & 3 deletions grovedb/src/operations/proof/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
use crate::{
element::helpers::raw_decode,
operations::proof::util::{
reduce_limit_and_offset_by, write_slice_of_slice_to_slice, write_slice_to_vec,
write_to_vec, ProofTokenType, EMPTY_TREE_HASH,
increase_limit_and_offset_by, reduce_limit_and_offset_by, write_slice_of_slice_to_slice,
write_slice_to_vec, write_to_vec, ProofTokenType, EMPTY_TREE_HASH,
},
reference_path::path_from_reference_path_type,
versioning::{prepend_version_to_bytes, PROOF_VERSION},
Expand Down Expand Up @@ -169,16 +169,16 @@

/// Perform a pre-order traversal of the tree based on the provided
/// subqueries
fn prove_subqueries(
&self,
proofs: &mut Vec<u8>,
path: Vec<&[u8]>,
query: &PathQuery,
current_limit: &mut Option<u16>,
current_offset: &mut Option<u16>,
is_first_call: bool,
is_verbose: bool,
) -> CostResult<(), Error> {

Check warning on line 181 in grovedb/src/operations/proof/generate.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/operations/proof/generate.rs:172:5 | 172 | / fn prove_subqueries( 173 | | &self, 174 | | proofs: &mut Vec<u8>, 175 | | path: Vec<&[u8]>, ... | 180 | | is_verbose: bool, 181 | | ) -> CostResult<(), Error> { | |______________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
let mut cost = OperationCost::default();
let mut to_add_to_result_set: u16 = 0;

Expand Down Expand Up @@ -216,6 +216,9 @@

let mut is_leaf_tree = true;

let mut offset_inc = 0;
let mut limit_inc = 0;

let mut kv_iterator = KVIterator::new(subtree.storage.raw_iter(), &query.query.query)
.unwrap_add_cost(&mut cost);

Expand All @@ -231,7 +234,13 @@
if subquery_value.is_none() && subquery_path.is_none() {
// this element should be added to the result set
// hence we have to update the limit and offset value
reduce_limit_and_offset_by(current_limit, current_offset, 1);
let reduced_offset =
reduce_limit_and_offset_by(current_limit, current_offset, 1);
if reduced_offset {
offset_inc += 1;
} else {
limit_inc += 1;
}
continue;
}

Expand Down Expand Up @@ -411,6 +420,7 @@
if is_leaf_tree {
// if no useful subtree, then we care about the result set of this subtree.
// apply the sized query
increase_limit_and_offset_by(current_limit, current_offset, limit_inc, offset_inc);
let limit_offset = cost_return_on_error!(
&mut cost,
self.generate_and_store_merk_proof(
Expand Down Expand Up @@ -475,17 +485,17 @@

/// Generates query proof given a subtree and appends the result to a proof
/// list
fn generate_and_store_merk_proof<'a, S, B>(
&self,
path: &SubtreePath<B>,
subtree: &'a Merk<S>,
query: &Query,
limit_offset: LimitOffset,
proof_token_type: ProofTokenType,
proofs: &mut Vec<u8>,
is_verbose: bool,
key: &[u8],
) -> CostResult<(Option<u16>, Option<u16>), Error>

Check warning on line 498 in grovedb/src/operations/proof/generate.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (9/7)

warning: this function has too many arguments (9/7) --> grovedb/src/operations/proof/generate.rs:488:5 | 488 | / fn generate_and_store_merk_proof<'a, S, B>( 489 | | &self, 490 | | path: &SubtreePath<B>, 491 | | subtree: &'a Merk<S>, ... | 497 | | key: &[u8], 498 | | ) -> CostResult<(Option<u16>, Option<u16>), Error> | |______________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
where
S: StorageContext<'a> + 'a,
B: AsRef<[u8]>,
Expand Down
14 changes: 14 additions & 0 deletions grovedb/src/operations/proof/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,20 @@ pub fn reduce_limit_and_offset_by(
skip_limit
}

pub fn increase_limit_and_offset_by(
limit: &mut Option<u16>,
offset: &mut Option<u16>,
limit_inc: u16,
offset_inc: u16,
) {
if let Some(offset_value) = *offset {
*offset = Some(offset_value + offset_inc);
}
if let Some(limit_value) = *limit {
*limit = Some(limit_value + limit_inc);
}
}

/// Proved path-key-values
pub type ProvedPathKeyValues = Vec<ProvedPathKeyValue>;

Expand Down
Loading