// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/rdb.c
// Function: rdbSave
int rdbSave(char *filename) {
    dictIterator *di = NULL;
    dictEntry *de;
    char tmpfile[256];
    char magic[10];
    int j;
    long long now = mstime();
    FILE *fp;
    rio rdb;
    uint64_t cksum;

    // 创建临时文件
    snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid());
    fp = fopen(tmpfile,"w");
    if (!fp) {
        redisLog(REDIS_WARNING, "Failed opening .rdb for saving: %s",
            strerror(errno));
        return REDIS_ERR;
    }

    // 初始化 I/O
    rioInitWithFile(&rdb,fp);

    // 设置校验和函数
    if (server.rdb_checksum)
        rdb.update_cksum = rioGenericUpdateChecksum;

    // 写入 RDB 版本号
    snprintf(magic,sizeof(magic),"REDIS%04d",REDIS_RDB_VERSION);
    if (rdbWriteRaw(&rdb,magic,9) == -1) goto werr;

    // 遍历所有数据库
    for (j = 0; j < server.dbnum; j++) {

        // 指向数据库
        redisDb *db = server.db+j;

        // 指向数据库键空间
        dict *d = db->dict;

        // 跳过空数据库
        if (dictSize(d) == 0) continue;

        // 创建键空间迭代器
        di = dictGetSafeIterator(d);
        if (!di) {
            fclose(fp);
            return REDIS_ERR;
        }

        /* Write the SELECT DB opcode 
         *
         * 写入 DB 选择器
         */
        if (rdbSaveType(&rdb,REDIS_RDB_OPCODE_SELECTDB) == -1) goto werr;
        if (rdbSaveLen(&rdb,j) == -1) goto werr;

        /* Iterate this DB writing every entry 
         *
         * 遍历数据库，并写入每个键值对的数据
         */
        while((de = dictNext(di)) != NULL) {
            sds keystr = dictGetKey(de);
            robj key, *o = dictGetVal(de);
            long long expire;
            
            // 根据 keystr ，在栈中创建一个 key 对象
            initStaticStringObject(key,keystr);

            // 获取键的过期时间
            expire = getExpire(db,&key);

            // 保存键值对数据
            if (rdbSaveKeyValuePair(&rdb,&key,o,expire,now) == -1) goto werr;
        }
        dictReleaseIterator(di);
    }
    di = NULL; /* So that we don't release it again on error. */

    /* EOF opcode 
     *
     * 写入 EOF 代码
     */
    if (rdbSaveType(&rdb,REDIS_RDB_OPCODE_EOF) == -1) goto werr;

    /* CRC64 checksum. It will be zero if checksum computation is disabled, the
     * loading code skips the check in this case. 
     *
     * CRC64 校验和。
     *
     * 如果校验和功能已关闭，那么 rdb.cksum 将为 0 ，
     * 在这种情况下， RDB 载入时会跳过校验和检查。
     */
    cksum = rdb.cksum;
    memrev64ifbe(&cksum);
    rioWrite(&rdb,&cksum,8);

    /* Make sure data will not remain on the OS's output buffers */
    // 冲洗缓存，确保数据已写入磁盘
    if (fflush(fp) == EOF) goto werr;
    if (fsync(fileno(fp)) == -1) goto werr;
    if (fclose(fp) == EOF) goto werr;

    /* Use RENAME to make sure the DB file is changed atomically only
     * if the generate DB file is ok. 
     *
     * 使用 RENAME ，原子性地对临时文件进行改名，覆盖原来的 RDB 文件。
     */
    if (rename(tmpfile,filename) == -1) {
        redisLog(REDIS_WARNING,"Error moving temp DB file on the final destination: %s", strerror(errno));
        unlink(tmpfile);
        return REDIS_ERR;
    }

    // 写入完成，打印日志
    redisLog(REDIS_NOTICE,"DB saved on disk");

    // 清零数据库脏状态
    server.dirty = 0;

    // 记录最后一次完成 SAVE 的时间
    server.lastsave = time(NULL);

    // 记录最后一次执行 SAVE 的状态
    server.lastbgsave_status = REDIS_OK;

    return REDIS_OK;

werr:
    // 关闭文件
    fclose(fp);
    // 删除文件
    unlink(tmpfile);

    redisLog(REDIS_WARNING,"Write error saving DB on disk: %s", strerror(errno));

    if (di) dictReleaseIterator(di);

    return REDIS_ERR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rdi, %r13
callq 0x22843
movq %rax, %r15
callq 0x1f410
leaq 0x59dc7(%rip), %rdx     # 0x8eaf5
leaq 0x90(%rsp), %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x1f120
leaq 0x5875e(%rip), %rsi     # 0x8d4ac
movq %rbx, %rdi
callq 0x1fa60
testq %rax, %rax
je 0x34ecc
movq %rax, %r14
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x541e8
leaq 0x92232(%rip), %rax     # 0xc6fa8
cmpl $0x0, 0x594(%rax)
je 0x34d8b
leaq 0x1f4b6(%rip), %rax     # 0x5423c
movq %rax, 0x58(%rsp)
leaq 0x59d92(%rip), %rdx     # 0x8eb24
xorl %r12d, %r12d
leaq 0x36(%rsp), %rbx
pushq $0xa
popq %rsi
pushq $0x6
popq %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1f120
leaq 0x40(%rsp), %rdi
pushq $0x9
popq %rdx
movq %rbx, %rsi
callq 0x33eca
cmpl $-0x1, %eax
je 0x34fc9
movq %r13, 0x18(%rsp)
movq %r14, 0x8(%rsp)
leaq 0x921d4(%rip), %r12     # 0xc6fa8
movl 0x480(%r12), %eax
xorl %edx, %edx
leaq 0x40(%rsp), %rbp
leaq 0x20(%rsp), %r14
movslq %eax, %rcx
cmpq %rcx, %rdx
jge 0x34ef4
movq 0x10(%r12), %rbx
movq %rdx, %r13
shlq $0x6, %r13
movq (%rbx,%r13), %rdi
movq 0x28(%rdi), %rcx
addq 0x48(%rdi), %rcx
je 0x34ec4
movq %rdx, 0x10(%rsp)
callq 0x21f20
testq %rax, %rax
je 0x34fad
movq %rax, %r12
movq %rbp, %rdi
movl $0xfe, %esi
callq 0x33eb4
cmpl $-0x1, %eax
je 0x34fc4
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0x33fef
cmpl $-0x1, %eax
je 0x34fc4
addq %r13, %rbx
movq %r12, %rdi
callq 0x21f2f
testq %rax, %rax
je 0x34ea8
movq (%rax), %rcx
movq 0x8(%rax), %r13
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30ab1
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rax, %rcx
movq %r15, %r8
callq 0x34c75
cmpl $-0x1, %eax
jne 0x34e57
jmp 0x34fc4
movq %r12, %rdi
callq 0x21fe6
leaq 0x920f1(%rip), %r12     # 0xc6fa8
movl 0x480(%r12), %eax
movq 0x10(%rsp), %rdx
incq %rdx
jmp 0x34de8
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x59c22(%rip), %rsi     # 0x8eb01
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22613
pushq $-0x1
popq %rbx
jmp 0x3500e
leaq 0x40(%rsp), %rdi
movl $0xff, %esi
callq 0x33eb4
cmpl $-0x1, %eax
je 0x34fc1
leaq 0x40(%rsp), %rdi
movq 0x20(%rdi), %rax
leaq 0x20(%rsp), %rsi
movq %rax, (%rsi)
pushq $0x8
popq %rdx
callq 0x35054
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x1fc60
cmpl $-0x1, %eax
je 0x34fbc
movq %r14, %rdi
callq 0x1f070
movl %eax, %edi
callq 0x1f5d0
cmpl $-0x1, %eax
je 0x34fbc
movq %r14, %rdi
callq 0x1f9a0
cmpl $-0x1, %eax
je 0x34fbc
leaq 0x90(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1fc20
cmpl $-0x1, %eax
je 0x35022
leaq 0x59be7(%rip), %rsi     # 0x8eb65
xorl %ebx, %ebx
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
andq $0x0, 0x560(%r12)
xorl %edi, %edi
callq 0x1fc50
movq %rax, 0x598(%r12)
andl $0x0, 0x5b8(%r12)
jmp 0x3500e
movq 0x8(%rsp), %rdi
callq 0x1f9a0
jmp 0x34eec
xorl %r12d, %r12d
jmp 0x34fc9
xorl %r12d, %r12d
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x1f9a0
leaq 0x90(%rsp), %rdi
callq 0x1f3a0
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x59b85(%rip), %rsi     # 0x8eb76
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22613
pushq $-0x1
popq %rbx
testq %r12, %r12
je 0x3500e
movq %r12, %rdi
callq 0x21fe6
movl %ebx, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x59af9(%rip), %rsi     # 0x8eb2e
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22613
leaq 0x90(%rsp), %rdi
callq 0x1f3a0
jmp 0x34eec