forked from miolini/mapstore
/
mapstore.go
102 lines (88 loc) · 1.88 KB
/
mapstore.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
package mapstore
import (
"github.com/spaolacci/murmur3"
"sync"
)
const defaultShardsCount = 1024
type _shard struct {
data map[string]interface{}
mutex sync.RWMutex
}
func newShard() *_shard {
s := new(_shard)
s.data = make(map[string]interface{})
return s
}
type Store struct {
shards []*_shard
shardsCount int
mutex sync.RWMutex
}
func NewWithSize(shardsCount int) *Store {
s := new(Store)
s.mutex.Lock()
s.shardsCount = shardsCount
s.shards = make([]*_shard, s.shardsCount)
for i := 0; i < shardsCount; i++ {
s.shards[i] = newShard()
}
s.mutex.Unlock()
return s
}
func New() *Store {
return NewWithSize(defaultShardsCount)
}
func (s *Store) getShard(key string) *_shard {
s.mutex.RLock()
num := int(murmur3.Sum64([]byte(key))>>1) % s.shardsCount
shard := s.shards[num]
s.mutex.RUnlock()
return shard
}
func (s *Store) Set(key string, value interface{}) {
shard := s.getShard(key)
shard.mutex.Lock()
shard.data[key] = value
shard.mutex.Unlock()
}
func (s *Store) Get(key string, defaultValue interface{}) (interface{}, bool) {
shard := s.getShard(key)
shard.mutex.RLock()
result, ok := shard.data[key]
shard.mutex.RUnlock()
if !ok {
result = defaultValue
}
return result, ok
}
func (s *Store) ShardStats() []int {
result := make([]int, s.shardsCount)
for i := 0; i < s.shardsCount; i++ {
shard := s.shards[i]
shard.mutex.RLock()
result[i] = len(shard.data)
shard.mutex.RUnlock()
}
return result
}
type Entry struct {
Key string
Value interface{}
}
func (s *Store) Load(entries chan Entry) {
s.mutex.Lock()
defer s.mutex.Unlock()
for entry := range entries {
shard := s.getShard(entry.Key)
shard.data[entry.Key] = entry.Value
}
}
func (s *Store) Save(entries chan<- Entry) {
s.mutex.RLock()
defer s.mutex.RUnlock()
for _, shard := range s.shards {
for key, value := range shard.data {
entries <- Entry{key, value}
}
}
}