Skip to content

Commit 9b3d50c

Browse files
Clean up indexing
1 parent ee54ac7 commit 9b3d50c

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

application/src/main/java/org/opentripplanner/graph_builder/module/StreetLinkerModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public StreetLinkerModule(
7272
@Override
7373
public void buildGraph() {
7474
timetableRepository.index();
75-
graph.index();
75+
graph.requestIndex();
7676

7777
if (graph.hasStreets) {
7878
linkTransitStops(graph, timetableRepository);

application/src/main/java/org/opentripplanner/routing/graph/Graph.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ public <T extends Vertex> List<T> getVerticesOfType(Class<T> cls) {
208208
*/
209209
@Nullable
210210
public TransitStopVertex getStopVertexForStopId(FeedScopedId id) {
211+
requireIndex();
211212
return streetIndex.findTransitStopVertex(id);
212213
}
213214

@@ -253,6 +254,9 @@ public boolean containsVertex(Vertex v) {
253254

254255
public void remove(Vertex vertex) {
255256
vertices.remove(vertex.getLabel());
257+
if (streetIndex != null) {
258+
streetIndex.remove(vertex);
259+
}
256260
}
257261

258262
public void removeIfUnconnected(Vertex v) {
@@ -282,7 +286,7 @@ public int countEdges() {
282286
/**
283287
* Perform indexing on vertices, edges and create transient data structures. This used to be done
284288
* in readObject methods upon deserialization, but stand-alone mode now allows passing graphs from
285-
* graphbuilder to server in memory, without a round trip through serialization.
289+
* graph builder to server in memory, without a round trip through serialization.
286290
* <p>
287291
* TODO OTP2 - Indexing the streetIndex is not something that should be delegated outside the
288292
* - graph. This allows a module to index the streetIndex BEFORE another module add
@@ -294,6 +298,15 @@ public void index() {
294298
LOG.info("Index street model complete.");
295299
}
296300

301+
/**
302+
* Index this graph if it hasn't been already.
303+
*/
304+
public void requestIndex() {
305+
if (streetIndex == null) {
306+
index();
307+
}
308+
}
309+
297310
@Nullable
298311
public OpeningHoursCalendarService getOpeningHoursCalendarService() {
299312
return this.openingHoursCalendarService;
@@ -314,6 +327,7 @@ public Collection<Vertex> findVertices(Envelope env) {
314327
* the station centroid if the station is configured to route to centroid.
315328
*/
316329
public Set<Vertex> findStopVertices(FeedScopedId stopId) {
330+
requireIndex();
317331
return streetIndex.findStopVertices(stopId);
318332
}
319333

@@ -374,12 +388,6 @@ public void setDistanceBetweenElevationSamples(double distanceBetweenElevationSa
374388
CompactElevationProfile.setDistanceBetweenSamplesM(distanceBetweenElevationSamples);
375389
}
376390

377-
private void indexIfNotIndexed() {
378-
if (streetIndex == null) {
379-
index();
380-
}
381-
}
382-
383391
private void requireIndex() {
384392
if (streetIndex == null) {
385393
throw new IllegalStateException("Graph must be indexed before querying.");

application/src/main/java/org/opentripplanner/routing/graph/StreetIndex.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ void remove(Edge e, Scope scope) {
141141
edgeIndex.remove(e, scope);
142142
}
143143

144+
/**
145+
* Remove a vertex from the index.
146+
*/
147+
void remove(Vertex vertex) {
148+
vertexIndex.remove(new Envelope(vertex.getCoordinate()), vertex);
149+
}
150+
144151
private static LineString edgeGeometryOrStraightLine(Edge e) {
145152
LineString geometry = e.getGeometry();
146153
if (geometry == null) {

0 commit comments

Comments
 (0)