mapdbstorage.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. // Iterates through all key/values for the storage and calls the handler func. If a handler returns
  32. // an error, the iteration stops.
  33. func (cs *MapDBClusterStorage) Each(handler func(string, []byte) error) error {
  34. for k, v := range cs.store {
  35. value := make([]byte, len(v))
  36. copy(value, v)
  37. if err := handler(k, value); err != nil {
  38. return err
  39. }
  40. }
  41. return nil
  42. }
  43. // Closes the backing storage
  44. func (cs *MapDBClusterStorage) Close() error {
  45. return nil
  46. }