From c990961d919d41a2876a7ad9071a6e766ed363f7 Mon Sep 17 00:00:00 2001 From: Sheharyar Naseer Date: Thu, 28 Feb 2019 20:47:08 -0800 Subject: [PATCH 1/4] Catching mnesia exits only for transactions --- lib/memento/mnesia.ex | 17 +++++++++++++++++ lib/memento/transaction.ex | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/memento/mnesia.ex b/lib/memento/mnesia.ex index 0e21800..b8545f2 100644 --- a/lib/memento/mnesia.ex +++ b/lib/memento/mnesia.ex @@ -14,6 +14,23 @@ defmodule Memento.Mnesia do @doc "Call an Mnesia function" defmacro call(method, arguments \\ []) do + quote(bind_quoted: [fun: method, args: arguments]) do + apply(:mnesia, fun, args) + end + end + + + + @doc """ + Call an Mnesia function and catch any exits + + Should ONLY be used with transaction methods, because catching + exits inside transactions seriously impact the performance of + Mnesia. + + Reference: https://github.com/sheharyarn/memento/issues/2 + """ + defmacro call_and_catch(method, arguments \\ []) do quote(bind_quoted: [fun: method, args: arguments]) do require Memento.Error diff --git a/lib/memento/transaction.ex b/lib/memento/transaction.ex index 5d5dccd..a3d3ad9 100644 --- a/lib/memento/transaction.ex +++ b/lib/memento/transaction.ex @@ -68,7 +68,7 @@ defmodule Memento.Transaction do @spec execute(fun, retries) :: {:ok, any} | {:error, any} def execute(function, retries \\ :infinity) do :transaction - |> Memento.Mnesia.call([function, retries]) + |> Memento.Mnesia.call_and_catch([function, retries]) |> Memento.Mnesia.handle_result end @@ -102,7 +102,7 @@ defmodule Memento.Transaction do @spec execute_sync(fun, retries) :: {:ok, any} | {:error, any} def execute_sync(function, retries \\ :infinity) do :sync_transaction - |> Memento.Mnesia.call([function, retries]) + |> Memento.Mnesia.call_and_catch([function, retries]) |> Memento.Mnesia.handle_result end From ebdec4721fd22edfeadcaaad265023c1288eb521 Mon Sep 17 00:00:00 2001 From: Sheharyar Naseer Date: Thu, 28 Feb 2019 21:47:36 -0800 Subject: [PATCH 2/4] Get tests to pass --- lib/memento/schema.ex | 8 ++++---- test/memento/mnesia_test.exs | 14 +++++++++++--- test/support/support.ex | 9 ++++----- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/memento/schema.ex b/lib/memento/schema.ex index 562cc1f..e1ef0a2 100644 --- a/lib/memento/schema.ex +++ b/lib/memento/schema.ex @@ -66,7 +66,7 @@ defmodule Memento.Schema do end :create_schema - |> Memento.Mnesia.call([nodes]) + |> Memento.Mnesia.call_and_catch([nodes]) |> Memento.Mnesia.handle_result end @@ -83,7 +83,7 @@ defmodule Memento.Schema do @spec delete(list(node)) :: :ok | {:error, any} def delete(nodes) do :delete_schema - |> Memento.Mnesia.call([nodes]) + |> Memento.Mnesia.call_and_catch([nodes]) |> Memento.Mnesia.handle_result end @@ -96,7 +96,7 @@ defmodule Memento.Schema do @spec info() :: :ok def info do :schema - |> Memento.Mnesia.call + |> Memento.Mnesia.call_and_catch |> Memento.Mnesia.handle_result end @@ -109,7 +109,7 @@ defmodule Memento.Schema do @spec info(Memento.Table.name) :: :ok def info(table) do :schema - |> Memento.Mnesia.call([table]) + |> Memento.Mnesia.call_and_catch([table]) |> Memento.Mnesia.handle_result end diff --git a/test/memento/mnesia_test.exs b/test/memento/mnesia_test.exs index aae0683..0fe631c 100644 --- a/test/memento/mnesia_test.exs +++ b/test/memento/mnesia_test.exs @@ -7,6 +7,14 @@ defmodule Memento.Tests.Mnesia do describe "#call" do + test "delegates method calls to the mnesia module" do + assert :yes == Mnesia.call(:system_info, [:is_running]) + end + end + + + + describe "#call_and_catch" do setup do Support.Mnesia.stop :ok @@ -16,7 +24,7 @@ defmodule Memento.Tests.Mnesia do @func :system_info @args [:is_running] test "delegates method calls to the mnesia module" do - assert :no == Mnesia.call(@func, @args) + assert :no == Mnesia.call_and_catch(@func, @args) end @@ -24,7 +32,7 @@ defmodule Memento.Tests.Mnesia do @args [] test "re-raises mnesia exits as memento exceptions" do assert_raise(MnesiaException, ~r/not running/i, fn -> - Mnesia.call(@func, @args) + Mnesia.call_and_catch(@func, @args) end) end @@ -33,7 +41,7 @@ defmodule Memento.Tests.Mnesia do @args [Tables.User, :all] test "prints descriptions of the error" do assert_raise(MnesiaException, ~r/tried to perform op on non-existing/i, fn -> - Mnesia.call(@func, @args) + Mnesia.call_and_catch(@func, @args) end) end end diff --git a/test/support/support.ex b/test/support/support.ex index 0e37e80..85cddf1 100644 --- a/test/support/support.ex +++ b/test/support/support.ex @@ -29,6 +29,10 @@ defmodule Memento.Support do end + def transaction(fun) when is_function(fun) do + Memento.Transaction.execute(fun) + end + def transaction({module, method, args}) when is_atom(module) and is_atom(method) do transaction(fn -> @@ -43,10 +47,5 @@ defmodule Memento.Support do end) end - - def transaction(fun) when is_function(fun) do - Memento.Transaction.execute(fun) - end - end end From da0a9733e403a26fa8820185cdac5e39c1e83147 Mon Sep 17 00:00:00 2001 From: Sheharyar Naseer Date: Tue, 5 Mar 2019 12:57:53 -0800 Subject: [PATCH 3/4] Fix typo --- lib/memento/mnesia.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/memento/mnesia.ex b/lib/memento/mnesia.ex index b8545f2..051b116 100644 --- a/lib/memento/mnesia.ex +++ b/lib/memento/mnesia.ex @@ -25,7 +25,7 @@ defmodule Memento.Mnesia do Call an Mnesia function and catch any exits Should ONLY be used with transaction methods, because catching - exits inside transactions seriously impact the performance of + exits inside transactions seriously impacts the performance of Mnesia. Reference: https://github.com/sheharyarn/memento/issues/2 From 357e078631c0998c4dbb9a8cf0f360c407229200 Mon Sep 17 00:00:00 2001 From: Sheharyar Naseer Date: Tue, 5 Mar 2019 12:59:45 -0800 Subject: [PATCH 4/4] Bump version to v0.3.0 --- README.md | 2 +- mix.exs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 11cbc02..8657d35 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Add `:memento` to your list of dependencies in your Mix file: ```elixir def deps do - [{:memento, "~> 0.2.1"}] + [{:memento, "~> 0.3.0"}] end ``` diff --git a/mix.exs b/mix.exs index e0c7432..45e298e 100644 --- a/mix.exs +++ b/mix.exs @@ -3,7 +3,7 @@ defmodule Memento.Mixfile do @app :memento @name "Memento" - @version "0.2.1" + @version "0.3.0" @github "https://github.com/sheharyarn/#{@app}" @author "Sheharyar Naseer" @license "MIT"