// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/t_hash.c
// Function: hashTypeSet
int hashTypeSet(robj *o, robj *field, robj *value) {
    int update = 0;

    // 添加到 ziplist
    if (o->encoding == REDIS_ENCODING_ZIPLIST) {
        unsigned char *zl, *fptr, *vptr;

        // 解码成字符串或者数字
        field = getDecodedObject(field);
        value = getDecodedObject(value);

        // 遍历整个 ziplist ，尝试查找并更新 field （如果它已经存在的话）
        zl = o->ptr;
        fptr = ziplistIndex(zl, ZIPLIST_HEAD);
        if (fptr != NULL) {
            // 定位到域 field
            fptr = ziplistFind(fptr, field->ptr, sdslen(field->ptr), 1);
            if (fptr != NULL) {
                /* Grab pointer to the value (fptr points to the field) */
                // 定位到域的值
                vptr = ziplistNext(zl, fptr);
                redisAssert(vptr != NULL);

                // 标识这次操作为更新操作
                update = 1;

                /* Delete value */
                // 删除旧的键值对
                zl = ziplistDelete(zl, &vptr);

                /* Insert new value */
                // 添加新的键值对
                zl = ziplistInsert(zl, vptr, value->ptr, sdslen(value->ptr));
            }
        }

        // 如果这不是更新操作，那么这就是一个添加操作
        if (!update) {
            /* Push new field/value pair onto the tail of the ziplist */
            // 将新的 field-value 对推入到 ziplist 的末尾
            zl = ziplistPush(zl, field->ptr, sdslen(field->ptr), ZIPLIST_TAIL);
            zl = ziplistPush(zl, value->ptr, sdslen(value->ptr), ZIPLIST_TAIL);
        }
        
        // 更新对象指针
        o->ptr = zl;

        // 释放临时对象
        decrRefCount(field);
        decrRefCount(value);

        /* Check if the ziplist needs to be converted to a hash table */
        // 检查在添加操作完成之后，是否需要将 ZIPLIST 编码转换成 HT 编码
        if (hashTypeLength(o) > server.hash_max_ziplist_entries)
            hashTypeConvert(o, REDIS_ENCODING_HT);

    // 添加到字典
    } else if (o->encoding == REDIS_ENCODING_HT) {

        // 添加或替换键值对到字典
        // 添加返回 1 ，替换返回 0
        if (dictReplace(o->ptr, field, value)) { /* Insert */
            incrRefCount(field);
        } else { /* Update */
            update = 1;
        }

        incrRefCount(value);
    } else {
        redisPanic("Unknown hash encoding");
    }

    // 更新/添加指示变量
    return update;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movl (%rdi), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x2, %eax
je 0x3f3b2
cmpl $0x5, %eax
jne 0x3f44e
movq %r14, %rdi
callq 0x2f087
movq %rax, %r14
movq %r15, %rdi
callq 0x2f087
movq %rax, %r15
movq 0x8(%rbx), %r12
xorl %ebp, %ebp
movq %r12, %rdi
xorl %esi, %esi
callq 0x2abfb
testq %rax, %rax
je 0x3f3d1
movq 0x8(%r14), %rsi
movl -0x8(%rsi), %edx
pushq $0x1
popq %r13
movq %rax, %rdi
movl %r13d, %ecx
callq 0x2b27f
testq %rax, %rax
je 0x3f3d1
movq %r12, %rdi
movq %rax, %rsi
callq 0x2ae0d
movq %rax, (%rsp)
testq %rax, %rax
je 0x3f468
movq %rsp, %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2afb7
movq (%rbp), %rsi
movq 0x8(%r15), %rdx
movl -0x8(%rdx), %ecx
movq %rax, %rdi
callq 0x2afb2
movl %r13d, %ebp
jmp 0x3f3f9
movq 0x8(%rbx), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x21af4
testl %eax, %eax
je 0x3f432
movq %r14, %rdi
callq 0x2eacc
xorl %ebp, %ebp
jmp 0x3f435
movq 0x8(%r14), %rsi
movl -0x8(%rsi), %edx
pushq $0x1
popq %r13
movq %r12, %rdi
movl %r13d, %ecx
callq 0x2a889
movq 0x8(%r15), %rsi
movl -0x8(%rsi), %edx
movq %rax, %rdi
movl %r13d, %ecx
callq 0x2a889
movq %rax, 0x8(%rbx)
movq %r14, %rdi
callq 0x2ee1f
movq %r15, %rdi
callq 0x2ee1f
movq %rbx, %rdi
callq 0x3f488
leaq 0x87b8c(%rip), %rcx     # 0xc6fa8
cmpq 0x758(%rcx), %rax
jbe 0x3f43d
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x3f011
jmp 0x3f43d
pushq $0x1
popq %rbp
movq %r15, %rdi
callq 0x2eacc
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4f67e(%rip), %rdi     # 0x8ead3
leaq 0x5012a(%rip), %rsi     # 0x8f586
movl $0x142, %edx            # imm = 0x142
callq 0x490e8
jmp 0x3f480
leaq 0x50120(%rip), %rdi     # 0x8f58f
leaq 0x50110(%rip), %rsi     # 0x8f586
movl $0x112, %edx            # imm = 0x112
callq 0x4906b
pushq $0x1
popq %rdi
callq 0x1f3b0