use super::super::ScoringAttachment;
use crate::{HawDBError, Result};
use hawdb_executor::pipeline::BatchControl;
use hawdb_executor::scoring::{HostScorer, HostScorerProvider};
use hawdb_plan_cypher::HostScoringPlan;
use std::num::NonZeroUsize;
use std::sync::Mutex;
pub struct OwnedHostScoringRequest {
plan: HostScoringPlan,
candidate_window: bool,
reference_time_millis: Option<u64>,
scorer: Mutex<Box<dyn HostScorer + Send>>,
}
impl OwnedHostScoringRequest {
pub fn new(
scorer: impl HostScorer + Send + 'static,
score_column: impl Into<String>,
max_candidate_rows: NonZeroUsize,
limit: usize,
) -> Result<Self> {
let descriptor = scorer.descriptor();
let plan = HostScoringPlan::new(
descriptor.name(),
descriptor.version(),
descriptor.cpu_units_per_row(),
score_column,
max_candidate_rows,
limit,
)?;
Ok(Self {
plan,
candidate_window: false,
reference_time_millis: None,
scorer: Mutex::new(Box::new(scorer)),
})
}
pub fn with_candidate_window(mut self) -> Self {
self.candidate_window = true;
self
}
pub fn with_reference_time_millis(mut self, time: u64) -> Self {
self.reference_time_millis = Some(time);
self
}
pub fn with_contiguous_evidence_ties(
mut self,
reason_column: impl Into<String>,
) -> Result<Self> {
self.plan = self.plan.with_contiguous_evidence_ties(reason_column)?;
Ok(self)
}
pub fn with_vector_graph_input(
mut self,
seed_variable: impl Into<String>,
candidate_variable: impl Into<String>,
) -> Result<Self> {
self.plan = self
.plan
.with_vector_graph_input(seed_variable, candidate_variable)?;
Ok(self)
}
pub fn with_graph_seed_input(
mut self,
seed_variable: impl Into<String>,
candidate_variable: impl Into<String>,
) -> Result<Self> {
self.plan = self
.plan
.with_graph_seed_input(seed_variable, candidate_variable)?;
Ok(self)
}
pub fn with_text_graph_input(
mut self,
seed_variable: impl Into<String>,
candidate_variable: impl Into<String>,
) -> Result<Self> {
self.plan = self
.plan
.with_text_graph_input(seed_variable, candidate_variable)?;
Ok(self)
}
pub(super) fn attachment(&self) -> ScoringAttachment<'_> {
ScoringAttachment::Host {
plan: &self.plan,
candidate_window: self.candidate_window,
reference_time_millis: self.reference_time_millis,
provider: self,
}
}
}
impl HostScorerProvider for OwnedHostScoringRequest {
fn with_scorer(
&self,
run: &mut dyn FnMut(&mut dyn HostScorer) -> Result<BatchControl>,
) -> Result<BatchControl> {
let mut scorer = self.scorer.try_lock().map_err(|error| {
HawDBError::Execution(format!("owned host scorer is unavailable: {error}"))
})?;
run(&mut **scorer)
}
}