Class DistributedPriorityQueue<V>
java.lang.Object
com.loomcache.server.datastructures.DistributedPriorityQueue<V>
- Type Parameters:
V- the type of elements held in this queue
Distributed Priority Queue — priority queue backed by ConcurrentSkipListMap.
Ordering:
- Items are sorted by priority (lower number = higher priority)
- Within the same priority, items are ordered FIFO (insertion order)
Operations:
- offer: add an item with a specific priority
- offer (default priority): add an item with default priority 5
- poll: remove and return highest priority item (FIFO within same priority)
- peek: view the highest priority item without removing
- size: number of items
- drainTo: bulk drain into a collection
Thread-safe via ConcurrentSkipListMap and AtomicLong for ordering. Uses records and sealed classes per Java 25 best practices.
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordPriority entry key combining priority, value, and sequence for stable ordering.static final recordPersistence snapshot entry preserving both priority and FIFO sequence.static final recordStatistics snapshot of the priority queue. -
Constructor Summary
ConstructorsConstructorDescriptionDistributedPriorityQueue(String name, int instanceNumber) DistributedPriorityQueue(String name, int instanceNumber, int maxCapacity) DistributedPriorityQueue(String name, int instanceNumber, int maxCapacity, @Nullable Comparator<? super V> itemComparator, @Nullable String priorityComparatorClassName) DistributedPriorityQueue(String name, int instanceNumber, @Nullable Comparator<? super V> itemComparator, @Nullable String priorityComparatorClassName) -
Method Summary
Modifier and TypeMethodDescriptionintaddAll(Collection<? extends V> items, int priority) Add multiple items with the same priority.booleanchangePriority(V item, int newPriority) Change the priority of an existing item.voidclear()Remove all items from the queue.booleanCheck if an item exists in the queue.intdrainTo(Collection<? super V> target, int maxElements) Drain up to maxElements from the queue into the target collection.intintGet the maximum priority value in the queue.intGet the minimum priority value in the queue.intgetPriority(V item) Get the current priority of an item.longgetStats()Get statistics snapshot of the priority queue.longlongbooleanisEmpty()Check if the queue is empty.intmerge(DistributedPriorityQueue<V> other) Merge another DistributedPriorityQueue into this one.booleanAdd an item with the default priority (5).booleanAdd an item with the specified priority.@Nullable Vpeek()View the highest priority item without removing it.peekAll()Return all items in the queue in priority order without removing them.@Nullable Vpoll()Remove and return the highest priority item (lowest priority number).pollBatch(int count) Poll up to count items from the queue.intRemove all items matching the given predicate.voidrestoreFromSnapshot(List<DistributedPriorityQueue.PriorityQueueSnapshotEntry<V>> entries, long restoredSequenceCounter, long restoredTotalAdded, long restoredTotalRemoved) longsize()Returns the number of items in the queue.Return the full persistence state, including priorities and insertion sequence.toList()Return a non-destructive copy of all items in the queue.Return all elements sorted by priority without removing them.
-
Constructor Details
-
DistributedPriorityQueue
-
DistributedPriorityQueue
public DistributedPriorityQueue(String name, int instanceNumber, @Nullable Comparator<? super V> itemComparator, @Nullable String priorityComparatorClassName) -
DistributedPriorityQueue
-
DistributedPriorityQueue
public DistributedPriorityQueue(String name, int instanceNumber, int maxCapacity, @Nullable Comparator<? super V> itemComparator, @Nullable String priorityComparatorClassName)
-
-
Method Details
-
getMaxCapacity
public int getMaxCapacity() -
offer
Add an item with the specified priority. Lower priority number = higher priority.- Parameters:
item- the item to addpriority- the priority (lower = higher priority)- Returns:
- true if successfully added, false if capacity is full
-
offer
Add an item with the default priority (5).- Parameters:
item- the item to add- Returns:
- true if successfully added, false if capacity is full
-
poll
Remove and return the highest priority item (lowest priority number). Within the same priority, FIFO order is preserved.- Returns:
- the highest priority item, or null if the queue is empty
-
peek
View the highest priority item without removing it.- Returns:
- the highest priority item, or null if the queue is empty
-
size
public long size()Returns the number of items in the queue.- Returns:
- the size of the queue
-
isEmpty
public boolean isEmpty()Check if the queue is empty.- Returns:
- true if the queue contains no items
-
clear
public void clear()Remove all items from the queue. -
drainTo
Drain up to maxElements from the queue into the target collection. Items are removed in priority order (highest priority first, FIFO within same priority).- Parameters:
target- the collection to add drained elements tomaxElements- the maximum number of elements to drain (0 = unlimited)- Returns:
- the number of elements drained
-
toList
-
snapshotForPersistence
Return the full persistence state, including priorities and insertion sequence.User-facing views only expose values; Raft snapshots need the hidden priority/sequence metadata to preserve ordering after installSnapshot.
-
getSequenceCounterForPersistence
public long getSequenceCounterForPersistence() -
getTotalAddedForPersistence
public long getTotalAddedForPersistence() -
getTotalRemovedForPersistence
public long getTotalRemovedForPersistence() -
restoreFromSnapshot
public void restoreFromSnapshot(List<DistributedPriorityQueue.PriorityQueueSnapshotEntry<V>> entries, long restoredSequenceCounter, long restoredTotalAdded, long restoredTotalRemoved) -
peekAll
-
removeIf
-
contains
Check if an item exists in the queue.- Parameters:
item- the item to check- Returns:
- true if the item is in the queue
-
getPriority
Get the current priority of an item.- Parameters:
item- the item to get priority for- Returns:
- the priority, or -1 if the item is not in the queue
-
changePriority
Change the priority of an existing item.- Parameters:
item- the item to change priority fornewPriority- the new priority (lower = higher priority)- Returns:
- true if the item was found and priority changed, false if not found
-
addAll
Add multiple items with the same priority.- Parameters:
items- the items to addpriority- the priority for all items- Returns:
- the number of items successfully added
-
pollBatch
-
merge
Merge another DistributedPriorityQueue into this one. All items from the other queue are added to this queue with their original priorities.- Parameters:
other- the other priority queue to merge- Returns:
- the number of items successfully merged
-
toSortedList
-
getMinPriority
public int getMinPriority()Get the minimum priority value in the queue.- Returns:
- the minimum priority value, or Integer.MAX_VALUE if queue is empty
-
getMaxPriority
public int getMaxPriority()Get the maximum priority value in the queue.- Returns:
- the maximum priority value, or Integer.MIN_VALUE if queue is empty
-
getStats
Get statistics snapshot of the priority queue.- Returns:
- PriorityQueueStats containing aggregated statistics
-