Skip to content

Commit

Permalink
Add general prepare and commit functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Kräntzer committed Dec 17, 2018
1 parent 7f85245 commit 00b2221
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
44 changes: 33 additions & 11 deletions lib/two_phase_commit.ex
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
5 changes: 0 additions & 5 deletions test/two_phase_commit_test.exs
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

0 comments on commit 00b2221

Please sign in to comment.