2023-02-21 09:46:00 +01:00
|
|
|
use super::{Edge, RankingRuleGraph, RankingRuleGraphTrait};
|
2023-03-30 11:32:55 +02:00
|
|
|
use crate::search::new::interner::{DedupInterner, MappedInterner};
|
|
|
|
use crate::search::new::query_graph::{QueryNode, QueryNodeData};
|
2023-03-08 09:55:53 +01:00
|
|
|
use crate::search::new::small_bitmap::SmallBitmap;
|
|
|
|
use crate::search::new::{QueryGraph, SearchContext};
|
2023-03-06 19:21:55 +01:00
|
|
|
use crate::Result;
|
2023-03-30 11:32:55 +02:00
|
|
|
use std::collections::HashSet;
|
2023-02-21 09:46:00 +01:00
|
|
|
|
|
|
|
impl<G: RankingRuleGraphTrait> RankingRuleGraph<G> {
|
2023-03-08 15:04:25 +01:00
|
|
|
/// Build the ranking rule graph from the given query graph
|
2023-03-30 11:32:55 +02:00
|
|
|
pub fn build(
|
|
|
|
ctx: &mut SearchContext,
|
|
|
|
query_graph: QueryGraph,
|
|
|
|
cost_of_ignoring_node: MappedInterner<QueryNode, Option<(u32, SmallBitmap<QueryNode>)>>,
|
|
|
|
) -> Result<Self> {
|
2023-03-14 16:37:47 +01:00
|
|
|
let QueryGraph { nodes: graph_nodes, .. } = &query_graph;
|
2023-03-07 14:42:58 +01:00
|
|
|
|
2023-03-14 16:37:47 +01:00
|
|
|
let mut conditions_interner = DedupInterner::default();
|
2023-03-13 12:46:32 +01:00
|
|
|
|
2023-03-16 11:52:51 +01:00
|
|
|
let mut edges_store = DedupInterner::default();
|
2023-03-14 16:37:47 +01:00
|
|
|
let mut edges_of_node = query_graph.nodes.map(|_| HashSet::new());
|
2023-02-21 09:46:00 +01:00
|
|
|
|
2023-03-14 16:37:47 +01:00
|
|
|
for (source_id, source_node) in graph_nodes.iter() {
|
|
|
|
let new_edges = edges_of_node.get_mut(source_id);
|
2023-02-21 09:46:00 +01:00
|
|
|
|
2023-03-14 16:37:47 +01:00
|
|
|
for dest_idx in source_node.successors.iter() {
|
2023-03-30 11:32:55 +02:00
|
|
|
let src_term = match &source_node.data {
|
|
|
|
QueryNodeData::Term(t) => Some(t),
|
|
|
|
QueryNodeData::Start => None,
|
|
|
|
QueryNodeData::Deleted | QueryNodeData::End => panic!(),
|
|
|
|
};
|
2023-03-14 16:37:47 +01:00
|
|
|
let dest_node = graph_nodes.get(dest_idx);
|
2023-03-30 11:32:55 +02:00
|
|
|
let dest_term = match &dest_node.data {
|
|
|
|
QueryNodeData::Term(t) => t,
|
|
|
|
QueryNodeData::End => {
|
|
|
|
let new_edge_id = edges_store.insert(Some(Edge {
|
|
|
|
source_node: source_id,
|
|
|
|
dest_node: dest_idx,
|
|
|
|
cost: 0,
|
|
|
|
condition: None,
|
|
|
|
nodes_to_skip: SmallBitmap::for_interned_values_in(graph_nodes),
|
|
|
|
}));
|
|
|
|
new_edges.insert(new_edge_id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QueryNodeData::Deleted | QueryNodeData::Start => panic!(),
|
|
|
|
};
|
|
|
|
if let Some((cost_of_ignoring, forbidden_nodes)) =
|
|
|
|
cost_of_ignoring_node.get(dest_idx)
|
|
|
|
{
|
|
|
|
let new_edge_id = edges_store.insert(Some(Edge {
|
|
|
|
source_node: source_id,
|
|
|
|
dest_node: dest_idx,
|
|
|
|
cost: *cost_of_ignoring,
|
|
|
|
condition: None,
|
|
|
|
nodes_to_skip: forbidden_nodes.clone(),
|
|
|
|
}));
|
|
|
|
new_edges.insert(new_edge_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
let edges = G::build_edges(ctx, &mut conditions_interner, src_term, dest_term)?;
|
2023-02-21 12:33:32 +01:00
|
|
|
if edges.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
2023-03-08 15:04:25 +01:00
|
|
|
|
2023-03-13 12:46:32 +01:00
|
|
|
for (cost, condition) in edges {
|
2023-03-16 11:52:51 +01:00
|
|
|
let new_edge_id = edges_store.insert(Some(Edge {
|
2023-03-14 16:37:47 +01:00
|
|
|
source_node: source_id,
|
2023-03-14 10:54:55 +01:00
|
|
|
dest_node: dest_idx,
|
2023-02-21 09:46:00 +01:00
|
|
|
cost,
|
2023-03-30 11:32:55 +02:00
|
|
|
condition: Some(condition),
|
|
|
|
nodes_to_skip: SmallBitmap::for_interned_values_in(graph_nodes),
|
2023-02-21 09:46:00 +01:00
|
|
|
}));
|
2023-03-14 16:37:47 +01:00
|
|
|
new_edges.insert(new_edge_id);
|
2023-02-21 09:46:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-14 16:37:47 +01:00
|
|
|
let edges_store = edges_store.freeze();
|
|
|
|
let edges_of_node =
|
|
|
|
edges_of_node.map(|edges| SmallBitmap::from_iter(edges.iter().copied(), &edges_store));
|
|
|
|
|
2023-03-30 11:32:55 +02:00
|
|
|
let conditions_interner = conditions_interner.freeze();
|
|
|
|
|
|
|
|
Ok(RankingRuleGraph { query_graph, edges_store, edges_of_node, conditions_interner })
|
2023-02-21 09:46:00 +01:00
|
|
|
}
|
|
|
|
}
|