// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x811f9(%rip), %rsi     # 0xe0cd3
callq 0x1fa70
movq %rax, -0x20(%rbp)
leaq 0xb3e0e(%rip), %rax     # 0x1138f8
movl 0x4d0(%rax), %eax
movl %eax, -0xb4(%rbp)
movq $0x0, -0xc0(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x5fb52
movq -0x20(%rbp), %rdi
callq 0x1f070
movl %eax, %edi
leaq -0xb0(%rbp), %rsi
callq 0x1f690
cmpl $-0x1, %eax
je 0x5fb52
cmpq $0x0, -0x80(%rbp)
jne 0x5fb52
leaq 0xb3dc6(%rip), %rax     # 0x1138f8
movq $0x0, 0x4f8(%rax)
movq -0x20(%rbp), %rdi
callq 0x1f9b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5ff8f
cmpq $0x0, -0x20(%rbp)
jne 0x5fb85
callq 0x1f820
movl (%rax), %edi
callq 0x1f630
movq %rax, %rdx
movl $0x3, %edi
leaq 0x84eae(%rip), %rsi     # 0xe4a22
movb $0x0, %al
callq 0x24fb0
movl $0x1, %edi
callq 0x1f2c0
leaq 0xb3d6c(%rip), %rax     # 0x1138f8
movl $0x0, 0x4d0(%rax)
callq 0x5f940
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rdi
callq 0x45d80
movq -0xc0(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0xc0(%rbp)
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
cmpq $0x0, %rdx
jne 0x5fbe3
movq -0x20(%rbp), %rdi
callq 0x1fb70
movq %rax, %rdi
callq 0x45e10
callq 0x38ec0
leaq -0x160(%rbp), %rdi
movq -0x20(%rbp), %rdx
movl $0x80, %esi
callq 0x1f430
cmpq $0x0, %rax
jne 0x5fc16
movq -0x20(%rbp), %rdi
callq 0x1f990
cmpl $0x0, %eax
je 0x5fc11
jmp 0x5febb
jmp 0x5ff23
movsbl -0x160(%rbp), %eax
cmpl $0x2a, %eax
je 0x5fc27
jmp 0x5ff72
leaq -0x160(%rbp), %rdi
addq $0x1, %rdi
callq 0x1f700
movl %eax, -0xc4(%rbp)
cmpl $0x1, -0xc4(%rbp)
jge 0x5fc4b
jmp 0x5ff72
movslq -0xc4(%rbp), %rdi
shlq $0x3, %rdi
callq 0x2e130
movq %rax, -0xd8(%rbp)
movl $0x0, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jge 0x5fd75
leaq -0x160(%rbp), %rdi
movq -0x20(%rbp), %rdx
movl $0x80, %esi
callq 0x1f430
cmpq $0x0, %rax
jne 0x5fc9e
jmp 0x5ff23
movsbl -0x160(%rbp), %eax
cmpl $0x24, %eax
je 0x5fcaf
jmp 0x5ff72
leaq -0x160(%rbp), %rdi
addq $0x1, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x1f6c0
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edi
callq 0x2c000
movq %rax, -0x168(%rbp)
cmpq $0x0, -0xd0(%rbp)
je 0x5fd17
movq -0x168(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
callq 0x1f7d0
cmpq $0x0, %rax
jne 0x5fd17
jmp 0x5ff72
movq -0x168(%rbp), %rsi
xorl %edi, %edi
callq 0x3a0d0
movq %rax, %rdx
movq -0xd8(%rbp), %rax
movslq -0xc8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
leaq -0x160(%rbp), %rdi
movq -0x20(%rbp), %rcx
movl $0x2, %esi
movl $0x1, %edx
callq 0x1f7d0
cmpq $0x0, %rax
jne 0x5fd5f
jmp 0x5ff72
jmp 0x5fd61
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x5fc6c
movq -0xd8(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
callq 0x28710
movq %rax, -0x170(%rbp)
cmpq $0x0, -0x170(%rbp)
jne 0x5fdc4
movq -0xd8(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdx
movl $0x3, %edi
leaq 0x84cab(%rip), %rsi     # 0xe4a5e
movb $0x0, %al
callq 0x24fb0
movl $0x1, %edi
callq 0x1f2c0
movl -0xc4(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0xd8(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x170(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rdi
callq *%rax
movq -0x18(%rbp), %rax
cmpl $0x0, 0x170(%rax)
jne 0x5fe0f
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
cmpq $0x0, 0x28(%rax)
jne 0x5fe0f
jmp 0x5fe31
leaq 0x84c7a(%rip), %rdi     # 0xe4a90
leaq 0x848b8(%rip), %rsi     # 0xe46d5
movl $0x3b6, %edx            # imm = 0x3B6
callq 0x63ca0
movl $0x1, %edi
callq 0x1f3b0
movq -0x18(%rbp), %rax
movl 0x90(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0x5fe45
jmp 0x5fe67
leaq 0x84c82(%rip), %rdi     # 0xe4ace
leaq 0x84882(%rip), %rsi     # 0xe46d5
movl $0x3b8, %edx            # imm = 0x3B8
callq 0x63ca0
movl $0x1, %edi
callq 0x1f3b0
movl $0x0, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jge 0x5fea9
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0xc8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3aaa0
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x5fe71
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x2e3a0
jmp 0x5fba8
movq -0x18(%rbp), %rax
movl 0x90(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x5fecf
jmp 0x5ff23
movq -0x20(%rbp), %rdi
callq 0x1f9b0
movq -0x18(%rbp), %rdi
callq 0x5fa70
movl -0xb4(%rbp), %ecx
leaq 0xb3a0a(%rip), %rax     # 0x1138f8
movl %ecx, 0x4d0(%rax)
callq 0x45e70
callq 0x5ffa0
leaq 0xb39f3(%rip), %rax     # 0x1138f8
movq 0x4f8(%rax), %rcx
leaq 0xb39e5(%rip), %rax     # 0x1138f8
movq %rcx, 0x4f0(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x5ff8f
movq -0x20(%rbp), %rdi
callq 0x1f990
cmpl $0x0, %eax
je 0x5ff46
movl $0x3, %edi
leaq 0x84bba(%rip), %rsi     # 0xe4af7
movb $0x0, %al
callq 0x24fb0
jmp 0x5ff68
callq 0x1f820
movl (%rax), %edi
callq 0x1f630
movq %rax, %rdx
movl $0x3, %edi
leaq 0x84bca(%rip), %rsi     # 0xe4b2b
movb $0x0, %al
callq 0x24fb0
movl $0x1, %edi
callq 0x1f2c0
movl $0x3, %edi
leaq 0x84be2(%rip), %rsi     # 0xe4b60
movb $0x0, %al
callq 0x24fb0
movl $0x1, %edi
callq 0x1f2c0
movl -0x4(%rbp), %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopl (%rax,%rax)