Class PriorityQueueTemplate
java.lang.Object
org.apache.drill.exec.physical.impl.TopN.PriorityQueueTemplate
- All Implemented Interfaces:
PriorityQueue
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe estimated maximum queue size used with allocating the SV4 for the queue.Fields inherited from interface org.apache.drill.exec.physical.impl.TopN.PriorityQueue
TEMPLATE_DEFINITION -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(RecordBatchData batch) The elements in the given batch are added to the priority queue.voidcleanup()Releases all the memory consumed by the priority queue.intcompare(int leftIndex, int rightIndex) abstract intdoEval(int leftIndex, int rightIndex) Evaluates the value of record at leftIndex and rightIndex w.r.t min heap condition.abstract voiddoSetup(VectorContainer incoming, RecordBatch outgoing) Stores the reference to the hyperBatch container which holds all the records across incoming batches in it.voidgenerate()This method must be called before fetching the final priority queue hyper batch and final Sv4 vector.Retrieves the selection vector used to select the elements in the priority queue from the hyper batch provided by thePriorityQueue.getHyperBatch()method.Retrieves the final priority queue HyperBatch containing the results.getSv4()voidinit(int limit, BufferAllocator allocator, boolean hasSv2) Initializes the priority queue.booleanWhen cleanup is called then heapSv4 is cleared and set to null and is only initialized during init call.intpop()Pop the root element which holds the minimum value in heap.voidresetQueue(VectorContainer container, SelectionVector4 v4) Cleanup the old state of queue and recreate a new one with HyperContainer containing vectors in input container and the corresponding indexes (in SV4 format) from input SelectionVector4voidswap(int sv0, int sv1)
-
Field Details
-
EST_MAX_QUEUE_SIZE
public static final int EST_MAX_QUEUE_SIZEThe estimated maximum queue size used with allocating the SV4 for the queue. If the queue is larger, then a) we should probably be using a sort instead of top N, and b) the code will automatically grow the SV4 as needed up to the max supported size.- See Also:
-
-
Constructor Details
-
PriorityQueueTemplate
public PriorityQueueTemplate()
-
-
Method Details
-
init
Description copied from interface:PriorityQueueInitializes the priority queue. This method must be called before any other methods on the priority queue are called.- Specified by:
initin interfacePriorityQueue- Parameters:
limit- The size of the priority queue.allocator- TheBufferAllocatorto use when creating the priority queue.hasSv2- True when incoming batches have 2 byte selection vectors. False otherwise.- Throws:
SchemaChangeException
-
resetQueue
Description copied from interface:PriorityQueueCleanup the old state of queue and recreate a new one with HyperContainer containing vectors in input container and the corresponding indexes (in SV4 format) from input SelectionVector4- Specified by:
resetQueuein interfacePriorityQueue- Parameters:
container-v4-- Throws:
SchemaChangeException
-
add
Description copied from interface:PriorityQueueThe elements in the given batch are added to the priority queue. Note that the priority queue only retains the top elements that fit within the size specified by thePriorityQueue.init(int, BufferAllocator, boolean)method.- Specified by:
addin interfacePriorityQueue- Parameters:
batch- The batch containing elements we want to add.- Throws:
SchemaChangeException
-
generate
public void generate()Description copied from interface:PriorityQueueThis method must be called before fetching the final priority queue hyper batch and final Sv4 vector.- Specified by:
generatein interfacePriorityQueue
-
getHyperBatch
Description copied from interface:PriorityQueueRetrieves the final priority queue HyperBatch containing the results. Note: this should be called afterPriorityQueue.generate().- Specified by:
getHyperBatchin interfacePriorityQueue- Returns:
- The final priority queue HyperBatch containing the results.
-
getSv4
- Specified by:
getSv4in interfacePriorityQueue
-
getFinalSv4
Description copied from interface:PriorityQueueRetrieves the selection vector used to select the elements in the priority queue from the hyper batch provided by thePriorityQueue.getHyperBatch()method. Note: this should be called afterPriorityQueue.generate().- Specified by:
getFinalSv4in interfacePriorityQueue- Returns:
- The selection vector used to select the elements in the priority queue.
-
cleanup
public void cleanup()Description copied from interface:PriorityQueueReleases all the memory consumed by the priority queue.- Specified by:
cleanupin interfacePriorityQueue
-
isInitialized
public boolean isInitialized()When cleanup is called then heapSv4 is cleared and set to null and is only initialized during init call. Hence this is used to determine if priority queue is initialized or not.- Specified by:
isInitializedin interfacePriorityQueue- Returns:
- - true - queue is still initialized false - queue is not yet initialized and before using queue init should be called
-
pop
public int pop()Pop the root element which holds the minimum value in heap. In this case root element will be the index of record with minimum value. After extracting the root element it swaps the root element with last element in heapSv4 and does heapify (by calling siftDown) again.- Returns:
- - Index for
-
swap
public void swap(int sv0, int sv1) -
compare
- Throws:
SchemaChangeException
-
doSetup
public abstract void doSetup(@Named("incoming") VectorContainer incoming, @Named("outgoing") RecordBatch outgoing) throws SchemaChangeException Stores the reference to the hyperBatch container which holds all the records across incoming batches in it. This is used in doEval function to compare records in this hyper batch at given indexes.- Parameters:
incoming- - reference to hyperBatchoutgoing- - null- Throws:
SchemaChangeException
-
doEval
public abstract int doEval(@Named("leftIndex") int leftIndex, @Named("rightIndex") int rightIndex) throws SchemaChangeException Evaluates the value of record at leftIndex and rightIndex w.r.t min heap condition. It is used incompare(int, int)method while Heapifying the queue.- Parameters:
leftIndex-rightIndex-- Returns:
- Throws:
SchemaChangeException
-