// File: /workspace/github_c_makefile_star2_O1/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, %r15
callq 0x232a3
movq %rax, 0x28(%rsp)
callq 0x1f410
leaq 0x61851(%rip), %rdx     # 0x98ad5
leaq 0x90(%rsp), %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x1f120
leaq 0x601b4(%rip), %rsi     # 0x97458
movq %rbx, %rdi
callq 0x1fa60
testq %rax, %rax
je 0x37592
movq %rax, %r14
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x58e24
leaq 0x92cdc(%rip), %rax     # 0xc9fa8
cmpl $0x0, 0x594(%rax)
je 0x372e1
leaq 0x21b8b(%rip), %rax     # 0x58e67
movq %rax, 0x48(%rsp)
leaq 0x6181c(%rip), %rdx     # 0x98b04
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 0x35d7c
cmpl $-0x1, %eax
je 0x375cf
movq %r15, 0x20(%rsp)
movq %r14, (%rsp)
leaq 0x92c76(%rip), %rcx     # 0xc9fa8
cmpl $0x0, 0x480(%rcx)
jle 0x3746b
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x10(%rcx), %rbx
movq %r13, %rbp
shlq $0x6, %rbp
movq (%rbx,%rbp), %rdi
movq 0x28(%rdi), %rax
movl $0x5, %r15d
addq 0x48(%rdi), %rax
je 0x37449
callq 0x228e4
testq %rax, %rax
je 0x37423
movq %rax, 0x8(%rsp)
movb $-0x2, 0x10(%rsp)
movl $0x1, %edx
leaq 0x30(%rsp), %r14
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x35d7c
movl $0x2, %r15d
cmpl $-0x1, %eax
je 0x37415
movq %r14, %rdi
movl %r13d, %esi
callq 0x35fd0
cmpl $-0x1, %eax
je 0x37415
addq %rbp, %rbx
movq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x22913
testq %rax, %rax
je 0x37437
movq (%rax), %rcx
movq 0x8(%rax), %rbp
movl $0x1, 0x14(%rsp)
movb $0x0, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movq %rbx, %rdi
movq %r12, %r14
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x326b1
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %r14, %r12
movq %rbp, %rdx
movq %rax, %rcx
movq 0x28(%rsp), %r8
callq 0x371ae
cmpl $-0x1, %eax
jne 0x373ba
jmp 0x37442
leaq 0x92b8c(%rip), %rcx     # 0xc9fa8
movq 0x8(%rsp), %r12
jmp 0x37449
movq (%rsp), %rdi
callq 0x1f9a0
movl $0x1, %r15d
xorl %r12d, %r12d
jmp 0x37442
movq %r12, %rdi
callq 0x229b9
xorl %r15d, %r15d
leaq 0x92b5f(%rip), %rcx     # 0xc9fa8
testl %r15d, %r15d
je 0x37458
cmpl $0x5, %r15d
jne 0x375c0
incq %r13
movslq 0x480(%rcx), %rax
cmpq %rax, %r13
jl 0x37345
leaq 0x10(%rsp), %rbx
movb $-0x1, (%rbx)
leaq 0x30(%rsp), %rdi
movl $0x1, %edx
movq %rbx, %rsi
callq 0x35d7c
cmpl $-0x1, %eax
je 0x375c8
leaq 0x30(%rsp), %r14
movq 0x20(%r14), %rax
movq %rax, 0x10(%rsp)
movl $0x8, %r12d
testq %r12, %r12
je 0x374f4
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 0x374d2
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 0x374ef
addq %r15, %rbx
subq %r15, %r12
addq %r15, 0x58(%rsp)
testq %rax, %rax
jne 0x374a2
movq (%rsp), %r14
movq %r14, %rdi
callq 0x1fc60
cmpl $-0x1, %eax
je 0x375bb
movq %r14, %rdi
callq 0x1f070
movl %eax, %edi
callq 0x1f5d0
cmpl $-0x1, %eax
je 0x375bb
movq %r14, %rdi
callq 0x1f9a0
cmpl $-0x1, %eax
je 0x375bb
leaq 0x90(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x1fc20
cmpl $-0x1, %eax
je 0x3762c
leaq 0x615f1(%rip), %rsi     # 0x98b45
xorl %ebx, %ebx
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
leaq 0x92a3f(%rip), %r14     # 0xc9fa8
movq $0x0, 0x560(%r14)
xorl %edi, %edi
callq 0x1fc50
movq %rax, 0x598(%r14)
movl $0x0, 0x5b8(%r14)
jmp 0x37618
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x6153c(%rip), %rsi     # 0x98ae1
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23064
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x37618
xorl %r12d, %r12d
jmp 0x375cf
cmpl $0x2, %r15d
jne 0x375b4
jmp 0x375cb
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 0x6155f(%rip), %rsi     # 0x98b56
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23064
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %r12, %r12
je 0x37618
movq %r12, %rdi
callq 0x229b9
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 0x614cf(%rip), %rsi     # 0x98b0e
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23064
leaq 0x90(%rsp), %rdi
callq 0x1f3a0
jmp 0x375b4