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