-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Multiple reference tables lookup): Feed merge updated to han…
…dle multi ref tables
- Loading branch information
Robin Beer
authored and
Robin Beer
committed
Jul 7, 2023
1 parent
38fa3a2
commit 988fd51
Showing
4 changed files
with
105 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/com/conveyal/datatools/manager/jobs/feedmerge/ReferenceTableDiscovery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.conveyal.datatools.manager.jobs.feedmerge; | ||
|
||
import com.conveyal.gtfs.loader.Field; | ||
import com.conveyal.gtfs.loader.Table; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
import static com.conveyal.datatools.manager.jobs.feedmerge.MergeLineContext.SERVICE_ID; | ||
|
||
public class ReferenceTableDiscovery { | ||
|
||
public static final String REF_TABLE_SEPARATOR = "#~#"; | ||
|
||
public enum ReferenceTableKey { | ||
|
||
TRIP_SERVICE_ID_KEY( | ||
String.join(REF_TABLE_SEPARATOR, Table.TRIPS.name, SERVICE_ID, Table.CALENDAR.name, Table.CALENDAR_DATES.name) | ||
); | ||
|
||
private final String value; | ||
|
||
ReferenceTableKey(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public static ReferenceTableKey fromValue(String key) { | ||
for (ReferenceTableKey ref: ReferenceTableKey.values()) { | ||
if (ref.getValue().equals(key)) { | ||
return ref; | ||
} | ||
} | ||
throw new UnsupportedOperationException(String.format("Unsupported reference table key: %s.", key)); | ||
} | ||
} | ||
|
||
/** | ||
* Get reference table key by matching the provided values to predefined reference table keys. | ||
*/ | ||
public static ReferenceTableKey getReferenceTableKey(Field field, Table table) { | ||
return ReferenceTableKey.fromValue(createKey(field, table)); | ||
} | ||
|
||
/** | ||
* Create a unique key for this table, field and reference tables. | ||
*/ | ||
public static String createKey(Field field, Table table) { | ||
return String.format( | ||
"%s%s%s%s%s", | ||
table.name, | ||
REF_TABLE_SEPARATOR, | ||
field.name, | ||
REF_TABLE_SEPARATOR, | ||
field.referenceTables.stream().map(r -> r.name).collect(Collectors.joining(REF_TABLE_SEPARATOR)) | ||
); | ||
} | ||
|
||
/** | ||
* Define the reference table for a trip service id. | ||
*/ | ||
public static Table getTripServiceIdReferenceTable( | ||
FieldContext fieldContext, | ||
MergeFeedsResult mergeFeedsResult, | ||
String calendarKey, | ||
String calendarDatesKey | ||
) { | ||
if ( | ||
mergeFeedsResult.calendarServiceIds.contains(fieldContext.getValueToWrite()) || | ||
mergeFeedsResult.skippedIds.contains(calendarKey) | ||
) { | ||
return Table.CALENDAR; | ||
} else if ( | ||
mergeFeedsResult.calendarDatesServiceIds.contains(fieldContext.getValueToWrite()) || | ||
mergeFeedsResult.skippedIds.contains(calendarDatesKey) | ||
) { | ||
return Table.CALENDAR_DATES; | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters