-
Notifications
You must be signed in to change notification settings - Fork 7
/
subscription_group_table.go
123 lines (101 loc) · 3.11 KB
/
subscription_group_table.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
package subscription
import (
"github.com/ttstringiot/golangiot/stgcommon"
syncmap "github.com/ttstringiot/golangiot/stgcommon/sync"
"sync"
)
type SubscriptionGroupTable struct {
SubscriptionGroupTable map[string]*SubscriptionGroupConfig `json:"subscriptionGroupTable"` // key:group
DataVersion stgcommon.DataVersion `json:"dataVersion"`
sync.RWMutex `json:"-"`
}
func NewSubscriptionGroupTable() *SubscriptionGroupTable {
subscriptionGroupTable := &SubscriptionGroupTable{
SubscriptionGroupTable: make(map[string]*SubscriptionGroupConfig, 1024),
DataVersion: *stgcommon.NewDataVersion(),
}
return subscriptionGroupTable
}
func (table *SubscriptionGroupTable) Size() int {
table.RLock()
defer table.RUnlock()
return len(table.SubscriptionGroupTable)
}
func (table *SubscriptionGroupTable) Put(k string, v *SubscriptionGroupConfig) *SubscriptionGroupConfig {
table.Lock()
defer table.Unlock()
old := table.SubscriptionGroupTable[k]
table.SubscriptionGroupTable[k] = v
return old
}
func (table *SubscriptionGroupTable) Get(k string) *SubscriptionGroupConfig {
table.RLock()
defer table.RUnlock()
v, ok := table.SubscriptionGroupTable[k]
if !ok {
return nil
}
return v
}
func (table *SubscriptionGroupTable) Remove(k string) *SubscriptionGroupConfig {
table.Lock()
defer table.Unlock()
v, ok := table.SubscriptionGroupTable[k]
if !ok {
return nil
}
delete(table.SubscriptionGroupTable, k)
return v
}
func (table *SubscriptionGroupTable) Foreach(fn func(k string, v *SubscriptionGroupConfig)) {
table.RLock()
defer table.RUnlock()
for k, v := range table.SubscriptionGroupTable {
fn(k, v)
}
}
// Clear 清空
// Author rongzhihong
// Since 2017/9/18
func (table *SubscriptionGroupTable) Clear() {
table.Lock()
defer table.Unlock()
table.SubscriptionGroupTable = make(map[string]*SubscriptionGroupConfig, 1024)
}
// syncTopicConfig 同步Topic配置文件
// Author rongzhihong
// Since 2017/9/18
func (table *SubscriptionGroupTable) PutAll(offsetMap *syncmap.Map) {
table.Lock()
defer table.Unlock()
if offsetMap == nil {
return
}
for iter := offsetMap.Iterator(); iter.HasNext(); {
key, value, _ := iter.Next()
if groupName, ok := key.(string); ok && groupName != "" {
if subscriptionGroupConfig, ok := value.(*SubscriptionGroupConfig); ok {
table.SubscriptionGroupTable[groupName] = subscriptionGroupConfig
}
}
}
}
// ClearAndPutAll 清空map,再PutAll
// Author rongzhihong
// Since 2017/9/18
func (table *SubscriptionGroupTable) ClearAndPutAll(offsetMap *syncmap.Map) {
table.Lock()
defer table.Unlock()
table.SubscriptionGroupTable = make(map[string]*SubscriptionGroupConfig, 1024)
if offsetMap == nil {
return
}
for iter := offsetMap.Iterator(); iter.HasNext(); {
key, value, _ := iter.Next()
if groupName, ok := key.(string); ok && groupName != "" {
if subscriptionGroupConfig, ok := value.(*SubscriptionGroupConfig); ok {
table.SubscriptionGroupTable[groupName] = subscriptionGroupConfig
}
}
}
}