-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlmap.go
163 lines (141 loc) · 3.38 KB
/
lmap.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// +build genx
package cmap
import "sync"
// LMap is a simple sync.RWMutex locked map.
// Used by CMap internally for sharding.
type LMap struct {
m map[KT]VT
l *sync.RWMutex
}
// NewLMap returns a new LMap with the cap set to 0.
func NewLMap() *LMap {
return NewLMapSize(0)
}
// NewLMapSize is the equivalent of `m := make(map[KT]VT, cap)`
func NewLMapSize(cap int) *LMap {
return &LMap{
m: make(map[KT]VT, cap),
l: new(sync.RWMutex),
}
}
// Set is the equivalent of `map[key] = val`.
func (lm *LMap) Set(key KT, v VT) {
lm.l.Lock()
lm.m[key] = v
lm.l.Unlock()
}
// SetIfNotExists will only assign val to key if it wasn't already set.
// Use `Update` if you need more logic.
func (lm *LMap) SetIfNotExists(key KT, val VT) (set bool) {
lm.l.Lock()
if _, ok := lm.m[key]; !ok {
lm.m[key], set = val, true
}
lm.l.Unlock()
return
}
// Get is the equivalent of `val := map[key]`.
func (lm *LMap) Get(key KT) (v VT) {
lm.l.RLock()
v = lm.m[key]
lm.l.RUnlock()
return
}
// GetOK is the equivalent of `val, ok := map[key]`.
func (lm *LMap) GetOK(key KT) (v VT, ok bool) {
lm.l.RLock()
v, ok = lm.m[key]
lm.l.RUnlock()
return
}
// Has is the equivalent of `_, ok := map[key]`.
func (lm *LMap) Has(key KT) (ok bool) {
lm.l.RLock()
_, ok = lm.m[key]
lm.l.RUnlock()
return
}
// Delete is the equivalent of `delete(map, key)`.
func (lm *LMap) Delete(key KT) {
lm.l.Lock()
delete(lm.m, key)
lm.l.Unlock()
}
// DeleteAndGet is the equivalent of `oldVal := map[key]; delete(map, key)`.
func (lm *LMap) DeleteAndGet(key KT) (v VT) {
lm.l.Lock()
v = lm.m[key]
delete(lm.m, key)
lm.l.Unlock()
return v
}
// Update calls `fn` with the key's old value (or nil) and assigns the returned value to the key.
// The shard containing the key will be locked, it is NOT safe to call other cmap funcs inside `fn`.
func (lm *LMap) Update(key KT, fn func(oldVal VT) (newVal VT)) {
lm.l.Lock()
lm.m[key] = fn(lm.m[key])
lm.l.Unlock()
}
// Swap is the equivalent of `oldVal, map[key] = map[key], newVal`.
func (lm *LMap) Swap(key KT, newV VT) (oldV VT) {
lm.l.Lock()
oldV = lm.m[key]
lm.m[key] = newV
lm.l.Unlock()
return
}
// ForEach loops over all the key/values in the map.
// You can break early by returning an error .
// It **is** safe to modify the map while using this iterator, however it uses more memory and is slightly slower.
func (lm *LMap) ForEach(keys []KT, fn func(key KT, val VT) bool) bool {
lm.l.RLock()
for key := range lm.m {
keys = append(keys, key)
}
lm.l.RUnlock()
for _, key := range keys {
lm.l.RLock()
val, ok := lm.m[key]
lm.l.RUnlock()
if !ok {
continue
}
if !fn(key, val) {
return false
}
}
return true
}
// ForEachLocked loops over all the key/values in the map.
// You can break early by returning false
// It is **NOT* safe to modify the map while using this iterator.
func (lm *LMap) ForEachLocked(fn func(key KT, val VT) bool) bool {
lm.l.RLock()
defer lm.l.RUnlock()
for key, val := range lm.m {
if !fn(key, val) {
return false
}
}
return true
}
// Len returns the length of the map.
func (lm *LMap) Len() (ln int) {
lm.l.RLock()
ln = len(lm.m)
lm.l.RUnlock()
return
}
// Keys appends all the keys in the map to buf and returns buf.
// buf may be nil.
func (lm *LMap) Keys(buf []KT) []KT {
lm.l.RLock()
if cap(buf) == 0 {
buf = make([]KT, 0, len(lm.m))
}
for k := range lm.m {
buf = append(buf, k)
}
lm.l.RUnlock()
return buf
}