-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleagues_protobuffer_ets.ex
38 lines (27 loc) · 1.13 KB
/
leagues_protobuffer_ets.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
defmodule LeaguesData.LeaguesProtoBufferETS do
@behaviour LeaguesData.LeaguesDataBehavior
@moduledoc """
This module loads Leagues CSV in the ETS at initialization time in a Protocol Buffer format, and publishes the data.
"""
import LeaguesData.LeaguesProtoBufferUtils
@impl LeaguesData.LeaguesDataBehavior
def child_spec([path]) do
:ets.new(:leagues_protobuff, [:named_table, :set, :protected, read_concurrency: true])
list = LeaguesData.LeaguesCSV.read_file_mem!(path)
leagues_seasons = process_leagues_buffer(list)
:ets.insert(:leagues_protobuff, {:leaguesSeasons, leagues_seasons})
list |> Enum.map(fn ({k, l}) -> :ets.insert(:leagues_protobuff, {k, process_matches_buffer(l)}) end)
# return :ok as it is not a GenServer agent
:ok
end
@impl LeaguesData.LeaguesDataBehavior
def leagues() do
[{:leaguesSeasons, leagues}] = :ets.lookup(:leagues_protobuff, :leaguesSeasons)
leagues
end
@impl LeaguesData.LeaguesDataBehavior
def matches(league, season) do
[{{^league, ^season}, matches}] = :ets.lookup(:leagues_protobuff, {league, season})
matches
end
end