go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/prjmanager/prjpb/components.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  	"go.chromium.org/luci/cv/internal/prjmanager/copyonwrite"
    19  )
    20  
    21  // CloneShallow creates a new shallow clone.
    22  func (c *Component) CloneShallow() *Component {
    23  	return &Component{
    24  		Clids:          c.GetClids(),
    25  		Pruns:          c.GetPruns(),
    26  		DecisionTime:   c.GetDecisionTime(),
    27  		TriageRequired: c.GetTriageRequired(),
    28  	}
    29  }
    30  
    31  // COWComponents copy-on-write modifies components.
    32  func (p *PState) COWComponents(m func(*Component) *Component, toAdd []*Component) ([]*Component, bool) {
    33  	var mf copyonwrite.Modifier
    34  	if m != nil {
    35  		mf = func(v any) any {
    36  			if v := m(v.(*Component)); v != nil {
    37  				return v
    38  			}
    39  			return copyonwrite.Deletion
    40  		}
    41  	}
    42  	in := cowComponents(p.GetComponents())
    43  	out, updated := copyonwrite.Update(in, mf, cowComponents(toAdd))
    44  	return []*Component(out.(cowComponents)), updated
    45  }
    46  
    47  type cowComponents []*Component
    48  
    49  func (c cowComponents) CloneShallow(length int, capacity int) copyonwrite.Slice {
    50  	r := make(cowComponents, length, capacity)
    51  	copy(r, c[:length])
    52  	return r
    53  }
    54  
    55  func (c cowComponents) Append(v any) copyonwrite.Slice {
    56  	return append(c, v.(*Component))
    57  }
    58  
    59  func (c cowComponents) Len() int {
    60  	return len(c)
    61  }
    62  
    63  func (c cowComponents) At(index int) any {
    64  	return c[index]
    65  }