chore: clippy

This commit is contained in:
nazeh
2023-12-21 21:33:45 +03:00
parent cb3d0194cd
commit 0c6587e51f
4 changed files with 35 additions and 53 deletions

View File

@@ -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<Node> {
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<Hash>,
right: Option<Hash>,
) -> 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<u8> {

View File

@@ -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<Hash> = None;
let mut unzip_right_root: Option<Hash> = 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

View File

@@ -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<u8>)]) {
verify_ranks(treap);
verify_entries(
treap,
entries
.iter()
.map(|(k, v)| (k.to_vec(), v.to_vec()))
.collect::<Vec<_>>(),
);
}
/// 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<Node>) -> bool {
}
}
/// Verify that the expected entries are both sorted and present in the treap.
fn verify_entries(treap: &HashTreap, entries: Vec<(Vec<u8>, Vec<u8>)>) {
let collected = treap
.iter()
.map(|n| (n.key().to_vec(), n.value().to_vec()))
.collect::<Vec<_>>();
let mut sorted = entries.iter().cloned().collect::<Vec<_>>();
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()

View File

@@ -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.