Skip to content
Closed
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
84 changes: 29 additions & 55 deletions crates/core/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
RelationInput, RelationPatch, Result,
};
use crate::{Direction, GraphModel, PathOptions, PathOutcome};
use futures_util::TryStreamExt;
use sqlx::{PgConnection, Postgres, Transaction};
use std::collections::{BTreeSet, HashMap, VecDeque};

Expand Down Expand Up @@ -656,8 +657,8 @@ enum PathExpansion {
#[derive(sqlx::FromRow)]
struct PathRelation {
id: i32,
from_: Option<i32>,
to_: Option<i32>,
parent: i32,
neighbor: i32,
}

async fn expand_path_side(
Expand All @@ -670,39 +671,45 @@ async fn expand_path_side(
) -> Result<PathExpansion> {
let outward = matches!(options.direction, Direction::Out | Direction::Both);
let inward = matches!(options.direction, Direction::In | Direction::Both);
let endpoints = [
(if reverse { inward } else { outward }, "from_", "to_"),
(if reverse { outward } else { inward }, "to_", "from_"),
];
let mut next = BTreeSet::new();
let mut meeting = None;
let mut best_hops = None;
let frontier: Vec<_> = own.frontier.iter().copied().collect();
for chunk in frontier.chunks(200) {
let members: BTreeSet<_> = chunk.iter().copied().collect();
let relations = path_frontier_relations(connection, chunk, options, reverse).await?;
for relation in relations {
let relation_id = relation.id;
let (Some(from), Some(to)) = (relation.from_, relation.to_) else {
for (enabled, parent_column, neighbor_column) in endpoints {
if !enabled {
continue;
};
let steps = if !reverse {
[
(outward && members.contains(&from)).then_some((from, to)),
(inward && members.contains(&to)).then_some((to, from)),
]
} else {
[
(outward && members.contains(&to)).then_some((to, from)),
(inward && members.contains(&from)).then_some((from, to)),
]
};
for (current, neighbor) in steps.into_iter().flatten() {
}
let query = format!(
"SELECT id,{parent_column} AS parent,{neighbor_column} AS neighbor \
FROM inkcre.relations WHERE {parent_column}=ANY($1) \
AND {neighbor_column} IS NOT NULL \
AND (cardinality($2::text[])=0 OR content=ANY($2)) ORDER BY id DESC"
);
// Consume the frontier until SQL is exhausted or the Block budget is exceeded.
let mut relations = sqlx::query_as::<_, PathRelation>(&query)
.bind(chunk)
.bind(&options.contents)
.fetch(&mut *connection);
while let Some(PathRelation {
id,
parent,
neighbor,
}) = relations.try_next().await?
{
if own.steps.contains_key(&neighbor) {
continue;
}
own.steps.insert(neighbor, Some((current, relation_id)));
own.steps.insert(neighbor, Some((parent, id)));
explored.insert(neighbor);
if explored.len() > options.max_explored_blocks as usize {
return Ok(PathExpansion::BudgetExceeded);
}
let depth = own.depths[&current] + 1;
let depth = own.depths[&parent] + 1;
own.depths.insert(neighbor, depth);
next.insert(neighbor);
if let Some(other_depth) = other.depths.get(&neighbor) {
Expand All @@ -718,39 +725,6 @@ async fn expand_path_side(
own.frontier = next;
Ok(PathExpansion::Complete(meeting))
}
async fn path_frontier_relations(
connection: &mut PgConnection,
frontier: &[i32],
options: &PathOptions,
reverse: bool,
) -> Result<Vec<PathRelation>> {
let outward = matches!(options.direction, Direction::Out | Direction::Both);
let inward = matches!(options.direction, Direction::In | Direction::Both);
let needs_from = if reverse { inward } else { outward };
let needs_to = if reverse { outward } else { inward };
let mut result = Vec::new();
let mut seen = BTreeSet::new();
// Python preserves first insertion across the from-then-to branches, each ordered by ID DESC.
for (enabled, endpoint) in [(needs_from, "from_"), (needs_to, "to_")] {
if !enabled {
continue;
}
let query = format!(
"SELECT id,from_,to_ FROM inkcre.relations WHERE {endpoint}=ANY($1) AND (cardinality($2::text[])=0 OR content=ANY($2)) ORDER BY id DESC LIMIT 10000"
);
let rows: Vec<PathRelation> = sqlx::query_as(&query)
.bind(frontier)
.bind(&options.contents)
.fetch_all(&mut *connection)
.await?;
for relation in rows {
if seen.insert(relation.id) {
result.push(relation);
}
}
}
Ok(result)
}
async fn assemble_canonical_path(
connection: &mut PgConnection,
source: i32,
Expand Down