aboutsummaryrefslogtreecommitdiff
path: root/lib/data
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-07-04 14:45:01 +0200
committerAlex Auvolat <alex@adnab.me>2018-07-04 14:45:01 +0200
commitd786bbd6e43d88b50085d1ecce6288a617bebbe8 (patch)
tree886ad2b2c5c9371bb5122b0be8bda56b525e2ea1 /lib/data
parentb234a360dafa0a65d797614aad5a4514570784f8 (diff)
downloadshard-d786bbd6e43d88b50085d1ecce6288a617bebbe8.tar.gz
shard-d786bbd6e43d88b50085d1ecce6288a617bebbe8.zip
Document
Diffstat (limited to 'lib/data')
-rw-r--r--lib/data/data.ex14
-rw-r--r--lib/data/merklelist.ex29
2 files changed, 32 insertions, 11 deletions
diff --git a/lib/data/data.ex b/lib/data/data.ex
new file mode 100644
index 0000000..2c6e629
--- /dev/null
+++ b/lib/data/data.ex
@@ -0,0 +1,14 @@
+defmodule SData do
+ @moduledoc """
+ Utility functions
+ """
+
+ @doc """
+ Calculate the hash of an Erlang term by first converting it to its
+ binary representation.
+ """
+ def term_hash(term) do
+ :crypto.hash(:sha256, (:erlang.term_to_binary term))
+ end
+
+end
diff --git a/lib/data/merklelist.ex b/lib/data/merklelist.ex
index 42b80fc..727f2a8 100644
--- a/lib/data/merklelist.ex
+++ b/lib/data/merklelist.ex
@@ -1,26 +1,33 @@
defmodule SData.MerkleList do
+ @moduledoc """
+ A simple Merkle list store.
+
+ Future improvements:
+ - When messages are inserted other than at the top, all intermediate hashes
+ change. Keep track of the mapping from old hashes to new hashes so that get
+ requests can work even for hashes that are not valid anymore.
+ - group items in "pages" (bigger bundles)
+ """
+
use GenServer
- def start_link(cmp) do
- GenServer.start_link(__MODULE__, cmp)
- end
+ @doc """
+ Start a Merkle List storage process.
- defp term_hash(term) do
- :crypto.hash(:sha256, (:erlang.term_to_binary term))
- end
- @doc """
- Initialize a Merkle List storage.
`cmp` is a function that compares stored items and provides a total order.
-
It must return:
- `:after` if the first argument is more recent
- `:duplicate` if the two items are the same
- `:before` if the first argument is older
"""
+ def start_link(cmp) do
+ GenServer.start_link(__MODULE__, cmp)
+ end
+
def init(cmp) do
root_item = :root
- root_hash = term_hash root_item
+ root_hash = SData.term_hash root_item
state = %{
root: root_hash,
top: root_hash,
@@ -32,7 +39,7 @@ defmodule SData.MerkleList do
defp state_push(item, state) do
new_item = {item, state.top}
- new_item_hash = term_hash new_item
+ new_item_hash = SData.term_hash new_item
new_store = Map.put(state.store, new_item_hash, new_item)
%{ state | :top => new_item_hash, :store => new_store }
end