github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/pkg/storage/driver/memory.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  	"sync"
    21  
    22  	rspb "k8s.io/helm/pkg/proto/hapi/release"
    23  )
    24  
    25  // MemoryDriverName is the string name of this driver.
    26  const MemoryDriverName = "Memory"
    27  
    28  // Memory is the in-memory storage driver implementation.
    29  type Memory struct {
    30  	sync.RWMutex
    31  	cache map[string]*rspb.Release
    32  }
    33  
    34  // NewMemory initializes a new memory driver.
    35  func NewMemory() *Memory {
    36  	return &Memory{cache: map[string]*rspb.Release{}}
    37  }
    38  
    39  // Name returns the name of the driver.
    40  func (mem *Memory) Name() string {
    41  	return MemoryDriverName
    42  }
    43  
    44  // Get returns the release named by key or returns ErrReleaseNotFound.
    45  func (mem *Memory) Get(key string) (*rspb.Release, error) {
    46  	defer unlock(mem.rlock())
    47  
    48  	if rls, ok := mem.cache[key]; ok {
    49  		return rls, nil
    50  	}
    51  	return nil, ErrReleaseNotFound
    52  }
    53  
    54  // List returns the list of all releases such that filter(release) == true
    55  func (mem *Memory) List(filter func(*rspb.Release) bool) ([]*rspb.Release, error) {
    56  	defer unlock(mem.rlock())
    57  
    58  	var releases []*rspb.Release
    59  	for k := range mem.cache {
    60  		if filter(mem.cache[k]) {
    61  			releases = append(releases, mem.cache[k])
    62  		}
    63  	}
    64  	return releases, nil
    65  }
    66  
    67  // Create creates a new release or returns ErrReleaseExists.
    68  func (mem *Memory) Create(rls *rspb.Release) error {
    69  	defer unlock(mem.wlock())
    70  
    71  	if _, ok := mem.cache[rls.Name]; ok {
    72  		return ErrReleaseExists
    73  	}
    74  	mem.cache[rls.Name] = rls
    75  	return nil
    76  }
    77  
    78  // Update updates a release or returns ErrReleaseNotFound.
    79  func (mem *Memory) Update(rls *rspb.Release) error {
    80  	defer unlock(mem.wlock())
    81  
    82  	if _, ok := mem.cache[rls.Name]; ok {
    83  		mem.cache[rls.Name] = rls
    84  		return nil
    85  	}
    86  	return ErrReleaseNotFound
    87  }
    88  
    89  // Delete deletes a release or returns ErrReleaseNotFound.
    90  func (mem *Memory) Delete(key string) (*rspb.Release, error) {
    91  	defer unlock(mem.wlock())
    92  
    93  	if old, ok := mem.cache[key]; ok {
    94  		delete(mem.cache, key)
    95  		return old, nil
    96  	}
    97  	return nil, ErrReleaseNotFound
    98  }
    99  
   100  // wlock locks mem for writing
   101  func (mem *Memory) wlock() func() {
   102  	mem.Lock()
   103  	return func() {
   104  		mem.Unlock()
   105  	}
   106  }
   107  
   108  // rlock locks mem for reading
   109  func (mem *Memory) rlock() func() {
   110  	mem.RLock()
   111  	return func() {
   112  		mem.RUnlock()
   113  	}
   114  }
   115  
   116  // unlock calls fn which reverses a mem.rlock or mem.wlock. e.g:
   117  // ```defer unlock(mem.rlock())```, locks mem for reading at the
   118  // call point of defer and unlocks upon exiting the block.
   119  func unlock(fn func()) { fn() }