{
SortCollector sortCollector = res.getSortCollector();
if (sortCollector == null) continue;
int end = (res.getNumHits() % SortCollector.BLOCK_SIZE) - 1;
Iterator<CollectorContext> contextIter = sortCollector.contextList.descendingIterator();
CollectorContext currentContext = null;
int contextLeft = 0;
FacetDataCache[] dataCaches = new FacetDataCache[sortCollector.groupByMulti.length];
FacetDataCache[] distinctDataCaches = new FacetDataCache[distinctLength];
while (contextIter.hasNext()) {
currentContext = contextIter.next();
currentContext.restoreRuntimeFacets();
contextLeft = currentContext.length;
if (contextLeft > 0)
{
for (j=0; j<sortCollector.groupByMulti.length; ++j)
{
dataCaches[j] = (FacetDataCache)sortCollector.groupByMulti[j].getFacetData(currentContext.reader);
}
for (j=0; j<distinctLength; ++j)
{
distinctDataCaches[j] = (FacetDataCache)currentContext.
reader.
getFacetHandler(req.getDistinct()[j]).
getFacetData(currentContext.reader);
}
break;
}
}
Iterator<float[]> scoreArrayIter = sortCollector.scorearraylist != null ? sortCollector.scorearraylist.descendingIterator():null;
if (contextLeft > 0)
{
Iterator<int[]> docArrayIter = sortCollector.docidarraylist.descendingIterator();
while (docArrayIter.hasNext())
{
int[] docs = docArrayIter.next();
float[] scores = scoreArrayIter != null ? scoreArrayIter.next():null;
for (i = end; i >= 0; --i)
{
doc = docs[i];
score = scores != null ? scores[i]:0.0f;
for (j=0; j<sortCollector.groupByMulti.length; ++j)
{
rawGroupValue = extractRawGroupValue(rawGroupValueType, j, primitiveLongArrayWrapperTmp,
dataCaches[j].valArray.getRawValue(dataCaches[j].orderArray.get(doc)));
hitWithGroupQueue = groupMaps[j].get(rawGroupValue);
if (hitWithGroupQueue != null)
{
hitWithGroupQueue.hit.setGroupHitsCount(hitWithGroupQueue.hit.getGroupHitsCount() + 1);
if (hitWithGroupQueue.queue == null) break;
// Collect this hit.
if (tmpScoreDoc == null)
tmpScoreDoc = new MyScoreDoc(doc, score, currentContext.base + totalDocs + doc, currentContext.reader);
else
{
tmpScoreDoc.doc = doc;
tmpScoreDoc.score = score;
tmpScoreDoc.finalDoc = currentContext.base + totalDocs + doc;
tmpScoreDoc.reader = currentContext.reader;
}
tmpScoreDoc.sortValue = currentContext.comparator.value(tmpScoreDoc);
tmpScoreDoc.groupPos = j;
if (distinctLength > 0)
{
tmpScoreDoc.distinctValues = new Object[distinctLength];
}
if (hitWithGroupQueue.queue.size < maxPerGroup ||
tmpScoreDoc.sortValue.compareTo(((MyScoreDoc)hitWithGroupQueue.queue.top()).sortValue) < 0)
{
pre = null;
for (k = 0; k < distinctLength; ++k)
{
tmpScoreDoc.distinctValues[k] = extractRawGroupValue(distinctValueType,
k,
distinctPrimitiveLongArrayWrapperTmp,
distinctDataCaches[k].valArray.getRawValue(
distinctDataCaches[k].orderArray.get(doc)
));
if (pre == null)
pre = hitWithGroupQueue.distinctMap[k].get(tmpScoreDoc.distinctValues[k]);
}
if (pre != null)
{
if (tmpScoreDoc.sortValue.compareTo(pre.sortValue) < 0)
{
hitWithGroupQueue.queue.replace(tmpScoreDoc, pre);
for (k = 0; k < distinctLength; ++k)
{
hitWithGroupQueue.distinctMap[k].remove(pre.distinctValues[k]);
hitWithGroupQueue.distinctMap[k].put(tmpScoreDoc.distinctValues[k], tmpScoreDoc);
}
tmpScoreDoc = pre;
}
}
else
{
if (hitWithGroupQueue.queue.size >= maxPerGroup) // queue full
{
pre = (MyScoreDoc)hitWithGroupQueue.queue.top();
hitWithGroupQueue.queue.replace(tmpScoreDoc);
for (k = 0; k < distinctLength; ++k)
{
hitWithGroupQueue.distinctMap[k].remove(pre.distinctValues[k]);
hitWithGroupQueue.distinctMap[k].put(tmpScoreDoc.distinctValues[k], tmpScoreDoc);
}
tmpScoreDoc = pre;
}
else
{
hitWithGroupQueue.queue.add(tmpScoreDoc);
for (k = 0; k < distinctLength; ++k)
{
hitWithGroupQueue.distinctMap[k].put(tmpScoreDoc.distinctValues[k], tmpScoreDoc);
}
tmpScoreDoc = null;
}
}
}
break;
}
}
--contextLeft;
if (contextLeft <= 0)
{
while (contextIter.hasNext()) {
currentContext = contextIter.next();
currentContext.restoreRuntimeFacets();
contextLeft = currentContext.length;
if (contextLeft > 0)
{
for (j=0; j<sortCollector.groupByMulti.length; ++j)
{