Skip to content

Commit

Permalink
better injection of subqueries
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Jan 12, 2022
1 parent 2939868 commit 2b39d4d
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 174 deletions.
2 changes: 2 additions & 0 deletions grovedb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub enum Error {
InvalidProof(&'static str),
#[error("invalid path: {0}")]
InvalidPath(&'static str),
#[error("missing parameter: {0}")]
MissingParameter(&'static str),
// Irrecoverable errors
#[error("storage error: {0}")]
StorageError(#[from] PrefixedRocksDbStorageError),
Expand Down
171 changes: 2 additions & 169 deletions grovedb/src/operations/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl GroveDb {
None => &self.subtrees,
Some(_) => &self.temp_subtrees,
};
get_path_query_on_trees(self, path_query, subtrees)
self.get_path_query_on_trees(path_query, subtrees)
}

fn get_path_query_on_trees(
Expand All @@ -124,173 +124,6 @@ impl GroveDb {
let merk = subtrees
.get(&Self::compress_subtree_key(path, None))
.ok_or(Error::InvalidPath("no subtree found under that path"))?;
let sized_query = &path_query.query;
let mut result = Vec::new();
let mut iter = merk.raw_iter();

let mut limit = if sized_query.limit.is_some() {
sized_query.limit.unwrap()
} else {
u16::MAX
};
let original_offset = if sized_query.offset.is_some() {
sized_query.offset.unwrap()
} else {
0 as u16
};
let mut offset = original_offset;

for item in sized_query.query.iter() {
match item {
QueryItem::Key(key) => {
if limit > 0 {
if offset == 0 {
result.push(Element::get(merk, key)?);
limit -= 1;
} else {
offset -= 1;
}
}
}
QueryItem::Range(Range { start, end }) => {
iter.seek(if sized_query.left_to_right {
start
} else {
end
});
while limit > 0
&& iter.valid()
&& iter.key().is_some()
&& iter.key()
!= Some(if sized_query.left_to_right {
end
} else {
start
})
{
let element =
raw_decode(iter.value().expect("if key exists then value should too"))?;
match element {
Element::Tree(_) => {
// if the query had a subquery then we should get elements from it
if let Some(subquery_key) = path_query.subquery_key {
// this means that for each element we should get the element at
// the subquery_key
let mut path_vec = path.to_vec();
path_vec.push(iter.key().expect("key should exist"));

if path_query.subquery.is_some() {
path_vec.push(subquery_key);

let inner_merk = self
.subtrees
.get(&Self::compress_subtree_key(
path_vec.as_slice(),
None,
))
.ok_or(Error::InvalidPath(
"no subtree found under that path",
))?;
let inner_limit = if sized_query.limit.is_some() {
Some(limit)
} else {
None
};
let inner_offset = if sized_query.offset.is_some() {
Some(offset)
} else {
None
};
let inner_query = SizedQuery::new(
path_query.subquery.clone().unwrap(),
inner_limit,
inner_offset,
sized_query.left_to_right,
);
let (mut sub_elements, skipped) =
Element::get_sized_query(inner_merk, &inner_query)?;
limit -= sub_elements.len() as u16;
offset -= skipped;
result.append(&mut sub_elements);
} else {
let inner_merk = self
.subtrees
.get(&Self::compress_subtree_key(
path_vec.as_slice(),
None,
))
.ok_or(Error::InvalidPath(
"no subtree found under that path",
))?;
if offset == 0 {
result.push(Element::get(inner_merk, subquery_key)?);
limit -= 1;
} else {
offset -= 1;
}
}
}
}
_ => {
if offset == 0 {
result.push(element);
limit -= 1;
} else {
offset -= 1;
}
}
}
if sized_query.left_to_right {
iter.next();
} else {
iter.prev();
}
}
}
QueryItem::RangeInclusive(r) => {
let start = r.start();
let end = r.end();
iter.seek(if sized_query.left_to_right {
start
} else {
end
});
let mut work = true;
while iter.valid() && iter.key().is_some() && work {
if iter.key()
== Some(if sized_query.left_to_right {
end
} else {
start
})
{
work = false;
}
if offset == 0 {
let element = raw_decode(
iter.value().expect("if key exists then value should too"),
)?;
result.push(element);
limit -= 1;
} else {
offset -= 1;
}
if sized_query.left_to_right {
iter.next();
} else {
iter.prev();
}
}
}
QueryItem::RangeFull(_) => {}
QueryItem::RangeFrom(_) => {}
QueryItem::RangeTo(_) => {}
QueryItem::RangeToInclusive(_) => {}
}
if limit == 0 {
break;
}
}
Ok((result, original_offset - offset))
Element::get_path_query(merk, path_query)
}
}
2 changes: 1 addition & 1 deletion grovedb/src/operations/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl GroveDb {
// First we must get elements

if reduced_proof_query.subquery_key.is_some() {
self.get_path_queries(&[&reduced_proof_query]);
self.get_path_queries(&[&reduced_proof_query], None);


let mut path_vec = path.to_vec();
Expand Down
Loading

0 comments on commit 2b39d4d

Please sign in to comment.