From c2df59aa3a065aa6c8bea70be3d5b88ee953eeba Mon Sep 17 00:00:00 2001 From: Christopher Date: Sat, 7 Mar 2020 22:19:36 -0600 Subject: [PATCH] when no value exists check if it's list, and if it is, don't wrap it in onew --- lib/cluster_kv.ex | 2 +- lib/cluster_kv/db.ex | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/cluster_kv.ex b/lib/cluster_kv.ex index 71689a6..12aa55b 100644 --- a/lib/cluster_kv.ex +++ b/lib/cluster_kv.ex @@ -35,7 +35,7 @@ defmodule ClusterKV do {Ring, [{:local, ring}, ring_data, []]} ] - Supervisor.init(children, strategy: :one_for_one, max_restarts: 5, max_seconds: 10) + Supervisor.init(children, strategy: :rest_for_one, max_restarts: 5, max_seconds: 10) end def db_name(name), do: Module.concat([name, DB]) diff --git a/lib/cluster_kv/db.ex b/lib/cluster_kv/db.ex index f334c47..4b96300 100644 --- a/lib/cluster_kv/db.ex +++ b/lib/cluster_kv/db.ex @@ -31,7 +31,7 @@ defmodule ClusterKV.DB do GenServer.cast(name, {:upsert, key, value, fun}) end - def batch(name, batch, chunk \\ 100, fun \\ &{elem(&1, 0), Enum.uniq([&2 | elem(&1, 1)])}) do + def batch(name, batch, chunk \\ 100, fun \\ &{elem(&1, 0), &2}) do GenServer.cast(name, {:batch, batch, chunk, fun}) end @@ -100,7 +100,13 @@ defmodule ClusterKV.DB do defp do_upsert(db, key, value, fun) do case :ets.lookup(db, key) do [] -> - :ets.insert(db, {key, [value]}) + v = + case value do + val when is_list(val) -> val + val -> [val] + end + + :ets.insert(db, {key, v}) [{_, val} = old] when is_list(val) -> new = fun.(old, value) -- 2.45.3