// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rcx, %r13
movq %rdx, %rbp
movq %rsi, 0x20(%rsp)
movq %rdi, %r12
leaq (,%rdx,8), %rdi
callq 0x29709
movq %rax, %r15
testq %rbp, %rbp
je 0x3cafb
xorl %ebx, %ebx
movq 0x20(%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq %r13, %r14
testq %r13, %r13
je 0x3cabe
movq 0x8(%r12), %rdi
callq 0x31454
jmp 0x3cac8
movq 0x8(%r12), %rdi
callq 0x3136a
movq %rax, %r13
testq %rax, %rax
je 0x3cd9b
movq %r12, %rdi
movq %r13, %rsi
movl $0x2, %edx
callq 0x308e7
testl %eax, %eax
jne 0x3cde8
movq %r13, (%r15,%rbx,8)
incq %rbx
cmpq %rbx, %rbp
movq %r14, %r13
jne 0x3caa1
leaq -0xf4(%rip), %rcx       # 0x3ca0e
movl $0x8, %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1f820
testq %r13, %r13
movq %r13, 0x38(%rsp)
je 0x3cb31
movq $0x0, 0x30(%rsp)
callq 0x30604
movq %rax, 0x18(%rsp)
jmp 0x3cb47
movq %r12, %rdi
callq 0x2dc0a
movq %rax, 0x30(%rsp)
movq $0x0, 0x18(%rsp)
movq (%r15), %rdi
callq 0x3b9ce
leaq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rax, 0x20(%rsp)
movq %rax, %rdi
callq 0x3ba5a
cmpl $-0x1, %eax
je 0x3cd06
movl %eax, %r13d
movq $0x0, 0x28(%rsp)
movl $0xf0, %ebx
movq %r12, 0x40(%rsp)
movl $0x1, %r12d
movq 0x8(%rsp), %r14
cmpq $0x2, %rbp
jb 0x3cc4d
movq (%r15,%r12,8), %rax
cmpq (%r15), %rax
je 0x3cc3e
cmpl $0x2, %r13d
je 0x3cc0a
cmpl $0x6, %r13d
jne 0x3cc3e
movl (%rax), %ecx
andl %ebx, %ecx
cmpl $0x60, %ecx
jne 0x3cbd2
movq 0x8(%rax), %rdi
movq 0x10(%rsp), %rsi
callq 0x4f97c
testb %al, %al
je 0x3cc4d
movq (%r15,%r12,8), %rax
movl (%rax), %eax
andl %ebx, %eax
cmpl $0x20, %eax
jne 0x3cc3e
movq 0x10(%rsp), %rdi
callq 0x303c6
movq %rax, %r14
movq (%r15,%r12,8), %rdi
movq %rax, %rsi
callq 0x3b94d
testl %eax, %eax
je 0x3ccf4
movq %r14, %rdi
callq 0x30834
jmp 0x3cc3e
movl (%r14), %ecx
andl %ebx, %ecx
cmpl $0x10, %ecx
jne 0x3cc2e
movl (%rax), %ecx
andl %ebx, %ecx
cmpl $0x60, %ecx
jne 0x3cc2e
movq 0x8(%rax), %rdi
movq 0x8(%r14), %rsi
callq 0x4f97c
testb %al, %al
je 0x3cc4d
movq (%r15,%r12,8), %rdi
movq %r14, %rsi
callq 0x3b94d
testl %eax, %eax
je 0x3cc4d
incq %r12
cmpq %r12, %rbp
jne 0x3cb9a
movq %rbp, %r12
movq %r14, 0x8(%rsp)
cmpq %rbp, %r12
jne 0x3cc93
cmpq $0x0, 0x38(%rsp)
movq 0x40(%rsp), %r12
je 0x3cc9a
cmpl $0x6, %r13d
jne 0x3ccaf
movq 0x10(%rsp), %rdi
callq 0x303c6
movq %rax, %r14
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x3b674
movq %r14, %rdi
callq 0x30834
jmp 0x3ccd2
movq 0x40(%rsp), %r12
jmp 0x3ccd2
cmpl $0x2, %r13d
jne 0x3ccc0
movq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x2df74
jmp 0x3cccd
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0x3b674
jmp 0x3ccd2
movq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x2dfe7
incq 0x28(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x3ba5a
movl %eax, %r13d
cmpl $-0x1, %eax
jne 0x3cb85
jmp 0x3cd0f
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x30834
jmp 0x3cc52
movq $0x0, 0x28(%rsp)
movq 0x20(%rsp), %rdi
callq 0x3ba3e
movq 0x38(%rsp), %rbx
testq %rbx, %rbx
je 0x3cd84
movq 0x8(%r12), %rdi
movq %rbx, %rsi
callq 0x3174b
movl %eax, %ebp
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x3bb8a
testq %rax, %rax
je 0x3cdfe
movq 0x8(%r12), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x314ef
movq %r14, %rdi
callq 0x3bb8a
movq %r12, %rdi
movq %rax, %rsi
callq 0x2dea3
movq 0x8(%r12), %rax
movl 0x30(%rax), %ecx
leaq 0x575c9(%rip), %rsi     # 0x94343
movl $0x20, %edi
jmp 0x3ce31
movq %r12, %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x2dc46
jmp 0x3ce54
movq %r15, %rdi
callq 0x2984b
testq %r14, %r14
je 0x3ce6b
movq 0x8(%r12), %rdi
movq %r14, %rsi
callq 0x3174b
testl %eax, %eax
je 0x3cdd8
movq 0x8(%r12), %rdi
movq %r14, %rsi
callq 0x316b7
leaq 0x8d1d7(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
leaq 0x8da21(%rip), %rax     # 0xca800
movq 0x20(%rax), %rsi
jmp 0x3ce76
movq %r15, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2984b
movq %r14, %rdi
callq 0x30834
leaq 0x8d9f3(%rip), %rax     # 0xca800
movq 0x20(%rax), %rsi
movq %r12, %rdi
callq 0x2d6f0
testl %ebp, %ebp
je 0x3ce39
movq 0x8(%r12), %rax
movl 0x30(%rax), %ecx
leaq 0x57661(%rip), %rsi     # 0x9448d
movl $0x4, %edi
movq %rbx, %rdx
callq 0x608b5
movq 0x8(%r12), %rdi
movq %rbx, %rsi
callq 0x316b7
leaq 0x8d15b(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
movq %r15, %rdi
callq 0x2984b
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8d98e(%rip), %rax     # 0xca800
movq 0x68(%rax), %rsi
movq %r12, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d6f0