Packages
eventstore
1.0.2
1.4.8
1.4.7
1.4.6
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.2
1.3.1
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-rc.0
0.17.0
0.16.2
0.16.1
0.16.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.11.0-rc.0
0.10.1
0.10.0
0.9.0
0.8.1
0.8.0
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.0
0.2.1
0.2.0
0.1.0
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Event store using PostgreSQL for persistence.
Current section
Files
Jump to
Current section
Files
lib/event_store/storage/reader.ex
defmodule EventStore.Storage.Reader do
@moduledoc false
require Logger
alias EventStore.RecordedEvent
alias EventStore.Sql.Statements
alias EventStore.Storage.Reader
@doc """
Read events appended to a single stream forward from the given starting version
"""
def read_forward(conn, stream_id, start_version, count, opts \\ [])
def read_forward(conn, stream_id, start_version, count, opts) do
case Reader.Query.read_events_forward(conn, stream_id, start_version, count, opts) do
{:ok, []} = reply -> reply
{:ok, rows} -> map_rows_to_event_data(rows)
{:error, reason} -> failed_to_read(stream_id, reason)
end
end
defp map_rows_to_event_data(rows) do
{:ok, Reader.EventAdapter.to_event_data(rows)}
end
defp failed_to_read(stream_id, reason) do
Logger.warn(fn ->
"Failed to read events from stream id #{stream_id} due to: #{inspect(reason)}"
end)
{:error, reason}
end
defmodule EventAdapter do
@moduledoc false
@doc """
Map event data from the database to `RecordedEvent` struct
"""
def to_event_data(rows), do: Enum.map(rows, &to_event_data_from_row/1)
def to_event_data_from_row([
event_number,
event_id,
stream_uuid,
stream_version,
event_type,
correlation_id,
causation_id,
data,
metadata,
created_at
]) do
%RecordedEvent{
event_number: event_number,
event_id: event_id |> from_uuid(),
stream_uuid: stream_uuid,
stream_version: stream_version,
event_type: event_type,
correlation_id: correlation_id |> from_uuid(),
causation_id: causation_id |> from_uuid(),
data: data,
metadata: metadata,
created_at: created_at |> from_timestamp()
}
end
defp from_uuid(nil), do: nil
defp from_uuid(uuid), do: UUID.binary_to_string!(uuid)
defp from_timestamp(%DateTime{} = timestamp), do: timestamp
defp from_timestamp(%NaiveDateTime{} = timestamp) do
timestamp |> DateTime.from_naive("Etc/UTC")
end
if Code.ensure_loaded?(Postgrex.Timestamp) do
defp from_timestamp(%Postgrex.Timestamp{} = timestamp) do
%Postgrex.Timestamp{
year: year,
month: month,
day: day,
hour: hour,
min: minute,
sec: second,
usec: microsecond
} = timestamp
with {:ok, naive} <-
NaiveDateTime.new(year, month, day, hour, minute, second, {microsecond, 6}),
{:ok, datetime} <- DateTime.from_naive(naive, "Etc/UTC") do
datetime
end
end
end
end
defmodule Query do
@moduledoc false
def read_events_forward(conn, stream_id, start_version, count, opts) do
query = Statements.read_events_forward()
case Postgrex.query(conn, query, [stream_id, start_version, count], opts) do
{:ok, %Postgrex.Result{num_rows: 0}} ->
{:ok, []}
{:ok, %Postgrex.Result{rows: rows}} ->
{:ok, rows}
{:error, %Postgrex.Error{postgres: %{message: reason}}} ->
Logger.warn(fn -> "Failed to read events from stream due to: #{inspect(reason)}" end)
{:error, reason}
end
end
end
end