Skip to content
Snippets Groups Projects
Commit 5c14ebd1 authored by Stephen D's avatar Stephen D
Browse files

name from id binary search

parent 7243e0a5
No related branches found
No related tags found
No related merge requests found
......@@ -119,7 +119,7 @@ async fn save_nodes(mut rx: Receiver<String>) -> anyhow::Result<()> {
.into_iter()
.map(|(id, offset)| {
// An allocation in here is gross
let mut x = id.to_le_bytes().to_vec();
let mut x = id.to_le_bytes().to_vec(); // TODO no reason to put id here?
x.extend(offset.to_le_bytes());
x
......
......@@ -4,7 +4,9 @@ use commoncrawl_graph::graph::NodeReader;
async fn main() -> anyhow::Result<()> {
let mut nr = NodeReader::new().await?;
nr.id_from_name("com.scd31").await?;
let id = nr.id_from_name("com.scd31").await?.unwrap();
let host = nr.name_from_id(id).await?;
println!("{:?}", host);
Ok(())
}
......@@ -59,4 +59,33 @@ impl NodeReader {
}
}
}
pub async fn name_from_id(&mut self, id: u32) -> anyhow::Result<Option<String>> {
let mut min = 0;
let mut max = self.num_nodes;
loop {
let cur = (max + min) / 2 + 1;
self.id_index.seek(SeekFrom::Start(cur * 12 + 4)).await?;
let offset = self.id_index.read_u64_le().await?;
self.nodes.seek(SeekFrom::Start(offset.into())).await?;
let host_id = self.nodes.read_u32_le().await?;
let host = BufReader::new(&mut self.nodes)
.lines()
.next_line()
.await?
.context("Missing line")?;
let c = id.cmp(&host_id);
match c {
Ordering::Less => max = cur,
Ordering::Equal => {
return Ok(Some(host));
}
Ordering::Greater => min = cur,
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment