Skip to content

Commit

Permalink
Merge pull request #629 from Th3-M4jor/sunset-global-qlc-cache-functi…
Browse files Browse the repository at this point in the history
…onality

Sunset QLC requirements from cache behaviour
  • Loading branch information
jchristgit committed Aug 23, 2024
2 parents d0c62c9 + 224b88c commit 8e6e82e
Show file tree
Hide file tree
Showing 21 changed files with 326 additions and 493 deletions.
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

0 comments on commit 8e6e82e

Please sign in to comment.