Class GSet<E>

java.lang.Object
com.loomcache.server.datastructures.crdt.GSet<E>

public class GSet<E> extends Object
Grow-only Set CRDT (G-Set).

A G-Set is a state-based CRDT that represents a set where elements can only be added, never removed. Concurrent adds and removals are handled by semantics where only adds win.

Key properties:

  • Elements can only be added, never removed
  • Merge: union of all sets from all replicas
  • Simple and highly available (no need for tracking tombstones)
  • Convergent: all nodes reach the same state given the same history
Since:
1.3
  • Constructor Summary

    Constructors
    Constructor
    Description
    GSet(@NonNull String name, @NonNull String nodeId, int instanceNumber)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(@NonNull E element)
    Add an element to the set.
    boolean
    contains(@NonNull E element)
    Check if an element is in the set.
    @NonNull Set<E>
    Get an immutable copy of all elements in the set.
    @NonNull String
     
    @NonNull String
     
    boolean
    Check if the set is empty.
    void
    merge(@Nullable GSet<E> other)
    Merge another G-Set into this one (anti-entropy).
    void
    Reset is NOT supported for G-Sets.
    int
    Get the number of elements in the set.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • GSet

      public GSet(@NonNull String name, @NonNull String nodeId, int instanceNumber)
  • Method Details

    • getName

      public @NonNull String getName()
    • getNodeId

      public @NonNull String getNodeId()
    • add

      public void add(@NonNull E element)
      Add an element to the set. In a G-Set, this is the only mutation allowed.
      Parameters:
      element - the element to add
      Throws:
      NullPointerException - if element is null
    • contains

      public boolean contains(@NonNull E element)
      Check if an element is in the set.
      Parameters:
      element - the element to check
      Returns:
      true if the element is in the set
    • size

      public int size()
      Get the number of elements in the set.
      Returns:
      the set size
    • elements

      public @NonNull Set<E> elements()
      Get an immutable copy of all elements in the set.
      Returns:
      a snapshot of the current set
    • isEmpty

      public boolean isEmpty()
      Check if the set is empty.
      Returns:
      true if no elements are in the set
    • merge

      public void merge(@Nullable GSet<E> other)
      Merge another G-Set into this one (anti-entropy). Merge operation is a union of sets. Merge is idempotent and commutative.
      Parameters:
      other - the other G-Set to merge
      Throws:
      IllegalArgumentException - if names don't match
    • reset

      public void reset()
      Reset is NOT supported for G-Sets. G-Set state is a join-semilattice under set union; clearing is non-monotonic and will be silently reversed on the next merge with any replica that has the old state.
      Throws:
      UnsupportedOperationException - always — reset violates CRDT merge invariants