DEV Community

Cover image for #TIR: Build a Simple Persistent Key-Value Store in Elixir, using Logs - Part 1
chenge
chenge

Posted on

#TIR: Build a Simple Persistent Key-Value Store in Elixir, using Logs - Part 1

link-->

Today I read it from Awesome Elixir Newsletter. Nice for learning Elixir.

def lookup(key) do
  GenServer.call(__MODULE__, {:lookup, key})
end

def handle_call({:lookup, key}, _from, index_map) do
  {:reply, get_key_offset_size(key, index_map), index_map}
end

defp get_key_offset_size(key, index_map) do
  case Map.get(index_map, key) do
    {_offset, _size} = offset_size -> {:ok, offset_size}
    nil -> {:error, :not_found}
  end
end

Top comments (0)