Skip to content

Commit 18fe593

Browse files
committed
fix(snapshot): fix duplicated schedule exceptions issue with multimap
fixes #161
1 parent 065c0a0 commit 18fe593

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

src/main/java/com/conveyal/gtfs/loader/JdbcGtfsSnapshotter.java

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.conveyal.gtfs.model.Calendar;
44
import com.conveyal.gtfs.model.CalendarDate;
5+
import com.google.common.collect.HashMultimap;
6+
import com.google.common.collect.Multimap;
7+
import com.google.common.collect.Sets;
58
import org.apache.commons.dbutils.DbUtils;
69
import org.slf4j.Logger;
710
import org.slf4j.LoggerFactory;
@@ -13,10 +16,8 @@
1316
import java.sql.SQLException;
1417
import java.sql.Statement;
1518
import java.time.format.DateTimeFormatter;
16-
import java.util.ArrayList;
1719
import java.util.Arrays;
1820
import java.util.HashMap;
19-
import java.util.List;
2021
import java.util.Map;
2122

2223
import static com.conveyal.gtfs.util.Util.randomIdString;
@@ -209,21 +210,16 @@ private TableLoadResult createScheduleExceptionsTable() {
209210
);
210211
Iterable<CalendarDate> calendarDates = calendarDatesReader.getAll();
211212

212-
// keep track of calendars by service id in case we need to add dummy calendar entries
213+
// Keep track of calendars by service id in case we need to add dummy calendar entries.
213214
Map<String, Calendar> calendarsByServiceId = new HashMap<>();
214215

215-
// iterate through calendar dates to build up to get list of dates with exceptions
216-
HashMap<String, List<String>> removedServiceDays = new HashMap<>();
217-
HashMap<String, List<String>> addedServiceDays = new HashMap<>();
218-
List<String> datesWithExceptions = new ArrayList<>();
216+
// Iterate through calendar dates to build up to get maps from exceptions to their dates.
217+
Multimap<String, String> removedServiceForDate = HashMultimap.create();
218+
Multimap<String, String> addedServiceForDate = HashMultimap.create();
219219
for (CalendarDate calendarDate : calendarDates) {
220220
String date = calendarDate.date.format(DateTimeFormatter.BASIC_ISO_DATE);
221-
datesWithExceptions.add(date);
222221
if (calendarDate.exception_type == 1) {
223-
List<String> dateAddedServices = addedServiceDays.getOrDefault(date, new ArrayList<>());
224-
dateAddedServices.add(calendarDate.service_id);
225-
addedServiceDays.put(date, dateAddedServices);
226-
222+
addedServiceForDate.put(date, calendarDate.service_id);
227223
// create (if needed) and extend range of dummy calendar that would need to be created if we are
228224
// copying from a feed that doesn't have the calendar.txt file
229225
Calendar calendar = calendarsByServiceId.getOrDefault(calendarDate.service_id, new Calendar());
@@ -236,33 +232,24 @@ private TableLoadResult createScheduleExceptionsTable() {
236232
}
237233
calendarsByServiceId.put(calendarDate.service_id, calendar);
238234
} else {
239-
List<String> dateRemovedServices = removedServiceDays.getOrDefault(date, new ArrayList<>());
240-
dateRemovedServices.add(calendarDate.service_id);
241-
removedServiceDays.put(date, dateRemovedServices);
235+
removedServiceForDate.put(date, calendarDate.service_id);
242236
}
243237
}
244-
245-
// iterate through days and add to database
246-
// for usability and simplicity of code, don't attempt to find all dates with similar
247-
// added and removed services, but simply create an entry for each found date
248-
for (String dateWithException : datesWithExceptions) {
249-
scheduleExceptionsStatement.setString(1, dateWithException);
250-
String[] dates = {dateWithException};
238+
// Iterate through dates with added or removed service and add to database.
239+
// For usability and simplicity of code, don't attempt to find all dates with similar
240+
// added and removed services, but simply create an entry for each found date.
241+
for (String date : Sets.union(removedServiceForDate.keySet(), addedServiceForDate.keySet())) {
242+
scheduleExceptionsStatement.setString(1, date);
243+
String[] dates = {date};
251244
scheduleExceptionsStatement.setArray(2, connection.createArrayOf("text", dates));
252245
scheduleExceptionsStatement.setInt(3, 9); // FIXME use better static type
253246
scheduleExceptionsStatement.setArray(
254247
4,
255-
connection.createArrayOf(
256-
"text",
257-
addedServiceDays.getOrDefault(dateWithException, new ArrayList<>()).toArray()
258-
)
248+
connection.createArrayOf("text", addedServiceForDate.get(date).toArray())
259249
);
260250
scheduleExceptionsStatement.setArray(
261251
5,
262-
connection.createArrayOf(
263-
"text",
264-
removedServiceDays.getOrDefault(dateWithException, new ArrayList<>()).toArray()
265-
)
252+
connection.createArrayOf("text", removedServiceForDate.get(date).toArray())
266253
);
267254
scheduleExceptionsTracker.addBatch();
268255
}

0 commit comments

Comments
 (0)