-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: aporte mnesia driven adapter y behaviour
- Loading branch information
1 parent
2c288d7
commit 41b0563
Showing
3 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule DistributedPerformanceAnalyzer.Domain.Behaviours.MnesiaBehaviour do | ||
@moduledoc """ | ||
Definitions of operations on MnesiaAdapter | ||
""" | ||
|
||
@callback create_table(table :: term, attributes :: (list() | tuple())) :: nil | ||
@callback write_table(table :: term, data :: (list() | tuple())) :: nil | ||
@callback write_table_multiple(table :: term, data :: (list() | tuple())) :: nil | ||
@callback read_table_data_by_id(table :: term, key :: integer) :: nil | ||
@callback generate_data(opt :: String.t(), table :: term, data :: (list() | tuple() | :integer)) :: (list() | tuple()) | ||
@callback add_to_tuple_by_index(tuple :: tuple(), index :: integer, value :: any()) :: tuple() | ||
@callback to_tuple(obj :: list()) :: tuple() | ||
@callback to_list(tuple :: tuple()) :: list() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
defmodule DistributedPerformanceAnalyzer.Infrastructure.Adapters.MnesiaAdapter do | ||
Check warning on line 1 in lib/infrastructure/driven_adapters/mnesia/mnesia.ex GitHub Actions / build
|
||
use GenServer | ||
|
||
@behaviour DistributedPerformanceAnalyzer.Domain.Behaviours.MnesiaBehaviour | ||
|
||
alias :mnesia, as: Mnesia | ||
|
||
def start_link do | ||
case GenServer.start_link(__MODULE__, :ok, name: __MODULE__) do | ||
{:ok, _pid} -> IO.puts("MnesiaAdapter started") | ||
{:error, reason} -> IO.puts("MnesiaAdapter failed to start: #{inspect(reason)}") | ||
end | ||
end | ||
|
||
@impl true | ||
def init(_args) do | ||
Mnesia.create_schema([node()]) | ||
Mnesia.start() | ||
end | ||
|
||
@impl true | ||
def create_table(table, attributes) do | ||
case Mnesia.create_table(table, attributes: generate_data("create_table", table, attributes)) do | ||
{:atomic, :ok} -> IO.puts("Table #{table} created with attributes #{inspect(attributes)}") | ||
{:error, reason} -> IO.puts("Table #{table} failed to create: #{inspect(reason)}") | ||
end | ||
end | ||
# exmaple create_table | ||
# Mnesia.create_table(:table_name, [Array of attributes]) | ||
# MnesiaAdapter.create_table(:users, [:id, :name, :email]) | ||
# MnesiaAdapter.create_table(:users, {:id, :name, :email}) | ||
|
||
@impl true | ||
def write_table(table, data) do | ||
case Mnesia.dirty_write(generate_data("write_table", table, data)) do | ||
:ok -> IO.puts("Write to table #{table} with data #{inspect(data)}") | ||
{:error, reason} -> IO.puts("Write to table with data #{inspect(data)} failed: #{inspect(reason)}") | ||
{:aborted, reason} -> {:reply, {:error, reason}} | ||
{:bad_type, reason} -> {:reply, {:error, reason}} | ||
end | ||
end | ||
# example write | ||
# Mnesia.dirty_write_table({Person, 1, "Homer Simpson", "Safety Inspector"}) | ||
# MnesiaAdapter.write_table(Person, {1, "Homer Simpson", "Safety Inspector"}) | ||
# MnesiaAdapter.write_table(Person, [1, "Homer Simpson", "Safety Inspector"]) | ||
|
||
@impl true | ||
def write_table_multiple(table, data) do | ||
#Enum.each(data, fn row -> :mnesia.activity(:transaction, fn -> MnesiaAdapter.write_table(table, row) end, []) end) | ||
for row <- data do | ||
case Mnesia.activity(:transaction, fn -> write_table(table, row) end, []) do | ||
:ok -> :ok | ||
{:error, reason} -> IO.puts("Write to table with data #{inspect(data)} failed: #{inspect(reason)}") | ||
{:aborted, reason} -> {:reply, {:error, reason}} | ||
{:bad_type, reason} -> {:reply, {:error, reason}} | ||
end | ||
end | ||
end | ||
|
||
@impl true | ||
def read_table_data_by_id(table, key) do | ||
case Mnesia.dirty_read(generate_data("table_key_data", table, key)) do | ||
result when is_list(result) -> IO.puts("Read from table #{table} with key #{key} -> #{inspect(result)}") | ||
#result -> IO.puts("Read from table with data #{inspect(data_read)} -> #{inspect(result)}") | ||
{:error, reason} -> IO.puts("Read from table #{table} with key #{key} failed: #{inspect(reason)}") | ||
{:aborted, reason} -> {:reply, {:error, reason}} | ||
{:bad_type, reason} -> {:reply, {:error, reason}} | ||
end | ||
end | ||
# example read | ||
# Mnesia.dirty_read_table_data_by_id({:table_name, key}) | ||
# MnesiaAdapter.read_table_data_by_id(:users, 1) --> [{1, "Homer Simpson", "Safety Inspector"}] | ||
# MnesiaAdapter.read_table_data_by_id(:users, 2) --> [] | ||
|
||
@impl true | ||
def generate_data(opt, table, data) do | ||
cond do | ||
opt =="create_table" -> to_list(data) | ||
opt =="write_table" -> add_to_tuple_by_index(to_tuple(data), 0, table) | ||
opt =="table_key_data" -> {table, data} | ||
true -> data | ||
end | ||
end | ||
|
||
@impl true | ||
def add_to_tuple_by_index(tuple, index, value) do | ||
Tuple.insert_at(tuple, index, value) | ||
end | ||
|
||
@impl true | ||
def to_tuple(obj) do | ||
cond do | ||
is_list(obj) -> List.to_tuple(obj) | ||
true -> obj | ||
end | ||
end | ||
|
||
@impl true | ||
def to_list(obj) do | ||
cond do | ||
is_tuple(obj) -> Tuple.to_list(obj) | ||
true -> obj | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters