Java Code Examples for com.google.common.collect.MinMaxPriorityQueue#removeLast()

The following examples show how to use com.google.common.collect.MinMaxPriorityQueue#removeLast() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PMetaDataImpl.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * 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 PTableCache 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();
    PTableCache newCache = new PTableCache(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 : tables.values()) {
        newCache.put(tableRef.table.getKey(), new PTableRef(tableRef));
        toRemove.add(tableRef);
        toRemoveBytes += tableRef.estSize;
        if (toRemoveBytes - toRemove.peekLast().estSize > overage) {
            PTableRef removedRef = toRemove.removeLast();
            toRemoveBytes -= removedRef.estSize;
        }
    }
    for (PTableRef toRemoveRef : toRemove) {
        newCache.remove(toRemoveRef.table.getKey());
    }
    return newCache;
}
 
Example 2
Source File: PMetaDataCache.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * 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, this.tableRefFactory);
    
    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(), tableRefFactory.makePTableRef(tableRef));
        toRemove.add(tableRef);
        toRemoveBytes += tableRef.getEstimatedSize();
        while (toRemoveBytes - toRemove.peekLast().getEstimatedSize() >= overage) {
            PTableRef removedRef = toRemove.removeLast();
            toRemoveBytes -= removedRef.getEstimatedSize();
        }
    }
    for (PTableRef toRemoveRef : toRemove) {
        newCache.remove(toRemoveRef.getTable().getKey());
    }
    return newCache;
}