// File: /workspace/github_c_makefile_star2_O3/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 0x23461
movq %rax, 0x28(%rsp)
callq 0x1f410
leaq 0x616a7(%rip), %rdx     # 0x98b31
leaq 0x90(%rsp), %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x1f120
leaq 0x6000e(%rip), %rsi     # 0x974b8
movq %rbx, %rdi
callq 0x1fa60
testq %rax, %rax
je 0x37757
movq %rax, %r14
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x5887c
leaq 0x92ad6(%rip), %rbp     # 0xc9fa8
cmpl $0x0, 0x594(%rbp)
je 0x374e7
leaq 0x213f3(%rip), %rax     # 0x588d5
movq %rax, 0x48(%rsp)
leaq 0x61672(%rip), %rdx     # 0x98b60
xorl %r12d, %r12d
leaq 0x86(%rsp), %rbx
movl $0xa, %esi
movq %rbx, %rdi
movl $0x6, %ecx
xorl %eax, %eax
callq 0x1f120
leaq 0x30(%rsp), %rdi
movl $0x9, %edx
movq %rbx, %rsi
callq 0x361b8
cmpl $-0x1, %eax
je 0x3778c
movq %r13, 0x20(%rsp)
movq %r14, (%rsp)
movl 0x480(%rbp), %eax
testl %eax, %eax
jle 0x37644
movq 0x10(%rbp), %rbx
xorl %r13d, %r13d
leaq 0x30(%rsp), %r15
leaq 0x10(%rsp), %r14
movq %r13, %rbp
shlq $0x6, %rbp
movq (%rbx,%rbp), %rdi
movq 0x28(%rdi), %rcx
addq 0x48(%rdi), %rcx
jne 0x37571
leaq 0x92a3c(%rip), %rbp     # 0xc9fa8
jmp 0x37635
callq 0x22aa0
testq %rax, %rax
je 0x377e9
movq %rax, %r12
movq %r13, 0x8(%rsp)
movb $-0x2, 0x10(%rsp)
movl $0x1, %edx
movq %r15, %r13
movq %r15, %rdi
movq %r14, %rsi
callq 0x361b8
cmpl $-0x1, %eax
je 0x37788
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x363a7
cmpl $-0x1, %eax
je 0x37788
addq %rbp, %rbx
movq 0x28(%rsp), %r13
movq %r12, %rdi
callq 0x22acf
testq %rax, %rax
je 0x37617
movq (%rax), %rcx
movq 0x8(%rax), %rbp
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x329dd
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %rax, %rcx
movq %r13, %r8
callq 0x373b4
cmpl $-0x1, %eax
jne 0x375c6
jmp 0x37788
movq %r12, %rdi
callq 0x22b86
leaq 0x92982(%rip), %rbp     # 0xc9fa8
movq 0x10(%rbp), %rbx
movl 0x480(%rbp), %eax
movq 0x8(%rsp), %r13
incq %r13
movslq %eax, %rcx
cmpq %rcx, %r13
jl 0x37550
leaq 0x10(%rsp), %rbx
movb $-0x1, (%rbx)
leaq 0x30(%rsp), %rdi
movl $0x1, %edx
movq %rbx, %rsi
callq 0x361b8
cmpl $-0x1, %eax
je 0x37785
leaq 0x30(%rsp), %r14
movq 0x20(%r14), %rax
movq %rax, 0x10(%rsp)
movl $0x8, %r12d
movq 0x20(%rsp), %r13
movq 0x48(%rsp), %rax
movq 0x60(%rsp), %rcx
cmpq %r12, %rcx
movq %r12, %r15
cmovbq %rcx, %r15
testq %rcx, %rcx
cmoveq %r12, %r15
testq %rax, %rax
je 0x376ab
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq *%rax
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq *0x38(%rsp)
testq %rax, %rax
je 0x376ca
addq %r15, %rbx
addq %r15, 0x58(%rsp)
subq %r15, %r12
jne 0x37680
movq (%rsp), %r14
movq %r14, %rdi
callq 0x1fc60
cmpl $-0x1, %eax
je 0x37780
movq %r14, %rdi
callq 0x1f070
movl %eax, %edi
callq 0x1f5d0
cmpl $-0x1, %eax
je 0x37780
movq %r14, %rdi
callq 0x1f9a0
cmpl $-0x1, %eax
je 0x37780
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
callq 0x1fc20
cmpl $-0x1, %eax
je 0x377f4
leaq 0x6147d(%rip), %rsi     # 0x98ba1
xorl %ebx, %ebx
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
movq $0x0, 0x560(%rbp)
xorl %edi, %edi
callq 0x1fc50
movq %rax, 0x598(%rbp)
movl $0x0, 0x5b8(%rbp)
jmp 0x377d5
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x613d3(%rip), %rsi     # 0x98b3d
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23223
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x377d5
xorl %r12d, %r12d
jmp 0x3778c
xorl %r12d, %r12d
movq (%rsp), %r14
movq %r14, %rdi
callq 0x1f9a0
leaq 0x90(%rsp), %rdi
callq 0x1f3a0
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x613fe(%rip), %rsi     # 0x98bb2
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23223
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %r12, %r12
je 0x377d5
movq %r12, %rdi
callq 0x22b86
movl %ebx, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rdi
callq 0x1f9a0
jmp 0x37779
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x61363(%rip), %rsi     # 0x98b6a
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23223
leaq 0x90(%rsp), %rdi
callq 0x1f3a0
jmp 0x37779