Class ORSet<E>
java.lang.Object
com.loomcache.server.datastructures.crdt.ORSet<E>
Observed-Remove Set CRDT (OR-Set).
An OR-Set is a state-based CRDT that represents a set where elements can be added and removed concurrently. In case of concurrent add/remove, the add wins. This is implemented using unique tags for each add operation. When removing, all currently visible tags are marked for removal.
Key properties:
- Each add operation receives a unique identifier (UUID tag)
- Remove removes all known tags for an element
- Concurrent add/remove: add wins (element stays if new add after remove)
- Merge: union of all elements and their tags
- lookup(x): x is in the set if it has at least one tag
- Convergent: all nodes reach the same state given the same history
- Since:
- 1.3
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd an element to the set.intCompact tombstones once removal metadata is causally stable across replicas.booleanCheck if an element is in the set.elements()Get an immutable copy of all elements currently in the set.intgetTagCount(E element) Get the number of tags for an element (for diagnostics).intGet the number of tombstone entries (for diagnostics/monitoring).booleanisEmpty()Check if the set is empty.voidMerge another OR-Set into this one (anti-entropy).voidRemove an element from the set.voidreset()Reset is NOT supported for OR-Sets.voidrestoreFromSnapshot(Map<?, ?> snapshot) Restore full OR-Set state captured bytoSnapshot().intsize()Get the number of elements currently in the set.Export the full OR-Set state for Raft snapshots.
-
Constructor Details
-
ORSet
-
-
Method Details
-
add
Add an element to the set. Each add operation receives a unique UUID tag.- Parameters:
element- the element to add- Throws:
NullPointerException- if element is null
-
remove
Remove an element from the set. This removes all currently visible tags for the element. If the element is added again after this removal, it will reappear.- Parameters:
element- the element to remove- Throws:
NullPointerException- if element is null
-
contains
Check if an element is in the set. An element is in the set if it has at least one tag.- Parameters:
element- the element to check- Returns:
- true if the element is in the set
-
size
public int size()Get the number of elements currently in the set.- Returns:
- the set size
-
elements
-
toSnapshot
-
restoreFromSnapshot
Restore full OR-Set state captured bytoSnapshot(). -
isEmpty
public boolean isEmpty()Check if the set is empty.- Returns:
- true if no elements are in the set
-
merge
Merge another OR-Set into this one (anti-entropy). Merge operation is a union of all elements and their tags. If an element exists in both sets, tags are merged (union). Merge is idempotent and commutative.- Parameters:
other- the other OR-Set to merge- Throws:
IllegalArgumentException- if names don't match
-
reset
public void reset()Reset is NOT supported for OR-Sets. Clearing elements and tombstones is non-monotonic: a stale replica can replay pre-reset add tags, and previously removed elements can resurrect because the tombstones were erased.- Throws:
UnsupportedOperationException- always — reset violates CRDT merge invariants
-
compactTombstones
public int compactTombstones()Compact tombstones once removal metadata is causally stable across replicas.Until replica progress vectors are tracked, compaction is intentionally disabled to avoid resurrecting removed elements during stale-replica merges. Safe to call concurrently — acquires the lock.
- Returns:
- the number of tombstone tags removed
-
getTombstoneCount
public int getTombstoneCount()Get the number of tombstone entries (for diagnostics/monitoring).- Returns:
- the number of elements with tombstone entries
-
getTagCount
Get the number of tags for an element (for diagnostics).- Parameters:
element- the element to check- Returns:
- the number of tags for that element
-