@@ -198,7 +198,7 @@ async fn project_path(
198
198
let mut trains_hash_values = vec ! [ ] ;
199
199
let mut trains_details = vec ! [ ] ;
200
200
201
- for ( sim, pathfinding_result) in simulations {
201
+ for ( train , ( sim, pathfinding_result) ) in izip ! ( & trains , simulations) {
202
202
let track_ranges = match pathfinding_result {
203
203
PathfindingResult :: Success ( PathfindingResultSuccess {
204
204
track_section_ranges,
@@ -221,6 +221,7 @@ async fn project_path(
221
221
} = report_train;
222
222
223
223
let train_details = TrainSimulationDetails {
224
+ train_id : train. id ,
224
225
positions,
225
226
times,
226
227
signal_critical_positions,
@@ -242,17 +243,13 @@ async fn project_path(
242
243
let cached_projections: Vec < Option < CachedProjectPathTrainResult > > =
243
244
valkey_conn. json_get_bulk ( & trains_hash_values) . await ?;
244
245
245
- let mut hit_cache: HashMap < i64 , CachedProjectPathTrainResult > = HashMap :: new ( ) ;
246
- let mut miss_cache = HashMap :: new ( ) ;
247
- for ( train_details, projection, train_id) in izip ! (
248
- trains_details,
249
- cached_projections,
250
- trains. iter( ) . map( |t| t. id)
251
- ) {
246
+ let mut hit_cache = vec ! [ ] ;
247
+ let mut miss_cache = vec ! [ ] ;
248
+ for ( train_details, projection) in izip ! ( & trains_details, cached_projections) {
252
249
if let Some ( cached) = projection {
253
- hit_cache. insert ( train_id , cached ) ;
250
+ hit_cache. push ( ( cached , train_details . train_id ) ) ;
254
251
} else {
255
- miss_cache. insert ( train_id , train_details. clone ( ) ) ;
252
+ miss_cache. push ( train_details. clone ( ) ) ;
256
253
}
257
254
}
258
255
@@ -277,47 +274,47 @@ async fn project_path(
277
274
let signal_updates = signal_updates?;
278
275
279
276
// 3. Store the projection in the cache (using pipeline)
280
- let trains_hash_values: HashMap < _ , _ > = trains
277
+ let trains_hash_values: HashMap < _ , _ > = trains_details
281
278
. iter ( )
282
- . map ( |t| t. id )
279
+ . map ( |t| t. train_id )
283
280
. zip ( trains_hash_values)
284
281
. collect ( ) ;
285
282
let mut new_items = vec ! [ ] ;
286
- for id in miss_cache. keys ( ) {
287
- let hash = & trains_hash_values[ id ] ;
283
+ for train_id in miss_cache. iter ( ) . map ( |t| t . train_id ) {
284
+ let hash = & trains_hash_values[ & train_id ] ;
288
285
let cached_value = CachedProjectPathTrainResult {
289
286
space_time_curves : space_time_curves
290
- . get ( id )
287
+ . get ( & train_id )
291
288
. expect ( "Space time curves not available for train" )
292
289
. clone ( ) ,
293
290
signal_updates : signal_updates
294
- . get ( id )
291
+ . get ( & train_id )
295
292
. expect ( "Signal update not available for train" )
296
293
. clone ( ) ,
297
294
} ;
298
- hit_cache. insert ( * id , cached_value. clone ( ) ) ;
295
+ hit_cache. push ( ( cached_value. clone ( ) , train_id ) ) ;
299
296
new_items. push ( ( hash, cached_value) ) ;
300
297
}
301
298
valkey_conn. json_set_bulk ( & new_items) . await ?;
302
299
303
300
let train_map: HashMap < i64 , TrainSchedule > = trains. into_iter ( ) . map ( |ts| ( ts. id , ts) ) . collect ( ) ;
304
301
305
302
// 4.1 Fetch rolling stock length
306
- let mut project_path_result = HashMap :: new ( ) ;
307
303
let rolling_stock_length: HashMap < _ , _ > = rolling_stocks
308
304
. into_iter ( )
309
305
. map ( |rs| ( rs. name , rs. length ) )
310
306
. collect ( ) ;
311
307
312
308
// 4.2 Build the projection response
313
- for ( id, cached) in hit_cache {
314
- let train = train_map. get ( & id) . expect ( "Train not found" ) ;
309
+ let mut project_path_result = HashMap :: new ( ) ;
310
+ for ( cached, train_id) in hit_cache {
311
+ let train = train_map. get ( & train_id) . expect ( "Train not found" ) ;
315
312
let length = rolling_stock_length
316
313
. get ( & train. rolling_stock_name )
317
314
. expect ( "Rolling stock length not found" ) ;
318
315
319
316
project_path_result. insert (
320
- id ,
317
+ train_id ,
321
318
ProjectPathTrainResult {
322
319
departure_time : train. start_time ,
323
320
rolling_stock_length : ( length * 1000. ) . round ( ) as u64 ,
@@ -332,6 +329,7 @@ async fn project_path(
332
329
/// Input for the projection of a train schedule on a path
333
330
#[ derive( Debug , Clone , Hash ) ]
334
331
struct TrainSimulationDetails {
332
+ train_id : i64 ,
335
333
positions : Vec < u64 > ,
336
334
times : Vec < u64 > ,
337
335
train_path : Vec < TrackRange > ,
@@ -346,7 +344,7 @@ async fn compute_batch_signal_updates<'a>(
346
344
path_track_ranges : & ' a Vec < TrackRange > ,
347
345
path_routes : & ' a Vec < Identifier > ,
348
346
path_blocks : & ' a Vec < Identifier > ,
349
- trains_details : & ' a HashMap < i64 , TrainSimulationDetails > ,
347
+ trains_details : & ' a [ TrainSimulationDetails ] ,
350
348
) -> Result < HashMap < i64 , Vec < SignalUpdate > > > {
351
349
if trains_details. is_empty ( ) {
352
350
return Ok ( HashMap :: new ( ) ) ;
@@ -359,13 +357,13 @@ async fn compute_batch_signal_updates<'a>(
359
357
blocks : path_blocks,
360
358
train_simulations : trains_details
361
359
. iter ( )
362
- . map ( |( id , details ) | {
360
+ . map ( |detail | {
363
361
(
364
- * id ,
362
+ detail . train_id ,
365
363
TrainSimulation {
366
- signal_critical_positions : & details . signal_critical_positions ,
367
- zone_updates : & details . zone_updates ,
368
- simulation_end_time : details . times [ details . times . len ( ) - 1 ] ,
364
+ signal_critical_positions : & detail . signal_critical_positions ,
365
+ zone_updates : & detail . zone_updates ,
366
+ simulation_end_time : detail . times [ detail . times . len ( ) - 1 ] ,
369
367
} ,
370
368
)
371
369
} )
@@ -377,14 +375,14 @@ async fn compute_batch_signal_updates<'a>(
377
375
378
376
/// Compute space time curves of a list of train schedules
379
377
async fn compute_batch_space_time_curves < ' a > (
380
- trains_details : & HashMap < i64 , TrainSimulationDetails > ,
378
+ trains_details : & Vec < TrainSimulationDetails > ,
381
379
path_projection : & PathProjection < ' a > ,
382
380
) -> HashMap < i64 , Vec < SpaceTimeCurve > > {
383
381
let mut space_time_curves = HashMap :: new ( ) ;
384
382
385
- for ( train_id , train_detail) in trains_details {
383
+ for train_detail in trains_details {
386
384
space_time_curves. insert (
387
- * train_id,
385
+ train_detail . train_id ,
388
386
compute_space_time_curves ( train_detail, path_projection) ,
389
387
) ;
390
388
}
@@ -584,6 +582,7 @@ mod tests {
584
582
] ;
585
583
586
584
let project_path_input = TrainSimulationDetails {
585
+ train_id : 0 ,
587
586
positions,
588
587
times,
589
588
train_path,
@@ -618,6 +617,7 @@ mod tests {
618
617
] ;
619
618
620
619
let project_path_input = TrainSimulationDetails {
620
+ train_id : 0 ,
621
621
positions : positions. clone ( ) ,
622
622
times : times. clone ( ) ,
623
623
train_path,
@@ -655,6 +655,7 @@ mod tests {
655
655
let path_projection = PathProjection :: new ( & path) ;
656
656
657
657
let project_path_input = TrainSimulationDetails {
658
+ train_id : 0 ,
658
659
positions,
659
660
times,
660
661
train_path,
0 commit comments