-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientset_atomicmap.go
81 lines (66 loc) · 1.74 KB
/
clientset_atomicmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Generated by: main
// TypeWriter: atomicmap
// Directive: +gen on ClientSet
package middleman
import (
"sync"
"sync/atomic"
)
// ClientSetAtomicMap is a copy-on-write thread-safe map of ClientSet
type ClientSetAtomicMap struct {
mu sync.Mutex
val atomic.Value
}
type _ClientSetMap map[string]ClientSet
// NewClientSetAtomicMap returns a new initialized ClientSetAtomicMap
func NewClientSetAtomicMap() *ClientSetAtomicMap {
am := &ClientSetAtomicMap{}
am.val.Store(make(_ClientSetMap, 0))
return am
}
// Get returns a ClientSet for a given key
func (am *ClientSetAtomicMap) Get(key string) (value ClientSet, ok bool) {
value, ok = am.val.Load().(_ClientSetMap)[key]
return value, ok
}
// GetAll returns the underlying map of ClientSet
// this map must NOT be modified, to change the map safely use the Set and Delete
// functions and Get the value again
func (am *ClientSetAtomicMap) GetAll() map[string]ClientSet {
return am.val.Load().(_ClientSetMap)
}
// Len returns the number of elements in the map
func (am *ClientSetAtomicMap) Len() int {
return len(am.val.Load().(_ClientSetMap))
}
// Set inserts in the map a ClientSet under a given key
func (am *ClientSetAtomicMap) Set(key string, value ClientSet) {
am.mu.Lock()
defer am.mu.Unlock()
m1 := am.val.Load().(_ClientSetMap)
m2 := make(_ClientSetMap, len(m1)+1)
for k, v := range m1 {
m2[k] = v
}
m2[key] = value
am.val.Store(m2)
return
}
// Delete removes the ClientSet under key from the map
func (am *ClientSetAtomicMap) Delete(key string) {
am.mu.Lock()
defer am.mu.Unlock()
m1 := am.val.Load().(_ClientSetMap)
_, ok := m1[key]
if !ok {
return
}
m2 := make(_ClientSetMap, len(m1)-1)
for k, v := range m1 {
if k != key {
m2[k] = v
}
}
am.val.Store(m2)
return
}