-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmap.go
181 lines (166 loc) · 4.37 KB
/
map.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
package shardmap
import (
"runtime"
"sync"
"github.com/cespare/xxhash"
"github.com/tidwall/rhh"
)
// Map is a hashmap. Like map[string]interface{}, but sharded and thread-safe.
type Map struct {
init sync.Once
cap int
shards int
seed uint32
mus []sync.RWMutex
maps []*rhh.Map
}
// New returns a new hashmap with the specified capacity. This function is only
// needed when you must define a minimum capacity, otherwise just use:
// var m shardmap.Map
func New(cap int) *Map {
return &Map{cap: cap}
}
// Clear out all values from map
func (m *Map) Clear() {
m.initDo()
for i := 0; i < m.shards; i++ {
m.mus[i].Lock()
m.maps[i] = rhh.New(m.cap / m.shards)
m.mus[i].Unlock()
}
}
// Set assigns a value to a key.
// Returns the previous value, or false when no value was assigned.
func (m *Map) Set(key string, value interface{}) (prev interface{}, replaced bool) {
m.initDo()
shard := m.choose(key)
m.mus[shard].Lock()
prev, replaced = m.maps[shard].Set(key, value)
m.mus[shard].Unlock()
return prev, replaced
}
// SetAccept assigns a value to a key. The "accept" function can be used to
// inspect the previous value, if any, and accept or reject the change.
// It's also provides a safe way to block other others from writing to the
// same shard while inspecting.
// Returns the previous value, or false when no value was assigned.
func (m *Map) SetAccept(
key string, value interface{},
accept func(prev interface{}, replaced bool) bool,
) (prev interface{}, replaced bool) {
m.initDo()
shard := m.choose(key)
m.mus[shard].Lock()
defer m.mus[shard].Unlock()
prev, replaced = m.maps[shard].Set(key, value)
if accept != nil {
if !accept(prev, replaced) {
// revert unaccepted change
if !replaced {
// delete the newly set data
m.maps[shard].Delete(key)
} else {
// reset updated data
m.maps[shard].Set(key, prev)
}
prev, replaced = nil, false
}
}
return prev, replaced
}
// Get returns a value for a key.
// Returns false when no value has been assign for key.
func (m *Map) Get(key string) (value interface{}, ok bool) {
m.initDo()
shard := m.choose(key)
m.mus[shard].RLock()
value, ok = m.maps[shard].Get(key)
m.mus[shard].RUnlock()
return value, ok
}
// Delete deletes a value for a key.
// Returns the deleted value, or false when no value was assigned.
func (m *Map) Delete(key string) (prev interface{}, deleted bool) {
m.initDo()
shard := m.choose(key)
m.mus[shard].Lock()
prev, deleted = m.maps[shard].Delete(key)
m.mus[shard].Unlock()
return prev, deleted
}
// DeleteAccept deletes a value for a key. The "accept" function can be used to
// inspect the previous value, if any, and accept or reject the change.
// It's also provides a safe way to block other others from writing to the
// same shard while inspecting.
// Returns the deleted value, or false when no value was assigned.
func (m *Map) DeleteAccept(
key string,
accept func(prev interface{}, replaced bool) bool,
) (prev interface{}, deleted bool) {
m.initDo()
shard := m.choose(key)
m.mus[shard].Lock()
defer m.mus[shard].Unlock()
prev, deleted = m.maps[shard].Delete(key)
if accept != nil {
if !accept(prev, deleted) {
// revert unaccepted change
if deleted {
// reset updated data
m.maps[shard].Set(key, prev)
}
prev, deleted = nil, false
}
}
return prev, deleted
}
// Len returns the number of values in map.
func (m *Map) Len() int {
m.initDo()
var len int
for i := 0; i < m.shards; i++ {
m.mus[i].Lock()
len += m.maps[i].Len()
m.mus[i].Unlock()
}
return len
}
// Range iterates overall all key/values.
// It's not safe to call or Set or Delete while ranging.
func (m *Map) Range(iter func(key string, value interface{}) bool) {
m.initDo()
var done bool
for i := 0; i < m.shards; i++ {
func() {
m.mus[i].RLock()
defer m.mus[i].RUnlock()
m.maps[i].Range(func(key string, value interface{}) bool {
if !iter(key, value) {
done = true
return false
}
return true
})
}()
if done {
break
}
}
}
func (m *Map) choose(key string) int {
return int(xxhash.Sum64String(key) & uint64(m.shards-1))
}
func (m *Map) initDo() {
m.init.Do(func() {
m.shards = 1
for m.shards < runtime.NumCPU()*16 {
m.shards *= 2
}
scap := m.cap / m.shards
m.mus = make([]sync.RWMutex, m.shards)
m.maps = make([]*rhh.Map, m.shards)
for i := 0; i < len(m.maps); i++ {
m.maps[i] = rhh.New(scap)
}
})
}