Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove service date from cache key of SiriTripPatternCache #6069

Draft
wants to merge 2 commits into
base: dev-2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ private Result<UpdateSuccess, UpdateError> addTripToGraphAndBuffer(TripUpdate tr
pattern = tripUpdate.addedTripPattern();
} else {
// Get cached trip pattern or create one if it doesn't exist yet
pattern =
tripPatternCache.getOrCreateTripPattern(tripUpdate.stopPattern(), trip, serviceDate);
pattern = tripPatternCache.getOrCreateTripPattern(tripUpdate.stopPattern(), trip);
}

// Add new trip times to buffer, making protective copies as needed. Bubble success/error up.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.opentripplanner.ext.siri;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -30,10 +29,13 @@
*/
public class SiriTripPatternCache {

// TODO RT_AB: Improve documentation. This seems to be the primary collection of added
// TripPatterns, with other collections serving as indexes. Similar to TripPatternCache.cache
// in the GTFS version of this class, but with service date as part of the key.
private final Map<StopPatternServiceDateKey, TripPattern> cache = new HashMap<>();
/**
* We cache the trip pattern based on the stop pattern only in order to de-duplicate them.
* <p>
* Note that we don't really have a definition which properties are really part of the trip
* pattern and several pattern keys are used in different parts of OTP.
*/
private final Map<StopPattern, TripPattern> cache = new HashMap<>();

// TODO RT_AB: generalize this so we can generate IDs for SIRI or GTFS-RT sources.
private final SiriTripPatternIdGenerator tripPatternIdGenerator;
Expand Down Expand Up @@ -72,8 +74,7 @@ public SiriTripPatternCache(
*/
public synchronized TripPattern getOrCreateTripPattern(
@Nonnull final StopPattern stopPattern,
@Nonnull final Trip trip,
@Nonnull LocalDate serviceDate
@Nonnull final Trip trip
) {
TripPattern originalTripPattern = getPatternForTrip.apply(trip);

Expand All @@ -85,8 +86,7 @@ public synchronized TripPattern getOrCreateTripPattern(
}

// Check cache for trip pattern
StopPatternServiceDateKey key = new StopPatternServiceDateKey(stopPattern, serviceDate);
TripPattern tripPattern = cache.get(key);
TripPattern tripPattern = cache.get(stopPattern);

// Create TripPattern if it doesn't exist yet
if (tripPattern == null) {
Expand All @@ -104,44 +104,9 @@ public synchronized TripPattern getOrCreateTripPattern(
// TODO: Add pattern to transitModel index?

// Add pattern to cache
cache.put(key, tripPattern);
cache.put(stopPattern, tripPattern);
}

return tripPattern;
}
}

// TODO RT_AB: move the below classes inside the above class as private static inner classes.
// Defining these additional classes in the same top-level class file is unconventional.

/**
* Serves as the key for the collection of TripPatterns added by realtime messages.
* Must define hashcode and equals to confer semantic identity.
* TODO RT_AB: clarify why each date has a different TripPattern instead of a different Timetable.
* It seems like there's a separate TripPattern instance for each StopPattern and service date,
* rather a single TripPattern instance associated with a separate timetable for each date.
*/
class StopPatternServiceDateKey {

StopPattern stopPattern;
LocalDate serviceDate;

public StopPatternServiceDateKey(StopPattern stopPattern, LocalDate serviceDate) {
this.stopPattern = stopPattern;
this.serviceDate = serviceDate;
}

@Override
public int hashCode() {
return stopPattern.hashCode() + serviceDate.hashCode();
}

@Override
public boolean equals(Object thatObject) {
if (!(thatObject instanceof StopPatternServiceDateKey)) {
return false;
}
StopPatternServiceDateKey that = (StopPatternServiceDateKey) thatObject;
return (this.stopPattern.equals(that.stopPattern) && this.serviceDate.equals(that.serviceDate));
}
}
Loading