github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/boskos/storage/storage.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes 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 storage
    18  
    19  import (
    20  	"sync"
    21  
    22  	"fmt"
    23  
    24  	"k8s.io/test-infra/boskos/common"
    25  )
    26  
    27  // PersistenceLayer defines a simple interface to persists Boskos Information
    28  type PersistenceLayer interface {
    29  	Add(i common.Item) error
    30  	Delete(name string) error
    31  	Update(i common.Item) error
    32  	Get(name string) (common.Item, error)
    33  	List() ([]common.Item, error)
    34  }
    35  
    36  type inMemoryStore struct {
    37  	items map[string]common.Item
    38  	lock  sync.RWMutex
    39  }
    40  
    41  // NewMemoryStorage creates an in memory persistence layer
    42  func NewMemoryStorage() PersistenceLayer {
    43  	return &inMemoryStore{
    44  		items: map[string]common.Item{},
    45  	}
    46  }
    47  
    48  func (im *inMemoryStore) Add(i common.Item) error {
    49  	im.lock.Lock()
    50  	defer im.lock.Unlock()
    51  	_, ok := im.items[i.GetName()]
    52  	if ok {
    53  		return fmt.Errorf("item %s already exists", i.GetName())
    54  	}
    55  	im.items[i.GetName()] = i
    56  	return nil
    57  }
    58  
    59  func (im *inMemoryStore) Delete(name string) error {
    60  	im.lock.Lock()
    61  	defer im.lock.Unlock()
    62  	_, ok := im.items[name]
    63  	if !ok {
    64  		return fmt.Errorf("cannot find item %s", name)
    65  	}
    66  	delete(im.items, name)
    67  	return nil
    68  }
    69  
    70  func (im *inMemoryStore) Update(i common.Item) error {
    71  	im.lock.Lock()
    72  	defer im.lock.Unlock()
    73  	_, ok := im.items[i.GetName()]
    74  	if !ok {
    75  		return fmt.Errorf("cannot find item %s", i.GetName())
    76  	}
    77  	im.items[i.GetName()] = i
    78  	return nil
    79  }
    80  
    81  func (im *inMemoryStore) Get(name string) (common.Item, error) {
    82  	im.lock.RLock()
    83  	defer im.lock.RUnlock()
    84  	i, ok := im.items[name]
    85  	if !ok {
    86  		return nil, fmt.Errorf("cannot find item %s", name)
    87  	}
    88  	return i, nil
    89  }
    90  
    91  func (im *inMemoryStore) List() ([]common.Item, error) {
    92  	im.lock.RLock()
    93  	defer im.lock.RUnlock()
    94  	var items []common.Item
    95  	for _, i := range im.items {
    96  		items = append(items, i)
    97  	}
    98  	return items, nil
    99  }