@@ -208,17 +208,17 @@ impl CopyState {
208
208
} )
209
209
} )
210
210
. collect :: < Result < _ , _ > > ( ) ?;
211
- tables. sort_by_key ( |table| table. batch . dst . object . to_string ( ) ) ;
211
+ tables. sort_by_key ( |table| table. dst . object . to_string ( ) ) ;
212
212
213
213
let values = tables
214
214
. iter ( )
215
215
. map ( |table| {
216
216
(
217
- cts:: entity_type. eq ( table. batch . dst . object . as_str ( ) ) ,
217
+ cts:: entity_type. eq ( table. dst . object . as_str ( ) ) ,
218
218
cts:: dst. eq ( dst. site . id ) ,
219
- cts:: next_vid. eq ( table. batch . next_vid ( ) ) ,
220
- cts:: target_vid. eq ( table. batch . target_vid ( ) ) ,
221
- cts:: batch_size. eq ( table. batch . batch_size ( ) ) ,
219
+ cts:: next_vid. eq ( table. batcher . next_vid ( ) ) ,
220
+ cts:: target_vid. eq ( table. batcher . target_vid ( ) ) ,
221
+ cts:: batch_size. eq ( table. batcher . batch_size ( ) as i64 ) ,
222
222
)
223
223
} )
224
224
. collect :: < Vec < _ > > ( ) ;
@@ -294,51 +294,11 @@ pub(crate) fn source(
294
294
/// so that we can copy rows from one to the other with very little
295
295
/// transformation. See `CopyEntityBatchQuery` for the details of what
296
296
/// exactly that means
297
- pub ( crate ) struct BatchCopy {
297
+ struct TableState {
298
298
src : Arc < Table > ,
299
299
dst : Arc < Table > ,
300
- batcher : VidBatcher ,
301
- }
302
-
303
- impl BatchCopy {
304
- pub fn new ( batcher : VidBatcher , src : Arc < Table > , dst : Arc < Table > ) -> Self {
305
- Self { src, dst, batcher }
306
- }
307
-
308
- /// Copy one batch of entities and update internal state so that the
309
- /// next call to `run` will copy the next batch
310
- pub fn run ( & mut self , conn : & mut PgConnection ) -> Result < Duration , StoreError > {
311
- let ( duration, _) = self . batcher . step ( |start, end| {
312
- rq:: CopyEntityBatchQuery :: new ( self . dst . as_ref ( ) , & self . src , start, end) ?
313
- . execute ( conn) ?;
314
- Ok ( ( ) )
315
- } ) ?;
316
-
317
- Ok ( duration)
318
- }
319
-
320
- pub fn finished ( & self ) -> bool {
321
- self . batcher . finished ( )
322
- }
323
-
324
- /// The first `vid` that has not been copied yet
325
- pub fn next_vid ( & self ) -> i64 {
326
- self . batcher . next_vid ( )
327
- }
328
-
329
- /// The last `vid` that should be copied
330
- pub fn target_vid ( & self ) -> i64 {
331
- self . batcher . target_vid ( )
332
- }
333
-
334
- pub fn batch_size ( & self ) -> i64 {
335
- self . batcher . batch_size ( ) as i64
336
- }
337
- }
338
-
339
- struct TableState {
340
- batch : BatchCopy ,
341
300
dst_site : Arc < Site > ,
301
+ batcher : VidBatcher ,
342
302
duration_ms : i64 ,
343
303
}
344
304
@@ -354,14 +314,16 @@ impl TableState {
354
314
let vid_range = VidRange :: for_copy ( conn, & src, target_block) ?;
355
315
let batcher = VidBatcher :: load ( conn, & src_layout. site . namespace , src. as_ref ( ) , vid_range) ?;
356
316
Ok ( Self {
357
- batch : BatchCopy :: new ( batcher, src, dst) ,
317
+ src,
318
+ dst,
358
319
dst_site,
320
+ batcher,
359
321
duration_ms : 0 ,
360
322
} )
361
323
}
362
324
363
325
fn finished ( & self ) -> bool {
364
- self . batch . finished ( )
326
+ self . batcher . finished ( )
365
327
}
366
328
367
329
fn load (
@@ -427,11 +389,12 @@ impl TableState {
427
389
VidRange :: new ( current_vid, target_vid) ,
428
390
) ?
429
391
. with_batch_size ( size as usize ) ;
430
- let batch = BatchCopy :: new ( batcher, src, dst) ;
431
392
432
393
Ok ( TableState {
433
- batch,
394
+ src,
395
+ dst,
434
396
dst_site : dst_layout. site . clone ( ) ,
397
+ batcher,
435
398
duration_ms,
436
399
} )
437
400
}
@@ -460,20 +423,20 @@ impl TableState {
460
423
update (
461
424
cts:: table
462
425
. filter ( cts:: dst. eq ( self . dst_site . id ) )
463
- . filter ( cts:: entity_type. eq ( self . batch . dst . object . as_str ( ) ) )
426
+ . filter ( cts:: entity_type. eq ( self . dst . object . as_str ( ) ) )
464
427
. filter ( cts:: duration_ms. eq ( 0 ) ) ,
465
428
)
466
429
. set ( cts:: started_at. eq ( sql ( "now()" ) ) )
467
430
. execute ( conn) ?;
468
431
let values = (
469
- cts:: next_vid. eq ( self . batch . next_vid ( ) ) ,
470
- cts:: batch_size. eq ( self . batch . batch_size ( ) ) ,
432
+ cts:: next_vid. eq ( self . batcher . next_vid ( ) ) ,
433
+ cts:: batch_size. eq ( self . batcher . batch_size ( ) as i64 ) ,
471
434
cts:: duration_ms. eq ( self . duration_ms ) ,
472
435
) ;
473
436
update (
474
437
cts:: table
475
438
. filter ( cts:: dst. eq ( self . dst_site . id ) )
476
- . filter ( cts:: entity_type. eq ( self . batch . dst . object . as_str ( ) ) ) ,
439
+ . filter ( cts:: entity_type. eq ( self . dst . object . as_str ( ) ) ) ,
477
440
)
478
441
. set ( values)
479
442
. execute ( conn) ?;
@@ -486,7 +449,7 @@ impl TableState {
486
449
update (
487
450
cts:: table
488
451
. filter ( cts:: dst. eq ( self . dst_site . id ) )
489
- . filter ( cts:: entity_type. eq ( self . batch . dst . object . as_str ( ) ) ) ,
452
+ . filter ( cts:: entity_type. eq ( self . dst . object . as_str ( ) ) ) ,
490
453
)
491
454
. set ( cts:: finished_at. eq ( sql ( "now()" ) ) )
492
455
. execute ( conn) ?;
@@ -512,7 +475,11 @@ impl TableState {
512
475
}
513
476
514
477
fn copy_batch ( & mut self , conn : & mut PgConnection ) -> Result < Status , StoreError > {
515
- let duration = self . batch . run ( conn) ?;
478
+ let ( duration, _) = self . batcher . step ( |start, end| {
479
+ rq:: CopyEntityBatchQuery :: new ( self . dst . as_ref ( ) , & self . src , start, end) ?
480
+ . execute ( conn) ?;
481
+ Ok ( ( ) )
482
+ } ) ?;
516
483
517
484
self . record_progress ( conn, duration) ?;
518
485
@@ -539,12 +506,12 @@ impl<'a> CopyProgress<'a> {
539
506
let target_vid: i64 = state
540
507
. tables
541
508
. iter ( )
542
- . map ( |table| table. batch . target_vid ( ) )
509
+ . map ( |table| table. batcher . target_vid ( ) )
543
510
. sum ( ) ;
544
511
let current_vid = state
545
512
. tables
546
513
. iter ( )
547
- . map ( |table| table. batch . next_vid ( ) )
514
+ . map ( |table| table. batcher . next_vid ( ) )
548
515
. sum ( ) ;
549
516
Self {
550
517
logger,
@@ -577,23 +544,23 @@ impl<'a> CopyProgress<'a> {
577
544
}
578
545
}
579
546
580
- fn update ( & mut self , batch : & BatchCopy ) {
547
+ fn update ( & mut self , entity_type : & EntityType , batcher : & VidBatcher ) {
581
548
if self . last_log . elapsed ( ) > LOG_INTERVAL {
582
549
info ! (
583
550
self . logger,
584
551
"Copied {:.2}% of `{}` entities ({}/{} entity versions), {:.2}% of overall data" ,
585
- Self :: progress_pct( batch . next_vid( ) , batch . target_vid( ) ) ,
586
- batch . dst . object ,
587
- batch . next_vid( ) ,
588
- batch . target_vid( ) ,
589
- Self :: progress_pct( self . current_vid + batch . next_vid( ) , self . target_vid)
552
+ Self :: progress_pct( batcher . next_vid( ) , batcher . target_vid( ) ) ,
553
+ entity_type ,
554
+ batcher . next_vid( ) ,
555
+ batcher . target_vid( ) ,
556
+ Self :: progress_pct( self . current_vid + batcher . next_vid( ) , self . target_vid)
590
557
) ;
591
558
self . last_log = Instant :: now ( ) ;
592
559
}
593
560
}
594
561
595
- fn table_finished ( & mut self , batch : & BatchCopy ) {
596
- self . current_vid += batch . next_vid ( ) ;
562
+ fn table_finished ( & mut self , batcher : & VidBatcher ) {
563
+ self . current_vid += batcher . next_vid ( ) ;
597
564
}
598
565
599
566
fn finished ( & self ) {
@@ -728,9 +695,9 @@ impl Connection {
728
695
if status == Status :: Cancelled {
729
696
return Ok ( status) ;
730
697
}
731
- progress. update ( & table. batch ) ;
698
+ progress. update ( & table. dst . object , & table . batcher ) ;
732
699
}
733
- progress. table_finished ( & table. batch ) ;
700
+ progress. table_finished ( & table. batcher ) ;
734
701
}
735
702
736
703
// Create indexes for all the attributes that were postponed at the start of
@@ -740,8 +707,8 @@ impl Connection {
740
707
for table in state. tables . iter ( ) {
741
708
let arr = index_list. indexes_for_table (
742
709
& self . dst . site . namespace ,
743
- & table. batch . src . name . to_string ( ) ,
744
- & table. batch . dst ,
710
+ & table. src . name . to_string ( ) ,
711
+ & table. dst ,
745
712
true ,
746
713
true ,
747
714
) ?;
@@ -756,18 +723,12 @@ impl Connection {
756
723
// Here we need to skip those created in the first step for the old fields.
757
724
for table in state. tables . iter ( ) {
758
725
let orig_colums = table
759
- . batch
760
726
. src
761
727
. columns
762
728
. iter ( )
763
729
. map ( |c| c. name . to_string ( ) )
764
730
. collect_vec ( ) ;
765
- for sql in table
766
- . batch
767
- . dst
768
- . create_postponed_indexes ( orig_colums)
769
- . into_iter ( )
770
- {
731
+ for sql in table. dst . create_postponed_indexes ( orig_colums) . into_iter ( ) {
771
732
let query = sql_query ( sql) ;
772
733
query. execute ( conn) ?;
773
734
}
0 commit comments