Skip to content

Commit

Permalink
Cleaup
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKickliter committed Nov 2, 2023
1 parent 2960403 commit f74630e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/disktree/dptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(crate) fn read<R>(src: &mut R) -> Result<u64>
where
R: Read + Seek,
{
let mut buf = [0_u8; 8];
let mut buf = [0u8; size_of::<u64>()];
src.read_exact(&mut buf[..DPTR_SZ])?;
let dptr = u64::from_le_bytes(buf);
Ok(dptr)
Expand Down
30 changes: 7 additions & 23 deletions src/disktree/iter.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
use crate::{
cell::CellStack,
disktree::{dptr, tree::HDR_SZ},
disktree::{dptr, tree::HDR_SZ, Deserializer},
error::Result,
Cell,
};
use byteorder::ReadBytesExt;
use std::io::{Read, Seek, SeekFrom};

pub trait Deserializer<R> {
/// This can be the serialized type or a result, depending on
/// fallability.
type T;
fn read(&self, rdr: Result<(Cell, &mut R)>) -> Self::T;
}

impl<R, T, F> Deserializer<R> for F
where
R: Read,
F: Fn(Result<(Cell, &mut R)>) -> T,
{
type T = T;
fn read(&self, rdr: Result<(Cell, &mut R)>) -> T {
self(rdr)
}
}

pub(crate) struct Iter<'a, R, F> {
cell_stack: CellStack,
curr: Option<(u8, u64)>,
Expand Down Expand Up @@ -88,13 +69,16 @@ where
}

fn node_buf(&mut self) -> Vec<(u8, u64)> {
let buf = self.recycle_bin.pop().unwrap_or_default();
assert!(buf.is_empty());
let buf = self
.recycle_bin
.pop()
.unwrap_or_else(|| Vec::with_capacity(7));
debug_assert!(buf.is_empty());
buf
}

fn recycle_buf(&mut self, buf: Vec<(u8, u64)>) {
assert!(buf.is_empty());
debug_assert!(buf.is_empty());
self.recycle_bin.push(buf);
}

Expand Down
8 changes: 8 additions & 0 deletions src/disktree/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//! An on-disk hextree.
pub use tree::DiskTree;
pub use value_read::Deserializer;

mod dptr;
mod iter;
mod tree;
mod value_read;
mod writer;

#[cfg(test)]
Expand Down Expand Up @@ -112,8 +114,14 @@ mod tests {

// Create the iterator with the user-defined deserialzer.
let disktree_iter = monaco_disktree.iter(deserialze_cell).unwrap();
let start = std::time::Instant::now();
let disktree_collection: Vec<_> = disktree_iter.collect::<Result<Vec<_>, _>>().unwrap();
let elapsed = start.elapsed();
println!("{elapsed:?}");
let start = std::time::Instant::now();
let hextree_collection: Vec<_> = monaco.iter().map(|(k, v)| (k, *v)).collect();
let elapsed = start.elapsed();
println!("{elapsed:?}");

assert_eq!(
hextree_collection,
Expand Down
3 changes: 2 additions & 1 deletion src/disktree/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::{
digits::Digits,
disktree::{
dptr::{self, DPTR_NULL, DPTR_SZ},
iter::{Deserializer, Iter},
iter::Iter,
Deserializer,
},
error::{Error, Result},
Cell,
Expand Down
33 changes: 33 additions & 0 deletions src/disktree/value_read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::{error::Result, Cell};
use std::io::Read;

/// The `Deserializer` trait defines the contract for reading concrete types
/// from a disk tree.
pub trait Deserializer<R> {
/// The associated type `T` represents the result of deserialization,
/// which may be the deserialized type or an error result, depending on fallibility.
type T;

/// Reads data from the provided reader and returns the deserialized result.
///
/// # Arguments
///
/// * `rdr` - A `Result` containing a `Cell` and a mutable reference to the reader.
///
/// # Returns
///
/// The deserialized result, which can be the deserialized type or an error.
fn read(&self, rdr: Result<(Cell, &mut R)>) -> Self::T;
}

impl<R, T, F> Deserializer<R> for F
where
R: Read,
F: Fn(Result<(Cell, &mut R)>) -> T,
{
type T = T;

fn read(&self, rdr: Result<(Cell, &mut R)>) -> T {
self(rdr)
}
}

0 comments on commit f74630e

Please sign in to comment.