Skip to content

Commit 4cfae61

Browse files
Merge pull request opentripplanner#7219 from ibi-group/combine-trip-pattern-caches
Use SIRI trip pattern cache in GTFS updater
2 parents e5b1e88 + c4787c9 commit 4cfae61

5 files changed

Lines changed: 33 additions & 26 deletions

File tree

application/src/main/java/org/opentripplanner/updater/trip/gtfs/GtfsRealTimeTripUpdateAdapter.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
import org.opentripplanner.updater.trip.UpdateIncrementality;
6363
import org.opentripplanner.updater.trip.gtfs.model.AddedRoute;
6464
import org.opentripplanner.updater.trip.gtfs.model.TripUpdate;
65+
import org.opentripplanner.updater.trip.siri.SiriTripPatternCache;
66+
import org.opentripplanner.updater.trip.siri.SiriTripPatternIdGenerator;
6567
import org.slf4j.Logger;
6668
import org.slf4j.LoggerFactory;
6769
import org.slf4j.event.Level;
@@ -73,8 +75,15 @@ public class GtfsRealTimeTripUpdateAdapter {
7375

7476
private static final Logger LOG = LoggerFactory.getLogger(GtfsRealTimeTripUpdateAdapter.class);
7577

76-
/** A synchronized cache of trip patterns added to the graph due to GTFS-realtime messages. */
77-
private final TripPatternCache tripPatternCache = new TripPatternCache();
78+
/**
79+
* A synchronized cache of trip patterns added to the timetable repository
80+
* due to GTFS-realtime messages.
81+
* <p>
82+
* This has "Siri" in the name because we are combining the two versions very carefully, step by
83+
* step. Once this process is complete, we will clean up the name and move it to an appropriate
84+
* package.
85+
**/
86+
private final SiriTripPatternCache tripPatternCache;
7887

7988
private final ZoneId timeZone;
8089

@@ -106,6 +115,10 @@ public GtfsRealTimeTripUpdateAdapter(
106115
snapshotManager.getTimetableSnapshotBuffer()
107116
);
108117
this.deduplicator = timetableRepository.getDeduplicator();
118+
this.tripPatternCache = new SiriTripPatternCache(
119+
new SiriTripPatternIdGenerator(),
120+
transitEditorService::findPattern
121+
);
109122
}
110123

111124
/**
@@ -404,11 +417,7 @@ private Result<UpdateSuccess, UpdateError> handleScheduledTrip(
404417

405418
final Trip trip = transitEditorService.getTrip(tripId);
406419
// Get cached trip pattern or create one if it doesn't exist yet
407-
final TripPattern newPattern = tripPatternCache.getOrCreateTripPattern(
408-
newStopPattern,
409-
trip,
410-
pattern
411-
);
420+
final TripPattern newPattern = tripPatternCache.getOrCreateTripPattern(newStopPattern, trip);
412421

413422
cancelScheduledTrip(tripId, serviceDate, CancelationType.DELETE);
414423
return snapshotManager.updateBuffer(
@@ -649,13 +658,8 @@ private Result<UpdateSuccess, UpdateError> addNewOrReplacementTripToSnapshot(
649658
// Create StopPattern
650659
final StopPattern stopPattern = tripTimesWithStopPattern.stopPattern();
651660

652-
final TripPattern originalTripPattern = transitEditorService.findPattern(trip);
653661
// Get cached trip pattern or create one if it doesn't exist yet
654-
final TripPattern pattern = tripPatternCache.getOrCreateTripPattern(
655-
stopPattern,
656-
trip,
657-
originalTripPattern
658-
);
662+
final TripPattern pattern = tripPatternCache.getOrCreateTripPattern(stopPattern, trip);
659663

660664
trace(
661665
trip.getId(),

application/src/main/java/org/opentripplanner/updater/trip/siri/SiriTripPatternCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* the thread-safety, but just by looking at a few lines of code I see problems - a strategy needs to be
2727
* analysed, designed and documented.
2828
*/
29-
class SiriTripPatternCache {
29+
public class SiriTripPatternCache {
3030

3131
/**
3232
* We cache the trip pattern based on the stop pattern only in order to de-duplicate them.
@@ -80,7 +80,7 @@ public synchronized TripPattern getOrCreateTripPattern(
8080
// TODO RT_AB: Verify implementation, which is different than the GTFS-RT version.
8181
// It can return a TripPattern from the scheduled data, but protective copies are handled in
8282
// TimetableSnapshot.update. Better document this aspect of the contract in this method's Javadoc.
83-
if (originalTripPattern.getStopPattern().equals(stopPattern)) {
83+
if (originalTripPattern != null && originalTripPattern.getStopPattern().equals(stopPattern)) {
8484
return originalTripPattern;
8585
}
8686

application/src/main/java/org/opentripplanner/updater/trip/siri/SiriTripPatternIdGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* usage pattern is not enforced due to differing needs in tests.
1515
* The ID generation is threadsafe, even if that is probably not needed.
1616
*/
17-
class SiriTripPatternIdGenerator {
17+
public class SiriTripPatternIdGenerator {
1818

1919
private final AtomicInteger counter = new AtomicInteger(0);
2020

application/src/test/java/org/opentripplanner/updater/trip/gtfs/moduletests/addition/AddedThenRemovedTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void addedThenRemoved() {
4545

4646
assertThat(env.raptorData().summarizePatterns()).containsExactly(
4747
"F:Pattern1[SCHEDULED]",
48-
"F:AddedTrip::rt#1[ADDED]"
48+
"F:AddedTrip::001:RT[ADDED]"
4949
);
5050

5151
// the GTFS updater is configured to clear timetables, so an empty list should remove the

application/src/test/java/org/opentripplanner/updater/trip/gtfs/moduletests/delay/AssignedStopIdsTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void assignedThenRevertedStopIds() {
7171
env.tripData(TRIP_1_ID).showTimetable()
7272
);
7373
assertTrue(env.tripData(TRIP_1_ID).tripPattern().isStopPatternModifiedInRealTime());
74-
assertEquals(List.of("F:Route1::rt#1[UPDATED]"), env.raptorData().summarizePatterns());
74+
assertEquals(List.of("F:Route1::001:RT[UPDATED]"), env.raptorData().summarizePatterns());
7575

7676
var tripUpdate2 = rt
7777
.tripUpdateScheduled(TRIP_1_ID)
@@ -86,7 +86,7 @@ void assignedThenRevertedStopIds() {
8686
env.tripData(TRIP_1_ID).showTimetable()
8787
);
8888
assertTrue(env.tripData(TRIP_1_ID).tripPattern().isStopPatternModifiedInRealTime());
89-
assertEquals(List.of("F:Route1::rt#2[UPDATED]"), env.raptorData().summarizePatterns());
89+
assertEquals(List.of("F:Route1::002:RT[UPDATED]"), env.raptorData().summarizePatterns());
9090

9191
var tripUpdate3 = rt
9292
.tripUpdateScheduled(TRIP_1_ID)
@@ -136,7 +136,7 @@ void reuseRealtimeTripPatterns() {
136136
assertTrue(env.tripData(TRIP_1_ID).tripPattern().isStopPatternModifiedInRealTime());
137137
assertFalse(env.tripData(TRIP_2_ID).tripPattern().isStopPatternModifiedInRealTime());
138138
assertEquals(
139-
List.of("F:Pattern1[SCHEDULED]", "F:Route1::rt#1[UPDATED]"),
139+
List.of("F:Pattern1[SCHEDULED]", "F:Route1::001:RT[UPDATED]"),
140140
env.raptorData().summarizePatterns()
141141
);
142142

@@ -151,7 +151,10 @@ void reuseRealtimeTripPatterns() {
151151
);
152152
assertTrue(env.tripData(TRIP_1_ID).tripPattern().isStopPatternModifiedInRealTime());
153153
assertTrue(env.tripData(TRIP_2_ID).tripPattern().isStopPatternModifiedInRealTime());
154-
assertEquals(List.of("F:Route1::rt#1[UPDATED,UPDATED]"), env.raptorData().summarizePatterns());
154+
assertEquals(
155+
List.of("F:Route1::001:RT[UPDATED,UPDATED]"),
156+
env.raptorData().summarizePatterns()
157+
);
155158

156159
assertSuccess(rt.applyTripUpdate(tripUpdate2));
157160
assertEquals(
@@ -165,7 +168,7 @@ void reuseRealtimeTripPatterns() {
165168
assertFalse(env.tripData(TRIP_1_ID).tripPattern().isStopPatternModifiedInRealTime());
166169
assertTrue(env.tripData(TRIP_2_ID).tripPattern().isStopPatternModifiedInRealTime());
167170
assertEquals(
168-
List.of("F:Pattern1[SCHEDULED]", "F:Route1::rt#1[UPDATED]"),
171+
List.of("F:Pattern1[SCHEDULED]", "F:Route1::001:RT[UPDATED]"),
169172
env.raptorData().summarizePatterns()
170173
);
171174

@@ -256,7 +259,7 @@ void reuseRealtimeTripPatternsOnDifferentServiceDates() {
256259
env.tripData(TRIP_2_ID, SERVICE_DATE_PLUS).tripPattern().isStopPatternModifiedInRealTime()
257260
);
258261
assertEquals(
259-
List.of("F:Route1::rt#1[UPDATED,UPDATED]"),
262+
List.of("F:Route1::001:RT[UPDATED,UPDATED]"),
260263
env.raptorData(SERVICE_DATE).summarizePatterns()
261264
);
262265
assertEquals(
@@ -296,11 +299,11 @@ void reuseRealtimeTripPatternsOnDifferentServiceDates() {
296299
env.tripData(TRIP_2_ID, SERVICE_DATE_PLUS).tripPattern().isStopPatternModifiedInRealTime()
297300
);
298301
assertEquals(
299-
List.of("F:Route1::rt#1[UPDATED,UPDATED]"),
302+
List.of("F:Route1::001:RT[UPDATED,UPDATED]"),
300303
env.raptorData(SERVICE_DATE).summarizePatterns()
301304
);
302305
assertEquals(
303-
List.of("F:Route1::rt#1[UPDATED,UPDATED]"),
306+
List.of("F:Route1::001:RT[UPDATED,UPDATED]"),
304307
env.raptorData(SERVICE_DATE_PLUS).summarizePatterns()
305308
);
306309

@@ -338,7 +341,7 @@ void reuseRealtimeTripPatternsOnDifferentServiceDates() {
338341
env.raptorData(SERVICE_DATE).summarizePatterns()
339342
);
340343
assertEquals(
341-
List.of("F:Route1::rt#1[UPDATED,UPDATED]"),
344+
List.of("F:Route1::001:RT[UPDATED,UPDATED]"),
342345
env.raptorData(SERVICE_DATE_PLUS).summarizePatterns()
343346
);
344347

0 commit comments

Comments
 (0)