From 90cb1fefa57a90947bef026cab46d329d598ec05 Mon Sep 17 00:00:00 2001 From: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com> Date: Thu, 27 Jan 2022 16:28:55 +0700 Subject: [PATCH] refactor: fix more warnings, avoid duplicate code, fix typos (#38) * 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 --- grovedb/src/lib.rs | 13 +- grovedb/src/operations/delete.rs | 19 +-- grovedb/src/operations/get.rs | 14 +- grovedb/src/operations/insert.rs | 12 +- grovedb/src/operations/proof.rs | 12 +- grovedb/src/subtree.rs | 99 ++++++------ grovedb/src/tests.rs | 252 +++++++++++++++---------------- merk/src/proofs/chunk.rs | 2 +- merk/src/proofs/query/map.rs | 4 +- merk/src/proofs/query/mod.rs | 46 +++--- merk/src/test_utils/mod.rs | 4 +- merk/src/tree/walk/fetch.rs | 2 +- 12 files changed, 220 insertions(+), 259 deletions(-) diff --git a/grovedb/src/lib.rs b/grovedb/src/lib.rs index 4a4d7a35b..59c545262 100644 --- a/grovedb/src/lib.rs +++ b/grovedb/src/lib.rs @@ -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; @@ -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"; @@ -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 { diff --git a/grovedb/src/operations/delete.rs b/grovedb/src/operations/delete.rs index 6f80f8eee..788af53f5 100644 --- a/grovedb/src/operations/delete.rs +++ b/grovedb/src/operations/delete.rs @@ -9,10 +9,8 @@ impl GroveDb { key: Vec, 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 @@ -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); } } } diff --git a/grovedb/src/operations/get.rs b/grovedb/src/operations/get.rs index 7ae358a30..8b6e53e5b 100644 --- a/grovedb/src/operations/get.rs +++ b/grovedb/src/operations/get.rs @@ -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}; diff --git a/grovedb/src/operations/insert.rs b/grovedb/src/operations/insert.rs index 8f891ecf1..33964b236 100644 --- a/grovedb/src/operations/insert.rs +++ b/grovedb/src/operations/insert.rs @@ -72,10 +72,8 @@ impl GroveDb { key: &[u8], transaction: Option<&'b ::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 @@ -113,10 +111,8 @@ impl GroveDb { key: Vec, transaction: Option<&'b ::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 diff --git a/grovedb/src/operations/proof.rs b/grovedb/src/operations/proof.rs index 8c628cf54..b219dfc57 100644 --- a/grovedb/src/operations/proof.rs +++ b/grovedb/src/operations/proof.rs @@ -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(); @@ -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, +// compressed_path: &[u8], // path_query: PathQuery, // ) -> Result, Error> { // let merk = self @@ -135,7 +135,7 @@ // } // } // -// fn prove_item(&self, path: &Vec, query: Query) -> Result, +// fn prove_item(&self, path: &[u8], query: Query) -> Result, // Error> { let merk = self // .subtrees // .get(path) @@ -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), diff --git a/grovedb/src/subtree.rs b/grovedb/src/subtree.rs index b380cf7a7..7ddf3bdaa 100644 --- a/grovedb/src/subtree.rs +++ b/grovedb/src/subtree.rs @@ -1,7 +1,6 @@ //! 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::Sub}; use merk::{ proofs::{query::QueryItem, Query}, @@ -17,7 +16,7 @@ use storage::{ RawIterator, Storage, Store, }; -use crate::{Error, GroveDb, Merk, PathQuery, SizedQuery, Subtrees}; +use crate::{Error, Merk, PathQuery, SizedQuery, Subtrees}; /// Variants of GroveDB stored entities #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] @@ -190,14 +189,7 @@ impl Element { } } _ => { - if offset.is_none() || offset.is_some() && offset.unwrap() == 0 { - results.push(element); - if limit.is_some() { - *limit = Some(limit.unwrap() - 1); - } - } else if offset.is_some() { - *offset = Some(offset.unwrap() - 1); - } + Element::basic_push(subtrees_option, key, element, path, subquery_key_option, subquery, left_to_right, results, limit, offset)?; } } Ok(()) @@ -253,7 +245,6 @@ impl Element { } else { // this is a query on a range item.seek_for_iter(&mut iter, sized_query.query.left_to_right); - let mut work = true; while item.iter_is_valid_for_type(&iter, limit, sized_query.query.left_to_right) { let element = @@ -286,8 +277,8 @@ impl Element { break; } } - let skipped = if original_offset.is_some() { - original_offset.unwrap() - offset.unwrap() + let skipped = if let Some(original_offset_unwrapped) = original_offset { + original_offset_unwrapped - offset.unwrap() } else { 0 }; @@ -429,7 +420,7 @@ mod tests { query.insert_key(b"c".to_vec()); query.insert_key(b"a".to_vec()); assert_eq!( - Element::get_query(&mut merk, &query, None).expect("expected successful get_query"), + Element::get_query(&merk, &query, None).expect("expected successful get_query"), vec![ Element::Item(b"ayya".to_vec()), Element::Item(b"ayyc".to_vec()) @@ -441,7 +432,7 @@ mod tests { query.insert_range(b"b".to_vec()..b"d".to_vec()); query.insert_range(b"a".to_vec()..b"c".to_vec()); assert_eq!( - Element::get_query(&mut merk, &query, None).expect("expected successful get_query"), + Element::get_query(&merk, &query, None).expect("expected successful get_query"), vec![ Element::Item(b"ayya".to_vec()), Element::Item(b"ayyb".to_vec()), @@ -454,7 +445,7 @@ mod tests { query.insert_range_inclusive(b"b".to_vec()..=b"d".to_vec()); query.insert_range(b"b".to_vec()..b"c".to_vec()); assert_eq!( - Element::get_query(&mut merk, &query, None).expect("expected successful get_query"), + Element::get_query(&merk, &query, None).expect("expected successful get_query"), vec![ Element::Item(b"ayyb".to_vec()), Element::Item(b"ayyc".to_vec()), @@ -468,7 +459,7 @@ mod tests { query.insert_range(b"b".to_vec()..b"d".to_vec()); query.insert_range(b"a".to_vec()..b"c".to_vec()); assert_eq!( - Element::get_query(&mut merk, &query, None).expect("expected successful get_query"), + Element::get_query(&merk, &query, None).expect("expected successful get_query"), vec![ Element::Item(b"ayya".to_vec()), Element::Item(b"ayyb".to_vec()), @@ -498,7 +489,7 @@ mod tests { query.insert_range(b"a".to_vec()..b"d".to_vec()); let ascending_query = SizedQuery::new(query.clone(), None, None); - let (elements, skipped) = Element::get_sized_query(&mut merk, &ascending_query, None) + let (elements, skipped) = Element::get_sized_query(&merk, &ascending_query, None) .expect("expected successful get_query"); assert_eq!( elements, @@ -513,7 +504,7 @@ mod tests { query.left_to_right = false; let backwards_query = SizedQuery::new(query.clone(), None, None); - let (elements, skipped) = Element::get_sized_query(&mut merk, &backwards_query, None) + let (elements, skipped) = Element::get_sized_query(&merk, &backwards_query, None) .expect("expected successful get_query"); assert_eq!( elements, @@ -547,34 +538,37 @@ mod tests { query.insert_range_inclusive(b"a".to_vec()..=b"d".to_vec()); let ascending_query = SizedQuery::new(query.clone(), None, None); - let (elements, skipped) = Element::get_sized_query(&mut merk, &ascending_query, None) - .expect("expected successful get_query"); - assert_eq!( - elements, - vec![ + fn check_elements_no_skipped((elements, skipped): (Vec, u16), reverse: bool) { + let mut expected = vec![ Element::Item(b"ayya".to_vec()), Element::Item(b"ayyb".to_vec()), Element::Item(b"ayyc".to_vec()), Element::Item(b"ayyd".to_vec()), - ] + ]; + if reverse { + expected.reverse(); + } + assert_eq!( + elements, + expected + ); + assert_eq!(skipped, 0); + } + + check_elements_no_skipped( + Element::get_sized_query(&merk, &ascending_query, None) + .expect("expected successful get_query"), + false ); - assert_eq!(skipped, 0); query.left_to_right = false; let backwards_query = SizedQuery::new(query.clone(), None, None); - let (elements, skipped) = Element::get_sized_query(&mut merk, &backwards_query, None) - .expect("expected successful get_query"); - assert_eq!( - elements, - vec![ - Element::Item(b"ayyd".to_vec()), - Element::Item(b"ayyc".to_vec()), - Element::Item(b"ayyb".to_vec()), - Element::Item(b"ayya".to_vec()), - ] + check_elements_no_skipped( + Element::get_sized_query(&merk, &backwards_query, None) + .expect("expected successful get_query"), + true ); - assert_eq!(skipped, 0); // Test range inclusive query let mut query = Query::new_with_direction(false); @@ -582,18 +576,11 @@ mod tests { query.insert_range(b"a".to_vec()..b"c".to_vec()); let backwards_query = SizedQuery::new(query.clone(), None, None); - let (elements, skipped) = Element::get_sized_query(&mut merk, &backwards_query, None) - .expect("expected successful get_query"); - assert_eq!( - elements, - vec![ - Element::Item(b"ayyd".to_vec()), - Element::Item(b"ayyc".to_vec()), - Element::Item(b"ayyb".to_vec()), - Element::Item(b"ayya".to_vec()), - ] + check_elements_no_skipped( + Element::get_sized_query(&merk, &backwards_query, None) + .expect("expected successful get_query"), + true ); - assert_eq!(skipped, 0); } #[test] @@ -619,7 +606,7 @@ mod tests { // since these are just keys a backwards query will keep same order let backwards_query = SizedQuery::new(query.clone(), None, None); - let (elements, skipped) = Element::get_sized_query(&mut merk, &backwards_query, None) + let (elements, skipped) = Element::get_sized_query(&merk, &backwards_query, None) .expect("expected successful get_query"); assert_eq!( elements, @@ -632,7 +619,7 @@ mod tests { // The limit will mean we will only get back 1 item let limit_query = SizedQuery::new(query.clone(), Some(1), None); - let (elements, skipped) = Element::get_sized_query(&mut merk, &limit_query, None) + let (elements, skipped) = Element::get_sized_query(&merk, &limit_query, None) .expect("expected successful get_query"); assert_eq!(elements, vec![Element::Item(b"ayya".to_vec()),]); assert_eq!(skipped, 0); @@ -642,7 +629,7 @@ mod tests { query.insert_range(b"b".to_vec()..b"d".to_vec()); query.insert_range(b"a".to_vec()..b"c".to_vec()); let limit_query = SizedQuery::new(query.clone(), Some(2), None); - let (elements, skipped) = Element::get_sized_query(&mut merk, &limit_query, None) + let (elements, skipped) = Element::get_sized_query(&merk, &limit_query, None) .expect("expected successful get_query"); assert_eq!( elements, @@ -654,7 +641,7 @@ mod tests { assert_eq!(skipped, 0); let limit_offset_query = SizedQuery::new(query.clone(), Some(2), Some(1)); - let (elements, skipped) = Element::get_sized_query(&mut merk, &limit_offset_query, None) + let (elements, skipped) = Element::get_sized_query(&merk, &limit_offset_query, None) .expect("expected successful get_query"); assert_eq!( elements, @@ -672,7 +659,7 @@ mod tests { let limit_offset_backwards_query = SizedQuery::new(query.clone(), Some(2), Some(1)); let (elements, skipped) = - Element::get_sized_query(&mut merk, &limit_offset_backwards_query, None) + Element::get_sized_query(&merk, &limit_offset_backwards_query, None) .expect("expected successful get_query"); assert_eq!( elements, @@ -688,7 +675,7 @@ mod tests { query.insert_range_inclusive(b"b".to_vec()..=b"d".to_vec()); query.insert_range(b"b".to_vec()..b"c".to_vec()); let limit_full_query = SizedQuery::new(query.clone(), Some(5), Some(0)); - let (elements, skipped) = Element::get_sized_query(&mut merk, &limit_full_query, None) + let (elements, skipped) = Element::get_sized_query(&merk, &limit_full_query, None) .expect("expected successful get_query"); assert_eq!( elements, @@ -706,7 +693,7 @@ mod tests { let limit_offset_backwards_query = SizedQuery::new(query.clone(), Some(2), Some(1)); let (elements, skipped) = - Element::get_sized_query(&mut merk, &limit_offset_backwards_query, None) + Element::get_sized_query(&merk, &limit_offset_backwards_query, None) .expect("expected successful get_query"); assert_eq!( elements, @@ -723,7 +710,7 @@ mod tests { query.insert_range(b"b".to_vec()..b"d".to_vec()); query.insert_range(b"b".to_vec()..b"c".to_vec()); let limit_backwards_query = SizedQuery::new(query.clone(), Some(2), None); - let (elements, skipped) = Element::get_sized_query(&mut merk, &limit_backwards_query, None) + let (elements, skipped) = Element::get_sized_query(&merk, &limit_backwards_query, None) .expect("expected successful get_query"); assert_eq!( elements, diff --git a/grovedb/src/tests.rs b/grovedb/src/tests.rs index 6cafc48fe..019e863f0 100644 --- a/grovedb/src/tests.rs +++ b/grovedb/src/tests.rs @@ -63,7 +63,7 @@ fn test_insert_value_to_merk() { db.insert(&[TEST_LEAF], b"key".to_vec(), element.clone(), None) .expect("successful insert"); assert_eq!( - db.get(&[TEST_LEAF], b"key", None).expect("succesful get"), + db.get(&[TEST_LEAF], b"key", None).expect("successful get"), element ); } @@ -86,7 +86,7 @@ fn test_insert_value_to_subtree() { .expect("successful value insert"); assert_eq!( db.get(&[TEST_LEAF, b"key1"], b"key2", None) - .expect("succesful get"), + .expect("successful get"), element ); } @@ -117,7 +117,7 @@ fn test_changes_propagated() { .expect("successful value insert"); assert_eq!( db.get(&[TEST_LEAF, b"key1", b"key2"], b"key3", None) - .expect("succesful get"), + .expect("successful get"), element ); assert_ne!(old_hash, db.root_tree.root()); @@ -149,7 +149,7 @@ fn test_follow_references() { .expect("successful value insert"); assert_eq!( db.get(&[TEST_LEAF], b"reference_key", None) - .expect("succesful get"), + .expect("successful get"), element ); } @@ -213,7 +213,7 @@ fn test_too_many_indirections() { } #[test] -fn test_tree_structure_is_presistent() { +fn test_tree_structure_is_persistent() { let tmp_dir = TempDir::new("db").unwrap(); let element = Element::Item(b"ayy".to_vec()); // Create a scoped GroveDB @@ -241,7 +241,7 @@ fn test_tree_structure_is_presistent() { .expect("successful value insert"); assert_eq!( db.get(&[TEST_LEAF, b"key1", b"key2"], b"key3", None) - .expect("succesful get 1"), + .expect("successful get 1"), element ); } @@ -249,7 +249,7 @@ fn test_tree_structure_is_presistent() { let db = GroveDb::open(tmp_dir).unwrap(); assert_eq!( db.get(&[TEST_LEAF, b"key1", b"key2"], b"key3", None) - .expect("succesful get 2"), + .expect("successful get 2"), element ); assert!(db @@ -406,7 +406,7 @@ fn test_root_tree_leafs_are_noted() { // .proof(vec![ // PathQuery::new_unsized(&[TEST_LEAF, b"innertree"], // path_one_query), PathQuery::new_unsized(&[ANOTHER_TEST_LEAF, -// b"innertree3"], path_two_query), +// b"innertree3"], path_two_query), // PathQuery::new_unsized(&[ANOTHER_TEST_LEAF, b"innertree2"], // path_three_query), ]) // .unwrap(); @@ -731,15 +731,13 @@ fn test_insert_if_not_exists() { let mut db = make_grovedb(); // Insert twice at the same path - assert_eq!( + assert!( db.insert_if_not_exists(&[TEST_LEAF], b"key1".to_vec(), Element::empty_tree(), None) - .expect("Provided valid path"), - true + .expect("Provided valid path") ); - assert_eq!( - db.insert_if_not_exists(&[TEST_LEAF], b"key1".to_vec(), Element::empty_tree(), None) - .expect("Provided valid path"), - false + assert!( + !db.insert_if_not_exists(&[TEST_LEAF], b"key1".to_vec(), Element::empty_tree(), None) + .expect("Provided valid path") ); // Should propagate errors from insertion @@ -765,10 +763,9 @@ fn test_is_empty_tree() { ) .unwrap(); - assert_eq!( + assert!( db.is_empty_tree(&[TEST_LEAF, b"innertree"], None) - .expect("path is valid tree"), - true + .expect("path is valid tree") ); // add an element to the tree to make it non empty @@ -779,10 +776,9 @@ fn test_is_empty_tree() { None, ) .unwrap(); - assert_eq!( - db.is_empty_tree(&[TEST_LEAF, b"innertree"], None) - .expect("path is valid tree"), - false + assert!( + !db.is_empty_tree(&[TEST_LEAF, b"innertree"], None) + .expect("path is valid tree") ); } @@ -804,7 +800,7 @@ fn transaction_insert_item_with_transaction_should_use_transaction() { db.insert( &[TEST_LEAF], item_key.clone(), - element1.clone(), + element1, Some(&transaction), ) .expect("cannot insert an item into GroveDB"); @@ -884,7 +880,7 @@ fn transaction_insert_should_return_error_when_trying_to_insert_while_transactio db.commit_transaction(transaction).unwrap(); // Check that writes are unlocked after the transaction is committed - let result = db.insert(&[TEST_LEAF], item_key.clone(), element1.clone(), None); + let result = db.insert(&[TEST_LEAF], item_key, element1, None); assert!(matches!(result, Ok(()))); } @@ -903,7 +899,7 @@ fn transaction_should_be_aborted_when_rollback_is_called() { let result = db.insert( &[TEST_LEAF], item_key.clone(), - element1.clone(), + element1, Some(&transaction), ); @@ -911,7 +907,7 @@ fn transaction_should_be_aborted_when_rollback_is_called() { db.rollback_transaction(&transaction).unwrap(); - let result = db.get(&[TEST_LEAF], &item_key.clone(), Some(&transaction)); + let result = db.get(&[TEST_LEAF], &item_key, Some(&transaction)); assert!(matches!(result, Err(Error::InvalidPathKey(_)))); } @@ -948,7 +944,7 @@ fn transaction_should_be_aborted() { db.insert( &[TEST_LEAF], item_key.clone(), - element.clone(), + element, Some(&transaction), ) .unwrap(); @@ -966,8 +962,8 @@ fn transaction_should_be_aborted() { // Transaction should be closed assert!(!db.is_transaction_started()); - // Transactional data shouldn't be commited to the main database - let result = db.get(&[TEST_LEAF], &item_key.clone(), None); + // Transactional data shouldn't be committed to the main database + let result = db.get(&[TEST_LEAF], &item_key, None); assert!(matches!(result, Err(Error::InvalidPathKey(_)))); } @@ -1002,7 +998,7 @@ fn test_subtree_pairs_iterator() { .expect("successful value insert"); assert_eq!( db.get(&[TEST_LEAF, b"subtree1", b"subtree11"], b"key1", None) - .expect("succesful get 1"), + .expect("successful get 1"), element ); db.insert( @@ -1025,14 +1021,14 @@ fn test_subtree_pairs_iterator() { element.clone(), None, ) - .expect("succesful value insert"); + .expect("successful value insert"); db.insert( &[TEST_LEAF, b"subtree1"], b"key2".to_vec(), element2.clone(), None, ) - .expect("succesful value insert"); + .expect("successful value insert"); // Iterate over subtree1 to see if keys of other subtrees messed up // let mut iter = db @@ -1072,7 +1068,7 @@ fn test_compress_path_not_possible_collision() { fn test_element_deletion() { let mut db = make_grovedb(); let element = Element::Item(b"ayy".to_vec()); - db.insert(&[TEST_LEAF], b"key".to_vec(), element.clone(), None) + db.insert(&[TEST_LEAF], b"key".to_vec(), element, None) .expect("successful insert"); let root_hash = db.root_tree.root().unwrap(); assert!(db.delete(&[TEST_LEAF], b"key".to_vec(), None).is_ok()); @@ -1101,7 +1097,7 @@ fn test_find_subtrees() { db.insert( &[TEST_LEAF, b"key1", b"key2"], b"key3".to_vec(), - element.clone(), + element, None, ) .expect("successful value insert"); @@ -1222,7 +1218,7 @@ fn test_subtree_deletion() { db.insert( &[TEST_LEAF, b"key1", b"key2"], b"key3".to_vec(), - element.clone(), + element, None, ) .expect("successful value insert"); @@ -1370,7 +1366,7 @@ fn test_aux_with_transaction() { db.insert( &[TEST_LEAF], key.clone(), - element.clone(), + element, Some(&db_transaction), ) .expect("unable to insert"); @@ -1386,12 +1382,12 @@ fn test_aux_with_transaction() { db.get_aux(&key, None).expect("unable to get aux value"), None ); - // And should be able to get data when commited + // And should be able to get data when committed db.commit_transaction(db_transaction) .expect("unable to commit transaction"); assert_eq!( db.get_aux(&key, None) - .expect("unable to get commited aux value"), + .expect("unable to get committed aux value"), Some(aux_value) ); } @@ -1604,7 +1600,7 @@ fn test_get_range_query_with_non_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); - query.insert_range((1988 as u32).to_be_bytes().to_vec()..(1992 as u32).to_be_bytes().to_vec()); + query.insert_range(1988_u32.to_be_bytes().to_vec()..1992_u32.to_be_bytes().to_vec()); let subquery_key: Vec = b"\0".to_vec(); let mut subquery = Query::new(); @@ -1621,12 +1617,12 @@ fn test_get_range_query_with_non_unique_subquery() { assert_eq!(elements.len(), 200); - let mut first_value = (1988 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (100 as u32).to_be_bytes().to_vec()); + let mut first_value = 1988_u32.to_be_bytes().to_vec(); + first_value.append(&mut 100_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); - let mut last_value = (1991 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (149 as u32).to_be_bytes().to_vec()); + let mut last_value = 1991_u32.to_be_bytes().to_vec(); + last_value.append(&mut 149_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); } @@ -1637,7 +1633,7 @@ fn test_get_range_query_with_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); - query.insert_range((1988 as u32).to_be_bytes().to_vec()..(1992 as u32).to_be_bytes().to_vec()); + query.insert_range(1988_u32.to_be_bytes().to_vec()..1992_u32.to_be_bytes().to_vec()); let subquery_key: Vec = b"\0".to_vec(); @@ -1651,10 +1647,10 @@ fn test_get_range_query_with_unique_subquery() { assert_eq!(elements.len(), 4); - let first_value = (1988 as u32).to_be_bytes().to_vec(); + let first_value = 1988_u32.to_be_bytes().to_vec(); assert_eq!(elements[0], first_value); - let last_value = (1991 as u32).to_be_bytes().to_vec(); + let last_value = 1991_u32.to_be_bytes().to_vec(); assert_eq!(elements[elements.len() - 1], last_value); } @@ -1665,7 +1661,7 @@ fn test_get_range_query_with_unique_subquery_on_references() { let path = vec![TEST_LEAF.to_vec(), b"1".to_vec()]; let mut query = Query::new(); - query.insert_range((1988 as u32).to_be_bytes().to_vec()..(1992 as u32).to_be_bytes().to_vec()); + query.insert_range(1988_u32.to_be_bytes().to_vec()..1992_u32.to_be_bytes().to_vec()); let subquery_key: Vec = b"\0".to_vec(); @@ -1679,10 +1675,10 @@ fn test_get_range_query_with_unique_subquery_on_references() { assert_eq!(elements.len(), 4); - let first_value = (1988 as u32).to_be_bytes().to_vec(); + let first_value = 1988_u32.to_be_bytes().to_vec(); assert_eq!(elements[0], first_value); - let last_value = (1991 as u32).to_be_bytes().to_vec(); + let last_value = 1991_u32.to_be_bytes().to_vec(); assert_eq!(elements[elements.len() - 1], last_value); } @@ -1694,7 +1690,7 @@ fn test_get_range_inclusive_query_with_non_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); query.insert_range_inclusive( - (1988 as u32).to_be_bytes().to_vec()..=(1995 as u32).to_be_bytes().to_vec(), + 1988_u32.to_be_bytes().to_vec()..=1995_u32.to_be_bytes().to_vec(), ); let subquery_key: Vec = b"\0".to_vec(); @@ -1712,12 +1708,12 @@ fn test_get_range_inclusive_query_with_non_unique_subquery() { assert_eq!(elements.len(), 400); - let mut first_value = (1988 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (100 as u32).to_be_bytes().to_vec()); + let mut first_value = 1988_u32.to_be_bytes().to_vec(); + first_value.append(&mut 100_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); - let mut last_value = (1995 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (149 as u32).to_be_bytes().to_vec()); + let mut last_value = 1995_u32.to_be_bytes().to_vec(); + last_value.append(&mut 149_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); } @@ -1729,7 +1725,7 @@ fn test_get_range_inclusive_query_with_non_unique_subquery_on_references() { let path = vec![TEST_LEAF.to_vec(), b"1".to_vec()]; let mut query = Query::new(); query.insert_range_inclusive( - (1988 as u32).to_be_bytes().to_vec()..=(1995 as u32).to_be_bytes().to_vec(), + 1988_u32.to_be_bytes().to_vec()..=1995_u32.to_be_bytes().to_vec(), ); let subquery_key: Vec = b"\0".to_vec(); @@ -1747,12 +1743,12 @@ fn test_get_range_inclusive_query_with_non_unique_subquery_on_references() { assert_eq!(elements.len(), 400); - let mut first_value = (1988 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (100 as u32).to_be_bytes().to_vec()); + let mut first_value = 1988_u32.to_be_bytes().to_vec(); + first_value.append(&mut 100_u32.to_be_bytes().to_vec()); assert!(elements.contains(&first_value)); - let mut last_value = (1995 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (149 as u32).to_be_bytes().to_vec()); + let mut last_value = 1995_u32.to_be_bytes().to_vec(); + last_value.append(&mut 149_u32.to_be_bytes().to_vec()); assert!(elements.contains(&last_value)); } @@ -1764,7 +1760,7 @@ fn test_get_range_inclusive_query_with_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); query.insert_range_inclusive( - (1988 as u32).to_be_bytes().to_vec()..=(1995 as u32).to_be_bytes().to_vec(), + 1988_u32.to_be_bytes().to_vec()..=1995_u32.to_be_bytes().to_vec(), ); let subquery_key: Vec = b"\0".to_vec(); @@ -1779,10 +1775,10 @@ fn test_get_range_inclusive_query_with_unique_subquery() { assert_eq!(elements.len(), 8); - let first_value = (1988 as u32).to_be_bytes().to_vec(); + let first_value = 1988_u32.to_be_bytes().to_vec(); assert_eq!(elements[0], first_value); - let last_value = (1995 as u32).to_be_bytes().to_vec(); + let last_value = 1995_u32.to_be_bytes().to_vec(); assert_eq!(elements[elements.len() - 1], last_value); } @@ -1793,7 +1789,7 @@ fn test_get_range_from_query_with_non_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); - query.insert_range_from((1995 as u32).to_be_bytes().to_vec()..); + query.insert_range_from(1995_u32.to_be_bytes().to_vec()..); let subquery_key: Vec = b"\0".to_vec(); let mut subquery = Query::new(); @@ -1810,12 +1806,12 @@ fn test_get_range_from_query_with_non_unique_subquery() { assert_eq!(elements.len(), 250); - let mut first_value = (1995 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (100 as u32).to_be_bytes().to_vec()); + let mut first_value = 1995_u32.to_be_bytes().to_vec(); + first_value.append(&mut 100_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); - let mut last_value = (1999 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (149 as u32).to_be_bytes().to_vec()); + let mut last_value = 1999_u32.to_be_bytes().to_vec(); + last_value.append(&mut 149_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); } @@ -1826,7 +1822,7 @@ fn test_get_range_from_query_with_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); - query.insert_range_from((1995 as u32).to_be_bytes().to_vec()..); + query.insert_range_from(1995_u32.to_be_bytes().to_vec()..); let subquery_key: Vec = b"\0".to_vec(); @@ -1840,10 +1836,10 @@ fn test_get_range_from_query_with_unique_subquery() { assert_eq!(elements.len(), 5); - let first_value = (1995 as u32).to_be_bytes().to_vec(); + let first_value = 1995_u32.to_be_bytes().to_vec(); assert_eq!(elements[0], first_value); - let last_value = (1999 as u32).to_be_bytes().to_vec(); + let last_value = 1999_u32.to_be_bytes().to_vec(); assert_eq!(elements[elements.len() - 1], last_value); } @@ -1854,7 +1850,7 @@ fn test_get_range_to_query_with_non_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); - query.insert_range_to(..(1995 as u32).to_be_bytes().to_vec()); + query.insert_range_to(..1995_u32.to_be_bytes().to_vec()); let subquery_key: Vec = b"\0".to_vec(); let mut subquery = Query::new(); @@ -1871,12 +1867,12 @@ fn test_get_range_to_query_with_non_unique_subquery() { assert_eq!(elements.len(), 500); - let mut first_value = (1985 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (100 as u32).to_be_bytes().to_vec()); + let mut first_value = 1985_u32.to_be_bytes().to_vec(); + first_value.append(&mut 100_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); - let mut last_value = (1994 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (149 as u32).to_be_bytes().to_vec()); + let mut last_value = 1994_u32.to_be_bytes().to_vec(); + last_value.append(&mut 149_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); } @@ -1887,7 +1883,7 @@ fn test_get_range_to_query_with_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); - query.insert_range_to(..(1995 as u32).to_be_bytes().to_vec()); + query.insert_range_to(..1995_u32.to_be_bytes().to_vec()); let subquery_key: Vec = b"\0".to_vec(); @@ -1901,10 +1897,10 @@ fn test_get_range_to_query_with_unique_subquery() { assert_eq!(elements.len(), 10); - let first_value = (1985 as u32).to_be_bytes().to_vec(); + let first_value = 1985_u32.to_be_bytes().to_vec(); assert_eq!(elements[0], first_value); - let last_value = (1994 as u32).to_be_bytes().to_vec(); + let last_value = 1994_u32.to_be_bytes().to_vec(); assert_eq!(elements[elements.len() - 1], last_value); } @@ -1915,7 +1911,7 @@ fn test_get_range_to_inclusive_query_with_non_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); - query.insert_range_to_inclusive(..=(1995 as u32).to_be_bytes().to_vec()); + query.insert_range_to_inclusive(..=1995_u32.to_be_bytes().to_vec()); let subquery_key: Vec = b"\0".to_vec(); let mut subquery = Query::new(); @@ -1932,12 +1928,12 @@ fn test_get_range_to_inclusive_query_with_non_unique_subquery() { assert_eq!(elements.len(), 550); - let mut first_value = (1985 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (100 as u32).to_be_bytes().to_vec()); + let mut first_value = 1985_u32.to_be_bytes().to_vec(); + first_value.append(&mut 100_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); - let mut last_value = (1995 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (149 as u32).to_be_bytes().to_vec()); + let mut last_value = 1995_u32.to_be_bytes().to_vec(); + last_value.append(&mut 149_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); } @@ -1948,7 +1944,7 @@ fn test_get_range_to_inclusive_query_with_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); - query.insert_range_to_inclusive(..=(1995 as u32).to_be_bytes().to_vec()); + query.insert_range_to_inclusive(..=1995_u32.to_be_bytes().to_vec()); let subquery_key: Vec = b"\0".to_vec(); @@ -1962,10 +1958,10 @@ fn test_get_range_to_inclusive_query_with_unique_subquery() { assert_eq!(elements.len(), 11); - let first_value = (1985 as u32).to_be_bytes().to_vec(); + let first_value = 1985_u32.to_be_bytes().to_vec(); assert_eq!(elements[0], first_value); - let last_value = (1995 as u32).to_be_bytes().to_vec(); + let last_value = 1995_u32.to_be_bytes().to_vec(); assert_eq!(elements[elements.len() - 1], last_value); } @@ -1976,7 +1972,7 @@ fn test_get_range_after_query_with_non_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); - query.insert_range_after((1995 as u32).to_be_bytes().to_vec()..); + query.insert_range_after(1995_u32.to_be_bytes().to_vec()..); let subquery_key: Vec = b"\0".to_vec(); let mut subquery = Query::new(); @@ -1993,12 +1989,12 @@ fn test_get_range_after_query_with_non_unique_subquery() { assert_eq!(elements.len(), 200); - let mut first_value = (1996 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (100 as u32).to_be_bytes().to_vec()); + let mut first_value = 1996_u32.to_be_bytes().to_vec(); + first_value.append(&mut 100_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); - let mut last_value = (1999 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (149 as u32).to_be_bytes().to_vec()); + let mut last_value = 1999_u32.to_be_bytes().to_vec(); + last_value.append(&mut 149_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); } @@ -2010,7 +2006,7 @@ fn test_get_range_after_to_query_with_non_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); query.insert_range_after_to( - (1995 as u32).to_be_bytes().to_vec()..(1997 as u32).to_be_bytes().to_vec(), + 1995_u32.to_be_bytes().to_vec()..1997_u32.to_be_bytes().to_vec(), ); let subquery_key: Vec = b"\0".to_vec(); @@ -2028,12 +2024,12 @@ fn test_get_range_after_to_query_with_non_unique_subquery() { assert_eq!(elements.len(), 50); - let mut first_value = (1996 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (100 as u32).to_be_bytes().to_vec()); + let mut first_value = 1996_u32.to_be_bytes().to_vec(); + first_value.append(&mut 100_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); - let mut last_value = (1996 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (149 as u32).to_be_bytes().to_vec()); + let mut last_value = 1996_u32.to_be_bytes().to_vec(); + last_value.append(&mut 149_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); } @@ -2045,7 +2041,7 @@ fn test_get_range_after_to_inclusive_query_with_non_unique_subquery() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new(); query.insert_range_after_to_inclusive( - (1995 as u32).to_be_bytes().to_vec()..=(1997 as u32).to_be_bytes().to_vec(), + 1995_u32.to_be_bytes().to_vec()..=1997_u32.to_be_bytes().to_vec(), ); let subquery_key: Vec = b"\0".to_vec(); @@ -2063,12 +2059,12 @@ fn test_get_range_after_to_inclusive_query_with_non_unique_subquery() { assert_eq!(elements.len(), 100); - let mut first_value = (1996 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (100 as u32).to_be_bytes().to_vec()); + let mut first_value = 1996_u32.to_be_bytes().to_vec(); + first_value.append(&mut 100_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); - let mut last_value = (1997 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (149 as u32).to_be_bytes().to_vec()); + let mut last_value = 1997_u32.to_be_bytes().to_vec(); + last_value.append(&mut 149_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); } @@ -2101,16 +2097,16 @@ fn test_get_range_inclusive_query_with_double_non_unique_subquery() { let path_query = PathQuery::new_unsized(path, query.clone()); - let (elements, skipped) = db + let (elements, _) = db .get_path_query(&path_query, None) .expect("expected successful get_path_query"); assert_eq!(elements.len(), 60); - let mut first_value = (100 as u32).to_be_bytes().to_vec(); + let first_value = 100_u32.to_be_bytes().to_vec(); assert_eq!(elements[0], first_value); - let mut last_value = (109 as u32).to_be_bytes().to_vec(); + let last_value = 109_u32.to_be_bytes().to_vec(); assert_eq!(elements[elements.len() - 1], last_value); } @@ -2121,7 +2117,7 @@ fn test_get_range_query_with_limit_and_offset() { let path = vec![TEST_LEAF.to_vec()]; let mut query = Query::new_with_direction(true); - query.insert_range((1990 as u32).to_be_bytes().to_vec()..(1995 as u32).to_be_bytes().to_vec()); + query.insert_range(1990_u32.to_be_bytes().to_vec()..1995_u32.to_be_bytes().to_vec()); let subquery_key: Vec = b"\0".to_vec(); let mut subquery = Query::new(); @@ -2139,12 +2135,12 @@ fn test_get_range_query_with_limit_and_offset() { assert_eq!(elements.len(), 250); - let mut first_value = (1990 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (100 as u32).to_be_bytes().to_vec()); + let mut first_value = 1990_u32.to_be_bytes().to_vec(); + first_value.append(&mut 100_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); - let mut last_value = (1994 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (149 as u32).to_be_bytes().to_vec()); + let mut last_value = 1994_u32.to_be_bytes().to_vec(); + last_value.append(&mut 149_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); subquery.left_to_right = false; @@ -2163,12 +2159,12 @@ fn test_get_range_query_with_limit_and_offset() { assert_eq!(elements.len(), 250); - let mut first_value = (1994 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (149 as u32).to_be_bytes().to_vec()); + let mut first_value = 1994_u32.to_be_bytes().to_vec(); + first_value.append(&mut 149_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); - let mut last_value = (1990 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (100 as u32).to_be_bytes().to_vec()); + let mut last_value = 1990_u32.to_be_bytes().to_vec(); + last_value.append(&mut 100_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); subquery.left_to_right = true; @@ -2187,13 +2183,13 @@ fn test_get_range_query_with_limit_and_offset() { assert_eq!(elements.len(), 55); - let mut first_value = (1990 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (100 as u32).to_be_bytes().to_vec()); + let mut first_value = 1990_u32.to_be_bytes().to_vec(); + first_value.append(&mut 100_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); // Second tree 5 element [100, 101, 102, 103, 104] - let mut last_value = (1991 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (104 as u32).to_be_bytes().to_vec()); + let mut last_value = 1991_u32.to_be_bytes().to_vec(); + last_value.append(&mut 104_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); query.set_subquery_key(subquery_key.clone()); @@ -2213,15 +2209,15 @@ fn test_get_range_query_with_limit_and_offset() { // Skips the first 14 elements, starts from the 15th // i.e skips [100 - 113] starts from 114 - let mut first_value = (1990 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (114 as u32).to_be_bytes().to_vec()); + let mut first_value = 1990_u32.to_be_bytes().to_vec(); + first_value.append(&mut 114_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); // Continues for 60 iterations // Takes 36 elements from the first tree (50 - 14) // takes the remaining 24 from the second three (60 - 36) - let mut last_value = (1991 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (123 as u32).to_be_bytes().to_vec()); + let mut last_value = 1991_u32.to_be_bytes().to_vec(); + last_value.append(&mut 123_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); query.set_subquery_key(subquery_key.clone()); @@ -2245,12 +2241,12 @@ fn test_get_range_query_with_limit_and_offset() { // Skips the first 10 elements from the back // last tree and starts from the 11th before the end // Underlying subquery is ascending - let mut first_value = (1994 as u32).to_be_bytes().to_vec(); - first_value.append(&mut (110 as u32).to_be_bytes().to_vec()); + let mut first_value = 1994_u32.to_be_bytes().to_vec(); + first_value.append(&mut 110_u32.to_be_bytes().to_vec()); assert_eq!(elements[0], first_value); - let mut last_value = (1993 as u32).to_be_bytes().to_vec(); - last_value.append(&mut (119 as u32).to_be_bytes().to_vec()); + let mut last_value = 1993_u32.to_be_bytes().to_vec(); + last_value.append(&mut 119_u32.to_be_bytes().to_vec()); assert_eq!(elements[elements.len() - 1], last_value); query.set_subquery_key(subquery_key.clone()); @@ -2290,7 +2286,7 @@ fn test_get_range_query_with_limit_and_offset() { populate_tree_for_unique_range_subquery(&mut db); let mut query = Query::new_with_direction(true); - query.insert_range((1990 as u32).to_be_bytes().to_vec()..(2000 as u32).to_be_bytes().to_vec()); + query.insert_range(1990_u32.to_be_bytes().to_vec()..2000_u32.to_be_bytes().to_vec()); query.set_subquery_key(subquery_key.clone()); @@ -2302,10 +2298,10 @@ fn test_get_range_query_with_limit_and_offset() { assert_eq!(elements.len(), 5); - let first_value = (1992 as u32).to_be_bytes().to_vec(); + let first_value = 1992_u32.to_be_bytes().to_vec(); assert_eq!(elements[0], first_value); - let last_value = (1996 as u32).to_be_bytes().to_vec(); + let last_value = 1996_u32.to_be_bytes().to_vec(); assert_eq!(elements[elements.len() - 1], last_value); } diff --git a/merk/src/proofs/chunk.rs b/merk/src/proofs/chunk.rs index cbaaf23f4..86301e5ab 100644 --- a/merk/src/proofs/chunk.rs +++ b/merk/src/proofs/chunk.rs @@ -24,7 +24,7 @@ where /// Returns a tuple containing the produced proof, and a boolean indicating /// whether or not there will be more chunks to follow. If the chunk /// contains the entire tree, the boolean will be `false`, if the chunk - /// is abdriged and will be connected to leaf chunks, it will be `true`. + /// is abridged and will be connected to leaf chunks, it will be `true`. pub fn create_trunk_proof(&mut self) -> Result<(Vec, bool)> { let approx_size = 2usize.pow((self.tree().height() / 2) as u32) * 3; let mut proof = Vec::with_capacity(approx_size); diff --git a/merk/src/proofs/query/map.rs b/merk/src/proofs/query/map.rs index 9bfebcabb..827988a8b 100644 --- a/merk/src/proofs/query/map.rs +++ b/merk/src/proofs/query/map.rs @@ -20,7 +20,7 @@ impl MapBuilder { }) } - /// Adds the node's data to the uncerlying `Map` (if node is type `KV`), or + /// Adds the node's data to the underlying `Map` (if node is type `KV`), or /// makes a note of non-contiguous data (if node is type `KVHash` or /// `Hash`). pub fn insert(&mut self, node: &Node) -> Result<()> { @@ -198,7 +198,7 @@ impl<'a> Iterator for Range<'a> { // if nodes weren't contiguous, we cannot verify that we have all values // in the desired range - if !skip_exclusion_check && !contiguous { + if !skip_exclusion_check && !*contiguous { return Some(Err(anyhow!("Proof is missing data for query"))); } diff --git a/merk/src/proofs/query/mod.rs b/merk/src/proofs/query/mod.rs index ac87c1580..822df121a 100644 --- a/merk/src/proofs/query/mod.rs +++ b/merk/src/proofs/query/mod.rs @@ -243,10 +243,10 @@ impl QueryItem { QueryItem::Key(key) => (key.as_slice(), false), QueryItem::Range(range) => (range.start.as_ref(), false), QueryItem::RangeInclusive(range) => (range.start().as_ref(), false), - QueryItem::RangeFull(range) => (b"", false), + QueryItem::RangeFull(_) => (b"", false), QueryItem::RangeFrom(range) => (range.start.as_ref(), false), - QueryItem::RangeTo(range) => (b"", false), - QueryItem::RangeToInclusive(range) => (b"", false), + QueryItem::RangeTo(_) => (b"", false), + QueryItem::RangeToInclusive(_) => (b"", false), QueryItem::RangeAfter(range) => (range.start.as_ref(), true), QueryItem::RangeAfterTo(range) => (range.start.as_ref(), true), QueryItem::RangeAfterToInclusive(range) => (range.start().as_ref(), true), @@ -301,12 +301,12 @@ impl QueryItem { pub fn contains(&self, key: &[u8]) -> bool { let (lower_bound, lower_bound_non_inclusive) = self.lower_bound(); let (upper_bound, upper_bound_inclusive) = self.upper_bound(); - return (self.lower_unbounded() + (self.lower_unbounded() || key > lower_bound || (key == lower_bound && !lower_bound_non_inclusive)) && (self.upper_unbounded() || key < upper_bound - || (key == upper_bound && upper_bound_inclusive)); + || (key == upper_bound && upper_bound_inclusive)) } fn merge(self, other: QueryItem) -> QueryItem { @@ -318,32 +318,32 @@ impl QueryItem { let (end, end_inclusive) = max(self.upper_bound(), other.upper_bound()); if start_non_inclusive { - if upper_unbounded { - return QueryItem::RangeAfter(RangeFrom { + return if upper_unbounded { + QueryItem::RangeAfter(RangeFrom { start: start.to_vec(), - }); + }) } else if end_inclusive { - return QueryItem::RangeAfterToInclusive(RangeInclusive::new( + QueryItem::RangeAfterToInclusive(RangeInclusive::new( start.to_vec(), end.to_vec(), - )); + )) } else { // upper is bounded and not inclusive - return QueryItem::RangeAfterTo(Range { + QueryItem::RangeAfterTo(Range { start: start.to_vec(), end: end.to_vec(), - }); + }) } } if lower_unbounded { - if upper_unbounded { - return QueryItem::RangeFull(RangeFull); + return if upper_unbounded { + QueryItem::RangeFull(RangeFull) } else if end_inclusive { - return QueryItem::RangeToInclusive(RangeToInclusive { end: end.to_vec() }); + QueryItem::RangeToInclusive(RangeToInclusive { end: end.to_vec() }) } else { // upper is bounded and not inclusive - return QueryItem::RangeTo(RangeTo { end: end.to_vec() }); + QueryItem::RangeTo(RangeTo { end: end.to_vec() }) } } @@ -1503,22 +1503,20 @@ mod test { #[test] fn query_item_cmp() { assert!(QueryItem::Key(vec![10]) < QueryItem::Key(vec![20])); - assert!(QueryItem::Key(vec![10]) == QueryItem::Key(vec![10])); + assert_eq!(QueryItem::Key(vec![10]), QueryItem::Key(vec![10])); assert!(QueryItem::Key(vec![20]) > QueryItem::Key(vec![10])); assert!(QueryItem::Key(vec![10]) < QueryItem::Range(vec![20]..vec![30])); - assert!(QueryItem::Key(vec![10]) == QueryItem::Range(vec![10]..vec![20])); - assert!(QueryItem::Key(vec![15]) == QueryItem::Range(vec![10]..vec![20])); + assert_eq!(QueryItem::Key(vec![10]), QueryItem::Range(vec![10]..vec![20])); + assert_eq!(QueryItem::Key(vec![15]), QueryItem::Range(vec![10]..vec![20])); assert!(QueryItem::Key(vec![20]) > QueryItem::Range(vec![10]..vec![20])); - assert!(QueryItem::Key(vec![20]) == QueryItem::RangeInclusive(vec![10]..=vec![20])); + assert_eq!(QueryItem::Key(vec![20]), QueryItem::RangeInclusive(vec![10]..=vec![20])); assert!(QueryItem::Key(vec![30]) > QueryItem::Range(vec![10]..vec![20])); assert!(QueryItem::Range(vec![10]..vec![20]) < QueryItem::Range(vec![30]..vec![40])); assert!(QueryItem::Range(vec![10]..vec![20]) < QueryItem::Range(vec![20]..vec![30])); - assert!( - QueryItem::RangeInclusive(vec![10]..=vec![20]) == QueryItem::Range(vec![20]..vec![30]) - ); - assert!(QueryItem::Range(vec![15]..vec![25]) == QueryItem::Range(vec![20]..vec![30])); + assert_eq!(QueryItem::RangeInclusive(vec![10]..=vec![20]), QueryItem::Range(vec![20]..vec![30])); + assert_eq!(QueryItem::Range(vec![15]..vec![25]), QueryItem::Range(vec![20]..vec![30])); assert!(QueryItem::Range(vec![20]..vec![30]) > QueryItem::Range(vec![10]..vec![20])); } diff --git a/merk/src/test_utils/mod.rs b/merk/src/test_utils/mod.rs index 7c0fca3b4..3015c1964 100644 --- a/merk/src/test_utils/mod.rs +++ b/merk/src/test_utils/mod.rs @@ -117,7 +117,7 @@ pub fn make_del_batch_rand(size: u64, seed: u64) -> Vec { pub fn make_tree_rand(node_count: u64, batch_size: u64, initial_seed: u64) -> Tree { assert!(node_count >= batch_size); - assert!((node_count % batch_size) == 0); + assert_eq!((node_count % batch_size), 0); let value = vec![123; 60]; let mut tree = Tree::new(vec![0; 20], value); @@ -136,7 +136,7 @@ pub fn make_tree_rand(node_count: u64, batch_size: u64, initial_seed: u64) -> Tr pub fn make_tree_seq(node_count: u64) -> Tree { let batch_size = if node_count >= 10_000 { - assert!(node_count % 10_000 == 0); + assert_eq!(node_count % 10_000, 0); 10_000 } else { node_count diff --git a/merk/src/tree/walk/fetch.rs b/merk/src/tree/walk/fetch.rs index 505b9a90d..401d788ab 100644 --- a/merk/src/tree/walk/fetch.rs +++ b/merk/src/tree/walk/fetch.rs @@ -3,7 +3,7 @@ use anyhow::Result; use super::super::{Link, Tree}; /// A source of data to be used by the tree when encountering a pruned node. -/// This typcially means fetching the tree node from a backing store by its key, +/// This typically means fetching the tree node from a backing store by its key, /// but could also implement an in-memory cache for example. pub trait Fetch { /// Called when the tree needs to fetch a node with the given `Link`. The