]> Entropealabs - cluster_kv.git/commitdiff
add get_wildcard_key function
authorChristopher <chris@entropealabs.com>
Wed, 18 Mar 2020 19:44:05 +0000 (14:44 -0500)
committerChristopher <chris@entropealabs.com>
Wed, 18 Mar 2020 19:44:05 +0000 (14:44 -0500)
lib/cluster_kv.ex
lib/cluster_kv/ring.ex
test/cluster_kv_test.exs

index dd784374a41826de065e829d9ff5ac7cd969211a..623dad75bf1029dfdafa92e952d464d875952bc7 100644 (file)
@@ -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
index 7733775ea22b40389525e6b6fb89280e8c10bfeb..27ef1ef8fb81d482108253edcdf28d81300cb2f1 100644 (file)
@@ -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(),
index 86612622d92b20be3321fdbf9a64051983528797..2537a534beca910ed548143d4839b6a68d660a28 100644 (file)
@@ -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,