Skip to content

Commit

Permalink
Rename TimetableResolver to TimetableSnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoelewijn committed Mar 23, 2015
1 parent 38bf22a commit e42da19
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 75 deletions.
Expand Up @@ -27,7 +27,7 @@ the License, or (at your option) any later version.
import org.opentripplanner.routing.algorithm.strategies.TrivialRemainingWeightHeuristic; import org.opentripplanner.routing.algorithm.strategies.TrivialRemainingWeightHeuristic;
import org.opentripplanner.routing.edgetype.StreetEdge; import org.opentripplanner.routing.edgetype.StreetEdge;
import org.opentripplanner.routing.edgetype.TemporaryPartialStreetEdge; import org.opentripplanner.routing.edgetype.TemporaryPartialStreetEdge;
import org.opentripplanner.routing.edgetype.TimetableResolver; import org.opentripplanner.routing.edgetype.TimetableSnapshot;
import org.opentripplanner.routing.error.GraphNotFoundException; import org.opentripplanner.routing.error.GraphNotFoundException;
import org.opentripplanner.routing.error.TransitTimesException; import org.opentripplanner.routing.error.TransitTimesException;
import org.opentripplanner.routing.error.VertexNotFoundException; import org.opentripplanner.routing.error.VertexNotFoundException;
Expand Down Expand Up @@ -93,8 +93,8 @@ public class RoutingContext implements Cloneable {


public final TransferTable transferTable; public final TransferTable transferTable;


/** The timetableSnapshot is a {@link TimetableResolver} for looking up real-time updates. */ /** The timetableSnapshot is a {@link TimetableSnapshot} for looking up real-time updates. */
public final TimetableResolver timetableSnapshot; public final TimetableSnapshot timetableSnapshot;


/** /**
* Cache lists of which transit services run on which midnight-to-midnight periods. This ties a TraverseOptions to a particular start time for the * Cache lists of which transit services run on which midnight-to-midnight periods. This ties a TraverseOptions to a particular start time for the
Expand Down
Expand Up @@ -44,7 +44,7 @@ the License, or (at your option) any later version.
* *
* At this point, only one writing thread at a time is supported. * At this point, only one writing thread at a time is supported.
*/ */
public class TimetableResolver { public class TimetableSnapshot {


protected static class SortedTimetableComparator implements Comparator<Timetable> { protected static class SortedTimetableComparator implements Comparator<Timetable> {
@Override @Override
Expand Down Expand Up @@ -94,7 +94,7 @@ public boolean equals(Object obj) {
} }
} }


private static final Logger LOG = LoggerFactory.getLogger(TimetableResolver.class); private static final Logger LOG = LoggerFactory.getLogger(TimetableSnapshot.class);


// Use HashMap not Map so we can clone. // Use HashMap not Map so we can clone.
// if this turns out to be slow/spacious we can use an array with integer pattern indexes // if this turns out to be slow/spacious we can use an array with integer pattern indexes
Expand Down Expand Up @@ -169,7 +169,7 @@ public boolean update(TripPattern pattern, TripTimes updatedTripTimes, ServiceDa
// synchronization prevents commits/snapshots while update is in progress // synchronization prevents commits/snapshots while update is in progress
synchronized(this) { synchronized(this) {
if (dirty == null) if (dirty == null)
throw new ConcurrentModificationException("This TimetableResolver is read-only."); throw new ConcurrentModificationException("This TimetableSnapshot is read-only.");
Timetable tt = resolve(pattern, serviceDate); Timetable tt = resolve(pattern, serviceDate);
// we need to perform the copy of Timetable here rather than in Timetable.update() // we need to perform the copy of Timetable here rather than in Timetable.update()
// to avoid repeatedly copying in case several updates are applied to the same timetable // to avoid repeatedly copying in case several updates are applied to the same timetable
Expand Down Expand Up @@ -221,19 +221,19 @@ public boolean update(TripPattern pattern, TripTimes updatedTripTimes, ServiceDa
* the same timetable in rapid succession. This compromise is expressed by the * the same timetable in rapid succession. This compromise is expressed by the
* maxSnapshotFrequency property of StoptimeUpdater. The indexing could be made much more * maxSnapshotFrequency property of StoptimeUpdater. The indexing could be made much more
* efficient as well. * efficient as well.
* @return an immutable copy of this TimetableResolver with all updates applied * @return an immutable copy of this TimetableSnapshot with all updates applied
*/ */
public TimetableResolver commit() { public TimetableSnapshot commit() {
return commit(false); return commit(false);
} }


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public TimetableResolver commit(boolean force) { public TimetableSnapshot commit(boolean force) {
TimetableResolver ret = new TimetableResolver(); TimetableSnapshot ret = new TimetableSnapshot();
// synchronization prevents updates while commit/snapshot in progress // synchronization prevents updates while commit/snapshot in progress
synchronized(this) { synchronized(this) {
if (dirty == null) { if (dirty == null) {
throw new ConcurrentModificationException("This TimetableResolver is read-only."); throw new ConcurrentModificationException("This TimetableSnapshot is read-only.");
} }
if (!force && !this.isDirty()) return null; if (!force && !this.isDirty()) return null;
for (Timetable tt : dirty) { for (Timetable tt : dirty) {
Expand All @@ -254,7 +254,7 @@ public TimetableResolver commit(boolean force) {
public boolean purgeExpiredData(ServiceDate serviceDate) { public boolean purgeExpiredData(ServiceDate serviceDate) {
synchronized(this) { synchronized(this) {
if (dirty == null) { if (dirty == null) {
throw new ConcurrentModificationException("This TimetableResolver is read-only."); throw new ConcurrentModificationException("This TimetableSnapshot is read-only.");
} }


boolean modified = false; boolean modified = false;
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/opentripplanner/routing/graph/GraphIndex.java
Expand Up @@ -29,7 +29,7 @@
import org.opentripplanner.routing.core.ServiceDay; import org.opentripplanner.routing.core.ServiceDay;
import org.opentripplanner.routing.edgetype.TablePatternEdge; import org.opentripplanner.routing.edgetype.TablePatternEdge;
import org.opentripplanner.routing.edgetype.Timetable; import org.opentripplanner.routing.edgetype.Timetable;
import org.opentripplanner.routing.edgetype.TimetableResolver; import org.opentripplanner.routing.edgetype.TimetableSnapshot;
import org.opentripplanner.routing.edgetype.TripPattern; import org.opentripplanner.routing.edgetype.TripPattern;
import org.opentripplanner.routing.trippattern.FrequencyEntry; import org.opentripplanner.routing.trippattern.FrequencyEntry;
import org.opentripplanner.routing.trippattern.TripTimes; import org.opentripplanner.routing.trippattern.TripTimes;
Expand Down Expand Up @@ -327,9 +327,9 @@ public List<StopTimesInPattern> getStopTimesForStop(Stop stop, int timeRange, in


long now = System.currentTimeMillis()/1000; long now = System.currentTimeMillis()/1000;
List<StopTimesInPattern> ret = new ArrayList<>(); List<StopTimesInPattern> ret = new ArrayList<>();
TimetableResolver timetableResolver = null; TimetableSnapshot snapshot = null;
if (graph.timetableSnapshotSource != null) { if (graph.timetableSnapshotSource != null) {
timetableResolver = graph.timetableSnapshotSource.getTimetableSnapshot(); snapshot = graph.timetableSnapshotSource.getTimetableSnapshot();
} }
ServiceDate[] serviceDates = {new ServiceDate().previous(), new ServiceDate(), new ServiceDate().next()}; ServiceDate[] serviceDates = {new ServiceDate().previous(), new ServiceDate(), new ServiceDate().next()};


Expand All @@ -349,8 +349,8 @@ protected boolean lessThan(TripTimeShort tripTimeShort, TripTimeShort t1) {
for (ServiceDate serviceDate : serviceDates) { for (ServiceDate serviceDate : serviceDates) {
ServiceDay sd = new ServiceDay(graph, serviceDate, calendarService, pattern.route.getAgency().getId()); ServiceDay sd = new ServiceDay(graph, serviceDate, calendarService, pattern.route.getAgency().getId());
Timetable tt; Timetable tt;
if (timetableResolver != null){ if (snapshot != null){
tt = timetableResolver.resolve(pattern, serviceDate); tt = snapshot.resolve(pattern, serviceDate);
} else { } else {
tt = pattern.scheduledTimetable; tt = pattern.scheduledTimetable;
} }
Expand Down Expand Up @@ -409,16 +409,16 @@ protected boolean lessThan(TripTimeShort tripTimeShort, TripTimeShort t1) {
*/ */
public List<StopTimesInPattern> getStopTimesForStop(Stop stop, ServiceDate serviceDate) { public List<StopTimesInPattern> getStopTimesForStop(Stop stop, ServiceDate serviceDate) {
List<StopTimesInPattern> ret = new ArrayList<>(); List<StopTimesInPattern> ret = new ArrayList<>();
TimetableResolver timetableResolver = null; TimetableSnapshot snapshot = null;
if (graph.timetableSnapshotSource != null) { if (graph.timetableSnapshotSource != null) {
timetableResolver = graph.timetableSnapshotSource.getTimetableSnapshot(); snapshot = graph.timetableSnapshotSource.getTimetableSnapshot();
} }
Collection<TripPattern> patterns = patternsForStop.get(stop); Collection<TripPattern> patterns = patternsForStop.get(stop);
for (TripPattern pattern : patterns) { for (TripPattern pattern : patterns) {
StopTimesInPattern stopTimes = new StopTimesInPattern(pattern); StopTimesInPattern stopTimes = new StopTimesInPattern(pattern);
Timetable tt; Timetable tt;
if (timetableResolver != null){ if (snapshot != null){
tt = timetableResolver.resolve(pattern, serviceDate); tt = snapshot.resolve(pattern, serviceDate);
} else { } else {
tt = pattern.scheduledTimetable; tt = pattern.scheduledTimetable;
} }
Expand Down
Expand Up @@ -36,7 +36,7 @@ the License, or (at your option) any later version.
import org.opentripplanner.model.StopPattern; import org.opentripplanner.model.StopPattern;
import org.opentripplanner.routing.edgetype.Timetable; import org.opentripplanner.routing.edgetype.Timetable;
import org.opentripplanner.routing.edgetype.TripPattern; import org.opentripplanner.routing.edgetype.TripPattern;
import org.opentripplanner.routing.edgetype.TimetableResolver; import org.opentripplanner.routing.edgetype.TimetableSnapshot;
import org.opentripplanner.routing.graph.Graph; import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.GraphIndex; import org.opentripplanner.routing.graph.GraphIndex;
import org.opentripplanner.routing.trippattern.TripTimes; import org.opentripplanner.routing.trippattern.TripTimes;
Expand Down Expand Up @@ -81,10 +81,10 @@ public class TimetableSnapshotSource {
* The last committed snapshot that was handed off to a routing thread. This snapshot may be * The last committed snapshot that was handed off to a routing thread. This snapshot may be
* given to more than one routing thread if the maximum snapshot frequency is exceeded. * given to more than one routing thread if the maximum snapshot frequency is exceeded.
*/ */
private TimetableResolver snapshot = null; private TimetableSnapshot snapshot = null;


/** The working copy of the timetable resolver. Should not be visible to routing threads. */ /** The working copy of the timetable resolver. Should not be visible to routing threads. */
private TimetableResolver buffer = new TimetableResolver(); private TimetableSnapshot buffer = new TimetableSnapshot();


/** /**
* A synchronized cache of trip patterns that are added to the graph due to GTFS-realtime messages. * A synchronized cache of trip patterns that are added to the graph due to GTFS-realtime messages.
Expand Down Expand Up @@ -120,11 +120,11 @@ public TimetableSnapshotSource(Graph graph) {
* thread is provided a consistent view of all TripTimes. The routing thread need only * thread is provided a consistent view of all TripTimes. The routing thread need only
* release its reference to the snapshot to release resources. * release its reference to the snapshot to release resources.
*/ */
public TimetableResolver getTimetableSnapshot() { public TimetableSnapshot getTimetableSnapshot() {
return getTimetableSnapshot(false); return getTimetableSnapshot(false);
} }


protected synchronized TimetableResolver getTimetableSnapshot(boolean force) { protected synchronized TimetableSnapshot getTimetableSnapshot(boolean force) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if (force || now - lastSnapshotTime > maxSnapshotFrequency) { if (force || now - lastSnapshotTime > maxSnapshotFrequency) {
if (force || buffer.isDirty()) { if (force || buffer.isDirty()) {
Expand Down
Expand Up @@ -623,13 +623,13 @@ private GraphPath[] buildPaths() {


TripUpdate tripUpdate = tripUpdateBuilder.build(); TripUpdate tripUpdate = tripUpdateBuilder.build();


// Create dummy TimetableResolver // Create dummy TimetableSnapshot
TimetableResolver resolver = new TimetableResolver(); TimetableSnapshot snapshot = new TimetableSnapshot();


// Mock TimetableSnapshotSource to return dummy TimetableResolver // Mock TimetableSnapshotSource to return dummy TimetableSnapshot
TimetableSnapshotSource timetableSnapshotSource = mock(TimetableSnapshotSource.class); TimetableSnapshotSource timetableSnapshotSource = mock(TimetableSnapshotSource.class);


when(timetableSnapshotSource.getTimetableSnapshot()).thenReturn(resolver); when(timetableSnapshotSource.getTimetableSnapshot()).thenReturn(snapshot);


TripTimes updatedTripTimes = thirdTripPattern.scheduledTimetable.createUpdatedTripTimes(tripUpdate, TripTimes updatedTripTimes = thirdTripPattern.scheduledTimetable.createUpdatedTripTimes(tripUpdate,
timeZone, serviceDate); timeZone, serviceDate);
Expand Down
Expand Up @@ -15,7 +15,7 @@ the License, or (at your option) any later version.


import org.onebusaway.gtfs.model.AgencyAndId; import org.onebusaway.gtfs.model.AgencyAndId;
import org.onebusaway.gtfs.model.Stop; import org.onebusaway.gtfs.model.Stop;
import org.opentripplanner.routing.edgetype.TimetableResolver; import org.opentripplanner.routing.edgetype.TimetableSnapshot;
import org.opentripplanner.routing.graph.Graph; import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.Vertex; import org.opentripplanner.routing.graph.Vertex;
import org.opentripplanner.routing.vertextype.TransitStop; import org.opentripplanner.routing.vertextype.TransitStop;
Expand Down Expand Up @@ -49,12 +49,12 @@ public void testIgnoreRealtimeUpdates() throws Exception {
Vertex from = new TransitStop(graph, stop1); Vertex from = new TransitStop(graph, stop1);
Vertex to = new TransitStop(graph, stop2); Vertex to = new TransitStop(graph, stop2);


// Create dummy TimetableResolver // Create dummy TimetableSnapshot
TimetableResolver resolver = new TimetableResolver(); TimetableSnapshot snapshot = new TimetableSnapshot();


// Mock TimetableSnapshotSource to return dummy TimetableResolver // Mock TimetableSnapshotSource to return dummy TimetableSnapshot
TimetableSnapshotSource source = mock(TimetableSnapshotSource.class); TimetableSnapshotSource source = mock(TimetableSnapshotSource.class);
when(source.getTimetableSnapshot()).thenReturn(resolver); when(source.getTimetableSnapshot()).thenReturn(snapshot);
graph.timetableSnapshotSource = (source); graph.timetableSnapshotSource = (source);


// Create routing context // Create routing context
Expand Down
Expand Up @@ -40,7 +40,7 @@ the License, or (at your option) any later version.
import org.opentripplanner.routing.edgetype.SimpleTransfer; import org.opentripplanner.routing.edgetype.SimpleTransfer;
import org.opentripplanner.routing.edgetype.TimedTransferEdge; import org.opentripplanner.routing.edgetype.TimedTransferEdge;
import org.opentripplanner.routing.edgetype.Timetable; import org.opentripplanner.routing.edgetype.Timetable;
import org.opentripplanner.routing.edgetype.TimetableResolver; import org.opentripplanner.routing.edgetype.TimetableSnapshot;
import org.opentripplanner.routing.edgetype.TransitBoardAlight; import org.opentripplanner.routing.edgetype.TransitBoardAlight;
import org.opentripplanner.routing.edgetype.TripPattern; import org.opentripplanner.routing.edgetype.TripPattern;
import org.opentripplanner.routing.edgetype.factory.GTFSPatternHopFactory; import org.opentripplanner.routing.edgetype.factory.GTFSPatternHopFactory;
Expand Down Expand Up @@ -99,13 +99,13 @@ public Context() throws IOException {
// Add simple transfer to make transfer possible between U-V and I-J // Add simple transfer to make transfer possible between U-V and I-J
createSimpleTransfer("agency:V", "agency:I", 100); createSimpleTransfer("agency:V", "agency:I", 100);


// Create dummy TimetableResolver // Create dummy TimetableSnapshot
TimetableResolver resolver = new TimetableResolver(); TimetableSnapshot snapshot = new TimetableSnapshot();


// Mock TimetableSnapshotSource to return dummy TimetableResolver // Mock TimetableSnapshotSource to return dummy TimetableSnapshot
TimetableSnapshotSource timetableSnapshotSource = mock(TimetableSnapshotSource.class); TimetableSnapshotSource timetableSnapshotSource = mock(TimetableSnapshotSource.class);


when(timetableSnapshotSource.getTimetableSnapshot()).thenReturn(resolver); when(timetableSnapshotSource.getTimetableSnapshot()).thenReturn(snapshot);


graph.timetableSnapshotSource = (timetableSnapshotSource); graph.timetableSnapshotSource = (timetableSnapshotSource);
} }
Expand Down Expand Up @@ -185,7 +185,7 @@ private List<Trip> extractTrips(GraphPath path) {
private void applyUpdateToTripPattern(TripPattern pattern, String tripId, String stopId, private void applyUpdateToTripPattern(TripPattern pattern, String tripId, String stopId,
int stopSeq, int arrive, int depart, ScheduleRelationship scheduleRelationship, int stopSeq, int arrive, int depart, ScheduleRelationship scheduleRelationship,
int timestamp, ServiceDate serviceDate) throws ParseException { int timestamp, ServiceDate serviceDate) throws ParseException {
TimetableResolver snapshot = graph.timetableSnapshotSource.getTimetableSnapshot(); TimetableSnapshot snapshot = graph.timetableSnapshotSource.getTimetableSnapshot();
Timetable timetable = snapshot.resolve(pattern, serviceDate); Timetable timetable = snapshot.resolve(pattern, serviceDate);
TimeZone timeZone = new SimpleTimeZone(-7, "PST"); TimeZone timeZone = new SimpleTimeZone(-7, "PST");
long today = serviceDate.getAsDate(timeZone).getTime() / 1000; long today = serviceDate.getAsDate(timeZone).getTime() / 1000;
Expand Down
Expand Up @@ -46,7 +46,7 @@ the License, or (at your option) any later version.
import com.google.transit.realtime.GtfsRealtime.TripUpdate; import com.google.transit.realtime.GtfsRealtime.TripUpdate;
import com.google.transit.realtime.GtfsRealtime.TripDescriptor.ScheduleRelationship; import com.google.transit.realtime.GtfsRealtime.TripDescriptor.ScheduleRelationship;


public class TimetableResolverTest { public class TimetableSnapshotTest {
private static Graph graph; private static Graph graph;
private static GtfsContext context; private static GtfsContext context;
private static Map<AgencyAndId, TripPattern> patternIndex; private static Map<AgencyAndId, TripPattern> patternIndex;
Expand Down Expand Up @@ -79,10 +79,10 @@ public void testCompare() {
Timetable orig = new Timetable(null); Timetable orig = new Timetable(null);
Timetable a = new Timetable(orig, new ServiceDate().previous()); Timetable a = new Timetable(orig, new ServiceDate().previous());
Timetable b = new Timetable(orig, new ServiceDate()); Timetable b = new Timetable(orig, new ServiceDate());
assertEquals(-1, (new TimetableResolver.SortedTimetableComparator()).compare(a, b)); assertEquals(-1, (new TimetableSnapshot.SortedTimetableComparator()).compare(a, b));
} }


private boolean updateResolver(TimetableResolver resolver, TripPattern pattern, TripUpdate tripUpdate, String feedId, ServiceDate serviceDate) { private boolean updateResolver(TimetableSnapshot resolver, TripPattern pattern, TripUpdate tripUpdate, String feedId, ServiceDate serviceDate) {
TripTimes updatedTripTimes = pattern.scheduledTimetable.createUpdatedTripTimes(tripUpdate, TripTimes updatedTripTimes = pattern.scheduledTimetable.createUpdatedTripTimes(tripUpdate,
timeZone, serviceDate); timeZone, serviceDate);
return resolver.update(pattern, updatedTripTimes, serviceDate); return resolver.update(pattern, updatedTripTimes, serviceDate);
Expand All @@ -94,7 +94,7 @@ public void testResolve() {
ServiceDate yesterday = today.previous(); ServiceDate yesterday = today.previous();
ServiceDate tomorrow = today.next(); ServiceDate tomorrow = today.next();
TripPattern pattern = patternIndex.get(new AgencyAndId("agency", "1.1")); TripPattern pattern = patternIndex.get(new AgencyAndId("agency", "1.1"));
TimetableResolver resolver = new TimetableResolver(); TimetableSnapshot resolver = new TimetableSnapshot();


Timetable scheduled = resolver.resolve(pattern, today); Timetable scheduled = resolver.resolve(pattern, today);
assertEquals(scheduled, resolver.resolve(pattern, null)); assertEquals(scheduled, resolver.resolve(pattern, null));
Expand Down Expand Up @@ -133,7 +133,7 @@ public void testUpdate() {
ServiceDate yesterday = today.previous(); ServiceDate yesterday = today.previous();
TripPattern pattern = patternIndex.get(new AgencyAndId("agency", "1.1")); TripPattern pattern = patternIndex.get(new AgencyAndId("agency", "1.1"));


TimetableResolver resolver = new TimetableResolver(); TimetableSnapshot resolver = new TimetableSnapshot();
Timetable origNow = resolver.resolve(pattern, today); Timetable origNow = resolver.resolve(pattern, today);


TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder(); TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
Expand Down Expand Up @@ -162,7 +162,7 @@ public void testUpdate() {
assertNotSame(updatedNow, resolver.resolve(pattern, yesterday)); assertNotSame(updatedNow, resolver.resolve(pattern, yesterday));


// exception if we try to modify a snapshot // exception if we try to modify a snapshot
TimetableResolver snapshot = resolver.commit(); TimetableSnapshot snapshot = resolver.commit();
updateResolver(snapshot, pattern, tripUpdate, "agency", yesterday); updateResolver(snapshot, pattern, tripUpdate, "agency", yesterday);
} }


Expand All @@ -172,10 +172,10 @@ public void testCommit() {
ServiceDate yesterday = today.previous(); ServiceDate yesterday = today.previous();
TripPattern pattern = patternIndex.get(new AgencyAndId("agency", "1.1")); TripPattern pattern = patternIndex.get(new AgencyAndId("agency", "1.1"));


TimetableResolver resolver = new TimetableResolver(); TimetableSnapshot resolver = new TimetableSnapshot();


// only return a new snapshot if there are changes // only return a new snapshot if there are changes
TimetableResolver snapshot = resolver.commit(); TimetableSnapshot snapshot = resolver.commit();
assertNull(snapshot); assertNull(snapshot);


TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder(); TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
Expand Down Expand Up @@ -230,7 +230,7 @@ public void testPurge() {


TripUpdate tripUpdate = tripUpdateBuilder.build(); TripUpdate tripUpdate = tripUpdateBuilder.build();


TimetableResolver resolver = new TimetableResolver(); TimetableSnapshot resolver = new TimetableSnapshot();
updateResolver(resolver, pattern, tripUpdate, "agency", today); updateResolver(resolver, pattern, tripUpdate, "agency", today);
updateResolver(resolver, pattern, tripUpdate, "agency", yesterday); updateResolver(resolver, pattern, tripUpdate, "agency", yesterday);


Expand Down

0 comments on commit e42da19

Please sign in to comment.