// File: /workspace/github_c_makefile_star2_O2/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 0x46604(%rip), %rsi     # 0x8cbd3
callq 0x1fa60
testq %rax, %rax
je 0x46905
movq %rax, %rbx
leaq 0x809c1(%rip), %rax     # 0xc6fa8
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 0x46637
cmpq $0x0, 0xd8(%rsp)
jne 0x46637
leaq 0x80989(%rip), %rax     # 0xc6fa8
andq $0x0, 0x4f8(%rax)
movq %rbx, %rdi
callq 0x1f9a0
pushq $-0x1
popq %rax
jmp 0x4685a
leaq 0x8096a(%rip), %rax     # 0xc6fa8
andl $0x0, 0x4d0(%rax)
callq 0x464dd
movq %rax, %r15
movq %rbx, %rdi
callq 0x3595a
movq $0x0, 0x18(%rsp)
movq %r15, 0x10(%rsp)
movq 0x18(%rsp), %rax
xorl %edx, %edx
movl $0x3e8, %ecx            # imm = 0x3E8
divq %rcx
testq %rdx, %rdx
jne 0x4668c
movq %rbx, %rdi
callq 0x1fb50
movq %rax, %rdi
callq 0x359bc
callq 0x2e0b2
leaq 0x20(%rsp), %rdi
movl $0x80, %esi
movq %rbx, %rdx
callq 0x1f430
testq %rax, %rax
je 0x46804
cmpb $0x2a, 0x20(%rsp)
jne 0x4686c
leaq 0x21(%rsp), %rdi
callq 0x1f710
movl %eax, 0x8(%rsp)
testl %eax, %eax
jle 0x4686c
incq 0x18(%rsp)
movl 0x8(%rsp), %r13d
leaq (,%r13,8), %rdi
callq 0x2828d
movq %rax, %r12
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x46784
leaq 0x20(%rsp), %rdi
movl $0x80, %esi
movq %rbx, %rdx
callq 0x1f430
testq %rax, %rax
je 0x46885
cmpb $0x24, 0x20(%rsp)
jne 0x4686c
leaq 0x21(%rsp), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x1f6d0
movq %rax, %rbp
xorl %edi, %edi
movq %rax, %rsi
callq 0x27158
movq %rax, %r15
testq %rbp, %rbp
je 0x46752
movq %r15, %rdi
movq %rbp, %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x1f7c0
testq %rax, %rax
je 0x4686c
xorl %edi, %edi
movq %r15, %rsi
callq 0x2e9b4
movq %rax, (%r12,%r14,8)
leaq 0x20(%rsp), %rdi
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x1f7c0
incq %r14
testq %rax, %rax
jne 0x466e5
jmp 0x4686c
movq (%r12), %rax
movq 0x8(%rax), %rdi
callq 0x24bcc
testq %rax, %rax
je 0x468d1
movq 0x10(%rsp), %r15
movl 0x8(%rsp), %ecx
movl %ecx, 0x30(%r15)
movq %r12, 0x38(%r15)
movq %r15, %rdi
callq *0x8(%rax)
cmpl $0x0, 0x170(%r15)
jne 0x468bc
movq 0x60(%r15), %rax
cmpq $0x0, 0x28(%rax)
jne 0x468bc
testb $0x10, 0x90(%r15)
jne 0x468e5
xorl %r14d, %r14d
movslq 0x30(%r15), %rax
movq 0x38(%r15), %rdi
cmpq %rax, %r14
jge 0x467fa
movq (%rdi,%r14,8), %rdi
callq 0x2ee1f
incq %r14
jmp 0x467df
callq 0x283cd
jmp 0x46663
movq %rbx, %rdi
callq 0x1f980
testl %eax, %eax
je 0x46885
movq 0x10(%rsp), %r14
testb $0x8, 0x90(%r14)
jne 0x46885
movq %rbx, %rdi
callq 0x1f9a0
movq %r14, %rdi
callq 0x46584
leaq 0x80772(%rip), %rbx     # 0xc6fa8
movl 0xc(%rsp), %eax
movl %eax, 0x4d0(%rbx)
callq 0x359f3
callq 0x4691a
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 0x4a22d(%rip), %rsi     # 0x90aa0
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
pushq $0x1
popq %rdi
callq 0x1f2b0
movq %rbx, %rdi
callq 0x1f980
testl %eax, %eax
jne 0x468b3
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x4a1c7(%rip), %rsi     # 0x90a6b
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22613
jmp 0x4687d
leaq 0x4a17d(%rip), %rsi     # 0x90a37
jmp 0x46873
leaq 0x4a10d(%rip), %rdi     # 0x909d0
leaq 0x49d4b(%rip), %rsi     # 0x90615
movl $0x3b6, %edx            # imm = 0x3B6
jmp 0x468f8
movq (%r12), %rax
movq 0x8(%rax), %rdx
leaq 0x4a0be(%rip), %rsi     # 0x9099e
pushq $0x3
popq %rdi
jmp 0x468aa
leaq 0x4a122(%rip), %rdi     # 0x90a0e
leaq 0x49d22(%rip), %rsi     # 0x90615
movl $0x3b8, %edx            # imm = 0x3B8
callq 0x4906b
pushq $0x1
popq %rdi
callq 0x1f3b0
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x4a04a(%rip), %rsi     # 0x90962
jmp 0x468a4