-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_int_fd_info.go
371 lines (329 loc) · 9.24 KB
/
gen_int_fd_info.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Code generated by gotemplate. DO NOT EDIT.
package xlistener
import (
"sync"
)
//template type Concurrent(KType,VType,KeyHash)
// A thread safe map.
// To avoid lock bottlenecks this map is dived to several (DefaultShardCount) map shards.
var DefaultShardCountIntFdInfo = uint64(32)
type IntFdInfo struct {
shardedList []*shardedIntFdInfo
shardedCount uint64
}
type shardedIntFdInfo struct {
items map[int]*fdInfo
sync.RWMutex
}
// Tuple Used by the Iter & IterBuffered functions to wrap two variables together over a channel,
type TupleIntFdInfo struct {
Key int
Val *fdInfo
}
// NewWithSharedCount 返回协程安全版本
func NewWithSharedCountIntFdInfo(sharedCount uint64) *IntFdInfo {
p := &IntFdInfo{
shardedCount: sharedCount,
shardedList: make([]*shardedIntFdInfo, sharedCount),
}
for i := uint64(0); i < sharedCount; i++ {
p.shardedList[i] = &shardedIntFdInfo{items: make(map[int]*fdInfo)}
}
return p
}
// New 返回协程安全版本
func NewIntFdInfo() *IntFdInfo {
return NewWithSharedCountIntFdInfo(DefaultShardCountIntFdInfo)
}
// GetShard 返回key对应的分片
func (m *IntFdInfo) GetShard(key int) *shardedIntFdInfo {
return m.shardedList[func(k int) uint64 {
return uint64(k)
}(key)%m.shardedCount]
}
// IsEmpty 返回容器是否为空
func (m *IntFdInfo) IsEmpty() bool {
return m.Count() == 0
}
// Set 设定元素
func (m *IntFdInfo) Set(key int, value *fdInfo) {
shard := m.GetShard(key)
shard.Lock()
shard.items[key] = value
shard.Unlock()
}
// Keys 返回所有的key列表
func (m *IntFdInfo) Keys() []int {
var ret []int
for _, shard := range m.shardedList {
shard.RLock()
for key := range shard.items {
ret = append(ret, key)
}
shard.RUnlock()
}
return ret
}
// GetAll 返回所有元素副本,其中value浅拷贝
func (m *IntFdInfo) GetAll() map[int]*fdInfo {
data := make(map[int]*fdInfo)
for _, shard := range m.shardedList {
shard.RLock()
for key, val := range shard.items {
data[key] = val
}
shard.RUnlock()
}
return data
}
// Clear 清空元素
func (m *IntFdInfo) Clear() {
for _, shard := range m.shardedList {
shard.Lock()
shard.items = make(map[int]*fdInfo)
shard.Unlock()
}
}
// ClearWithFunc 清空元素,onClear在对应分片的Lock内执行,执行完毕后对容器做整体clear操作
//
// Note: 不要在onClear对当前容器做读写操作,容易死锁
//
// data.ClearWithFuncLock(func(key string,val string) {
// data.Get(...) // 死锁
// })
//
func (m *IntFdInfo) ClearWithFuncLock(onClear func(key int, val *fdInfo)) {
for _, shard := range m.shardedList {
shard.Lock()
for key, val := range shard.items {
onClear(key, val)
}
shard.items = make(map[int]*fdInfo)
shard.Unlock()
}
}
// MGet 返回多个元素
func (m *IntFdInfo) MGet(keys ...int) map[int]*fdInfo {
data := make(map[int]*fdInfo)
for _, key := range keys {
if val, ok := m.Get(key); ok {
data[key] = val
}
}
return data
}
// MSet 同时设定多个元素
func (m *IntFdInfo) MSet(data map[int]*fdInfo) {
for key, value := range data {
m.Set(key, value)
}
}
// SetNX 如果key不存在,则设定为value, 设定成功则返回true,否则返回false
func (m *IntFdInfo) SetNX(key int, value *fdInfo) (isSet bool) {
shard := m.GetShard(key)
shard.Lock()
if _, ok := shard.items[key]; !ok {
shard.items[key] = value
isSet = true
}
shard.Unlock()
return isSet
}
// LockFuncWithKey 对key对应的分片加写锁,并用f操作该分片内数据
//
// Note: 不要对f中对容器的该分片做读写操作,可以直接操作shardData数据源
//
// data.LockFuncWithKey("test",func(shardData map[string]string) {
// data.Remove("test") // 当前分片已被加读锁, 死锁
// })
//
func (m *IntFdInfo) LockFuncWithKey(key int, f func(shardData map[int]*fdInfo)) {
shard := m.GetShard(key)
shard.Lock()
defer shard.Unlock()
f(shard.items)
}
// RLockFuncWithKey 对key对应的分片加读锁,并用f操作该分片内数据
//
// Note: 不要在f内对容器做写操作,否则会引起死锁,可以直接操作shardData数据源
//
// data.RLockFuncWithKey("test",func(shardData map[string]string) {
// data.Remove("test") // 当前分片已被加读锁, 死锁
// })
//
func (m *IntFdInfo) RLockFuncWithKey(key int, f func(shardData map[int]*fdInfo)) {
shard := m.GetShard(key)
shard.RLock()
defer shard.RUnlock()
f(shard.items)
}
// LockFunc 遍历容器分片,f在Lock写锁内执行
//
// Note: 不要在f内对容器做读写操作,否则会引起死锁,可以直接操作shardData数据源
//
// data.LockFunc(func(shardData map[string]string) {
// data.Count() // 当前分片已被加写锁, 死锁
// })
//
func (m *IntFdInfo) LockFunc(f func(shardData map[int]*fdInfo)) {
for _, shard := range m.shardedList {
shard.Lock()
f(shard.items)
shard.Unlock()
}
}
// RLockFunc 遍历容器分片,f在RLock读锁内执行
//
// Note: 不要在f内对容器做修改操作,否则会引起死锁,可以直接操作shardData数据源
//
// data.RLockFunc(func(shardData map[string]string) {
// data.Remove("test") // 当前分片已被加读锁, 死锁
// })
//
func (m *IntFdInfo) RLockFunc(f func(shardData map[int]*fdInfo)) {
for _, shard := range m.shardedList {
shard.RLock()
f(shard.items)
shard.RUnlock()
}
}
func (m *IntFdInfo) doSetWithLockCheck(key int, val *fdInfo) (result *fdInfo, isSet bool) {
shard := m.GetShard(key)
shard.Lock()
if got, ok := shard.items[key]; ok {
shard.Unlock()
return got, false
}
shard.items[key] = val
isSet = true
shard.Unlock()
return
}
func (m *IntFdInfo) doSetWithLockCheckWithFunc(key int, f func(key int) *fdInfo) (result *fdInfo, isSet bool) {
shard := m.GetShard(key)
shard.Lock()
if got, ok := shard.items[key]; ok {
shard.Unlock()
return got, false
}
shard.items[key] = f(key)
isSet = true
shard.Unlock()
return
}
// GetOrSetFunc 获取或者设定数值,方法f在Lock写锁外执行, 如元素早已存在则返回false,设定成功返回true
func (m *IntFdInfo) GetOrSetFunc(key int, f func(key int) *fdInfo) (result *fdInfo, isSet bool) {
if v, ok := m.Get(key); ok {
return v, false
}
return m.doSetWithLockCheck(key, f(key))
}
// GetOrSetFuncLock 获取或者设定数值,方法f在Lock写锁内执行, 如元素早已存在则返回false,设定成功返回true
//
// Note: 不要在f内对容器做操作,否则会死锁
//
// data.GetOrSetFuncLock(“test”,func(key string)string {
// data.Count() // 死锁
// })
//
func (m *IntFdInfo) GetOrSetFuncLock(key int, f func(key int) *fdInfo) (result *fdInfo, isSet bool) {
if v, ok := m.Get(key); ok {
return v, false
}
return m.doSetWithLockCheckWithFunc(key, f)
}
// GetOrSet 获取或设定元素, 如元素早已存在则返回false,设定成功返回true
func (m *IntFdInfo) GetOrSet(key int, val *fdInfo) (*fdInfo, bool) {
if v, ok := m.Get(key); ok {
return v, false
}
return m.doSetWithLockCheck(key, val)
}
// Get 返回key对应的元素,不存在返回false
func (m *IntFdInfo) Get(key int) (*fdInfo, bool) {
shard := m.GetShard(key)
shard.RLock()
val, ok := shard.items[key]
shard.RUnlock()
return val, ok
}
// Len Count方法别名
func (m *IntFdInfo) Len() int { return m.Count() }
// Size Count方法别名
func (m *IntFdInfo) Size() int { return m.Count() }
// Count 返回容器内元素数量
func (m *IntFdInfo) Count() int {
count := 0
for i := uint64(0); i < m.shardedCount; i++ {
shard := m.shardedList[i]
shard.RLock()
count += len(shard.items)
shard.RUnlock()
}
return count
}
// Has 是否存在key对应的元素
func (m *IntFdInfo) Has(key int) bool {
shard := m.GetShard(key)
shard.RLock()
_, ok := shard.items[key]
shard.RUnlock()
return ok
}
// Remove 删除key对应的元素
func (m *IntFdInfo) Remove(key int) {
shard := m.GetShard(key)
shard.Lock()
delete(shard.items, key)
shard.Unlock()
}
// GetAndRemove 返回key对应的元素并将其由容器中删除,如果元素不存在则返回false
func (m *IntFdInfo) GetAndRemove(key int) (*fdInfo, bool) {
shard := m.GetShard(key)
shard.Lock()
val, ok := shard.items[key]
delete(shard.items, key)
shard.Unlock()
return val, ok
}
// Iter 迭代当前容器内所有元素,使用无缓冲chan
//
// Note: 不要在迭代过程中对当前容器作修改操作(申请写锁),容易会产生死锁
//
// for v:= data.Iter() {
// data.Remove(v.Key) // 尝试删除元素申请分片Lock,但是Iter内部的迭代协程对分片做了RLock,导致死锁
// }
//
func (m *IntFdInfo) Iter() <-chan TupleIntFdInfo {
ch := make(chan TupleIntFdInfo)
go func() {
// Foreach shard.
for _, shard := range m.shardedList {
shard.RLock()
// Foreach key, value pair.
for key, val := range shard.items {
ch <- TupleIntFdInfo{key, val}
}
shard.RUnlock()
}
close(ch)
}()
return ch
}
// IterBuffered 迭代当前容器内所有元素,使用有缓冲chan,缓冲区大小等于容器大小,迭代过程中操作容器是安全的
func (m *IntFdInfo) IterBuffered() <-chan TupleIntFdInfo {
ch := make(chan TupleIntFdInfo, m.Count())
go func() {
// Foreach shard.
for _, shard := range m.shardedList {
// Foreach key, value pair.
shard.RLock()
for key, val := range shard.items {
ch <- TupleIntFdInfo{key, val}
}
shard.RUnlock()
}
close(ch)
}()
return ch
}