derekkraan / delta_crdt_ex

Use DeltaCrdt to build distributed applications in Elixir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inconsistency between behavior spec and usage

sleipnir opened this issue · comments

I verified that the DeltaCrdt.Storage Behavior used for persistence is specifying a type called storage_format with the following representation:

defmodule DeltaCrdt.Storage do
  @moduledoc """
  This behaviour can be used to enable persistence of the CRDT.

  This can be helpful in the event of crashes.

  To use, implement this behaviour in a module, and pass it to your CRDT with the `storage_module` option.
  """

  @type t :: module()

  @opaque storage_format ::
            {node_id :: term(), sequence_number :: integer(), crdt_state :: term()}

  @callback write(name :: term(), storage_format()) :: :ok
  @callback read(name :: term()) :: storage_format() | nil
end

Note that the type is basically composed of {node_id :: term(), sequence_number :: integer(), crdt_state :: term()} but checking where this behavior is called I noticed that actually the expected return pattern of the read function is different from the type specified in the behavior. See in DeltaCrdt.CausalCrd:

defp read_from_storage(state) do
    case state.storage_module.read(state.name) do
      nil ->
        state

      {node_id, sequence_number, crdt_state, merkle_map} ->
        Map.put(state, :sequence_number, sequence_number)
        |> Map.put(:crdt_state, crdt_state)
        |> Map.put(:merkle_map, merkle_map)
        |> Map.put(:node_id, node_id)
        |> remove_crdt_state_keys()
    end
  end

In this case the expected return type pattern is {node_id, sequence_number, crdt_state, merkle_map}.

My first question is what is the correct format?