@Override public Facet facet() { TShortIntHashMap facets = aggregator.facets(); if (facets.isEmpty()) { pushFacets(facets); return new InternalShortTermsFacet( facetName, comparatorType, size, ImmutableList.<InternalShortTermsFacet.ShortEntry>of(), aggregator.missing()); } else { // we need to fetch facets of "size * numberOfShards" because of problems in how they are // distributed across shards BoundedTreeSet<InternalShortTermsFacet.ShortEntry> ordered = new BoundedTreeSet<InternalShortTermsFacet.ShortEntry>( comparatorType.comparator(), size * numberOfShards); for (TShortIntIterator it = facets.iterator(); it.hasNext(); ) { it.advance(); ordered.add(new InternalShortTermsFacet.ShortEntry(it.key(), it.value())); } pushFacets(facets); return new InternalShortTermsFacet( facetName, comparatorType, size, ordered, aggregator.missing()); } }
static TShortIntHashMap popFacets() { Deque<TShortIntHashMap> deque = cache.get().get(); if (deque.isEmpty()) { deque.add(new TShortIntHashMap()); } TShortIntHashMap facets = deque.pollFirst(); facets.clear(); return facets; }
static void pushFacets(TShortIntHashMap facets) { facets.clear(); Deque<TShortIntHashMap> deque = cache.get().get(); if (deque != null) { deque.add(facets); } }
@Override public void onValue(int docId, short value) { facets.adjustOrPutValue(value, 1, 1); }
@Override public void onValue(short value) { facets.putIfAbsent(value, 0); }