Class DistributedRingbuffer<V>
java.lang.Object
com.loomcache.server.datastructures.DistributedRingbuffer<V>
- Type Parameters:
V- the type of items in this buffer
Distributed Ring Buffer — a fixed-capacity circular buffer.
A fixed-size circular buffer where adding to a full buffer overwrites the oldest entry. Backed by a fixed-size slot buffer with head/tail indices. Ideal for bounded collections of time-series data, event logs, or metrics.
Features:
- add(item) — add an item (overwrites oldest if full)
- get(index) — retrieve item by relative index (0 = oldest)
- peek() — view newest item without removing
- peekOldest() — view oldest item without removing
- toList() — snapshot as ordered list (oldest to newest)
- size() — current number of items
- capacity() — max capacity
- isFull() — check if at capacity
- clear() — remove all items
- drainTo(Collection) — drain all items in order into a List target
Thread safety: all operations are protected by ReentrantLock to ensure atomicity under concurrent access.
- Since:
- 1.0
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDistributedRingbuffer(String name, int instanceNumber, int capacity) Create a ring buffer with the given capacity. -
Method Summary
Modifier and TypeMethodDescriptionlongintcapacity()voidclear()intdrainTo(Collection<? super V> target) Drain all buffered items intotargetin order.@Nullable Vget(int index) @Nullable RingbufferStore<V> Get the configured backing store, ornullwhen none is wired.booleanlongbooleanisEmpty()booleanisFull()@Nullable Vpeek()@Nullable VreadMany(long startSequence, int maxCount) readManyAsync(long startSequence, int minCount, int maxCount) readManyAsync(long startSequence, int minCount, int maxCount, @Nullable IFunction<? super V, Boolean> filter) readManyResult(long startSequence, int maxCount, @Nullable IFunction<? super V, Boolean> filter) @Nullable VreadOne(long sequence) voidrestoreFromSnapshot(List<V> items, long restoredHeadSequence, long restoredTailSequence) voidrestoreFromSnapshot(List<V> items, long restoredHeadSequence, long restoredTailSequence, long restoredStoreReadFloor) voidsetRingbufferStore(RingbufferStore<V> store) Wire a persistent backing store for this ringbuffer.intsize()longlongtoList()toString()
-
Field Details
-
MAX_CAPACITY
public static final int MAX_CAPACITY- See Also:
-
-
Constructor Details
-
DistributedRingbuffer
Create a ring buffer with the given capacity.- Parameters:
name- the name of the ring bufferinstanceNumber- the node instance numbercapacity- the fixed capacity (must be > 0 and invalid input: '<'=MAX_CAPACITY)- Throws:
IllegalArgumentException- if capacity is outside the supported range
-
-
Method Details
-
add
-
setRingbufferStore
Wire a persistent backing store for this ringbuffer.If the ringbuffer is empty and the store reports an existing largest sequence, the next append continues after that sequence so new writes do not reuse persisted sequence IDs.
- Parameters:
store- the backing store- Throws:
IllegalStateException- if a store is already configured
-
getRingbufferStore
Get the configured backing store, ornullwhen none is wired.- Returns:
- configured ringbuffer store, or null
-
hasRingbufferStoreForPersistence
public boolean hasRingbufferStoreForPersistence() -
readOne
-
readMany
-
readMany
-
readManyResult
public DistributedRingbuffer.ReadManyResult<V> readManyResult(long startSequence, int maxCount, @Nullable IFunction<? super V, Boolean> filter) -
readManyAsync
-
readManyAsync
-
headSequence
public long headSequence() -
tailSequence
public long tailSequence() -
get
-
peek
-
peekOldest
-
toList
-
size
public int size() -
capacity
public int capacity() -
isFull
public boolean isFull() -
isEmpty
public boolean isEmpty() -
clear
public void clear() -
drainTo
Drain all buffered items intotargetin order.The target must be a
Listso partial appends can be rolled back exactly if the transfer fails. The caller must not modify the target concurrently while this method is running. -
restoreFromSnapshot
-
restoreFromSnapshot
-
storeReadFloor
public long storeReadFloor() -
toString
-