@Test public void testResizeAndSortRecordsMap() { TableResizer tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "d1")); Map<Key, Record> recordsMap = new HashMap<>(_recordsMap); List<Record> sortedRecords = tableResizer.resizeAndSortRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(sortedRecords.size(), TRIM_TO_SIZE); assertEquals(sortedRecords.get(0), _records.get(0)); assertEquals(sortedRecords.get(1), _records.get(1)); recordsMap = new HashMap<>(_recordsMap); sortedRecords = tableResizer.resizeAndSortRecordsMap(recordsMap, 1); assertEquals(sortedRecords.get(0), _records.get(0)); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "d1, d3 DESC")); recordsMap = new HashMap<>(_recordsMap); sortedRecords = tableResizer.resizeAndSortRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(sortedRecords.size(), TRIM_TO_SIZE); assertEquals(sortedRecords.get(0), _records.get(0)); assertEquals(sortedRecords.get(1), _records.get(1)); assertEquals(sortedRecords.get(2), _records.get(4)); recordsMap = new HashMap<>(_recordsMap); sortedRecords = tableResizer.resizeAndSortRecordsMap(recordsMap, 1); assertEquals(sortedRecords.size(), 1); assertEquals(sortedRecords.get(0), _records.get(0)); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "d1, SUM(m1) DESC, max(m2) DESC")); recordsMap = new HashMap<>(_recordsMap); sortedRecords = tableResizer.resizeAndSortRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(sortedRecords.size(), TRIM_TO_SIZE); assertEquals(sortedRecords.get(0), _records.get(0)); assertEquals(sortedRecords.get(1), _records.get(1)); assertEquals(sortedRecords.get(2), _records.get(2)); recordsMap = new HashMap<>(_recordsMap); sortedRecords = tableResizer.resizeAndSortRecordsMap(recordsMap, 1); assertEquals(sortedRecords.size(), 1); assertEquals(sortedRecords.get(0), _records.get(0)); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "AVG(m4)")); recordsMap = new HashMap<>(_recordsMap); sortedRecords = tableResizer.resizeAndSortRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(sortedRecords.size(), TRIM_TO_SIZE); assertEquals(sortedRecords.get(0), _records.get(4)); assertEquals(sortedRecords.get(1), _records.get(3)); assertEquals(sortedRecords.get(2), _records.get(1)); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "DISTINCTCOUNT(m3) DESC, d1")); recordsMap = new HashMap<>(_recordsMap); sortedRecords = tableResizer.resizeAndSortRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(sortedRecords.size(), TRIM_TO_SIZE); assertEquals(sortedRecords.get(0), _records.get(4)); assertEquals(sortedRecords.get(1), _records.get(3)); assertEquals(sortedRecords.get(2), _records.get(1)); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "d2 / (DISTINCTCOUNT(m3) + 1), d1 DESC")); recordsMap = new HashMap<>(_recordsMap); sortedRecords = tableResizer.resizeAndSortRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(sortedRecords.size(), TRIM_TO_SIZE); assertEquals(sortedRecords.get(0), _records.get(1)); assertEquals(sortedRecords.get(1), _records.get(0)); assertEquals(sortedRecords.get(2), _records.get(3)); }
public List<Record> resizeAndSortRecordsMap(Map<Key, Record> recordsMap, int trimToSize) { int numRecords = recordsMap.size(); if (numRecords == 0) { return Collections.emptyList(); } int numRecordsToRetain = Math.min(numRecords, trimToSize); int numRecordsToEvict = numRecords - numRecordsToRetain; if (numRecordsToEvict < numRecordsToRetain) { if (numRecordsToEvict > 0) { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToEvict, _intermediateRecordComparator); for (IntermediateRecord evictRecord : priorityQueue) { recordsMap.remove(evictRecord._key); } } return sortRecordsMap(recordsMap); } else { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToRetain, _intermediateRecordComparator.reversed()); Record[] sortedArray = new Record[numRecordsToRetain]; ObjectOpenHashSet<Key> keysToRetain = new ObjectOpenHashSet<>(numRecordsToRetain); while (!priorityQueue.isEmpty()) { IntermediateRecord intermediateRecord = priorityQueue.poll(); keysToRetain.add(intermediateRecord._key); Record record = recordsMap.get(intermediateRecord._key); sortedArray[--numRecordsToRetain] = record; } recordsMap.keySet().retainAll(keysToRetain); return Arrays.asList(sortedArray); } }
TableResizer { public List<Record> resizeAndSortRecordsMap(Map<Key, Record> recordsMap, int trimToSize) { int numRecords = recordsMap.size(); if (numRecords == 0) { return Collections.emptyList(); } int numRecordsToRetain = Math.min(numRecords, trimToSize); int numRecordsToEvict = numRecords - numRecordsToRetain; if (numRecordsToEvict < numRecordsToRetain) { if (numRecordsToEvict > 0) { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToEvict, _intermediateRecordComparator); for (IntermediateRecord evictRecord : priorityQueue) { recordsMap.remove(evictRecord._key); } } return sortRecordsMap(recordsMap); } else { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToRetain, _intermediateRecordComparator.reversed()); Record[] sortedArray = new Record[numRecordsToRetain]; ObjectOpenHashSet<Key> keysToRetain = new ObjectOpenHashSet<>(numRecordsToRetain); while (!priorityQueue.isEmpty()) { IntermediateRecord intermediateRecord = priorityQueue.poll(); keysToRetain.add(intermediateRecord._key); Record record = recordsMap.get(intermediateRecord._key); sortedArray[--numRecordsToRetain] = record; } recordsMap.keySet().retainAll(keysToRetain); return Arrays.asList(sortedArray); } } }
TableResizer { public List<Record> resizeAndSortRecordsMap(Map<Key, Record> recordsMap, int trimToSize) { int numRecords = recordsMap.size(); if (numRecords == 0) { return Collections.emptyList(); } int numRecordsToRetain = Math.min(numRecords, trimToSize); int numRecordsToEvict = numRecords - numRecordsToRetain; if (numRecordsToEvict < numRecordsToRetain) { if (numRecordsToEvict > 0) { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToEvict, _intermediateRecordComparator); for (IntermediateRecord evictRecord : priorityQueue) { recordsMap.remove(evictRecord._key); } } return sortRecordsMap(recordsMap); } else { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToRetain, _intermediateRecordComparator.reversed()); Record[] sortedArray = new Record[numRecordsToRetain]; ObjectOpenHashSet<Key> keysToRetain = new ObjectOpenHashSet<>(numRecordsToRetain); while (!priorityQueue.isEmpty()) { IntermediateRecord intermediateRecord = priorityQueue.poll(); keysToRetain.add(intermediateRecord._key); Record record = recordsMap.get(intermediateRecord._key); sortedArray[--numRecordsToRetain] = record; } recordsMap.keySet().retainAll(keysToRetain); return Arrays.asList(sortedArray); } } TableResizer(DataSchema dataSchema, QueryContext queryContext); }
TableResizer { public List<Record> resizeAndSortRecordsMap(Map<Key, Record> recordsMap, int trimToSize) { int numRecords = recordsMap.size(); if (numRecords == 0) { return Collections.emptyList(); } int numRecordsToRetain = Math.min(numRecords, trimToSize); int numRecordsToEvict = numRecords - numRecordsToRetain; if (numRecordsToEvict < numRecordsToRetain) { if (numRecordsToEvict > 0) { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToEvict, _intermediateRecordComparator); for (IntermediateRecord evictRecord : priorityQueue) { recordsMap.remove(evictRecord._key); } } return sortRecordsMap(recordsMap); } else { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToRetain, _intermediateRecordComparator.reversed()); Record[] sortedArray = new Record[numRecordsToRetain]; ObjectOpenHashSet<Key> keysToRetain = new ObjectOpenHashSet<>(numRecordsToRetain); while (!priorityQueue.isEmpty()) { IntermediateRecord intermediateRecord = priorityQueue.poll(); keysToRetain.add(intermediateRecord._key); Record record = recordsMap.get(intermediateRecord._key); sortedArray[--numRecordsToRetain] = record; } recordsMap.keySet().retainAll(keysToRetain); return Arrays.asList(sortedArray); } } TableResizer(DataSchema dataSchema, QueryContext queryContext); void resizeRecordsMap(Map<Key, Record> recordsMap, int trimToSize); List<Record> resizeAndSortRecordsMap(Map<Key, Record> recordsMap, int trimToSize); }
TableResizer { public List<Record> resizeAndSortRecordsMap(Map<Key, Record> recordsMap, int trimToSize) { int numRecords = recordsMap.size(); if (numRecords == 0) { return Collections.emptyList(); } int numRecordsToRetain = Math.min(numRecords, trimToSize); int numRecordsToEvict = numRecords - numRecordsToRetain; if (numRecordsToEvict < numRecordsToRetain) { if (numRecordsToEvict > 0) { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToEvict, _intermediateRecordComparator); for (IntermediateRecord evictRecord : priorityQueue) { recordsMap.remove(evictRecord._key); } } return sortRecordsMap(recordsMap); } else { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToRetain, _intermediateRecordComparator.reversed()); Record[] sortedArray = new Record[numRecordsToRetain]; ObjectOpenHashSet<Key> keysToRetain = new ObjectOpenHashSet<>(numRecordsToRetain); while (!priorityQueue.isEmpty()) { IntermediateRecord intermediateRecord = priorityQueue.poll(); keysToRetain.add(intermediateRecord._key); Record record = recordsMap.get(intermediateRecord._key); sortedArray[--numRecordsToRetain] = record; } recordsMap.keySet().retainAll(keysToRetain); return Arrays.asList(sortedArray); } } TableResizer(DataSchema dataSchema, QueryContext queryContext); void resizeRecordsMap(Map<Key, Record> recordsMap, int trimToSize); List<Record> resizeAndSortRecordsMap(Map<Key, Record> recordsMap, int trimToSize);  }