mapdbstorage.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package clusters
  2. // MapDBClusterStorage is a map implementation of a database used to store cluster definitions
  3. type MapDBClusterStorage struct {
  4. store map[string][]byte
  5. }
  6. // NewMapDBClusterStorage creates a new map backed ClusterStorage implementation
  7. func NewMapDBClusterStorage() ClusterStorage {
  8. return &MapDBClusterStorage{
  9. store: make(map[string][]byte),
  10. }
  11. }
  12. // AddIfNotExists Adds the entry if the key does not exist
  13. func (cs *MapDBClusterStorage) AddIfNotExists(key string, cluster []byte) error {
  14. if _, ok := cs.store[key]; !ok {
  15. cs.store[key] = cluster
  16. }
  17. return nil
  18. }
  19. // AddOrUpdate Adds the encoded cluster to storage if it doesn't exist. Otherwise, update the existing
  20. // value with the provided.
  21. func (cs *MapDBClusterStorage) AddOrUpdate(key string, cluster []byte) error {
  22. cs.store[key] = cluster
  23. return nil
  24. }
  25. // Remove Removes a key from the cluster storage
  26. func (cs *MapDBClusterStorage) Remove(key string) error {
  27. delete(cs.store, key)
  28. return nil
  29. }
  30. // Each Iterates through all key/values for the storage and calls the handler func. If a handler returns
  31. // an error, the iteration stops.
  32. func (cs *MapDBClusterStorage) Each(handler func(string, []byte) error) error {
  33. for k, v := range cs.store {
  34. value := make([]byte, len(v))
  35. copy(value, v)
  36. if err := handler(k, value); err != nil {
  37. return err
  38. }
  39. }
  40. return nil
  41. }
  42. // Close Closes the backing storage
  43. func (cs *MapDBClusterStorage) Close() error {
  44. return nil
  45. }