From a1279d682f56928c0834dc2987c998a06fcd9fe4 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Wed, 26 Aug 2026 11:17:00 +0200 Subject: [PATCH] feat(command): add hyperloglog command handlers --- lib/excache/command/hyper_log_log.ex | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lib/excache/command/hyper_log_log.ex diff --git a/lib/excache/command/hyper_log_log.ex b/lib/excache/command/hyper_log_log.ex new file mode 100644 index 0000000..9dfaace --- /dev/null +++ b/lib/excache/command/hyper_log_log.ex @@ -0,0 +1,37 @@ +defmodule Excache.Command.HyperLogLog do + @moduledoc false + + alias Excache.Store + alias Excache.Command.Reply + + def dispatch("PFADD", [key | elements], state) do + case Store.pfadd(key, elements) do + {:ok, changed} -> reply({:integer, changed}, state) + {:error, reason} -> reply(hllError(reason), state) + end + end + + def dispatch("PFCOUNT", [_ | _] = keys, state) do + case Store.pfcount(keys) do + {:ok, count} -> reply({:integer, count}, state) + {:error, reason} -> reply(hllError(reason), state) + end + end + + def dispatch("PFMERGE", [dest | sources], state) do + case Store.pfmerge(dest, sources) do + {:ok, "OK"} -> reply({:simple, "OK"}, state) + {:error, reason} -> reply(hllError(reason), state) + end + end + + def dispatch(_name, _args, _state), do: :unhandled + + defp hllError(:not_hll), + do: {:error, "WRONGTYPE Key is not a valid HyperLogLog string value."} + + defp hllError(:wrongtype), do: Reply.wrongtype() + defp hllError(other), do: {:error, "ERR #{other}"} + + defp reply(term, state), do: {[term], state} +end