go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/prjmanager/prjpb/pcls.go (about)

     1  // Copyright 2021 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 prjpb
    16  
    17  import (
    18  	"sort"
    19  
    20  	"go.chromium.org/luci/cv/internal/prjmanager/copyonwrite"
    21  )
    22  
    23  // COWPCLs copy-on-write modifies PCLs.
    24  func (p *PState) COWPCLs(m func(*PCL) *PCL, toAdd []*PCL) ([]*PCL, bool) {
    25  	var mf copyonwrite.Modifier
    26  	if m != nil {
    27  		mf = func(v any) any {
    28  			if v := m(v.(*PCL)); v != nil {
    29  				return v
    30  			}
    31  			return copyonwrite.Deletion
    32  		}
    33  	}
    34  	in := cowPCLs(p.GetPcls())
    35  	out, updated := copyonwrite.Update(in, mf, cowPCLs(toAdd))
    36  	return []*PCL(out.(cowPCLs)), updated
    37  }
    38  
    39  // GetPCL returns the PCL of a given clid or nil, if not found.
    40  func (p *PState) GetPCL(clid int64) *PCL {
    41  	pcls := p.GetPcls()
    42  	idx := sort.Search(len(pcls), func(i int) bool {
    43  		return pcls[i].GetClid() >= clid
    44  	})
    45  	if idx >= len(pcls) || pcls[idx].GetClid() != clid {
    46  		return nil
    47  	}
    48  	return pcls[idx]
    49  }
    50  
    51  type cowPCLs []*PCL
    52  
    53  func (c cowPCLs) CloneShallow(length int, capacity int) copyonwrite.Slice {
    54  	r := make(cowPCLs, length, capacity)
    55  	copy(r, c[:length])
    56  	return r
    57  }
    58  
    59  func (c cowPCLs) Append(v any) copyonwrite.Slice {
    60  	return append(c, v.(*PCL))
    61  }
    62  
    63  func (c cowPCLs) Len() int {
    64  	return len(c)
    65  }
    66  
    67  func (c cowPCLs) At(index int) any {
    68  	return c[index]
    69  }