/
db_t_hash.go
568 lines (452 loc) · 11.3 KB
/
db_t_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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package storager
import (
"context"
"strconv"
"sync"
"time"
"github.com/weedge/pkg/driver"
"github.com/weedge/pkg/rdb"
"github.com/weedge/pkg/utils"
)
type DBHash struct {
*DB
batch *Batch
}
func NewDBHash(db *DB) *DBHash {
batch := NewBatch(db.store, db.IKV.NewWriteBatch(),
&dbBatchLocker{
l: &sync.Mutex{},
wrLock: &db.store.wLock,
})
return &DBHash{DB: db, batch: batch}
}
func checkHashKFSize(ctx context.Context, key []byte, field []byte) error {
if len(key) > MaxKeySize || len(key) == 0 {
return ErrKeySize
} else if len(field) > MaxHashFieldSize || len(field) == 0 {
return ErrHashFieldSize
}
return nil
}
func (db *DBHash) delete(t *Batch, key []byte) (num int64, err error) {
sk := db.hEncodeSizeKey(key)
start := db.hEncodeStartKey(key)
stop := db.hEncodeStopKey(key)
it := db.IKV.RangeLimitIterator(start, stop, driver.RangeROpen, 0, -1)
for ; it.Valid(); it.Next() {
t.Delete(it.Key())
num++
}
it.Close()
t.Delete(sk)
db.DelKeyMeta(t, key, HashType)
return num, nil
}
// Del cleans multi hash data.
func (db *DBHash) Del(ctx context.Context, keys ...[]byte) (int64, error) {
if len(keys) == 0 {
return 0, nil
}
for _, key := range keys {
if err := checkKeySize(key); err != nil {
return 0, err
}
}
t := db.batch
t.Lock()
defer t.Unlock()
nums := 0
for _, key := range keys {
if n, err := db.delete(t, key); err == nil && n > 0 {
nums++
}
db.rmExpire(t, HashType, key)
}
err := t.Commit(ctx)
return int64(nums), err
}
func (db *DBHash) hSetItem(ctx context.Context, key []byte, field []byte, value []byte) (int64, error) {
t := db.batch
ek := db.hEncodeHashKey(key, field)
var n int64 = 1
if v, _ := db.IKV.Get(ek); v != nil {
n = 0
} else {
if _, err := db.hIncrSize(ctx, t, key, 1); err != nil {
return 0, err
}
}
db.SetKeyMeta(t, key, HashType)
t.Put(ek, value)
return n, nil
}
func (db *DBHash) hIncrSize(ctx context.Context, t *Batch, key []byte, delta int64) (int64, error) {
sk := db.hEncodeSizeKey(key)
var err error
var size int64
if size, err = Int64(db.IKV.Get(sk)); err != nil {
return 0, err
}
size += delta
if size <= 0 {
size = 0
t.Delete(sk)
db.rmExpire(t, HashType, key)
} else {
t.Put(sk, PutInt64(size))
}
return size, nil
}
// HLen returns the lengh of hash.
func (db *DBHash) HLen(ctx context.Context, key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
return Int64(db.IKV.Get(db.hEncodeSizeKey(key)))
}
// uHSet sets the field with value of key.
func (db *DBHash) HSet(ctx context.Context, key []byte, field []byte, value []byte) (int64, error) {
if err := checkHashKFSize(ctx, key, field); err != nil {
return 0, err
} else if err := checkValueSize(value); err != nil {
return 0, err
}
t := db.batch
t.Lock()
defer t.Unlock()
n, err := db.hSetItem(ctx, key, field, value)
if err != nil {
return 0, err
}
err = t.Commit(ctx)
return n, err
}
// HGet gets the value of the field.
func (db *DBHash) HGet(ctx context.Context, key []byte, field []byte) ([]byte, error) {
if err := checkHashKFSize(ctx, key, field); err != nil {
return nil, err
}
return db.IKV.Get(db.hEncodeHashKey(key, field))
}
// HMset sets multi field-values.
func (db *DBHash) HMset(ctx context.Context, key []byte, args ...driver.FVPair) error {
t := db.batch
t.Lock()
defer t.Unlock()
var num int64
for i := 0; i < len(args); i++ {
if err := checkHashKFSize(ctx, key, args[i].Field); err != nil {
return err
} else if err := checkValueSize(args[i].Value); err != nil {
return err
}
ek := db.hEncodeHashKey(key, args[i].Field)
if v, err := db.IKV.Get(ek); err != nil {
return err
} else if v == nil {
num++
}
t.Put(ek, args[i].Value)
}
if _, err := db.hIncrSize(ctx, t, key, num); err != nil {
return err
}
db.SetKeyMeta(t, key, HashType)
err := t.Commit(ctx)
return err
}
// HMget gets multi values of fields
func (db *DBHash) HMget(ctx context.Context, key []byte, args ...[]byte) ([][]byte, error) {
var ek []byte
it := db.IKV.NewIterator()
defer it.Close()
r := make([][]byte, len(args))
for i := 0; i < len(args); i++ {
if err := checkHashKFSize(ctx, key, args[i]); err != nil {
return nil, err
}
ek = db.hEncodeHashKey(key, args[i])
r[i] = it.Find(ek)
}
return r, nil
}
// HDel deletes the fields.
func (db *DBHash) HDel(ctx context.Context, key []byte, args ...[]byte) (int64, error) {
t := db.batch
var ek []byte
var v []byte
var err error
t.Lock()
defer t.Unlock()
it := db.IKV.NewIterator()
defer it.Close()
var num int64
for i := 0; i < len(args); i++ {
if err := checkHashKFSize(ctx, key, args[i]); err != nil {
return 0, err
}
ek = db.hEncodeHashKey(key, args[i])
v = it.RawFind(ek)
if v == nil {
continue
} else {
num++
t.Delete(ek)
}
}
if _, err = db.hIncrSize(ctx, t, key, -num); err != nil {
return 0, err
}
err = t.Commit(ctx)
return num, err
}
// HIncrBy increases the value of field by delta.
func (db *DBHash) HIncrBy(ctx context.Context, key []byte, field []byte, delta int64) (int64, error) {
if err := checkHashKFSize(ctx, key, field); err != nil {
return 0, err
}
t := db.batch
var ek []byte
var err error
t.Lock()
defer t.Unlock()
ek = db.hEncodeHashKey(key, field)
var n int64
if n, err = utils.StrInt64(db.IKV.Get(ek)); err != nil {
return 0, ErrHashIntVal
}
n += delta
_, err = db.hSetItem(ctx, key, field, strconv.AppendInt(nil, n, 10))
if err != nil {
return 0, err
}
err = t.Commit(ctx)
return n, err
}
// HGetAll returns all field-values.
func (db *DBHash) HGetAll(ctx context.Context, key []byte) ([]driver.FVPair, error) {
if err := checkKeySize(key); err != nil {
return nil, err
}
start := db.hEncodeStartKey(key)
stop := db.hEncodeStopKey(key)
v := make([]driver.FVPair, 0, 16)
it := db.IKV.RangeLimitIterator(start, stop, driver.RangeROpen, 0, -1)
defer it.Close()
for ; it.Valid(); it.Next() {
_, f, err := db.hDecodeHashKey(it.Key())
if err != nil {
return nil, err
}
v = append(v, driver.FVPair{Field: f, Value: it.Value()})
}
return v, nil
}
// HKeys returns the all fields.
func (db *DBHash) HKeys(ctx context.Context, key []byte) ([][]byte, error) {
if err := checkKeySize(key); err != nil {
return nil, err
}
start := db.hEncodeStartKey(key)
stop := db.hEncodeStopKey(key)
v := make([][]byte, 0, 16)
it := db.IKV.RangeLimitIterator(start, stop, driver.RangeROpen, 0, -1)
defer it.Close()
for ; it.Valid(); it.Next() {
_, f, err := db.hDecodeHashKey(it.Key())
if err != nil {
return nil, err
}
v = append(v, f)
}
return v, nil
}
// HValues returns all values
func (db *DBHash) HValues(ctx context.Context, key []byte) ([][]byte, error) {
if err := checkKeySize(key); err != nil {
return nil, err
}
start := db.hEncodeStartKey(key)
stop := db.hEncodeStopKey(key)
v := make([][]byte, 0, 16)
it := db.IKV.RangeLimitIterator(start, stop, driver.RangeROpen, 0, -1)
defer it.Close()
for ; it.Valid(); it.Next() {
_, _, err := db.hDecodeHashKey(it.Key())
if err != nil {
return nil, err
}
v = append(v, it.Value())
}
return v, nil
}
func (db *DBHash) hExpireAt(ctx context.Context, key []byte, when int64) (int64, error) {
t := db.batch
t.Lock()
defer t.Unlock()
if hlen, err := db.HLen(ctx, key); err != nil || hlen == 0 {
return 0, err
}
db.expireAt(t, HashType, key, when)
if err := t.Commit(ctx); err != nil {
return 0, err
}
return 1, nil
}
// Expire expires the data with duration.
func (db *DBHash) Expire(ctx context.Context, key []byte, duration int64) (int64, error) {
if duration <= 0 {
return 0, ErrExpireValue
}
return db.hExpireAt(ctx, key, time.Now().Unix()+duration)
}
// ExpireAt expires the data at time when.
func (db *DBHash) ExpireAt(ctx context.Context, key []byte, when int64) (int64, error) {
if when <= time.Now().Unix() {
return 0, ErrExpireValue
}
return db.hExpireAt(ctx, key, when)
}
// TTL gets the TTL of data.
func (db *DBHash) TTL(ctx context.Context, key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return -1, err
}
sk := db.hEncodeSizeKey(key)
v, err := db.IKV.Get(sk)
if err != nil {
return -1, err
}
if v == nil {
return -2, nil
}
return db.ttl(HashType, key)
}
// Persist removes the TTL of data.
func (db *DBHash) Persist(ctx context.Context, key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
t := db.batch
t.Lock()
defer t.Unlock()
n, err := db.rmExpire(t, HashType, key)
if err != nil {
return 0, err
}
err = t.Commit(ctx)
return n, err
}
// HKeyExists checks whether data exists or not.
func (db *DBHash) Exists(ctx context.Context, key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
sk := db.hEncodeSizeKey(key)
v, err := db.IKV.Get(sk)
if v != nil && err == nil {
return 1, nil
}
return 0, err
}
// Clear clears the data.
func (db *DBHash) Clear(ctx context.Context, key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
t := db.batch
t.Lock()
defer t.Unlock()
num, err := db.delete(t, key)
if err != nil {
return 0, nil
}
db.rmExpire(t, HashType, key)
err = t.Commit(ctx)
return num, err
}
// Dump hash rdb
func (db *DBHash) Dump(ctx context.Context, key []byte) (binVal []byte, err error) {
v, err := db.HGetAll(ctx, key)
if err != nil {
return
} else if len(v) == 0 {
return
}
hv := make(rdb.Hash, len(v))
for i := 0; i < len(v); i++ {
hv[i].Field = v[i].Field
hv[i].Value = v[i].Value
}
return rdb.DumpHashValue(hv), nil
}
// Restore hash rdb
func (db *DBHash) Restore(ctx context.Context, t *Batch, key []byte, ttl int64, val rdb.Hash) (err error) {
if _, err = db.BatchDel(ctx, t, key); err != nil {
return
}
fv := make([]driver.FVPair, len(val))
for i := 0; i < len(val); i++ {
fv[i] = driver.FVPair{Field: val[i].Field, Value: val[i].Value}
}
if err = db.BatchHMset(ctx, t, key, fv...); err != nil {
return
}
if ttl > 0 {
if _, err = db.BatchExpire(ctx, t, key, ttl); err != nil {
return
}
}
return
}
// BatchDel cleans multi hash data.
func (db *DBHash) BatchDel(ctx context.Context, t *Batch, keys ...[]byte) (int64, error) {
if len(keys) == 0 {
return 0, nil
}
for _, key := range keys {
if err := checkKeySize(key); err != nil {
return 0, err
}
}
nums := 0
for _, key := range keys {
if n, err := db.delete(t, key); err == nil && n > 0 {
nums++
}
db.rmExpire(t, HashType, key)
}
return int64(nums), nil
}
// BatchHMset sets multi field-values.
func (db *DBHash) BatchHMset(ctx context.Context, t *Batch, key []byte, args ...driver.FVPair) error {
var num int64
for i := 0; i < len(args); i++ {
if err := checkHashKFSize(ctx, key, args[i].Field); err != nil {
return err
} else if err := checkValueSize(args[i].Value); err != nil {
return err
}
ek := db.hEncodeHashKey(key, args[i].Field)
if v, err := db.IKV.Get(ek); err != nil {
return err
} else if v == nil {
num++
}
t.Put(ek, args[i].Value)
}
if _, err := db.hIncrSize(ctx, t, key, num); err != nil {
return err
}
db.SetKeyMeta(t, key, HashType)
return nil
}
// BatchExpire expires the data with duration.
func (db *DBHash) BatchExpire(ctx context.Context, t *Batch, key []byte, duration int64) (int64, error) {
if duration <= 0 {
return 0, ErrExpireValue
}
when := time.Now().Unix() + duration
db.expireAt(t, HashType, key, when)
return 1, nil
}