use of com.linkedin.pinot.common.utils.Pairs.IntObjectPair in project pinot by linkedin.
the class ObjectGroupByResultHolder method trimResults.
/**
* {@inheritDoc}
*
* Keys with 'lowest' values (as per the sort order) are trimmed away to reduce the size to _trimSize.
*
* @return Array of keys that were trimmed.
*/
@Override
public int[] trimResults() {
if (_storageMode == StorageMode.ARRAY_STORAGE) {
// Still in array mode, trimming has not kicked in yet.
return EMPTY_ARRAY;
}
int numKeysToRemove = _resultMap.size() - _trimSize;
int[] removedGroupKeys = new int[numKeysToRemove];
for (int i = 0; i < numKeysToRemove; i++) {
IntObjectPair pair = _priorityQueue.poll();
int groupKey = pair.getIntValue();
_resultMap.remove(groupKey);
removedGroupKeys[i] = groupKey;
}
return removedGroupKeys;
}
use of com.linkedin.pinot.common.utils.Pairs.IntObjectPair in project pinot by linkedin.
the class ObjectGroupByResultHolderTest method testTrimResults.
/**
* Helper method for testing trimming of results.
* @param minOrder Min ordering if true, max ordering else.
*/
private void testTrimResults(boolean minOrder) {
GroupByResultHolder resultHolder = new ObjectGroupByResultHolder(INITIAL_CAPACITY, MAX_CAPACITY, INITIAL_CAPACITY, minOrder);
List<IntObjectPair<AvgPair>> expectedIntObjectPairs = new ArrayList<>(MAX_CAPACITY);
for (int i = 0; i < INITIAL_CAPACITY; i++) {
AvgPair avgPair = new AvgPair(RANDOM.nextDouble(), (long) RANDOM.nextInt(100));
IntObjectPair<AvgPair> intObjectPair = new IntObjectPair<>(i, avgPair);
expectedIntObjectPairs.add(intObjectPair);
resultHolder.setValueForKey(i, avgPair);
}
// This will trigger switch from array based storage to map based storage.
resultHolder.ensureCapacity(MAX_CAPACITY);
for (int i = INITIAL_CAPACITY; i < MAX_CAPACITY; i++) {
AvgPair avgPair = new AvgPair(RANDOM.nextDouble(), (long) RANDOM.nextInt(100));
IntObjectPair<AvgPair> intObjectPair = new IntObjectPair<>(i, avgPair);
expectedIntObjectPairs.add(intObjectPair);
resultHolder.setValueForKey(i, avgPair);
}
Collections.sort(expectedIntObjectPairs, new Pairs.IntObjectComparator(!minOrder));
// Trim the results.
Assert.assertEquals(resultHolder.trimResults().length, MAX_CAPACITY - INITIAL_CAPACITY, ERROR_MESSAGE);
// Ensure that all the correct group keys survive after trimming.
for (int i = 0; i < INITIAL_CAPACITY; i++) {
IntObjectPair intObjectPair = expectedIntObjectPairs.get(i);
AvgPair actualAvgPair = resultHolder.getResult(intObjectPair.getIntValue());
AvgPair expectedAvgPair = (AvgPair) intObjectPair.getObjectValue();
Assert.assertEquals(actualAvgPair.getSum(), expectedAvgPair.getSum(), ERROR_MESSAGE);
Assert.assertEquals(actualAvgPair.getCount(), expectedAvgPair.getCount(), ERROR_MESSAGE);
}
}
Aggregations