// File: /workspace/github_c_makefile_star2_O1/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 0x426d4
cmpl $0x5, %eax
jne 0x42711
movq %r14, %rdi
callq 0x30ac3
movq %rax, %r14
movq %r15, %rdi
callq 0x30ac3
movq %rax, %r15
movq 0x8(%rbx), %r12
xorl %ebp, %ebp
movq %r12, %rdi
xorl %esi, %esi
callq 0x2bff5
movb $0x1, %r13b
testq %rax, %rax
je 0x42669
movq 0x8(%r14), %rsi
movl -0x8(%rsi), %edx
movq %rax, %rdi
movl $0x1, %ecx
callq 0x2c76c
testq %rax, %rax
je 0x42669
movq %r12, %rdi
movq %rax, %rsi
callq 0x2c2b3
movq %rax, (%rsp)
testq %rax, %rax
je 0x4272b
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x2c46e
movq (%r13), %rsi
movq 0x8(%r15), %rdx
movl -0x8(%rdx), %ecx
movq %rax, %rdi
callq 0x2c469
movq %rax, %r12
movl $0x1, %ebp
xorl %r13d, %r13d
testb %r13b, %r13b
je 0x42699
movq 0x8(%r14), %rsi
movl -0x8(%rsi), %edx
movq %r12, %rdi
movl $0x1, %ecx
callq 0x2bc04
movq 0x8(%r15), %rsi
movl -0x8(%rsi), %edx
movq %rax, %rdi
movl $0x1, %ecx
callq 0x2bc04
movq %rax, %r12
movq %r12, 0x8(%rbx)
movq %r14, %rdi
callq 0x30834
movq %r15, %rdi
callq 0x30834
movq %rbx, %rdi
callq 0x4274d
leaq 0x878ec(%rip), %rcx     # 0xc9fa8
cmpq 0x758(%rcx), %rax
jbe 0x42700
movq %rbx, %rdi
movl $0x2, %esi
callq 0x4229b
jmp 0x42700
movq 0x8(%rbx), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2246a
testl %eax, %eax
je 0x426f3
movq %r14, %rdi
callq 0x3041b
xorl %ebp, %ebp
jmp 0x426f8
movl $0x1, %ebp
movq %r15, %rdi
callq 0x3041b
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5639b(%rip), %rdi     # 0x98ab3
leaq 0x56e5b(%rip), %rsi     # 0x9957a
movl $0x142, %edx            # imm = 0x142
callq 0x4d0e5
jmp 0x42743
leaq 0x56e51(%rip), %rdi     # 0x99583
leaq 0x56e41(%rip), %rsi     # 0x9957a
movl $0x112, %edx            # imm = 0x112
callq 0x4d037
movl $0x1, %edi
callq 0x1f3b0