From: Christopher Date: Wed, 18 Mar 2020 19:44:05 +0000 (-0500) Subject: add get_wildcard_key function X-Git-Url: http://git.entropealabs.com/?a=commitdiff_plain;h=4c36b8d68f40711cd167edb9917d32a992f49561;p=cluster_kv.git add get_wildcard_key function --- diff --git a/lib/cluster_kv.ex b/lib/cluster_kv.ex index dd78437..623dad7 100644 --- a/lib/cluster_kv.ex +++ b/lib/cluster_kv.ex @@ -54,6 +54,7 @@ defmodule ClusterKV do defdelegate put(name, keyspace, key, value), to: Ring defdelegate update(name, keyspace, key, value, fun), to: Ring defdelegate batch(name, keyspace, batch), to: Ring + defdelegate get_wildcard_key(key, split_on, join, wildcard), to: Ring defdelegate put_wildcard(name, keyspace, key, value, split_on, join, wildcard), to: Ring defdelegate prefix(name, keyspace, key, split_on, min, timeout \\ :infinity), to: Ring defdelegate stream(name, timeout \\ :infinity), to: Ring diff --git a/lib/cluster_kv/ring.ex b/lib/cluster_kv/ring.ex index 7733775..27ef1ef 100644 --- a/lib/cluster_kv/ring.ex +++ b/lib/cluster_kv/ring.ex @@ -55,6 +55,16 @@ defmodule ClusterKV.Ring do :gen_statem.cast(ClusterKV.ring_name(name), {:update, keyspace, key, value, fun}) end + @spec get_wildcard_key( + key :: String.t(), + split_on :: String.t(), + join :: String.t(), + wildcard :: String.t() + ) :: :ok + def get_wildcard_key(key, split_on, join, wildcard) do + do_get_wildcard_key(key, split_on, wildcard, join) + end + @spec put_wildcard( name :: module(), keyspace :: String.t(), @@ -463,6 +473,19 @@ defmodule ClusterKV.Ring do end) end + defp do_get_wildcard_key(topic, split_on, wildcard, join) do + topic + |> String.split(split_on) + |> Enum.with_index() + |> Enum.reduce_while(nil, fn + {^wildcard, _}, acc -> + {:cont, acc} + + {k, i}, _ -> + {:halt, Enum.join([k, i], join)} + end) + end + @spec redistribute_data( name :: module(), me :: node(), diff --git a/test/cluster_kv_test.exs b/test/cluster_kv_test.exs index 8661262..2537a53 100644 --- a/test/cluster_kv_test.exs +++ b/test/cluster_kv_test.exs @@ -22,6 +22,8 @@ defmodule ClusterKVTest do ClusterKV.put(db, @keyspace, "test1:test", :cruel) ClusterKV.put(db, @keyspace, "test1:test", :world) + assert "test:1" = ClusterKV.get_wildcard_key(".test.light..status", ".", ":", "") + ClusterKV.put_wildcard( db, @keyspace,