@@ -174,15 +174,14 @@ func (db *database) queryVersionsIfPresent(ctx context.Context, nsID string, que
174174 start := time .Now ()
175175 query := FmtNsID (queryVersionsSQLTempl , nsID )
176176
177- foundKeysVersions , err := retryQueryAndReadTwoItems [[]byte , int64 ](ctx , db , query , queryKeys )
177+ foundKeys , foundVersions , err := retryQueryAndReadTwoItems [[]byte , int64 ](ctx , db , query , queryKeys )
178178 if err != nil {
179179 return nil , fmt .Errorf ("failed to get keys' version from namespace [%s]: %w" , nsID , err )
180180 }
181181
182182 kToV := make (keyToVersion )
183- for _ , keyVersion := range foundKeysVersions {
184- //nolint:gosec // DB table is constraint to non-negative value.
185- kToV [string (keyVersion .item1 )] = uint64 (keyVersion .item2 )
183+ for i , key := range foundKeys {
184+ kToV [string (key )] = uint64 (foundVersions [i ]) //nolint:gosec // DB table is constraint to non-negative value.
186185 }
187186 promutil .Observe (db .metrics .databaseTxBatchQueryVersionLatencySeconds , time .Since (start ))
188187
@@ -483,33 +482,33 @@ func (db *database) readStatusWithHeight(
483482}
484483
485484func (db * database ) readNamespacePolicies (ctx context.Context ) (* applicationpb.NamespacePolicies , error ) {
486- keysValues , err := retryQueryAndReadTwoItems [[]byte , []byte ](ctx , db , queryPoliciesSQLStmt )
485+ keys , values , err := retryQueryAndReadTwoItems [[]byte , []byte ](ctx , db , queryPoliciesSQLStmt )
487486 if err != nil {
488487 metaTable := TableName (committerpb .MetaNamespaceID )
489488 return nil , fmt .Errorf ("failed to read the policies from table [%s]: %w" , metaTable , err )
490489 }
491490 policy := & applicationpb.NamespacePolicies {
492- Policies : make ([]* applicationpb.PolicyItem , len (keysValues )),
491+ Policies : make ([]* applicationpb.PolicyItem , len (keys )),
493492 }
494493
495- for i , keyValue := range keysValues {
494+ for i , key := range keys {
496495 policy .Policies [i ] = & applicationpb.PolicyItem {
497- Namespace : string (keyValue . item1 ),
498- Policy : keyValue . item2 ,
496+ Namespace : string (key ),
497+ Policy : values [ i ] ,
499498 }
500499 }
501500 return policy , nil
502501}
503502
504503func (db * database ) readConfigTX (ctx context.Context ) (* applicationpb.ConfigTransaction , error ) {
505- keysValues , err := retryQueryAndReadTwoItems [[]byte , []byte ](ctx , db , queryConfigSQLStmt )
504+ _ , values , err := retryQueryAndReadTwoItems [[]byte , []byte ](ctx , db , queryConfigSQLStmt )
506505 if err != nil {
507506 return nil , fmt .Errorf ("failed to read the config transaction from table [%s]: %w" ,
508507 TableName (committerpb .ConfigNamespaceID ), err )
509508 }
510509 configTX := & applicationpb.ConfigTransaction {}
511- for _ , v := range keysValues {
512- configTX .Envelope = v . item2
510+ for _ , v := range values {
511+ configTX .Envelope = v
513512 }
514513 return configTX , nil
515514}
@@ -529,28 +528,37 @@ func retryQueryAndReadArrayResult[T any](
529528
530529func retryQueryAndReadTwoItems [T1 , T2 any ](
531530 ctx context.Context , db * database , query string , args ... any ,
532- ) ([]tuple [ T1 , T2 ] , error ) {
533- return retry .ExecuteWithResult (ctx , db .retryProfile , func () ([] tuple [T1 , T2 ], error ) {
531+ ) ([]T1 , [] T2 , error ) {
532+ res , err := retry .ExecuteWithResult (ctx , db .retryProfile , func () (* tuple [[] T1 , [] T2 ], error ) {
534533 rows , queryErr := db .pool .Query (ctx , query , args ... )
535534 if queryErr != nil {
536535 return nil , errors .Wrapf (queryErr , "query rows: query [%s]" , query )
537536 }
538537 defer rows .Close ()
539- items , readErr := readTwoItems [T1 , T2 ](rows )
540- return items , errors .Wrapf (readErr , "read rows from the query [%s] results" , query )
538+ items1 , items2 , readErr := readTwoItems [T1 , T2 ](rows )
539+ return & tuple [[]T1 , []T2 ]{
540+ item1 : items1 ,
541+ item2 : items2 ,
542+ }, errors .Wrapf (readErr , "read rows from the query [%s] results" , query )
541543 })
544+ if err != nil {
545+ return nil , nil , err
546+ }
547+ return res .item1 , res .item2 , nil
542548}
543549
544550// readTwoItems reads two items from given rows.
545- func readTwoItems [T1 , T2 any ](r pgx.Rows ) (items []tuple [ T1 , T2 ] , err error ) {
551+ func readTwoItems [T1 , T2 any ](r pgx.Rows ) (items1 []T1 , items2 [] T2 , err error ) {
546552 for r .Next () {
547- var i tuple [T1 , T2 ]
548- if scanErr := r .Scan (& i .item1 , & i .item2 ); scanErr != nil {
549- return nil , errors .Wrap (scanErr , "failed while scanning a row" )
553+ var i1 T1
554+ var i2 T2
555+ if err := r .Scan (& i1 , & i2 ); err != nil {
556+ return nil , nil , errors .Wrap (err , "failed while scanning a row" )
550557 }
551- items = append (items , i )
558+ items1 = append (items1 , i1 )
559+ items2 = append (items2 , i2 )
552560 }
553- return items , errors .Wrap (r .Err (), "failed while reading from rows" )
561+ return items1 , items2 , errors .Wrap (r .Err (), "failed while reading from rows" )
554562}
555563
556564func readArrayResult [T any ](r pgx.Row ) (res []T , err error ) {
0 commit comments