Skip to content

Commit

Permalink
feat: aporte mnesia driven adapter y behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
bbatist-ban committed Apr 9, 2024
1 parent 2c288d7 commit 41b0563
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/domain/behaviours/mnesia/mnesia.ex
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
105 changes: 105 additions & 0 deletions lib/infrastructure/driven_adapters/mnesia/mnesia.ex
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

View workflow job for this annotation

GitHub Actions / build

Modules should have a @moduledoc tag.
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)

Check warning on line 78 in lib/infrastructure/driven_adapters/mnesia/mnesia.ex

View workflow job for this annotation

GitHub Actions / build

There are spaces around operators most of the time, but not here.
opt =="write_table" -> add_to_tuple_by_index(to_tuple(data), 0, table)

Check warning on line 79 in lib/infrastructure/driven_adapters/mnesia/mnesia.ex

View workflow job for this annotation

GitHub Actions / build

There are spaces around operators most of the time, but not here.
opt =="table_key_data" -> {table, data}

Check warning on line 80 in lib/infrastructure/driven_adapters/mnesia/mnesia.ex

View workflow job for this annotation

GitHub Actions / build

There are spaces around operators most of the time, but not here.
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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule DistributedPerformanceAnalyzer.MixProject do
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger, :opentelemetry_exporter, :opentelemetry],
extra_applications: [:mnesia, :logger, :opentelemetry_exporter, :opentelemetry],
mod: {DistributedPerformanceAnalyzer.Application, [Mix.env()]}
]
end
Expand Down

0 comments on commit 41b0563

Please sign in to comment.