-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsafemap.go
177 lines (159 loc) · 3.68 KB
/
safemap.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
package util
type smap struct {
m map[interface{}]interface{}
readSig chan *readReq
writeSig chan *writeReq
lenSig chan *lenReq
terminateSig chan bool
delSig chan *delReq
scanSig chan *scanReq
}
type readReq struct {
key interface{}
value interface{}
ok chan bool
}
type writeReq struct {
key interface{}
value interface{}
ok chan bool
}
type lenReq struct {
len chan int
}
type delReq struct {
key interface{}
ok chan bool
}
type scanReq struct {
do func(interface{}, interface{})
doWithBreak func(interface{}, interface{}) bool
brea int
done chan bool
}
// NewSmap returns an instance of the pointer of safemap
func NewSmap() *smap {
var mp smap
mp.m = make(map[interface{}]interface{})
mp.readSig = make(chan *readReq)
mp.writeSig = make(chan *writeReq)
mp.lenSig = make(chan *lenReq)
mp.delSig = make(chan *delReq)
mp.scanSig = make(chan *scanReq)
go mp.run()
return &mp
}
//background function to operate map in one goroutine
//this can ensure that the map is Concurrent security.
func (s *smap) run() {
for {
select {
case read := <-s.readSig:
if value, ok := s.m[read.key]; ok {
read.value = value
read.ok <- true
} else {
read.ok <- false
}
case write := <-s.writeSig:
s.m[write.key] = write.value
write.ok <- true
case l := <-s.lenSig:
l.len <- len(s.m)
case sc := <-s.scanSig:
if sc.brea == 0 {
for k, v := range s.m {
sc.do(k, v)
}
} else {
for k, v := range s.m {
ret := sc.doWithBreak(k, v)
if ret {
break
}
}
}
sc.done <- true
case d := <-s.delSig:
delete(s.m, d.key)
d.ok <- true
case <-s.terminateSig:
return
}
}
}
//Get returns the value of key which provided.
//if the key not found in map, ok will be false.
func (s *smap) Get(key interface{}) (interface{}, bool) {
req := &readReq{
key: key,
ok: make(chan bool),
}
s.readSig <- req
ok := <-req.ok
return req.value, ok
}
//Set set the key and value to map
//ok returns true indicates that key and value is successfully added to map
func (s *smap) Set(key interface{}, value interface{}) bool {
req := &writeReq{
key: key,
value: value,
ok: make(chan bool),
}
s.writeSig <- req
return <-req.ok //TODO 暂时先是同步的,异步的可能存在使用方面的问题。
}
//Clear clears all the key and value in map.
func (s *smap) Clear() {
s.m = make(map[interface{}]interface{})
}
//Size returns the size of map.
func (s *smap) Size() int {
req := &lenReq{
len: make(chan int),
}
s.lenSig <- req
return <-req.len
}
//terminate s.Run function. this function is usually called for debug.
//after this do NOT use smap again, because it can make your program block.
func (s *smap) TerminateBackGoroutine() {
s.terminateSig <- true
}
//Del delete the key in map
func (s *smap) Del(key interface{}) bool {
req := &delReq{
key: key,
ok: make(chan bool),
}
s.delSig <- req
return <-req.ok
}
//scan the map. do is a function which operate all of the key and value in map
func (s *smap) EachItem(do func(interface{}, interface{})) {
req := &scanReq{
do: do,
brea: 0,
done: make(chan bool),
}
s.scanSig <- req
<-req.done
}
//scan the map util function 'do' returns true. do is a function which operate all of the key and value in map
func (s *smap) EachItemBreak(do func(interface{}, interface{}) bool, condition bool) {
req := &scanReq{
doWithBreak: do,
brea: 1,
done: make(chan bool),
}
s.scanSig <- req
<-req.done
}
//Exists checks whether the key which provided is exists in map
func (s *smap) Exists(key interface{}) bool {
if _,found := s.Get(key); found {
return true
}
return false
}