go.ligato.io/vpp-agent/v3@v3.5.0/plugins/orchestrator/store.go (about)

     1  //  Copyright (c) 2019 Cisco and/or its affiliates.
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at:
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package orchestrator
    16  
    17  import (
    18  	"sort"
    19  
    20  	"google.golang.org/protobuf/proto"
    21  )
    22  
    23  // KVStore describes an interface for key-value store used by dispatcher.
    24  type KVStore interface {
    25  	ListAll() KVPairs
    26  	List(dataSrc string) KVPairs
    27  	Update(dataSrc, key string, val proto.Message)
    28  	Delete(dataSrc, key string)
    29  	Reset(dataSrc string)
    30  }
    31  
    32  // KLStore describes an interface for key-label store used by dispatcher.
    33  type KLStore interface {
    34  	ListLabels(key string) Labels
    35  	AddLabel(key, lkey, lval string)
    36  	HasLabel(key, lkey string) bool
    37  	DeleteLabel(key, lkey string)
    38  	ResetLabels(key string)
    39  }
    40  
    41  type Store interface {
    42  	KLStore
    43  	KVStore
    44  }
    45  
    46  // memStore is KStore implementation that stores data in memory.
    47  type memStore struct {
    48  	db  map[string]KVPairs
    49  	ldb map[string]Labels
    50  }
    51  
    52  func newMemStore() *memStore {
    53  	return &memStore{
    54  		db:  make(map[string]KVPairs),
    55  		ldb: make(map[string]Labels),
    56  	}
    57  }
    58  
    59  // List lists all key-value pairs.
    60  func (s *memStore) ListAll() KVPairs {
    61  	var dataSrcs []string
    62  	for dataSrc := range s.db {
    63  		dataSrcs = append(dataSrcs, dataSrc)
    64  	}
    65  	sort.Strings(dataSrcs)
    66  	pairs := make(KVPairs)
    67  	for _, dataSrc := range dataSrcs {
    68  		for k, v := range s.List(dataSrc) {
    69  			pairs[k] = v
    70  		}
    71  	}
    72  	return pairs
    73  }
    74  
    75  // List lists actual key-value pairs.
    76  func (s *memStore) List(dataSrc string) KVPairs {
    77  	pairs := make(KVPairs, len(s.db[dataSrc]))
    78  	for k, v := range s.db[dataSrc] {
    79  		pairs[k] = v
    80  	}
    81  	return pairs
    82  }
    83  
    84  // Update updates value stored under key with given value.
    85  func (s *memStore) Update(dataSrc, key string, val proto.Message) {
    86  	if _, ok := s.db[dataSrc]; !ok {
    87  		s.db[dataSrc] = make(KVPairs)
    88  	}
    89  	s.db[dataSrc][key] = val
    90  }
    91  
    92  // Delete deletes value stored under given key.
    93  func (s *memStore) Delete(dataSrc, key string) {
    94  	delete(s.db[dataSrc], key)
    95  }
    96  
    97  // Reset clears all key-value data.
    98  func (s *memStore) Reset(dataSrc string) {
    99  	delete(s.db, dataSrc)
   100  }
   101  
   102  func (s *memStore) ListLabels(key string) Labels {
   103  	labels := make(Labels, len(s.ldb[key]))
   104  	for lkey, lval := range s.ldb[key] {
   105  		labels[lkey] = lval
   106  	}
   107  	return labels
   108  }
   109  
   110  func (s *memStore) AddLabel(key, lkey, lval string) {
   111  	if _, ok := s.ldb[key]; !ok {
   112  		s.ldb[key] = make(Labels)
   113  	}
   114  	s.ldb[key][lkey] = lval
   115  }
   116  
   117  func (s *memStore) HasLabel(key, lkey string) bool {
   118  	_, ok := s.ldb[key][lkey]
   119  	return ok
   120  }
   121  
   122  func (s *memStore) DeleteLabel(key, lkey string) {
   123  	delete(s.ldb[key], lkey)
   124  }
   125  
   126  func (s *memStore) ResetLabels(key string) {
   127  	delete(s.ldb, key)
   128  }