Grove.Set.TwoPSet (Grove v0.1.1)

View Source

A Two-Phase Set (2P-Set) CRDT.

Elements can be added and removed, but once removed they cannot be re-added. This is implemented using two G-Sets: one for additions and one for removals (tombstones).

The value is the difference: added - removed.

Limitations

  • Elements cannot be re-added after removal
  • Tombstones grow unboundedly (no garbage collection)

For use cases requiring re-addition, see Grove.Set.ORSet.

Delta-State Support

This CRDT supports delta-state replication:

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

Example

iex> set = Grove.Set.TwoPSet.new(:node_a)
iex> set = Grove.Set.TwoPSet.add(set, "apple")
iex> set = Grove.Set.TwoPSet.add(set, "banana")
iex> set = Grove.Set.TwoPSet.remove(set, "apple")
iex> Grove.Set.TwoPSet.value(set) |> Enum.sort()
["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 2P-Sets.

Creates a new 2P-Set for the given actor.

Removes an element from the set.

Resets the delta buffer after synchronization.

Returns the number of elements in the set.

Returns the elements as a list.

Returns the current elements as a MapSet.

Types

actor()

@type actor() :: term()

t()

@type t() :: %Grove.Set.TwoPSet{
  actor: actor(),
  added: Grove.Set.GSet.t(),
  removed: Grove.Set.GSet.t()
}

Functions

add(set, element)

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

Adds an element to the set.

If the element was previously removed, it will NOT be re-added.

delta(two_p_set)

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

Returns the accumulated delta since the last reset.

member?(two_p_set, element)

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

Checks if an element is in the set.

Returns true if the element has been added and not removed.

merge(set1, set2)

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

Merges two 2P-Sets.

Both the added and removed G-Sets are merged independently.

new(actor)

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

Creates a new 2P-Set for the given actor.

remove(set, element)

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

Removes an element from the set.

Once removed, the element cannot be re-added.

reset_delta(set)

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

Resets the delta buffer after synchronization.

size(set)

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

Returns the number of elements in the set.

to_list(set)

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

Returns the elements as a list.

value(two_p_set)

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

Returns the current elements as a MapSet.

The value is the difference between added and removed elements.