go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/service/datastore/meta/eg.go (about)

     1  // Copyright 2015 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 meta
    16  
    17  import (
    18  	"context"
    19  
    20  	ds "go.chromium.org/luci/gae/service/datastore"
    21  )
    22  
    23  // EntityGroupMeta is the model corresponding to the __entity_group__ model in
    24  // appengine. You shouldn't need to use this struct directly, but instead should
    25  // use GetEntityGroupVersion.
    26  type EntityGroupMeta struct {
    27  	kind   string  `gae:"$kind,__entity_group__"`
    28  	id     int64   `gae:"$id,1"`
    29  	Parent *ds.Key `gae:"$parent"`
    30  
    31  	Version int64 `gae:"__version__"`
    32  }
    33  
    34  // GetEntityGroupVersion returns the entity group version for the entity group
    35  // containing root. If the entity group doesn't exist, this function will return
    36  // zero and a nil error.
    37  func GetEntityGroupVersion(c context.Context, key *ds.Key) (int64, error) {
    38  	egm := &EntityGroupMeta{Parent: key.Root()}
    39  	err := ds.Get(c, egm)
    40  	ret := egm.Version
    41  	if err == ds.ErrNoSuchEntity {
    42  		// this is OK for callers. The version of the entity group is effectively 0
    43  		// in this case.
    44  		err = nil
    45  	}
    46  	return ret, err
    47  }