diff options
author | Alex Auvolat <alex@adnab.me> | 2018-08-31 20:05:27 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-08-31 20:05:42 +0200 |
commit | c83ba74012e38c2fd1c46c063c9c094a78bf9680 (patch) | |
tree | 07a37f73494156b696cba10f00985f56809a4bc1 /lib/data/data.ex | |
parent | 599be4cdaa7b4f0f625cbbc3ffd5c250a8ce98ef (diff) | |
download | shard-c83ba74012e38c2fd1c46c063c9c094a78bf9680.tar.gz shard-c83ba74012e38c2fd1c46c063c9c094a78bf9680.zip |
Custom compare and merge functions for Merkle search tree0.0.1
Diffstat (limited to 'lib/data/data.ex')
-rw-r--r-- | lib/data/data.ex | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/data/data.ex b/lib/data/data.ex index 428957c..c2c659d 100644 --- a/lib/data/data.ex +++ b/lib/data/data.ex @@ -1,6 +1,13 @@ defmodule SData do @moduledoc """ Utility functions + + Compare functions are functions that compares stored items and provides a total order. + They 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 + These functions must only return :duplicate for equal items. """ @doc """ @@ -11,4 +18,32 @@ defmodule SData do :crypto.hash(algo, (:erlang.term_to_binary term)) end + @doc""" + Compare function for arbitrary terms using the Erlang order + """ + def cmp_term(a, b) do + cond do + a > b -> :after + a < b -> :before + a == b -> :duplicate + end + end + + @doc""" + Compare function for timestamped strings + """ + def cmp_ts_str({ts1, str1}, {ts2, str2}) do + cond do + ts1 > ts2 -> :after + ts1 < ts2 -> :before + str1 > str2 -> :after + str1 < str2 -> :before + true -> :duplicate + end + end + + @doc""" + Merge function for nils + """ + def merge_true(true, true), do: true end |