store.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package store
  2. import (
  3. "fmt"
  4. "sync"
  5. "github.com/google/uuid"
  6. )
  7. type IDer interface {
  8. GetID() string
  9. SetID(string)
  10. }
  11. type Hasher interface {
  12. GetHash() string
  13. }
  14. type Storable interface {
  15. IDer
  16. Hasher
  17. }
  18. type Storer[T Storable] interface {
  19. Create(T) (T, error)
  20. ReadAll() []T
  21. Read(string) (T, error)
  22. Update(T, string) (T, error)
  23. Delete(string) (T, error)
  24. }
  25. type FilterStorer[T Storable] interface {
  26. Storer[T]
  27. Filter([]T, func(T) bool) []T
  28. }
  29. type Store[T Storable] struct {
  30. ids map[string]T
  31. hashes map[string]T
  32. lock sync.RWMutex
  33. }
  34. type FilterStore[T Storable] struct {
  35. *Store[T]
  36. }
  37. func NewFilterStore[T Storable]() *FilterStore[T] {
  38. return &FilterStore[T]{NewStore[T]()}
  39. }
  40. func (fs *FilterStore[T]) Filter(slice []T, f func(T) bool) []T {
  41. result := make([]T, 0)
  42. for _, item := range slice {
  43. if f(item) {
  44. result = append(result, item)
  45. }
  46. }
  47. return result
  48. }
  49. func NewStore[T Storable]() *Store[T] {
  50. store := new(Store[T])
  51. store.ids = make(map[string]T)
  52. store.hashes = make(map[string]T)
  53. return store
  54. }
  55. func (s *Store[T]) Create(entity T) (T, error) {
  56. s.lock.Lock()
  57. defer s.lock.Unlock()
  58. if hash := entity.GetHash(); hash != "" {
  59. storedEntity, ok := s.hashes[hash]
  60. if ok {
  61. return storedEntity, nil
  62. }
  63. s.hashes[hash] = entity
  64. }
  65. id := entity.GetID()
  66. if id == "" {
  67. id = uuid.New().String()
  68. }
  69. entity.SetID(id)
  70. s.ids[id] = entity
  71. return entity, nil
  72. }
  73. func (s *Store[T]) ReadAll() []T {
  74. s.lock.Lock()
  75. defer s.lock.Unlock()
  76. result := make([]T, 0)
  77. for _, v := range s.ids {
  78. result = append(result, v)
  79. }
  80. return result
  81. }
  82. func (s *Store[T]) Read(id string) (T, error) {
  83. s.lock.RLock()
  84. defer s.lock.RUnlock()
  85. entity, ok := s.ids[id]
  86. if !ok {
  87. return entity, fmt.Errorf("Entity with ID %s was not found in the store.", id)
  88. }
  89. return entity, nil
  90. }
  91. func (s *Store[T]) Update(entity T, id string) (T, error) {
  92. sEntity, err := s.Read(id)
  93. if err != nil {
  94. return sEntity, err
  95. }
  96. s.lock.Lock()
  97. defer s.lock.Unlock()
  98. entity.SetID(id)
  99. s.ids[id] = entity
  100. if hash := entity.GetHash(); hash != "" {
  101. s.hashes[hash] = entity
  102. }
  103. return entity, nil
  104. }
  105. func (s *Store[T]) Delete(id string) (T, error) {
  106. sEntity, err := s.Read(id)
  107. if err != nil {
  108. return sEntity, err
  109. }
  110. s.lock.Lock()
  111. defer s.lock.Unlock()
  112. delete(s.ids, id)
  113. if hash := sEntity.GetHash(); hash != "" {
  114. delete(s.hashes, hash)
  115. }
  116. return sEntity, nil
  117. }