maps.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. defer m.mu.Unlock()
  32. m.inner = load()
  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. // Copy returns a copy of the inner map.
  87. func (m *Map[K, V]) Copy() map[K]V {
  88. m.mu.RLock()
  89. defer m.mu.RUnlock()
  90. return maps.Clone(m.inner)
  91. }
  92. // Seq2 returns an iter.Seq2 that yields key-value pairs from the map.
  93. func (m *Map[K, V]) Seq2() iter.Seq2[K, V] {
  94. dst := m.Copy()
  95. return func(yield func(K, V) bool) {
  96. for k, v := range dst {
  97. if !yield(k, v) {
  98. return
  99. }
  100. }
  101. }
  102. }
  103. // Seq returns an iter.Seq that yields values from the map.
  104. func (m *Map[K, V]) Seq() iter.Seq[V] {
  105. return func(yield func(V) bool) {
  106. for _, v := range m.Seq2() {
  107. if !yield(v) {
  108. return
  109. }
  110. }
  111. }
  112. }
  113. var (
  114. _ json.Unmarshaler = &Map[string, any]{}
  115. _ json.Marshaler = &Map[string, any]{}
  116. )
  117. func (Map[K, V]) JSONSchemaAlias() any { //nolint
  118. m := map[K]V{}
  119. return m
  120. }
  121. // UnmarshalJSON implements json.Unmarshaler.
  122. func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
  123. m.mu.Lock()
  124. defer m.mu.Unlock()
  125. m.inner = make(map[K]V)
  126. return json.Unmarshal(data, &m.inner)
  127. }
  128. // MarshalJSON implements json.Marshaler.
  129. func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
  130. m.mu.RLock()
  131. defer m.mu.RUnlock()
  132. return json.Marshal(m.inner)
  133. }