|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package azuremonitorreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/azuremonitorreceiver" |
| 5 | + |
| 6 | +import ( |
| 7 | + "sync" |
| 8 | + |
| 9 | + cmap "github.com/orcaman/concurrent-map/v2" |
| 10 | +) |
| 11 | + |
| 12 | +type concurrentMetricsBuilderMap[V any] interface { |
| 13 | + Get(string) (V, bool) |
| 14 | + Set(string, V) |
| 15 | + Clear() |
| 16 | + Range(func(string, V)) |
| 17 | +} |
| 18 | + |
| 19 | +// Implementation with concurrent-map (generic API) |
| 20 | +type concurrentMapImpl[V any] struct { |
| 21 | + m cmap.ConcurrentMap[string, V] |
| 22 | +} |
| 23 | + |
| 24 | +func newConcurrentMapImpl[V any]() concurrentMetricsBuilderMap[V] { |
| 25 | + return &concurrentMapImpl[V]{m: cmap.New[V]()} |
| 26 | +} |
| 27 | + |
| 28 | +func (c *concurrentMapImpl[V]) Get(key string) (V, bool) { |
| 29 | + return c.m.Get(key) |
| 30 | +} |
| 31 | + |
| 32 | +func (c *concurrentMapImpl[V]) Set(key string, value V) { |
| 33 | + c.m.Set(key, value) |
| 34 | +} |
| 35 | + |
| 36 | +func (c *concurrentMapImpl[V]) Clear() { |
| 37 | + c.m.Clear() |
| 38 | +} |
| 39 | + |
| 40 | +func (c *concurrentMapImpl[V]) Range(f func(string, V)) { |
| 41 | + c.m.IterCb(f) |
| 42 | +} |
| 43 | + |
| 44 | +// Implementation with sync.Map |
| 45 | + |
| 46 | +type syncMapImpl[V any] struct { |
| 47 | + m sync.Map |
| 48 | +} |
| 49 | + |
| 50 | +func NewSyncMapImpl[V any]() concurrentMetricsBuilderMap[V] { |
| 51 | + return &syncMapImpl[V]{} |
| 52 | +} |
| 53 | + |
| 54 | +func (s *syncMapImpl[V]) Get(key string) (V, bool) { |
| 55 | + v, ok := s.m.Load(key) |
| 56 | + if !ok { |
| 57 | + var zero V |
| 58 | + return zero, false |
| 59 | + } |
| 60 | + return v.(V), true |
| 61 | +} |
| 62 | + |
| 63 | +func (s *syncMapImpl[V]) Set(key string, value V) { |
| 64 | + s.m.Store(key, value) |
| 65 | +} |
| 66 | + |
| 67 | +func (s *syncMapImpl[V]) Clear() { |
| 68 | + s.m.Range(func(k, _ any) bool { |
| 69 | + s.m.Delete(k) |
| 70 | + return true |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func (s *syncMapImpl[V]) Range(f func(string, V)) { |
| 75 | + s.m.Range(func(k, v any) bool { |
| 76 | + f(k.(string), v.(V)) |
| 77 | + return true |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +// Implementation with classic map and mutex |
| 82 | + |
| 83 | +type mutexMapImpl[V any] struct { |
| 84 | + m map[string]V |
| 85 | + mutex sync.RWMutex |
| 86 | +} |
| 87 | + |
| 88 | +func NewMutexMapImpl[V any]() concurrentMetricsBuilderMap[V] { |
| 89 | + return &mutexMapImpl[V]{m: make(map[string]V)} |
| 90 | +} |
| 91 | + |
| 92 | +func (mm *mutexMapImpl[V]) Get(key string) (V, bool) { |
| 93 | + mm.mutex.RLock() |
| 94 | + defer mm.mutex.RUnlock() |
| 95 | + v, ok := mm.m[key] |
| 96 | + return v, ok |
| 97 | +} |
| 98 | + |
| 99 | +func (mm *mutexMapImpl[V]) Set(key string, value V) { |
| 100 | + mm.mutex.Lock() |
| 101 | + defer mm.mutex.Unlock() |
| 102 | + mm.m[key] = value |
| 103 | +} |
| 104 | + |
| 105 | +func (mm *mutexMapImpl[V]) Clear() { |
| 106 | + mm.mutex.Lock() |
| 107 | + defer mm.mutex.Unlock() |
| 108 | + mm.m = make(map[string]V) |
| 109 | +} |
| 110 | + |
| 111 | +func (mm *mutexMapImpl[V]) Range(f func(string, V)) { |
| 112 | + mm.mutex.RLock() |
| 113 | + defer mm.mutex.RUnlock() |
| 114 | + for k, v := range mm.m { |
| 115 | + f(k, v) |
| 116 | + } |
| 117 | +} |
0 commit comments