forked from yuly16/MarketPeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
43 lines (36 loc) · 806 Bytes
/
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
package chord
import "sync"
type ChordStorage struct {
sync.Mutex
data map[uint]interface{}
}
func (c *ChordStorage) get(key uint) (interface{}, bool) {
c.Lock()
defer c.Unlock()
data, ok := c.data[key]
return data, ok
}
func (c *ChordStorage) put(key uint, data interface{}) {
c.Lock()
defer c.Unlock()
c.data[key] = data
}
func (c *ChordStorage) findBetweenRightInclude(begin uint, end uint) map[uint]interface{} {
res := map[uint]interface{}{}
c.Lock()
defer c.Unlock()
for key, value := range c.data {
if betweenRightInclude(key, begin, end) {
res[key] = value
}
}
return res
}
func (c *ChordStorage) deleteBetweenRightInclude(begin uint, end uint) {
kv := c.findBetweenRightInclude(begin, end)
c.Lock()
defer c.Unlock()
for key := range kv {
delete(c.data, key)
}
}