Skip to content

Commit

Permalink
Hide disktree storage type
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKickliter committed Jan 22, 2024
1 parent 917b819 commit 950f5d6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
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
27 changes: 14 additions & 13 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)? };

Check failure on line 32 in src/disktree/tree.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> src/disktree/tree.rs:32:50 | 32 | let mm = unsafe { MmapOptions::new().map(&file)? }; | ^^^^^ help: change this to: `file` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-D clippy::needless-borrow` implied by `-D clippy::all` = help: to override `-D clippy::all` add `#[allow(clippy::needless_borrow)]`
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 950f5d6

Please sign in to comment.