private <T> T registerNewPage(Recycler.V<T> v, int page, int expectedSize) {
   cache = grow(cache, page + 1);
   assert cache[page] == null;
   cache[page] = v;
   assert Array.getLength(v.v()) == expectedSize;
   return v.v();
 }
  public TermsLongFacetExecutor(
      IndexNumericFieldData indexFieldData,
      int size,
      int shardSize,
      TermsFacet.ComparatorType comparatorType,
      boolean allTerms,
      SearchContext context,
      ImmutableSet<BytesRef> excluded,
      SearchScript script,
      CacheRecycler cacheRecycler) {
    this.indexFieldData = indexFieldData;
    this.size = size;
    this.shardSize = shardSize;
    this.comparatorType = comparatorType;
    this.script = script;
    this.excluded = excluded;
    this.facets = cacheRecycler.longIntMap(-1);

    if (allTerms) {
      for (AtomicReaderContext readerContext : context.searcher().getTopReaderContext().leaves()) {
        int maxDoc = readerContext.reader().maxDoc();
        LongValues values = indexFieldData.load(readerContext).getLongValues();
        for (int docId = 0; docId < maxDoc; docId++) {
          final int numValues = values.setDocument(docId);
          final LongIntOpenHashMap v = facets.v();
          for (int i = 0; i < numValues; i++) {
            v.putIfAbsent(values.nextValue(), 0);
          }
        }
      }
    }
  }
 @Override
 public InternalFacet buildFacet(String facetName) {
   if (facets.v().isEmpty()) {
     facets.close();
     return new InternalLongTermsFacet(
         facetName,
         comparatorType,
         size,
         ImmutableList.<InternalLongTermsFacet.LongEntry>of(),
         missing,
         total);
   } else {
     LongIntOpenHashMap facetEntries = facets.v();
     final boolean[] states = facets.v().allocated;
     final long[] keys = facets.v().keys;
     final int[] values = facets.v().values;
     if (size < EntryPriorityQueue.LIMIT) {
       EntryPriorityQueue ordered = new EntryPriorityQueue(shardSize, comparatorType.comparator());
       for (int i = 0; i < states.length; i++) {
         if (states[i]) {
           ordered.insertWithOverflow(new InternalLongTermsFacet.LongEntry(keys[i], values[i]));
         }
       }
       InternalLongTermsFacet.LongEntry[] list =
           new InternalLongTermsFacet.LongEntry[ordered.size()];
       for (int i = ordered.size() - 1; i >= 0; i--) {
         list[i] = (InternalLongTermsFacet.LongEntry) ordered.pop();
       }
       facets.close();
       return new InternalLongTermsFacet(
           facetName, comparatorType, size, Arrays.asList(list), missing, total);
     } else {
       BoundedTreeSet<InternalLongTermsFacet.LongEntry> ordered =
           new BoundedTreeSet<>(comparatorType.comparator(), shardSize);
       for (int i = 0; i < states.length; i++) {
         if (states[i]) {
           ordered.add(new InternalLongTermsFacet.LongEntry(keys[i], values[i]));
         }
       }
       facets.close();
       return new InternalLongTermsFacet(facetName, comparatorType, size, ordered, missing, total);
     }
   }
 }