2023-02-21 16:46:00 +08:00
|
|
|
use super::{Edge, RankingRuleGraph, RankingRuleGraphTrait};
|
2023-03-07 02:21:55 +08:00
|
|
|
use crate::new::{QueryGraph, SearchContext};
|
|
|
|
use crate::Result;
|
|
|
|
use roaring::RoaringBitmap;
|
2023-02-21 16:46:00 +08:00
|
|
|
|
|
|
|
impl<G: RankingRuleGraphTrait> RankingRuleGraph<G> {
|
2023-03-07 02:21:55 +08:00
|
|
|
pub fn build(ctx: &mut SearchContext, query_graph: QueryGraph) -> Result<Self> {
|
2023-02-21 19:33:32 +08:00
|
|
|
let mut ranking_rule_graph =
|
|
|
|
Self { query_graph, all_edges: vec![], node_edges: vec![], successors: vec![] };
|
2023-02-21 16:46:00 +08:00
|
|
|
|
|
|
|
for (node_idx, node) in ranking_rule_graph.query_graph.nodes.iter().enumerate() {
|
2023-02-21 19:33:32 +08:00
|
|
|
ranking_rule_graph.node_edges.push(RoaringBitmap::new());
|
|
|
|
ranking_rule_graph.successors.push(RoaringBitmap::new());
|
2023-02-21 16:46:00 +08:00
|
|
|
let new_edges = ranking_rule_graph.node_edges.last_mut().unwrap();
|
2023-02-21 19:33:32 +08:00
|
|
|
let new_successors = ranking_rule_graph.successors.last_mut().unwrap();
|
2023-02-21 16:46:00 +08:00
|
|
|
|
2023-03-07 02:21:55 +08:00
|
|
|
let Some(from_node_data) = G::build_visit_from_node(ctx, node)? else { continue };
|
2023-02-21 16:46:00 +08:00
|
|
|
|
2023-02-21 19:33:32 +08:00
|
|
|
for successor_idx in ranking_rule_graph.query_graph.edges[node_idx].successors.iter() {
|
|
|
|
let to_node = &ranking_rule_graph.query_graph.nodes[successor_idx as usize];
|
2023-03-07 02:21:55 +08:00
|
|
|
let mut edges = G::build_visit_to_node(ctx, to_node, &from_node_data)?;
|
2023-02-21 19:33:32 +08:00
|
|
|
if edges.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
edges.sort_by_key(|e| e.0);
|
2023-02-21 16:46:00 +08:00
|
|
|
for (cost, details) in edges {
|
|
|
|
ranking_rule_graph.all_edges.push(Some(Edge {
|
2023-02-21 19:55:44 +08:00
|
|
|
from_node: node_idx as u32,
|
|
|
|
to_node: successor_idx,
|
2023-02-21 16:46:00 +08:00
|
|
|
cost,
|
|
|
|
details,
|
|
|
|
}));
|
2023-02-21 19:33:32 +08:00
|
|
|
new_edges.insert(ranking_rule_graph.all_edges.len() as u32 - 1);
|
|
|
|
new_successors.insert(successor_idx);
|
2023-02-21 16:46:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(ranking_rule_graph)
|
|
|
|
}
|
|
|
|
}
|