// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/t_set.c
// Function: sinterGenericCommand
void sinterGenericCommand(redisClient *c, robj **setkeys, unsigned long setnum, robj *dstkey) {

    // 集合数组
    robj **sets = zmalloc(sizeof(robj*)*setnum);

    setTypeIterator *si;
    robj *eleobj, *dstset = NULL;
    int64_t intobj;
    void *replylen = NULL;
    unsigned long j, cardinality = 0;
    int encoding;

    for (j = 0; j < setnum; j++) {

        // 取出对象
        // 第一次执行时，取出的是 dest 集合
        // 之后执行时，取出的都是 source 集合
        robj *setobj = dstkey ?
            lookupKeyWrite(c->db,setkeys[j]) :
            lookupKeyRead(c->db,setkeys[j]);

        // 对象不存在，放弃执行，进行清理
        if (!setobj) {
            zfree(sets);
            if (dstkey) {
                if (dbDelete(c->db,dstkey)) {
                    signalModifiedKey(c->db,dstkey);
                    server.dirty++;
                }
                addReply(c,shared.czero);
            } else {
                addReply(c,shared.emptymultibulk);
            }
            return;
        }
        
        // 检查对象的类型
        if (checkType(c,setobj,REDIS_SET)) {
            zfree(sets);
            return;
        }

        // 将数组指针指向集合对象
        sets[j] = setobj;
    }

    /* Sort sets from the smallest to largest, this will improve our
     * algorithm's performance */
    // 按基数对集合进行排序，这样提升算法的效率
    qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality);

    /* The first thing we should output is the total number of elements...
     * since this is a multi-bulk write, but at this stage we don't know
     * the intersection set size, so we use a trick, append an empty object
     * to the output list and save the pointer to later modify it with the
     * right length */
    // 因为不知道结果集会有多少个元素，所有没有办法直接设置回复的数量
    // 这里使用了一个小技巧，直接使用一个 BUFF 列表，
    // 然后将之后的回复都添加到列表中
    if (!dstkey) {
        replylen = addDeferredMultiBulkLength(c);
    } else {
        /* If we have a target key where to store the resulting set
         * create this key with an empty set inside */
        dstset = createIntsetObject();
    }

    /* Iterate all the elements of the first (smallest) set, and test
     * the element against all the other sets, if at least one set does
     * not include the element it is discarded */
    // 遍历基数最小的第一个集合
    // 并将它的元素和所有其他集合进行对比
    // 如果有至少一个集合不包含这个元素，那么这个元素不属于交集
    si = setTypeInitIterator(sets[0]);
    while((encoding = setTypeNext(si,&eleobj,&intobj)) != -1) {
        // 遍历其他集合，检查元素是否在这些集合中存在
        for (j = 1; j < setnum; j++) {

            // 跳过第一个集合，因为它是结果集的起始值
            if (sets[j] == sets[0]) continue;

            // 元素的编码为 INTSET 
            // 在其他集合中查找这个对象是否存在
            if (encoding == REDIS_ENCODING_INTSET) {
                /* intset with intset is simple... and fast */
                if (sets[j]->encoding == REDIS_ENCODING_INTSET &&
                    !intsetFind((intset*)sets[j]->ptr,intobj))
                {
                    break;
                /* in order to compare an integer with an object we
                 * have to use the generic function, creating an object
                 * for this */
                } else if (sets[j]->encoding == REDIS_ENCODING_HT) {
                    eleobj = createStringObjectFromLongLong(intobj);
                    if (!setTypeIsMember(sets[j],eleobj)) {
                        decrRefCount(eleobj);
                        break;
                    }
                    decrRefCount(eleobj);
                }

            // 元素的编码为 字典
            // 在其他集合中查找这个对象是否存在
            } else if (encoding == REDIS_ENCODING_HT) {
                /* Optimization... if the source object is integer
                 * encoded AND the target set is an intset, we can get
                 * a much faster path. */
                if (eleobj->encoding == REDIS_ENCODING_INT &&
                    sets[j]->encoding == REDIS_ENCODING_INTSET &&
                    !intsetFind((intset*)sets[j]->ptr,(long)eleobj->ptr))
                {
                    break;
                /* else... object to object check is easy as we use the
                 * type agnostic API here. */
                } else if (!setTypeIsMember(sets[j],eleobj)) {
                    break;
                }
            }
        }

        /* Only take action when all sets contain the member */
        // 如果所有集合都带有目标元素的话，那么执行以下代码
        if (j == setnum) {

            // SINTER 命令，直接返回结果集元素
            if (!dstkey) {
                if (encoding == REDIS_ENCODING_HT)
                    addReplyBulk(c,eleobj);
                else
                    addReplyBulkLongLong(c,intobj);
                cardinality++;

            // SINTERSTORE 命令，将结果添加到结果集中
            } else {
                if (encoding == REDIS_ENCODING_INTSET) {
                    eleobj = createStringObjectFromLongLong(intobj);
                    setTypeAdd(dstset,eleobj);
                    decrRefCount(eleobj);
                } else {
                    setTypeAdd(dstset,eleobj);
                }
            }
        }
    }
    setTypeReleaseIterator(si);

    // SINTERSTORE 命令，将结果集关联到数据库
    if (dstkey) {
        /* Store the resulting set into the target, if the intersection
         * is not an empty set. */
        // 删除现在可能有的 dstkey
        int deleted = dbDelete(c->db,dstkey);

        // 如果结果集非空，那么将它关联到数据库中
        if (setTypeSize(dstset) > 0) {
            dbAdd(c->db,dstkey,dstset);
            addReplyLongLong(c,setTypeSize(dstset));
            notifyKeyspaceEvent(REDIS_NOTIFY_SET,"sinterstore",
                dstkey,c->db->id);
        } else {
            decrRefCount(dstset);
            addReply(c,shared.czero);
            if (deleted)
                notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",
                    dstkey,c->db->id);
        }

        signalModifiedKey(c->db,dstkey);

        server.dirty++;

    // SINTER 命令，回复结果集的基数
    } else {
        setDeferredMultiBulkLength(c,replylen,cardinality);
    }

    zfree(sets);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %r13
movq %rdx, %rbp
movq %rsi, 0x18(%rsp)
movq %rdi, %r12
leaq (,%rdx,8), %rdi
callq 0x299b5
movq %rax, %r15
testq %rbp, %rbp
je 0x3cb80
xorl %ebx, %ebx
movq 0x8(%r12), %rdi
movq 0x18(%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq %r13, %r14
testq %r13, %r13
je 0x3cb48
callq 0x31724
testq %rax, %rax
jne 0x3cb56
jmp 0x3ce94
callq 0x3163a
testq %rax, %rax
je 0x3ced5
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
movl $0x2, %edx
callq 0x30bbf
testl %eax, %eax
jne 0x3ce11
movq %r13, (%r15,%rbx,8)
incq %rbx
cmpq %rbx, %rbp
movq %r14, %r13
jne 0x3cb23
leaq -0xf7(%rip), %rcx       # 0x3ca90
movl $0x8, %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1f820
testq %r13, %r13
movq %r13, 0x30(%rsp)
je 0x3cbb6
movq $0x0, 0x28(%rsp)
callq 0x308dc
movq %rax, 0x10(%rsp)
jmp 0x3cbcc
movq %r12, %rdi
callq 0x2de9a
movq %rax, 0x28(%rsp)
movq $0x0, 0x10(%rsp)
movq (%r15), %rdi
callq 0x3ba3a
leaq 0x8(%rsp), %rsi
movq %rsp, %rdx
movq %rax, 0x18(%rsp)
movq %rax, %rdi
callq 0x3bac6
cmpl $-0x1, %eax
je 0x3cd86
movl %eax, %r13d
movq $0x0, 0x20(%rsp)
movq %r12, %rbx
movl $0x1, %r12d
movq 0x8(%rsp), %r14
cmpq $0x2, %rbp
jb 0x3ccd9
movq (%r15,%r12,8), %rdi
cmpq (%r15), %rdi
je 0x3ccca
cmpl $0x2, %r13d
je 0x3cc91
cmpl $0x6, %r13d
jne 0x3ccca
movl (%rdi), %eax
movl %eax, %ecx
andl $0xf0, %ecx
cmpl $0x60, %ecx
jne 0x3cc5d
movq 0x8(%rdi), %rdi
movq (%rsp), %rsi
callq 0x4f42d
testb %al, %al
je 0x3ccd9
movq (%r15,%r12,8), %rax
movl (%rax), %eax
andl $0xf0, %eax
cmpl $0x20, %eax
jne 0x3ccca
movq (%rsp), %rdi
callq 0x3069e
movq %rax, %r14
movq (%r15,%r12,8), %rdi
movq %rax, %rsi
callq 0x3b9b9
testl %eax, %eax
je 0x3cd74
movq %r14, %rdi
callq 0x30b0c
jmp 0x3ccca
movl (%r14), %eax
movl $0xf0, %ecx
andl %ecx, %eax
cmpl $0x10, %eax
jne 0x3ccbe
movl (%rdi), %eax
andl %ecx, %eax
cmpl $0x60, %eax
jne 0x3ccbe
movq 0x8(%rdi), %rdi
movq 0x8(%r14), %rsi
callq 0x4f42d
testb %al, %al
je 0x3ccd9
movq (%r15,%r12,8), %rdi
movq %r14, %rsi
callq 0x3b9b9
testl %eax, %eax
je 0x3ccd9
incq %r12
cmpq %r12, %rbp
jne 0x3cc16
movq %rbp, %r12
movq %r14, 0x8(%rsp)
cmpq %rbp, %r12
jne 0x3cd1c
cmpq $0x0, 0x30(%rsp)
movq %rbx, %r12
je 0x3cd21
cmpl $0x6, %r13d
jne 0x3cd34
movq (%rsp), %rdi
callq 0x3069e
movq %rax, %r14
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x3b6e0
movq %r14, %rdi
callq 0x30b0c
jmp 0x3cd54
movq %rbx, %r12
jmp 0x3cd54
cmpl $0x2, %r13d
jne 0x3cd43
movq %r12, %rdi
movq %r14, %rsi
callq 0x2e204
jmp 0x3cd4f
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x3b6e0
jmp 0x3cd54
movq (%rsp), %rsi
movq %r12, %rdi
callq 0x2e277
incq 0x20(%rsp)
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %rsp, %rdx
callq 0x3bac6
movl %eax, %r13d
cmpl $-0x1, %eax
jne 0x3cbfe
jmp 0x3cd8f
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x30b0c
jmp 0x3ccde
movq $0x0, 0x20(%rsp)
movq 0x18(%rsp), %rdi
callq 0x3baaa
movq 0x30(%rsp), %rbx
testq %rbx, %rbx
je 0x3cdfd
movq 0x8(%r12), %rdi
movq %rbx, %rsi
callq 0x31a36
movl %eax, %ebp
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x3bbf6
testq %rax, %rax
je 0x3ce27
movq 0x8(%r12), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x317f0
movq %r14, %rdi
callq 0x3bbf6
movq %r12, %rdi
movq %rax, %rsi
callq 0x2e133
movq 0x8(%r12), %rax
movl 0x30(%rax), %ecx
leaq 0x5759d(%rip), %rsi     # 0x94393
movl $0x20, %edi
jmp 0x3ce5a
movq %r12, %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x2ded6
jmp 0x3ce7d
movq %r15, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x29af7
movq %r14, %rdi
callq 0x30b0c
leaq 0x8d9ca(%rip), %rax     # 0xca800
movq 0x20(%rax), %rsi
movq %r12, %rdi
callq 0x2d97c
testl %ebp, %ebp
je 0x3ce62
movq 0x8(%r12), %rax
movl 0x30(%rax), %ecx
leaq 0x57688(%rip), %rsi     # 0x944dd
movl $0x4, %edi
movq %rbx, %rdx
callq 0x60371
movq 0x8(%r12), %rdi
movq %rbx, %rsi
callq 0x319b8
leaq 0x8d132(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
movq %r15, %rdi
callq 0x29af7
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x29af7
movq 0x8(%r12), %rdi
movq %r14, %rsi
callq 0x31a36
testl %eax, %eax
je 0x3cec8
movq 0x8(%r12), %rdi
movq %r14, %rsi
callq 0x319b8
leaq 0x8d0e7(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
leaq 0x8d931(%rip), %rax     # 0xca800
movq 0x20(%rax), %rsi
jmp 0x3cee8
movq %r15, %rdi
callq 0x29af7
leaq 0x8d91c(%rip), %rax     # 0xca800
movq 0x68(%rax), %rsi
movq %r12, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d97c