]> Entropealabs - cluster_kv.git/commitdiff
more logging in DB, don't overwrite current batch with new
authorChristopher <chris@entropealabs.com>
Sun, 8 Mar 2020 21:28:06 +0000 (16:28 -0500)
committerChristopher <chris@entropealabs.com>
Sun, 8 Mar 2020 21:28:06 +0000 (16:28 -0500)
lib/cluster_kv/db.ex

index c9a966b75385686e5941df682e9cc114b4cf108c..e0d096be1f3904033c570b9a991a84d122a52005 100644 (file)
@@ -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), &2}) do
+  def batch(name, batch, chunk \\ 10, fun \\ &{elem(&1, 0), &2}) do
     GenServer.cast(name, {:batch, batch, chunk, fun})
   end
 
@@ -54,22 +54,27 @@ defmodule ClusterKV.DB do
   end
 
   def handle_call({:get, key}, _from, %DB{db: db} = state) do
+    Logger.debug("DB handling GET #{key}")
     {:reply, do_get(db, key), state}
   end
 
   def handle_cast({:put, key, value}, %DB{db: db} = state) do
+    Logger.debug("DB handling PUT #{key}")
     :ets.insert(db, {key, [value]})
     {:noreply, state}
   end
 
   def handle_cast({:upsert, key, value, fun}, %DB{db: db} = state) do
+    Logger.debug("DB handling UPSERT #{key}")
     do_upsert(db, key, value, fun)
     {:noreply, state}
   end
 
-  def handle_cast({:batch, batch, chunk, fun}, %DB{db: db, last_batch: lb} = state) do
-    {batch, lb} = handle_next_batch_chunk(db, batch, chunk, lb, fun)
-    {:noreply, %DB{state | batch: batch, batch_chunk: chunk, batch_fun: fun, last_batch: lb}}
+  def handle_cast({:batch, batch, chunk, fun}, %DB{batch: b} = state) do
+    Logger.debug("DB handling BATCH")
+    batch = b ++ batch
+    Process.send_after(self(), :process_batch, 0)
+    {:noreply, %DB{state | batch: batch, batch_chunk: chunk, batch_fun: fun}}
   end
 
   def handle_info(
@@ -81,6 +86,8 @@ defmodule ClusterKV.DB do
   end
 
   defp handle_next_batch_chunk(db, batch, chunk, last_batch, fun) do
+    Logger.debug("processing batch of #{length(batch)} from #{last_batch} with chunk of #{chunk}")
+
     batch
     |> Enum.slice(last_batch, chunk)
     |> Enum.each(fn {k, v} ->
@@ -93,6 +100,7 @@ defmodule ClusterKV.DB do
 
       lb ->
         Process.send_after(self(), :process_batch, 0)
+        Logger.debug("Batch Processed")
         {batch, lb}
     end
   end