forked from rosedblabs/rosedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_hash.go
503 lines (403 loc) · 10.7 KB
/
db_hash.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package rosedb
import (
"bytes"
"github.com/roseduan/rosedb/utils"
"sync"
"time"
"github.com/roseduan/rosedb/ds/hash"
"github.com/roseduan/rosedb/storage"
)
// HashIdx hash index.
type HashIdx struct {
mu *sync.RWMutex
indexes *hash.Hash
}
// create a new hash index.
func newHashIdx() *HashIdx {
return &HashIdx{indexes: hash.New(), mu: new(sync.RWMutex)}
}
// HSet 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 num of elements in hash of the specified key.
func (db *RoseDB) HSet(key, field, value interface{}) (res int, err error) {
encKey, encVal, err := db.encode(key, value)
if err != nil {
return -1, err
}
encField, err := utils.EncodeKey(field)
if err != nil {
return -1, err
}
if err = db.checkKeyValue(encKey, encVal); err != nil {
return
}
if err = db.checkKeyValue(encField, nil); err != nil {
return
}
// If the existed value is the same as the set value, nothing will be done.
oldVal := db.HGet(encKey, encField)
if bytes.Compare(oldVal, encVal) == 0 {
return
}
db.hashIndex.mu.Lock()
defer db.hashIndex.mu.Unlock()
e := storage.NewEntry(encKey, encVal, encField, Hash, HashHSet)
if err = db.store(e); err != nil {
return
}
res = db.hashIndex.indexes.HSet(string(encKey), string(encField), encVal)
return
}
// HSetNx Sets field in the hash stored at key to value, only if field does not yet exist.
// If key does not exist, a new key holding a hash is created. If field already exists, this operation has no effect.
// Return if the operation is successful.
func (db *RoseDB) HSetNx(key, field, value interface{}) (res int, err error) {
encKey, encVal, err := db.encode(key, value)
if err != nil {
return -1, err
}
encField, err := utils.EncodeKey(field)
if err != nil {
return -1, err
}
if err = db.checkKeyValue(encKey, encVal); err != nil {
return
}
if err = db.checkKeyValue(encField, nil); err != nil {
return
}
db.hashIndex.mu.Lock()
defer db.hashIndex.mu.Unlock()
if res = db.hashIndex.indexes.HSetNx(string(encKey), string(encField), encVal); res == 1 {
e := storage.NewEntry(encKey, encVal, encField, Hash, HashHSet)
if err = db.store(e); err != nil {
return
}
}
return
}
// HGet returns the value associated with field in the hash stored at key.
func (db *RoseDB) HGet(key, field interface{}) []byte {
encKey, err := utils.EncodeKey(key)
if err != nil {
return nil
}
encField, err := utils.EncodeKey(field)
if err != nil {
return nil
}
if err = db.checkKeyValue(encKey, nil); err != nil {
return nil
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(encKey, Hash) {
return nil
}
return db.hashIndex.indexes.HGet(string(encKey), string(encField))
}
// HGetAll 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.
func (db *RoseDB) HGetAll(key interface{}) [][]byte {
encKey, err := utils.EncodeKey(key)
if err != nil {
return nil
}
if err := db.checkKeyValue(encKey, nil); err != nil {
return nil
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(encKey, Hash) {
return nil
}
return db.hashIndex.indexes.HGetAll(string(encKey))
}
// HMSet set multiple hash fields to multiple values
func (db *RoseDB) HMSet(key interface{}, values ...interface{}) error {
if len(values)%2 != 0 {
return ErrWrongNumberOfArgs
}
encKey, err := utils.EncodeKey(key)
if err != nil {
return err
}
var encVals [][]byte
for i := 0; i < len(values); i++ {
eval, err := utils.EncodeValue(values[i])
if err != nil {
return err
}
if err = db.checkKeyValue(encKey, eval); err != nil {
return err
}
encVals = append(encVals, eval)
}
fields := make([]interface{}, 0)
for i := 0; i < len(values); i += 2 {
fields = append(fields, encVals[i])
}
existVals := db.HMGet(encKey, fields...)
var encFields [][]byte
for i := 0; i < len(fields); i++ {
efields, err := utils.EncodeValue(fields[i])
if err != nil {
return err
}
if err = db.checkKeyValue(efields, nil); err != nil {
return err
}
encFields = append(encFields, efields)
}
// field1 value1 field2 value2 ...
insertVals := make([][]byte, 0)
if existVals == nil {
// existVals means key expired
insertVals = encVals
} else {
for i := 0; i < len(existVals); i++ {
// If the existed value is the same as the set value, pass this field and value
if bytes.Compare(encVals[i*2+1], existVals[i]) != 0 {
insertVals = append(insertVals, encFields[i], encVals[i*2+1])
}
}
}
// check all fields and values.
if err := db.checkKeyValue(encKey, insertVals...); err != nil {
return err
}
db.hashIndex.mu.Lock()
defer db.hashIndex.mu.Unlock()
for i := 0; i < len(insertVals); i += 2 {
e := storage.NewEntry(encKey, insertVals[i+1], insertVals[i], Hash, HashHSet)
if err := db.store(e); err != nil {
return err
}
db.hashIndex.indexes.HSet(string(encKey), string(insertVals[i]), insertVals[i+1])
}
return nil
}
// HMGet get the values of all the given hash fields
func (db *RoseDB) HMGet(key interface{}, fields ...interface{}) [][]byte {
encKey, err := utils.EncodeKey(key)
if err != nil {
return nil
}
if err = db.checkKeyValue(encKey, nil); err != nil {
return nil
}
var encFields [][]byte
for i := 0; i < len(fields); i++ {
efield, err := utils.EncodeValue(fields[i])
if err != nil {
return nil
}
if err = db.checkKeyValue(efield, nil); err != nil {
return nil
}
encFields = append(encFields, efield)
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(encKey, Hash) {
return nil
}
values := make([][]byte, 0)
for _, field := range encFields {
value := db.hashIndex.indexes.HGet(string(encKey), string(field))
values = append(values, value)
}
return values
}
// HDel 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 false.
func (db *RoseDB) HDel(key interface{}, fields ...interface{}) (res int, err error) {
encKey, err := utils.EncodeKey(key)
if err != nil {
return
}
if err = db.checkKeyValue(encKey, nil); err != nil {
return
}
if fields == nil || len(fields) == 0 {
return
}
var encFields [][]byte
for i := 0; i < len(fields); i++ {
var ef []byte
if ef, err = utils.EncodeValue(fields[i]); err != nil {
return
}
if err = db.checkKeyValue(ef, nil); err != nil {
return
}
encFields = append(encFields, ef)
}
db.hashIndex.mu.Lock()
defer db.hashIndex.mu.Unlock()
for _, f := range encFields {
if ok := db.hashIndex.indexes.HDel(string(encKey), string(f)); ok == 1 {
e := storage.NewEntry(encKey, nil, f, Hash, HashHDel)
if err = db.store(e); err != nil {
return
}
res++
}
}
return
}
// HKeyExists returns if the key is existed in hash.
func (db *RoseDB) HKeyExists(key interface{}) (ok bool) {
encKey, err := utils.EncodeKey(key)
if err != nil {
return
}
if err := db.checkKeyValue(encKey, nil); err != nil {
return
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(encKey, Hash) {
return
}
return db.hashIndex.indexes.HKeyExists(string(encKey))
}
// HExists returns if field is an existing field in the hash stored at key.
func (db *RoseDB) HExists(key, field interface{}) (ok bool) {
encKey, err := utils.EncodeKey(key)
if err != nil {
return
}
if err := db.checkKeyValue(encKey, nil); err != nil {
return
}
encField, err := utils.EncodeKey(field)
if err != nil {
return
}
if err := db.checkKeyValue(encField, nil); err != nil {
return
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(encKey, Hash) {
return
}
return db.hashIndex.indexes.HExists(string(encKey), string(encField))
}
// HLen returns the number of fields contained in the hash stored at key.
func (db *RoseDB) HLen(key interface{}) int {
encKey, err := utils.EncodeKey(key)
if err != nil {
return 0
}
if err := db.checkKeyValue(encKey, nil); err != nil {
return 0
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(encKey, Hash) {
return 0
}
return db.hashIndex.indexes.HLen(string(encKey))
}
// HKeys returns all field names in the hash stored at key.
func (db *RoseDB) HKeys(key interface{}) (val []string) {
encKey, err := utils.EncodeKey(key)
if err != nil {
return
}
if err := db.checkKeyValue(encKey, nil); err != nil {
return
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(encKey, Hash) {
return nil
}
return db.hashIndex.indexes.HKeys(string(encKey))
}
// HVals returns all values in the hash stored at key.
func (db *RoseDB) HVals(key interface{}) (val [][]byte) {
encKey, err := utils.EncodeKey(key)
if err != nil {
return
}
if err := db.checkKeyValue(encKey, nil); err != nil {
return
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(encKey, Hash) {
return nil
}
return db.hashIndex.indexes.HVals(string(encKey))
}
// HClear clear the key in hash.
func (db *RoseDB) HClear(key interface{}) (err error) {
encKey, err := utils.EncodeKey(key)
if err != nil {
return
}
if err = db.checkKeyValue(encKey, nil); err != nil {
return
}
if !db.HKeyExists(encKey) {
return ErrKeyNotExist
}
db.hashIndex.mu.Lock()
defer db.hashIndex.mu.Unlock()
e := storage.NewEntryNoExtra(encKey, nil, Hash, HashHClear)
if err := db.store(e); err != nil {
return err
}
db.hashIndex.indexes.HClear(string(encKey))
delete(db.expires[Hash], string(encKey))
return
}
// HExpire set expired time for a hash key.
func (db *RoseDB) HExpire(key interface{}, duration int64) (err error) {
if duration <= 0 {
return ErrInvalidTTL
}
encKey, err := utils.EncodeKey(key)
if err != nil {
return
}
if err = db.checkKeyValue(encKey, nil); err != nil {
return
}
if !db.HKeyExists(encKey) {
return ErrKeyNotExist
}
db.hashIndex.mu.Lock()
defer db.hashIndex.mu.Unlock()
deadline := time.Now().Unix() + duration
e := storage.NewEntryWithExpire(encKey, nil, deadline, Hash, HashHExpire)
if err := db.store(e); err != nil {
return err
}
db.expires[Hash][string(encKey)] = deadline
return
}
// HTTL return time to live for the key.
func (db *RoseDB) HTTL(key interface{}) (ttl int64) {
encKey, err := utils.EncodeKey(key)
if err != nil {
return
}
if err = db.checkKeyValue(encKey, nil); err != nil {
return
}
db.hashIndex.mu.RLock()
defer db.hashIndex.mu.RUnlock()
if db.checkExpired(encKey, Hash) {
return
}
deadline, exist := db.expires[Hash][string(encKey)]
if !exist {
return
}
return deadline - time.Now().Unix()
}