/
keyscan.go
1191 lines (1082 loc) · 27 KB
/
keyscan.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package myredis
import (
"dannytools/ehand"
"encoding/json"
"fmt"
"strings"
"time"
//"github.com/davecgh/go-spew/spew"
"github.com/go-redis/redis"
)
const (
G_Redis_BigKey_Format_Data string = "%-2d, %-10d, %-10d, %-6s, %-19s, %-10d, %-20s, %-20s\n"
G_Redis_BigKey_Format_Header string = "%-2s, %-10s, %-10s, %-6s, %-19s, %-10s, %-20s, %-20s\n"
)
var (
G_Redis_BigKey_Headers []string = []string{"db", "sizeInByte", "elementCnt", "type", "expire", "bigSize", "key", "big"}
)
func GetRedisBigkeyHeader() string {
return fmt.Sprintf(G_Redis_BigKey_Format_Header, "db", "sizeInByte", "elementCnt", "type", "expire", "bigSize", "key", "big")
}
type RedisKey struct {
Database uint
Bytes int64 // bytes of key and value
ElementCnt int64 // for list|set|hash|zset, the count of the elements
Type string
Expire string
MaxElementBytes int64 // bytes of max element of key
Key string // key
MaxElement string // max element of key
Value interface{}
}
func (this RedisKey) GetRedisKeyPrintLine(ifBigkey bool, ifPretty bool, indent string) (string, error) {
if ifBigkey {
return this.ToStringWithoutValue(), nil
} else {
return this.DumpJsonStr(ifPretty, indent)
}
}
func (this RedisKey) ToStringWithoutValue() string {
var (
key string = this.Key
maxElement string = this.MaxElement
)
if strings.Contains(this.Key, ",") {
key = fmt.Sprintf("'%s'", this.Key)
}
if strings.Contains(this.MaxElement, ",") {
maxElement = fmt.Sprintf("'%s'", this.MaxElement)
}
return fmt.Sprintf(G_Redis_BigKey_Format_Data, this.Database, this.Bytes, this.ElementCnt, this.Type, this.Expire, this.MaxElementBytes, key, maxElement)
}
func (this RedisKey) DumpJsonStr(ifPretty bool, indent string) (string, error) {
var (
err error
//result string
reBytes []byte
)
if ifPretty {
reBytes, err = json.MarshalIndent(this, "", indent)
} else {
reBytes, err = json.Marshal(this)
}
if err != nil {
return "", ehand.WithStackError(err)
} else {
return string(reBytes), nil
}
}
// keys: keyname=>elementCnt
func GetMemUseSampleNotString(keys map[string]int64, sampleRate int64, minNumb int64) map[string]int {
var (
result map[string]int = map[string]int{}
sample int64
)
for k, v := range keys {
if v == -1 {
result[k] = -1 // for string
continue
}
if sampleRate == 100 || sampleRate == 0 { // sample all elements
sample = 0
} else {
sample = (v / 100) * sampleRate
if sample < minNumb {
sample = minNumb
}
}
result[k] = int(sample)
}
return result
}
func GetStringKeySample(keys []string) map[string]int {
var (
result map[string]int = map[string]int{}
)
for _, k := range keys {
result[k] = -1
}
return result
}
// param: key:sample
//return : key:memBytes
func (client ClusterAndRedisClient) GetMemByMemUsagePipe(keys map[string]int, minBytes int64) (map[string]int64, error) {
var (
err error
rp redis.Pipeliner
cmdArr []*redis.IntCmd
result map[string]int64 = map[string]int64{}
arr []string
k string
i int
bytesCnt int64
)
if client.IsCluster {
rp = client.Cluster.Pipeline()
} else {
rp = client.Redis.Pipeline()
}
defer rp.Close()
for k = range keys {
arr = append(arr, k)
if keys[k] == -1 {
cmdArr = append(cmdArr, rp.MemoryUsage(k))
} else {
cmdArr = append(cmdArr, rp.MemoryUsage(k, keys[k]))
}
}
_, err = rp.Exec()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
for i = range arr {
bytesCnt, err = cmdArr[i].Result()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
if bytesCnt >= minBytes {
result[arr[i]] = bytesCnt
}
}
return result, nil
}
func (client ClusterAndRedisClient) GetRedisKeyArrBySample(keys []string, keyLengths map[string]int64, ttls map[string]string, db uint, keyTp string,
sampleRate uint, minSample uint, minBytes int64) ([]RedisKey, error) {
var (
results []RedisKey
samples map[string]int
err error
)
if keyTp == "string" {
samples = GetStringKeySample(keys)
} else {
samples = GetMemUseSampleNotString(keyLengths, int64(sampleRate), int64(minSample))
}
sampleMem, err := client.GetMemByMemUsagePipe(samples, minBytes)
if err != nil {
return nil, err
}
for k, oneBytes := range sampleMem {
if keyTp == "string" {
results = append(results, RedisKey{
Database: db,
Bytes: oneBytes,
Type: keyTp,
Expire: ttls[k],
Key: k})
} else {
results = append(results, RedisKey{
Database: db,
Bytes: oneBytes,
ElementCnt: keyLengths[k],
Type: keyTp,
Expire: ttls[k],
Key: k})
}
}
return results, nil
}
func (client ClusterAndRedisClient) GetKeysTypePipe(keys []string) (map[string]string, error) {
var (
rp redis.Pipeliner
cmdArr []*redis.StatusCmd
err error
result map[string]string = map[string]string{}
tp string
)
if client.IsCluster {
rp = client.Cluster.Pipeline()
} else {
rp = client.Redis.Pipeline()
}
defer rp.Close()
for _, k := range keys {
cmdArr = append(cmdArr, rp.Type(k))
}
_, err = rp.Exec()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
for i := range keys {
tp, err = cmdArr[i].Result()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
result[keys[i]] = tp
}
return result, nil
}
func (client ClusterAndRedisClient) DeleteKeysByExpireItPipe(keyes []string, expTime time.Duration) error {
var (
err error
rp redis.Pipeliner
//cmdArr []*redis.BoolCmd
)
if client.IsCluster {
rp = client.Cluster.Pipeline()
} else {
rp = client.Redis.Pipeline()
}
defer rp.Close()
for _, key := range keyes {
//cmdArr = append(cmdArr, rp.Expire(key, expTime))
rp.Expire(key, expTime)
}
_, err = rp.Exec()
if err != nil {
return ehand.CreateErrorWithStack(err)
} else {
return nil
}
}
// if minTtl == -1 || maxTtl == -1: only keys not set ttl
// if minTtl == 0 && maxTtl == 0: any key
// if minTtl > 0 : only key with ttl >= minTtl
// if maxTtl > 0 : only key with ttl <= maxTtl
// expired key is not return
func (client ClusterAndRedisClient) GetKeysTTLConditionPipe(keys []string, timeFmt string, minTtl, maxTtl int) (map[string]string, error) {
var (
ttl time.Duration
err error
cmdArr []*redis.DurationCmd
k string
i int
secs float64
result map[string]string = map[string]string{}
maxTtlFloat64 float64 = float64(maxTtl)
minTtlFloat64 float64 = float64(minTtl)
ifOk bool
)
rp := client.GetPipeline()
defer rp.Close()
for _, k = range keys {
cmdArr = append(cmdArr, rp.TTL(k))
}
_, err = rp.Exec()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
for i = range keys {
ttl, err = cmdArr[i].Result()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
secs = ttl.Seconds()
//fmt.Printf("%s %f maxt=%f mint=%f\n", keys[i], secs, maxTtlFloat64, minTtlFloat64)
if secs == -2 {
continue
}
if secs == -1 {
ifOk = false
if minTtl == -1 && maxTtl == -1 {
ifOk = true
}
if minTtl == 0 && maxTtl == 0 {
ifOk = true
}
if minTtl > 0 {
ifOk = true
}
if maxTtl > 0 {
ifOk = false
}
if ifOk {
//fmt.Printf("%s ok\n", keys[i])
result[keys[i]] = "" // not set
}
} else {
ifOk = true
if minTtl == -1 && maxTtl == -1 {
ifOk = false
}
if minTtl > 0 {
if secs < minTtlFloat64 {
ifOk = false
}
}
if maxTtl > 0 {
if secs > maxTtlFloat64 {
ifOk = false
}
}
if ifOk {
//fmt.Printf("%s ok\n", keys[i])
result[keys[i]] = time.Now().Add(ttl).Format(timeFmt)
}
}
}
return result, nil
}
func (client ClusterAndRedisClient) GetKeysTTLPipe(keys []string, timeFmt string) (map[string]string, error) {
var (
ttl time.Duration
err error
rp redis.Pipeliner
cmdArr []*redis.DurationCmd
k string
i int
secs float64
result map[string]string = map[string]string{}
)
if client.IsCluster {
rp = client.Cluster.Pipeline()
} else {
rp = client.Redis.Pipeline()
}
defer rp.Close()
for _, k = range keys {
cmdArr = append(cmdArr, rp.TTL(k))
}
_, err = rp.Exec()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
for i = range keys {
ttl, err = cmdArr[i].Result()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
secs = ttl.Seconds()
if secs == -1 {
result[keys[i]] = "" // not set
} else if secs == -2 {
continue
//result[keys[i]] = "-2" // already expired
} else {
result[keys[i]] = time.Now().Add(ttl).Format(timeFmt)
}
}
return result, nil
}
func (client ClusterAndRedisClient) GetStringValuePipe(keys []string, minBytes int64, ifValue, ifBytes bool) (map[string]RedisValueString, error) {
var (
rp redis.Pipeliner
err error
cmdArr []*redis.StringCmd
k string
i int
result map[string]RedisValueString = map[string]RedisValueString{}
str string
)
if client.IsCluster {
rp = client.Cluster.Pipeline()
} else {
rp = client.Redis.Pipeline()
}
defer rp.Close()
for _, k = range keys {
cmdArr = append(cmdArr, rp.Get(k))
}
_, err = rp.Exec()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
for i = range keys {
str, err = cmdArr[i].Result()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
val := RedisValueString{}
if ifBytes {
val.BytesCnt = int64(len(keys[i]) + len(str))
if val.BytesCnt < minBytes {
continue
}
} else {
val.BytesCnt = 0
}
if ifValue {
val.Value = str
} else {
val.Value = ""
}
result[keys[i]] = val
}
return result, nil
}
func (client ClusterAndRedisClient) GetListLenPipe(keys []string) (map[string]int64, error) {
var (
err error
rp redis.Pipeliner
cmdArr []*redis.IntCmd
i int
result map[string]int64 = map[string]int64{}
length int64
)
if client.IsCluster {
rp = client.Cluster.Pipeline()
} else {
rp = client.Redis.Pipeline()
}
defer rp.Close()
for i = range keys {
cmdArr = append(cmdArr, rp.LLen(keys[i]))
}
_, err = rp.Exec()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
for i = range keys {
length, err = cmdArr[i].Result()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
result[keys[i]] = length
}
return result, nil
}
func (client ClusterAndRedisClient) GetListValueSameKeyPipe(key string, length int64, rangeBatch int64, elementBatch int64,
elementInterval time.Duration, ifSleep, ifBytes, ifValue bool) (RedisValueList, error) {
var (
err error
i int64 = 0
cmdArr []*redis.StringSliceCmd
rp redis.Pipeliner
stop int64
result RedisValueList
alreadyCnt int64
)
rp = client.GetPipeline()
defer rp.Close()
for {
if i >= length {
break
}
stop = i + rangeBatch - 1
if stop >= length {
stop = length
}
cmdArr = append(cmdArr, rp.LRange(key, i, stop))
alreadyCnt += stop - i + 1
if alreadyCnt >= elementBatch {
_, err = rp.Exec()
if err != nil {
return result, ehand.CreateErrorWithStack(err)
}
err = ProcessArrayOfStringSliceCmdResultSameKey(cmdArr, &result, ifBytes, ifValue)
if err != nil {
return result, err
}
alreadyCnt = 0
cmdArr = nil
if ifSleep {
time.Sleep(elementInterval)
}
}
i = stop + 1
}
if len(cmdArr) > 0 {
_, err = rp.Exec()
if err != nil {
return result, ehand.CreateErrorWithStack(err)
}
err = ProcessArrayOfStringSliceCmdResultSameKey(cmdArr, &result, ifBytes, ifValue)
if err != nil {
return result, err
}
}
if ifBytes {
result.BytesCnt += int64(len(key))
}
return result, nil
}
func (client ClusterAndRedisClient) GetMultiListValuesPipeAdaptive(keys []string, minBytes int64, rangeBatch int64, elementBatch int64,
elementInterval time.Duration, ifSleep, ifBytes, ifValue bool) (map[string]RedisValueList, error) {
var (
err error
keyLengths map[string]int64
k string
oneKeyLen int64
result map[string]RedisValueList = map[string]RedisValueList{}
alreadyCnt int64 = 0
oneKeysArr []string
)
//keyLengths, err = client.GetListLenPipe(keys)
keyLengths, err = client.GetElementCntPipe(keys, "list")
if err != nil {
return nil, err
}
for k = range keyLengths {
oneKeyLen = keyLengths[k]
if oneKeyLen > rangeBatch {
oneListValue, err := client.GetListValueSameKeyPipe(k, oneKeyLen, rangeBatch, elementBatch,
elementInterval, ifSleep, ifBytes, ifValue)
if err != nil {
return nil, err
}
if oneListValue.BytesCnt < minBytes {
continue
}
result[k] = oneListValue
continue
} else {
oneKeysArr = append(oneKeysArr, k)
alreadyCnt += oneKeyLen
if alreadyCnt >= elementBatch {
rMap, err := client.GetMultiKeyValuesByRangeAllPipe(oneKeysArr, "list", minBytes, ifBytes, ifValue)
if err != nil {
return nil, err
}
for ky := range rMap {
result[ky] = rMap[ky]
}
oneKeysArr = nil
alreadyCnt = 0
}
}
}
if len(oneKeysArr) > 0 {
rMap, err := client.GetMultiKeyValuesByRangeAllPipe(oneKeysArr, "list", minBytes, ifBytes, ifValue)
if err != nil {
return nil, err
}
for ky := range rMap {
result[ky] = rMap[ky]
}
}
return result, nil
}
func (client ClusterAndRedisClient) GetMultiHashValueAdaptive(keys []string, minBytes int64, rangeBatch int64, elementBatch int64,
elementInterval time.Duration, ifSleep, ifBytes, ifValue bool) (map[string]RedisValueHash, error) {
var (
result map[string]RedisValueHash = map[string]RedisValueHash{}
keyArr []string
alreadyCnt int64 = 0
)
//hLengths, err := client.GetMultiHashLenPipe(keys)
hLengths, err := client.GetElementCntPipe(keys, "hash")
if err != nil {
return nil, err
}
for k, kL := range hLengths {
if kL > rangeBatch {
oneKeyVal, err := client.GetOneHashValueByScan(k, rangeBatch, elementBatch, elementInterval, ifSleep, ifBytes, ifValue)
if err != nil {
return nil, err
}
if oneKeyVal.BytesCnt < minBytes {
continue
}
result[k] = oneKeyVal
continue
} else {
keyArr = append(keyArr, k)
alreadyCnt += kL
}
if alreadyCnt >= elementBatch {
rMap, err := client.GetMultiHashValueByHgetallPipe(keyArr, minBytes, ifBytes, ifValue)
if err != nil {
return nil, err
}
for ky := range rMap {
result[ky] = rMap[ky]
}
alreadyCnt = 0
keyArr = nil
if ifSleep {
time.Sleep(elementInterval)
}
}
}
if len(keyArr) > 0 {
rMap, err := client.GetMultiHashValueByHgetallPipe(keyArr, minBytes, ifBytes, ifValue)
if err != nil {
return nil, err
}
for ky := range rMap {
result[ky] = rMap[ky]
}
}
return result, nil
}
func (client ClusterAndRedisClient) GetMultiHashLenPipe(keys []string) (map[string]int64, error) {
var (
err error
result map[string]int64 = map[string]int64{}
cmdArr []*redis.IntCmd
length int64
)
rp := client.GetPipeline()
defer rp.Close()
for _, k := range keys {
cmdArr = append(cmdArr, rp.HLen(k))
}
_, err = rp.Exec()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
for i := range keys {
length, err = cmdArr[i].Result()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
result[keys[i]] = length
}
return result, nil
}
func (client ClusterAndRedisClient) GetMultiHashValueByHgetallPipe(keys []string, minBytes int64, ifBytes, ifValue bool) (map[string]RedisValueHash, error) {
var (
err error
result map[string]RedisValueHash = map[string]RedisValueHash{}
cmdArr []*redis.StringStringMapCmd
oneByteCnt int64
)
rp := client.GetPipeline()
defer rp.Close()
for _, k := range keys {
cmdArr = append(cmdArr, rp.HGetAll(k))
}
_, err = rp.Exec()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
for i := range keys {
oneK, err := cmdArr[i].Result()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
oneVal := RedisValueHash{ElementCnt: int64(len(oneK))}
if ifBytes {
for k, v := range oneK {
oneByteCnt = int64(len(k) + len(v))
oneVal.BytesCnt += oneByteCnt
if oneByteCnt > oneVal.BiggestBytesCnt {
oneVal.BiggestBytesCnt = oneByteCnt
oneVal.BiggestElement = k
}
}
if oneVal.BytesCnt < minBytes {
continue
}
}
if ifValue {
oneVal.Value = oneK
}
result[keys[i]] = oneVal
}
return result, nil
}
func (client ClusterAndRedisClient) GetOneHashValueByScan(key string, rangeBatch int64, elementBatch int64,
elementInterval time.Duration, ifSleep, ifBytes, ifValue bool) (RedisValueHash, error) {
var (
err error
eKeys []string
result RedisValueHash
cursor uint64 = 0
alreadyCnt int64 = 0
oneBytes int64 = 0
i int
)
for {
if client.IsCluster {
eKeys, cursor, err = client.Cluster.HScan(key, cursor, "", rangeBatch).Result()
} else {
eKeys, cursor, err = client.Redis.HScan(key, cursor, "", rangeBatch).Result()
}
if err != nil {
return result, ehand.CreateErrorWithStack(err)
}
for i = 0; i < len(eKeys); i += 2 {
alreadyCnt++
result.ElementCnt++
if ifValue {
result.Value[eKeys[i]] = eKeys[i+1]
}
if ifBytes {
oneBytes = int64(len(eKeys[i]) + len(eKeys[i+1]))
result.BytesCnt += oneBytes
if oneBytes > result.BiggestBytesCnt {
result.BiggestBytesCnt = oneBytes
result.BiggestElement = eKeys[i]
}
}
}
if cursor == 0 {
break
}
if ifSleep {
if alreadyCnt > elementBatch {
time.Sleep(elementInterval)
alreadyCnt = 0
}
}
}
return result, nil
}
func (client ClusterAndRedisClient) GetMultiSetValueAdaptive(keys []string, minBytes int64, rangeBatch int64, elementBatch int64,
elementInterval time.Duration, ifSleep, ifBytes, ifValue bool) (map[string]RedisValueList, error) {
var (
err error
result map[string]RedisValueList = map[string]RedisValueList{}
keyArr []string
alreadyCnt int64 = 0
)
//kLengths, err := client.GetMultiSetCard(keys)
kLengths, err := client.GetElementCntPipe(keys, "set")
if err != nil {
return nil, err
}
for k, kL := range kLengths {
if kL > rangeBatch {
oneVal, err := client.GetOneSetValueByScan(k, rangeBatch, elementBatch, elementInterval, ifSleep, ifBytes, ifValue)
if err != nil {
return nil, err
}
if oneVal.BytesCnt < minBytes {
continue
}
continue
} else {
keyArr = append(keyArr, k)
alreadyCnt += kL
if alreadyCnt >= elementBatch {
rMap, err := client.GetMultiSetValueBySmembersPipe(keyArr, minBytes, ifBytes, ifValue)
if err != nil {
return nil, err
}
for ky := range rMap {
result[ky] = rMap[ky]
}
keyArr = nil
alreadyCnt = 0
}
}
}
if len(keyArr) > 0 {
rMap, err := client.GetMultiSetValueBySmembersPipe(keyArr, minBytes, ifBytes, ifValue)
if err != nil {
return nil, err
}
for ky := range rMap {
result[ky] = rMap[ky]
}
}
return result, nil
}
func (client ClusterAndRedisClient) GetMultiSetCard(keys []string) (map[string]int64, error) {
var (
err error
cmdArr []*redis.IntCmd
result map[string]int64 = map[string]int64{}
length int64
)
rp := client.GetPipeline()
defer rp.Close()
for _, k := range keys {
cmdArr = append(cmdArr, rp.SCard(k))
}
_, err = rp.Exec()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
for i := range keys {
length, err = cmdArr[i].Result()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
result[keys[i]] = length
}
return result, nil
}
func (client ClusterAndRedisClient) GetMultiSetValueBySmembersPipe(keys []string, minBytes int64, ifBytes, ifValue bool) (map[string]RedisValueList, error) {
var (
result map[string]RedisValueList = map[string]RedisValueList{}
err error
cmdArr []*redis.StringSliceCmd
)
rp := client.GetPipeline()
defer rp.Close()
for _, k := range keys {
cmdArr = append(cmdArr, rp.SMembers(k))
}
_, err = rp.Exec()
if err != nil {
return nil, ehand.CreateErrorWithStack(err)
}
for i := range keys {
oneResult := RedisValueList{}
err = ProcessOneStringSliceCmdResult(cmdArr[i], &oneResult, ifBytes, ifValue)
if err != nil {
return nil, err
}
if oneResult.BytesCnt < minBytes {
continue
}
result[keys[i]] = oneResult
}
return result, nil
}
func (client ClusterAndRedisClient) GetOneSetValueByScan(key string, rangeBatch int64, elementBatch int64,
elementInterval time.Duration, ifSleep, ifBytes, ifValue bool) (RedisValueList, error) {
var (
err error
cursor uint64 = 0
oneVals []string
alreadyCnt int64 = 0
result RedisValueList
oneByte int64
)
for {
if client.IsCluster {
oneVals, cursor, err = client.Cluster.SScan(key, cursor, "", rangeBatch).Result()
} else {
oneVals, cursor, err = client.Redis.SScan(key, cursor, "", rangeBatch).Result()
}
if err != nil {
return result, ehand.CreateErrorWithStack(err)
}
result.ElementCnt += int64(len(oneVals))
if ifValue {
result.Value = append(result.Value, oneVals...)
}
if ifBytes {
for _, v := range oneVals {
oneByte = int64(len(v))
result.BytesCnt += oneByte
if oneByte > result.BiggestBytesCnt {
result.BiggestBytesCnt = oneByte
result.BiggestElement = v
}
}
}
if cursor == 0 {
break
}
if ifSleep {
alreadyCnt += int64(len(oneVals))
if alreadyCnt > elementBatch {
time.Sleep(elementInterval)
}
alreadyCnt = 0
}
}
return result, nil
}
func ProcessOneZsliceResult(cmd *redis.ZSliceCmd, result *RedisValueZset, ifBytes, ifValue bool) error {
var (
err error
arr []redis.Z
z redis.Z
ke string
ok bool
oneBytes int64
)
arr, err = cmd.Result()
if err != nil {
return err
}
for _, z = range arr {
ke, ok = z.Member.(string)
if !ok {
return ehand.CreateStrErrorWithStack(fmt.Sprintf("error to convert zset member %v to string", z.Member))
}
result.ElementCnt++
if ifValue {
result.Value[ke] = z.Score
}
if ifBytes {
oneBytes = int64(len(ke)) + 8
result.BytesCnt += oneBytes
if oneBytes > result.BiggestBytesCnt {
result.BiggestBytesCnt = oneBytes
result.BiggestElement = ke
}
}
}
return nil
}
func ProcessZsliceResultArrayButSameKey(cmdArr []*redis.ZSliceCmd, result *RedisValueZset, ifBytes, ifValue bool) error {
var (
err error
)
for _, cmd := range cmdArr {
err = ProcessOneZsliceResult(cmd, result, ifBytes, ifValue)
if err != nil {
return err
}
}
return nil
}
func (client ClusterAndRedisClient) GetOneZsetWithScoreSameKeyPipe(key string, length int64, rangeBatch int64, elementBatch int64,
elementInterval time.Duration, ifSleep, ifBytes bool) (RedisValueZset, error) {
var (
err error
i int64 = 0
cmdArr []*redis.ZSliceCmd