MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / sinterGenericCommand

Function sinterGenericCommand

src/t_set.cpp:866–1002  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

864}
865
866void sinterGenericCommand(client *c, robj **setkeys,
867 unsigned long setnum, robj *dstkey) {
868 robj **sets = (robj**)zmalloc(sizeof(robj*)*setnum, MALLOC_SHARED);
869 setTypeIterator *si;
870 robj *dstset = NULL;
871 const char *elesds;
872 int64_t intobj;
873 void *replylen = NULL;
874 unsigned long j, cardinality = 0;
875 int encoding, empty = 0;
876
877 for (j = 0; j < setnum; j++) {
878 robj *setobj = dstkey ?
879 lookupKeyWrite(c->db,setkeys[j]) :
880 lookupKeyRead(c->db,setkeys[j]).unsafe_robjcast();
881 if (!setobj) {
882 /* A NULL is considered an empty set */
883 empty += 1;
884 sets[j] = NULL;
885 continue;
886 }
887 if (checkType(c,setobj,OBJ_SET)) {
888 zfree(sets);
889 return;
890 }
891 sets[j] = setobj;
892 }
893
894 /* Set intersection with an empty set always results in an empty set.
895 * Return ASAP if there is an empty set. */
896 if (empty > 0) {
897 zfree(sets);
898 if (dstkey) {
899 if (dbDelete(c->db,dstkey)) {
900 signalModifiedKey(c,c->db,dstkey);
901 notifyKeyspaceEvent(NOTIFY_GENERIC,"del",dstkey,c->db->id);
902 g_pserver->dirty++;
903 }
904 addReply(c,shared.czero);
905 } else {
906 addReply(c,shared.emptyset[c->resp]);
907 }
908 return;
909 }
910
911 /* Sort sets from the smallest to largest, this will improve our
912 * algorithm's performance */
913 qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality);
914
915 /* The first thing we should output is the total number of elements...
916 * since this is a multi-bulk write, but at this stage we don't know
917 * the intersection set size, so we use a trick, append an empty object
918 * to the output list and save the pointer to later modify it with the
919 * right length */
920 if (!dstkey) {
921 replylen = addReplyDeferredLen(c);
922 } else {
923 /* If we have a target key where to store the resulting set

Callers 2

sinterCommandFunction · 0.85
sinterstoreCommandFunction · 0.85

Calls 15

zmallocFunction · 0.85
lookupKeyWriteFunction · 0.85
lookupKeyReadFunction · 0.85
checkTypeFunction · 0.85
zfreeFunction · 0.85
dbDeleteFunction · 0.85
signalModifiedKeyFunction · 0.85
notifyKeyspaceEventFunction · 0.85
addReplyFunction · 0.85
addReplyDeferredLenFunction · 0.85
createIntsetObjectFunction · 0.85
setTypeInitIteratorFunction · 0.85

Tested by

no test coverage detected