use of com.linkedin.pinot.core.util.IntObjectIndexedPriorityQueue in project pinot by linkedin.
the class ObjectGroupByResultHolder method switchToMapMode.
/**
* Helper method to switch the storage from array mode to map mode.
*
* @param initialPriorityQueueSize Initial size of priority queue
*/
@SuppressWarnings("unchecked")
private void switchToMapMode(int initialPriorityQueueSize) {
_storageMode = StorageMode.MAP_STORAGE;
_resultMap = new Int2ObjectOpenHashMap(_resultArray.length);
_priorityQueue = new IntObjectIndexedPriorityQueue(initialPriorityQueueSize, _minHeap);
for (int id = 0; id < _resultArray.length; id++) {
_resultMap.put(id, _resultArray[id]);
_priorityQueue.put(id, (Comparable) _resultArray[id]);
}
_resultArray = null;
}
use of com.linkedin.pinot.core.util.IntObjectIndexedPriorityQueue in project pinot by linkedin.
the class IntObjectIndexedPriorityQueueTest method test.
/**
* Helper method builds the priority queue, randomly updates elements and
* then asserts the following:
* <ul>
* <li> Elements are popped from the priority queue in the expected order. </li>
* <li> Size of the priority queue is as expected (after elements are updated). </li>
* </ul>
* @param minHeap Min mode
*/
public void test(boolean minHeap) {
Random random = new Random(0);
IntObjectIndexedPriorityQueue<AvgPair> pq = new IntObjectIndexedPriorityQueue<>(NUM_RECORDS, minHeap);
Map<Integer, AvgPair> map = new HashMap<>(NUM_RECORDS);
// Initialize the priority queue.
for (int i = 0; i < NUM_RECORDS; i++) {
// Avoid zeros
double first = 1 + random.nextInt(INT_VALUE_BOUND);
Long second = (long) 1 + random.nextInt(INT_VALUE_BOUND);
AvgPair value = new AvgPair(first, second);
pq.put(i, value);
map.put(i, value);
}
// Update some records randomly
for (int i = 0; i < NUM_RECORDS; i++) {
int key = random.nextInt(NUM_RECORDS);
// Avoid zeros
double first = 1 + random.nextInt(INT_VALUE_BOUND);
Long second = (long) 1 + random.nextInt(INT_VALUE_BOUND);
AvgPair value = new AvgPair(first, second);
pq.put(key, value);
map.put(key, value);
}
// Transfer the map into list so it can be sorted.
List<Pairs.IntObjectPair<AvgPair>> list = new ArrayList<>(NUM_RECORDS);
for (Map.Entry<Integer, AvgPair> entry : map.entrySet()) {
list.add(new Pairs.IntObjectPair<>(entry.getKey(), entry.getValue()));
}
// Comparison for min heap is the same as that for ascending order.
boolean descendingOrder = !minHeap;
Collections.sort(list, new Pairs.IntObjectComparator(descendingOrder));
// Ensure that elements are popped from priority queue in the expected order.
int i = 0;
while (!pq.isEmpty()) {
Pairs.IntObjectPair<AvgPair> actual = pq.poll();
Pairs.IntObjectPair<AvgPair> expected = list.get(i++);
Assert.assertEquals(actual.getIntValue(), expected.getIntValue());
Assert.assertEquals(actual.getObjectValue(), expected.getObjectValue());
}
// Assert that priority queue had expected number of elements.
Assert.assertEquals(i, list.size());
}
Aggregations