Browse Source

Use routing facility

master
Piotr Orzechowski 7 months ago
parent
commit
1cc265f1e3

+ 3
- 3
apps/kv_server/lib/kv_server/command.ex View File

@@ -48,14 +48,14 @@ defmodule KVServer.Command do
def run(command, registry \\ KV.Registry)

def run({:create, bucket}, registry) do
case KV.Registry.create(registry, bucket) do
case KV.Router.route(bucket, KV.Registry, :create, [registry, bucket]) do
{:ok, _} -> {:ok, "OK\n"}
:error -> {:error, :already_exists}
end
end

def run({:delete, bucket}, registry) do
case KV.Registry.delete(registry, bucket) do
case KV.Router.route(bucket, KV.Registry, :delete, [registry, bucket]) do
:ok -> {:ok, "OK\n"}
:error -> {:error, :not_found}
end
@@ -83,7 +83,7 @@ defmodule KVServer.Command do
end

defp lookup(bucket, registry, callback) do
case KV.Registry.lookup(registry, bucket) do
case KV.Router.route(bucket, KV.Registry, :lookup, [registry, bucket]) do
{:ok, pid} -> callback.(pid)
:error -> {:error, :not_found}
end

+ 4
- 0
apps/kv_server/test/kv_server/command_test.exs View File

@@ -2,6 +2,10 @@ defmodule KVServer.CommandTest do
use ExUnit.Case, async: true
doctest KVServer.Command

setup do
:ok = Application.put_env(:kv, :routing_table, [{?a..?z, node()}])
end

setup context do
_ = start_supervised!({KV.Registry, name: context.test})
%{registry: context.test}

Loading…
Cancel
Save