go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/prjmanager/prjpb/purges.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 // COWPurgingCLs copy-on-write modifies PurgingCLs. 24 func (p *PState) COWPurgingCLs(m func(*PurgingCL) *PurgingCL, toAdd []*PurgingCL) ([]*PurgingCL, bool) { 25 in := cowPurgingCLs(p.GetPurgingCls()) 26 out, updated := copyonwrite.Update(in, purgingCLModifier(m), cowPurgingCLs(toAdd)) 27 return []*PurgingCL(out.(cowPurgingCLs)), updated 28 } 29 30 // GetPurgingCL returns the PurgingCL of a given clid or nil, if not found. 31 func (p *PState) GetPurgingCL(clid int64) *PurgingCL { 32 prcls := p.GetPurgingCls() 33 idx := sort.Search(len(prcls), func(i int) bool { 34 return prcls[i].GetClid() >= clid 35 }) 36 if idx >= len(prcls) || prcls[idx].GetClid() != clid { 37 return nil 38 } 39 return prcls[idx] 40 } 41 42 func purgingCLModifier(f func(*PurgingCL) *PurgingCL) copyonwrite.Modifier { 43 if f == nil { 44 return nil 45 } 46 return func(v any) any { 47 if v := f(v.(*PurgingCL)); v != nil { 48 return v 49 } 50 return copyonwrite.Deletion 51 } 52 } 53 54 type cowPurgingCLs []*PurgingCL 55 56 // It's important that PurgingCLs are always sorted. 57 var _ copyonwrite.SortedSlice = cowPurgingCLs(nil) 58 59 func (c cowPurgingCLs) At(index int) any { 60 return c[index] 61 } 62 63 func (c cowPurgingCLs) Append(v any) copyonwrite.Slice { 64 return append(c, v.(*PurgingCL)) 65 } 66 67 func (c cowPurgingCLs) CloneShallow(length int, capacity int) copyonwrite.Slice { 68 r := make(cowPurgingCLs, length, capacity) 69 copy(r, c[:length]) 70 return r 71 } 72 73 func (c cowPurgingCLs) LessElements(a any, b any) bool { 74 return a.(*PurgingCL).GetClid() < b.(*PurgingCL).GetClid() 75 } 76 77 func (c cowPurgingCLs) Len() int { return len(c) } 78 func (c cowPurgingCLs) Less(i int, j int) bool { return c[i].GetClid() < c[j].GetClid() } 79 func (c cowPurgingCLs) Swap(i int, j int) { c[i], c[j] = c[j], c[i] }