Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sunset QLC requirements from cache behaviour #629

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions lib/nostrum/cache/guild_cache/ets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,21 @@ defmodule Nostrum.Cache.GuildCache.ETS do
@doc since: "0.10.0"
@spec all() :: Enumerable.t(Guild.t())
def all do
ms = [{{:_, :"$1"}, [], [:"$1"]}]
Stream.resource(
fn -> :ets.select(@table_name, ms, 100)
end,
fn items ->
case items do
{matches, cont} ->
{matches, :ets.select(cont)}
:"$end_of_table" -> {:halt, nil} end
end,
fn _cont -> :ok end
)
ms = [{{:_, :"$1"}, [], [:"$1"]}]

Stream.resource(
fn -> :ets.select(@table_name, ms, 100) end,
fn items ->
case items do
{matches, cont} ->
{matches, :ets.select(cont)}

:"$end_of_table" ->
{:halt, nil}
end
end,
fn _cont -> :ok end
)
end

@doc "Create the given guild in the cache."
Expand Down
18 changes: 11 additions & 7 deletions lib/nostrum/cache/guild_cache/mnesia.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,26 @@ if Code.ensure_loaded?(:mnesia) do
end)
end

@impl GuildCache
@doc since: "0.10.0"
@spec all() :: Enumerable.t(Guild.t())
def all do
@impl GuildCache
@doc since: "0.10.0"
@spec all() :: Enumerable.t(Guild.t())
def all do
ms = [{{:_, :_, :"$1"}, [], [:"$1"]}]

Stream.resource(
fn -> :mnesia.select(@table_name, ms, 100, :read) end,
fn items ->
case items do
{matches, cont} ->
{matches, :mnesia.select(cont)}
:"$end_of_table" -> {:halt, nil} end

:"$end_of_table" ->
{:halt, nil}
end
end,
fn _cont -> :ok end
fn _cont -> :ok end
)
end
end

# Used by dispatch

Expand Down
8 changes: 4 additions & 4 deletions lib/nostrum/cache/member_cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ defmodule Nostrum.Cache.MemberCache do
to wrap calls to this function in `wrap_query/1`.
"""
@doc since: "0.10.0"
@callback by_user(User.id()) :: {:ok, Enumerable.t({Guild.id(), Member.t()})} | {:error. atom()}
@callback by_user(User.id()) :: Enumerable.t({Guild.id(), Member.t()})

@doc """
Yield an enumerable of members associated with the given guild ID.
"""
@doc since: "0.10.0"
@callback by_guild(Guild.id()) :: {:ok, Enumerable.t(Member.t())} | {:error. atom()}
@callback by_guild(Guild.id()) :: Enumerable.t(Member.t())

@doc """
Add the member for the given guild from upstream data.
Expand Down Expand Up @@ -221,13 +221,13 @@ defmodule Nostrum.Cache.MemberCache do
Enum.reduce(
enumerable,
acc,
fn (%Member{user_id: user_id} = member, acc) ->
fn %Member{user_id: user_id} = member, acc ->
# credo:disable-for-next-line
case UserCache.get(user_id) do
{:ok, user} -> fun.({member, user}, acc)
_error -> acc
end
end
end
)
end)
end
Expand Down
54 changes: 30 additions & 24 deletions lib/nostrum/cache/member_cache/ets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,35 +79,41 @@ defmodule Nostrum.Cache.MemberCache.ETS do
@impl MemberCache
@doc since: "0.10.0"
def by_user(user_id) do
ms = [{{{:"$1", user_id}, :"$2"}, [], [{{:"$1", :"$2"}}]}]
Stream.resource(
fn -> :ets.select(@table_name, ms, 100)
end,
fn items ->
case items do
{matches, cont} ->
{matches, :ets.select(cont)}
:"$end_of_table" -> {:halt, nil} end
end,
fn _cont -> :ok end
)
ms = [{{{:"$1", user_id}, :"$2"}, [], [{{:"$1", :"$2"}}]}]

Stream.resource(
fn -> :ets.select(@table_name, ms, 100) end,
fn items ->
case items do
{matches, cont} ->
{matches, :ets.select(cont)}

:"$end_of_table" ->
{:halt, nil}
end
end,
fn _cont -> :ok end
)
end

@impl MemberCache
@doc since: "0.10.0"
def by_guild(guild_id) do
ms = [{{{guild_id, :_}, :"$1"}, [], [:"$1"]}]
Stream.resource(
fn -> :ets.select(@table_name, ms, 100)
end,
fn items ->
case items do
{matches, cont} ->
{matches, :ets.select(cont)}
:"$end_of_table" -> {:halt, nil} end
end,
fn _cont -> :ok end
)
ms = [{{{guild_id, :_}, :"$1"}, [], [:"$1"]}]

Stream.resource(
fn -> :ets.select(@table_name, ms, 100) end,
fn items ->
case items do
{matches, cont} ->
{matches, :ets.select(cont)}

:"$end_of_table" ->
{:halt, nil}
end
end,
fn _cont -> :ok end
)
end

@doc "Add the given member to the given guild in the cache."
Expand Down
16 changes: 12 additions & 4 deletions lib/nostrum/cache/member_cache/mnesia.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,39 @@ if Code.ensure_loaded?(:mnesia) do
@doc since: "0.10.0"
def by_user(user_id) do
ms = [{{:_, :_, :"$1", user_id, :"$2"}, [], [{{:"$1", :"$2"}}]}]

Stream.resource(
fn -> :mnesia.select(@table_name, ms, 100, :read) end,
fn items ->
case items do
{matches, cont} ->
{matches, :mnesia.select(cont)}
:"$end_of_table" -> {:halt, nil} end

:"$end_of_table" ->
{:halt, nil}
end
end,
fn _cont -> :ok end
fn _cont -> :ok end
)
end

@impl MemberCache
@doc since: "0.10.0"
def by_guild(guild_id) do
ms = [{{:_, :_, guild_id, :_, :"$1"}, [], [:"$1"]}]

Stream.resource(
fn -> :mnesia.select(@table_name, ms, 100, :read) end,
fn items ->
case items do
{matches, cont} ->
{matches, :mnesia.select(cont)}
:"$end_of_table" -> {:halt, nil} end

:"$end_of_table" ->
{:halt, nil}
end
end,
fn _cont -> :ok end
fn _cont -> :ok end
)
end

Expand Down
Loading