diff options
Diffstat (limited to 'shard/lib/manager.ex')
-rw-r--r-- | shard/lib/manager.ex | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/shard/lib/manager.ex b/shard/lib/manager.ex index 7aa3758..57f2371 100644 --- a/shard/lib/manager.ex +++ b/shard/lib/manager.ex @@ -17,6 +17,11 @@ defmodule Shard.Manager do List of { id, manifest, pid | nil } + - :shard_state + + List of + { id, state } + - :shard_procs List of @@ -43,6 +48,7 @@ defmodule Shard.Manager do @peer_db [Application.get_env(:shard, :data_path), "peer_db"] |> Path.join |> String.to_atom @shard_db [Application.get_env(:shard, :data_path), "shard_db"] |> Path.join |> String.to_atom + @shard_state [Application.get_env(:shard, :data_path), "shard_state"] |> Path.join |> String.to_atom @shard_peer_db [Application.get_env(:shard, :data_path), "shard_peer_db"] |> Path.join |> String.to_atom def start_link(my_port) do @@ -63,6 +69,7 @@ defmodule Shard.Manager do spawn fn -> Shard.Manifest.start manifest end end + :dets.open_file(@shard_state, [type: :set]) :dets.open_file(@shard_peer_db, [type: :bag]) :ets.new(:shard_procs, [:set, :protected, :named_table]) @@ -260,15 +267,41 @@ defmodule Shard.Manager do GenServer.cast(__MODULE__, {:dispatch_to, shard_id, path, pid}) end + @doc""" + Return the list of all shards. + """ def list_shards() do for [x] <- :dets.match(@shard_db, :"$1"), do: x end + @doc""" + Return the list of all peers + """ def list_peers() do for [x] <- :dets.match(@peer_db, :"$1"), do: x end + @doc""" + Return the list of all peer IDs that are interested in a certain shard + """ def get_shard_peers(shard_id) do for [x] <- :dets.match(@shard_peer_db, {shard_id, :"$1"}), do: x end + + @doc""" + Return the saved state value for a shard + """ + def load_state(shard_id) do + case :dets.lookup(@shard_state, shard_id) do + [{^shard_id, state}] -> state + _ -> nil + end + end + + @doc""" + Save a state value for a shard + """ + def save_state(shard_id, state) do + :dets.insert(@shard_state, {shard_id, state}) + end end |