diff options
Diffstat (limited to 'lib/polyjuice')
-rw-r--r-- | lib/polyjuice/client/storage.ex | 14 | ||||
-rw-r--r-- | lib/polyjuice/client/storage/dets.ex | 12 | ||||
-rw-r--r-- | lib/polyjuice/client/storage/ets.ex | 12 |
3 files changed, 26 insertions, 12 deletions
diff --git a/lib/polyjuice/client/storage.ex b/lib/polyjuice/client/storage.ex index 9172e5b..21a32c0 100644 --- a/lib/polyjuice/client/storage.ex +++ b/lib/polyjuice/client/storage.ex @@ -63,13 +63,19 @@ defprotocol Polyjuice.Client.Storage do @doc """ Store data for a specific key. """ - @spec kv_put(storage :: __MODULE__.t(), key :: String, value :: __MODULE__.value()) :: any - def kv_put(storage, key, value) + @spec kv_put(__MODULE__.t(), String.t(), String.t(), __MODULE__.value()) :: any + def kv_put(storage, namespace, key, value) @doc """ Get the data for a specific key. """ - @spec kv_get(storage :: __MODULE__.t(), key :: String, default :: __MODULE__.value()) :: + @spec kv_get(__MODULE__.t(), String.t(), String.t(), __MODULE__.value()) :: __MODULE__.value() - def kv_get(storage, key, default \\ nil) + def kv_get(storage, namespace, key, default \\ nil) + + @doc """ + Delete the data for a specific key. + """ + @spec kv_del(__MODULE__.t(), String.t(), String.t()) :: __MODULE__.value() + def kv_del(storage, namespace, key) end diff --git a/lib/polyjuice/client/storage/dets.ex b/lib/polyjuice/client/storage/dets.ex index 71999ee..c6d578f 100644 --- a/lib/polyjuice/client/storage/dets.ex +++ b/lib/polyjuice/client/storage/dets.ex @@ -67,15 +67,19 @@ defmodule Polyjuice.Client.Storage.Dets do end end - def kv_put(%{table: table}, key, value) when is_binary(key) do - :dets.insert(table, {"kv_" <> key, value}) + def kv_put(%{table: table}, namespace, key, value) when is_binary(namespace) and is_binary(key) do + :dets.insert(table, {{"kv", namespace, key}, value}) end - def kv_get(%{table: table}, key, default \\ nil) when is_binary(key) do - case :dets.lookup(table, "kv_" <> key) do + def kv_get(%{table: table}, namespace, key, default \\ nil) when is_binary(namespace) and is_binary(key) do + case :dets.lookup(table, {"kv", namespace, key}) do [{_, value}] -> value _ -> default end end + + def kv_del(%{table: table}, namespace, key) when is_binary(namespace) and is_binary(key) do + :dets.delete(table, {"kv", namespace, key}) + end end end diff --git a/lib/polyjuice/client/storage/ets.ex b/lib/polyjuice/client/storage/ets.ex index 0fae13b..06f7835 100644 --- a/lib/polyjuice/client/storage/ets.ex +++ b/lib/polyjuice/client/storage/ets.ex @@ -62,15 +62,19 @@ defmodule Polyjuice.Client.Storage.Ets do end end - def kv_put(%{table: table}, key, value) when is_binary(key) do - :ets.insert(table, {"kv_" <> key, value}) + def kv_put(%{table: table}, namespace, key, value) when is_binary(namespace) and is_binary(key) do + :ets.insert(table, {{"kv", namespace, key}, value}) end - def kv_get(%{table: table}, key, default \\ nil) when is_binary(key) do - case :ets.lookup(table, "kv_" <> key) do + def kv_get(%{table: table}, namespace, key, default \\ nil) when is_binary(namespace) and is_binary(key) do + case :ets.lookup(table, {"kv", namespace, key}) do [{_, value}] -> value _ -> default end end + + def kv_del(%{table: table}, namespace, key) when is_binary(namespace) and is_binary(key) do + :ets.delete(table, {"kv", namespace, key}) + end end end |