aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-09-01 15:28:05 +0200
committerAlex Auvolat <alex@adnab.me>2018-09-01 15:28:05 +0200
commitdd078cfecbe07f1e4d4e002467707ed53aeff180 (patch)
tree0a2af29409942e62f42e50b534688c21c5579ee6
parent0b5d43e1857e541af58575a7e1c2bbe729436b15 (diff)
downloadshard-dd078cfecbe07f1e4d4e002467707ed53aeff180.tar.gz
shard-dd078cfecbe07f1e4d4e002467707ed53aeff180.zip
Make merkle search tree work over network
-rw-r--r--lib/app/blockstore.ex2
-rw-r--r--lib/data/merklesearchtree.ex1
-rw-r--r--lib/manager.ex3
-rw-r--r--test/mst_test.exs20
4 files changed, 24 insertions, 2 deletions
diff --git a/lib/app/blockstore.ex b/lib/app/blockstore.ex
index 4bef0b5..8e4fddc 100644
--- a/lib/app/blockstore.ex
+++ b/lib/app/blockstore.ex
@@ -112,7 +112,7 @@ defmodule SApp.BlockStore do
peers = :ets.lookup(:shard_peer_db, state.shard_id)
|> Enum.shuffle
|> Enum.take(3)
- for peer <- peers do
+ for {_, peer} <- peers do
Shard.Manager.send(peer, {state.shard_id, state.path, {:get, key}})
end
end
diff --git a/lib/data/merklesearchtree.ex b/lib/data/merklesearchtree.ex
index 3676244..941d31d 100644
--- a/lib/data/merklesearchtree.ex
+++ b/lib/data/merklesearchtree.ex
@@ -232,6 +232,7 @@ defmodule SData.MerkleSearchTree do
{ [], [ {k, v, r} | rst2 ] } ->
{low1l, low1h, store} = split(s1, store, low1, k)
{store, newlow} = merge_aux(s1, s2, store, low1l, low2, callback)
+ callback.(k, v)
{store, newr, newrst} = merge_aux_rec(s1, s2, store, low1h, [], r, rst2, callback)
{store, newlow, [ {k, v, newr} | newrst ]}
{ [ {k, v, r} | rst1 ], [] } ->
diff --git a/lib/manager.ex b/lib/manager.ex
index 7c6c849..82984d6 100644
--- a/lib/manager.ex
+++ b/lib/manager.ex
@@ -131,7 +131,8 @@ defmodule Shard.Manager do
[{:<, :'$1', currtime - 60}],
[:'$1']}]
:ets.select_delete(state.outbox, outbox_cleanup)
- _ -> nil
+ _ ->
+ Logger.info "Dropping message #{inspect msg} for peer #{inspect peer_id}: peer not in database"
end
{:noreply, state}
end
diff --git a/test/mst_test.exs b/test/mst_test.exs
index cf5a898..c1758ad 100644
--- a/test/mst_test.exs
+++ b/test/mst_test.exs
@@ -174,4 +174,24 @@ defmodule ShardTest.MST do
assert MST.last(mg1, nil, 2000) == all_items
end
+ test "merkle search tree 8: MST.merge callback" do
+ items1 = (for i <- 1..1000, do: i*2+40)
+ items2 = (for i <- 1..1000, do: i*3)
+
+ y = Enum.reduce(items1, %MST{}, fn i, acc -> MST.insert(acc, i) end)
+ z = Enum.reduce(items2, %MST{}, fn i, acc -> MST.insert(acc, i) end)
+
+ {:ok, cb_called} = Agent.start_link fn -> [] end
+
+ cb = fn i, true -> Agent.update(cb_called, fn x -> [i | x] end) end
+ mg = MST.merge(y, z, cb)
+
+ cb_vals = Agent.get cb_called, &(&1)
+ expected = MapSet.difference(MapSet.new(items2), MapSet.new(items1))
+ |> MapSet.to_list
+ |> Enum.sort
+ |> Enum.reverse
+ assert expected == cb_vals
+ end
+
end