use crate::core::search::bulk_scorer::BulkScorer;
use crate::core::search::disi_wrapper::DisiWrapper;
use crate::core::search::doc_id_stream::{DocIdStream, DocIdStreamConsumer};
use crate::core::search::scorable::Scorable;
use crate::core::search::score::Score;
use crate::core::search::scorer::Scorer;
use crate::core::search::scorer_util::ScorerUtil;
use crate::core::util::bits::Bits;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::priority_queue::{Compare, PriorityQueue};
pub(crate) const SHIFT: usize = 12;
pub(crate) const SIZE: usize = 1 << SHIFT;
pub(crate) const MASK: usize = SIZE - 1;
pub(crate) const SET_SIZE: usize = 1 << (SHIFT - 6);
#[allow(dead_code)]
pub(crate) const SET_MASK: usize = SET_SIZE - 1;
pub struct BooleanScorer<S> {
pub(crate) buckets: Option<Vec<Bucket>>,
pub(crate) matching: [u64; SET_SIZE],
pub(crate) head: PriorityQueue<DisiWrapper<S>, HeadPriorityQueueCmp>,
pub(crate) tail: PriorityQueue<DisiWrapper<S>, TailPriorityQueueCmp>,
leads: Vec<DisiWrapper<S>>,
pub(crate) score: Score,
pub(crate) min_should_match: usize,
pub(crate) cost: i64,
pub(crate) needs_scores: bool,
word_cache_min_cost: i64,
}
impl<S> BooleanScorer<S>
where
S: Scorer,
{
pub(crate) fn new(
scorers: Vec<S>,
min_should_match: usize,
needs_scores: bool,
max_doc: i32,
) -> Result<Self> {
if min_should_match < 1 || min_should_match > scorers.len() {
return Err(LuceneError::illegal_argument(format!(
"minShouldMatch should be within 1..num_scorers. Got {}",
min_should_match
)));
}
if scorers.len() <= 1 {
return Err(LuceneError::illegal_argument(format!(
"This scorer can only be used with two scorers or more, got {}",
scorers.len()
)));
}
let buckets = if needs_scores || min_should_match > 1 {
Some((0..SIZE).map(|_| Bucket::new()).collect())
} else {
None
};
let matching = [0u64; SET_SIZE];
let head_size = scorers.len() - min_should_match + 1;
let tail_size = min_should_match - 1;
let mut head = PriorityQueue::new(head_size, HeadPriorityQueueCmp)?;
let mut tail = PriorityQueue::new(tail_size, TailPriorityQueueCmp)?;
for s in scorers {
let w = DisiWrapper::new(s)?;
if let Some(evicted) = tail.insert_with_overflow(w)? {
head.add(evicted)?;
}
}
let cost = ScorerUtil::cost_with_min_should_match(
head.iter_ref().chain(tail.iter_ref()).map(|w| w.cost),
head_size + tail_size,
min_should_match,
)?;
Ok(Self {
buckets,
matching,
head,
tail,
leads: Vec::with_capacity(head_size + tail_size),
score: Score::new(0.0),
min_should_match,
cost,
needs_scores,
word_cache_min_cost: (i64::from(max_doc) + 7) / 8,
})
}
fn score_disi_wrapper_into_bitset<AB: Bits + ?Sized>(
&mut self,
w: &mut DisiWrapper<S>,
accept_docs: Option<&AB>,
min: i32,
max: i32,
) -> Result<()> {
let mut doc = w.doc;
if doc < min {
doc = w.scorer.advance(min)?;
}
if self.buckets.is_none() && accept_docs.is_none() && w.cost >= self.word_cache_min_cost {
while doc < max {
let index = (doc as usize & MASK) >> 6;
let word_end = max.min((doc | 0x3f).saturating_add(1));
let matching = &mut self.matching[index];
let mut bits = *matching;
while doc < word_end {
bits |= 1u64 << (doc as usize & 0x3f);
*matching = bits;
doc = w.scorer.next_doc()?;
}
}
w.doc = doc;
return Ok(());
}
while doc < max {
let doc_index = doc as usize;
let accepted = match accept_docs {
Some(accept_docs) => accept_docs.get(doc_index)?,
None => true,
};
if accepted {
let i = doc_index & MASK;
let idx = i >> 6;
self.matching[idx] |= 1u64 << (i & 0x3f);
if let Some(ref mut buckets) = self.buckets {
let bucket = &mut buckets[i];
bucket.freq += 1;
if self.needs_scores {
bucket.score += w.scorer.score()? as f64;
}
}
}
doc = w.scorer.next_doc()?;
}
w.doc = doc;
Ok(())
}
#[allow(clippy::too_many_arguments)]
fn score_window_into_bitset_and_replay<
CLC: crate::core::search::leaf_collector::LeafCollector,
AB: Bits + ?Sized,
>(
&mut self,
collector: &mut CLC,
accept_docs: Option<&AB>,
base: i32,
min: i32,
max: i32,
scorers: &mut [DisiWrapper<S>],
num_scorers: usize,
) -> Result<()> {
for w in scorers.iter_mut().take(num_scorers) {
debug_assert!(w.doc < max);
self.score_disi_wrapper_into_bitset(w, accept_docs, min, max)?;
}
let mut score = std::mem::take(&mut self.score);
let mut stream = DocIdStreamView::new(self, base);
let result = collector.collect_stream(&mut stream, &mut score);
self.score = score;
result?;
for m in self.matching.iter_mut() {
*m = 0;
}
Ok(())
}
fn advance(&mut self, min: i32) -> Result<&DisiWrapper<S>> {
debug_assert!(self.tail.size() == (self.min_should_match - 1));
match self.head.take_top() {
None => return Err(LuceneError::illegal_state("head queue is empty")),
Some(mut head_top) => {
while head_top.doc < min {
match self.tail.take_top() {
None => {
let v = head_top.scorer.advance(min)?;
head_top.doc = v;
let _ = self.head.update_top_with_new_top(head_top)?;
head_top = self
.head
.take_top()
.ok_or_else(|| LuceneError::illegal_state("head queue is empty after update"))?;
},
Some(mut tail_top) => {
if head_top.cost <= tail_top.cost {
let v = head_top.scorer.advance(min)?;
head_top.doc = v;
let _ = self.head.update_top_with_new_top(head_top)?;
head_top = self
.head
.take_top()
.ok_or_else(|| LuceneError::illegal_state("head queue is empty after update"))?;
self.tail.update_top_with_new_top(tail_top)?;
} else {
let v = tail_top.scorer.advance(min)?;
tail_top.doc = v;
let _ = self.head.update_top_with_new_top(tail_top)?;
let _ = self.tail.update_top_with_new_top(head_top)?;
head_top = self
.head
.take_top()
.ok_or_else(|| LuceneError::illegal_state("head queue is empty after update"))?;
}
},
}
}
self.head.update_top_with_new_top(head_top)?;
},
}
match self.head.top() {
None => Err(LuceneError::illegal_state("head queue is empty")),
Some(top) => Ok(top),
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn score_window_multiple_scorers<
CLC: crate::core::search::leaf_collector::LeafCollector,
AB: Bits + ?Sized,
>(
&mut self,
collector: &mut CLC,
accept_docs: Option<&AB>,
window_base: i32,
window_min: i32,
window_max: i32,
mut max_freq: usize,
leads: &mut Vec<DisiWrapper<S>>,
) -> Result<()> {
while max_freq < self.min_should_match && max_freq + self.tail.size() >= self.min_should_match {
let mut candidate = self
.tail
.pop()?
.ok_or_else(|| LuceneError::illegal_state("tail.pop returned None"))?;
if candidate.doc < window_min {
let new_doc = candidate.scorer.advance(window_min)?;
candidate.doc = new_doc;
}
if candidate.doc < window_max {
leads.push(candidate);
max_freq += 1;
} else {
self.head.add(candidate)?;
}
}
if max_freq >= self.min_should_match {
for x in self.tail.get().into_iter() {
leads.push(x);
max_freq += 1;
}
self.tail.clear();
self.score_window_into_bitset_and_replay(
collector,
accept_docs,
window_base,
window_min,
window_max,
leads.as_mut_slice(),
max_freq,
)?;
}
for v in leads.drain(..) {
let evicted = self.head.insert_with_overflow(v)?;
if let Some(e) = evicted {
self.tail.add(e)?;
}
}
Ok(())
}
pub(crate) fn score_window_single_scorer<
CLC: crate::core::search::leaf_collector::LeafCollector,
AB: Bits + ?Sized,
>(
&mut self,
w: &mut DisiWrapper<S>,
collector: &mut CLC,
accept_docs: Option<&AB>,
window_min: i32,
window_max: i32,
max_doc: i32,
) -> Result<()> {
debug_assert!(self.tail.size() == 0);
let next_window_base = match self.head.top() {
None => return Err(LuceneError::illegal_state("head queue is empty")),
Some(top) => top.doc & !(MASK as i32),
};
let end = std::cmp::max(window_max, std::cmp::min(max_doc, next_window_base));
let doc = {
let mut doc;
{
doc = w.doc;
let it = &mut w.scorer;
if doc < window_min {
doc = it.advance(window_min)?;
}
}
collector.set_scorer(&mut w.scorer)?;
while doc < end {
let accepted = match accept_docs {
None => true,
Some(bits) => bits.get(doc as usize)?,
};
if accepted {
collector.collect(doc, &mut w.scorer)?;
}
doc = w.scorer.next_doc()?;
}
doc
};
w.doc = doc;
collector.set_scorer(&mut self.score)?;
Ok(())
}
pub(crate) fn score_window<
CLC: crate::core::search::leaf_collector::LeafCollector,
AB: Bits + ?Sized,
>(
&mut self,
top_doc: i32,
collector: &mut CLC,
accept_docs: Option<&AB>,
min: i32,
max: i32,
) -> Result<&DisiWrapper<S>> {
let window_base = top_doc & !(MASK as i32);
let window_min = std::cmp::max(min, window_base);
let window_max = std::cmp::min(max, window_base + SIZE as i32);
let mut leads = std::mem::take(&mut self.leads);
let head_top = self
.head
.pop()?
.ok_or_else(|| LuceneError::illegal_state("head's top() returned None"))?;
leads.push(head_top);
let mut max_freq = 1usize;
while self.head.size() > 0 {
let head_top_doc = self
.head
.top()
.ok_or_else(|| LuceneError::illegal_state("head's top() returned None"))?
.doc;
if head_top_doc >= window_max {
break;
}
let w = self
.head
.pop()?
.ok_or_else(|| LuceneError::illegal_state("head's top() returned None"))?;
leads.push(w);
max_freq += 1;
}
if self.min_should_match == 1 && max_freq == 1 {
let mut bulk_scorer = leads.remove(0);
self.score_window_single_scorer(
&mut bulk_scorer,
collector,
accept_docs,
window_min,
window_max,
max,
)?;
let top = self.head.add(bulk_scorer)?;
self.leads = leads;
return Ok(top);
}
self.score_window_multiple_scorers(
collector,
accept_docs,
window_base,
window_min,
window_max,
max_freq,
&mut leads,
)?;
let top = self
.head
.top()
.ok_or_else(|| LuceneError::illegal_state("head's top() returned None"))?;
self.leads = leads;
Ok(top)
}
}
impl<S> BulkScorer for BooleanScorer<S>
where
S: Scorer,
{
fn score<CLC: crate::core::search::leaf_collector::LeafCollector, AB: Bits + ?Sized>(
&mut self,
collector: &mut CLC,
accept_docs: Option<&AB>,
min: i32,
max: i32,
) -> Result<i32> {
collector.set_scorer(&mut self.score)?;
let mut top = self.advance(min)?;
let mut doc = top.doc;
while doc < max {
top = self.score_window(doc, collector, accept_docs, min, max)?;
doc = top.doc;
}
Ok(top.doc)
}
fn cost(&mut self) -> Result<i64> {
Ok(self.cost)
}
}
pub struct HeadPriorityQueueCmp;
impl<S> Compare<DisiWrapper<S>> for HeadPriorityQueueCmp
where
S: Scorer,
{
fn less_than(&self, a: &DisiWrapper<S>, b: &DisiWrapper<S>) -> Result<bool> {
Ok(a.doc < b.doc)
}
}
pub struct TailPriorityQueueCmp;
impl<S> Compare<DisiWrapper<S>> for TailPriorityQueueCmp
where
S: Scorer,
{
fn less_than(&self, a: &DisiWrapper<S>, b: &DisiWrapper<S>) -> Result<bool> {
Ok(a.cost < b.cost)
}
}
pub struct Bucket {
score: f64,
freq: usize,
}
impl Bucket {
fn new() -> Self {
Self {
score: 0.0,
freq: 0,
}
}
}
impl<S> PriorityQueue<DisiWrapper<S>, TailPriorityQueueCmp>
where
S: Scorer,
{
fn get(&mut self) -> Vec<DisiWrapper<S>> {
self.take_heap_array()
}
}
struct DocIdStreamView<'a, S> {
scorer: &'a mut BooleanScorer<S>,
base: i32,
}
impl<'a, S> DocIdStreamView<'a, S>
where
S: Scorer,
{
fn new(scorer: &'a mut BooleanScorer<S>, base: i32) -> Self {
Self { scorer, base }
}
}
impl<'a, S> DocIdStream for DocIdStreamView<'a, S>
where
S: Scorer,
{
fn for_each<DC: DocIdStreamConsumer + ?Sized>(&mut self, consumer: &mut DC) -> Result<()> {
for (idx, bits_ref) in self.scorer.matching.iter().enumerate() {
let mut bits = *bits_ref;
while bits != 0 {
let ntz = bits.trailing_zeros() as usize;
let index_in_window = (idx << 6) | ntz;
match self.scorer.buckets {
Some(ref mut buckets) => {
let bucket = &mut buckets[index_in_window];
if bucket.freq >= self.scorer.min_should_match {
consumer
.accept_with_score(self.base | index_in_window as i32, bucket.score as f32)?;
}
bucket.freq = 0;
bucket.score = 0.0;
},
None => {
consumer.accept(self.base | index_in_window as i32)?;
},
}
bits &= bits - 1;
}
}
Ok(())
}
fn count<SC: Scorable + ?Sized>(&mut self, scorer: &mut SC) -> Result<i32> {
if self.scorer.min_should_match > 1 {
return self.default_count(scorer);
}
let count: i32 = self
.scorer
.matching
.iter()
.map(|l| l.count_ones() as i32)
.sum();
Ok(count)
}
}