aboutsummaryrefslogtreecommitdiff
path: root/shard/lib/data/data.ex
diff options
context:
space:
mode:
Diffstat (limited to 'shard/lib/data/data.ex')
-rw-r--r--shard/lib/data/data.ex22
1 files changed, 17 insertions, 5 deletions
diff --git a/shard/lib/data/data.ex b/shard/lib/data/data.ex
index 33dca09..8d2b277 100644
--- a/shard/lib/data/data.ex
+++ b/shard/lib/data/data.ex
@@ -10,34 +10,46 @@ defmodule SData do
These functions must only return :duplicate for equal items.
"""
- @doc """
+ @doc"""
Calculate the hash of an Erlang term by first converting it to its
- binary representation.
+ binary representation. Equivalent to `bin_hash(term_bin(term))`.
"""
def term_hash(term, algo \\ :sha256) do
:crypto.hash(algo, (:erlang.term_to_binary term))
end
+ @doc"""
+ Convert any Erlang term to a binary representation.
+ """
def term_bin(term) do
:erlang.term_to_binary term
end
+ @doc"""
+ Calculate the hash of a binary.
+ """
def bin_hash(bin, algo \\ :sha256) do
:crypto.hash(algo, bin)
end
+ @doc"""
+ Calculate the hash of a file.
+ """
def file_hash(path, algo \\ :sha256) do
File.stream!(path, [], 65536)
|> Enum.reduce(:crypto.hash_init(algo), &(:crypto.hash_update(&2, &1)))
|> :crypto.hash_final()
end
+ @doc"""
+ Recover an Erlang term from its binary representation.
+ """
def term_unbin(bin) do
:erlang.binary_to_term(bin, [:safe])
end
@doc"""
- Compare function for arbitrary terms using the Erlang order
+ Compare function for arbitrary terms using the Erlang order
"""
def cmp_term(a, b) do
cond do
@@ -48,7 +60,7 @@ defmodule SData do
end
@doc"""
- Compare function for timestamped strings
+ Compare function for timestamped strings
"""
def cmp_ts_str({ts1, str1}, {ts2, str2}) do
cond do
@@ -61,7 +73,7 @@ defmodule SData do
end
@doc"""
- Merge function for nils
+ Merge function for nils
"""
def merge_true(true, true), do: true
end