-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add general prepare and commit functions
- Loading branch information
Tobias Kräntzer
committed
Dec 17, 2018
1 parent
7f85245
commit 00b2221
Showing
2 changed files
with
33 additions
and
16 deletions.
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 |
---|---|---|
@@ -1,18 +1,40 @@ | ||
defmodule TwoPhaseCommit do | ||
@moduledoc """ | ||
Documentation for TwoPhaseCommit. | ||
""" | ||
alias TwoPhaseCommit.Store | ||
alias TwoPhaseCommit.Action | ||
|
||
@doc """ | ||
Hello world. | ||
@type store :: module() | ||
@type action :: module() | ||
@type ref :: any() | ||
@type revision :: term() | ||
@type state :: term() | ||
@type transaction :: term() | ||
@type transaction_ref :: term() | ||
@type args :: any() | ||
|
||
## Examples | ||
@type error_reason :: | ||
{:conflict, revision()} | ||
| {:pending, transaction_ref()} | ||
| term() | ||
|
||
iex> TwoPhaseCommit.hello | ||
:world | ||
@type on_error :: {:error, error_reason} | ||
|
||
""" | ||
def hello do | ||
:world | ||
@spec prepare(action(), state(), args(), store(), ref(), revision()) :: | ||
{:ok, transaction_ref()} | ||
| on_error() | ||
def prepare(action, state, args, store, ref, revision) do | ||
with {:ok, transaction} <- action.prepare(state, args), | ||
{:ok, transaction_ref} <- store.prepare(ref, revision, action, transaction) do | ||
{:ok, transaction_ref} | ||
end | ||
end | ||
|
||
@spec commit(action(), state(), transaction(), store(), ref(), transaction_ref()) :: | ||
{:ok, state(), revision(), result :: any()} | ||
| on_error() | ||
def commit(action, state, transaction, store, ref, transaction_ref) do | ||
with {:ok, new_state, result} <- action.commit(state, transaction), | ||
{:ok, revison} <- store.commit(ref, transaction_ref, new_state) do | ||
{:ok, new_state, revison, result} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
defmodule TwoPhaseCommitTest do | ||
use ExUnit.Case | ||
doctest TwoPhaseCommit | ||
|
||
test "greets the world" do | ||
assert TwoPhaseCommit.hello() == :world | ||
end | ||
end |