-
Notifications
You must be signed in to change notification settings - Fork 1k
/
TimetableSnapshot.java
316 lines (274 loc) · 12.3 KB
/
TimetableSnapshot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* This program is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package org.opentripplanner.routing.edgetype;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.onebusaway.gtfs.model.calendar.ServiceDate;
import org.opentripplanner.routing.trippattern.TripTimes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
// this is only currently in edgetype because that's where Trippattern is.
// move these classes elsewhere.
/**
* Part of concurrency control for stoptime updates.
*
* All updates should be performed on a snapshot before it is handed off to any searches.
* A single snapshot should be used for an entire search, and should remain unchanged
* for that duration to provide a consistent view not only of trips that have been boarded, but of
* relative arrival and departure times of other trips that have not necessarily been boarded.
*
* At this point, only one writing thread at a time is supported.
*/
public class TimetableSnapshot {
protected static class SortedTimetableComparator implements Comparator<Timetable> {
@Override
public int compare(Timetable t1, Timetable t2) {
return t1.serviceDate.compareTo(t2.serviceDate);
}
}
/**
* Class to use as key in HashMap containing trip id and service date
*/
protected class TripIdAndServiceDate {
private final String tripId;
private final ServiceDate serviceDate;
public TripIdAndServiceDate(final String tripId, final ServiceDate serviceDate) {
this.tripId = tripId;
this.serviceDate = serviceDate;
}
public String getTripId() {
return tripId;
}
public ServiceDate getServiceDate() {
return serviceDate;
}
@Override
public int hashCode() {
int result = Objects.hash(tripId, serviceDate);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TripIdAndServiceDate other = (TripIdAndServiceDate) obj;
boolean result = Objects.equals(this.tripId, other.tripId) &&
Objects.equals(this.serviceDate, other.serviceDate);
return result;
}
}
private static final Logger LOG = LoggerFactory.getLogger(TimetableSnapshot.class);
// 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
// The SortedSet members are copy-on-write
// FIXME: this could be made into a flat hashtable with compound keys.
private HashMap<TripPattern, SortedSet<Timetable>> timetables =
new HashMap<TripPattern, SortedSet<Timetable>>();
/**
* <p>
* Map containing the last <b>added</b> trip pattern given a trip id (without agency) and a
* service date as a result of a call to {@link #update(TripPattern, TripTimes, ServiceDate)}
* with trip times of a trip that didn't exist yet in the trip pattern.
* </p>
* <p>
* This is a HashMap and not a Map so the clone function is available.
* </p>
*/
private HashMap<TripIdAndServiceDate, TripPattern> lastAddedTripPattern = new HashMap<>();
/**
* A set of all timetables which have been modified and are waiting to be indexed. When
* <code>dirty</code> is <code>null</code>, it indicates that the snapshot is read-only.
*/
private Set<Timetable> dirty = new HashSet<Timetable>();
/**
* Returns an updated timetable for the specified pattern if one is available in this snapshot,
* or the originally scheduled timetable if there are no updates in this snapshot.
*/
public Timetable resolve(TripPattern pattern, ServiceDate serviceDate) {
SortedSet<Timetable> sortedTimetables = timetables.get(pattern);
if(sortedTimetables != null && serviceDate != null) {
for(Timetable timetable : sortedTimetables) {
if (timetable != null && timetable.isValidFor(serviceDate)) {
LOG.trace("returning modified timetable");
return timetable;
}
}
}
return pattern.scheduledTimetable;
}
/**
* Get the last <b>added</b> trip pattern given a trip id (without agency) and a service date as
* a result of a call to {@link #update(TripPattern, TripTimes, ServiceDate)} with trip times of
* a trip that didn't exist yet in the trip pattern.
*
* @param tripId trip id (without agency)
* @param serviceDate service date
* @return last added trip pattern; null if trip never was added to a trip pattern
*/
public TripPattern getLastAddedTripPattern(String tripId, ServiceDate serviceDate) {
TripIdAndServiceDate tripIdAndServiceDate = new TripIdAndServiceDate(tripId, serviceDate);
TripPattern pattern = lastAddedTripPattern.get(tripIdAndServiceDate);
return pattern;
}
/**
* Update the trip times of one trip in a timetable of a trip pattern. If the trip of the trip
* times does not exist yet in the timetable, add it.
*
* @param pattern trip pattern
* @param updatedTripTimes updated trip times
* @param serviceDate service day for which this update is valid
* @return whether or not the update was actually applied
*/
public boolean update(TripPattern pattern, TripTimes updatedTripTimes, ServiceDate serviceDate) {
// Preconditions
Preconditions.checkNotNull(pattern);
Preconditions.checkNotNull(serviceDate);
if (dirty == null) {
throw new ConcurrentModificationException("This TimetableSnapshot is read-only.");
}
Timetable tt = resolve(pattern, serviceDate);
// 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
if ( ! dirty.contains(tt)) {
Timetable old = tt;
tt = new Timetable(tt, serviceDate);
SortedSet<Timetable> sortedTimetables = timetables.get(pattern);
if(sortedTimetables == null) {
sortedTimetables = new TreeSet<Timetable>(new SortedTimetableComparator());
} else {
SortedSet<Timetable> temp =
new TreeSet<Timetable>(new SortedTimetableComparator());
temp.addAll(sortedTimetables);
sortedTimetables = temp;
}
if(old.serviceDate != null)
sortedTimetables.remove(old);
sortedTimetables.add(tt);
timetables.put(pattern, sortedTimetables);
dirty.add(tt);
}
// Assume all trips in a pattern are from the same feed, which should be the case.
// Find trip index
int tripIndex = tt.getTripIndex(updatedTripTimes.trip.getId());
if (tripIndex == -1) {
// Trip not found, add it
tt.addTripTimes(updatedTripTimes);
// Remember this pattern for the added trip id and service date
String tripId = updatedTripTimes.trip.getId().getId();
TripIdAndServiceDate tripIdAndServiceDate = new TripIdAndServiceDate(tripId, serviceDate);
lastAddedTripPattern.put(tripIdAndServiceDate, pattern);
} else {
// Set updated trip times of trip
tt.setTripTimes(tripIndex, updatedTripTimes);
}
// The time tables are finished during the commit
return true;
}
/**
* This produces a small delay of typically around 50ms, which is almost entirely due to
* the indexing step. Cloning the map is much faster (2ms).
* It is perhaps better to index timetables as they are changed to avoid experiencing all
* this lag at once, but we want to avoid re-indexing when receiving multiple updates for
* the same timetable in rapid succession. This compromise is expressed by the
* maxSnapshotFrequency property of StoptimeUpdater. The indexing could be made much more
* efficient as well.
* @return an immutable copy of this TimetableSnapshot with all updates applied
*/
public TimetableSnapshot commit() {
return commit(false);
}
@SuppressWarnings("unchecked")
public TimetableSnapshot commit(boolean force) {
if (dirty == null) {
throw new ConcurrentModificationException("This TimetableSnapshot is read-only.");
}
TimetableSnapshot ret = new TimetableSnapshot();
if (!force && !this.isDirty()) return null;
for (Timetable tt : dirty) {
tt.finish(); // summarize, index, etc. the new timetables
}
ret.timetables = (HashMap<TripPattern, SortedSet<Timetable>>) this.timetables.clone();
ret.lastAddedTripPattern = (HashMap<TripIdAndServiceDate, TripPattern>)
this.lastAddedTripPattern.clone();
this.dirty.clear();
ret.dirty = null; // mark the snapshot as henceforth immutable
return ret;
}
/**
* Clear all data from snapshot
*/
public void clear() {
if (dirty == null) {
throw new ConcurrentModificationException("This TimetableSnapshot is read-only.");
}
// Clear all data from snapshot
timetables.clear();
lastAddedTripPattern.clear();
}
/**
* Removes all Timetables which are valid for a ServiceDate on-or-before the one supplied.
*/
public boolean purgeExpiredData(ServiceDate serviceDate) {
if (dirty == null) {
throw new ConcurrentModificationException("This TimetableSnapshot is read-only.");
}
boolean modified = false;
for (Iterator<TripPattern> it = timetables.keySet().iterator(); it.hasNext();){
TripPattern pattern = it.next();
SortedSet<Timetable> sortedTimetables = timetables.get(pattern);
SortedSet<Timetable> toKeepTimetables =
new TreeSet<Timetable>(new SortedTimetableComparator());
for(Timetable timetable : sortedTimetables) {
if(serviceDate.compareTo(timetable.serviceDate) < 0) {
toKeepTimetables.add(timetable);
} else {
modified = true;
}
}
if(toKeepTimetables.isEmpty()) {
it.remove();
} else {
timetables.put(pattern, toKeepTimetables);
}
}
// Also remove last added trip pattern for days that are purged
for (Iterator<Entry<TripIdAndServiceDate, TripPattern>> iterator = lastAddedTripPattern
.entrySet().iterator(); iterator.hasNext();) {
TripIdAndServiceDate tripIdAndServiceDate = iterator.next().getKey();
if (serviceDate.compareTo(tripIdAndServiceDate.getServiceDate()) >= 0) {
iterator.remove();
modified = true;
}
}
return modified;
}
public boolean isDirty() {
if (dirty == null) return false;
return dirty.size() > 0;
}
public String toString() {
String d = dirty == null ? "committed" : String.format("%d dirty", dirty.size());
return String.format("Timetable snapshot: %d timetables (%s)", timetables.size(), d);
}
}