// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
callq 0x252e0
movq %rax, -0x138(%rbp)
leaq -0x120(%rbp), %rax
movq %rax, -0x1d8(%rbp)
callq 0x1f410
movq -0x1d8(%rbp), %rdi
movl %eax, %ecx
movl $0x100, %esi            # imm = 0x100
leaq 0x9e1b2(%rip), %rdx     # 0xe2b9d
movb $0x0, %al
callq 0x1f120
leaq -0x120(%rbp), %rdi
leaq 0x9cbb6(%rip), %rsi     # 0xe15b6
callq 0x1fa70
movq %rax, -0x140(%rbp)
cmpq $0x0, -0x140(%rbp)
jne 0x44a44
callq 0x1f820
movl (%rax), %edi
callq 0x1f630
movq %rax, %rdx
movl $0x3, %edi
leaq 0x9e178(%rip), %rsi     # 0xe2ba9
movb $0x0, %al
callq 0x24fb0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x44dfe
movq -0x140(%rbp), %rsi
leaq -0x190(%rbp), %rdi
callq 0x75d70
leaq 0xcee9a(%rip), %rax     # 0x1138f8
cmpl $0x0, 0x594(%rax)
je 0x44a75
leaq 0x313a2(%rip), %rax     # 0x75e10
movq %rax, -0x178(%rbp)
leaq -0x12a(%rbp), %rdi
movl $0xa, %esi
leaq 0x9e144(%rip), %rdx     # 0xe2bcc
movl $0x6, %ecx
movb $0x0, %al
callq 0x1f120
leaq -0x12a(%rbp), %rsi
leaq -0x190(%rbp), %rdi
movl $0x9, %edx
callq 0x42f30
cmpl $-0x1, %eax
jne 0x44ab6
jmp 0x44dad
movl $0x0, -0x130(%rbp)
movl -0x130(%rbp), %eax
leaq 0xcee2b(%rip), %rcx     # 0x1138f8
cmpl 0x480(%rcx), %eax
jge 0x44c73
leaq 0xcee18(%rip), %rax     # 0x1138f8
movq 0x10(%rax), %rax
movslq -0x130(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x1a0(%rbp)
movq -0x1a0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1a8(%rbp)
movq -0x1a8(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x1a8(%rbp), %rcx
addq 0x48(%rcx), %rax
cmpq $0x0, %rax
jne 0x44b2b
jmp 0x44c5f
movq -0x1a8(%rbp), %rdi
callq 0x24240
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x44b5a
movq -0x140(%rbp), %rdi
callq 0x1f9b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x44dfe
leaq -0x190(%rbp), %rdi
movl $0xfe, %esi
callq 0x42f00
cmpl $-0x1, %eax
jne 0x44b75
jmp 0x44dad
movl -0x130(%rbp), %esi
leaq -0x190(%rbp), %rdi
callq 0x43150
cmpl $-0x1, %eax
jne 0x44b91
jmp 0x44dad
jmp 0x44b93
movq -0x18(%rbp), %rdi
callq 0x24270
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
je 0x44c56
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1b0(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x1c8(%rbp)
movl $0x1, -0x1bc(%rbp)
movl -0x1c0(%rbp), %eax
andl $-0x10, %eax
orl $0x0, %eax
movl %eax, -0x1c0(%rbp)
movl -0x1c0(%rbp), %eax
andl $0xffffff0f, %eax       # imm = 0xFFFFFF0F
orl $0x0, %eax
movl %eax, -0x1c0(%rbp)
movq -0x1b0(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x1a0(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x3dce0
movq %rax, -0x1d0(%rbp)
movq -0x1c8(%rbp), %rdx
movq -0x1d0(%rbp), %rcx
movq -0x138(%rbp), %r8
leaq -0x190(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x448c0
cmpl $-0x1, %eax
jne 0x44c51
jmp 0x44dad
jmp 0x44b93
movq -0x18(%rbp), %rdi
callq 0x243d0
movl -0x130(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x130(%rbp)
jmp 0x44ac0
movq $0x0, -0x18(%rbp)
leaq -0x190(%rbp), %rdi
movl $0xff, %esi
callq 0x42f00
cmpl $-0x1, %eax
jne 0x44c96
jmp 0x44dad
movq -0x170(%rbp), %rax
movq %rax, -0x198(%rbp)
leaq -0x190(%rbp), %rdi
leaq -0x198(%rbp), %rsi
movl $0x8, %edx
callq 0x44e10
movq -0x140(%rbp), %rdi
callq 0x1fc80
cmpl $-0x1, %eax
jne 0x44cd2
jmp 0x44dad
movq -0x140(%rbp), %rdi
callq 0x1f070
movl %eax, %edi
callq 0x1f5d0
cmpl $-0x1, %eax
jne 0x44cef
jmp 0x44dad
movq -0x140(%rbp), %rdi
callq 0x1f9b0
cmpl $-0x1, %eax
jne 0x44d05
jmp 0x44dad
leaq -0x120(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1fc40
cmpl $-0x1, %eax
jne 0x44d54
callq 0x1f820
movl (%rax), %edi
callq 0x1f630
movq %rax, %rdx
movl $0x3, %edi
leaq 0x9dea1(%rip), %rsi     # 0xe2bd6
movb $0x0, %al
callq 0x24fb0
leaq -0x120(%rbp), %rdi
callq 0x1f3a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x44dfe
movl $0x2, %edi
leaq 0x9dead(%rip), %rsi     # 0xe2c0d
movb $0x0, %al
callq 0x24fb0
leaq 0xceb8a(%rip), %rax     # 0x1138f8
movq $0x0, 0x560(%rax)
xorl %eax, %eax
movl %eax, %edi
callq 0x1fc70
movq %rax, %rcx
leaq 0xceb6c(%rip), %rax     # 0x1138f8
movq %rcx, 0x598(%rax)
leaq 0xceb5e(%rip), %rax     # 0x1138f8
movl $0x0, 0x5b8(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x44dfe
movq -0x140(%rbp), %rdi
callq 0x1f9b0
leaq -0x120(%rbp), %rdi
callq 0x1f3a0
callq 0x1f820
movl (%rax), %edi
callq 0x1f630
movq %rax, %rdx
movl $0x3, %edi
leaq 0x9de3e(%rip), %rsi     # 0xe2c1e
movb $0x0, %al
callq 0x24fb0
cmpq $0x0, -0x18(%rbp)
je 0x44df7
movq -0x18(%rbp), %rdi
callq 0x243d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
nopw (%rax,%rax)