go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/model/bucket.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 model
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/gae/service/datastore"
    21  
    22  	pb "go.chromium.org/luci/buildbucket/proto"
    23  )
    24  
    25  const (
    26  	// BucketKind is a bucket entity's kind in the datastore.
    27  	BucketKind = "BucketV2"
    28  )
    29  
    30  // Bucket is a representation of a bucket in the datastore.
    31  type Bucket struct {
    32  	_     datastore.PropertyMap `gae:"-,extra"`
    33  	_kind string                `gae:"$kind,BucketV2"`
    34  	// ID is the bucket in v2 format.
    35  	// e.g. try (never luci.chromium.try).
    36  	ID     string         `gae:"$id"`
    37  	Parent *datastore.Key `gae:"$parent"`
    38  
    39  	// Bucket is the bucket in v2 format.
    40  	// e.g. try (never luci.chromium.try).
    41  	//
    42  	// Note: This field is "computed" in *Bucket.Save phase and exists because v1
    43  	// APIs are allowed to search without a luci project context.
    44  	Bucket string `gae:"bucket_name"`
    45  	// Proto is the pb.Bucket proto representation of the bucket.
    46  	//
    47  	// swarming.builders is zeroed and stored in separate Builder datastore
    48  	// entities due to potentially large size.
    49  	Proto *pb.Bucket `gae:"config,legacy"`
    50  	// Revision is the config revision this entity was created from.
    51  	// TODO(crbug/1042991): Switch to noindex.
    52  	Revision string `gae:"revision"`
    53  	// Schema is this entity's schema version.
    54  	// TODO(crbug/1042991): Switch to noindex.
    55  	Schema int32 `gae:"entity_schema_version"`
    56  	// Shadows is the list of buckets this bucket shadows.
    57  	// It will be empty if this bucket is not a shadow bucket.
    58  	Shadows []string `gae:"shadows,noindex"`
    59  }
    60  
    61  // BucketKey returns a datastore key of a bucket.
    62  func BucketKey(ctx context.Context, project, bucket string) *datastore.Key {
    63  	return datastore.KeyForObj(ctx, &Bucket{
    64  		ID:     bucket,
    65  		Parent: ProjectKey(ctx, project),
    66  	})
    67  }
    68  
    69  var _ datastore.PropertyLoadSaver = (*Bucket)(nil)
    70  
    71  // Load implements datastore.PropertyLoadSaver
    72  func (b *Bucket) Load(p datastore.PropertyMap) error {
    73  	return datastore.GetPLS(b).Load(p)
    74  }
    75  
    76  // Save implements datastore.PropertyLoadSaver. It mutates this bucket entity
    77  // by always copying its ID value into the bucket_name field.
    78  func (b *Bucket) Save(withMeta bool) (datastore.PropertyMap, error) {
    79  	b.Bucket = b.ID
    80  	return datastore.GetPLS(b).Save(withMeta)
    81  }