forked from orcaman/concurrent-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent_map_template.txt
165 lines (142 loc) · 3.63 KB
/
concurrent_map_template.txt
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
package cmap_<KEY>_<VAL>
import (
"encoding/json"
"hash/fnv"
"sync"
)
var SHARD_COUNT = 32
// TODO: Add Keys function which returns an array of keys for the map.
// A "thread" safe map of type <KEY>:<VAL>.
// To avoid lock bottlenecks this map is dived to several (SHARD_COUNT) map shards.
type ConcurrentMap []*ConcurrentMapShared
type ConcurrentMapShared struct {
items map[<KEY>]<VAL>
sync.RWMutex // Read Write mutex, guards access to internal map.
}
// Creates a new concurrent map.
func New() ConcurrentMap {
m := make(ConcurrentMap, SHARD_COUNT)
for i := 0; i < SHARD_COUNT; i++ {
m[i] = &ConcurrentMapShared{items: make(map[<KEY>]<VAL>)}
}
return m
}
// Returns shard under given key
func (m ConcurrentMap) GetShard(key <KEY>) *ConcurrentMapShared {
hasher := fnv.New32()
hasher.Write([]byte(key))
return m[hasher.Sum32()%uint32(SHARD_COUNT)]
}
// Sets the given value under the specified key.
func (m *ConcurrentMap) Set(key <KEY>, value <VAL>) {
// Get map shard.
shard := m.GetShard(key)
shard.Lock()
defer shard.Unlock()
shard.items[key] = value
}
// Retrieves an element from map under given key.
func (m ConcurrentMap) Get(key <KEY>) (<VAL>, bool) {
// Get shard
shard := m.GetShard(key)
shard.RLock()
defer shard.RUnlock()
// Get item from shard.
val, ok := shard.items[key]
return val, ok
}
// Returns the number of elements within the map.
func (m ConcurrentMap) Count() int {
count := 0
for i := 0; i < SHARD_COUNT; i++ {
shard := m[i]
shard.RLock()
count += len(shard.items)
shard.RUnlock()
}
return count
}
// Looks up an item under specified key
func (m *ConcurrentMap) Has(key <KEY>) bool {
// Get shard
shard := m.GetShard(key)
shard.RLock()
defer shard.RUnlock()
// See if element is within shard.
_, ok := shard.items[key]
return ok
}
// Removes an element from the map.
func (m *ConcurrentMap) Remove(key <KEY>) {
// Try to get shard.
shard := m.GetShard(key)
shard.Lock()
defer shard.Unlock()
delete(shard.items, key)
}
// Checks if map is empty.
func (m *ConcurrentMap) IsEmpty() bool {
return m.Count() == 0
}
// Used by the Iter & IterBuffered functions to wrap two variables together over a channel,
type Tuple struct {
Key <KEY>
Val <VAL>
}
// Returns an iterator which could be used in a for range loop.
func (m ConcurrentMap) Iter() <-chan Tuple {
ch := make(chan Tuple)
go func() {
// Foreach shard.
for _, shard := range m {
// Foreach key, value pair.
shard.RLock()
for key, val := range shard.items {
ch <- Tuple{key, val}
}
shard.RUnlock()
}
close(ch)
}()
return ch
}
// Returns a buffered iterator which could be used in a for range loop.
func (m ConcurrentMap) IterBuffered() <-chan Tuple {
ch := make(chan Tuple, m.Count())
go func() {
// Foreach shard.
for _, shard := range m {
// Foreach key, value pair.
shard.RLock()
for key, val := range shard.items {
ch <- Tuple{key, val}
}
shard.RUnlock()
}
close(ch)
}()
return ch
}
//Reviles ConcurrentMap "private" variables to json marshal.
func (m ConcurrentMap) MarshalJSON() ([]byte, error) {
// Create a temporary map, which will hold all item spread across shards.
tmp := make(map[<KEY>]<VAL>)
// Insert items to temporary map.
for item := range m.Iter() {
tmp[item.Key] = item.Val
}
return json.Marshal(tmp)
}
func (m *ConcurrentMap) UnmarshalJSON(b []byte) (err error) {
// Reverse process of Marshal.
tmp := make(map[<KEY>]<VAL>)
// Unmarshal into a single map.
if err := json.Unmarshal(b, &tmp); err != nil {
return nil
}
// foreach key,value pair in temporary map insert into our concurrent map.
for key, val := range tmp {
m.Set(key, val)
}
return nil
}