/** * {@inheritDoc} */ public boolean freeSpace(Collection<T> from, long bytes) { if (from == null || from.isEmpty()) { return false; } List<T> random = new ArrayList<T>(from); Collections.shuffle(random); for (int i = 0; i < random.size(); i += SAMPLE_SIZE) { List<T> sorted = random.subList(i, Math.min(SAMPLE_SIZE + i, random.size())); Collections.sort(sorted, new EvictionCostComparator(getDesiredUnloadedSize(sorted), sorted.size() + 1)); for (T store : sorted) { int count; long byteSize = byteSize(store); long countSize = countSize(store); if (countSize == 0 || byteSize == 0) { count = 1; } else { count = (int) Math.max((bytes * countSize) / byteSize, 1L); } if (evict(store, count, bytes)) { return true; } } } return false; }