Grove.Set.GSet (Grove v0.1.1)

View Source

A Grow-only Set (G-Set) CRDT.

Elements can only be added, never removed. This makes it the simplest set CRDT with straightforward merge semantics (union).

Delta-State Support

This CRDT supports delta-state replication for efficient synchronization:

  • delta/1 - Returns accumulated changes since last reset
  • reset_delta/1 - Clears the delta buffer after synchronization

Example

iex> set = Grove.Set.GSet.new(:node_a)
iex> set = Grove.Set.GSet.add(set, "apple")
iex> set = Grove.Set.GSet.add(set, "banana")
iex> Grove.Set.GSet.value(set) |> Enum.sort()
["apple", "banana"]

Summary

Functions

Adds an element to the set.

Returns the accumulated delta since the last reset.

Checks if an element is in the set.

Merges two G-Sets.

Creates a new G-Set for the given actor.

Resets the delta buffer after synchronization.

Returns the number of elements in the set.

Returns the elements as a list.

Returns the elements as a MapSet.

Types

actor()

@type actor() :: term()

t()

@type t() :: %Grove.Set.GSet{
  actor: actor(),
  delta_buffer: MapSet.t(),
  elements: MapSet.t()
}

Functions

add(set, element)

@spec add(t(), term()) :: t()

Adds an element to the set.

Adding an element that already exists is a no-op.

delta(g_set)

@spec delta(t()) :: t()

Returns the accumulated delta since the last reset.

The delta contains only the elements added locally, which can be sent to other replicas for efficient synchronization.

member?(g_set, element)

@spec member?(t(), term()) :: boolean()

Checks if an element is in the set.

merge(set1, set2)

@spec merge(t(), t()) :: t()

Merges two G-Sets.

The result is the union of both sets.

new(actor)

@spec new(actor()) :: t()

Creates a new G-Set for the given actor.

reset_delta(set)

@spec reset_delta(t()) :: t()

Resets the delta buffer after synchronization.

Call this after sending the delta to other replicas.

size(g_set)

@spec size(t()) :: non_neg_integer()

Returns the number of elements in the set.

to_list(g_set)

@spec to_list(t()) :: [term()]

Returns the elements as a list.

value(g_set)

@spec value(t()) :: MapSet.t()

Returns the elements as a MapSet.