Hume.EventStore behaviour (hume v0.0.9)

View Source

Behaviour module for event stores.

An event store is responsible for storing and retrieving events in a stream. Each event has a sequence number that is strictly increasing within a stream. Events must be appended in order.

Summary

Callbacks

Append a batch of events to the stream.

Get all events from the stream starting from the given sequence number (exclusive).

Get the next sequence number.

Functions

Appends a single event or a batch of ordered events to the given stream.

Assigns sequence numbers to a single payload or a list of payloads for the given stream.

Validates that the given module implements the required functions of the Hume.EventStore behaviour.

Types

event()

@type event() :: {seq(), payload()}

payload()

@type payload() :: term()

seq()

@type seq() :: non_neg_integer()

stream()

@type stream() :: term()

Callbacks

append_batch(stream, ordered)

@callback append_batch(stream(), Hume.EventOrder.ordered()) :: :ok | {:error, term()}

Append a batch of events to the stream.

The events must be strictly ordered by sequence number.

events(stream, from)

@callback events(stream(), from :: seq()) :: Hume.EventOrder.ordered()

Get all events from the stream starting from the given sequence number (exclusive).

The events are returned in strictly ascending order by sequence number.

next_sequence()

@callback next_sequence() :: seq()

Get the next sequence number.

This should return the next available sequence number. 0 if the stream is empty, otherwise the last sequence number + 1.

Functions

append(mod, stream, event)

@spec append(module(), stream(), event() | Hume.EventOrder.ordered()) ::
  :ok | {:error, term()}

Appends a single event or a batch of ordered events to the given stream.

  • If given a single event {seq, payload}, it appends it as a batch of one event.
  • If given an ordered list of events {:ordered, [events]}, it appends them as a batch.

Ensures the events are appended in order.

number(mod, payloads)

@spec number(module(), [payload()]) :: Hume.EventOrder.ordered()
@spec number(module(), payload()) :: event()

Assigns sequence numbers to a single payload or a list of payloads for the given stream.

  • If given a single payload, it returns a single event tuple {seq, payload}
  • If given a list of payloads, it returns an ordered list of events {:ordered, [events]}

Ensures the events are assigned in order.

validate(mod)

@spec validate(module()) :: :ok | {:error, :invalid_module}

Validates that the given module implements the required functions of the Hume.EventStore behaviour.