Skip to content

Commit

Permalink
Navitia: More efficient product parsing
Browse files Browse the repository at this point in the history
According to hove-io/navitia#1386, Navitia has finally added the
product to departure query results, so all information we need can now
be retrieved by one network query only.

The AbstractNavitiaProvider was adapted accordingly and all legacy code
including the Product Cache was removed.
  • Loading branch information
grote committed May 27, 2016
1 parent 62a5a54 commit 0bd5bf4
Showing 1 changed file with 6 additions and 59 deletions.
65 changes: 6 additions & 59 deletions enabler/src/de/schildbach/pte/AbstractNavitiaProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -669,15 +669,18 @@ private void parseQueryTripsResult(final JSONObject head, final Location from, f
}
}

private Line parseLine(final JSONObject jsonLine) throws IOException
private Line parseLine(final JSONObject jsonRoute) throws IOException
{
try
{
final JSONObject jsonLine = jsonRoute.getJSONObject("line");
final String lineId = jsonLine.getString("id");
String network = null;
if (jsonLine.has("network"))
network = Strings.emptyToNull(jsonLine.getJSONObject("network").optString("name"));
final Product product = parseLineProduct(jsonLine);
final JSONObject mode = jsonRoute.getJSONArray("physical_modes").getJSONObject(0);
final String modeId = mode.getString("id");
final Product product = parseLineProductFromMode(modeId);
final String code = jsonLine.getString("code");
final String name = Strings.emptyToNull(jsonLine.optString("name"));
final String color = Strings.emptyToNull(jsonLine.getString("color"));
Expand All @@ -692,8 +695,6 @@ private Line parseLine(final JSONObject jsonLine) throws IOException
}
}

private Map<String, Product> lineProductCache = new WeakHashMap<String, Product>();

private Product parseLineProductFromMode(final String modeId)
{
final String modeType = modeId.replace("physical_mode:", "");
Expand Down Expand Up @@ -726,59 +727,6 @@ private Product parseLineProductFromMode(final String modeId)
}
}

private Product parseLineProduct(final JSONObject line) throws IOException
{
try
{
final String lineId = line.getString("id");
final JSONObject mode;

if (line.has("physical_modes"))
{
mode = line.getJSONArray("physical_modes").getJSONObject(0);
}
else
{
final Product cachedProduct = lineProductCache.get(lineId);
if (cachedProduct != null)
return cachedProduct;

// this makes a network request and is sometimes necessary
mode = getLinePhysicalMode(lineId);
}

final String modeId = mode.getString("id");
final Product product = parseLineProductFromMode(modeId);

lineProductCache.put(lineId, product);

return product;
}
catch (final JSONException jsonExc)
{
throw new ParserException(jsonExc);
}
}

private JSONObject getLinePhysicalMode(final String lineId) throws IOException
{
final String uri = uri() + "lines/" + ParserUtils.urlEncode(lineId) + "/physical_modes";
final CharSequence page = httpClient.get(uri);

try
{
final JSONObject head = new JSONObject(page.toString());
final JSONArray physicalModes = head.getJSONArray("physical_modes");
final JSONObject physicalMode = physicalModes.getJSONObject(0);

return physicalMode;
}
catch (final JSONException jsonExc)
{
throw new ParserException(jsonExc);
}
}

private LineDestination getStationLine(final Line line, final JSONObject jsonDeparture) throws IOException
{
try
Expand Down Expand Up @@ -971,8 +919,7 @@ public QueryDeparturesResult queryDepartures(final String stationId, final @Null

// Build line.
final JSONObject route = jsonDeparture.getJSONObject("route");
final JSONObject jsonLine = route.getJSONObject("line");
final Line line = parseLine(jsonLine);
final Line line = parseLine(route);

final JSONObject stopPoint = jsonDeparture.getJSONObject("stop_point");
final Location location = parsePlace(stopPoint, PlaceType.STOP_POINT);
Expand Down

0 comments on commit 0bd5bf4

Please sign in to comment.