Skip to content

Commit

Permalink
refactor: Fix numerous warnings (#31)
Browse files Browse the repository at this point in the history
* refactor: remove unused includes

* refactor: remove unneeded mut

* remove a number of unneeded `mut`

* replace usages of unused `skipped` variable with `_`

* ensure the values aren't None

* don't silently ignore failures

* don't silently ignore failures

* use _ instead of skipped

* use _ instead of other when not using it

* remove usused includes (according to cargo test)

* remove usused type alias

* remove usused include

* remove unneeded copies in make_prefixed_key

* boolean simplification as suggested by cargo clippy

* use !matches! as suggested by cargo clippy

* use implicit return syntax

* remove same body, if else

* remove unneeded clones

* use implicit return

* use iter instead of into_iter

* fix warning
```
warning: this expression borrows a reference (`&mut std::collections::HashMap<std::vec::Vec<u8>, merk::Merk<storage::rocksdb_storage::PrefixedRocksDbStorage>>`) that is immediately dereferenced by the compiler
```

* remove unneeded declaration

* use -=

* remove unnecessary borrowing

* remove unneeded clone

* unneeded borrow

* collapse

* use cloned and copied
  • Loading branch information
PastaPastaPasta authored Jan 21, 2022
1 parent 2e397d5 commit 1563eaf
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 191 deletions.
19 changes: 9 additions & 10 deletions grovedb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl GroveDb {
Some(_) => &self.temp_subtrees,
};

let prefixes: Vec<Vec<u8>> = subtrees.keys().map(|x| x.clone()).collect();
let prefixes: Vec<Vec<u8>> = subtrees.keys().cloned().collect();

// TODO: make StorageOrTransaction which will has the access to either storage
// or transaction
Expand Down Expand Up @@ -299,8 +299,7 @@ impl GroveDb {
.expect("`root_leaf_keys` must be in sync with `subtrees`");
leaf_hashes[*root_leaf_idx] = subtree_merk.root_hash();
}
let res = MerkleTree::<Sha256>::from_leaves(&leaf_hashes);
res
MerkleTree::<Sha256>::from_leaves(&leaf_hashes)
}

pub fn elements_iterator(
Expand Down Expand Up @@ -341,9 +340,9 @@ impl GroveDb {
if path_slice.is_empty() {
// Hit the root tree
match transaction {
None => self.root_tree = Self::build_root_tree(&subtrees, &root_leaf_keys),
None => self.root_tree = Self::build_root_tree(subtrees, root_leaf_keys),
Some(_) => {
self.temp_root_tree = Self::build_root_tree(&subtrees, &root_leaf_keys)
self.temp_root_tree = Self::build_root_tree(subtrees, root_leaf_keys)
}
};
break;
Expand All @@ -367,19 +366,19 @@ impl GroveDb {
/// A helper method to build a prefix to rocksdb keys or identify a subtree
/// in `subtrees` map by tree path;
fn compress_subtree_key(path: &[&[u8]], key: Option<&[u8]>) -> Vec<u8> {
let segments_iter = path.into_iter().map(|x| *x).chain(key.into_iter());
let segments_iter = path.iter().copied().chain(key.into_iter());
let mut segments_count = path.len();
if key.is_some() {
segments_count += 1;
}
let mut res = segments_iter.fold(Vec::<u8>::new(), |mut acc, p| {
acc.extend(p.into_iter());
acc.extend(p.iter());
acc
});

res.extend(segments_count.to_ne_bytes());
path.into_iter()
.map(|x| *x)
path.iter()
.copied()
.chain(key.into_iter())
.fold(&mut res, |acc, p| {
acc.extend(p.len().to_ne_bytes());
Expand Down Expand Up @@ -463,7 +462,7 @@ impl GroveDb {
/// Returns true if transaction is started. For more details on the
/// transaction usage, please check [`GroveDb::start_transaction`]
pub fn is_transaction_started(&self) -> bool {
return self.is_readonly;
self.is_readonly
}

/// Commits previously started db transaction. For more details on the
Expand Down
4 changes: 2 additions & 2 deletions grovedb/src/operations/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl GroveDb {
let mut merk = subtrees
.get_mut(&Self::compress_subtree_key(path, None))
.ok_or(Error::InvalidPath("no subtree found under that path"))?;
Element::delete(&mut merk, key.clone(), transaction)?;
Element::delete(merk, key.clone(), transaction)?;
}

if let Element::Tree(_) = element {
Expand Down Expand Up @@ -73,7 +73,7 @@ impl GroveDb {
transaction: Option<&OptimisticTransactionDBTransaction>,
) -> Result<Vec<Vec<Vec<u8>>>, Error> {
let mut queue: Vec<Vec<Vec<u8>>> = vec![path.clone()];
let mut result: Vec<Vec<Vec<u8>>> = vec![path.clone()];
let mut result: Vec<Vec<Vec<u8>>> = vec![path];

while let Some(q) = queue.pop() {
// TODO: eventually we need to do something about this nested slices
Expand Down
21 changes: 6 additions & 15 deletions grovedb/src/operations/get.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
use std::{
collections::{HashMap, HashSet},
ops::Range,
};
use std::collections::{HashMap, HashSet};

use merk::{
proofs::{query::QueryItem, Query},
Merk,
};
use storage::{
rocksdb_storage::{OptimisticTransactionDBTransaction, PrefixedRocksDbStorage},
RawIterator,
};
use merk::Merk;
use storage::rocksdb_storage::{OptimisticTransactionDBTransaction, PrefixedRocksDbStorage};

use crate::{subtree::raw_decode, Element, Error, GroveDb, PathQuery, SizedQuery};
use crate::{Element, Error, GroveDb, PathQuery};

/// Limit of possible indirections
pub(crate) const MAX_REFERENCE_HOPS: usize = 10;
Expand Down Expand Up @@ -83,7 +74,7 @@ impl GroveDb {
let merk = subtrees
.get(&Self::compress_subtree_key(path, None))
.ok_or(Error::InvalidPath("no subtree found under that path"))?;
Element::get(&merk, key)
Element::get(merk, key)
}

pub fn get_path_queries(
Expand All @@ -102,7 +93,7 @@ impl GroveDb {
Err(Error::InvalidQuery("the reference must result in an item"))
}
}
other => Err(Error::InvalidQuery("path_queries can only refer to references")),
_ => Err(Error::InvalidQuery("path_queries can only refer to references")),
}
}).collect::<Result<Vec<Vec<u8>>, Error>>()?;
Ok(results)
Expand Down
8 changes: 4 additions & 4 deletions grovedb/src/operations/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn create_merk_with_prefix(
path: &[&[u8]],
key: &[u8],
) -> Result<(Vec<u8>, Merk<PrefixedRocksDbStorage>), Error> {
let subtree_prefix = GroveDb::compress_subtree_key(&path, Some(&key));
let subtree_prefix = GroveDb::compress_subtree_key(path, Some(key));
Ok((
subtree_prefix.clone(),
Merk::open(PrefixedRocksDbStorage::new(db, subtree_prefix)?)
Expand Down Expand Up @@ -94,14 +94,14 @@ impl GroveDb {
};
// Open Merk and put handle into `subtrees` dictionary accessible by its
// compressed path
let (subtree_prefix, subtree_merk) = create_merk_with_prefix(self.db.clone(), &[], &key)?;
let (subtree_prefix, subtree_merk) = create_merk_with_prefix(self.db.clone(), &[], key)?;
subtrees.insert(subtree_prefix.clone(), subtree_merk);

// Update root leafs index to persist rs-merkle structure later
if root_leaf_keys.get(&subtree_prefix).is_none() {
root_leaf_keys.insert(subtree_prefix, root_tree.leaves_len());
}
self.propagate_changes(&[&key], transaction)?;
self.propagate_changes(&[key], transaction)?;
Ok(())
}

Expand Down Expand Up @@ -137,7 +137,7 @@ impl GroveDb {
.get_mut(&compressed_path)
.expect("merk object must exist in `subtrees`");
// need to mark key as taken in the upper tree
element.insert(&mut merk, key, transaction)?;
element.insert(merk, key, transaction)?;
self.propagate_changes(path, transaction)?;
Ok(())
}
Expand Down
12 changes: 6 additions & 6 deletions grovedb/src/operations/proof.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::HashMap;

use merk::proofs::query::{Map, QueryItem};
use merk::proofs::query::Map;
use rs_merkle::{algorithms::Sha256, MerkleProof};

use crate::{Element, Error, GroveDb, PathQuery, Proof, Query, SizedQuery};
use crate::{Element, Error, GroveDb, PathQuery, Proof, Query};

impl GroveDb {
pub fn proof(&mut self, proof_queries: Vec<PathQuery>) -> Result<Vec<u8>, Error> {
Expand Down Expand Up @@ -56,11 +56,11 @@ impl GroveDb {

// Construct the leaf proofs
for proof_query in proof_queries {
let mut path = proof_query.path;
let path = proof_query.path;

// If there is a subquery with a limit it's possible that we only need a reduced
// proof for this leaf.
let mut reduced_proof_query = proof_query;
let reduced_proof_query = proof_query;

// First we must get elements

Expand Down Expand Up @@ -164,14 +164,14 @@ impl GroveDb {
// and store hash + index for later root proof execution
let root_key = &path[0];
let (hash, proof_result_map) = GroveDb::execute_path(&path, &proof.proofs)?;
let compressed_root_key_path = GroveDb::compress_subtree_key(&[], Some(&root_key));
let compressed_root_key_path = GroveDb::compress_subtree_key(&[], Some(root_key));
let compressed_query_path = GroveDb::compress_subtree_key(&path, None);

let index = proof
.root_leaf_keys
.get(&compressed_root_key_path)
.ok_or(Error::InvalidPath("Bad path"))?;
if !root_keys_index.contains(&index) {
if !root_keys_index.contains(index) {
root_keys_index.push(*index);
root_hashes.push(hash);
}
Expand Down
35 changes: 13 additions & 22 deletions grovedb/src/subtree.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Module for subtrees handling.
//! Subtrees handling is isolated so basically this module is about adapting
//! Merk API to GroveDB needs.
use std::{
collections::HashMap,
ops::{Range, RangeFrom, RangeTo, RangeToInclusive},
};
use std::collections::HashMap;

use merk::{
proofs::{query::QueryItem, Query},
Expand Down Expand Up @@ -57,7 +54,7 @@ impl Element {
/// Merk should be loaded by this moment
pub fn get(merk: &Merk<PrefixedRocksDbStorage>, key: &[u8]) -> Result<Element, Error> {
let element = bincode::deserialize(
merk.get(&key)
merk.get(key)
.map_err(|e| Error::CorruptedData(e.to_string()))?
.ok_or(Error::InvalidPath("key not found in Merk"))?
.as_slice(),
Expand All @@ -71,7 +68,7 @@ impl Element {
query: &Query,
) -> Result<Vec<Element>, Error> {
let sized_query = SizedQuery::new(query.clone(), None, None, true);
let (elements, skipped) = Element::get_sized_query(merk, &sized_query)?;
let (elements, _) = Element::get_sized_query(merk, &sized_query)?;
Ok(elements)
}

Expand All @@ -92,10 +89,8 @@ impl Element {
if limit.is_some() {
*limit = Some(limit.unwrap() - 1);
}
} else {
if offset.is_some() {
*offset = Some(offset.unwrap() - 1);
}
} else if offset.is_some() {
*offset = Some(offset.unwrap() - 1);
}
Ok(())
}
Expand Down Expand Up @@ -140,10 +135,10 @@ impl Element {
let (mut sub_elements, skipped) =
Element::get_sized_query(inner_merk, &inner_query)?;
if let Some(limit) = limit {
*limit = *limit - sub_elements.len() as u16;
*limit -= sub_elements.len() as u16;
}
if let Some(offset) = offset {
*offset = *offset - skipped;
*offset -= skipped;
}
results.append(&mut sub_elements);
} else {
Expand All @@ -155,10 +150,8 @@ impl Element {
if limit.is_some() {
*limit = Some(limit.unwrap() - 1);
}
} else {
if offset.is_some() {
*offset = Some(offset.unwrap() - 1);
}
} else if offset.is_some() {
*offset = Some(offset.unwrap() - 1);
}
}
}
Expand All @@ -169,10 +162,8 @@ impl Element {
if limit.is_some() {
*limit = Some(limit.unwrap() - 1);
}
} else {
if offset.is_some() {
*offset = Some(offset.unwrap() - 1);
}
} else if offset.is_some() {
*offset = Some(offset.unwrap() - 1);
}
}
}
Expand Down Expand Up @@ -237,7 +228,7 @@ impl Element {
&mut results,
&mut limit,
&mut offset,
);
)?;
}
} else {
// this is a query on a range
Expand Down Expand Up @@ -265,7 +256,7 @@ impl Element {
&mut results,
&mut limit,
&mut offset,
);
)?;
if sized_query.left_to_right {
iter.next();
} else {
Expand Down
Loading

0 comments on commit 1563eaf

Please sign in to comment.