Grove.Schema.Types (Grove v0.1.1)

View Source

Type registry for schema field CRDT types.

Maps schema type atoms to their corresponding CRDT modules and provides helper functions for initializing, reading, and updating field values.

Supported Types

  • :lww_register - Last-writer-wins register for scalar values
  • :mv_register - Multi-value register that exposes conflicts
  • :or_set - Add-wins set for unordered collections
  • :rga_list - Ordered list with concurrent insert support
  • :counter - Positive-negative counter

Example

Grove.Schema.Types.init(:lww_register, "replica_1", "initial value")
# => %Grove.Register.LWWRegister{...}

Summary

Functions

Returns the default value for a field type if none is specified.

Extracts the user-facing value from a CRDT.

Initializes a new CRDT of the given type with an optional initial value.

Returns the CRDT module for the given type.

Returns the list of supported schema types.

Updates a CRDT with a new value.

Checks if the given type is valid.

Types

field_type()

@type field_type() :: :lww_register | :mv_register | :or_set | :rga_list | :counter

Functions

default_value(type)

@spec default_value(field_type()) :: term()

Returns the default value for a field type if none is specified.

extract(crdt)

@spec extract(struct()) :: term()

Extracts the user-facing value from a CRDT.

Uses the Grove.Viewable protocol.

init(type, replica_id, value)

@spec init(field_type(), term(), term()) :: struct()

Initializes a new CRDT of the given type with an optional initial value.

Examples

iex> crdt = Grove.Schema.Types.init(:lww_register, "replica_1", "hello")
iex> Grove.Viewable.value(crdt)
"hello"

iex> crdt = Grove.Schema.Types.init(:or_set, "replica_1", MapSet.new(["a", "b"]))
iex> Grove.Viewable.value(crdt)
#MapSet<["a", "b"]>

module_for!(type)

@spec module_for!(field_type()) :: module()

Returns the CRDT module for the given type.

Raises if the type is invalid.

supported_types()

@spec supported_types() :: [field_type()]

Returns the list of supported schema types.

update(type, crdt, value, replica_id)

@spec update(field_type(), struct(), term(), term()) :: struct()

Updates a CRDT with a new value.

The update semantics depend on the CRDT type:

  • :lww_register - Replaces the value
  • :mv_register - Replaces the value (creates new branch)
  • :or_set - Replaces entire set (clears and re-adds)
  • :rga_list - Replaces entire list (clears and re-inserts)
  • :counter - Sets to the given value (adjusts via increment/decrement)

Examples

iex> crdt = Grove.Schema.Types.init(:lww_register, "r1", "old")
iex> crdt = Grove.Schema.Types.update(:lww_register, crdt, "new", "r1")
iex> Grove.Viewable.value(crdt)
"new"

valid_type?(type)

@spec valid_type?(atom()) :: boolean()

Checks if the given type is valid.