diff --git a/mast/src/node.rs b/mast/src/node.rs index 3b5716c..00faa38 100644 --- a/mast/src/node.rs +++ b/mast/src/node.rs @@ -6,7 +6,6 @@ use crate::{Hash, Hasher, HASH_LEN}; // TODO: Are we creating too many hashers? // TODO: are we calculating the rank and hash too often? -// TODO: remove unused // TODO: remove unwrap #[derive(Debug, Clone, PartialEq)] @@ -41,7 +40,7 @@ impl Node { table: &'_ impl ReadableTable<&'static [u8], (u64, &'static [u8])>, hash: Hash, ) -> Option { - let mut existing = table.get(hash.as_bytes().as_slice()).unwrap(); + let existing = table.get(hash.as_bytes().as_slice()).unwrap(); existing.map(|existing| { let (ref_count, bytes) = { @@ -61,7 +60,7 @@ impl Node { left: Option, right: Option, ) -> Hash { - let mut node = Self { + let node = Self { key: key.into(), value: value.into(), left, @@ -73,10 +72,12 @@ impl Node { let encoded = node.canonical_encode(); let hash = hash(&encoded); - table.insert( - hash.as_bytes().as_slice(), - (node.ref_count, encoded.as_slice()), - ); + table + .insert( + hash.as_bytes().as_slice(), + (node.ref_count, encoded.as_slice()), + ) + .unwrap(); hash } @@ -158,10 +159,12 @@ impl Node { let encoded = self.canonical_encode(); let hash = hash(&encoded); - table.insert( - hash.as_bytes().as_slice(), - (self.ref_count, encoded.as_slice()), - ); + table + .insert( + hash.as_bytes().as_slice(), + (self.ref_count, encoded.as_slice()), + ) + .unwrap(); hash } @@ -170,10 +173,12 @@ impl Node { let encoded = self.canonical_encode(); let hash = hash(&encoded); - table.insert( - hash.as_bytes().as_slice(), - (self.ref_count, encoded.as_slice()), - ); + table + .insert( + hash.as_bytes().as_slice(), + (self.ref_count, encoded.as_slice()), + ) + .unwrap(); hash } @@ -200,7 +205,8 @@ impl Node { match ref_count { 0 => table.remove(hash.as_bytes().as_slice()), _ => table.insert(hash.as_bytes().as_slice(), (ref_count, bytes.as_slice())), - }; + } + .unwrap(); } fn canonical_encode(&self) -> Vec { diff --git a/mast/src/operations/insert.rs b/mast/src/operations/insert.rs index ddfee3f..67ff10c 100644 --- a/mast/src/operations/insert.rs +++ b/mast/src/operations/insert.rs @@ -1,10 +1,8 @@ use std::cmp::Ordering; use crate::node::{rank, Branch, Node}; -use crate::treap::{HashTreap, NODES_TABLE, ROOTS_TABLE}; -use crate::HASH_LEN; use blake3::Hash; -use redb::{Database, ReadTransaction, ReadableTable, Table, TableDefinition, WriteTransaction}; +use redb::Table; // Watch this [video](https://youtu.be/NxRXhBur6Xs?si=GNwaUOfuGwr_tBKI&t=1763) for a good explanation of the unzipping algorithm. // Also see the Iterative insertion algorithm in the page 12 of the [original paper](https://arxiv.org/pdf/1806.06726.pdf). @@ -90,7 +88,6 @@ pub fn insert( value: &[u8], ) -> Hash { let mut path = binary_search_path(table, root, key); - dbg!(&path); let mut unzip_left_root: Option = None; let mut unzip_right_root: Option = None; @@ -157,7 +154,7 @@ fn binary_search_path( // as lower nodes shouldn't change then. current_node.decrement_ref_count(table); - let mut path = if current_node.rank().as_bytes() > rank.as_bytes() { + let path = if current_node.rank().as_bytes() > rank.as_bytes() { &mut result.upper_path } else { &mut result.unzip_path diff --git a/mast/src/test.rs b/mast/src/test.rs index f841277..64adae3 100644 --- a/mast/src/test.rs +++ b/mast/src/test.rs @@ -1,9 +1,9 @@ use crate::node::Node; -use crate::treap::{HashTreap, NODES_TABLE}; +use crate::treap::HashTreap; use crate::Hash; use redb::backends::InMemoryBackend; -use redb::{Database, Error, ReadableTable, TableDefinition}; +use redb::Database; #[test] fn cases() { @@ -32,7 +32,7 @@ fn cases() { let upsert_at_root = ["X", "X"] .iter() .enumerate() - .map(|(i, key)| { + .map(|(i, _)| { ( Entry { key: b"X".to_vec(), @@ -201,12 +201,14 @@ fn test(name: &str, input: &[(Entry, Operation)], expected: &[Entry], root_hash: let mut sorted = expected.to_vec(); sorted.sort_by(|a, b| a.key.cmp(&b.key)); - dbg!(&treap.root_hash()); - dbg!(&input, &expected); - println!("{}", into_mermaid_graph(&treap)); + // println!("{}", into_mermaid_graph(&treap)); if root_hash.is_some() { assert_root(&treap, root_hash.unwrap()); + } else { + dbg!(&treap.root_hash()); + + verify_ranks(&treap); } assert_eq!( @@ -217,18 +219,6 @@ fn test(name: &str, input: &[(Entry, Operation)], expected: &[Entry], root_hash: ); } -/// Verify ranks, and keys order -fn verify(treap: &HashTreap, entries: &[(&[u8], Vec)]) { - verify_ranks(treap); - verify_entries( - treap, - entries - .iter() - .map(|(k, v)| (k.to_vec(), v.to_vec())) - .collect::>(), - ); -} - /// Verify that every node has higher rank than its children. fn verify_ranks(treap: &HashTreap) { assert!( @@ -255,19 +245,6 @@ fn verify_children_rank(treap: &HashTreap, node: Option) -> bool { } } -/// Verify that the expected entries are both sorted and present in the treap. -fn verify_entries(treap: &HashTreap, entries: Vec<(Vec, Vec)>) { - let collected = treap - .iter() - .map(|n| (n.key().to_vec(), n.value().to_vec())) - .collect::>(); - - let mut sorted = entries.iter().cloned().collect::>(); - sorted.sort_by(|a, b| a.0.cmp(&b.0)); - - assert_eq!(collected, sorted, "Entries do not match"); -} - fn assert_root(treap: &HashTreap, expected_root_hash: &str) { let root_hash = treap .root() diff --git a/mast/src/treap.rs b/mast/src/treap.rs index 4e2ef0e..917e6d0 100644 --- a/mast/src/treap.rs +++ b/mast/src/treap.rs @@ -69,7 +69,9 @@ impl<'treap> HashTreap<'treap> { let new_root = crate::operations::insert::insert(&mut nodes_table, root, key, value); - roots_table.insert(self.name.as_bytes(), new_root.as_bytes().as_slice()); + roots_table + .insert(self.name.as_bytes(), new_root.as_bytes().as_slice()) + .unwrap(); }; // Finally commit the changes to the storage.