Class CircularPriorityQueue<K,V,SLK extends Keyed<K>>
maxSize.
Concurrency contract: all mutating methods (add(Object),
remove(Object), replaceValue(Object, Object),
addPriorityKey(Object), removePriorityKey(Object),
setMaxSize(int) and setEvictionListener(java.util.function.Consumer))
are single-writer — callers serialize them on the control plane (the
Netty event loop / per-store synchronization). Read methods
(stream(), toSortedList(), getByKey(Object),
size(), keyMap()) may run concurrently with the single
writer and are eventually consistent: a read concurrent with an in-flight
mutation may not yet reflect it, but never returns nulls (the
filter(nonNull) guard) or corrupt state.
Precondition: add(Object) must not be called for a key that
already exists in the queue — use replaceValue(Object, Object) for
in-place updates. Adding a duplicate key would push the key twice into the
insertion-order queue and corrupt eviction accounting.
- Author:
- jamesdbloom
-
Constructor Summary
ConstructorsConstructorDescriptionCircularPriorityQueue(int maxSize, Comparator<? super SLK> skipListComparator, Function<V, SLK> skipListKeyFunction, Function<V, K> mapKeyFunction) -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidaddPriorityKey(V element) booleanisEmpty()keyMap()booleanvoidremovePriorityKey(V element) booleanreplaceValue(K key, V newValue) Replaces the value associated with the given key in place, preserving the element's position ininsertionOrderQueue(and therefore its eviction order).voidsetEvictionListener(Consumer<V> evictionListener) Registers a listener invoked once for every element evicted because the queue grew pastmaxSize.voidsetMaxSize(int maxSize) intsize()stream()Returns a cached, unmodifiable sorted snapshot of this queue's elements.
-
Constructor Details
-
CircularPriorityQueue
-
-
Method Details
-
setEvictionListener
Registers a listener invoked once for every element evicted because the queue grew pastmaxSize. The listener is called AFTER the element has been removed from the insertion queue, sort skip-list and byKey map, so satellite state can be cleaned up safely. It is NOT invoked on explicitremove(Object)orreplaceValue(Object, Object).- Parameters:
evictionListener- the listener, ornullto restore the no-op default
-
setMaxSize
public void setMaxSize(int maxSize) -
removePriorityKey
-
addPriorityKey
-
add
-
replaceValue
Replaces the value associated with the given key in place, preserving the element's position ininsertionOrderQueue(and therefore its eviction order). Because the insertion queue holds keys and the key is invariant on update, the queue is left untouched — the element keeps its exact eviction slot. Only the byKey map and the priority sort keys (old removed, new added) are updated. O(log n).- Parameters:
key- the key that identifies the existing elementnewValue- the replacement value- Returns:
trueif the key was found and the value replaced
-
remove
-
size
public int size() -
stream
-
getByKey
-
keyMap
-
isEmpty
public boolean isEmpty() -
toSortedList
Returns a cached, unmodifiable sorted snapshot of this queue's elements. The snapshot is rebuilt lazily when any mutation nulls the cache.Eventually-consistent under concurrent mutation: a call to this method concurrent with a control-plane mutation (add/remove/ reconcileFromBackend) may return a snapshot that does not yet reflect the in-flight mutation. This is the existing control-plane / data-plane concurrency contract — no lock is held on the matching hot path.
-