-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move route_type cache to separate module instead of relying on trip_i…
…d to check route type
- Loading branch information
Showing
7 changed files
with
104 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule Concentrate.GTFS.Routes do | ||
@moduledoc """ | ||
Server which maintains a list of route_id -> route_type mappings. | ||
""" | ||
use GenStage | ||
require Logger | ||
import :binary, only: [copy: 1] | ||
@table __MODULE__ | ||
|
||
def start_link(opts) do | ||
GenStage.start_link(__MODULE__, opts, name: __MODULE__) | ||
end | ||
|
||
def route_type(route_id) do | ||
hd(:ets.lookup_element(@table, route_id, 2)) | ||
rescue | ||
ArgumentError -> nil | ||
end | ||
|
||
def init(opts) do | ||
@table = :ets.new(@table, [:named_table, :public, :duplicate_bag]) | ||
{:consumer, %{}, opts} | ||
end | ||
|
||
def handle_events(events, _from, state) do | ||
inserts = | ||
for event <- events, | ||
{"routes.txt", route_body} <- event, | ||
lines = String.split(route_body, "\n"), | ||
{:ok, row} <- CSV.decode(lines, headers: true) do | ||
{copy(row["route_id"]), String.to_integer(row["route_type"])} | ||
end | ||
|
||
_ = | ||
if inserts == [] do | ||
:ok | ||
else | ||
true = :ets.delete_all_objects(@table) | ||
:ets.insert(@table, inserts) | ||
|
||
Logger.info(fn -> | ||
"#{__MODULE__}: updated with #{length(inserts)} records" | ||
end) | ||
end | ||
|
||
{:noreply, [], state, :hibernate} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule Concentrate.GTFS.RoutesTest do | ||
@moduledoc false | ||
use ExUnit.Case | ||
import Concentrate.GTFS.Routes | ||
|
||
@body """ | ||
route_id,route_type | ||
route_1,0 | ||
route_2,1 | ||
route_3,2 | ||
""" | ||
|
||
defp supervised(_) do | ||
start_supervised(Concentrate.GTFS.Routes) | ||
event = [{"routes.txt", @body}] | ||
# relies on being able to update the table from a different process | ||
handle_events([event], :ignored, :ignored) | ||
:ok | ||
end | ||
|
||
describe "route_type/1" do | ||
setup :supervised | ||
|
||
test "returns the route_type for the given route_id" do | ||
assert route_type("route_1") == 0 | ||
assert route_type("route_2") == 1 | ||
assert route_type("route_3") == 2 | ||
assert route_type("unknown") == nil | ||
end | ||
end | ||
|
||
describe "missing ETS table" do | ||
test "route_type/1 returns nil" do | ||
assert route_type("route_1") == nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters