/** Resize the array to the exact provided size. */ public IntArray resize(IntArray array, long size) { if (array instanceof BigIntArray) { return resizeInPlace((BigIntArray) array, size); } else { AbstractArray arr = (AbstractArray) array; final IntArray newArray = newIntArray(size, arr.clearOnResize); for (long i = 0, end = Math.min(size, array.size()); i < end; ++i) { newArray.set(i, array.get(i)); } array.close(); return newArray; } }
/** * Utility method to increment the doc counts of the given bucket (identified by the bucket ordinal) */ public final void incrementBucketDocCount(long bucketOrd, int inc) { docCounts = bigArrays.grow(docCounts, bucketOrd + 1); docCounts.increment(bucketOrd, inc); }
public final void mergeBuckets(long[] mergeMap, long newNumBuckets) { try (IntArray oldDocCounts = docCounts) { docCounts = bigArrays.newIntArray(newNumBuckets, true); docCounts.fill(0, newNumBuckets, 0); for (int i = 0; i < oldDocCounts.size(); i++) { int docCount = oldDocCounts.get(i); if (docCount != 0) { docCounts.increment(mergeMap[i], docCount); } } } }
/** * Utility method to return the number of documents that fell in the given bucket (identified by the bucket ordinal) */ public final int bucketDocCount(long bucketOrd) { if (bucketOrd >= docCounts.size()) { // This may happen eg. if no document in the highest buckets is accepted by a sub aggregator. // For example, if there is a long terms agg on 3 terms 1,2,3 with a sub filter aggregator and if no document with 3 as a value // matches the filter, then the filter will never collect bucket ord 3. However, the long terms agg will call bucketAggregations(3) // on the filter aggregator anyway to build sub-aggregations. return 0; } else { return docCounts.get(bucketOrd); } }
private void mapSegmentCountsToGlobalCounts(LongUnaryOperator mapping) throws IOException { for (long i = 1; i < segmentDocCounts.size(); i++) { // We use set(...) here, because we need to reset the slow to 0. // segmentDocCounts get reused over the segments and otherwise counts would be too high. final int inc = segmentDocCounts.set(i, 0); if (inc == 0) { continue; } final long ord = i - 1; // remember we do +1 when counting final long globalOrd = mapping.applyAsLong(ord); long bucketOrd = bucketOrds == null ? globalOrd : bucketOrds.find(globalOrd); incrementBucketDocCount(bucketOrd, inc); } } }
/** * Returns the document count in <code>slot</code>. */ int getDocCount(int slot) { return docCounts.get(slot); }
/** * Copies the current value in <code>slot</code>. */ private void copyCurrent(int slot) { for (int i = 0; i < arrays.length; i++) { arrays[i].copyCurrent(slot); } docCounts = bigArrays.grow(docCounts, slot+1); docCounts.set(slot, 1); }
/** * Return an upper bound of the maximum bucket ordinal seen so far. */ public final long maxBucketOrd() { return docCounts.size(); }
@Override public void fill(long fromIndex, long toIndex, int value) { in.fill(fromIndex, toIndex, value); }
public IntArray values(final long bucket) { final int size = size(bucket); final IntArray values = bigArrays.newIntArray(size); if (size == 0) { return values; } int i = 0; for (int j = 0; j < capacity; ++j) { final int k = get(bucket, j); if (k != 0) { values.set(i++, k); } } assert i == values.size(); return values; }
@Override protected void removeAndAdd(long index) { final long id = id(index, -1); assert id >= 0; final int code = hashes.get(id); reset(code, id); }
private void append(long id, BytesRef key, int code) { assert size == id; final long startOffset = startOffsets.get(size); bytes = bigArrays.grow(bytes, startOffset + key.length); bytes.set(startOffset, key.bytes, key.offset, key.length); startOffsets = bigArrays.grow(startOffsets, size + 2); startOffsets.set(size + 1, startOffset + key.length); hashes = bigArrays.grow(hashes, id + 1); hashes.set(id, code); }
/** Grow an array to a size that is larger than <code>minSize</code>, * preserving content, and potentially reusing part of the provided array. */ public IntArray grow(IntArray array, long minSize) { if (minSize <= array.size()) { return array; } final long newSize = overSize(minSize, PageCacheRecycler.INT_PAGE_SIZE, Integer.BYTES); return resize(array, newSize); }
/** Resize the array to the exact provided size. */ public IntArray resize(IntArray array, long size) { if (array instanceof BigIntArray) { return resizeInPlace((BigIntArray) array, size); } else { AbstractArray arr = (AbstractArray) array; final IntArray newArray = newIntArray(size, arr.clearOnResize); for (long i = 0, end = Math.min(size, array.size()); i < end; ++i) { newArray.set(i, array.get(i)); } array.close(); return newArray; } }
private Object getComparableData(long bucket) { if (algorithm.get(bucket) == LINEAR_COUNTING) { Set<Integer> values = new HashSet<>(); try (IntArray hashSetValues = hashSet.values(bucket)) { for (long i = 0; i < hashSetValues.size(); i++) { values.add(hashSetValues.get(i)); } } return values; } else { Map<Byte, Integer> values = new HashMap<>(); for (long i = 0; i < runLens.size(); i++) { byte runLength = runLens.get((bucket << p) + i); Integer numOccurances = values.get(runLength); if (numOccurances == null) { values.put(runLength, 1); } else { values.put(runLength, numOccurances + 1); } } return values; } }
public IntArray values(final long bucket) { final int size = size(bucket); final IntArray values = bigArrays.newIntArray(size); if (size == 0) { return values; } int i = 0; for (int j = 0; j < capacity; ++j) { final int k = get(bucket, j); if (k != 0) { values.set(i++, k); } } assert i == values.size(); return values; }
/** * Same as {@link #collectBucket(LeafBucketCollector, int, long)}, but doesn't check if the docCounts needs to be re-sized. */ public final void collectExistingBucket(LeafBucketCollector subCollector, int doc, long bucketOrd) throws IOException { docCounts.increment(bucketOrd, 1); subCollector.collect(doc, bucketOrd); }