go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/invocations/misc.go (about)

     1  // Copyright 2020 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package invocations
    16  
    17  import (
    18  	"context"
    19  
    20  	"cloud.google.com/go/spanner"
    21  
    22  	"go.chromium.org/luci/common/errors"
    23  	"go.chromium.org/luci/server/span"
    24  
    25  	"go.chromium.org/luci/resultdb/internal/pagination"
    26  	"go.chromium.org/luci/resultdb/internal/spanutil"
    27  )
    28  
    29  // Shards is the sharding level for the Invocations table.
    30  // Column Invocations.ShardId is a value in range [0, Shards).
    31  const Shards = 100
    32  
    33  // CurrentMaxShard reads the highest shard id in the Invocations table.
    34  // This may differ from the constant above when it has changed recently.
    35  func CurrentMaxShard(ctx context.Context) (int, error) {
    36  	var ret int64
    37  	err := spanutil.QueryFirstRow(span.Single(ctx), spanner.NewStatement(`
    38  		SELECT ShardId
    39  		FROM Invocations@{FORCE_INDEX=InvocationsByInvocationExpiration}
    40  		ORDER BY ShardID DESC
    41  		LIMIT 1
    42  	`), &ret)
    43  	return int(ret), err
    44  }
    45  
    46  // TokenToMap parses a page token to a map.
    47  // The first component of the token is expected to be an invocation ID.
    48  // Convenient to initialize Spanner statement parameters.
    49  // Expects the token to be either empty or have len(keys) components.
    50  // If the token is empty, sets map values to "".
    51  func TokenToMap(token string, dest map[string]any, keys ...string) error {
    52  	if len(keys) == 0 {
    53  		panic("keys is empty")
    54  	}
    55  	switch parts, err := pagination.ParseToken(token); {
    56  	case err != nil:
    57  		return err
    58  
    59  	case len(parts) == 0:
    60  		for i, k := range keys {
    61  			if i == 0 {
    62  				dest[k] = ID("")
    63  			} else {
    64  				dest[k] = ""
    65  			}
    66  		}
    67  		return nil
    68  
    69  	case len(parts) != len(keys):
    70  		return pagination.InvalidToken(errors.Reason("expected %d components, got %q", len(keys), parts).Err())
    71  
    72  	default:
    73  		for i, k := range keys {
    74  			if i == 0 {
    75  				dest[k] = ID(parts[i])
    76  			} else {
    77  				dest[k] = parts[i]
    78  			}
    79  		}
    80  		return nil
    81  	}
    82  }