go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipd/appengine/impl/admin/mappers.go (about)

     1  // Copyright 2018 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 admin
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"google.golang.org/protobuf/proto"
    22  
    23  	"go.chromium.org/luci/common/errors"
    24  	"go.chromium.org/luci/gae/service/datastore"
    25  	"go.chromium.org/luci/server/dsmapper"
    26  
    27  	api "go.chromium.org/luci/cipd/api/admin/v1"
    28  )
    29  
    30  // A registry of mapping job configurations.
    31  //
    32  // Populated during init time. See other *.go files in this package.
    33  var mappers = map[api.MapperKind]*mapperDef{}
    34  
    35  // initMapper is called during init time to register some mapper kind.
    36  func initMapper(d mapperDef) {
    37  	if _, ok := mappers[d.Kind]; ok {
    38  		panic(fmt.Sprintf("mapper with kind %s has already been initialized", d.Kind))
    39  	}
    40  	mappers[d.Kind] = &d
    41  }
    42  
    43  // mapperDef is some single flavor of mapping jobs.
    44  //
    45  // It contains parameters for the mapper (what entity to map over, number of
    46  // shards, etc), and the actual mapping function.
    47  type mapperDef struct {
    48  	Kind   api.MapperKind // also used to derive dsmapper.ID
    49  	Func   func(context.Context, dsmapper.JobID, *api.JobConfig, []*datastore.Key) error
    50  	Config dsmapper.JobConfig // note: Params will be overwritten
    51  }
    52  
    53  // mapperID returns an identifier for this mapper (to use cross-process).
    54  func (m *mapperDef) mapperID() dsmapper.ID {
    55  	return dsmapper.ID(fmt.Sprintf("cipd:v1:%s", m.Kind))
    56  }
    57  
    58  // newMapper creates new instance of a mapping function.
    59  func (m *mapperDef) newMapper(ctx context.Context, j *dsmapper.Job, shardIdx int) (dsmapper.Mapper, error) {
    60  	cfg := &api.JobConfig{}
    61  	if err := proto.Unmarshal(j.Config.Params, cfg); err != nil {
    62  		return nil, errors.Annotate(err, "failed to unmarshal JobConfig").Err()
    63  	}
    64  	return func(ctx context.Context, keys []*datastore.Key) error {
    65  		return m.Func(ctx, j.ID, cfg, keys)
    66  	}, nil
    67  }