9
9
10
10
//! Logic related to executing catalog transactions.
11
11
12
+ use std:: borrow:: Cow ;
12
13
use std:: collections:: { BTreeMap , BTreeSet } ;
13
14
use std:: sync:: Arc ;
14
15
use std:: time:: Duration ;
@@ -425,10 +426,8 @@ impl Catalog {
425
426
. transaction ( )
426
427
. await
427
428
. unwrap_or_terminate ( "starting catalog transaction" ) ;
428
- // Prepare a candidate catalog state.
429
- let mut state = self . state . clone ( ) ;
430
429
431
- Self :: transact_inner (
430
+ let new_state = Self :: transact_inner (
432
431
storage_collections,
433
432
oracle_write_ts,
434
433
session,
@@ -437,7 +436,7 @@ impl Catalog {
437
436
& mut builtin_table_updates,
438
437
& mut audit_events,
439
438
& mut tx,
440
- & mut state,
439
+ & self . state ,
441
440
)
442
441
. await ?;
443
442
@@ -452,8 +451,10 @@ impl Catalog {
452
451
// Dropping here keeps the mutable borrow on self, preventing us accidentally
453
452
// mutating anything until after f is executed.
454
453
drop ( storage) ;
455
- self . state = state;
456
- self . transient_revision += 1 ;
454
+ if let Some ( new_state) = new_state {
455
+ self . transient_revision += 1 ;
456
+ self . state = new_state;
457
+ }
457
458
458
459
// Drop in-memory planning metadata.
459
460
let dropped_notices = self . drop_plans_and_metainfos ( & dropped_global_ids) ;
@@ -472,7 +473,8 @@ impl Catalog {
472
473
} )
473
474
}
474
475
475
- /// Performs the transaction described by `ops`.
476
+ /// Performs the transaction described by `ops` and returns the new state of the catalog, if
477
+ /// it has changed. If `ops` don't result in a change in the state this method returns `None`.
476
478
///
477
479
/// # Panics
478
480
/// - If `ops` contains [`Op::TransactionDryRun`] and the value is not the
@@ -490,8 +492,10 @@ impl Catalog {
490
492
builtin_table_updates : & mut Vec < BuiltinTableUpdate > ,
491
493
audit_events : & mut Vec < VersionedEvent > ,
492
494
tx : & mut Transaction < ' _ > ,
493
- state : & mut CatalogState ,
494
- ) -> Result < ( ) , AdapterError > {
495
+ state : & CatalogState ,
496
+ ) -> Result < Option < CatalogState > , AdapterError > {
497
+ let mut state = Cow :: Borrowed ( state) ;
498
+
495
499
let dry_run_ops = match ops. last ( ) {
496
500
Some ( Op :: TransactionDryRun ) => {
497
501
// Remove dry run marker.
@@ -500,7 +504,7 @@ impl Catalog {
500
504
ops. clone ( )
501
505
}
502
506
Some ( _) => vec ! [ ] ,
503
- None => return Ok ( ( ) ) ,
507
+ None => return Ok ( None ) ,
504
508
} ;
505
509
506
510
let mut storage_collections_to_create = BTreeSet :: new ( ) ;
@@ -515,7 +519,7 @@ impl Catalog {
515
519
& temporary_ids,
516
520
audit_events,
517
521
tx,
518
- state,
522
+ & * state,
519
523
& mut storage_collections_to_create,
520
524
& mut storage_collections_to_drop,
521
525
& mut storage_collections_to_register,
@@ -550,10 +554,13 @@ impl Catalog {
550
554
551
555
let mut updates: Vec < _ > = tx. get_and_commit_op_updates ( ) ;
552
556
updates. extend ( temporary_item_updates) ;
553
- let op_builtin_table_updates = state. apply_updates ( updates) ?;
554
- let op_builtin_table_updates =
555
- state. resolve_builtin_table_updates ( op_builtin_table_updates) ;
556
- builtin_table_updates. extend ( op_builtin_table_updates) ;
557
+ if !updates. is_empty ( ) {
558
+ let op_builtin_table_updates = state. to_mut ( ) . apply_updates ( updates) ?;
559
+ let op_builtin_table_updates = state
560
+ . to_mut ( )
561
+ . resolve_builtin_table_updates ( op_builtin_table_updates) ;
562
+ builtin_table_updates. extend ( op_builtin_table_updates) ;
563
+ }
557
564
}
558
565
559
566
if dry_run_ops. is_empty ( ) {
@@ -569,16 +576,22 @@ impl Catalog {
569
576
}
570
577
571
578
let updates = tx. get_and_commit_op_updates ( ) ;
572
- let op_builtin_table_updates = state. apply_updates ( updates) ?;
573
- let op_builtin_table_updates =
574
- state. resolve_builtin_table_updates ( op_builtin_table_updates) ;
575
- builtin_table_updates. extend ( op_builtin_table_updates) ;
579
+ if !updates. is_empty ( ) {
580
+ let op_builtin_table_updates = state. to_mut ( ) . apply_updates ( updates) ?;
581
+ let op_builtin_table_updates = state
582
+ . to_mut ( )
583
+ . resolve_builtin_table_updates ( op_builtin_table_updates) ;
584
+ builtin_table_updates. extend ( op_builtin_table_updates) ;
585
+ }
576
586
577
- Ok ( ( ) )
587
+ match state {
588
+ Cow :: Owned ( state) => Ok ( Some ( state) ) ,
589
+ Cow :: Borrowed ( _) => Ok ( None ) ,
590
+ }
578
591
} else {
579
592
Err ( AdapterError :: TransactionDryRun {
580
593
new_ops : dry_run_ops,
581
- new_state : state. clone ( ) ,
594
+ new_state : state. into_owned ( ) ,
582
595
} )
583
596
}
584
597
}
0 commit comments