-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy patht_set.c
1736 lines (1442 loc) · 64.9 KB
/
t_set.c
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
/*
* Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "server.h"
/*-----------------------------------------------------------------------------
* Set Commands
*----------------------------------------------------------------------------*/
void sunionDiffGenericCommand(client *c, robj **setkeys, int setnum,
robj *dstkey, int op);
/* Factory method to return a set that *can* hold "value". When the object has
* an integer-encodable value, an intset will be returned. Otherwise a regular
* hash table. */
/* 工厂方法,返回一个能容纳 "value" 的集合。当对象有一个可编码成整数编码的值时,
* 将返回一个intset(整数集合)。否则就是一个普通的 hashtable(哈希表)。 */
robj *setTypeCreate(sds value) {
/* 如果 value 能转换为 longlong 型的整数,说明可以使用整数编码,则创建一个整数集合 */
if (isSdsRepresentableAsLongLong(value,NULL) == C_OK)
return createIntsetObject();
/* 创建一个普通的哈希表集合 */
return createSetObject();
}
/* Add the specified value into a set.
*
* If the value was already member of the set, nothing is done and 0 is
* returned, otherwise the new element is added and 1 is returned. */
/* 将指定的元素(value) 添加到一个集合里。
*
* 如果该值已经是该集合的成员,则不做任何事,返回0,否则将添加新元素,返回1。*/
int setTypeAdd(robj *subject, sds value) {
long long llval;
/* 对象编码类型为 hashtable */
if (subject->encoding == OBJ_ENCODING_HT) {
/* 获取对象的哈希表(集合) */
dict *ht = subject->ptr;
/* 将元素加入到哈希表中,若已经存在会返回 NULL */
dictEntry *de = dictAddRaw(ht,value,NULL);
if (de) {
/* 给元素所在的 entry 设置 key 和 value */
dictSetKey(ht,de,sdsdup(value));
dictSetVal(ht,de,NULL);
return 1;
}
} else if (subject->encoding == OBJ_ENCODING_INTSET) {
if (isSdsRepresentableAsLongLong(value,&llval) == C_OK) {
uint8_t success = 0;
subject->ptr = intsetAdd(subject->ptr,llval,&success);
if (success) {
/* Convert to regular set when the intset contains
* too many entries. */
/* 当整数集合中的元素数量超出限制值时,转换为普通(hashtable)集合 */
size_t max_entries = server.set_max_intset_entries;
/* limit to 1G entries due to intset internals. */
/* 由于整数集合内部结构的原因,限制整数集合最大限制为 1073741824(1<<30) 个 元素 */
if (max_entries >= 1<<30) max_entries = 1<<30;
/* 整数集合元素数量超过限制 */
if (intsetLen(subject->ptr) > max_entries)
/* 转换集合内部编码为 hashtable */
setTypeConvert(subject,OBJ_ENCODING_HT);
return 1;
}
} else {
/* Failed to get integer from object, convert to regular set. */
/* 从对象中获取整数失败,转换内部编码为 hashtable */
setTypeConvert(subject,OBJ_ENCODING_HT);
/* The set *was* an intset and this value is not integer
* encodable, so dictAdd should always work. */
/* 该集合之前是一个整数集合,参数 value 不可被编码为整数,所以这里调用 dictAdd 总是有效的 */
serverAssert(dictAdd(subject->ptr,sdsdup(value),NULL) == DICT_OK);
return 1;
}
} else {
serverPanic("Unknown set encoding");
}
return 0;
}
/* 删除集合中的元素 */
int setTypeRemove(robj *setobj, sds value) {
long long llval;
/* 对象编码类型为 hashtable */
if (setobj->encoding == OBJ_ENCODING_HT) {
/* 删除元素 */
if (dictDelete(setobj->ptr,value) == DICT_OK) {
/* 检查删除元素后是否能够缩容 */
if (htNeedsResize(setobj->ptr)) dictResize(setobj->ptr);
return 1;
}
/* 对象编码类型为 intset */
} else if (setobj->encoding == OBJ_ENCODING_INTSET) {
/* 检查 value 是否能转换为 longlong 型的整数,若不能则不会做任何操作(因为 intset 中不会有字符串编码的元素) */
if (isSdsRepresentableAsLongLong(value,&llval) == C_OK) {
int success; /* 用于记录是否删除元素成功 */
/* 删除元素 */
setobj->ptr = intsetRemove(setobj->ptr,llval,&success);
if (success) return 1;
}
/* 对象类型不是集合,报错 */
} else {
serverPanic("Unknown set encoding");
}
return 0;
}
/* 检查元素是否存在集合中 */
int setTypeIsMember(robj *subject, sds value) {
long long llval;
/* 对象编码类型为 hashtable */
if (subject->encoding == OBJ_ENCODING_HT) {
/* 使用查找函数,找不到会返回 NULL,不为 NULL 返回 1,NULL 返回 0 */
return dictFind((dict*)subject->ptr,value) != NULL;
/* 对象编码类型为 intset */
} else if (subject->encoding == OBJ_ENCODING_INTSET) {
/* 检查 value 是否能转换为 longlong 型的整数,若不能则不会做任何操作(因为 intset 中不会有字符串编码的元素) */
if (isSdsRepresentableAsLongLong(value,&llval) == C_OK) {
/* 使用查找函数,查找成功返回 1,失败返回 0 */
return intsetFind((intset*)subject->ptr,llval);
}
} else {
serverPanic("Unknown set encoding");
}
return 0;
}
/* 初始化迭代器函数 */
setTypeIterator *setTypeInitIterator(robj *subject) {
/* 分配内存空间 */
setTypeIterator *si = zmalloc(sizeof(setTypeIterator));
/* 迭代器指向的对象和对象编码类型 设置为 所输入的对象和对象编码类型 */
si->subject = subject;
si->encoding = subject->encoding;
/* 编码类型为 hashtable */
if (si->encoding == OBJ_ENCODING_HT) {
/* 初始化迭代器成员 di(字典(或称哈希表)迭代器) */
si->di = dictGetIterator(subject->ptr);
/* 编码类型为 intset */
} else if (si->encoding == OBJ_ENCODING_INTSET) {
/* 初始化迭代器成员 li 为 0,li 的值代表了迭代器所在的元素在 intset 里的位置 */
si->ii = 0;
} else {
serverPanic("Unknown set encoding");
}
return si;
}
/* 释放迭代器 */
void setTypeReleaseIterator(setTypeIterator *si) {
/* 如果编码类型是 hashtable ,还要额外释放字典迭代器 di */
if (si->encoding == OBJ_ENCODING_HT)
dictReleaseIterator(si->di);
zfree(si);
}
/* Move to the next entry in the set. Returns the object at the current
* position.
*
* Since set elements can be internally be stored as SDS strings or
* simple arrays of integers, setTypeNext returns the encoding of the
* set object you are iterating, and will populate the appropriate pointer
* (sdsele) or (llele) accordingly.
*
* Note that both the sdsele and llele pointers should be passed and cannot
* be NULL since the function will try to defensively populate the non
* used field with values which are easy to trap if misused.
*
* When there are no longer elements -1 is returned. */
/* 移动到集合中的下一个 entry。返回当前位置的对象
*
* 由于集合元素在内部可以被存储为 SDS字符串 或简单的整数数组,
* setTypeNext 返回正在迭代的集合对象的编码,
* 并将相应地填充适当的指针(sdsele)或(llele)。
*
* 注意,sdsele 和 llele 指针都应该被传递,而且不能为空,
* 该函数将尝试用一些被误用时能容易捕获到的值(是特殊的且出现概率极低的值)来防御性地填充未使用的字段。
*
* 当不再有元素时,将返回 -1 */
int setTypeNext(setTypeIterator *si, sds *sdsele, int64_t *llele) {
/* 编码类型为 hashtable */
if (si->encoding == OBJ_ENCODING_HT) {
/* 获取下一个位置的 entry */
dictEntry *de = dictNext(si->di);
/* 若 de 为空即不存在下一个位置,返回 -1 */
if (de == NULL) return -1;
/* 获取下一个位置的元素(字符串值) */
*sdsele = dictGetKey(de);
/* llele 代表获取到的是整数值,编码类型为 hashtable 元素都是以字符串编码形式保存,
* 所以这里用 -123456789 进行防御性填充 */
*llele = -123456789; /* Not needed. Defensive. */
/* 编码类型为 intset */
} else if (si->encoding == OBJ_ENCODING_INTSET) {
/* 获取下一个位置的元素 */
if (!intsetGet(si->subject->ptr,si->ii++,llele))
return -1;
/* 整数集合中的元素都是整数编码,所以代表字符串值的 sdsele 用 NULL 进行防御性填充 */
*sdsele = NULL; /* Not needed. Defensive. */
} else {
serverPanic("Wrong set encoding in setTypeNext");
}
/* 返回集合对象的编码类型 */
return si->encoding;
}
/* The not copy on write friendly version but easy to use version
* of setTypeNext() is setTypeNextObject(), returning new SDS
* strings. So if you don't retain a pointer to this object you should call
* sdsfree() against it.
*
* This function is the way to go for write operations where COW is not
* an issue. */
/* setTypeNext() 的一个对写时复制不友好的,但容易使用的版本是 setTypeNextObject(),
* 返回新的 SDS 字符串。所以如果你不保留这个对象的指针,你应该对它调用sdsfree() 来将它释放 */
sds setTypeNextObject(setTypeIterator *si) {
int64_t intele;
sds sdsele;
int encoding;
/* 调用 setTypeNext 函数获取下一个元素值以及集合编码类型 */
encoding = setTypeNext(si,&sdsele,&intele);
/* 根据集合编码类型选择返回值 */
switch(encoding) {
case -1: return NULL;
case OBJ_ENCODING_INTSET:
return sdsfromlonglong(intele);
case OBJ_ENCODING_HT:
return sdsdup(sdsele);
default:
serverPanic("Unsupported encoding");
}
return NULL; /* just to suppress warnings */
}
/* Return random element from a non empty set.
* The returned element can be an int64_t value if the set is encoded
* as an "intset" blob of integers, or an SDS string if the set
* is a regular set.
*
* The caller provides both pointers to be populated with the right
* object. The return value of the function is the object->encoding
* field of the object and is used by the caller to check if the
* int64_t pointer or the sds pointer was populated.
*
* Note that both the sdsele and llele pointers should be passed and cannot
* be NULL since the function will try to defensively populate the non
* used field with values which are easy to trap if misused. */
/* 如果集合编码类型为 intset ,则返回的元素是一个 int64_t 值,
* 如果集合是一个普通集合(hashtable),则返回一个 SDS字符串。
*
* 调用者提供两个指针,以填充正确的对象。
* 该函数的返回值是对象的 object->encoding 字段,
* 被调用者用它来检查 int64_t 指针或 sds 指针是否被填充。
*
* 注意,sdsele 和 llele 指针都应该被传递,而且不能为空,
* 该函数将尝试用一些被误用时能容易捕获到的值(是特殊的且出现概率极低的值)来防御性地填充未使用的字段。 */
int setTypeRandomElement(robj *setobj, sds *sdsele, int64_t *llele) {
/* 编码类型为 hashtable */
if (setobj->encoding == OBJ_ENCODING_HT) {
/* 在字典中获取随机一个 entry */
dictEntry *de = dictGetFairRandomKey(setobj->ptr);
/* 获取元素的值(字符串值) */
*sdsele = dictGetKey(de);
*llele = -123456789; /* Not needed. Defensive. */
/* 编码类型为 intset */
} else if (setobj->encoding == OBJ_ENCODING_INTSET) {
/* 在整数集合中获取随机一个元素 */
*llele = intsetRandom(setobj->ptr);
*sdsele = NULL; /* Not needed. Defensive. */
} else {
serverPanic("Unknown set encoding");
}
return setobj->encoding;
}
/* 获取集合元素总数 */
unsigned long setTypeSize(const robj *subject) {
if (subject->encoding == OBJ_ENCODING_HT) {
return dictSize((const dict*)subject->ptr);
} else if (subject->encoding == OBJ_ENCODING_INTSET) {
return intsetLen((const intset*)subject->ptr);
} else {
serverPanic("Unknown set encoding");
}
}
/* Convert the set to specified encoding. The resulting dict (when converting
* to a hash table) is presized to hold the number of elements in the original
* set. */
/* 将该集合转换为指定的编码(目前仅支持从 intset 到 hashtable 转换)
* 由此产生的 dict(当转换为哈希表时)被预分配空间为足够容纳原始集合中的元素数量 */
void setTypeConvert(robj *setobj, int enc) {
setTypeIterator *si;
serverAssertWithInfo(NULL,setobj,setobj->type == OBJ_SET &&
setobj->encoding == OBJ_ENCODING_INTSET);
/* 要转换成的编码类型为 hashtable */
if (enc == OBJ_ENCODING_HT) {
int64_t intele;
dict *d = dictCreate(&setDictType);
sds element;
/* Presize the dict to avoid rehashing */
/* 预分配空间来避免 rehashing(重哈希) */
dictExpand(d,intsetLen(setobj->ptr));
/* To add the elements we extract integers and create redis objects */
/* 为了添加元素,我们提取整数并创建 redis 对象 */
si = setTypeInitIterator(setobj);
/* 遍历 intset */
while (setTypeNext(si,&element,&intele) != -1) {
/* 将整数编码的元素转换为字符串编码,并赋值给 element */
element = sdsfromlonglong(intele);
/* 断言元素加入字典中不会出现重复 */
serverAssert(dictAdd(d,element,NULL) == DICT_OK);
}
/* 释放迭代器 */
setTypeReleaseIterator(si);
/* 修改对象编码信息 */
setobj->encoding = OBJ_ENCODING_HT;
/* 释放原来的 intset */
zfree(setobj->ptr);
/* 令对象指针指向转换完成的字典(哈希表) */
setobj->ptr = d;
} else {
serverPanic("Unsupported set conversion");
}
}
/* This is a helper function for the COPY command.
* Duplicate a set object, with the guarantee that the returned object
* has the same encoding as the original one.
*
* The resulting object always has refcount set to 1 */
/* 这是一个用于 COPY 命令的辅助函数。
* 复制一个集合对象,并保证返回的对象具有与原始对象相同的编码类型。
*
* 返回的对象总是将 refcount 设置为1 */
robj *setTypeDup(robj *o) {
robj *set;
setTypeIterator *si;
sds elesds;
int64_t intobj;
serverAssert(o->type == OBJ_SET);
/* Create a new set object that have the same encoding as the original object's encoding */
/* 创建一个新的集合对象,其编码与原对象的编码相同 */
if (o->encoding == OBJ_ENCODING_INTSET) {
intset *is = o->ptr;
size_t size = intsetBlobLen(is);
intset *newis = zmalloc(size);
memcpy(newis,is,size);
set = createObject(OBJ_SET, newis);
set->encoding = OBJ_ENCODING_INTSET;
} else if (o->encoding == OBJ_ENCODING_HT) {
set = createSetObject();
dict *d = o->ptr;
dictExpand(set->ptr, dictSize(d));
si = setTypeInitIterator(o);
while (setTypeNext(si, &elesds, &intobj) != -1) {
setTypeAdd(set, elesds);
}
setTypeReleaseIterator(si);
} else {
serverPanic("Unknown set encoding");
}
return set;
}
/* 处理 sadd 命令的函数 */
void saddCommand(client *c) {
robj *set;
int j, added = 0;
/* 获取 key 对应的 Redis对象 */
set = lookupKeyWrite(c->db,c->argv[1]);
/* 检查对象类型 */
if (checkType(c,set,OBJ_SET)) return;
/* 如果对象不存在,则创建一个新的集合对象 */
if (set == NULL) {
/* 创建集合对象 */
set = setTypeCreate(c->argv[2]->ptr);
/* 将对象加入到数据库中 */
dbAdd(c->db,c->argv[1],set);
}
/* 解析输入参数 member */
for (j = 2; j < c->argc; j++) {
/* 将元素加入到集合中,若加入成功则 added 计数增加 */
if (setTypeAdd(set,c->argv[j]->ptr)) added++;
}
/* 如果 added 不为 0,则说明有元素插入集合 */
if (added) {
/* 发送 key 被修改信号 */
signalModifiedKey(c,c->db,c->argv[1]);
/* 发送事件通知 */
notifyKeyspaceEvent(NOTIFY_SET,"sadd",c->argv[1],c->db->id);
}
/* 将 added 加到脏数据计数上(脏数据为缓存对于本地存储的数据变动次数) */
server.dirty += added;
/* 回复客户端加入集合成功的元素个数 */
addReplyLongLong(c,added);
}
/* 处理 srem 命令的函数 */
void sremCommand(client *c) {
robj *set;
int j, deleted = 0, keyremoved = 0;
/* 获取 key 对应的对象,获取后检查对象类型 */
if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
checkType(c,set,OBJ_SET)) return;
/* 解析输入参数 member */
for (j = 2; j < c->argc; j++) {
/* 删除集合中的元素 */
if (setTypeRemove(set,c->argv[j]->ptr)) {
/* 删除成功时 deleted 计数增加 */
deleted++;
/* 集合元素数量为0,即已将集合元素删空 */
if (setTypeSize(set) == 0) {
/* 在数据库中将该 key(对象)删除 */
dbDelete(c->db,c->argv[1]);
/* keyremoved = 1 代表 key 已经被删除 */
keyremoved = 1;
break;
}
}
}
/* 有元素被删除 */
if (deleted) {
/* 发送 key 被修改信号,发送事件通知 */
signalModifiedKey(c,c->db,c->argv[1]);
notifyKeyspaceEvent(NOTIFY_SET,"srem",c->argv[1],c->db->id);
/* key 被删除时,再多发送一个关于删除 key 的事件通知 */
if (keyremoved)
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],
c->db->id);
/* 将 deleted 加到脏数据上 */
server.dirty += deleted;
}
/* 向客户端回复删除元素个数 */
addReplyLongLong(c,deleted);
}
/* 处理 smove 命令的函数 */
void smoveCommand(client *c) {
robj *srcset, *dstset, *ele;
srcset = lookupKeyWrite(c->db,c->argv[1]);
dstset = lookupKeyWrite(c->db,c->argv[2]);
ele = c->argv[3];
/* If the source key does not exist return 0 */
/* 如果 source key 不存在则向客户端回复0并退出函数 */
if (srcset == NULL) {
addReply(c,shared.czero);
return;
}
/* If the source key has the wrong type, or the destination key
* is set and has the wrong type, return with an error. */
/* 如果 source key 或 destination key 的编码类型错误,则退出函数 */
if (checkType(c,srcset,OBJ_SET) ||
checkType(c,dstset,OBJ_SET)) return;
/* If srcset and dstset are equal, SMOVE is a no-op */
/* 如果 srcset(source) 与 dstset(destination) 相等,smove 会是空操作 */
if (srcset == dstset) {
/* 我们只需要向客户端返回 srcset 中是否存在输入的元素即可(存在返回1,否则返回0) */
addReply(c,setTypeIsMember(srcset,ele->ptr) ?
shared.cone : shared.czero);
return;
}
/* If the element cannot be removed from the src set, return 0. */
/* 如果元素不能从 srcset 中被删除,返回0 */
if (!setTypeRemove(srcset,ele->ptr)) {
addReply(c,shared.czero);
return;
}
/* 发送删除元素的事件通知 */
notifyKeyspaceEvent(NOTIFY_SET,"srem",c->argv[1],c->db->id);
/* Remove the src set from the database when empty */
/* 如果 srcset 为空,则将它从数据库中删除 */
if (setTypeSize(srcset) == 0) {
dbDelete(c->db,c->argv[1]);
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],c->db->id);
}
/* Create the destination set when it doesn't exist */
/* 如果 dstset 不存在则为它创建集合 */
if (!dstset) {
dstset = setTypeCreate(ele->ptr);
dbAdd(c->db,c->argv[2],dstset);
}
/* 发送 key 被修改信号 */
signalModifiedKey(c,c->db,c->argv[1]);
/* 脏数据 + 1 */
server.dirty++;
/* An extra key has changed when ele was successfully added to dstset */
/* 当元素被成功添加到 dstset 时,destination key 发生了改变,则需要事件通知等操作(见下文) */
if (setTypeAdd(dstset,ele->ptr)) {
server.dirty++;
signalModifiedKey(c,c->db,c->argv[2]);
notifyKeyspaceEvent(NOTIFY_SET,"sadd",c->argv[2],c->db->id);
}
/* 回复客户端 : 1 */
addReply(c,shared.cone);
}
/* 处理 sismember 命令的函数 */
void sismemberCommand(client *c) {
robj *set;
/* 获取 key 对应的对象 */
if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
checkType(c,set,OBJ_SET)) return;
/* 查找元素是否存在于集合中,存在返回1,不存在返回0 */
if (setTypeIsMember(set,c->argv[2]->ptr))
addReply(c,shared.cone);
else
addReply(c,shared.czero);
}
/* 处理 smismemberCommand 命令的函数 */
void smismemberCommand(client *c) {
robj *set;
int j;
/* Don't abort when the key cannot be found. Non-existing keys are empty
* sets, where SMISMEMBER should respond with a series of zeros. */
/* 当找不到 key 时不要中止。不存在的 key 是空集,
* 在这里 SMISMEMBER 应该用与输入元素数量对应数量的0来回应 */
set = lookupKeyRead(c->db,c->argv[1]);
if (set && checkType(c,set,OBJ_SET)) return;
/* 以数组形式回复客户端 */
addReplyArrayLen(c,c->argc - 2);
/* 解析输入参数 member */
for (j = 2; j < c->argc; j++) {
/* 查找元素是否存在于集合中,存在返回1,不存在返回0 */
if (set && setTypeIsMember(set,c->argv[j]->ptr))
addReply(c,shared.cone);
else
addReply(c,shared.czero);
}
}
/* 处理 scard 命令的函数 */
void scardCommand(client *c) {
robj *o;
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
checkType(c,o,OBJ_SET)) return;
/* 调用 setTypeSize 函数返回集合中的元素总数 */
addReplyLongLong(c,setTypeSize(o));
}
/* Handle the "SPOP key <count>" variant. The normal version of the
* command is handled by the spopCommand() function itself. */
/* How many times bigger should be the set compared to the remaining size
* for us to use the "create new set" strategy? Read later in the
* implementation for more info. */
/* 处理 "SPOP key <count>" 命令(输入了 count 参数)。
* 该命令的普通版本由 spopCommand() 函数本身处理 */
/* 集合与剩余的大小相比应该大多少倍,我们才能使用 "创建新集合" 策略?请阅读后面的实现的更多信息。*/
#define SPOP_MOVE_STRATEGY_MUL 5
/* spop 命令带可选参数 count 时的具体实现函数 */
void spopWithCountCommand(client *c) {
long l;
unsigned long count, size;
robj *set;
/* Get the count argument */
/* 获取输入参数 count */
if (getPositiveLongFromObjectOrReply(c,c->argv[2],&l,NULL) != C_OK) return;
count = (unsigned long) l;
/* Make sure a key with the name inputted exists, and that it's type is
* indeed a set. Otherwise, return nil */
/* 确保输入的 key 存在,并且它的类型是一个集合。否则返回nil */
if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.emptyset[c->resp]))
== NULL || checkType(c,set,OBJ_SET)) return;
/* If count is zero, serve an empty set ASAP to avoid special
* cases later. */
/* 如果 count 为 0,尽快提供一个空集,以避免以后出现特殊情况。*/
if (count == 0) {
addReply(c,shared.emptyset[c->resp]);
return;
}
/* 获取集合元素总数 */
size = setTypeSize(set);
/* Generate an SPOP keyspace notification */
/* 生成一个 spop 事件通知 */
notifyKeyspaceEvent(NOTIFY_SET,"spop",c->argv[1],c->db->id);
server.dirty += (count >= size) ? size : count;
/* CASE 1:
* The number of requested elements is greater than or equal to
* the number of elements inside the set: simply return the whole set. */
/* 第一种情况:
* 请求的元素数量(count)大于或等于集合内的元素数量:简单地返回整个集合 */
if (count >= size) {
/* We just return the entire set */
/* 返回整个集合 */
sunionDiffGenericCommand(c,c->argv+1,1,NULL,SET_OP_UNION);
/* Delete the set as it is now empty */
/* 现在集合中的元素已被删空,可以将集合从数据库中删除 */
dbDelete(c->db,c->argv[1]);
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],c->db->id);
/* Propagate this command as a DEL operation */
/* 将该命令以 del 命令的形式进行传播(传播给 AOF 或 从服务器 )*/
rewriteClientCommandVector(c,2,shared.del,c->argv[1]);
signalModifiedKey(c,c->db,c->argv[1]);
return;
}
/* Case 2 and 3 require to replicate SPOP as a set of SREM commands.
* Prepare our replication argument vector. Also send the array length
* which is common to both the code paths. */
/* 第二种情况和第三种情况要求将 SPOP 复制成一组 SREM 命令(用于 AOF 或 从服务器)。
* 准备好我们的复制参数。同时发送数组长度,这是两个代码执行路径所共有的。*/
robj *propargv[3];
propargv[0] = shared.srem;
propargv[1] = c->argv[1];
addReplySetLen(c,count);
/* Common iteration vars. */
/* 通常的迭代变量 */
sds sdsele;
robj *objele;
int encoding;
int64_t llele;
unsigned long remaining = size-count; /* 经过 SPOP 后剩下的元素数量 */
/* If we are here, the number of requested elements is less than the
* number of elements inside the set. Also we are sure that count < size.
* Use two different strategies.
*
* CASE 2: The number of elements to return is small compared to the
* set size. We can just extract random elements and return them to
* the set. */
/* 如果我们请求的元素数量少于集合内的元素数量,也就是 count < size. 我们会使用两种不同的策略。
*
* 第二种情况: 与集合的大小相比,要返回的元素数量很少。我们可以直接提取随机元素并返回到集合。*/
if (remaining*SPOP_MOVE_STRATEGY_MUL > count) {
while(count--) {
/* Emit and remove. */
encoding = setTypeRandomElement(set,&sdsele,&llele);
if (encoding == OBJ_ENCODING_INTSET) {
addReplyBulkLongLong(c,llele);
objele = createStringObjectFromLongLong(llele);
set->ptr = intsetRemove(set->ptr,llele,NULL);
} else {
addReplyBulkCBuffer(c,sdsele,sdslen(sdsele));
objele = createStringObject(sdsele,sdslen(sdsele));
setTypeRemove(set,sdsele);
}
/* Replicate/AOF this command as an SREM operation */
/* 将此命令作为 SREM 命令进行 从服务器的复制 和 写入AOF */
propargv[2] = objele;
alsoPropagate(c->db->id,propargv,3,PROPAGATE_AOF|PROPAGATE_REPL);
decrRefCount(objele);
}
} else {
/* CASE 3: The number of elements to return is very big, approaching
* the size of the set itself. After some time extracting random elements
* from such a set becomes computationally expensive, so we use
* a different strategy, we extract random elements that we don't
* want to return (the elements that will remain part of the set),
* creating a new set as we do this (that will be stored as the original
* set). Then we return the elements left in the original set and
* release it. */
/* 第三种情况:要返回的元素数量非常多,接近于集合本身的元素总数。
* 一段时间后,从这样一个集合中提取随机元素的计算成本会变得很高,
* 所以我们使用不同的策略,我们提取我们不想返回的随机元素(这些元素将保持为集合的一部分),
* 在我们这样做的时候创建一个新的集合(将作为新的原始集合存储)。
* 然后,我们返回留在原始集合中的元素并释放它。*/
robj *newset = NULL;
/* Create a new set with just the remaining elements. */
/* 创建新集合 newset 并只保存需要保留的元素 */
/* 提取随机元素,只提取到满足需要保留的数量 */
while(remaining--) {
/* 随机获取集合中的一个元素 */
encoding = setTypeRandomElement(set,&sdsele,&llele);
/* 检查编码类型 */
if (encoding == OBJ_ENCODING_INTSET) {
/* 将整数元素(llele)转换为 SDS字符串 */
sdsele = sdsfromlonglong(llele);
} else {
/* 创建一个副本 */
sdsele = sdsdup(sdsele);
}
/* 如果 newset 为空则创建集合 */
if (!newset) newset = setTypeCreate(sdsele);
/* 将随机获取的元素加入到 newset,并将元素从原始集合中删除 */
setTypeAdd(newset,sdsele);
setTypeRemove(set,sdsele);
sdsfree(sdsele);
}
/* Transfer the old set to the client. */
/* 将原始集合中剩下的元素回复给客户端 */
setTypeIterator *si;
si = setTypeInitIterator(set);
/* 遍历集合获取元素并回复给客户端 */
while((encoding = setTypeNext(si,&sdsele,&llele)) != -1) {
if (encoding == OBJ_ENCODING_INTSET) {
addReplyBulkLongLong(c,llele);
objele = createStringObjectFromLongLong(llele);
} else {
addReplyBulkCBuffer(c,sdsele,sdslen(sdsele));
objele = createStringObject(sdsele,sdslen(sdsele));
}
/* Replicate/AOF this command as an SREM operation */
/* 将此命令作为 SREM 命令进行 从服务器的复制 和 写入AOF */
propargv[2] = objele;
alsoPropagate(c->db->id,propargv,3,PROPAGATE_AOF|PROPAGATE_REPL);
decrRefCount(objele);
}
setTypeReleaseIterator(si);
/* Assign the new set as the key value. */
/* 将 newset 覆盖到数据库中原来的 key 上 */
dbOverwrite(c->db,c->argv[1],newset);
}
/* Don't propagate the command itself even if we incremented the
* dirty counter. We don't want to propagate an SPOP command since
* we propagated the command as a set of SREMs operations using
* the alsoPropagate() API. */
/* 即使我们增加了脏数据的计数,也不要传播命令本身。
* 我们不想传播 SPOP 命令,因为我们使用了 alsoPropagate() API 将命令作为一组 SREM 传播 */
preventCommandPropagation(c);
signalModifiedKey(c,c->db,c->argv[1]);
}
/* 处理 spop 命令的函数 */
void spopCommand(client *c) {
robj *set, *ele;
sds sdsele;
int64_t llele;
int encoding;
/* 输入参数的数量为3 */
if (c->argc == 3) {
/* 参数数量为3时,可能输入了合法 count 参数,调用 spopWithCountCommand 函数进行处理 */
spopWithCountCommand(c);
return;
/* 输入参数数量超过3 */
} else if (c->argc > 3) {
/* 向客户端回复语法错误 */
addReplyErrorObject(c,shared.syntaxerr);
return;
}
/* Make sure a key with the name inputted exists, and that it's type is
* indeed a set */
/* 确保存在输入的 key,并且它的类型是一个集合 */
if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.null[c->resp]))
== NULL || checkType(c,set,OBJ_SET)) return;
/* Get a random element from the set */
/* 从集合中获取随机元素 */
encoding = setTypeRandomElement(set,&sdsele,&llele);
/* Remove the element from the set */
/* 从集合中删除获取到的元素 */
if (encoding == OBJ_ENCODING_INTSET) {
ele = createStringObjectFromLongLong(llele);
set->ptr = intsetRemove(set->ptr,llele,NULL);
} else {
ele = createStringObject(sdsele,sdslen(sdsele));
setTypeRemove(set,ele->ptr);
}
/* 发送事件通知 */
notifyKeyspaceEvent(NOTIFY_SET,"spop",c->argv[1],c->db->id);
/* Replicate/AOF this command as an SREM operation */
/* 将此命令作为 SREM 命令进行 从服务器的复制 和 写入AOF */
rewriteClientCommandVector(c,3,shared.srem,c->argv[1],ele);
/* Add the element to the reply */
/* 向客户端回复被弹出的元素 */
addReplyBulk(c,ele);
/* 减少 ele 被引用次数计数(-1)*/
decrRefCount(ele);
/* Delete the set if it's empty */
/* 当集合为空时,从数据库中把集合删除 */
if (setTypeSize(set) == 0) {
dbDelete(c->db,c->argv[1]);
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],c->db->id);
}
/* Set has been modified */
/* 集合已被修改,发送 key 被修改信号 */
signalModifiedKey(c,c->db,c->argv[1]);
server.dirty++;
}
/* handle the "SRANDMEMBER key <count>" variant. The normal version of the
* command is handled by the srandmemberCommand() function itself. */
/* How many times bigger should be the set compared to the requested size
* for us to don't use the "remove elements" strategy? Read later in the
* implementation for more info. */
/* 处理 “SRANDMEMBER key <count>” 命令(输入了 count 参数)。 该命令的普通版本由 srandmemberCommand() 函数本身处理。 */
/* 与请求的元素数量相比,该集合应该大多少倍让我们不要使用“移除元素”策略? 阅读后面的代码实现获取更多信息。 */
#define SRANDMEMBER_SUB_STRATEGY_MUL 3
/* 输入了 count 参数的 srandmember 命令的具体实现函数 */
void srandmemberWithCountCommand(client *c) {
long l;
unsigned long count, size;
int uniq = 1;
robj *set;
sds ele;
int64_t llele;
int encoding;
dict *d;
/* 获取输入参数 count */
if (getLongFromObjectOrReply(c,c->argv[2],&l,NULL) != C_OK) return;
if (l >= 0) {
count = (unsigned long) l;
} else {
/* A negative count means: return the same elements multiple times
* (i.e. don't remove the extracted element after every extraction). */
/* 负数表示:多次返回相同的元素。
* (即不要在每次提取后删除提取的元素) */
count = -l; /* 将负数的 count 取相反数转换为正数 */
uniq = 0; /* uniq 设为 0,表示结果不需要去重 */
}
/* 获取 key 对应的对象 */
if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.emptyarray))
== NULL || checkType(c,set,OBJ_SET)) return;
/* 获取集合中的元素总数 */
size = setTypeSize(set);
/* If count is zero, serve it ASAP to avoid special cases later. */
/* 如果 count 为 0,尽快返回空数组,避免之后的特殊情况 */
if (count == 0) {
addReply(c,shared.emptyarray);
return;
}
/* CASE 1: The count was negative, so the extraction method is just:
* "return N random elements" sampling the whole set every time.
* This case is trivial and can be served without auxiliary data