Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion apps/state/lib/state/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ defmodule State.Server do
defmacro __using__(opts) do
indices = Keyword.fetch!(opts, :indices)
hibernate? = Keyword.get(opts, :hibernate, true)
table_type = Keyword.get(opts, :table_type, :bag)

unless table_type in [:bag, :set] do
raise CompileError,
description: "invalid :table_type #{inspect(table_type)}, expected :bag or :set"
end

recordable =
case Keyword.fetch!(opts, :recordable) do
Expand Down Expand Up @@ -164,6 +170,10 @@ defmodule State.Server do
@spec parser :: module | nil
def parser, do: unquote(opts[:parser])

@doc "Table type for the Mnesia table (:bag or :set)."
@spec table_type :: :bag | :set
def table_type, do: unquote(table_type)

# Server functions

@impl GenServer
Expand Down Expand Up @@ -309,13 +319,16 @@ defmodule State.Server do
end

def recreate_table(module, keywords) do
table_type =
if function_exported?(module, :table_type, 0), do: module.table_type(), else: :bag

case :mnesia.create_table(
module,
record_name: Keyword.fetch!(keywords, :record_name),
attributes: Keyword.fetch!(keywords, :attributes),
index: Keyword.fetch!(keywords, :index),
storage_properties: [ets: [{:read_concurrency, true}]],
type: :bag,
type: table_type,
local_content: true
) do
{:atomic, :ok} ->
Expand Down
3 changes: 2 additions & 1 deletion apps/state/lib/state/stop_event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ defmodule State.StopEvent do
use State.Server,
indices: [:id, :trip_id, :stop_id, :route_id, :vehicle_id],
parser: Parse.StopEvents,
recordable: Model.StopEvent
recordable: Model.StopEvent,
table_type: :set

alias Model.Route
alias Model.Stop
Expand Down
Loading