aboutsummaryrefslogtreecommitdiff
path: root/lib/app/blockstore.ex_
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-07-19 17:08:23 +0200
committerAlex Auvolat <alex@adnab.me>2018-07-19 17:08:23 +0200
commit058bab0d7097405126566360308ace986c18ff8e (patch)
treeeaf3ca0d607829af3ad07bdb51bb170b70f8eef5 /lib/app/blockstore.ex_
parent582f1d65463f8f5cbcc34c6129670b473793c4dd (diff)
downloadshard-058bab0d7097405126566360308ace986c18ff8e.tar.gz
shard-058bab0d7097405126566360308ace986c18ff8e.zip
Refactoring ; template for block store
Diffstat (limited to 'lib/app/blockstore.ex_')
-rw-r--r--lib/app/blockstore.ex_78
1 files changed, 78 insertions, 0 deletions
diff --git a/lib/app/blockstore.ex_ b/lib/app/blockstore.ex_
new file mode 100644
index 0000000..2854161
--- /dev/null
+++ b/lib/app/blockstore.ex_
@@ -0,0 +1,78 @@
+defmodule SApp.BlockStore do
+ @moduledoc """
+ A module that implements a content-adressable storage (blocks, or pages,
+ identified by the hash of their contents).
+
+ Establishes full node connectivity and uses rendez-vous hashing to select
+ which nodes are responsible of a given hash.
+
+ TODO: WIP
+ """
+
+ use GenServer
+
+ defmodule State do
+ defstruct [:name, :id, :manifest,
+ :ncopies,
+ :store, :peers]
+ end
+
+
+ def start_link(name) do
+ GenServer.start_link(__MODULE__, name)
+ end
+
+ def init(name) do
+ manifest = {:blockstore, name}
+ id = SData.term_hash manifest
+
+ GenServer.cast(Shard.Manager, {:register, id, self()})
+ GenServer.cast(self(), :init_pull)
+
+ {:ok, %State{name: name, id: id, manifest: manifest,
+ ncopies: 3,
+ store: %{}, peers: %{}}}
+ end
+
+ def handle_call(:manifest, _from, state) do
+ {:reply, state.manifest, state}
+ end
+
+ def handle_call({:get, key}, from, state) do
+ # TODO
+ end
+
+ def handle_call({:put, val}, state) do
+ # TODO
+ end
+
+ def handle_cast({:redundant, _}, _state) do
+ exit :normal
+ end
+
+ def handle_cast(:init_pull, state) do
+ GenServer.call(SNet.Manager, :get_all)
+ |> Enum.each(&(GenServer.cast(&1, {:send_msg, {:interested, [state.id]}})))
+ {:noreply, state}
+ end
+
+ def handle_cast({:interested, peer_id, peer_pid}, state) do
+ new_peers = Map.put(state.peers, peer_id, peer_pid)
+ new_state = %{ state | peers: new_peers }
+ initial_sync(new_state, peer_id, peer_pid)
+ {:noreply, new_state}
+ end
+
+ def handle_cast({:msg, peer_id, peer_pid, msg}, state) do
+ # TODO
+ {:noreply, state}
+ end
+
+ defp initial_sync(state, peer_id, peer_pid) do
+ # TODO
+ end
+
+ defp send(state, to, msg) do
+ GenServer.cast(to, {:send_msg, {state.id, msg}})
+ end
+end