github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+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  	rspb "k8s.io/helm/pkg/proto/hapi/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) FindByVersion(vers int32) (int, bool) {
    97  	i := sort.Search(len(rs), func(i int) bool {
    98  		return rs[i].rls.Version == vers
    99  	})
   100  	if i < len(rs) && rs[i].rls.Version == vers {
   101  		return i, true
   102  	}
   103  	return i, false
   104  }
   105  
   106  func (rs *records) removeAt(index int) *record {
   107  	r := (*rs)[index]
   108  	(*rs)[index] = nil
   109  	copy((*rs)[index:], (*rs)[index+1:])
   110  	*rs = (*rs)[:len(*rs)-1]
   111  	return r
   112  }
   113  
   114  // record is the data structure used to cache releases
   115  // for the in-memory storage driver
   116  type record struct {
   117  	key string
   118  	lbs labels
   119  	rls *rspb.Release
   120  }
   121  
   122  // newRecord creates a new in-memory release record
   123  func newRecord(key string, rls *rspb.Release) *record {
   124  	var lbs labels
   125  
   126  	lbs.init()
   127  	lbs.set("NAME", rls.Name)
   128  	lbs.set("OWNER", "TILLER")
   129  	lbs.set("STATUS", rspb.Status_Code_name[int32(rls.Info.Status.Code)])
   130  	lbs.set("VERSION", strconv.Itoa(int(rls.Version)))
   131  
   132  	return &record{key: key, lbs: lbs, rls: rls}
   133  }