Skip to content

Commit

Permalink
Clear added pattern in TimetableSnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
vpaturet committed Oct 10, 2024
1 parent 9e58db4 commit 2491e22
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/main/java/org/opentripplanner/model/TimetableSnapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,13 @@ public void clear(String feedId) {
boolean newTripPatternsForModifiedTripsWereCleared = clearNewTripPatternsForModifiedTrips(
feedId
);
boolean addedTripPatternsWereCleared = clearRealtimeAddedTripPatterns(feedId);
// If this snapshot was modified, it will be dirty after the clear actions.
if (timetablesWereCleared || newTripPatternsForModifiedTripsWereCleared) {
if (
timetablesWereCleared ||
newTripPatternsForModifiedTripsWereCleared ||
addedTripPatternsWereCleared
) {
dirty = true;
}
}
Expand Down Expand Up @@ -561,6 +566,22 @@ private boolean clearNewTripPatternsForModifiedTrips(String feedId) {
.removeIf(tripIdAndServiceDate -> feedId.equals(tripIdAndServiceDate.tripId().getFeedId()));
}

private boolean clearRealtimeAddedTripPatterns(String feedId) {
boolean removedEntry = realTimeAddedTrips
.keySet()
.removeIf(id -> feedId.equals(id.getFeedId()));
realTimeAddedPatternForTrip.keySet().removeIf(trip -> feedId.equals(trip.getId().getFeedId()));
realTimeAddedTripOnServiceDateForTripAndDay
.keySet()
.removeIf(tripOnServiceDate -> feedId.equals(tripOnServiceDate.tripId().getFeedId()));
realTimeAddedTripOnServiceDateById.keySet().removeIf(id -> feedId.equals(id.getFeedId()));
realTimeAddedPatternsForRoute
.keySet()
.removeIf(route -> feedId.equals(route.getId().getFeedId()));
realtimeAddedRoutes.keySet().removeIf(id -> feedId.equals(id.getFeedId()));
return removedEntry;
}

/**
* Add the patterns to the stop index, only if they come from a modified pattern
*/
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/org/opentripplanner/model/TimetableSnapshotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,30 @@
import java.time.ZoneId;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.opentripplanner.ConstantsForTests;
import org.opentripplanner.TestOtpModel;
import org.opentripplanner._support.time.ZoneIds;
import org.opentripplanner.transit.model.framework.Deduplicator;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.framework.Result;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripIdAndServiceDate;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.transit.model.timetable.TripTimesFactory;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.updater.spi.UpdateError;
import org.opentripplanner.updater.trip.BackwardsDelayPropagationType;

public class TimetableSnapshotTest {

private static final ZoneId timeZone = ZoneIds.GMT;
public static final LocalDate SERVICE_DATE = LocalDate.of(2024, 1, 1);
private static Map<FeedScopedId, TripPattern> patternIndex;
static String feedId;

Expand Down Expand Up @@ -304,6 +311,48 @@ void testCannotRevertReadOnlyTimetableSnapshot() {
);
}

@Test
void testClear() {
TimetableSnapshot snapshot = new TimetableSnapshot();
TripPattern pattern = patternIndex.get(new FeedScopedId(feedId, "1.1"));
Trip trip = pattern.scheduledTripsAsStream().findFirst().orElseThrow();
TripIdAndServiceDate tripIdAndServiceDate = new TripIdAndServiceDate(
trip.getId(),
SERVICE_DATE
);
TripTimes updatedTriptimes = TripTimesFactory.tripTimes(
trip,
List.of(new StopTime()),
new Deduplicator()
);
RealTimeTripUpdate realTimeTripUpdate = new RealTimeTripUpdate(
pattern,
updatedTriptimes,
SERVICE_DATE,
TripOnServiceDate.of(trip.getId()).withTrip(trip).withServiceDate(SERVICE_DATE).build(),
true,
true
);

snapshot.update(realTimeTripUpdate);

assertNotNull(snapshot.getRealTimeAddedTrip(trip.getId()));
assertNotNull(snapshot.getRealTimeAddedPatternForTrip(trip));
assertNotNull(snapshot.getRealTimeAddedPatternForRoute(pattern.getRoute()));
assertNotNull(snapshot.getRealTimeAddedTripOnServiceDateById(trip.getId()));
assertNotNull(snapshot.getRealTimeAddedTripOnServiceDateForTripAndDay(tripIdAndServiceDate));
assertNotNull(snapshot.getRealtimeAddedRoute(pattern.getRoute().getId()));

snapshot.clear(trip.getId().getFeedId());

assertNull(snapshot.getRealTimeAddedTrip(trip.getId()));
assertNull(snapshot.getRealTimeAddedPatternForTrip(trip));
assertNull(snapshot.getRealTimeAddedTripOnServiceDateById(trip.getId()));
assertNull(snapshot.getRealTimeAddedTripOnServiceDateForTripAndDay(tripIdAndServiceDate));
assertNull(snapshot.getRealtimeAddedRoute(pattern.getRoute().getId()));
assertTrue(snapshot.getRealTimeAddedPatternForRoute(pattern.getRoute()).isEmpty());
}

private static TimetableSnapshot createCommittedSnapshot() {
TimetableSnapshot timetableSnapshot = new TimetableSnapshot();
return timetableSnapshot.commit(null, true);
Expand Down

0 comments on commit 2491e22

Please sign in to comment.