maps.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package csync
  2. import (
  3. "encoding/json"
  4. "iter"
  5. "maps"
  6. "sync"
  7. )
  8. // Map is a concurrent map implementation that provides thread-safe access.
  9. type Map[K comparable, V any] struct {
  10. inner map[K]V
  11. mu sync.RWMutex
  12. }
  13. // NewMap creates a new thread-safe map with the specified key and value types.
  14. func NewMap[K comparable, V any]() *Map[K, V] {
  15. return &Map[K, V]{
  16. inner: make(map[K]V),
  17. }
  18. }
  19. // NewMapFrom creates a new thread-safe map from an existing map.
  20. func NewMapFrom[K comparable, V any](m map[K]V) *Map[K, V] {
  21. return &Map[K, V]{
  22. inner: m,
  23. }
  24. }
  25. // NewLazyMap creates a new lazy-loaded map. The provided load function is
  26. // executed in a separate goroutine to populate the map.
  27. func NewLazyMap[K comparable, V any](load func() map[K]V) *Map[K, V] {
  28. m := &Map[K, V]{}
  29. m.mu.Lock()
  30. go func() {
  31. m.inner = load()
  32. m.mu.Unlock()
  33. }()
  34. return m
  35. }
  36. // Reset replaces the inner map with the new one.
  37. func (m *Map[K, V]) Reset(input map[K]V) {
  38. m.mu.Lock()
  39. defer m.mu.Unlock()
  40. m.inner = input
  41. }
  42. // Set sets the value for the specified key in the map.
  43. func (m *Map[K, V]) Set(key K, value V) {
  44. m.mu.Lock()
  45. defer m.mu.Unlock()
  46. m.inner[key] = value
  47. }
  48. // Del deletes the specified key from the map.
  49. func (m *Map[K, V]) Del(key K) {
  50. m.mu.Lock()
  51. defer m.mu.Unlock()
  52. delete(m.inner, key)
  53. }
  54. // Get gets the value for the specified key from the map.
  55. func (m *Map[K, V]) Get(key K) (V, bool) {
  56. m.mu.RLock()
  57. defer m.mu.RUnlock()
  58. v, ok := m.inner[key]
  59. return v, ok
  60. }
  61. // Len returns the number of items in the map.
  62. func (m *Map[K, V]) Len() int {
  63. m.mu.RLock()
  64. defer m.mu.RUnlock()
  65. return len(m.inner)
  66. }
  67. // GetOrSet gets and returns the key if it exists, otherwise, it executes the
  68. // given function, set its return value for the given key, and returns it.
  69. func (m *Map[K, V]) GetOrSet(key K, fn func() V) V {
  70. got, ok := m.Get(key)
  71. if ok {
  72. return got
  73. }
  74. value := fn()
  75. m.Set(key, value)
  76. return value
  77. }
  78. // Take gets an item and then deletes it.
  79. func (m *Map[K, V]) Take(key K) (V, bool) {
  80. m.mu.Lock()
  81. defer m.mu.Unlock()
  82. v, ok := m.inner[key]
  83. delete(m.inner, key)
  84. return v, ok
  85. }
  86. // Seq2 returns an iter.Seq2 that yields key-value pairs from the map.
  87. func (m *Map[K, V]) Seq2() iter.Seq2[K, V] {
  88. dst := make(map[K]V)
  89. m.mu.RLock()
  90. maps.Copy(dst, m.inner)
  91. m.mu.RUnlock()
  92. return func(yield func(K, V) bool) {
  93. for k, v := range dst {
  94. if !yield(k, v) {
  95. return
  96. }
  97. }
  98. }
  99. }
  100. // Seq returns an iter.Seq that yields values from the map.
  101. func (m *Map[K, V]) Seq() iter.Seq[V] {
  102. return func(yield func(V) bool) {
  103. for _, v := range m.Seq2() {
  104. if !yield(v) {
  105. return
  106. }
  107. }
  108. }
  109. }
  110. var (
  111. _ json.Unmarshaler = &Map[string, any]{}
  112. _ json.Marshaler = &Map[string, any]{}
  113. )
  114. func (Map[K, V]) JSONSchemaAlias() any { //nolint
  115. m := map[K]V{}
  116. return m
  117. }
  118. // UnmarshalJSON implements json.Unmarshaler.
  119. func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
  120. m.mu.Lock()
  121. defer m.mu.Unlock()
  122. m.inner = make(map[K]V)
  123. return json.Unmarshal(data, &m.inner)
  124. }
  125. // MarshalJSON implements json.Marshaler.
  126. func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
  127. m.mu.RLock()
  128. defer m.mu.RUnlock()
  129. return json.Marshal(m.inner)
  130. }