-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashes.go
271 lines (223 loc) · 5.28 KB
/
hashes.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package redis
import "sync"
var (
allHashes = make(map[string]Hash)
hashesMu sync.RWMutex
)
// Hash is a concurrent safe string map
type Hash struct {
m map[string]string
mu *sync.RWMutex
}
// NewHash creates a new Hash
func NewHash() Hash {
return Hash{
m: make(map[string]string),
mu: new(sync.RWMutex),
}
}
// Valid returns true if the Hash is a valid non-nil instance
func (h Hash) Valid() bool {
return h.m != nil && h.mu != nil
}
// Size returns the number of items in the hash
func (h Hash) Size() int {
h.mu.RLock()
size := len(h.m)
h.mu.RUnlock()
return size
}
// Exists returns whether a key exists in the hash
func (h Hash) Exists(key string) bool {
h.mu.RLock()
_, ok := h.m[key]
h.mu.RUnlock()
return ok
}
// Get returns the value of a key in the map,
// or an empty string if it doesn't exist
func (h Hash) Get(key string) string {
h.mu.RLock()
val := h.m[key]
h.mu.RUnlock()
return val
}
// GetExists returns the value of a key in the map,
// and whether it existed.
func (h Hash) GetExists(key string) (string, bool) {
h.mu.RLock()
val, exists := h.m[key]
h.mu.RUnlock()
return val, exists
}
// Set a key to a value
func (h Hash) Set(key, value string) {
h.mu.Lock()
h.m[key] = value
h.mu.Unlock()
}
// Delete a key in the hash
func (h Hash) Delete(key string) {
h.mu.Lock()
delete(h.m, key)
h.mu.Unlock()
}
// Keys returns all keys in the hash
func (h Hash) Keys() []string {
h.mu.RLock()
keys := make([]string, 0, len(h.m))
for k := range h.m {
keys = append(keys, k)
}
h.mu.RUnlock()
return keys
}
// Values returns all values in the hash
func (h Hash) Values() []string {
h.mu.RLock()
values := make([]string, 0, len(h.m))
for _, v := range h.m {
values = append(values, v)
}
h.mu.RUnlock()
return values
}
// Copy keys and values to a new Hash
func (h Hash) Copy() Hash {
newHash := NewHash()
h.mu.RLock()
for k, v := range h.m {
newHash.m[k] = v
}
h.mu.RUnlock()
return newHash
}
// ToMap returns a copy of all data in the hash, as a map
func (h Hash) ToMap() map[string]string {
h.mu.RLock()
retMap := make(map[string]string, len(h.m))
for k, v := range h.m {
retMap[k] = v
}
h.mu.RUnlock()
return retMap
}
// Sets field in the hash stored at key to value. If key does not exist, a
// new key holding a hash is created. If field already exists in the hash,
// it is overwritten.
//
// Return value
// Integer reply, specifically:
// 1 if field is a new field in the hash and value was set.
// 0 if field already exists in the hash and the value was updated.
func HSet(key, field, value string) (existed int) {
hashesMu.Lock()
existed = 0
h, exists := allHashes[key]
if !exists {
h = NewHash()
allHashes[key] = h
existed = 1
}
hashesMu.Unlock()
h.Set(field, value)
publish <- notice{"hash", key, field, h}
return
}
// Returns the value associated with field in the hash stored at key.
//
// Return value
// Bulk string reply: the value associated with field, or nil when field is not
// present in the hash or key does not exist.
func HGet(key, field string) string {
hashesMu.RLock()
h, ok := allHashes[key]
hashesMu.RUnlock()
if !ok {
return ""
}
return h.Get(field)
}
// Removes the specified fields from the hash stored at key. Specified fields that do not
// exist within this hash are ignored. If key does not exist, it is treated as an empty
// hash and this command returns 0.
//
// Return value
// Integer reply: the number of fields that were removed from the hash, not including
// specified but non existing fields.
func HDel(key, field string) (existed int) {
hashesMu.Lock()
existed = 0
h, exists := allHashes[key]
if exists {
if h.Exists(field) {
h.Delete(field)
existed++
}
} else {
// Publish a valid empty Hash
h = NewHash()
}
hashesMu.Unlock()
publish <- notice{"hash", key, field, h}
return
}
// Returns if field is an existing field in the hash stored at key.
//
// Return value
// Integer reply, specifically:
// 1 if the hash contains field.
// 0 if the hash does not contain field, or key does not exist.
func HExists(key, field string) (existed int) {
hashesMu.RLock()
h, hashExists := allHashes[key]
hashesMu.RUnlock()
existed = 0
if hashExists {
if h.Exists(field) {
existed = 1
}
}
return
}
// Returns all fields and values of the hash stored at key. In the returned
// value, every field name is followed by its value, so the length of the reply
// is twice the size of the hash.
//
// Return value
// map[string]string reply: list of fields and their values stored in the hash, or an empty list when key does not exist.
func Hgetall(key string) Hash {
hashesMu.RLock()
h, ok := allHashes[key]
hashesMu.RUnlock()
if !ok {
return NewHash()
}
return h.Copy()
}
// Returns all values in the hash stored at key.
//
// Return value
// Slice reply: list of values in the hash, or an empty list when key does not exist.
func Hvals(key string) []string {
hashesMu.RLock()
h, ok := allHashes[key]
hashesMu.RUnlock()
if !ok {
return []string{}
}
return h.Values()
}
// Returns all field names in the hash stored at key.
//
// Return value
// Array reply: list of fields in the hash, or an empty list when key does not exist.
func Hkeys(key string) []string {
hashesMu.RLock()
h, ok := allHashes[key]
hashesMu.RUnlock()
if !ok {
return []string{}
}
return h.Keys()
}