use super::{CancellationToken, CapturedScoredEntries, SQLError, ScoredEntry, ScoredEntryCapture};
impl CapturedScoredEntries {
pub(crate) fn merge_text_scores(
self,
right: Self,
intersection: bool,
bytes: usize,
cancellation: CancellationToken,
) -> Result<Self, SQLError> {
self.merge_ordered_scores(right, intersection, bytes, cancellation, |left, right| {
left + right
})
}
pub(crate) fn merge_field_scores(
self,
right: Self,
bytes: usize,
cancellation: CancellationToken,
) -> Result<Self, SQLError> {
self.merge_ordered_scores(right, false, bytes, cancellation, f64::max)
}
fn merge_ordered_scores(
mut self,
mut right: Self,
intersection: bool,
bytes: usize,
cancellation: CancellationToken,
merge: impl Fn(f64, f64) -> f64,
) -> Result<Self, SQLError> {
let mut output = ScoredEntryCapture::new(bytes, cancellation)?;
let mut left_entry = self.next().transpose()?;
let mut right_entry = right.next().transpose()?;
while let (Some(left), Some(other)) = (left_entry.as_ref(), right_entry.as_ref()) {
match left.doc_id.cmp(&other.doc_id) {
std::cmp::Ordering::Equal => {
output.push(ScoredEntry {
doc_id: left.doc_id,
score: merge(left.score, other.score),
})?;
left_entry = self.next().transpose()?;
right_entry = right.next().transpose()?;
}
std::cmp::Ordering::Less => {
if !intersection {
output.push(left.clone())?;
}
left_entry = self.next().transpose()?;
}
std::cmp::Ordering::Greater => {
if !intersection {
output.push(other.clone())?;
}
right_entry = right.next().transpose()?;
}
}
}
if !intersection {
for entry in left_entry.into_iter().map(Ok).chain(self) {
output.push(entry?)?;
}
for entry in right_entry.into_iter().map(Ok).chain(right) {
output.push(entry?)?;
}
}
output.finish()
}
}