Skip to content

Commit

Permalink
refactor: fix more warnings, avoid duplicate code, fix typos (#38)
Browse files Browse the repository at this point in the history
* avoid duplicate code

* use is_none

* use a slice instead of reference to vec

* collapse ifs

* collapse if

* avoid an unneeded unwrap

* avoid unneeded return

* lift return out of if

* use assert_eq! instead of assert!

* use _ instead of an unused variable

* dereference &bool before evaluating

* fix typos

* use if let Element::Tree(_) = value

* avoid mutable references when not needed

* remove redundant clones

* don't use assert_eq! with bool literals

* use _u32 insteadof a cast to u32 for literals

* fix accidentally changed value

* more _u32

* unneeded mut

* return unreturned error

* use is_some instead of let some

* remove unused bool

* use _ for unused

* comment out unused value

* fix unused includes
  • Loading branch information
PastaPastaPasta authored Jan 27, 2022
1 parent 6711fbc commit 90cb1fe
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 259 deletions.
13 changes: 5 additions & 8 deletions grovedb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ use std::{cell::RefCell, collections::HashMap, path::Path, rc::Rc};
use blake3;
pub use merk::proofs::{query::QueryItem, Query};
use merk::{self, Merk};
use rs_merkle::{algorithms::Sha256, Hasher, MerkleTree};
use rs_merkle::{algorithms::Sha256, MerkleTree};
use serde::{Deserialize, Serialize};
pub use storage::{rocksdb_storage::PrefixedRocksDbStorage, Storage};
use storage::{
rocksdb_storage::{OptimisticTransactionDBTransaction, PrefixedRocksDbStorageError},
Transaction,
};
use storage::rocksdb_storage::{OptimisticTransactionDBTransaction, PrefixedRocksDbStorageError};
pub use subtree::Element;
use subtrees::Subtrees;

Expand All @@ -25,7 +22,7 @@ use subtrees::Subtrees;

/// A key to store serialized data about subtree prefixes to restore HADS
/// structure
const SUBTREES_SERIALIZED_KEY: &[u8] = b"subtreesSerialized";
// const SUBTREES_SERIALIZED_KEY: &[u8] = b"subtreesSerialized";
/// A key to store serialized data about root tree leafs keys and order
const ROOT_LEAFS_SERIALIZED_KEY: &[u8] = b"rootLeafsSerialized";

Expand Down Expand Up @@ -266,8 +263,8 @@ impl GroveDb {

let (key, parent_path) = path.split_last().ok_or(Error::InvalidPath("empty path"))?;
let (mut upper_tree, prefix) = subtrees.get(parent_path, transaction)?;
element.insert(&mut upper_tree, key.to_vec(), transaction);
if let Some(prefix) = prefix {
element.insert(&mut upper_tree, key.to_vec(), transaction)?;
if prefix.is_some() {
self.get_subtrees()
.insert_temp_tree(parent_path, upper_tree, transaction);
} else {
Expand Down
19 changes: 7 additions & 12 deletions grovedb/src/operations/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ impl GroveDb {
key: Vec<u8>,
transaction: Option<&OptimisticTransactionDBTransaction>,
) -> Result<(), Error> {
if let None = transaction {
if self.is_readonly {
return Err(Error::DbIsInReadonlyMode);
}
if transaction.is_none() && self.is_readonly {
return Err(Error::DbIsInReadonlyMode);
}
if path.is_empty() {
// Attempt to delete a root tree leaf
Expand Down Expand Up @@ -78,14 +76,11 @@ impl GroveDb {
let mut iter = Element::iterator(merk.raw_iter());
// let mut iter = self.elements_iterator(&q_ref, transaction)?;
while let Some((key, value)) = iter.next()? {
match value {
Element::Tree(_) => {
let mut sub_path = q.clone();
sub_path.push(key);
queue.push(sub_path.clone());
result.push(sub_path);
}
_ => {}
if let Element::Tree(_) = value {
let mut sub_path = q.clone();
sub_path.push(key);
queue.push(sub_path.clone());
result.push(sub_path);
}
}
}
Expand Down
14 changes: 3 additions & 11 deletions grovedb/src/operations/get.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
use std::{
collections::{HashMap, HashSet},
ops::Range,
rc::Rc,
};

use merk::Merk;
use storage::{
rocksdb_storage::{OptimisticTransactionDBTransaction, PrefixedRocksDbStorage},
RawIterator,
};
use std::collections::HashSet;

use storage::rocksdb_storage::OptimisticTransactionDBTransaction;

use crate::{Element, Error, GroveDb, PathQuery, Subtrees};

Expand Down
12 changes: 4 additions & 8 deletions grovedb/src/operations/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ impl GroveDb {
key: &[u8],
transaction: Option<&'b <PrefixedRocksDbStorage as Storage>::DBTransaction<'b>>,
) -> Result<(), Error> {
if let None = transaction {
if self.is_readonly {
return Err(Error::DbIsInReadonlyMode);
}
if transaction.is_none() && self.is_readonly {
return Err(Error::DbIsInReadonlyMode);
}

// Open Merk and put handle into `subtrees` dictionary accessible by its
Expand Down Expand Up @@ -113,10 +111,8 @@ impl GroveDb {
key: Vec<u8>,
transaction: Option<&'b <PrefixedRocksDbStorage as Storage>::DBTransaction<'b>>,
) -> Result<(), Error> {
if let None = transaction {
if self.is_readonly {
return Err(Error::DbIsInReadonlyMode);
}
if transaction.is_none() && self.is_readonly {
return Err(Error::DbIsInReadonlyMode);
}

// First, check if a subtree exists to create a new subtree under it
Expand Down
12 changes: 6 additions & 6 deletions grovedb/src/operations/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
// // First we must get elements
//
// if let Some(subquery_key) =
// reduced_proof_query.query.query.subquery_key.clone() {
// reduced_proof_query.query.query.subquery_key.clone() {
// self.get_path_queries_raw(&[&reduced_proof_query], None)?;
//
// let mut path_vec = path.to_vec();
Expand Down Expand Up @@ -98,16 +98,16 @@
// root_leaf_keys: self.root_leaf_keys.clone(),
// };
//
// let seralized_proof = bincode::serialize(&proof)
// let serialized_proof = bincode::serialize(&proof)
// .map_err(|_| Error::CorruptedData(String::from("unable to
// serialize proof")))?;
//
// Ok(seralized_proof)
// Ok(serialized_proof)
// }
//
// fn prove_path_item(
// &self,
// compressed_path: &Vec<u8>,
// compressed_path: &[u8],
// path_query: PathQuery,
// ) -> Result<Vec<u8>, Error> {
// let merk = self
Expand Down Expand Up @@ -135,7 +135,7 @@
// }
// }
//
// fn prove_item(&self, path: &Vec<u8>, query: Query) -> Result<Vec<u8>,
// fn prove_item(&self, path: &[u8], query: Query) -> Result<Vec<u8>,
// Error> { let merk = self
// .subtrees
// .get(path)
Expand Down Expand Up @@ -235,7 +235,7 @@
// let result_map = proof_result.1;
// // TODO: Handle the error better here
// let elem: Element =
//
//
// bincode::deserialize(result_map.get(key).unwrap().unwrap()).unwrap();
// let merk_root_hash = match elem {
// Element::Tree(hash) => Ok(hash),
Expand Down
Loading

0 comments on commit 90cb1fe

Please sign in to comment.