From 150a32e9521b4ee06cecdabe184cc7e3cdab0408 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Tue, 8 Sep 2026 20:28:41 +0200 Subject: [PATCH] refactor(ops): rework binary and extra --- lib/excache/store/ops/binary.ex | 5 +++-- lib/excache/store/ops/extra.ex | 19 ++++++------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/excache/store/ops/binary.ex b/lib/excache/store/ops/binary.ex index 8efabbc..6d2ffda 100644 --- a/lib/excache/store/ops/binary.ex +++ b/lib/excache/store/ops/binary.ex @@ -55,6 +55,9 @@ defmodule Excache.Store.Ops.Binary do :wrongtype -> {:error, :wrongtype} + {:ok, current, _ea, _exists} when value == "" -> + {:ok, byte_size(current)} + {:ok, current, ea, _exists} -> updated = overwrite(current, offset, value) store(table, key, updated, ea) @@ -149,8 +152,6 @@ defmodule Excache.Store.Ops.Binary do end end - defp overwrite(current, offset, value) when value == "", do: current - defp overwrite(current, offset, value) do size = byte_size(current) padded = if size < offset, do: current <> :binary.copy(<<0>>, offset - size), else: current diff --git a/lib/excache/store/ops/extra.ex b/lib/excache/store/ops/extra.ex index bd0a48e..91f9960 100644 --- a/lib/excache/store/ops/extra.ex +++ b/lib/excache/store/ops/extra.ex @@ -313,22 +313,15 @@ defmodule Excache.Store.Ops.Extra do scanned = if rank < 0, do: Enum.reverse(indexed), else: indexed - skip = abs(rank) - 1 - limit = if count == 0, do: :all, else: count + matches = collectMatches(scanned, element, abs(rank) - 1, matchLimit(count), maxlen, 0, []) - collectMatches(scanned, element, skip, limit, maxlen, 0, []) - |> then(fn matches -> - if count == nil do - case matches do - [] -> nil - [first | _] -> first - end - else - matches - end - end) + if count == nil, do: List.first(matches), else: matches end + defp matchLimit(nil), do: 1 + defp matchLimit(0), do: :all + defp matchLimit(count), do: count + defp collectMatches([], _element, _skip, _limit, _maxlen, _compared, acc), do: Enum.reverse(acc)