2023-02-21 09:48:49 +01:00
|
|
|
use heed::RoTxn;
|
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
2023-02-21 13:57:34 +01:00
|
|
|
use super::db_cache::DatabaseCache;
|
2023-02-22 15:34:37 +01:00
|
|
|
use super::logger::SearchLogger;
|
2023-03-06 08:35:01 +01:00
|
|
|
use super::ranking_rule_graph::EdgeDocidsCache;
|
|
|
|
use super::ranking_rule_graph::EmptyPathsCache;
|
2023-03-02 21:27:42 +01:00
|
|
|
|
2023-02-21 13:57:34 +01:00
|
|
|
use super::ranking_rule_graph::{RankingRuleGraph, RankingRuleGraphTrait};
|
2023-03-02 21:27:42 +01:00
|
|
|
use super::{BitmapOrAllRef, QueryGraph, RankingRule, RankingRuleOutput};
|
2023-02-28 11:49:24 +01:00
|
|
|
|
2023-02-21 13:57:34 +01:00
|
|
|
use crate::{Index, Result};
|
2023-02-21 09:48:49 +01:00
|
|
|
|
|
|
|
pub struct GraphBasedRankingRule<G: RankingRuleGraphTrait> {
|
2023-02-22 15:34:37 +01:00
|
|
|
id: String,
|
2023-02-21 09:48:49 +01:00
|
|
|
state: Option<GraphBasedRankingRuleState<G>>,
|
|
|
|
}
|
2023-02-22 15:34:37 +01:00
|
|
|
impl<G: RankingRuleGraphTrait> GraphBasedRankingRule<G> {
|
|
|
|
pub fn new(id: String) -> Self {
|
|
|
|
Self { id, state: None }
|
2023-02-21 09:48:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GraphBasedRankingRuleState<G: RankingRuleGraphTrait> {
|
|
|
|
graph: RankingRuleGraph<G>,
|
|
|
|
edge_docids_cache: EdgeDocidsCache<G>,
|
|
|
|
empty_paths_cache: EmptyPathsCache,
|
2023-03-02 21:27:42 +01:00
|
|
|
all_distances: Vec<Vec<u64>>,
|
|
|
|
cur_distance_idx: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_empty_edges<'transaction, G: RankingRuleGraphTrait>(
|
|
|
|
graph: &mut RankingRuleGraph<G>,
|
|
|
|
edge_docids_cache: &mut EdgeDocidsCache<G>,
|
|
|
|
index: &Index,
|
|
|
|
txn: &'transaction RoTxn,
|
|
|
|
db_cache: &mut DatabaseCache<'transaction>,
|
|
|
|
universe: &RoaringBitmap,
|
|
|
|
empty_paths_cache: &mut EmptyPathsCache,
|
|
|
|
) -> Result<()> {
|
|
|
|
for edge_index in 0..graph.all_edges.len() as u32 {
|
|
|
|
if graph.all_edges[edge_index as usize].is_none() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let docids = edge_docids_cache
|
|
|
|
.get_edge_docids(index, txn, db_cache, edge_index, &*graph, universe)?;
|
|
|
|
match docids {
|
|
|
|
BitmapOrAllRef::Bitmap(bitmap) => {
|
|
|
|
if bitmap.is_disjoint(universe) {
|
|
|
|
graph.remove_edge(edge_index);
|
|
|
|
empty_paths_cache.forbid_edge(edge_index);
|
|
|
|
edge_docids_cache.cache.remove(&edge_index);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BitmapOrAllRef::All => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
2023-02-21 09:48:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'transaction, G: RankingRuleGraphTrait> RankingRule<'transaction, QueryGraph>
|
|
|
|
for GraphBasedRankingRule<G>
|
|
|
|
{
|
2023-02-22 15:34:37 +01:00
|
|
|
fn id(&self) -> String {
|
|
|
|
self.id.clone()
|
|
|
|
}
|
2023-02-21 09:48:49 +01:00
|
|
|
fn start_iteration(
|
|
|
|
&mut self,
|
|
|
|
index: &Index,
|
|
|
|
txn: &'transaction RoTxn,
|
|
|
|
db_cache: &mut DatabaseCache<'transaction>,
|
2023-02-28 11:49:24 +01:00
|
|
|
_logger: &mut dyn SearchLogger<QueryGraph>,
|
2023-03-02 21:27:42 +01:00
|
|
|
universe: &RoaringBitmap,
|
2023-02-21 09:48:49 +01:00
|
|
|
query_graph: &QueryGraph,
|
|
|
|
) -> Result<()> {
|
2023-02-21 13:57:34 +01:00
|
|
|
// TODO: update old state instead of starting from scratch
|
2023-03-02 21:27:42 +01:00
|
|
|
let mut graph = RankingRuleGraph::build(index, txn, db_cache, query_graph.clone())?;
|
|
|
|
let mut edge_docids_cache = EdgeDocidsCache::default();
|
|
|
|
let mut empty_paths_cache = EmptyPathsCache::new(graph.all_edges.len());
|
|
|
|
|
|
|
|
remove_empty_edges(
|
|
|
|
&mut graph,
|
|
|
|
&mut edge_docids_cache,
|
|
|
|
index,
|
|
|
|
txn,
|
|
|
|
db_cache,
|
|
|
|
universe,
|
|
|
|
&mut empty_paths_cache,
|
|
|
|
)?;
|
|
|
|
let all_distances = graph.initialize_distances_cheapest();
|
2023-02-21 09:48:49 +01:00
|
|
|
|
|
|
|
let state = GraphBasedRankingRuleState {
|
|
|
|
graph,
|
2023-03-02 21:27:42 +01:00
|
|
|
edge_docids_cache,
|
|
|
|
empty_paths_cache,
|
|
|
|
all_distances,
|
|
|
|
cur_distance_idx: 0,
|
2023-02-21 09:48:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
self.state = Some(state);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_bucket(
|
|
|
|
&mut self,
|
|
|
|
index: &Index,
|
|
|
|
txn: &'transaction RoTxn,
|
|
|
|
db_cache: &mut DatabaseCache<'transaction>,
|
2023-02-22 15:34:37 +01:00
|
|
|
logger: &mut dyn SearchLogger<QueryGraph>,
|
2023-02-21 09:48:49 +01:00
|
|
|
universe: &RoaringBitmap,
|
|
|
|
) -> Result<Option<RankingRuleOutput<QueryGraph>>> {
|
|
|
|
assert!(universe.len() > 1);
|
|
|
|
let mut state = self.state.take().unwrap();
|
2023-03-02 21:27:42 +01:00
|
|
|
remove_empty_edges(
|
|
|
|
&mut state.graph,
|
|
|
|
&mut state.edge_docids_cache,
|
|
|
|
index,
|
|
|
|
txn,
|
|
|
|
db_cache,
|
|
|
|
universe,
|
|
|
|
&mut state.empty_paths_cache,
|
|
|
|
)?;
|
2023-02-28 14:19:57 +01:00
|
|
|
|
2023-03-02 21:27:42 +01:00
|
|
|
if state.cur_distance_idx
|
|
|
|
>= state.all_distances[state.graph.query_graph.root_node as usize].len()
|
|
|
|
{
|
|
|
|
self.state = None;
|
2023-02-28 14:19:57 +01:00
|
|
|
return Ok(None);
|
|
|
|
}
|
2023-03-02 21:27:42 +01:00
|
|
|
let cost =
|
|
|
|
state.all_distances[state.graph.query_graph.root_node as usize][state.cur_distance_idx];
|
|
|
|
state.cur_distance_idx += 1;
|
|
|
|
|
|
|
|
let paths = state.graph.paths_of_cost(
|
|
|
|
state.graph.query_graph.root_node as usize,
|
|
|
|
cost,
|
|
|
|
&state.all_distances,
|
|
|
|
&state.empty_paths_cache,
|
|
|
|
);
|
|
|
|
|
|
|
|
G::log_state(
|
|
|
|
&state.graph,
|
|
|
|
&paths,
|
|
|
|
&state.empty_paths_cache,
|
|
|
|
universe,
|
|
|
|
&state.all_distances,
|
|
|
|
cost,
|
|
|
|
logger,
|
|
|
|
);
|
2023-02-23 13:13:19 +01:00
|
|
|
|
2023-02-21 09:48:49 +01:00
|
|
|
let bucket = state.graph.resolve_paths(
|
|
|
|
index,
|
|
|
|
txn,
|
|
|
|
db_cache,
|
|
|
|
&mut state.edge_docids_cache,
|
|
|
|
&mut state.empty_paths_cache,
|
|
|
|
universe,
|
|
|
|
paths,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let next_query_graph = state.graph.query_graph.clone();
|
|
|
|
|
|
|
|
self.state = Some(state);
|
|
|
|
|
|
|
|
Ok(Some(RankingRuleOutput { query: next_query_graph, candidates: bucket }))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end_iteration(
|
|
|
|
&mut self,
|
|
|
|
_index: &Index,
|
|
|
|
_txn: &'transaction RoTxn,
|
|
|
|
_db_cache: &mut DatabaseCache<'transaction>,
|
2023-02-28 11:49:24 +01:00
|
|
|
_logger: &mut dyn SearchLogger<QueryGraph>,
|
2023-02-21 09:48:49 +01:00
|
|
|
) {
|
|
|
|
self.state = None;
|
|
|
|
}
|
|
|
|
}
|