-
Notifications
You must be signed in to change notification settings - Fork 13
/
ccmap.go
204 lines (173 loc) · 3.76 KB
/
ccmap.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
package fasthttpsession
import (
"sync"
)
var (
// default slice number 32
defaultSliceNumber = 32
)
// Concurrent Map
type CCMap struct {
sliceNumber int
values []*MapSlice
}
// Map slice
type MapSlice struct {
lock sync.RWMutex
items map[string]interface{}
}
// New default slice number ccMap
func NewDefaultCCMap() *CCMap {
return NewCCMap(defaultSliceNumber)
}
// New slice number ccMap
func NewCCMap(sliceNumber int) *CCMap {
ccMap := &CCMap{
sliceNumber: sliceNumber,
values: make([]*MapSlice, sliceNumber),
}
for i := 0; i < sliceNumber; i++ {
ccMap.values[i] = &MapSlice{
items: make(map[string]interface{}),
}
}
return ccMap
}
func fnvHash(key string) uint32 {
hash := uint32(2166136261)
const prime32 = uint32(16777619)
for i := 0; i < len(key); i++ {
hash *= prime32
hash ^= uint32(key[i])
}
return hash
}
// get slice key
func (c *CCMap) GetSliceMap(key string) *MapSlice {
return c.values[uint(fnvHash(key))%uint(c.sliceNumber)]
}
// key is exist
func (c *CCMap) IsExist(key string) bool {
sliceMap := c.GetSliceMap(key)
sliceMap.lock.RLock()
_, ok := sliceMap.items[key]
sliceMap.lock.RUnlock()
return ok
}
// set key value
func (c *CCMap) Set(key string, value interface{}) {
sliceMap := c.GetSliceMap(key)
sliceMap.lock.Lock()
sliceMap.items[key] = value
sliceMap.lock.Unlock()
}
// get by key
func (c *CCMap) Get(key string) interface{} {
sliceMap := c.GetSliceMap(key)
sliceMap.lock.RLock()
value, ok := sliceMap.items[key]
if ok {
sliceMap.lock.RUnlock()
return value
}
sliceMap.lock.RUnlock()
return nil
}
// delete by key
func (c *CCMap) Delete(key string) {
sliceMap := c.GetSliceMap(key)
sliceMap.lock.Lock()
delete(sliceMap.items, key)
sliceMap.lock.Unlock()
}
// update by key
// if key exist, update value
func (c *CCMap) Update(key string, value interface{}) {
sliceMap := c.GetSliceMap(key)
sliceMap.lock.RLock()
value, ok := sliceMap.items[key]
if ok {
sliceMap.lock.RUnlock()
sliceMap.lock.Lock()
sliceMap.items[key] = value
sliceMap.lock.Unlock()
return
}
sliceMap.lock.RUnlock()
}
// replace
// if key exist, update value.
// if key not exist, insert value.
func (c *CCMap) Replace(key string, value interface{}) {
sliceMap := c.GetSliceMap(key)
sliceMap.lock.Lock()
sliceMap.items[key] = value
sliceMap.lock.Unlock()
}
// multiple set
func (c *CCMap) MSet(data map[string]interface{}) {
for key, value := range data {
c.Set(key, value)
}
}
// multiple get by keys
func (c *CCMap) MGet(keys ...string) map[string]interface{} {
data := make(map[string]interface{})
for _, key := range keys {
val := c.Get(key)
data[key] = val
}
return data
}
// get value by key and delete key
func (c *CCMap) GetOnce(key string) interface{} {
val := c.Get(key)
c.Delete(key)
return val
}
// get all values
func (c *CCMap) GetAll() map[string]interface{} {
data := make(map[string]interface{})
for i := 0; i < c.sliceNumber; i++ {
sliceMap := c.values[i]
sliceMap.lock.RLock()
for key, value := range sliceMap.items {
data[key] = value
}
sliceMap.lock.RUnlock()
}
return data
}
// clear all values
func (c *CCMap) Clear() {
for i := 0; i < c.sliceNumber; i++ {
sliceMap := c.values[i]
sliceMap.lock.Lock()
c.values[i].items = make(map[string]interface{})
sliceMap.lock.Unlock()
}
}
// get all keys
func (c *CCMap) Keys() []string {
data := []string{}
for i := 0; i < c.sliceNumber; i++ {
sliceMap := c.values[i]
sliceMap.lock.RLock()
for key, _ := range sliceMap.items {
data = append(data, key)
}
sliceMap.lock.RUnlock()
}
return data
}
// values count
func (c *CCMap) Count() int {
count := 0
for i := 0; i < c.sliceNumber; i++ {
sliceMap := c.values[i]
sliceMap.lock.RLock()
count += len(sliceMap.items)
sliceMap.lock.RUnlock()
}
return count
}