Search in sources :

Example 1 with IntObjectIndexedPriorityQueue

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;
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) IntObjectIndexedPriorityQueue(com.linkedin.pinot.core.util.IntObjectIndexedPriorityQueue)

Example 2 with IntObjectIndexedPriorityQueue

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());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Pairs(com.linkedin.pinot.common.utils.Pairs) AvgPair(com.linkedin.pinot.core.query.aggregation.function.customobject.AvgPair) Random(java.util.Random) IntObjectIndexedPriorityQueue(com.linkedin.pinot.core.util.IntObjectIndexedPriorityQueue) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

IntObjectIndexedPriorityQueue (com.linkedin.pinot.core.util.IntObjectIndexedPriorityQueue)2 Pairs (com.linkedin.pinot.common.utils.Pairs)1 AvgPair (com.linkedin.pinot.core.query.aggregation.function.customobject.AvgPair)1 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1