Skip to content

Commit

Permalink
Merge pull request #37 from JayKickliter/jsk/disktree/abstract-inner
Browse files Browse the repository at this point in the history
Hide disktree storage type
  • Loading branch information
JayKickliter committed Jan 22, 2024
2 parents 917b819 + 9f9a324 commit cfdc1aa
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ serde = { version = "1", optional = true, features = ["derive"] }
bincode = { version = "1.3.3" }
byteorder = { version = "1" }
criterion = { version = "0.3", features = ["html_reports"] }
geo = "0.26.0"
h3o = { version = "0.4.0", features = ["geo"] }
h3ron = "0.15.1"
geo = "0.27"
h3o = { version = "0.5.0", features = ["geo"] }
h3ron = "0.18"
tempfile = "3"

[dev-dependencies.h3-lorawan-regions]
Expand Down
9 changes: 7 additions & 2 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use h3o::{
CellIndex, Resolution,
};
use h3ron::H3Cell;
use hextree::{compaction::EqCompactor, disktree::DiskTreeMap, Cell, HexTreeMap, HexTreeSet};
use hextree::{compaction::EqCompactor, Cell, HexTreeMap, HexTreeSet};
use std::convert::TryFrom;

fn set_lookup(c: &mut Criterion) {
Expand Down Expand Up @@ -48,7 +48,12 @@ fn set_lookup(c: &mut Criterion) {
}
}

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

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

let us915_disk_set = {
Expand All @@ -60,7 +65,7 @@ fn disk_set_lookup(c: &mut Criterion) {
us915_set
.to_disktree(&mut file, |_, _| Ok::<(), std::io::Error>(()))
.unwrap();
DiskTreeMap::memmap(file).unwrap()
DiskTreeMap::memmap(&file).unwrap()
};

let tarpon_springs = coord! {x: -82.753822, y: 28.15215};
Expand Down
2 changes: 1 addition & 1 deletion src/disktree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ mod tests {
map
};

let monaco_disktree: DiskTreeMap<_> = {
let monaco_disktree: DiskTreeMap = {
let file = tempfile::NamedTempFile::new().unwrap();
let (mut file, path) = file.keep().unwrap();
monaco_hextree
Expand Down
29 changes: 15 additions & 14 deletions src/disktree/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
Cell, Error,
};
use byteorder::ReadBytesExt;
use memmap::{Mmap, MmapOptions};
use memmap::MmapOptions;
use std::{
fs::File,
io::{Cursor, Read, Seek, SeekFrom},
Expand All @@ -17,26 +17,27 @@ pub(crate) const HDR_MAGIC: &[u8] = b"hextree\0";
pub(crate) const HDR_SZ: u64 = HDR_MAGIC.len() as u64 + 1;

/// An on-disk hextree map.
pub struct DiskTreeMap<B>(B);
pub struct DiskTreeMap(Box<dyn AsRef<[u8]>>);

impl DiskTreeMap<Mmap> {
impl DiskTreeMap {
/// Opens a `DiskTree` at the specified path.
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
let file = File::open(path)?;
Self::memmap(file)
Self::memmap(&file)
}

/// Memory maps the provided disktree-containing.
pub fn memmap(file: File) -> Result<Self> {
/// Memory maps the provided disktree-containing file.
pub fn memmap(file: &File) -> Result<Self> {
#[allow(unsafe_code)]
let mm = unsafe { MmapOptions::new().map(&file)? };
let mm = unsafe { MmapOptions::new().map(file)? };
Self::with_buf(mm)
}
}

impl<B: AsRef<[u8]>> DiskTreeMap<B> {
/// Opens a `DiskTree` with a provided buffer.
pub fn with_buf(buf: B) -> Result<Self> {
pub fn with_buf<B>(buf: B) -> Result<Self>
where
B: AsRef<[u8]> + 'static,
{
let mut csr = Cursor::new(buf);
let magic = {
let mut buf = [0_u8; HDR_MAGIC.len()];
Expand All @@ -53,23 +54,23 @@ impl<B: AsRef<[u8]>> DiskTreeMap<B> {
0xFE - csr.read_u8()?
};
match version {
0 => Ok(Self(csr.into_inner())),
0 => Ok(Self(Box::new(csr.into_inner()))),
unsupported_version => Err(Error::Version(unsupported_version)),
}
}

/// Returns `(Cell, &[u8])`, if present.
pub fn get(&self, cell: Cell) -> Result<Option<(Cell, &[u8])>> {
let base_cell_pos = Self::base_cell_dptr(cell);
let mut csr = Cursor::new(self.0.as_ref());
let mut csr = Cursor::new((*self.0).as_ref());
csr.seek(SeekFrom::Start(base_cell_pos.into()))?;
let node_dptr = Dptr::read(&mut csr)?;
if node_dptr.is_null() {
return Ok(None);
}
let digits = Digits::new(cell);
if let Some((cell, range)) = Self::_get(&mut csr, 0, node_dptr, cell, digits)? {
let val_bytes = &self.0.as_ref()[range];
let val_bytes = &(*self.0).as_ref()[range];
Ok(Some((cell, val_bytes)))
} else {
Ok(None)
Expand All @@ -84,7 +85,7 @@ impl<B: AsRef<[u8]>> DiskTreeMap<B> {
/// Returns an iterator visiting all `(Cell, &[u8])` pairs in
/// arbitrary order.
pub fn iter(&self) -> Result<impl Iterator<Item = Result<(Cell, &[u8])>>> {
Iter::new(self.0.as_ref())
Iter::new((*self.0).as_ref())
}

fn _get(
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//! with H3 cells.
//!
//! The primary structures are:
//! - [`HexTreeMap`][crate::HexTreeMap]: an Cell-to-value map.
//! - [`HexTreeSet`][crate::HexTreeSet]: a Cell set for hit-testing.
//! - [`HexTreeMap`]: an Cell-to-value map.
//! - [`HexTreeSet`]: a Cell set for hit-testing.
//!
//! You can think of `HexTreeMap` vs. `HexTreeSet` as [`HashMap`] vs. [`HashSet`].
//!
Expand Down

0 comments on commit cfdc1aa

Please sign in to comment.