// File: /workspace/github_c_makefile_star2_O2/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, 0x8(%rsp)
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r13
leaq (,%rdx,8), %rdi
callq 0x2828d
movq %rax, %r15
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x3a026
movq %r13, %rbp
movq 0x8(%r13), %rdi
movq (%r14,%rbx,8), %rsi
cmpq $0x0, 0x8(%rsp)
je 0x39ff7
callq 0x2f988
jmp 0x39ffc
callq 0x2f89e
movq %rax, %r13
testq %rax, %rax
je 0x3a058
movq %rbp, %rdi
movq %r13, %rsi
pushq $0x2
popq %rdx
callq 0x2eec6
testl %eax, %eax
jne 0x3a0a8
movq %r13, (%r15,%rbx,8)
incq %rbx
movq %rbp, %r13
jmp 0x39fd8
leaq -0xe3(%rip), %rcx       # 0x39f4a
pushq $0x8
popq %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x1f820
cmpq $0x0, 0x8(%rsp)
je 0x3a0be
movq $0x0, 0x38(%rsp)
callq 0x2ec4e
movq %rax, 0x20(%rsp)
jmp 0x3a0d4
movq %r15, %rdi
callq 0x283cd
movq 0x8(%rsp), %rbx
testq %rbx, %rbx
je 0x3a2fd
movq 0x8(%rbp), %rdi
movq %rbx, %rsi
callq 0x2fc3b
testl %eax, %eax
je 0x3a098
movq 0x8(%rbp), %rdi
movq %rbx, %rsi
callq 0x2fbc0
leaq 0x8cf17(%rip), %rax     # 0xc6fa8
incq 0x560(%rax)
leaq 0x8d761(%rip), %rax     # 0xc7800
movq 0x20(%rax), %rsi
jmp 0x3a308
movq %r15, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x283cd
movq %r13, %rdi
callq 0x2c506
movq %rax, 0x38(%rsp)
movq $0x0, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movq (%r15), %rdi
callq 0x38fcc
movq %rax, 0x40(%rsp)
movq $0x0, 0x10(%rsp)
movl $0xf0, %ebp
movq 0x40(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x39051
cmpl $-0x1, %eax
je 0x3a27a
movl %eax, %r14d
movq 0x30(%rsp), %r13
pushq $0x1
popq %rbx
cmpq %r12, %rbx
jae 0x3a1db
movq (%r15,%rbx,8), %rdi
cmpq (%r15), %rdi
je 0x3a1d3
cmpl $0x2, %r14d
je 0x3a19e
cmpl $0x6, %r14d
jne 0x3a1d3
movl (%rdi), %eax
movl %eax, %ecx
andl $0xf0, %ecx
cmpl $0x60, %ecx
jne 0x3a169
movq 0x8(%rdi), %rdi
movq 0x28(%rsp), %rsi
callq 0x4b5a5
testb %al, %al
je 0x3a1db
movq (%r15,%rbx,8), %rax
movl (%rax), %eax
andl $0xf0, %eax
cmpl $0x20, %eax
jne 0x3a1d3
movq 0x28(%rsp), %rdi
callq 0x2ea90
movq %rax, %r13
movq (%r15,%rbx,8), %rdi
movq %rax, %rsi
callq 0x38f4d
testl %eax, %eax
je 0x3a235
movq %r13, %rdi
callq 0x2ee1f
jmp 0x3a1d3
movl (%r13), %eax
andl %ebp, %eax
cmpl $0x10, %eax
jne 0x3a1c7
movl (%rdi), %eax
andl %ebp, %eax
cmpl $0x60, %eax
jne 0x3a1c7
movq 0x8(%rdi), %rdi
movq 0x8(%r13), %rsi
callq 0x4b5a5
testb %al, %al
je 0x3a1db
movq (%r15,%rbx,8), %rdi
movq %r13, %rsi
callq 0x38f4d
testl %eax, %eax
je 0x3a1db
incq %rbx
jmp 0x3a11c
movq %r13, 0x30(%rsp)
cmpq %rbx, %r12
jne 0x3a0f4
cmpq $0x0, 0x8(%rsp)
je 0x3a244
cmpl $0x6, %r14d
jne 0x3a223
movq 0x28(%rsp), %rdi
callq 0x2ea90
movq %rax, %r14
movq %rax, 0x30(%rsp)
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x38c8c
movq %r14, %rdi
callq 0x2ee1f
jmp 0x3a0f4
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x38c8c
jmp 0x3a0f4
movq %r13, 0x30(%rsp)
movq %r13, %rdi
callq 0x2ee1f
jmp 0x3a1e0
cmpl $0x2, %r14d
jne 0x3a259
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x2c84a
jmp 0x3a268
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0x2c8bb
movq 0x10(%rsp), %rax
incq %rax
movq %rax, 0x10(%rsp)
jmp 0x3a0f4
movq 0x40(%rsp), %rdi
callq 0x39035
movq 0x8(%rsp), %r14
testq %r14, %r14
je 0x3a2e7
movq 0x18(%rsp), %rbx
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x2fc3b
movl %eax, %ebp
movq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x3917b
testq %rax, %rax
je 0x3a31e
movq 0x8(%rbx), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2fa09
movq %r12, %rdi
callq 0x3917b
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2c794
movq 0x8(%rbx), %rax
movl 0x30(%rax), %ecx
leaq 0x50070(%rip), %rsi     # 0x8a353
pushq $0x20
jmp 0x3a34d
movq 0x18(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x2c542
jmp 0x3a370
leaq 0x8d4fc(%rip), %rax     # 0xc7800
movq 0x68(%rax), %rsi
movq %rbp, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c10d
movq %r12, %rdi
callq 0x2ee1f
leaq 0x8d4d3(%rip), %rax     # 0xc7800
movq 0x20(%rax), %rsi
movq %rbx, %rdi
callq 0x2c10d
testl %ebp, %ebp
je 0x3a356
movq 0x8(%rbx), %rax
movl 0x30(%rax), %ecx
leaq 0x50152(%rip), %rsi     # 0x8a49d
pushq $0x4
popq %rdi
movq %r14, %rdx
callq 0x5b488
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x2fbc0
leaq 0x8cc3f(%rip), %rax     # 0xc6fa8
incq 0x560(%rax)
movq %r15, %rdi
callq 0x283cd
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq