/** * Used when the cache is growing past its max size to clone in a single pass. Removes least * recently used tables to get size of cache below its max size by the overage amount. */ public PMetaDataCache cloneMinusOverage(long overage) { assert (overage > 0); int nToRemove = Math.max( MIN_REMOVAL_SIZE, (int) Math.ceil((currentByteSize - maxByteSize) / ((double) currentByteSize / size())) + 1); MinMaxPriorityQueue<PTableRef> toRemove = BUILDER.expectedSize(nToRemove).create(); PMetaDataCache newCache = new PMetaDataCache(this.size(), this.maxByteSize, this.timeKeeper); long toRemoveBytes = 0; // Add to new cache, but track references to remove when done // to bring cache at least overage amount below it's max size. for (PTableRef tableRef : this.tables.values()) { newCache.put(tableRef.getTable().getKey(), new PTableRef(tableRef)); toRemove.add(tableRef); toRemoveBytes += tableRef.getEstSize(); while (toRemoveBytes - toRemove.peekLast().getEstSize() >= overage) { PTableRef removedRef = toRemove.removeLast(); toRemoveBytes -= removedRef.getEstSize(); } } for (PTableRef toRemoveRef : toRemove) { newCache.remove(toRemoveRef.getTable().getKey()); } return newCache; }