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

this is definitely stupid overkill. and reads are kind of slow anyway. I am reverted to postgres

parent dfca76f2
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ async fn main() -> anyhow::Result<()> { ...@@ -5,7 +5,7 @@ async fn main() -> anyhow::Result<()> {
let mut nr = NodeReader::new().await?; let mut nr = NodeReader::new().await?;
let mut er = EdgeReader::new().await?; let mut er = EdgeReader::new().await?;
let id = nr.id_from_name("com.scd31.git").await?.unwrap(); let id = nr.id_from_name("com.scd31").await?.unwrap();
for e in er.get_nodes_to(id).await? { for e in er.get_nodes_to(id).await? {
dbg!(&e); dbg!(&e);
......
...@@ -44,27 +44,29 @@ impl NodeReader { ...@@ -44,27 +44,29 @@ impl NodeReader {
// binary search for name // binary search for name
let mut min = 0; let mut min = 0;
let mut max = self.num_nodes; let mut max = self.num_nodes - 1;
loop { while max >= min {
let cur = (max + min) / 2 + 1; let cur = (max + min) / 2;
let node = self.get_node_from_name_idx(cur).await?; let node = self.get_node_from_name_idx(cur).await?;
let c = name.cmp(&node.name); let c = name.cmp(&node.name);
match c { match c {
Ordering::Less => max = cur, Ordering::Less => max = cur - 1,
Ordering::Equal => { Ordering::Equal => {
return Ok(Some(node.id)); return Ok(Some(node.id));
} }
Ordering::Greater => min = cur, Ordering::Greater => min = cur + 1,
} }
} }
Ok(None)
} }
pub async fn name_from_id(&mut self, id: u32) -> anyhow::Result<Option<String>> { pub async fn name_from_id(&mut self, id: u32) -> anyhow::Result<Option<String>> {
let mut min = 0; let mut min = 0;
let mut max = self.num_nodes; let mut max = self.num_nodes - 1;
loop { loop {
let cur = (max + min) / 2; let cur = (max + min) / 2;
...@@ -82,11 +84,11 @@ impl NodeReader { ...@@ -82,11 +84,11 @@ impl NodeReader {
let c = id.cmp(&host_id); let c = id.cmp(&host_id);
match c { match c {
Ordering::Less => max = cur, Ordering::Less => max = cur - 1,
Ordering::Equal => { Ordering::Equal => {
return Ok(Some(host)); return Ok(Some(host));
} }
Ordering::Greater => min = cur, Ordering::Greater => min = cur + 1,
} }
if max == min { if max == min {
...@@ -154,40 +156,40 @@ impl EdgeReader { ...@@ -154,40 +156,40 @@ impl EdgeReader {
pub async fn get_nodes_from(&mut self, from: u32) -> anyhow::Result<Vec<Edge>> { pub async fn get_nodes_from(&mut self, from: u32) -> anyhow::Result<Vec<Edge>> {
let mut min = 0; let mut min = 0;
let mut max = self.num_edges; let mut max = self.num_edges - 1;
loop { loop {
let cur = (max + min) / 2 + 1; let cur = (max + min) / 2;
let edge = self.get_edge_from_from_idx(cur).await?; let edge = self.get_edge_from_from_idx(cur).await?;
let c = from.cmp(&edge.from); let c = from.cmp(&edge.from);
match c { match c {
Ordering::Less => max = cur, Ordering::Less => max = cur - 1,
Ordering::Equal => { Ordering::Equal => {
return self.get_all_consecutive_matching_from(cur, from).await; return self.get_all_consecutive_matching_from(cur, from).await;
} }
Ordering::Greater => min = cur, Ordering::Greater => min = cur + 1,
} }
} }
} }
pub async fn get_nodes_to(&mut self, to: u32) -> anyhow::Result<Vec<Edge>> { pub async fn get_nodes_to(&mut self, to: u32) -> anyhow::Result<Vec<Edge>> {
let mut min = 0; let mut min = 0;
let mut max = self.num_edges; let mut max = self.num_edges - 1;
loop { loop {
let cur = (max + min) / 2 + 1; let cur = (max + min) / 2;
let edge = self.get_edge_from_to_idx(cur).await?; let edge = self.get_edge_from_to_idx(cur).await?;
let c = to.cmp(&edge.to); let c = to.cmp(&edge.to);
match c { match c {
Ordering::Less => max = cur, Ordering::Less => max = cur - 1,
Ordering::Equal => { Ordering::Equal => {
return self.get_all_consecutive_matching_to(cur, to).await; return self.get_all_consecutive_matching_to(cur, to).await;
} }
Ordering::Greater => min = cur, Ordering::Greater => min = cur + 1,
} }
} }
} }
......
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