Skip to content

Commit

Permalink
Rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
cabol committed Jul 10, 2022
1 parent 0e03ac9 commit e424107
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 76 deletions.
79 changes: 38 additions & 41 deletions lib/nebulex/adapters/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,7 @@ defmodule Nebulex.Adapters.Local do
backend = Keyword.fetch!(opts, :backend)

# Internal option for max nested match specs based on number of keys
purge_batch_size =
get_option(
opts,
:purge_batch_size,
"an integer > 0",
&(is_integer(&1) and &1 > 0),
100
)
purge_chunk_size = Keyword.fetch!(opts, :purge_chunk_size)

# Build adapter metadata
adapter_meta = %{
Expand All @@ -413,7 +406,7 @@ defmodule Nebulex.Adapters.Local do
meta_tab: meta_tab,
stats_counter: stats_counter,
backend: backend,
purge_batch_size: purge_batch_size,
purge_chunk_size: purge_chunk_size,
started_at: DateTime.utc_now()
}

Expand Down Expand Up @@ -489,18 +482,18 @@ defmodule Nebulex.Adapters.Local do
on_write,
adapter_meta.meta_tab,
adapter_meta.backend,
adapter_meta.purge_batch_size,
adapter_meta.purge_chunk_size,
entries
)
|> wrap_ok()
end

defp do_put_all(:put, meta_tab, backend, batch_size, entries) do
put_entries(meta_tab, backend, entries, batch_size)
defp do_put_all(:put, meta_tab, backend, chunk_size, entries) do
put_entries(meta_tab, backend, entries, chunk_size)
end

defp do_put_all(:put_new, meta_tab, backend, batch_size, entries) do
put_new_entries(meta_tab, backend, entries, batch_size)
defp do_put_all(:put_new, meta_tab, backend, chunk_size, entries) do
put_new_entries(meta_tab, backend, entries, chunk_size)
end

@impl true
Expand Down Expand Up @@ -532,7 +525,7 @@ defmodule Nebulex.Adapters.Local do
_ =
meta_tab
|> list_gen()
|> do_get(key, backend)
|> do_fetch(key, adapter_meta)

# Run the counter operation
meta_tab
Expand Down Expand Up @@ -614,7 +607,7 @@ defmodule Nebulex.Adapters.Local do
meta_tab
|> list_gen()
|> Enum.reduce(0, fn gen, acc ->
do_delete_all(adapter_meta.backend, gen, keys, adapter_meta.purge_batch_size) + acc
do_delete_all(adapter_meta.backend, gen, keys, adapter_meta.purge_chunk_size) + acc
end)
end

Expand Down Expand Up @@ -730,6 +723,7 @@ defmodule Nebulex.Adapters.Local do

entries when is_list(entries) ->
now = Time.now()

{:ok, for(entry(touched: touched, ttl: ttl) = e <- entries, now - touched < ttl, do: e)}
end
end
Expand All @@ -753,23 +747,23 @@ defmodule Nebulex.Adapters.Local do
backend_call(adapter_meta, :take, tab, key)
end

defp put_entries(meta_tab, backend, entry_or_entries) do
meta_tab
|> newer_gen()
|> backend.insert(entry_or_entries)
end
# defp put_entries(meta_tab, backend, entry_or_entries) do
# meta_tab
# |> newer_gen()
# |> backend.insert(entry_or_entries)
# end

defp put_entries(meta_tab, backend, entries, batch_size \\ 0)
defp put_entries(meta_tab, backend, entries, chunk_size \\ 0)

defp put_entries(meta_tab, backend, entries, batch_size) when is_list(entries) do
defp put_entries(meta_tab, backend, entries, chunk_size) when is_list(entries) do
do_put_entries(meta_tab, backend, entries, fn older_gen ->
keys = Enum.map(entries, fn entry(key: key) -> key end)

do_delete_all(backend, older_gen, keys, batch_size)
do_delete_all(backend, older_gen, keys, chunk_size)
end)
end

defp put_entries(meta_tab, backend, entry(key: key) = entry, _batch_size) do
defp put_entries(meta_tab, backend, entry(key: key) = entry, _chunk_size) do
do_put_entries(meta_tab, backend, entry, fn older_gen ->
true = backend.delete(older_gen, key)
end)
Expand All @@ -787,21 +781,21 @@ defmodule Nebulex.Adapters.Local do
end
end

defp put_new_entries(meta_tab, backend, entries, batch_size \\ 0)
defp put_new_entries(meta_tab, backend, entries, chunk_size \\ 0)

defp put_new_entries(meta_tab, backend, entries, batch_size) when is_list(entries) do
defp put_new_entries(meta_tab, backend, entries, chunk_size) when is_list(entries) do
do_put_new_entries(meta_tab, backend, entries, fn newer_gen, older_gen ->
with true <- backend.insert_new(older_gen, entries) do
keys = Enum.map(entries, fn entry(key: key) -> key end)

_ = do_delete_all(backend, older_gen, keys, batch_size)
_ = do_delete_all(backend, older_gen, keys, chunk_size)

backend.insert_new(newer_gen, entries)
end
end)
end

defp put_new_entries(meta_tab, backend, entry(key: key) = entry, _batch_size) do
defp put_new_entries(meta_tab, backend, entry(key: key) = entry, _chunk_size) do
do_put_new_entries(meta_tab, backend, entry, fn newer_gen, older_gen ->
with true <- backend.insert_new(older_gen, entry) do
true = backend.delete(older_gen, key)
Expand All @@ -828,7 +822,7 @@ defmodule Nebulex.Adapters.Local do

[newer_gen, older_gen] ->
with false <- backend.update_element(newer_gen, key, updates),
entry() = entry <- pop_entry(older_gen, key, false, backend) do
[entry() = entry] <- backend.take(older_gen, key) do
entry =
Enum.reduce(updates, entry, fn
{3, value}, acc -> entry(acc, value: value)
Expand All @@ -837,54 +831,57 @@ defmodule Nebulex.Adapters.Local do
end)

backend.insert(newer_gen, entry)
else
[] -> false
other -> other
end
end
end

defp do_delete_all(backend, tab, keys, batch_size) do
do_delete_all(backend, tab, keys, batch_size, 0)
defp do_delete_all(backend, tab, keys, chunk_size) do
do_delete_all(backend, tab, keys, chunk_size, 0)
end

defp do_delete_all(backend, tab, [key], _batch_size, deleted) do
defp do_delete_all(backend, tab, [key], _chunk_size, deleted) do
true = backend.delete(tab, key)

deleted + 1
end

defp do_delete_all(backend, tab, [k1, k2 | keys], batch_size, deleted) do
defp do_delete_all(backend, tab, [k1, k2 | keys], chunk_size, deleted) do
k1 = if is_tuple(k1), do: {k1}, else: k1
k2 = if is_tuple(k2), do: {k2}, else: k2

do_delete_all(
backend,
tab,
keys,
batch_size,
chunk_size,
deleted,
2,
{:orelse, {:==, :"$1", k1}, {:==, :"$1", k2}}
)
end

defp do_delete_all(backend, tab, [], _batch_size, deleted, _count, acc) do
defp do_delete_all(backend, tab, [], _chunk_size, deleted, _count, acc) do
backend.select_delete(tab, delete_all_match_spec(acc)) + deleted
end

defp do_delete_all(backend, tab, keys, batch_size, deleted, count, acc)
when count >= batch_size do
defp do_delete_all(backend, tab, keys, chunk_size, deleted, count, acc)
when count >= chunk_size do
deleted = backend.select_delete(tab, delete_all_match_spec(acc)) + deleted

do_delete_all(backend, tab, keys, batch_size, deleted)
do_delete_all(backend, tab, keys, chunk_size, deleted)
end

defp do_delete_all(backend, tab, [k | keys], batch_size, deleted, count, acc) do
defp do_delete_all(backend, tab, [k | keys], chunk_size, deleted, count, acc) do
k = if is_tuple(k), do: {k}, else: k

do_delete_all(
backend,
tab,
keys,
batch_size,
chunk_size,
deleted,
count + 1,
{:orelse, acc, {:==, :"$1", k}}
Expand Down
9 changes: 9 additions & 0 deletions lib/nebulex/adapters/local/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ defmodule Nebulex.Adapters.Local.Options do
This option is built internally for creating the ETS tables
used by the local adapter underneath.
"""
],
purge_chunk_size: [
required: false,
type: :pos_integer,
default: 100,
doc: """
This option is for limiting the max nested match specs based on number
of keys when purging the older cache generation.
"""
]
] ++ Options.base_definition()

Expand Down
2 changes: 1 addition & 1 deletion test/nebulex/adapters/local_ets_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Nebulex.Adapters.LocalEtsTest do
alias Nebulex.Adapter
alias Nebulex.TestCache.Cache

setup_with_dynamic_cache(Cache, :local_with_ets, purge_batch_size: 10)
setup_with_dynamic_cache(Cache, :local_with_ets, purge_chunk_size: 10)

describe "ets" do
test "backend", %{name: name} do
Expand Down
16 changes: 8 additions & 8 deletions test/nebulex/caching_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -471,45 +471,45 @@ defmodule Nebulex.CachingTest do

describe "option :cache with MFA" do
test "cacheable annotation" do
refute Cache.get("foo")
refute Cache.get!("foo")
assert get_mfa_cache_without_extra_args("foo") == "foo"
assert Cache.get("foo") == "foo"
assert Cache.get!("foo") == "foo"
end

test "cache_put annotation" do
:ok = Cache.put("foo", "bar")

assert update_mfa_cache_without_extra_args("bar bar") == "bar bar"
assert Cache.get("foo") == "bar bar"
assert Cache.get!("foo") == "bar bar"
end

test "cache_evict annotation" do
:ok = Cache.put("foo", "bar")

assert delete_mfa_cache_without_extra_args("bar bar") == "bar bar"
refute Cache.get("foo")
refute Cache.get!("foo")
end
end

describe "option :cache with MFA and extra args" do
test "cacheable annotation" do
refute Cache.get("foo")
refute Cache.get!("foo")
assert get_mfa_cache_with_extra_args("foo") == "foo"
assert Cache.get("foo") == "foo"
assert Cache.get!("foo") == "foo"
end

test "cache_put annotation" do
:ok = Cache.put("foo", "bar")

assert update_mfa_cache_with_extra_args("bar bar") == "bar bar"
assert Cache.get("foo") == "bar bar"
assert Cache.get!("foo") == "bar bar"
end

test "cache_evict annotation" do
:ok = Cache.put("foo", "bar")

assert delete_mfa_cache_with_extra_args("bar bar") == "bar bar"
refute Cache.get("foo")
refute Cache.get!("foo")
end
end

Expand Down
Loading

0 comments on commit e424107

Please sign in to comment.