-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
184 lines (141 loc) · 3.16 KB
/
storage.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
package gostorage
import (
"github.com/link-yundi/ytools/ylog"
"sync"
)
/**
@Since 2022-12-02 13:34
@Author: Huang
@Description: save for multiple goroutines
**/
type MapL1 struct {
Lock sync.RWMutex
Map map[interface{}]interface{}
}
func NewMapL1() MapL1 {
return MapL1{
Lock: sync.RWMutex{},
Map: make(map[interface{}]interface{}, 0),
}
}
type MapL2 struct {
Lock sync.RWMutex
Map map[interface{}]map[interface{}]interface{}
}
func NewMapL2() MapL2 {
return MapL2{
Lock: sync.RWMutex{},
Map: make(map[interface{}]map[interface{}]interface{}, 0),
}
}
func (m *MapL1) Set(name interface{}, value interface{}) {
m.Lock.Lock()
defer m.Lock.Unlock()
if len(m.Map) == 0 {
m.Map = make(map[interface{}]interface{}, 0)
}
m.Map[name] = value
}
func (m *MapL1) Get(name interface{}) (interface{}, bool) {
m.Lock.RLock()
defer m.Lock.RUnlock()
if val, ok := m.Map[name]; ok {
return val, true
}
return nil, false
}
func (m *MapL1) Delete(name interface{}) {
m.Lock.Lock()
defer m.Lock.Unlock()
delete(m.Map, name)
}
func (m *MapL1) Clear() {
m.Lock.Lock()
defer m.Lock.Unlock()
m.Map = make(map[interface{}]interface{}, 0)
}
func (m *MapL1) Size() int {
m.Lock.RLock()
defer m.Lock.RUnlock()
return len(m.Map)
}
func (m *MapL1) Copy() map[interface{}]interface{} {
m.Lock.RLock()
defer m.Lock.RUnlock()
_copy := make(map[interface{}]interface{}, 0)
for k, v := range m.Map {
_copy[k] = v
}
return _copy
}
func (m2 *MapL2) Set(key1 interface{}, key2 interface{}, val interface{}) {
m2.Lock.Lock()
defer m2.Lock.Unlock()
if len(m2.Map) == 0 {
m2.Map = make(map[interface{}]map[interface{}]interface{}, 0)
}
if len(m2.Map[key1]) == 0 {
m2.Map[key1] = make(map[interface{}]interface{}, 0)
}
m2.Map[key1][key2] = val
}
func (m2 *MapL2) GetL2(key1 interface{}, key2 interface{}) (interface{}, bool) {
m2.Lock.RLock()
defer m2.Lock.RUnlock()
if val, ok := m2.Map[key1][key2]; ok {
return val, true
}
return nil, false
}
func (m2 *MapL2) GetL1(key1 interface{}) (map[interface{}]interface{}, bool) {
m2.Lock.RLock()
defer m2.Lock.RUnlock()
mLists, ok := m2.Map[key1]
if !ok {
return nil, false
}
_copy := make(map[interface{}]interface{}, 0)
for k, v := range mLists {
_copy[k] = v
}
return _copy, true
}
func (m2 *MapL2) Delete(keys ...interface{}) {
m2.Lock.Lock()
defer m2.Lock.Unlock()
switch len(keys) {
case 1:
delete(m2.Map, keys[0])
case 2:
delete(m2.Map[keys[0]], keys[1])
default:
ylog.Warnf("Params %v out of Bound, the mapL2 have no change", keys)
}
}
func (m2 *MapL2) Clear() {
m2.Lock.Lock()
defer m2.Lock.Unlock()
m2.Map = make(map[interface{}]map[interface{}]interface{}, 0)
}
func (m2 *MapL2) Size(key ...interface{}) int {
m2.Lock.RLock()
defer m2.Lock.RUnlock()
if key == nil {
return len(m2.Map)
} else {
return len(m2.Map[key[0]])
}
}
func (m2 *MapL2) Copy() map[interface{}]map[interface{}]interface{} {
m2.Lock.RLock()
defer m2.Lock.RUnlock()
_copy := make(map[interface{}]map[interface{}]interface{}, 0)
for k, v := range m2.Map {
_copyL2 := make(map[interface{}]interface{}, 0)
for key, val := range v {
_copyL2[key] = val
}
_copy[k] = _copyL2
}
return _copy
}