// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/aof.c
// Function: loadAppendOnlyFile
int loadAppendOnlyFile(char *filename) {

    // 为客户端
    struct redisClient *fakeClient;

    // 打开 AOF 文件
    FILE *fp = fopen(filename,"r");

    struct redis_stat sb;
    int old_aof_state = server.aof_state;
    long loops = 0;

    // 检查文件的正确性
    if (fp && redis_fstat(fileno(fp),&sb) != -1 && sb.st_size == 0) {
        server.aof_current_size = 0;
        fclose(fp);
        return REDIS_ERR;
    }

    // 检查文件是否正常打开
    if (fp == NULL) {
        redisLog(REDIS_WARNING,"Fatal error: can't open the append log file for reading: %s",strerror(errno));
        exit(1);
    }

    /* Temporarily disable AOF, to prevent EXEC from feeding a MULTI
     * to the same file we're about to read. 
     *
     * 暂时性地关闭 AOF ，防止在执行 MULTI 时，
     * EXEC 命令被传播到正在打开的 AOF 文件中。
     */
    server.aof_state = REDIS_AOF_OFF;

    fakeClient = createFakeClient();

    // 设置服务器的状态为：正在载入
    // startLoading 定义于 rdb.c
    startLoading(fp);

    while(1) {
        int argc, j;
        unsigned long len;
        robj **argv;
        char buf[128];
        sds argsds;
        struct redisCommand *cmd;

        /* Serve the clients from time to time 
         *
         * 间隔性地处理客户端发送来的请求
         * 因为服务器正处于载入状态，所以能正常执行的只有 PUBSUB 等模块
         */
        if (!(loops++ % 1000)) {
            loadingProgress(ftello(fp));
            processEventsWhileBlocked();
        }

        // 读入文件内容到缓存
        if (fgets(buf,sizeof(buf),fp) == NULL) {
            if (feof(fp))
                // 文件已经读完，跳出
                break;
            else
                goto readerr;
        }

        // 确认协议格式，比如 *3\r\n
        if (buf[0] != '*') goto fmterr;
        
        // 取出命令参数，比如 *3\r\n 中的 3
        argc = atoi(buf+1);

        // 至少要有一个参数（被调用的命令）
        if (argc < 1) goto fmterr;

        // 从文本中创建字符串对象：包括命令，以及命令参数
        // 例如 $3\r\nSET\r\n$3\r\nKEY\r\n$5\r\nVALUE\r\n
        // 将创建三个包含以下内容的字符串对象：
        // SET 、 KEY 、 VALUE
        argv = zmalloc(sizeof(robj*)*argc);
        for (j = 0; j < argc; j++) {
            if (fgets(buf,sizeof(buf),fp) == NULL) goto readerr;

            if (buf[0] != '$') goto fmterr;

            // 读取参数值的长度
            len = strtol(buf+1,NULL,10);
            // 读取参数值
            argsds = sdsnewlen(NULL,len);
            if (len && fread(argsds,len,1,fp) == 0) goto fmterr;
            // 为参数创建对象
            argv[j] = createObject(REDIS_STRING,argsds);

            if (fread(buf,2,1,fp) == 0) goto fmterr; /* discard CRLF */
        }

        /* Command lookup 
         *
         * 查找命令
         */
        cmd = lookupCommand(argv[0]->ptr);
        if (!cmd) {
            redisLog(REDIS_WARNING,"Unknown command '%s' reading the append only file", (char*)argv[0]->ptr);
            exit(1);
        }

        /* Run the command in the context of a fake client 
         *
         * 调用伪客户端，执行命令
         */
        fakeClient->argc = argc;
        fakeClient->argv = argv;
        cmd->proc(fakeClient);

        /* The fake client should not have a reply */
        redisAssert(fakeClient->bufpos == 0 && listLength(fakeClient->reply) == 0);
        /* The fake client should never get blocked */
        redisAssert((fakeClient->flags & REDIS_BLOCKED) == 0);

        /* Clean up. Command code may have changed argv/argc so we use the
         * argv/argc of the client instead of the local variables. 
         *
         * 清理命令和命令参数对象
         */
        for (j = 0; j < fakeClient->argc; j++)
            decrRefCount(fakeClient->argv[j]);
        zfree(fakeClient->argv);
    }

    /* This point can only be reached when EOF is reached without errors.
     * If the client is in the middle of a MULTI/EXEC, log error and quit. 
     *
     * 如果能执行到这里，说明 AOF 文件的全部内容都可以正确地读取，
     * 但是，还要检查 AOF 是否包含未正确结束的事务
     */
    if (fakeClient->flags & REDIS_MULTI) goto readerr;

    // 关闭 AOF 文件
    fclose(fp);
    // 释放伪客户端
    freeFakeClient(fakeClient);
    // 复原 AOF 状态
    server.aof_state = old_aof_state;
    // 停止载入
    stopLoading();
    // 更新服务器状态中， AOF 文件的当前大小
    aofUpdateCurrentSize();
    // 记录前一次重写时的大小
    server.aof_rewrite_base_size = server.aof_current_size;
    
    return REDIS_OK;

// 读入错误
readerr:
    // 非预期的末尾，可能是 AOF 文件在写入的中途遭遇了停机
    if (feof(fp)) {
        redisLog(REDIS_WARNING,"Unexpected end of file reading the append only file");
    
    // 文件内容出错
    } else {
        redisLog(REDIS_WARNING,"Unrecoverable error reading the append only file: %s", strerror(errno));
    }
    exit(1);

// 内容格式错误
fmterr:
    redisLog(REDIS_WARNING,"Bad file format reading the append only file: make a backup of your AOF file, then use ./redis-check-aof --fix <filename>");
    exit(1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
leaq 0x585bd(%rip), %rsi     # 0xa211a
callq 0x1fa60
testq %rax, %rax
je 0x49ec4
movq %rax, %rbx
leaq 0x80433(%rip), %rax     # 0xc9fa8
movl 0x4d0(%rax), %eax
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
callq 0x1f070
leaq 0xa8(%rsp), %rsi
movl %eax, %edi
callq 0x1f690
cmpl $-0x1, %eax
je 0x49bca
cmpq $0x0, 0xd8(%rsp)
jne 0x49bca
leaq 0x803fb(%rip), %rax     # 0xc9fa8
movq $0x0, 0x4f8(%rax)
movq %rbx, %rdi
callq 0x1f9a0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x49e0f
leaq 0x803d7(%rip), %rax     # 0xc9fa8
movl $0x0, 0x4d0(%rax)
callq 0x49a62
movq %rax, %r15
movq %rbx, %rdi
callq 0x3816a
movq $0x0, 0x18(%rsp)
movq %r15, 0x10(%rsp)
movq 0x18(%rsp), %rax
movabsq $0x1cac083126e978d5, %rcx # imm = 0x1CAC083126E978D5
imulq %rcx, %rax
rorq $0x3, %rax
movabsq $0x4189374bc6a7ef, %rcx # imm = 0x4189374BC6A7EF
cmpq %rcx, %rax
ja 0x49c34
movq %rbx, %rdi
callq 0x1fb50
movq %rax, %rdi
callq 0x381cc
callq 0x2fc38
leaq 0x20(%rsp), %rdi
movl $0x80, %esi
movq %rbx, %rdx
callq 0x1f430
testq %rax, %rax
je 0x49db9
cmpb $0x2a, 0x20(%rsp)
jne 0x49e21
leaq 0x21(%rsp), %rdi
callq 0x1f710
movl %eax, 0x8(%rsp)
testl %eax, %eax
jle 0x49e21
incq 0x18(%rsp)
movl 0x8(%rsp), %r12d
leaq (,%r12,8), %rdi
callq 0x299b5
movq %rax, %r13
xorl %r14d, %r14d
leaq 0x20(%rsp), %rdi
movl $0x80, %esi
movq %rbx, %rdx
callq 0x1f430
testq %rax, %rax
je 0x49e3e
cmpb $0x24, 0x20(%rsp)
jne 0x49e21
leaq 0x21(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1f6d0
movq %rax, %r15
xorl %edi, %edi
movq %rax, %rsi
callq 0x283f4
movq %rax, %rbp
testq %r15, %r15
je 0x49cf5
movl $0x1, %edx
movq %rbp, %rdi
movq %r15, %rsi
movq %rbx, %rcx
callq 0x1f7c0
testq %rax, %rax
je 0x49e21
xorl %edi, %edi
movq %rbp, %rsi
callq 0x3059c
movq %rax, (%r13,%r14,8)
movl $0x2, %esi
movl $0x1, %edx
leaq 0x20(%rsp), %rdi
movq %rbx, %rcx
callq 0x1f7c0
testq %rax, %rax
je 0x49e21
incq %r14
cmpq %r14, %r12
jne 0x49c8d
movq (%r13), %rax
movq 0x8(%rax), %rdi
callq 0x25c20
testq %rax, %rax
je 0x49eae
movq 0x10(%rsp), %r15
movl 0x8(%rsp), %ecx
movl %ecx, 0x30(%r15)
movq %r13, 0x38(%r15)
movq %r15, %rdi
callq *0x8(%rax)
cmpl $0x0, 0x170(%r15)
jne 0x49e77
movq 0x60(%r15), %rax
cmpq $0x0, 0x28(%rax)
jne 0x49e77
testb $0x10, 0x90(%r15)
jne 0x49e8c
cmpl $0x0, 0x30(%r15)
jle 0x49dab
xorl %r14d, %r14d
movq 0x38(%r15), %rax
movq (%rax,%r14,8), %rdi
callq 0x30b0c
incq %r14
movslq 0x30(%r15), %rax
cmpq %rax, %r14
jl 0x49d92
movq 0x38(%r15), %rdi
callq 0x29af7
jmp 0x49bf9
movq %rbx, %rdi
callq 0x1f980
testl %eax, %eax
je 0x49e3e
movq 0x10(%rsp), %r14
testb $0x8, 0x90(%r14)
jne 0x49e3e
movq %rbx, %rdi
callq 0x1f9a0
movq %r14, %rdi
callq 0x49b12
leaq 0x801bd(%rip), %rbx     # 0xc9fa8
movl 0xc(%rsp), %eax
movl %eax, 0x4d0(%rbx)
callq 0x38203
callq 0x49ed9
movq 0x4f8(%rbx), %rax
movq %rax, 0x4f0(%rbx)
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x50cb4(%rip), %rsi     # 0x9aadc
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
movl $0x1, %edi
callq 0x1f2b0
movq %rbx, %rdi
callq 0x1f980
testl %eax, %eax
jne 0x49e6e
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x50c4a(%rip), %rsi     # 0x9aaa7
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23223
jmp 0x49e34
leaq 0x50bfe(%rip), %rsi     # 0x9aa73
jmp 0x49e28
leaq 0x50b8e(%rip), %rdi     # 0x9aa0c
leaq 0x507cc(%rip), %rsi     # 0x9a651
movl $0x3b6, %edx            # imm = 0x3B6
jmp 0x49e9f
leaq 0x50bb7(%rip), %rdi     # 0x9aa4a
leaq 0x507b7(%rip), %rsi     # 0x9a651
movl $0x3b8, %edx            # imm = 0x3B8
callq 0x4ca32
movl $0x1, %edi
callq 0x1f3b0
movq (%r13), %rax
movq 0x8(%rax), %rdx
leaq 0x50b1d(%rip), %rsi     # 0x9a9da
movl $0x3, %edi
jmp 0x49e65
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x50ac7(%rip), %rsi     # 0x9a99e
jmp 0x49e5d