Files
music_library/lib/music_library/wishlist.ex
T
Claudio Ortolina f48372f7b3 Use MapSets for performance
Not noticeable with current database size, but lays a good foundation
for larger libraries.

For example, given a list of 300 artist IDs, checking if an ID is in the
list takes between 30 and 40 ms (for an ID towards the end of the list),
while the MapSet version takes consistently between 15 and 20 ms.
2024-12-10 14:21:57 +03:00

38 lines
926 B
Elixir

defmodule MusicLibrary.Wishlist do
import Ecto.Query, warn: false
alias MusicLibrary.Repo
alias MusicLibrary.Records
alias MusicLibrary.Records.SearchIndex
def search_records(query, opts \\ []) do
limit = Keyword.get(opts, :limit, 20)
offset = Keyword.get(opts, :offset, 0)
Records.search_records(base_search(), query, limit: limit, offset: offset)
end
def search_records_count(query) do
Records.search_records_count(base_search(), query)
end
def count do
Repo.aggregate(base_search(), :count)
end
def wishlisted_release_ids(release_ids) do
q =
from r in fragment("records, json_each(records.release_ids)"),
where: r.value in ^release_ids,
where: fragment("records.purchased_at IS NULL"),
select: r.value
q |> Repo.all() |> MapSet.new()
end
defp base_search do
from r in SearchIndex,
where: is_nil(r.purchased_at)
end
end