@@ -230,7 +230,7 @@ impl<'a> Transaction<'a> {
230230 privileges : Vec < MzAclItem > ,
231231 temporary_oids : & HashSet < u32 > ,
232232 ) -> Result < ( DatabaseId , u32 ) , CatalogError > {
233- let id = self . get_and_increment_id ( DATABASE_ID_ALLOC_KEY . to_string ( ) ) ?;
233+ let id = self . get_and_increment_id ( & [ DATABASE_ID_ALLOC_KEY ] ) ?;
234234 let id = DatabaseId :: User ( id) ;
235235 let oid = self . allocate_oid ( temporary_oids) ?;
236236 self . insert_database ( id, database_name, owner_id, privileges, oid) ?;
@@ -268,7 +268,7 @@ impl<'a> Transaction<'a> {
268268 privileges : Vec < MzAclItem > ,
269269 temporary_oids : & HashSet < u32 > ,
270270 ) -> Result < ( SchemaId , u32 ) , CatalogError > {
271- let id = self . get_and_increment_id ( SCHEMA_ID_ALLOC_KEY . to_string ( ) ) ?;
271+ let id = self . get_and_increment_id ( & [ SCHEMA_ID_ALLOC_KEY ] ) ?;
272272 let id = SchemaId :: User ( id) ;
273273 let oid = self . allocate_oid ( temporary_oids) ?;
274274 self . insert_schema (
@@ -341,7 +341,7 @@ impl<'a> Transaction<'a> {
341341 vars : RoleVars ,
342342 temporary_oids : & HashSet < u32 > ,
343343 ) -> Result < ( RoleId , u32 ) , CatalogError > {
344- let id = self . get_and_increment_id ( USER_ROLE_ID_ALLOC_KEY . to_string ( ) ) ?;
344+ let id = self . get_and_increment_id ( & [ USER_ROLE_ID_ALLOC_KEY ] ) ?;
345345 let id = RoleId :: User ( id) ;
346346 let oid = self . allocate_oid ( temporary_oids) ?;
347347 self . insert_role ( id, name, attributes, membership, vars, oid) ?;
@@ -436,7 +436,7 @@ impl<'a> Transaction<'a> {
436436 config : ClusterConfig ,
437437 temporary_oids : & HashSet < u32 > ,
438438 ) -> Result < ( ) , CatalogError > {
439- let cluster_id = self . get_and_increment_id ( SYSTEM_CLUSTER_ID_ALLOC_KEY . to_string ( ) ) ?;
439+ let cluster_id = self . get_and_increment_id ( & [ SYSTEM_CLUSTER_ID_ALLOC_KEY ] ) ?;
440440 let cluster_id = ClusterId :: system ( cluster_id) . ok_or ( SqlCatalogError :: IdExhaustion ) ?;
441441 self . insert_cluster (
442442 cluster_id,
@@ -615,7 +615,7 @@ impl<'a> Transaction<'a> {
615615 temporary_oids : & HashSet < u32 > ,
616616 ) -> Result < NetworkPolicyId , CatalogError > {
617617 let oid = self . allocate_oid ( temporary_oids) ?;
618- let id = self . get_and_increment_id ( USER_NETWORK_POLICY_ID_ALLOC_KEY . to_string ( ) ) ?;
618+ let id = self . get_and_increment_id ( & [ USER_NETWORK_POLICY_ID_ALLOC_KEY ] ) ?;
619619 let id = NetworkPolicyId :: User ( id) ;
620620 self . insert_network_policy ( id, name, rules, privileges, owner_id, oid)
621621 }
@@ -733,40 +733,48 @@ impl<'a> Transaction<'a> {
733733 }
734734 }
735735
736- pub fn get_and_increment_id ( & mut self , key : String ) -> Result < u64 , CatalogError > {
737- Ok ( self . get_and_increment_id_by ( key , 1 ) ?. into_element ( ) )
736+ pub fn get_and_increment_id ( & mut self , keys : & [ & str ] ) -> Result < u64 , CatalogError > {
737+ Ok ( self . get_and_increment_id_by ( keys , 1 ) ?. into_element ( ) )
738738 }
739739
740740 pub fn get_and_increment_id_by (
741741 & mut self ,
742- key : String ,
742+ keys : & [ & str ] ,
743743 amount : u64 ,
744744 ) -> Result < Vec < u64 > , CatalogError > {
745+ let items = self . id_allocator . items ( ) ;
746+
745747 assert ! (
746- key != SYSTEM_ITEM_ALLOC_KEY || !self . durable_catalog. is_bootstrap_complete( ) ,
748+ keys != & [ SYSTEM_ITEM_ALLOC_KEY ] || !self . durable_catalog. is_bootstrap_complete( ) ,
747749 "system item IDs cannot be allocated outside of bootstrap"
748750 ) ;
749751
750- let current_id = self
751- . id_allocator
752- . items ( )
753- . get ( & IdAllocKey { name : key. clone ( ) } )
754- . unwrap_or_else ( || panic ! ( "{key} id allocator missing" ) )
755- . next_id ;
752+ let mut current_id = 1 ;
753+ for key in keys {
754+ let next_id = items
755+ . get ( & IdAllocKey {
756+ name : key. to_string ( ) ,
757+ } )
758+ . unwrap_or_else ( || panic ! ( "{key} id allocator missing" ) )
759+ . next_id ;
760+
761+ current_id = current_id. max ( next_id)
762+ }
763+
756764 let next_id = current_id
757765 . checked_add ( amount)
758766 . ok_or ( SqlCatalogError :: IdExhaustion ) ?;
759- let prev = self . id_allocator . set (
760- IdAllocKey { name : key } ,
761- Some ( IdAllocValue { next_id } ) ,
762- self . op_id ,
763- ) ? ;
764- assert_eq ! (
765- prev ,
766- Some ( IdAllocValue {
767- next_id: current_id
768- } )
769- ) ;
767+ for key in keys {
768+ let prev = self . id_allocator . set (
769+ IdAllocKey {
770+ name : key . to_string ( ) ,
771+ } ,
772+ Some ( IdAllocValue { next_id } ) ,
773+ self . op_id ,
774+ ) ? ;
775+ assert ! ( prev . is_some_and ( |value| value . next_id <= current_id) , ) ;
776+ }
777+
770778 Ok ( ( current_id..next_id) . collect ( ) )
771779 }
772780
@@ -779,7 +787,7 @@ impl<'a> Transaction<'a> {
779787 "we can only allocate system item IDs during bootstrap"
780788 ) ;
781789 Ok ( self
782- . get_and_increment_id_by ( SYSTEM_ITEM_ALLOC_KEY . to_string ( ) , amount) ?
790+ . get_and_increment_id_by ( & [ SYSTEM_ITEM_ALLOC_KEY ] , amount) ?
783791 . into_iter ( )
784792 // TODO(alter_table): Use separate ID allocators.
785793 . map ( |x| ( CatalogItemId :: System ( x) , GlobalId :: System ( x) ) )
@@ -882,29 +890,29 @@ impl<'a> Transaction<'a> {
882890 amount : u64 ,
883891 ) -> Result < Vec < ( CatalogItemId , GlobalId ) > , CatalogError > {
884892 Ok ( self
885- . get_and_increment_id_by ( USER_ITEM_ALLOC_KEY . to_string ( ) , amount) ?
893+ . get_and_increment_id_by ( & [ USER_ITEM_ALLOC_KEY ] , amount) ?
886894 . into_iter ( )
887895 // TODO(alter_table): Use separate ID allocators.
888896 . map ( |x| ( CatalogItemId :: User ( x) , GlobalId :: User ( x) ) )
889897 . collect ( ) )
890898 }
891899
892900 pub fn allocate_user_replica_id ( & mut self ) -> Result < ReplicaId , CatalogError > {
893- let id = self . get_and_increment_id ( USER_REPLICA_ID_ALLOC_KEY . to_string ( ) ) ?;
901+ let id = self . get_and_increment_id ( & [ USER_REPLICA_ID_ALLOC_KEY ] ) ?;
894902 Ok ( ReplicaId :: User ( id) )
895903 }
896904
897905 pub fn allocate_system_replica_id ( & mut self ) -> Result < ReplicaId , CatalogError > {
898- let id = self . get_and_increment_id ( SYSTEM_REPLICA_ID_ALLOC_KEY . to_string ( ) ) ?;
906+ let id = self . get_and_increment_id ( & [ SYSTEM_REPLICA_ID_ALLOC_KEY ] ) ?;
899907 Ok ( ReplicaId :: System ( id) )
900908 }
901909
902910 pub fn allocate_audit_log_id ( & mut self ) -> Result < u64 , CatalogError > {
903- self . get_and_increment_id ( AUDIT_LOG_ID_ALLOC_KEY . to_string ( ) )
911+ self . get_and_increment_id ( & [ AUDIT_LOG_ID_ALLOC_KEY ] )
904912 }
905913
906914 pub fn allocate_storage_usage_ids ( & mut self ) -> Result < u64 , CatalogError > {
907- self . get_and_increment_id ( STORAGE_USAGE_ID_ALLOC_KEY . to_string ( ) )
915+ self . get_and_increment_id ( & [ STORAGE_USAGE_ID_ALLOC_KEY ] )
908916 }
909917
910918 /// Allocates `amount` OIDs. OIDs can be recycled if they aren't currently assigned to any
0 commit comments