Class BytesRefTopNSet

java.lang.Object
org.elasticsearch.compute.data.sort.BytesRefTopNSet
All Implemented Interfaces:
Closeable, AutoCloseable, Releasable

public class BytesRefTopNSet extends Object implements Releasable
Aggregates the top N collected BytesRef values, kept sorted.

Collection is O(1) for values out of the current top N. For values better than the worst value, it's O(log(n)) for the binary search plus O(n) for the shift on insertion (n is bounded by limit).

Backing storage is an array of BreakingBytesRefBuilder, one per slot, sized at most limit. Each slot owns its bytes; on insertion, references are shifted right and the displaced (worst) slot is reused as the destination for the new value.

  • Constructor Details

    • BytesRefTopNSet

      public BytesRefTopNSet(BigArrays bigArrays, SortOrder order, int limit)
  • Method Details

    • collect

      public boolean collect(org.apache.lucene.util.BytesRef value)
      Adds the value to the top N, as long as it is "better" than the worst value, or the top isn't full yet.
    • reduceLimitByOne

      public void reduceLimitByOne()
      Reduces the limit of the top N by 1.

      This method is specifically used to count for the null value, and ignore the extra element here without extra cost.

    • getWorstValue

      public org.apache.lucene.util.BytesRef getWorstValue()
      Returns a deep copy of the worst value in the top.

      The worst is the greatest value for SortOrder.ASC, and the lowest value for SortOrder.DESC.

      This always allocates. Hot callers that only need to compare against the worst value should prefer getWorstValueView() — which returns a view that is invalidated by the next mutation of this set.

    • getWorstValueView

      public org.apache.lucene.util.BytesRef getWorstValueView()
      Returns a non-owning BytesRef view of the worst value in the top.

      The returned reference is only valid until the next mutating call on this set (collect(BytesRef), reduceLimitByOne(), close()). Callers that need to keep the bytes across mutations must use getWorstValue() instead.

    • getOrder

      public SortOrder getOrder()
      The order of the sort.
    • getLimit

      public int getLimit()
    • getCount

      public int getCount()
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface Releasable