Skip to content

Commit f9399cf

Browse files
committed
fix(snapshots): add normalizeStopTimes flag to preserve stop_sequence vals
re #283
1 parent 686ccae commit f9399cf

File tree

6 files changed

+225
-152
lines changed

6 files changed

+225
-152
lines changed

src/main/java/com/conveyal/gtfs/GTFS.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,24 @@ public static FeedLoadResult load (String filePath, DataSource dataSource) {
8181
* 1. The tables' id column has been modified to be auto-incrementing.
8282
* 2. Primary keys may be added to certain columns/tables.
8383
* 3. Additional editor-specific columns are added to certain tables.
84-
* @param feedId feed ID (schema namespace) to copy from
85-
* @param dataSource JDBC connection to existing database
86-
* @return FIXME should this be a separate SnapshotResult object?
84+
* @param feedId feed ID (schema namespace) to copy from
85+
* @param dataSource JDBC connection to existing database
86+
* @param normalizeStopTimes whether to normalize stop sequence values on snapshot
87+
* @return the result of the snapshot
8788
*/
88-
public static SnapshotResult makeSnapshot (String feedId, DataSource dataSource) {
89-
JdbcGtfsSnapshotter snapshotter = new JdbcGtfsSnapshotter(feedId, dataSource);
89+
public static SnapshotResult makeSnapshot (String feedId, DataSource dataSource, boolean normalizeStopTimes) {
90+
JdbcGtfsSnapshotter snapshotter = new JdbcGtfsSnapshotter(feedId, dataSource, normalizeStopTimes);
9091
SnapshotResult result = snapshotter.copyTables();
9192
return result;
9293
}
9394

95+
/**
96+
* Overloaded makeSnapshot method that defaults to normalize stop times.
97+
*/
98+
public static SnapshotResult makeSnapshot (String feedId, DataSource dataSource) {
99+
return makeSnapshot(feedId, dataSource, true);
100+
}
101+
94102
/**
95103
* Once a feed has been loaded into the database, examine its contents looking for various problems and errors.
96104
*/
@@ -277,7 +285,7 @@ public static void main (String[] args) throws IOException {
277285
}
278286
if (namespaceToSnapshot != null) {
279287
LOG.info("Snapshotting feed with unique identifier {}", namespaceToSnapshot);
280-
FeedLoadResult snapshotResult = makeSnapshot(namespaceToSnapshot, dataSource);
288+
FeedLoadResult snapshotResult = makeSnapshot(namespaceToSnapshot, dataSource, false);
281289
if (storeResults) {
282290
File snapshotResultFile = new File(directory, String.format("%s-snapshot.json", snapshotResult.uniqueIdentifier));
283291
LOG.info("Storing validation result at {}", snapshotResultFile.getAbsolutePath());

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public class JdbcGtfsSnapshotter {
3939
private static final Logger LOG = LoggerFactory.getLogger(JdbcGtfsSnapshotter.class);
4040

4141
private final DataSource dataSource;
42+
/**
43+
* Whether to normalize stop_times#stop_sequence values on snapshot (or leave them intact).
44+
*
45+
* TODO: if more options are added in the future, this should be folded into a SnapshotOptions
46+
* object.
47+
*/
48+
private final boolean normalizeStopTimes;
4249

4350
// These fields will be filled in once feed snapshot begins.
4451
private Connection connection;
@@ -49,10 +56,13 @@ public class JdbcGtfsSnapshotter {
4956
/**
5057
* @param feedId namespace (schema) to snapshot. If null, a blank snapshot will be created.
5158
* @param dataSource the JDBC data source with database connection details
59+
* @param normalizeStopTimes whether to keep stop sequence values intact or normalize to be zero-based and
60+
* incrementing
5261
*/
53-
public JdbcGtfsSnapshotter(String feedId, DataSource dataSource) {
62+
public JdbcGtfsSnapshotter(String feedId, DataSource dataSource, boolean normalizeStopTimes) {
5463
this.feedIdToSnapshot = feedId;
5564
this.dataSource = dataSource;
65+
this.normalizeStopTimes = normalizeStopTimes;
5666
}
5767

5868
/**
@@ -135,7 +145,7 @@ private TableLoadResult copy (Table table, boolean createIndexes) {
135145
// Otherwise, use the createTableFrom method to copy the data from the original.
136146
String fromTableName = String.format("%s.%s", feedIdToSnapshot, table.name);
137147
LOG.info("Copying table {} to {}", fromTableName, targetTable.name);
138-
success = targetTable.createSqlTableFrom(connection, fromTableName);
148+
success = targetTable.createSqlTableFrom(connection, fromTableName, normalizeStopTimes);
139149
}
140150
// Only create indexes if table creation was successful.
141151
if (success && createIndexes) {

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -834,16 +834,19 @@ public void createIndexes(Connection connection, String namespace) throws SQLExc
834834
/**
835835
* Creates a SQL table from the table to clone. This uses the SQL syntax "create table x as y" not only copies the
836836
* table structure, but also the data from the original table. Creating table indexes is not handled by this method.
837+
*
838+
* Note: the stop_times table is a special case that will optionally normalize the stop_sequence values to be
839+
* zero-based and incrementing.
837840
*/
838-
public boolean createSqlTableFrom(Connection connection, String tableToClone) {
841+
public boolean createSqlTableFrom(Connection connection, String tableToClone, boolean normalizeStopTimes) {
839842
long startTime = System.currentTimeMillis();
840843
try {
841844
Statement statement = connection.createStatement();
842845
// Drop target table to avoid a conflict.
843846
String dropSql = String.format("drop table if exists %s", name);
844847
LOG.info(dropSql);
845848
statement.execute(dropSql);
846-
if (tableToClone.endsWith("stop_times")) {
849+
if (tableToClone.endsWith("stop_times") && normalizeStopTimes) {
847850
normalizeAndCloneStopTimes(statement, name, tableToClone);
848851
} else {
849852
// Adding the unlogged keyword gives about 12 percent speedup on loading, but is non-standard.

0 commit comments

Comments
 (0)