Packages
cubdb
0.13.0
2.0.2
2.0.1
2.0.0
2.0.0-rc.1
1.1.0
1.0.0
1.0.0-rc.10
1.0.0-rc.9
1.0.0-rc.8
1.0.0-rc.7
1.0.0-rc.6
1.0.0-rc.5
1.0.0-rc.4
1.0.0-rc.3
1.0.0-rc.2
1.0.0-rc.1
0.17.0
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.0
0.14.0
0.13.1
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
A pure-Elixir embedded key-value database
Current section
Files
Jump to
Current section
Files
lib/cubdb/store/test_store.ex
defmodule CubDB.Store.TestStore do
@moduledoc false
# `CubDB.Store.TestStore` is an implementation of the `Store` protocol
# intended for test purposes only. It is backed by a map, but supports all the
# operations of a `CubDB.Store`. It allows some tests to be simpler and faster
# by avoid using the file system.
defstruct agent: nil
alias CubDB.Store.TestStore
def new do
with {:ok, pid} <- Agent.start_link(fn -> {%{}, nil} end) do
%TestStore{agent: pid}
end
end
end
defimpl CubDB.Store, for: CubDB.Store.TestStore do
alias CubDB.Store.TestStore
def put_node(%TestStore{agent: agent}, node) do
Agent.get_and_update(agent, fn {map, latest_header_loc} ->
loc = Enum.count(map)
{loc, {Map.put(map, loc, node), latest_header_loc}}
end)
end
def put_header(%TestStore{agent: agent}, header) do
Agent.get_and_update(agent, fn {map, _} ->
loc = Enum.count(map)
{loc, {Map.put(map, loc, header), loc}}
end)
end
def sync(%TestStore{}), do: :ok
def get_node(%TestStore{agent: agent}, location) do
case Agent.get(agent, fn {map, _} ->
Map.fetch(map, location)
end) do
{:ok, value} -> value
:error -> raise(ArgumentError, message: "End of file")
end
end
def get_latest_header(%TestStore{agent: agent}) do
Agent.get(agent, fn
{_, nil} -> nil
{map, header_loc} -> {header_loc, Map.get(map, header_loc)}
end)
end
def close(%TestStore{agent: agent}) do
Agent.stop(agent)
end
def blank?(%TestStore{agent: agent}) do
Agent.get(agent, fn
{_, nil} -> true
_ -> false
end)
end
end