@Test public void testResizeRecordsMap() { TableResizer tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "d1")); Map<Key, Record> recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(recordsMap.size(), TRIM_TO_SIZE); assertTrue(recordsMap.containsKey(_keys.get(0))); assertTrue(recordsMap.containsKey(_keys.get(1))); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "d1 DESC")); recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(recordsMap.size(), TRIM_TO_SIZE); assertTrue(recordsMap.containsKey(_keys.get(2))); assertTrue(recordsMap.containsKey(_keys.get(3))); assertTrue(recordsMap.containsKey(_keys.get(4))); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "d1, d3 DESC")); recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(recordsMap.size(), TRIM_TO_SIZE); assertTrue(recordsMap.containsKey(_keys.get(0))); assertTrue(recordsMap.containsKey(_keys.get(1))); assertTrue(recordsMap.containsKey(_keys.get(4))); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "d1, SUM(m1) DESC, max(m2) DESC")); recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(recordsMap.size(), TRIM_TO_SIZE); assertTrue(recordsMap.containsKey(_keys.get(0))); assertTrue(recordsMap.containsKey(_keys.get(1))); assertTrue(recordsMap.containsKey(_keys.get(2))); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "AVG(m4)")); recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(recordsMap.size(), TRIM_TO_SIZE); assertTrue(recordsMap.containsKey(_keys.get(4))); assertTrue(recordsMap.containsKey(_keys.get(3))); assertTrue(recordsMap.containsKey(_keys.get(1))); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "DISTINCTCOUNT(m3) DESC, d1")); recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(recordsMap.size(), TRIM_TO_SIZE); assertTrue(recordsMap.containsKey(_keys.get(4))); assertTrue(recordsMap.containsKey(_keys.get(3))); assertTrue(recordsMap.containsKey(_keys.get(1))); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "d2 + d3")); recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(recordsMap.size(), TRIM_TO_SIZE); assertTrue(recordsMap.containsKey(_keys.get(0))); assertTrue(recordsMap.containsKey(_keys.get(1))); assertTrue(recordsMap.containsKey(_keys.get(3))); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "SUM(m1) * d3 DESC")); recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(recordsMap.size(), TRIM_TO_SIZE); assertTrue(recordsMap.containsKey(_keys.get(3))); assertTrue(recordsMap.containsKey(_keys.get(4))); assertTrue(recordsMap.containsKey(_keys.get(2))); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "d2 / (DISTINCTCOUNT(m3) + 1), d1 DESC")); recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, TRIM_TO_SIZE); assertEquals(recordsMap.size(), TRIM_TO_SIZE); assertTrue(recordsMap.containsKey(_keys.get(1))); assertTrue(recordsMap.containsKey(_keys.get(0))); assertTrue(recordsMap.containsKey(_keys.get(3))); int trimToSize = 2; tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "d1")); recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, trimToSize); assertEquals(recordsMap.size(), trimToSize); assertTrue(recordsMap.containsKey(_keys.get(0))); assertTrue(recordsMap.containsKey(_keys.get(1))); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "AVG(m4)")); recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, trimToSize); assertEquals(recordsMap.size(), trimToSize); assertTrue(recordsMap.containsKey(_keys.get(4))); assertTrue(recordsMap.containsKey(_keys.get(3))); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "DISTINCTCOUNT(m3) DESC, d1")); recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, trimToSize); assertEquals(recordsMap.size(), trimToSize); assertTrue(recordsMap.containsKey(_keys.get(4))); assertTrue(recordsMap.containsKey(_keys.get(3))); tableResizer = new TableResizer(DATA_SCHEMA, QueryContextConverterUtils.getQueryContextFromSQL(QUERY_PREFIX + "d2 / (DISTINCTCOUNT(m3) + 1), d1 DESC")); recordsMap = new HashMap<>(_recordsMap); tableResizer.resizeRecordsMap(recordsMap, trimToSize); assertEquals(recordsMap.size(), trimToSize); assertTrue(recordsMap.containsKey(_keys.get(1))); assertTrue(recordsMap.containsKey(_keys.get(0))); }
public void resizeRecordsMap(Map<Key, Record> recordsMap, int trimToSize) { int numRecordsToEvict = recordsMap.size() - trimToSize; if (numRecordsToEvict > 0) { if (numRecordsToEvict < trimToSize) { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToEvict, _intermediateRecordComparator); for (IntermediateRecord evictRecord : priorityQueue) { recordsMap.remove(evictRecord._key); } } else { Comparator<IntermediateRecord> comparator = _intermediateRecordComparator.reversed(); PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, trimToSize, comparator); ObjectOpenHashSet<Key> keysToRetain = new ObjectOpenHashSet<>(priorityQueue.size()); for (IntermediateRecord retainRecord : priorityQueue) { keysToRetain.add(retainRecord._key); } recordsMap.keySet().retainAll(keysToRetain); } } }
TableResizer { public void resizeRecordsMap(Map<Key, Record> recordsMap, int trimToSize) { int numRecordsToEvict = recordsMap.size() - trimToSize; if (numRecordsToEvict > 0) { if (numRecordsToEvict < trimToSize) { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToEvict, _intermediateRecordComparator); for (IntermediateRecord evictRecord : priorityQueue) { recordsMap.remove(evictRecord._key); } } else { Comparator<IntermediateRecord> comparator = _intermediateRecordComparator.reversed(); PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, trimToSize, comparator); ObjectOpenHashSet<Key> keysToRetain = new ObjectOpenHashSet<>(priorityQueue.size()); for (IntermediateRecord retainRecord : priorityQueue) { keysToRetain.add(retainRecord._key); } recordsMap.keySet().retainAll(keysToRetain); } } } }
TableResizer { public void resizeRecordsMap(Map<Key, Record> recordsMap, int trimToSize) { int numRecordsToEvict = recordsMap.size() - trimToSize; if (numRecordsToEvict > 0) { if (numRecordsToEvict < trimToSize) { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToEvict, _intermediateRecordComparator); for (IntermediateRecord evictRecord : priorityQueue) { recordsMap.remove(evictRecord._key); } } else { Comparator<IntermediateRecord> comparator = _intermediateRecordComparator.reversed(); PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, trimToSize, comparator); ObjectOpenHashSet<Key> keysToRetain = new ObjectOpenHashSet<>(priorityQueue.size()); for (IntermediateRecord retainRecord : priorityQueue) { keysToRetain.add(retainRecord._key); } recordsMap.keySet().retainAll(keysToRetain); } } } TableResizer(DataSchema dataSchema, QueryContext queryContext); }
TableResizer { public void resizeRecordsMap(Map<Key, Record> recordsMap, int trimToSize) { int numRecordsToEvict = recordsMap.size() - trimToSize; if (numRecordsToEvict > 0) { if (numRecordsToEvict < trimToSize) { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToEvict, _intermediateRecordComparator); for (IntermediateRecord evictRecord : priorityQueue) { recordsMap.remove(evictRecord._key); } } else { Comparator<IntermediateRecord> comparator = _intermediateRecordComparator.reversed(); PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, trimToSize, comparator); ObjectOpenHashSet<Key> keysToRetain = new ObjectOpenHashSet<>(priorityQueue.size()); for (IntermediateRecord retainRecord : priorityQueue) { keysToRetain.add(retainRecord._key); } recordsMap.keySet().retainAll(keysToRetain); } } } TableResizer(DataSchema dataSchema, QueryContext queryContext); void resizeRecordsMap(Map<Key, Record> recordsMap, int trimToSize); List<Record> resizeAndSortRecordsMap(Map<Key, Record> recordsMap, int trimToSize); }
TableResizer { public void resizeRecordsMap(Map<Key, Record> recordsMap, int trimToSize) { int numRecordsToEvict = recordsMap.size() - trimToSize; if (numRecordsToEvict > 0) { if (numRecordsToEvict < trimToSize) { PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, numRecordsToEvict, _intermediateRecordComparator); for (IntermediateRecord evictRecord : priorityQueue) { recordsMap.remove(evictRecord._key); } } else { Comparator<IntermediateRecord> comparator = _intermediateRecordComparator.reversed(); PriorityQueue<IntermediateRecord> priorityQueue = convertToIntermediateRecordsPQ(recordsMap, trimToSize, comparator); ObjectOpenHashSet<Key> keysToRetain = new ObjectOpenHashSet<>(priorityQueue.size()); for (IntermediateRecord retainRecord : priorityQueue) { keysToRetain.add(retainRecord._key); } recordsMap.keySet().retainAll(keysToRetain); } } } TableResizer(DataSchema dataSchema, QueryContext queryContext); void resizeRecordsMap(Map<Key, Record> recordsMap, int trimToSize); List<Record> resizeAndSortRecordsMap(Map<Key, Record> recordsMap, int trimToSize);  }