Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/loom/awormap.ex
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ defmodule Loom.AWORMap do
@spec value(t) :: [{key,term}] | nil
def value(%M{dots: d}) do
res = Enum.reduce(Dots.dots(d), %{}, fn {_, {k, %{__struct__: module}=crdt}}, values ->
Dict.update(values, {k, module}, crdt, &CRDT.join(crdt,&1))
Map.update(values, {k, module}, crdt, &CRDT.join(crdt,&1))
end)
|> Enum.map(fn {k,v} -> {k, CRDT.value(v)} end)
|> Enum.into %{}
|> Enum.into(%{})
case map_size(res) do
0 -> nil
_ -> res
Expand Down
22 changes: 11 additions & 11 deletions lib/loom/dots.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ defmodule Loom.Dots do
"""
@spec add({t, t}, actor, value) :: {t, t}
def add({%Dots{dots: d, ctx: ctx}=dots, delta_dots}, actor, value) do
clock = Dict.get(ctx, actor, 0) + 1 # What's the value of our clock?
clock = Map.get(ctx, actor, 0) + 1 # What's the value of our clock?
dot = {actor, clock}
new_dots = %Dots{dots|
dots: Dict.put(d, dot, value), # Add the value to the dot values
ctx: Dict.put(ctx, actor, clock) # Add the actor/clock to the context
dots: Map.put(d, dot, value), # Add the value to the dot values
ctx: Map.put(ctx, actor, clock) # Add the actor/clock to the context
}
# A new changeset
new_delta = %Dots{dots: Dict.put(%{}, dot, value), cloud: [dot]}
new_delta = %Dots{dots: Map.put(%{}, dot, value), cloud: [dot]}
|> join(delta_dots)
|> compact
{new_dots, new_delta}
Expand All @@ -90,7 +90,7 @@ defmodule Loom.Dots do
{d, [dot|cloud]}
else
# Reinsert, don't worry about causation dot
{Dict.put(d, dot, v), cloud}
{Map.put(d, dot, v), cloud}
end
end)
new_dots = %Dots{dots|dots: new_d}
Expand All @@ -114,7 +114,7 @@ defmodule Loom.Dots do
@spec remove({t, t}) :: {t, t}
def remove({%Dots{dots: d}=dots, %Dots{}=delta}) do
new_dots = %Dots{dots|dots: %{}}
new_delta = join(delta, %Dots{cloud: Dict.keys(d)})
new_delta = join(delta, %Dots{cloud: Map.keys(d)})
{new_dots, new_delta}
end

Expand All @@ -130,13 +130,13 @@ defmodule Loom.Dots do
case {ctx[actor], clock} do
{nil, 1} ->
# We can merge nil with 1 in the cloud
compact_reduce(cloud, Dict.put(ctx, actor, clock), cloud_acc)
compact_reduce(cloud, Map.put(ctx, actor, clock), cloud_acc)
{nil, _} ->
# Can't do anything with this
compact_reduce(cloud, ctx, [dot|cloud_acc])
{ctx_clock, _} when ctx_clock + 1 == clock ->
# Add to context, delete from cloud
compact_reduce(cloud, Dict.put(ctx, actor, clock), cloud_acc)
compact_reduce(cloud, Map.put(ctx, actor, clock), cloud_acc)
{ctx_clock, _} when ctx_clock >= clock -> # Dominates
# Delete from cloud by not accumulating.
compact_reduce(cloud, ctx, cloud_acc)
Expand All @@ -148,7 +148,7 @@ defmodule Loom.Dots do

defp do_join(%Dots{dots: d1, ctx: ctx1, cloud: c1}=dots1, %Dots{dots: d2, ctx: ctx2, cloud: c2}=dots2) do
new_dots = do_join_dots(Enum.sort(d1), Enum.sort(d2), {dots1, dots2}, [])
new_ctx = Dict.merge(ctx1, ctx2, fn (_, a, b) -> max(a, b) end)
new_ctx = Map.merge(ctx1, ctx2, fn (_, a, b) -> max(a, b) end)
new_cloud = Enum.uniq(c1 ++ c2)
compact(%Dots{dots: new_dots, ctx: new_ctx, cloud: new_cloud})
end
Expand All @@ -159,14 +159,14 @@ defmodule Loom.Dots do
# Remove when the other knows about our dots in context/cloud, but isn't in
# their dot values list (they observed a remove)
new_d1 = Enum.reject(d1, fn ({dot, _}) -> dotin(dots2, dot) end)
Enum.reverse(acc, new_d1) |> Enum.into %{}
Enum.reverse(acc, new_d1) |> Enum.into(%{})
end
# If we run out of d1
defp do_join_dots([], d2, {dots1, _}, acc) do
# Add dot when it is only at the other side. This happens when they've got
# values that we do not.
new_d1 = Enum.reject(d2, fn ({dot, _}) -> dotin(dots1, dot) end)
Enum.reverse(acc, new_d1) |> Enum.into %{}
Enum.reverse(acc, new_d1) |> Enum.into(%{})
end
# Always advance d1 when dot1 < dot2
defp do_join_dots([{dot1,value1}|d1], [{dot2,_}|_]=d2, {_, dots2}=dots, acc) when dot1 < dot2 do
Expand Down
6 changes: 3 additions & 3 deletions lib/loom/gcounter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule Loom.GCounter do
"""
@spec new([values: [{actor, pos_integer}]]) :: t
def new(opts) do
new_values = Keyword.get(opts, :values, []) |> Enum.into %{}
new_values = Keyword.get(opts, :values, []) |> Enum.into(%{})
%Counter{counter: new_values}
end

Expand All @@ -71,7 +71,7 @@ defmodule Loom.GCounter do
"""
@spec value(t) :: non_neg_integer
def value(%Counter{counter: c}) do
Dict.values(c) |> Enum.sum
Map.values(c) |> Enum.sum
end

@doc """
Expand All @@ -88,7 +88,7 @@ defmodule Loom.GCounter do
"""
@spec join(t, t) :: t
def join(%Counter{counter: c1}, %Counter{counter: c2}) do
%Counter{counter: Dict.merge(c1, c2, fn (_,v1,v2) -> max(v1,v2) end)}
%Counter{counter: Map.merge(c1, c2, fn (_,v1,v2) -> max(v1,v2) end)}
end

end
Expand Down
6 changes: 3 additions & 3 deletions lib/loom/lwwregister.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Loom.LWWRegister do

"""
@spec new(term) :: t
def new(value), do: new |> set(value)
def new(value), do: new() |> set(value)

@doc """
Returns a new LWWRegister CRDT. Initializes to `value` with another clock
Expand All @@ -48,7 +48,7 @@ defmodule Loom.LWWRegister do

"""
@spec new(term, pos_integer) :: t
def new(value, clock), do: new |> set(value, clock)
def new(value, clock), do: new() |> set(value, clock)


@doc """
Expand All @@ -62,7 +62,7 @@ defmodule Loom.LWWRegister do

"""
@spec set(t, term) :: t
def set(reg, value), do: set(reg, value, make_microtime)
def set(reg, value), do: set(reg, value, make_microtime())

@doc """
Set a value according to your own clock.
Expand Down
12 changes: 6 additions & 6 deletions lib/loom/pncounter.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Loom.PNCounter do
@moduledoc """
Positive-negative counters

PNCounters are counters that are capable of incrementing and decrementing.
They are useful for like counters, where a user may like and then unlike
something in succession.
Expand Down Expand Up @@ -55,8 +55,8 @@ defmodule Loom.PNCounter do
end
{p, n} = Enum.reduce(values, {%{},%{}}, fn {actor, {inc,dec}}, {p, n} ->
{
(if inc, do: Dict.put(p, actor, inc), else: p),
(if dec, do: Dict.put(n, actor, dec), else: n)
(if inc, do: Map.put(p, actor, inc), else: p),
(if dec, do: Map.put(n, actor, dec), else: n)
}
end)
%Counter{p: p, n: n}
Expand Down Expand Up @@ -106,7 +106,7 @@ defmodule Loom.PNCounter do
"""
@spec value(t) :: integer
def value(%Counter{p: p, n: n}) do
(Dict.values(p) |> Enum.sum) - (Dict.values(n) |> Enum.sum)
(Map.values(p) |> Enum.sum) - (Map.values(n) |> Enum.sum)
end

@doc """
Expand All @@ -122,8 +122,8 @@ defmodule Loom.PNCounter do
@spec join(t, t) :: t
def join(%Counter{p: p1, n: n1}, %Counter{p: p2, n: n2}) do
%Counter{
p: Dict.merge(p1, p2, fn (_,v1,v2) -> max(v1,v2) end),
n: Dict.merge(n1, n2, fn (_,v1,v2) -> max(v1,v2) end)
p: Map.merge(p1, p2, fn (_,v1,v2) -> max(v1,v2) end),
n: Map.merge(n1, n2, fn (_,v1,v2) -> max(v1,v2) end)
}
end

Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ defmodule Loom.Mixfile do
def project do
[app: :loom,
description: "A modern CRDT library that uses protocols to create composable CRDTs.",
package: package,
package: package(),
version: "0.1.0-dev",
elixir: "~> 1.0",
deps: deps,
deps: deps(),
test_coverage: [tool: ExCoveralls],
# docs: [readme: true, main: "README"]
]
Expand Down
4 changes: 2 additions & 2 deletions test/loom_gcounter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ defmodule LoomGcounterTest do
# IO.inspect {:call, :inc, [actor, int]}
# IO.inspect val[actor]
# val
# |> Dict.put(:flat, f+int)
# |> Dict.put(actor, Counter.inc(val[actor], actor, int))
# |> Map.put(:flat, f+int)
# |> Map.put(actor, Counter.inc(val[actor], actor, int))
# end
#
# def precondition(val, {:call, _, :inc, [_, _]}) do
Expand Down