github.com/lrills/helm@v2.8.1+incompatible/pkg/storage/driver/records.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 "k8s.io/helm/pkg/storage/driver" 18 19 import ( 20 "sort" 21 "strconv" 22 23 "github.com/golang/protobuf/proto" 24 25 rspb "k8s.io/helm/pkg/proto/hapi/release" 26 ) 27 28 // records holds a list of in-memory release records 29 type records []*record 30 31 func (rs records) Len() int { return len(rs) } 32 func (rs records) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] } 33 func (rs records) Less(i, j int) bool { return rs[i].rls.Version < rs[j].rls.Version } 34 35 func (rs *records) Add(r *record) error { 36 if r == nil { 37 return nil 38 } 39 40 if rs.Exists(r.key) { 41 return ErrReleaseExists(r.key) 42 } 43 44 *rs = append(*rs, r) 45 sort.Sort(*rs) 46 47 return nil 48 } 49 50 func (rs records) Get(key string) *record { 51 if i, ok := rs.Index(key); ok { 52 return rs[i] 53 } 54 return nil 55 } 56 57 func (rs *records) Iter(fn func(int, *record) bool) { 58 cp := make([]*record, len(*rs)) 59 copy(cp, *rs) 60 61 for i, r := range cp { 62 if !fn(i, r) { 63 return 64 } 65 } 66 } 67 68 func (rs *records) Index(key string) (int, bool) { 69 for i, r := range *rs { 70 if r.key == key { 71 return i, true 72 } 73 } 74 return -1, false 75 } 76 77 func (rs records) Exists(key string) bool { 78 _, ok := rs.Index(key) 79 return ok 80 } 81 82 func (rs *records) Remove(key string) (r *record) { 83 if i, ok := rs.Index(key); ok { 84 return rs.removeAt(i) 85 } 86 return nil 87 } 88 89 func (rs *records) Replace(key string, rec *record) *record { 90 if i, ok := rs.Index(key); ok { 91 old := (*rs)[i] 92 (*rs)[i] = rec 93 return old 94 } 95 return nil 96 } 97 98 func (rs records) FindByVersion(vers int32) (int, bool) { 99 i := sort.Search(len(rs), func(i int) bool { 100 return rs[i].rls.Version == vers 101 }) 102 if i < len(rs) && rs[i].rls.Version == vers { 103 return i, true 104 } 105 return i, false 106 } 107 108 func (rs *records) removeAt(index int) *record { 109 r := (*rs)[index] 110 (*rs)[index] = nil 111 copy((*rs)[index:], (*rs)[index+1:]) 112 *rs = (*rs)[:len(*rs)-1] 113 return r 114 } 115 116 // record is the data structure used to cache releases 117 // for the in-memory storage driver 118 type record struct { 119 key string 120 lbs labels 121 rls *rspb.Release 122 } 123 124 // newRecord creates a new in-memory release record 125 func newRecord(key string, rls *rspb.Release) *record { 126 var lbs labels 127 128 lbs.init() 129 lbs.set("NAME", rls.Name) 130 lbs.set("OWNER", "TILLER") 131 lbs.set("STATUS", rspb.Status_Code_name[int32(rls.Info.Status.Code)]) 132 lbs.set("VERSION", strconv.Itoa(int(rls.Version))) 133 134 return &record{key: key, lbs: lbs, rls: proto.Clone(rls).(*rspb.Release)} 135 }