github.com/doitroot/helm@v3.0.0-beta.3+incompatible/pkg/storage/driver/records.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package driver // import "helm.sh/helm/pkg/storage/driver" 18 19 import ( 20 "sort" 21 "strconv" 22 23 rspb "helm.sh/helm/pkg/release" 24 ) 25 26 // records holds a list of in-memory release records 27 type records []*record 28 29 func (rs records) Len() int { return len(rs) } 30 func (rs records) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] } 31 func (rs records) Less(i, j int) bool { return rs[i].rls.Version < rs[j].rls.Version } 32 33 func (rs *records) Add(r *record) error { 34 if r == nil { 35 return nil 36 } 37 38 if rs.Exists(r.key) { 39 return ErrReleaseExists 40 } 41 42 *rs = append(*rs, r) 43 sort.Sort(*rs) 44 45 return nil 46 } 47 48 func (rs records) Get(key string) *record { 49 if i, ok := rs.Index(key); ok { 50 return rs[i] 51 } 52 return nil 53 } 54 55 func (rs *records) Iter(fn func(int, *record) bool) { 56 cp := make([]*record, len(*rs)) 57 copy(cp, *rs) 58 59 for i, r := range cp { 60 if !fn(i, r) { 61 return 62 } 63 } 64 } 65 66 func (rs *records) Index(key string) (int, bool) { 67 for i, r := range *rs { 68 if r.key == key { 69 return i, true 70 } 71 } 72 return -1, false 73 } 74 75 func (rs records) Exists(key string) bool { 76 _, ok := rs.Index(key) 77 return ok 78 } 79 80 func (rs *records) Remove(key string) (r *record) { 81 if i, ok := rs.Index(key); ok { 82 return rs.removeAt(i) 83 } 84 return nil 85 } 86 87 func (rs *records) Replace(key string, rec *record) *record { 88 if i, ok := rs.Index(key); ok { 89 old := (*rs)[i] 90 (*rs)[i] = rec 91 return old 92 } 93 return nil 94 } 95 96 func (rs *records) removeAt(index int) *record { 97 r := (*rs)[index] 98 (*rs)[index] = nil 99 copy((*rs)[index:], (*rs)[index+1:]) 100 *rs = (*rs)[:len(*rs)-1] 101 return r 102 } 103 104 // record is the data structure used to cache releases 105 // for the in-memory storage driver 106 type record struct { 107 key string 108 lbs labels 109 rls *rspb.Release 110 } 111 112 // newRecord creates a new in-memory release record 113 func newRecord(key string, rls *rspb.Release) *record { 114 var lbs labels 115 116 lbs.init() 117 lbs.set("name", rls.Name) 118 lbs.set("owner", "helm") 119 lbs.set("status", rls.Info.Status.String()) 120 lbs.set("version", strconv.Itoa(rls.Version)) 121 122 // return &record{key: key, lbs: lbs, rls: proto.Clone(rls).(*rspb.Release)} 123 return &record{key: key, lbs: lbs, rls: rls} 124 }