Packages
ferricstore
0.9.1
0.11.14
0.11.12
0.11.11
0.11.10
0.11.9
0.11.8
0.11.7
0.11.6
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.3
0.10.2
0.10.1
0.10.0
0.9.1
0.9.0
0.8.0
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.0
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.2.0
0.1.0
FerricFlow durable workflows and queues with native-protocol storage, Raft durability, and Bitcask persistence.
Current section
Files
Jump to
Current section
Files
lib/ferricstore/flow/info_counts.ex
defmodule Ferricstore.Flow.InfoCounts do
@moduledoc false
alias Ferricstore.BatchResult
alias Ferricstore.Flow.Keys
def zero_counts(default_state, terminal_states) do
[default_state, "running" | terminal_states]
|> Map.new(&{String.to_atom(&1), 0})
end
def state_keys(type, partition_key, default_state, terminal_states) do
Enum.map([default_state, "running" | terminal_states], fn state ->
{state, Keys.state_index_key(type, state, partition_key)}
end)
end
def inflight_key(type, partition_key) do
{"inflight", Keys.inflight_index_key(type, partition_key)}
end
def merge_auto({counts_acc, inflight_acc}, counts, inflight) do
merged =
Map.merge(counts_acc, counts, fn _state, left, right ->
left + right
end)
{merged, inflight_acc + inflight}
end
def terminal_keys(state_keys, terminal_states) do
state_keys
|> Enum.filter(fn {state, _key} -> state in terminal_states end)
|> Enum.map(fn {_state, key} -> key end)
end
def validate_counts(expected, counts) do
with {:ok, validated} <-
BatchResult.map_exact(expected, counts, fn _entry, count -> count end),
true <- Enum.all?(validated, &(is_integer(&1) and &1 >= 0)) do
{:ok, validated}
else
false -> {:error, {:invalid_batch_results, counts}}
{:error, _reason} = error -> error
end
end
def merge_terminal_counts(acc, terminal_keys, counts) do
with {:ok, counts} <- validate_counts(terminal_keys, counts) do
{:ok,
terminal_keys
|> Enum.zip(counts)
|> Enum.reduce(acc, fn {key, count}, count_acc ->
Map.update!(count_acc, key, &(&1 + count))
end)}
end
end
end