mapdbstorage.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package clustermanager
  2. import (
  3. _ "k8s.io/klog"
  4. )
  5. type MapDBClusterStorage struct {
  6. store map[string][]byte
  7. }
  8. func NewMapDBClusterStorage() ClusterStorage {
  9. return &MapDBClusterStorage{
  10. store: make(map[string][]byte),
  11. }
  12. }
  13. // Adds the entry if the key does not exist
  14. func (cs *MapDBClusterStorage) AddIfNotExists(key string, cluster []byte) error {
  15. if _, ok := cs.store[key]; !ok {
  16. cs.store[key] = cluster
  17. }
  18. return nil
  19. }
  20. // Adds the encoded cluster to storage if it doesn't exist. Otherwise, update the existing
  21. // value with the provided.
  22. func (cs *MapDBClusterStorage) AddOrUpdate(key string, cluster []byte) error {
  23. cs.store[key] = cluster
  24. return nil
  25. }
  26. // Removes a key from the cluster storage
  27. func (cs *MapDBClusterStorage) Remove(key string) error {
  28. delete(cs.store, key)
  29. return nil
  30. }
  31. // Retrieve all the keys stored
  32. func (cs *MapDBClusterStorage) Each(handler func(string, []byte)) error {
  33. for k, v := range cs.store {
  34. value := make([]byte, len(v))
  35. copy(value, v)
  36. handler(k, value)
  37. }
  38. return nil
  39. }
  40. // Closes the backing storage
  41. func (cs *MapDBClusterStorage) Close() error {
  42. return nil
  43. }