diff --git a/lib/august_db/commitlog.ex b/lib/august_db/commitlog.ex index 3c30890..b5e8416 100644 --- a/lib/august_db/commitlog.ex +++ b/lib/august_db/commitlog.ex @@ -16,14 +16,14 @@ defmodule CommitLog do {:ok, device_out} end - def handle_cast(:new, _) do + def handle_cast(:new, old_device_out) do + :ok = :file.close(old_device_out) :ok = :file.delete(@log_file) - {:ok, device_out} = :file.open(@log_file, [:append]) - {:noreply, device_out} + {:ok, new_device_out} = :file.open(@log_file, [:append, :raw]) + {:noreply, new_device_out} end def handle_cast(:close, device_out) do - :ok = :file.close(device_out) {:noreply, device_out} end @@ -96,8 +96,11 @@ defmodule CommitLog do File.touch!(@log_file) end + @doc """ + Delete the old commit log and open a device to a new one + in raw,append mode. + """ def new() do - GenServer.cast(CommitLogDevice, :close) GenServer.cast(CommitLogDevice, :new) end end diff --git a/lib/august_db/memtable_sizer.ex b/lib/august_db/memtable_sizer.ex index cc5d544..ee11922 100644 --- a/lib/august_db/memtable_sizer.ex +++ b/lib/august_db/memtable_sizer.ex @@ -1,7 +1,7 @@ defmodule Memtable.Sizer do use GenServer - @max_size_bytes 64 * 1024 * 1024 + @max_size_bytes 1024 * 1024 defmodule State do defstruct total_size: 0, sizes: %{} @@ -15,7 +15,7 @@ defmodule Memtable.Sizer do {:ok, state} end - def handle_call({:resize, key, kv_size}, _from, state) do + def handle_cast({:resize, key, kv_size}, state) do old_kv_size = case Map.get(state.sizes, key) do nil -> 0 @@ -26,8 +26,14 @@ defmodule Memtable.Sizer do new_total_size = state.total_size + kv_size_diff - {:reply, new_total_size, - %State{total_size: new_total_size, sizes: Map.put(state.sizes, key, kv_size)}} + if new_total_size > @max_size_bytes do + IO.puts("Clearing Memtable with size #{new_total_size}B, max is #{@max_size_bytes}B") + + Memtable.flush() + {:noreply, %State{total_size: 9, sizes: %{}}} + else + {:noreply, %State{total_size: new_total_size, sizes: Map.put(state.sizes, key, kv_size)}} + end end def handle_cast(:clear, _state) do @@ -37,25 +43,10 @@ defmodule Memtable.Sizer do def resize(key, value) when is_binary(key) and is_binary(value) do kv_size = byte_size(key) + byte_size(value) - update(key, kv_size) + GenServer.cast(MemtableSizer, {:resize, key, kv_size}) end def remove(key) when is_binary(key) do - update(key, byte_size(key)) - end - - defp update(key, kv_size) do - new_total_size = GenServer.call(MemtableSizer, {:resize, key, kv_size}) - - if new_total_size > @max_size_bytes do - IO.puts("Clearing Memtable with size #{new_total_size}B, max is #{@max_size_bytes}B") - - Memtable.flush() - __MODULE__.clear() - end - end - - def clear() do - GenServer.cast(MemtableSizer, :clear) + GenServer.cast(MemtableSizer, {:resize, key, byte_size(key)}) end end diff --git a/test/AugustDB.jmx b/test/AugustDB.jmx index 342c700..2e1be71 100644 --- a/test/AugustDB.jmx +++ b/test/AugustDB.jmx @@ -20,7 +20,7 @@ 200 500 - 1 + 5 false @@ -42,7 +42,7 @@ - 4294967295 + 2147483647 1 true