package org.apache.lucene.search;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
final class CoveringScorer extends Scorer {
final int numScorers;
final int maxDoc;
final LongValues minMatchValues;
boolean matches;
int doc;
DisiWrapper topList;
int freq;
long minMatch;
final DisiPriorityQueue subScorers;
final long cost;
CoveringScorer(Weight weight, Collection<Scorer> scorers, LongValues minMatchValues, int maxDoc) {
super(weight);
this.numScorers = scorers.size();
this.maxDoc = maxDoc;
this.minMatchValues = minMatchValues;
this.doc = -1;
subScorers = new DisiPriorityQueue(scorers.size());
for (Scorer scorer : scorers) {
subScorers.add(new DisiWrapper(scorer));
}
this.cost = scorers.stream().map(Scorer::iterator).mapToLong(DocIdSetIterator::cost).sum();
}
@Override
public final Collection<ChildScorable> getChildren() throws IOException {
List<ChildScorable> matchingChildren = new ArrayList<>();
setTopListAndFreqIfNecessary();
for (DisiWrapper s = topList; s != null; s = s.next) {
matchingChildren.add(new ChildScorable(s.scorer, "SHOULD"));
}
return matchingChildren;
}
private final DocIdSetIterator approximation = new DocIdSetIterator() {
@Override
public int docID() {
return doc;
}
@Override
public int nextDoc() throws IOException {
return advance(docID() + 1);
}
@Override
public int advance(int target) throws IOException {
matches = false;
topList = null;
doc = target;
setMinMatch();
DisiWrapper top = subScorers.top();
int numMatches = 0;
int maxPotentialMatches = numScorers;
while (top.doc < target) {
if (maxPotentialMatches < minMatch) {
if (target >= maxDoc - 1) {
doc = NO_MORE_DOCS;
} else {
doc = target + 1;
}
setMinMatch();
return doc;
}
top.doc = top.iterator.advance(target);
boolean match = top.doc == target;
top = subScorers.updateTop();
if (match) {
numMatches++;
if (numMatches >= minMatch) {
matches = true;
return doc;
}
} else {
maxPotentialMatches--;
}
}
doc = top.doc;
setMinMatch();
return doc;
}
private void setMinMatch() throws IOException {
if (doc >= maxDoc) {
minMatch = 1;
} else if (minMatchValues.advanceExact(doc)) {
minMatch = Math.max(1, minMatchValues.longValue());
} else {
minMatch = Long.MAX_VALUE;
}
}
@Override
public long cost() {
return maxDoc;
}
};
private final TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) {
@Override
public boolean matches() throws IOException {
if (matches) {
return true;
}
if (topList == null) {
advanceAll(doc);
}
if (subScorers.top().doc != doc) {
assert subScorers.top().doc > doc;
return false;
}
setTopListAndFreq();
assert topList.doc == doc;
return matches = freq >= minMatch;
}
@Override
public float matchCost() {
return numScorers;
}
};
@Override
public DocIdSetIterator iterator() {
return TwoPhaseIterator.asDocIdSetIterator(twoPhase);
}
@Override
public TwoPhaseIterator twoPhaseIterator() {
return twoPhase;
}
private void advanceAll(int target) throws IOException {
DisiWrapper top = subScorers.top();
while (top.doc < target) {
top.doc = top.iterator.advance(target);
top = subScorers.updateTop();
}
}
private void setTopListAndFreq() {
topList = subScorers.topList();
freq = 0;
for (DisiWrapper w = topList; w != null; w = w.next) {
freq++;
}
}
private void setTopListAndFreqIfNecessary() throws IOException {
if (topList == null) {
advanceAll(doc);
setTopListAndFreq();
}
}
@Override
public float score() throws IOException {
setTopListAndFreqIfNecessary();
double score = 0;
for (DisiWrapper w = topList; w != null; w = w.next) {
score += w.scorer.score();
}
return (float) score;
}
@Override
public float getMaxScore(int upTo) throws IOException {
return Float.POSITIVE_INFINITY;
}
@Override
public int docID() {
return doc;
}
}