Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReadWriteLock;
Expand Down Expand Up @@ -66,6 +67,10 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
*/
private final AtomicReference<Deque<Entry<K, ValueHolder<V>>>> lastChanges
= new AtomicReference<>(new ConcurrentLinkedDeque<>());
/**
* The total amount of changes recorded.
*/
private final AtomicInteger totalChanges = new AtomicInteger();
/**
* The function to call when an entry is evicted.
*/
Expand Down Expand Up @@ -103,6 +108,7 @@ private ValueHolder<V> addChange(OperationContext<K, V> context, Function<? supe
}
ValueHolder<V> holder = newValue(value);
lastChanges.get().add(Map.entry(key, holder));
totalChanges.incrementAndGet();
return holder;
}

Expand Down Expand Up @@ -292,6 +298,7 @@ public void clear() {
lock.writeLock().lock();
try {
lastChanges.getAndSet(new ConcurrentLinkedDeque<>());
totalChanges.set(0);
delegate.clear();
} finally {
lock.writeLock().unlock();
Expand Down Expand Up @@ -335,7 +342,7 @@ public Set<Entry<K, V>> entrySet() {
* @return the size of the queue of changes.
*/
int getQueueSize() {
return lastChanges.get().size();
return totalChanges.get();
}

/**
Expand Down Expand Up @@ -370,7 +377,9 @@ private boolean isQueueFull() {
* @return the oldest existing change.
*/
private Entry<K, ValueHolder<V>> nextOldestChange() {
return lastChanges.get().poll();
Entry<K, ValueHolder<V>> oldestChange = lastChanges.get().poll();
totalChanges.decrementAndGet();
return oldestChange;
}

/**
Expand All @@ -383,6 +392,7 @@ private void compressChangesIfNeeded() {
try {
if (isQueueFull()) {
newChanges = new ConcurrentLinkedDeque<>();
totalChanges.set(0);
currentChanges = lastChanges.getAndSet(newChanges);
} else {
return;
Expand All @@ -395,6 +405,7 @@ private void compressChangesIfNeeded() {
while ((entry = currentChanges.pollLast()) != null) {
if (keys.add(entry.getKey())) {
newChanges.addFirst(entry);
totalChanges.incrementAndGet();
}
}
}
Expand Down
Loading