Skip to content

Commit

Permalink
[#189] Overall cleanup to move adapters to separate repos
Browse files Browse the repository at this point in the history
  • Loading branch information
cabol committed Mar 26, 2023
1 parent 15808aa commit 9bf8c9e
Show file tree
Hide file tree
Showing 67 changed files with 1,319 additions and 8,250 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,11 @@ jobs:
if: ${{ matrix.style }}

- name: Run tests
run: |
epmd -daemon
mix test --trace
run: mix test
if: ${{ !matrix.coverage }}

- name: Run tests with coverage
run: |
epmd -daemon
mix coveralls.github
run: mix coveralls.github
if: ${{ matrix.coverage }}

- name: Run sobelow
Expand Down
30 changes: 15 additions & 15 deletions guides/creating-new-adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ end
We won't be writing tests ourselves. Instead, we will use shared tests from the
Nebulex parent repo. To do so, we will create a helper module in
`test/shared/cache_test.exs` that will `use` test suites for behaviour we are
going to implement. The minimal set of behaviours is `Entry` and `Queryable` so
going to implement. The minimal set of behaviours is `KV` and `Queryable` so
we'll go with them.

```elixir
Expand All @@ -119,7 +119,7 @@ defmodule NebulexMemoryAdapter.CacheTest do

defmacro __using__(_opts) do
quote do
use Nebulex.Cache.EntryTest
use Nebulex.Cache.KVTest
use Nebulex.Cache.QueryableTest
end
end
Expand Down Expand Up @@ -187,7 +187,7 @@ Another try
```console
mix test
== Compilation error in file test/nebulex_memory_adapter_test.exs ==
** (CompileError) test/nebulex_memory_adapter_test.exs:3: module Nebulex.Cache.EntryTest is not loaded and could not be found
** (CompileError) test/nebulex_memory_adapter_test.exs:3: module Nebulex.Cache.KVTest is not loaded and could not be found
(elixir 1.13.2) expanding macro: Kernel.use/1
test/nebulex_memory_adapter_test.exs:3: NebulexMemoryAdapterTest (module)
expanding macro: NebulexMemoryAdapter.CacheTest.__using__/1
Expand Down Expand Up @@ -305,7 +305,7 @@ one-by-one or define them all in bulk. For posterity, we put a complete
```elixir
defmodule NebulexMemoryAdapter do
@behaviour Nebulex.Adapter
@behaviour Nebulex.Adapter.Entry
@behaviour Nebulex.Adapter.KV
@behaviour Nebulex.Adapter.Queryable

import Nebulex.Helpers
Expand All @@ -320,17 +320,17 @@ defmodule NebulexMemoryAdapter do
{:ok, child_spec, %{}}
end

@impl Nebulex.Adapter.Entry
@impl Nebulex.Adapter.KV
def fetch(adapter_meta, key, _opts) do
wrap_ok Agent.get(adapter_meta.pid, &Map.get(&1, key))
end

@impl Nebulex.Adapter.Entry
@impl Nebulex.Adapter.KV
def get_all(adapter_meta, keys, _opts) do
wrap_ok Agent.get(adapter_meta.pid, &Map.take(&1, keys))
end

@impl Nebulex.Adapter.Entry
@impl Nebulex.Adapter.KV
def put(adapter_meta, key, value, ttl, :put_new, opts) do
if get(adapter_meta, key, []) do
false
Expand Down Expand Up @@ -358,7 +358,7 @@ defmodule NebulexMemoryAdapter do
wrap_ok true
end

@impl Nebulex.Adapter.Entry
@impl Nebulex.Adapter.KV
def put_all(adapter_meta, entries, ttl, :put_new, opts) do
if get_all(adapter_meta, Map.keys(entries), []) == %{} do
put_all(adapter_meta, entries, ttl, :put, opts)
Expand All @@ -378,12 +378,12 @@ defmodule NebulexMemoryAdapter do
wrap_ok true
end

@impl Nebulex.Adapter.Entry
@impl Nebulex.Adapter.KV
def delete(adapter_meta, key, _opts) do
wrap_ok Agent.update(adapter_meta.pid, &Map.delete(&1, key))
end

@impl Nebulex.Adapter.Entry
@impl Nebulex.Adapter.KV
def take(adapter_meta, key, _opts) do
value = get(adapter_meta, key, [])

Expand All @@ -392,7 +392,7 @@ defmodule NebulexMemoryAdapter do
wrap_ok value
end

@impl Nebulex.Adapter.Entry
@impl Nebulex.Adapter.KV
def update_counter(adapter_meta, key, amount, _ttl, default, _opts) do
Agent.update(adapter_meta.pid, fn state ->
Map.update(state, key, default + amount, fn v -> v + amount end)
Expand All @@ -401,22 +401,22 @@ defmodule NebulexMemoryAdapter do
wrap_ok get(adapter_meta, key, [])
end

@impl Nebulex.Adapter.Entry
@impl Nebulex.Adapter.KV
def has_key?(adapter_meta, key, _opts) do
wrap_ok Agent.get(adapter_meta.pid, &Map.has_key?(&1, key))
end

@impl Nebulex.Adapter.Entry
@impl Nebulex.Adapter.KV
def ttl(_adapter_meta, _key, _opts) do
wrap_ok nil
end

@impl Nebulex.Adapter.Entry
@impl Nebulex.Adapter.KV
def expire(_adapter_meta, _key, _ttl, _opts) do
wrap_ok true
end

@impl Nebulex.Adapter.Entry
@impl Nebulex.Adapter.KV
def touch(_adapter_meta, _key, _opts) do
wrap_ok true
end
Expand Down
1 change: 1 addition & 0 deletions lib/mix/tasks/nbx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Mix.Tasks.Nbx do

defp general do
_ = Application.ensure_all_started(:nebulex)

Mix.shell().info("Nebulex v#{Application.spec(:nebulex, :vsn)}")
Mix.shell().info("In-Process and Distributed Cache Toolkit for Elixir.")

Expand Down
17 changes: 8 additions & 9 deletions lib/nebulex/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ defmodule Nebulex.Adapter do
@typedoc "Adapter"
@type t :: module

@typedoc "Metadata type"
@type metadata :: %{optional(atom) => term}

@typedoc """
The metadata returned by the adapter `c:init/1`.
It must be a map and Nebulex itself will always inject
the following keys into the meta:
* `:cache` - The cache module.
* `:name` - The name of the cache supervisor process.
* `:pid` - The PID returned by the child spec returned in `c:init/1`.
* `:adapter` - The defined cache adapter.
"""
@type adapter_meta :: metadata
@type adapter_meta :: %{optional(term) => term}

## Callbacks

Expand All @@ -32,7 +30,8 @@ defmodule Nebulex.Adapter do
@macrocallback __before_compile__(env :: Macro.Env.t()) :: Macro.t()

@doc """
Initializes the adapter supervision tree by returning the children.
Initializes the adapter supervision tree by returning the children
and adapter metadata.
"""
@callback init(config :: keyword) :: {:ok, :supervisor.child_spec(), adapter_meta}

Expand Down Expand Up @@ -115,11 +114,11 @@ defmodule Nebulex.Adapter do

## Private Functions

defp build_defspan(fun, opts) when is_list(opts) do
defp build_defspan(ast, opts) when is_list(opts) do
{name, args} =
case Macro.decompose_call(fun) do
{_, _} = pair -> pair
_ -> raise ArgumentError, "invalid syntax in defspan #{Macro.to_string(fun)}"
case Macro.decompose_call(ast) do
{_, _} = parts -> parts
_ -> raise ArgumentError, "invalid syntax in defspan #{Macro.to_string(ast)}"
end

as = Keyword.get(opts, :as, name)
Expand Down
51 changes: 0 additions & 51 deletions lib/nebulex/adapter/keyslot.ex

This file was deleted.

6 changes: 3 additions & 3 deletions lib/nebulex/adapter/entry.ex → lib/nebulex/adapter/kv.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule Nebulex.Adapter.Entry do
defmodule Nebulex.Adapter.KV do
@moduledoc """
Specifies the entry API required from adapters.
Specifies the Key/Value API required from adapters.
This behaviour specifies all read/write key-based functions,
the ones applied to a specific cache entry.
the ones applied to a specific cache key.
"""

@typedoc "Proxy type to the adapter meta"
Expand Down
Loading

0 comments on commit 9bf8c9e

Please sign in to comment.