Skip to content

Commit

Permalink
DiskTree → HexDb
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKickliter committed Sep 30, 2024
1 parent bc331f1 commit c99b3d9
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 104 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ all-features = true

[features]
default = []
disktree = [
hexdb = [
"byteorder",
"memmap",
"serde",
Expand Down
12 changes: 6 additions & 6 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ fn set_lookup(c: &mut Criterion) {
}
}

#[cfg(not(feature = "disktree"))]
#[cfg(not(feature = "hexdb"))]
fn disk_set_lookup(_c: &mut Criterion) {}

#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
fn disk_set_lookup(c: &mut Criterion) {
use hextree::disktree::DiskTreeMap;
let mut group = c.benchmark_group("US915 DiskTreeSet lookup");
use hextree::hexdb::HexDb;
let mut group = c.benchmark_group("US915 HexDbSet lookup");

let us915_disk_set = {
let us915_set: HexTreeSet = PLAIN_US915_INDICES
Expand All @@ -63,9 +63,9 @@ fn disk_set_lookup(c: &mut Criterion) {
.collect();
let mut file = tempfile::tempfile().unwrap();
us915_set
.to_disktree(&mut file, |_, _| Ok::<(), std::io::Error>(()))
.to_hexdb(&mut file, |_, _| Ok::<(), std::io::Error>(()))
.unwrap();
DiskTreeMap::memmap(&file).unwrap()
HexDb::memmap(&file).unwrap()
};

let tarpon_springs = coord! {x: -82.753822, y: 28.15215};
Expand Down
50 changes: 25 additions & 25 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ pub enum Error {
Index(u64),

/// An io error.
#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Io(std::io::Error),

/// Not a disktree.
#[cfg(feature = "disktree")]
NotDisktree,
/// Not a hexdb.
#[cfg(feature = "hexdb")]
NotHexDb,

/// Unsupported version.
#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Version(u8),

/// Invalid value tag found in disktree.
#[cfg(feature = "disktree")]
/// Invalid value tag found in hexdb.
#[cfg(feature = "hexdb")]
InvalidTag(u8, u64),

/// Invalid value size bytes found in disktree header.
#[cfg(feature = "disktree")]
/// Invalid value size bytes found in hexdb header.
#[cfg(feature = "hexdb")]
Varint(u32),

/// User-provided serializer failed.
#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Writer(Box<dyn std::error::Error + Send + Sync>),
}

#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
impl std::convert::From<std::io::Error> for Error {
fn from(other: std::io::Error) -> Self {
Error::Io(other)
Expand All @@ -45,22 +45,22 @@ impl std::error::Error for Error {
match self {
Error::Index(_) => None,

#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Error::Io(inner) => inner.source(),

#[cfg(feature = "disktree")]
Error::NotDisktree => None,
#[cfg(feature = "hexdb")]
Error::NotHexDb => None,

#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Error::Version(_) => None,

#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Error::InvalidTag(_, _) => None,

#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Error::Varint(_) => None,

#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Error::Writer(inner) => inner.source(),
}
}
Expand All @@ -71,30 +71,30 @@ impl std::fmt::Display for Error {
match self {
Error::Index(bits) => write!(f, "raw u64 is not a valid H3 index: {bits}"),

#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Error::Io(io_error) => io_error.fmt(f),

#[cfg(feature = "disktree")]
Error::NotDisktree => {
#[cfg(feature = "hexdb")]
Error::NotHexDb => {
write!(f, "file missing magic header")
}

#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Error::Version(version) => {
write!(f, "unsupported version, got {version}")
}

#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Error::InvalidTag(tag, pos) => {
write!(f, "invalid tag, got {tag}, pos {pos}")
}

#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Error::Varint(val) => {
write!(f, "byte sequence is not a valid varint, got {val}")
}

#[cfg(feature = "disktree")]
#[cfg(feature = "hexdb")]
Error::Writer(writer_error) => {
write!(f, "provided writer returned an error, got {writer_error}")
}
Expand Down
6 changes: 3 additions & 3 deletions src/disktree/dtseek.rs → src/hexdb/dbseek.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::disktree::dptr::Dp;
use crate::hexdb::dptr::Dp;

pub(crate) trait DtSeek {
pub(crate) trait DbSeek {
fn pos(&mut self) -> std::io::Result<Dp>;

fn seek(&mut self, dp: Dp) -> std::io::Result<Dp>;

fn fast_forward(&mut self) -> std::io::Result<Dp>;
}

impl<S> DtSeek for S
impl<S> DbSeek for S
where
S: std::io::Seek,
{
Expand Down
File renamed without changes.
34 changes: 17 additions & 17 deletions src/disktree/iter.rs → src/hexdb/iter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
cell::CellStack,
disktree::{dptr::Dp, dtseek::DtSeek, tree::HDR_SZ, varint},
error::{Error, Result},
hexdb::{dbseek::DbSeek, dptr::Dp, tree::HDR_SZ, varint},
Cell,
};
use byteorder::ReadBytesExt;
Expand All @@ -10,8 +10,8 @@ use std::io::Cursor;
pub(crate) struct Iter<'a> {
cell_stack: CellStack,
curr_node: Option<(u8, Dp)>,
disktree_buf: &'a [u8],
disktree_csr: Cursor<&'a [u8]>,
hexdb_buf: &'a [u8],
hexdb_csr: Cursor<&'a [u8]>,
node_stack: Vec<Vec<(u8, Dp)>>,
recycle_bin: Vec<Vec<(u8, Dp)>>,
}
Expand Down Expand Up @@ -40,13 +40,13 @@ impl<'a> Iter<'a> {
// `pos` is a position in the file of this node's tag.
fn read_node(&mut self, dptr: Dp) -> Result<Node> {
let dptr = self.seek(dptr)?;
let node_tag = self.disktree_csr.read_u8()?;
let node_tag = self.hexdb_csr.read_u8()?;
if 0 == node_tag & 0b1000_0000 {
Ok(Node::Leaf(dptr))
} else {
let mut children = self.node_buf();
let n_children = (node_tag & 0b0111_1111).count_ones() as usize;
let child_dptrs = Dp::read_n(&mut self.disktree_csr, n_children)?;
let child_dptrs = Dp::read_n(&mut self.hexdb_csr, n_children)?;
children.extend(
(0..7)
.rev()
Expand Down Expand Up @@ -86,12 +86,12 @@ impl<'a> Iter<'a> {
self.curr_node = None;
}

pub(crate) fn new(disktree_buf: &'a [u8]) -> Result<Iter<'a>> {
let mut disktree_csr = Cursor::new(disktree_buf);
pub(crate) fn new(hexdb_buf: &'a [u8]) -> Result<Iter<'a>> {
let mut hexdb_csr = Cursor::new(hexdb_buf);
let mut cell_stack = CellStack::new();
let mut node_stack = Vec::new();
let recycle_bin = Vec::new();
let mut base_nodes = Self::read_base_nodes(&mut disktree_csr)?;
let mut base_nodes = Self::read_base_nodes(&mut hexdb_csr)?;
let curr_node = base_nodes.pop();
node_stack.push(base_nodes);
if let Some((digit, _)) = curr_node {
Expand All @@ -100,8 +100,8 @@ impl<'a> Iter<'a> {
Ok(Self {
cell_stack,
curr_node,
disktree_buf,
disktree_csr,
hexdb_buf,
hexdb_csr,
recycle_bin,
node_stack,
})
Expand Down Expand Up @@ -148,14 +148,14 @@ impl<'a> Iterator for Iter<'a> {
self.stop_yielding();
return Some(Err(Error::from(e)));
}
match varint::read(&mut self.disktree_csr) {
match varint::read(&mut self.hexdb_csr) {
Err(e) => {
self.stop_yielding();
return Some(Err(e));
}
Ok((val_len, _n_read)) => {
let pos = self.disktree_csr.position() as usize;
let val_buf = &self.disktree_buf[pos..][..val_len as usize];
let pos = self.hexdb_csr.position() as usize;
let val_buf = &self.hexdb_buf[pos..][..val_len as usize];
return Some(Ok((
*self.cell_stack.cell().expect("corrupted cell-stack"),
val_buf,
Expand All @@ -169,16 +169,16 @@ impl<'a> Iterator for Iter<'a> {
}
}

impl<'a> DtSeek for Iter<'a> {
impl<'a> DbSeek for Iter<'a> {
fn pos(&mut self) -> std::io::Result<Dp> {
self.disktree_csr.pos()
self.hexdb_csr.pos()
}

fn seek(&mut self, dp: Dp) -> std::io::Result<Dp> {
self.disktree_csr.seek(dp)
self.hexdb_csr.seek(dp)
}

fn fast_forward(&mut self) -> std::io::Result<Dp> {
self.disktree_csr.fast_forward()
self.hexdb_csr.fast_forward()
}
}
Loading

0 comments on commit c99b3d9

Please sign in to comment.