// File: /workspace/github_c_makefile_star2_O3/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 0x42540
cmpl $0x5, %eax
jne 0x425e0
movq %r14, %rdi
callq 0x30d93
movq %rax, %r14
movq %r15, %rdi
callq 0x30d93
movq %rax, %r15
movq 0x8(%rbx), %r12
xorl %ebp, %ebp
movq %r12, %rdi
xorl %esi, %esi
callq 0x2c28b
testq %rax, %rax
je 0x4255f
movq 0x8(%r14), %rsi
movl -0x8(%rsi), %edx
movq %rax, %rdi
movl $0x1, %ecx
callq 0x2ca1a
testq %rax, %rax
je 0x4255f
movq %r12, %rdi
movq %rax, %rsi
callq 0x2c558
movq %rax, (%rsp)
testq %rax, %rax
je 0x425fa
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x2c713
movq (%r13), %rsi
movq 0x8(%r15), %rdx
movl -0x8(%rdx), %ecx
movq %rax, %rdi
callq 0x2c70e
movl $0x1, %ebp
jmp 0x42587
movq 0x8(%rbx), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2263a
testl %eax, %eax
je 0x425c2
movq %r14, %rdi
callq 0x306f3
xorl %ebp, %ebp
jmp 0x425c7
movq 0x8(%r14), %rsi
movl -0x8(%rsi), %edx
movq %r12, %rdi
movl $0x1, %ecx
callq 0x2be64
movq 0x8(%r15), %rsi
movl -0x8(%rsi), %edx
movq %rax, %rdi
movl $0x1, %ecx
callq 0x2be64
movq %rax, 0x8(%rbx)
movq %r14, %rdi
callq 0x30b0c
movq %r15, %rdi
callq 0x30b0c
movq %rbx, %rdi
callq 0x4261c
leaq 0x879fe(%rip), %rcx     # 0xc9fa8
cmpq 0x758(%rcx), %rax
jbe 0x425cf
movq %rbx, %rdi
movl $0x2, %esi
callq 0x42183
jmp 0x425cf
movl $0x1, %ebp
movq %r15, %rdi
callq 0x306f3
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x56528(%rip), %rdi     # 0x98b0f
leaq 0x56fd4(%rip), %rsi     # 0x995c2
movl $0x142, %edx            # imm = 0x142
callq 0x4cae0
jmp 0x42612
leaq 0x56fca(%rip), %rdi     # 0x995cb
leaq 0x56fba(%rip), %rsi     # 0x995c2
movl $0x112, %edx            # imm = 0x112
callq 0x4ca32
movl $0x1, %edi
callq 0x1f3b0