Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index check command #28

Merged
merged 3 commits into from
Apr 5, 2023
Merged
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
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde_json = "1"
flate2 = "1"
h3ron = {version = "0.16"}
geo-types = "*"
hextree = "0"
byteorder = "1.4"
rayon = "1.6"
log = "0"
Expand Down
55 changes: 54 additions & 1 deletion generator/src/index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::polyfill;
use crate::{polyfill, print_json};
use anyhow::Result;
use byteorder::ReadBytesExt;
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
Expand All @@ -23,13 +23,15 @@ impl Cmd {
pub enum IndexCmd {
Generate(Generate),
Export(Export),
Find(Find),
}

impl IndexCmd {
pub fn run(&self) -> Result<()> {
match self {
Self::Generate(cmd) => cmd.run(),
Self::Export(cmd) => cmd.run(),
Self::Find(cmd) => cmd.run(),
}
}
}
Expand Down Expand Up @@ -101,6 +103,18 @@ fn read_cells<P: AsRef<path::Path>>(file: P) -> Result<Vec<H3Cell>> {
Ok(vec)
}

fn read_hexset<P: AsRef<path::Path>>(file: P) -> Result<hextree::HexTreeSet> {
let file = fs::File::open(file.as_ref())?;
let mut reader = GzDecoder::new(file);
let mut vec = Vec::new();

while let Ok(entry) = reader.read_u64::<byteorder::LittleEndian>() {
vec.push(hextree::Cell::from_raw(entry)?);
}

Ok(vec.iter().collect())
}

fn write_cells<P: AsRef<path::Path>>(cells: Vec<H3Cell>, output: P) -> Result<()> {
let file = fs::File::create(output.as_ref())?;
let mut writer = GzEncoder::new(file, Compression::default());
Expand Down Expand Up @@ -148,3 +162,42 @@ impl Export {
Ok(())
}
}

/// Check membership of one or moreh3 indexes in all h3idz files in a given
/// folder
#[derive(Debug, clap::Args)]
pub struct Find {
input: path::PathBuf,
cells: Vec<h3ron::H3Cell>,
Comment on lines +170 to +171
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you're doing here, and why you have to choose between a PathBuf, Vec<H3Cell> or Vec<PathBuf>, H3Cell due to positional arguments. But my preference would be to have a single Cell and and Vec<PathBuf>. This way, the user can call find <SOMEHEX> path/to/indicies/* or even find <SOMEHEX> path/to/indicies/*.h3idz.

Note that #29 uses Vec<PathBuf>, so we should use the same convention regardless which we choose.

Copy link
Member Author

@madninja madninja Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted for a folder argument which would be searched for all h3idz files to look for multiple cells in. This seems to be how we've been wanting to use it to find multiple locations at once.

Open to being convinced otherwise

}

impl Find {
pub fn run(&self) -> Result<()> {
use std::collections::HashMap;
let paths = std::fs::read_dir(&self.input)?;
let needles: Vec<(String, hextree::Cell)> = self
.cells
.iter()
.map(|entry| hextree::Cell::from_raw(**entry).map(|cell| (entry.to_string(), cell)))
.collect::<hextree::Result<Vec<(String, hextree::Cell)>>>()?;
let mut matches: HashMap<String, Vec<path::PathBuf>> = HashMap::new();
for path_result in paths {
let path = path_result?.path();
if path.extension().map(|ext| ext == "h3idz").unwrap_or(false) {
let hex_set = read_hexset(&path)?;
for (name, needle) in &needles {
if hex_set.contains(*needle) {
let match_list = matches.entry(name.to_string()).or_insert(vec![]);

// Avoid duplicate path entries if the same location is
// specified multiple times
if !match_list.contains(&path) {
match_list.push(path.clone())
}
}
}
}
}
print_json(&matches)
}
}
5 changes: 5 additions & 0 deletions generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ fn main() -> Result<()> {
let cli = Cli::parse();
cli.run()
}

pub(crate) fn print_json<T: ?Sized + serde::Serialize>(value: &T) -> Result<()> {
println!("{}", serde_json::to_string_pretty(value)?);
Ok(())
}