// File: /workspace/github_c_makefile_star2_O1/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 0x5800f(%rip), %rsi     # 0xa208a
callq 0x1fa60
testq %rax, %rax
je 0x4a3fe
movq %rax, %rbx
leaq 0x7ff15(%rip), %r14     # 0xc9fa8
movl 0x4d0(%r14), %ebp
movq %rax, %rdi
callq 0x1f070
leaq 0xa8(%rsp), %rsi
movl %eax, %edi
callq 0x1f690
cmpl $-0x1, %eax
je 0x4a0de
cmpq $0x0, 0xd8(%rsp)
jne 0x4a0de
movq $0x0, 0x4f8(%r14)
movq %rbx, %rdi
callq 0x1f9a0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x4a396
movl %ebp, 0x8(%rsp)
movl $0x0, 0x4d0(%r14)
callq 0x49f80
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
callq 0x37fe6
xorl %r14d, %r14d
movabsq $0x1cac083126e978d5, %r13 # imm = 0x1CAC083126E978D5
leaq 0x20(%rsp), %rbp
movq %r14, %rax
imulq %r13, %rax
rorq $0x3, %rax
movabsq $0x4189374bc6a7ef, %rcx # imm = 0x4189374BC6A7EF
cmpq %rcx, %rax
ja 0x4a140
movq %rbx, %rdi
callq 0x1fb50
movq %rax, %rdi
callq 0x38048
callq 0x2f949
movq %rbp, %rdi
movl $0x80, %esi
movq %rbx, %rdx
callq 0x1f430
testq %rax, %rax
je 0x4a2dd
movl $0x5, %eax
cmpb $0x2a, 0x20(%rsp)
jne 0x4a32c
leaq 0x21(%rsp), %rdi
callq 0x1f710
testl %eax, %eax
jle 0x4a2f2
movq %r14, (%rsp)
movl %eax, 0xc(%rsp)
movl %eax, %eax
movq %rax, 0x18(%rsp)
leaq (,%rax,8), %rdi
callq 0x29709
movq %rax, %r14
xorl %r13d, %r13d
movq %rbp, %rdi
movl $0x80, %esi
movq %rbx, %rdx
callq 0x1f430
testq %rax, %rax
je 0x4a305
cmpb $0x24, 0x20(%rsp)
jne 0x4a2f9
movq %rbp, %r12
leaq 0x21(%rsp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1f6d0
movq %rax, %rbp
xorl %edi, %edi
movq %rax, %rsi
callq 0x28148
movq %rax, %r15
testq %rbp, %rbp
je 0x4a206
movl $0x1, %edx
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rcx
callq 0x1f7c0
testq %rax, %rax
je 0x4a316
xorl %edi, %edi
movq %r15, %rsi
callq 0x302c4
movq %rax, (%r14,%r13,8)
movl $0x2, %esi
movl $0x1, %edx
movq %r12, %rbp
movq %r12, %rdi
movq %rbx, %rcx
callq 0x1f7c0
testq %rax, %rax
je 0x4a2f9
incq %r13
cmpq %r13, 0x18(%rsp)
jne 0x4a19d
movq (%r14), %rax
movq 0x8(%rax), %rdi
callq 0x25a1e
testq %rax, %rax
je 0x4a44a
movq 0x10(%rsp), %r12
movl 0xc(%rsp), %ecx
movl %ecx, 0x30(%r12)
movq %r14, 0x38(%r12)
movq %r12, %rdi
callq *0x8(%rax)
cmpl $0x0, 0x170(%r12)
movabsq $0x1cac083126e978d5, %r13 # imm = 0x1CAC083126E978D5
jne 0x4a413
movq 0x60(%r12), %rax
cmpq $0x0, 0x28(%rax)
jne 0x4a413
testb $0x10, 0x90(%r12)
jne 0x4a428
cmpl $0x0, 0x30(%r12)
jle 0x4a2cf
xorl %r15d, %r15d
movq 0x38(%r12), %rax
movq (%rax,%r15,8), %rdi
callq 0x30834
incq %r15
movslq 0x30(%r12), %rax
cmpq %rax, %r15
jl 0x4a2b4
movq 0x38(%r12), %rdi
callq 0x2984b
xorl %eax, %eax
jmp 0x4a328
movq %rbx, %rdi
callq 0x1f980
cmpl $0x1, %eax
movl $0x0, %eax
adcl $0x3, %eax
jmp 0x4a32c
movl $0x5, %eax
jmp 0x4a32c
movabsq $0x1cac083126e978d5, %r13 # imm = 0x1CAC083126E978D5
jmp 0x4a323
movl $0x4, %eax
movabsq $0x1cac083126e978d5, %r13 # imm = 0x1CAC083126E978D5
jmp 0x4a328
movabsq $0x1cac083126e978d5, %r13 # imm = 0x1CAC083126E978D5
movq %r12, %rbp
movl $0x5, %eax
movq (%rsp), %r14
incq %r14
testl %eax, %eax
je 0x4a111
cmpl $0x3, %eax
je 0x4a34c
cmpl $0x4, %eax
je 0x4a3a8
cmpl $0x5, %eax
je 0x4a3d8
jmp 0x4a396
movq 0x10(%rsp), %r14
testb $0x8, 0x90(%r14)
jne 0x4a3a8
movq %rbx, %rdi
callq 0x1f9a0
movq %r14, %rdi
callq 0x4a030
leaq 0x7fc36(%rip), %rbx     # 0xc9fa8
movl 0x8(%rsp), %eax
movl %eax, 0x4d0(%rbx)
callq 0x3807f
callq 0x4a462
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
movq %rbx, %rdi
callq 0x1f980
testl %eax, %eax
jne 0x4a3e1
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x50698(%rip), %rsi     # 0x9aa5f
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23064
jmp 0x4a3f4
leaq 0x506b5(%rip), %rsi     # 0x9aa94
jmp 0x4a3e8
leaq 0x50643(%rip), %rsi     # 0x9aa2b
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
movl $0x1, %edi
callq 0x1f2b0
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x50545(%rip), %rsi     # 0x9a956
jmp 0x4a3c7
leaq 0x505aa(%rip), %rdi     # 0x9a9c4
leaq 0x501e8(%rip), %rsi     # 0x9a609
movl $0x3b6, %edx            # imm = 0x3B6
jmp 0x4a43b
leaq 0x505d3(%rip), %rdi     # 0x9aa02
leaq 0x501d3(%rip), %rsi     # 0x9a609
movl $0x3b8, %edx            # imm = 0x3B8
callq 0x4d037
movl $0x1, %edi
callq 0x1f3b0
movq (%r14), %rax
movq 0x8(%rax), %rdx
leaq 0x5053a(%rip), %rsi     # 0x9a992
movl $0x3, %edi
jmp 0x4a3cf