Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 0 additions & 26 deletions src/disktree/dtseek.rs

This file was deleted.

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
26 changes: 26 additions & 0 deletions src/hexdb/dbseek.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::hexdb::dptr::P;

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

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

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

impl<S> DbSeek for S
where
S: std::io::Seek,
{
fn pos(&mut self) -> std::io::Result<P> {
self.stream_position().map(P::from)
}

fn seek(&mut self, dp: P) -> std::io::Result<P> {
self.seek(std::io::SeekFrom::Start(dp.into())).map(P::from)
}

fn fast_forward(&mut self) -> std::io::Result<P> {
self.seek(std::io::SeekFrom::End(0)).map(P::from)
}
}
59 changes: 30 additions & 29 deletions src/disktree/dptr.rs → src/hexdb/dptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use std::{
/// A 'disk' pointer.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub(crate) struct Dp(u64);
pub(crate) struct P(u64);

impl Dp {
impl P {
#[allow(clippy::cast_possible_truncation)]
const MAX: u64 = 2_u64.pow(Self::DISK_REPR_SZ as u32 * 8) - 1;
const DISK_REPR_SZ: usize = 5;
Expand All @@ -21,15 +21,16 @@ impl Dp {
self.0 == Self::NULL
}

pub(crate) const fn null() -> Dp {
Dp(Self::NULL)
pub(crate) const fn null() -> P {
P(Self::NULL)
}

pub(crate) const fn size() -> usize {
Self::DISK_REPR_SZ
}

/// Read 5 bytes from disk and parses them as little-endian `u64`.
/// Read [`DISK_REPR_SZ`][Self::DISK_REPR_SZ] bytes from disk and
/// parses them as little-endian `u64`.
pub(crate) fn read<R>(src: &mut R) -> Result<Self>
where
R: Read,
Expand All @@ -42,7 +43,7 @@ impl Dp {

/// Read 5 * `n` bytes from disk, for up to n=7, and parses them as
/// little-endian `u64`s.
pub(crate) fn read_n<R>(src: &mut R, n: usize) -> Result<Vec<Dp>>
pub(crate) fn read_n<R>(src: &mut R, n: usize) -> Result<Vec<P>>
where
R: Read,
{
Expand All @@ -56,7 +57,7 @@ impl Dp {
buf[..Self::DISK_REPR_SZ].copy_from_slice(chunk);
u64::from_le_bytes(buf)
})
.map(Dp::from)
.map(P::from)
.collect())
}

Expand All @@ -70,51 +71,51 @@ impl Dp {
}
}

impl Add<usize> for Dp {
type Output = Dp;
impl Add<usize> for P {
type Output = P;

fn add(self, rhs: usize) -> Dp {
Dp::from(self.0 + rhs as u64)
fn add(self, rhs: usize) -> P {
P::from(self.0 + rhs as u64)
}
}

impl Add<u64> for Dp {
type Output = Dp;
impl Add<u64> for P {
type Output = P;

fn add(self, rhs: u64) -> Dp {
Dp::from(self.0 + rhs)
fn add(self, rhs: u64) -> P {
P::from(self.0 + rhs)
}
}

impl Add<u32> for Dp {
type Output = Dp;
impl Add<u32> for P {
type Output = P;

fn add(self, rhs: u32) -> Dp {
Dp::from(self.0 + rhs as u64)
fn add(self, rhs: u32) -> P {
P::from(self.0 + rhs as u64)
}
}

impl From<Dp> for u64 {
fn from(Dp(raw): Dp) -> u64 {
impl From<P> for u64 {
fn from(P(raw): P) -> u64 {
raw
}
}

impl From<u64> for Dp {
fn from(raw: u64) -> Dp {
impl From<u64> for P {
fn from(raw: u64) -> P {
assert!(raw <= Self::MAX);
Dp(raw)
P(raw)
}
}

impl From<usize> for Dp {
fn from(raw: usize) -> Dp {
Dp::from(raw as u64)
impl From<usize> for P {
fn from(raw: usize) -> P {
P::from(raw as u64)
}
}

impl From<Dp> for usize {
fn from(Dp(raw): Dp) -> usize {
impl From<P> for usize {
fn from(P(raw): P) -> usize {
usize::try_from(raw).unwrap()
}
}
Loading