x-algorithm/candidate-pipeline/hydrator.rs
2026-01-20 02:31:49 +00:00

40 lines
1.4 KiB
Rust

use crate::util;
use std::any::{Any, type_name_of_val};
use tonic::async_trait;
// Hydrators run in parallel and update candidate fields
#[async_trait]
pub trait Hydrator<Q, C>: Any + Send + Sync
where
Q: Clone + Send + Sync + 'static,
C: Clone + Send + Sync + 'static,
{
/// Decide if this hydrator should run for the given query
fn enable(&self, _query: &Q) -> bool {
true
}
/// Hydrate candidates by performing async operations.
/// Returns candidates with this hydrator's fields populated.
///
/// IMPORTANT: The returned vector must have the same candidates in the same order as the input.
/// Dropping candidates in a hydrator is not allowed - use a filter stage instead.
async fn hydrate(&self, query: &Q, candidates: &[C]) -> Result<Vec<C>, String>;
/// Update a single candidate with the hydrated fields.
/// Only the fields this hydrator is responsible for should be copied.
fn update(&self, candidate: &mut C, hydrated: C);
/// Update all candidates with the hydrated fields from `hydrated`.
/// Default implementation iterates and calls `update` for each pair.
fn update_all(&self, candidates: &mut [C], hydrated: Vec<C>) {
for (c, h) in candidates.iter_mut().zip(hydrated) {
self.update(c, h);
}
}
fn name(&self) -> &'static str {
util::short_type_name(type_name_of_val(self))
}
}