/** * {@inheritDoc} */ public PoolAccessor createPoolAccessor(PoolParticipant participant, SizeOfEngine sizeOfEngine) { LockedPoolAccessor accessor = new LockedPoolAccessor(this, participant, sizeOfEngine, 0); registerPoolAccessor(accessor); return accessor; } }
/** * {@inheritDoc} */ public PoolAccessor createPoolAccessor(PoolParticipant participant, SizeOfEngine sizeOfEngine) { AtomicPoolAccessor accessor = new AtomicPoolAccessor(this, participant, sizeOfEngine, 0); registerPoolAccessor(accessor); return accessor; } }
/** * {@inheritDoc} */ public final void clear() { doClear(); abortedSizeOf = false; }
/** * {@inheritDoc} */ public final long replace(long currentSize, Object key, Object value, Object container, boolean force) { Size sizeOf = sizeOfEngine.sizeOf(key, value, container); long delta = sizeOf.getCalculated() - currentSize; if (delta == 0L) { return 0L; } else if (delta < 0L) { return -delete(-delta); } else { long added = add(delta, force); return added == -1L ? Long.MIN_VALUE : added; } }
/** * {@inheritDoc} */ public final long add(Object key, Object value, Object container, boolean force) { checkLinked(); Size sizeOf = sizeOfEngine.sizeOf(key, value, container); if (!sizeOf.isExact()) { abortedSizeOf = true; } return add(sizeOf.getCalculated(), force); }
/** * {@inheritDoc} */ public void setMaxSize(long newSize) { long oldSize = this.maximumPoolSize; this.maximumPoolSize = newSize; long sizeToEvict = oldSize - newSize; if (sizeToEvict > 0) { getEvictor().freeSpace(getPoolAccessors(), sizeToEvict); } }
/** * Creates a persitent-to-disk store for the given cache, using the given disk path. * Heap and disk usage are not tracked by the returned store. * * @param cache cache that fronts this store * @return a fully initialized store */ public static DiskStore create(Cache cache) { return create(cache, new UnboundedPool(), new UnboundedPool()); }
/** * {@inheritDoc} */ public boolean freeSpace(Collection<PoolAccessor<PoolParticipant>> from, long bytes) { if (from == null || from.isEmpty()) { return false; } List<PoolAccessor> random = new ArrayList<PoolAccessor>(from); Collections.shuffle(random); for (int i = 0; i < random.size(); i += SAMPLE_SIZE) { List<PoolAccessor> sorted = random.subList(i, Math.min(SAMPLE_SIZE + i, random.size())); Collections.sort(sorted, new EvictionCostComparator(getDesiredUnloadedSize(sorted), sorted.size() + 1)); for (PoolAccessor accessor : sorted) { int count; long byteSize = accessor.getSize(); long countSize = accessor.getParticipant().getApproximateCountSize(); if (countSize == 0 || byteSize == 0) { count = 1; } else { count = (int) Math.max((bytes * countSize) / byteSize, 1L); } if (accessor.getParticipant().evict(count, bytes)) { return true; } } } return false; }
/** * {@inheritDoc} */ public PoolAccessor createPoolAccessor(PoolParticipant participant, SizeOfEngine sizeOfEngine) { return new UnboundedPoolAccessor(); }
/** * {@inheritDoc} */ public SizeOfEngine copyWith(int maxDepth, boolean abortWhenMaxDepthExceeded) { return new DefaultSizeOfEngine(this, maxDepth, abortWhenMaxDepthExceeded); }
/** * {@inheritDoc} */ public long delete(long sizeOf) throws IllegalArgumentException { checkLinked(); // if (sizeOf < 0L) { // throw new IllegalArgumentException("cannot delete negative size"); // } size.addAndGet(-sizeOf); return sizeOf; }
/** * {@inheritDoc} */ public long delete(long sizeOf) throws IllegalArgumentException { checkLinked(); // if (sizeOf < 0L) { // throw new IllegalArgumentException("cannot delete negative size"); // } // synchronized makes the size update MT-safe but slow lock.lock(); try { size -= sizeOf; } finally { lock.unlock(); } return sizeOf; }
/** * {@inheritDoc} */ public PoolAccessor createPoolAccessor(PoolParticipant participant, int maxDepth, boolean abortWhenMaxDepthExceeded) { return createPoolAccessor(participant, defaultSizeOfEngine.copyWith(maxDepth, abortWhenMaxDepthExceeded)); }
/** * {@inheritDoc} */ public final void unlink() { if (unlinked.compareAndSet(false, true)) { getPool().removePoolAccessor(this); } }
/** * {@inheritDoc} */ public final boolean canAddWithoutEvicting(Object key, Object value, Object container) { Size sizeOf = sizeOfEngine.sizeOf(key, value, container); return canAddWithoutEvicting(sizeOf.getCalculated()); }
private PoolAccessor<PoolParticipant> findUntriedLargestPoolableStore(Collection<PoolAccessor<PoolParticipant>> from, Collection<PoolAccessor<PoolParticipant>> tried) { PoolAccessor<PoolParticipant> largestPoolAccessor = null; for (PoolAccessor<PoolParticipant> accessor : from) { if (alreadyTried(tried, accessor)) { continue; } if (largestPoolAccessor == null || accessor.getSize() > largestPoolAccessor.getSize()) { largestPoolAccessor = accessor; } } return largestPoolAccessor; }
/** * {@inheritDoc} */ public PoolAccessor createPoolAccessor(PoolParticipant participant, int maxDepth, boolean abortWhenMaxDepthExceeded) { return new UnboundedPoolAccessor(); }
/** * {@inheritDoc} */ @Override public SizeOfEngine createSizeOfEngine(final int maxObjectCount, final boolean abort, final boolean silent) { SizeOfEngineFactory currentFactory = this.factory; if (currentFactory != null) { return currentFactory.createSizeOfEngine(maxObjectCount, abort, silent); } return new DefaultSizeOfEngine(maxObjectCount, abort, silent); }