fix: stopCode lookup added#93
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an additional lookup key for stops so callers can retrieve stops by stop.code as well as by the stop id suffix parsed from stop.id.
Changes:
- Store each stop in
stops_by_gtfsunderstop_code(fromstop.id) and, when different/non-empty, also understop.code. - Clone
stop_resto support inserting the same stop under multiple keys.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| stop_data.stops.insert(stop_code.to_string(), stop_res.clone()); | ||
| if stop.code != stop_code && !stop.code.is_empty() { | ||
| stop_data.stops.insert(stop.code.clone(), stop_res); |
There was a problem hiding this comment.
stop_res is cloned unconditionally to insert under stop_code, even when the alternate stop.code key is not inserted. This adds extra allocations per stop (potentially significant for large feeds). Consider inserting stop_res by move for the common case and only cloning when the alternate key is actually needed (e.g., branch on the condition and choose which insert gets the moved value).
| stop_data.stops.insert(stop_code.to_string(), stop_res.clone()); | |
| if stop.code != stop_code && !stop.code.is_empty() { | |
| stop_data.stops.insert(stop.code.clone(), stop_res); | |
| if stop.code != stop_code && !stop.code.is_empty() { | |
| stop_data | |
| .stops | |
| .insert(stop_code.to_string(), stop_res.clone()); | |
| stop_data.stops.insert(stop.code.clone(), stop_res); | |
| } else { | |
| stop_data.stops.insert(stop_code.to_string(), stop_res); |
| stop_data.stops.insert(stop_code.to_string(), stop_res); | ||
| stop_data.stops.insert(stop_code.to_string(), stop_res.clone()); | ||
| if stop.code != stop_code && !stop.code.is_empty() { | ||
| stop_data.stops.insert(stop.code.clone(), stop_res); |
There was a problem hiding this comment.
The new stop.code alias insertion uses HashMap::insert, which will silently overwrite an existing stop if another record already used the same stop.code key. Since GTFS stop_code is not guaranteed unique, this can lead to incorrect stop lookups depending on input ordering. Consider guarding against collisions (e.g., only insert if the key is vacant, or detect differing ids and log/skip/fail).
| stop_data.stops.insert(stop.code.clone(), stop_res); | |
| match stop_data.stops.entry(stop.code.clone()) { | |
| std::collections::hash_map::Entry::Vacant(entry) => { | |
| entry.insert(stop_res); | |
| } | |
| std::collections::hash_map::Entry::Occupied(entry) => { | |
| if entry.get().id != stop_res.id { | |
| // Preserve the first alias mapping and skip conflicting stop.code collisions. | |
| } | |
| } | |
| } |
No description provided.