@Override protected void doCollect(int doc) throws IOException {
fieldData.forEachValueInDoc(doc, aggregator);
}
@Override public Facet facet() {
TByteIntHashMap facets = aggregator.facets();
if (facets.isEmpty()) {
CacheRecycler.pushByteIntMap(facets);
return new InternalByteTermsFacet(facetName, comparatorType, size, ImmutableList.<InternalByteTermsFacet.ByteEntry>of(), aggregator.missing(), aggregator.total());
} else {
if (size < EntryPriorityQueue.LIMIT) {
EntryPriorityQueue ordered = new EntryPriorityQueue(size, comparatorType.comparator());
for (TByteIntIterator it = facets.iterator(); it.hasNext(); ) {
it.advance();
ordered.insertWithOverflow(new InternalByteTermsFacet.ByteEntry(it.key(), it.value()));
}
InternalByteTermsFacet.ByteEntry[] list = new InternalByteTermsFacet.ByteEntry[ordered.size()];
for (int i = ordered.size() - 1; i >= 0; i--) {
list[i] = (InternalByteTermsFacet.ByteEntry) ordered.pop();
}
CacheRecycler.pushByteIntMap(facets);
return new InternalByteTermsFacet(facetName, comparatorType, size, Arrays.asList(list), aggregator.missing(), aggregator.total());
} else {
BoundedTreeSet<InternalByteTermsFacet.ByteEntry> ordered = new BoundedTreeSet<InternalByteTermsFacet.ByteEntry>(comparatorType.comparator(), size);
for (TByteIntIterator it = facets.iterator(); it.hasNext(); ) {
it.advance();
ordered.add(new InternalByteTermsFacet.ByteEntry(it.key(), it.value()));
}
CacheRecycler.pushByteIntMap(facets);
return new InternalByteTermsFacet(facetName, comparatorType, size, ordered, aggregator.missing(), aggregator.total());