// File: /workspace/github_cpp_makefile_star2_O2/zimpha[P]MiniSQL/API.cc
// Function: API::Delete(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Filter const&)
Response API::Delete(const std::string &tableName, const Filter &filter) {
    if (!cm.hasTable(tableName + ".table")) {
        return Response("Table does not exist");
    }
    
    Table nt = cm.loadTable(tableName + ".table");
    std::string dbName = tableName + ".db";
    for (size_t i = 0; i < filter.rules.size(); ++ i) {
        element val = filter.rules[i].val;
        int attrIndex = filter.rules[i].index;
        if (nt.attributes[attrIndex].type != val.type) {
            return Response("Type mismatch");
        }
    }
    
    for (size_t i = 0; i < filter.rules.size(); ++ i) {
        if (filter.rules[i].type == 2) { // =
            element val = filter.rules[i].val;
            int attrIndex = filter.rules[i].index;
            if (!nt.attributes[attrIndex].indices.empty()) { // has index on it
                std::string indexFileName = tableName + "." + nt.attributes[attrIndex].name;
                long offset = getOffset(indexFileName, val);
                // offset == -1 表示没有这个val
                // Record Manager delete records by using index
                // 传入数据库文件名（dbName），偏移量(offset)，过滤器(filter)，当前表(nt)
                rm.RecordManagerRecordDelete(dbName, offset, filter, nt);
                return Response();
            }
        }
    }
    /*
    if (filter.rules.empty()) {
        // Record Manager delete records without index
        // 传入数据库文件名（dbName），过滤器(filter)，当前表(nt)
        rm.rmDeleteWithoutIndex(dbName, filter, nt);
        return Response();
    }
    */
    std::set<long> offset;
    // Record Manager get offset according to dbName
    // 传入数据库文件名(daName)，返回一个offset的集合(类型set)
    offset = rm.RecordManagerGetAllOffsets(dbName, nt);
    for (size_t i = 0; i < filter.rules.size(); ++ i) {
        Rule rule = filter.rules[i];
        int attrIndex = rule.index;
        if (nt.attributes[attrIndex].indices.empty()) {
            continue;
        }
        element val = rule.val;
        std::string indexFileName = tableName + "." + nt.attributes[attrIndex].name;
        std::set<long> newOffset;
        long tmpOffset;
        // B plus tree get offset according to filter rules
        // 传入文件名(indexFileName)，属性的值(val)
        switch (rule.type) {
            case 0: // <
                newOffset = getLessOffset(indexFileName, val);
                break;
            case 1: // <=
                newOffset = getLessOffset(indexFileName, val);
                tmpOffset = getOffset(indexFileName, val);
                if (tmpOffset != -1) {
                    newOffset.insert(tmpOffset);
                }
                break;
            case 2: // =
                assert(false);
                break;
            case 3: // >=
                newOffset = getMoreOffset(indexFileName, val);
                tmpOffset = getOffset(indexFileName, val);
                if (tmpOffset != -1) {
                    newOffset.insert(tmpOffset);
                }
                break;
            case 4: // >
                newOffset = getMoreOffset(indexFileName, val);
                break;
            case 5: // <>
                newOffset = offset;
                break;
            default: 
                assert(false);
                break;
        }
        std::vector<long> tmp(offset.size());
        std::vector<long>::iterator end = std::set_intersection(offset.begin(), offset.end(), newOffset.begin(), newOffset.end(), tmp.begin());
        offset.clear();
        for (std::vector<long>::iterator it = tmp.begin(); it != end; ++ it) {
            offset.insert(*it);
        }
    }
    
    for (auto x : offset) {
        // Record Manager delete records by using index
        // 传入数据库文件名（dbName），偏移量(x)，过滤器(filter)，当前表(nt)
        rm.RecordManagerRecordDelete(dbName, x, filter, nt);
    }
    return Response();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r12
leaq 0xc134(%rip), %rdx      # 0x11051
leaq -0x190(%rbp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5d7d
movq %r15, %rdi
movq %rbx, %rsi
callq 0xb838
movl %eax, %ebx
leaq -0x190(%rbp), %rdi
callq 0x34f8
testb %bl, %bl
je 0x5010
leaq 0xc0fa(%rip), %rdx      # 0x11051
leaq -0xb8(%rbp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5d7d
leaq -0x190(%rbp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0xb966
movq %r15, -0x30(%rbp)
movq %r12, -0x48(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x34f8
leaq 0xc128(%rip), %rdx      # 0x110be
leaq -0x1e0(%rbp), %rdi
movq %r13, -0x50(%rbp)
movq %r13, %rsi
callq 0x5d7d
pushq $0x8
popq %r12
leaq -0xb0(%rbp), %r15
xorl %r13d, %r13d
movq (%r14), %rsi
movq 0x8(%r14), %rcx
movq %rcx, %rax
subq %rsi, %rax
cqto
pushq $0x38
popq %rdi
idivq %rdi
cmpq %rax, %r13
jae 0x504a
addq %r12, %rsi
movq %rbx, %rdi
callq 0x5e98
movq (%r14), %rax
movslq -0x8(%rax,%r12), %rax
movq -0x168(%rbp), %rcx
imulq $0x60, %rax, %rax
movl 0x20(%rcx,%rax), %eax
cmpl -0x90(%rbp), %eax
jne 0x50bd
movq %r15, %rdi
callq 0x34f8
incq %r13
addq $0x38, %r12
jmp 0x4fb7
leaq 0xc041(%rip), %rsi      # 0x11058
leaq -0x280(%rbp), %rdi
leaq -0x190(%rbp), %rdx
callq 0x30e0
leaq -0x280(%rbp), %rsi
movq %r12, %rdi
callq 0xfde4
leaq -0x280(%rbp), %rdi
callq 0x34f8
jmp 0x572c
pushq $0x8
popq %r13
xorl %r15d, %r15d
leaq -0xb8(%rbp), %rbx
movq %rcx, %rax
subq %rsi, %rax
cqto
pushq $0x38
popq %rdi
idivq %rdi
cmpq %rax, %r15
jae 0x50fe
cmpl $0x2, -0x4(%rsi,%r13)
jne 0x50b4
addq %r13, %rsi
movq %rbx, %rdi
callq 0x5e98
movq (%r14), %rax
movslq -0x8(%rax,%r13), %rax
movq -0x168(%rbp), %rcx
imulq $0x60, %rax, %r12
cmpq $0x0, 0x58(%rcx,%r12)
jne 0x5615
leaq -0xb0(%rbp), %rdi
callq 0x34f8
movq (%r14), %rsi
movq 0x8(%r14), %rcx
incq %r15
addq $0x38, %r13
jmp 0x5058
leaq 0xc0ad(%rip), %rsi      # 0x11171
leaq -0x260(%rbp), %rdi
leaq -0xe8(%rbp), %rdx
callq 0x30e0
leaq -0x260(%rbp), %rsi
movq -0x48(%rbp), %r12
movq %r12, %rdi
callq 0xfde4
leaq -0x260(%rbp), %rdi
callq 0x34f8
movq %r15, %rdi
jmp 0x570f
leaq -0xe0(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq -0x200(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x3380
movq -0x30(%rbp), %r15
leaq 0x30(%r15), %rsi
leaq -0xb8(%rbp), %rdi
leaq -0x200(%rbp), %rdx
leaq -0x190(%rbp), %rcx
movq %rsi, -0x140(%rbp)
callq 0xf604
leaq -0xe8(%rbp), %rbx
leaq -0xb8(%rbp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x68e4
movq %r12, %rdi
callq 0x69f0
leaq -0x200(%rbp), %rdi
callq 0x34f8
addq $0x40, %r15
movq %r15, -0x30(%rbp)
xorl %edi, %edi
movq (%r14), %rsi
movq 0x8(%r14), %rax
subq %rsi, %rax
cqto
pushq $0x38
popq %rcx
idivq %rcx
cmpq %rax, %rdi
jae 0x5597
movq %rdi, -0x148(%rbp)
imulq $0x38, %rdi, %rax
addq %rax, %rsi
leaq -0xb8(%rbp), %rdi
callq 0x5ef0
movslq -0xb8(%rbp), %rax
movq -0x168(%rbp), %rcx
imulq $0x60, %rax, %r13
cmpq $0x0, 0x58(%rcx,%r13)
je 0x557c
leaq -0x138(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x5e98
leaq -0x80(%rbp), %r12
movq %r12, %rdi
movq -0x50(%rbp), %rsi
leaq 0xc0a1(%rip), %rdx      # 0x112a6
callq 0x5d7d
addq -0x168(%rbp), %r13
leaq -0x108(%rbp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x5dd8
movq %r12, %rdi
callq 0x34f8
andl $0x0, -0x78(%rbp)
andq $0x0, -0x70(%rbp)
leaq -0x78(%rbp), %rax
movq %rax, -0x68(%rbp)
movq %rax, -0x60(%rbp)
andq $0x0, -0x58(%rbp)
movl -0xb4(%rbp), %eax
cmpq $0x5, %rax
ja 0x5760
leaq 0xbddc(%rip), %rcx      # 0x11038
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x340(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x3380
leaq -0x4b0(%rbp), %r13
movq %r13, %rdi
leaq -0x138(%rbp), %rsi
callq 0x5e98
leaq -0x1c0(%rbp), %r15
movq %r15, %rdi
movq -0x30(%rbp), %rsi
leaq -0x340(%rbp), %r12
movq %r12, %rdx
movq %r13, %rcx
callq 0x9ecc
leaq -0x80(%rbp), %rdi
movq %r15, %rsi
callq 0x68e4
movq %r15, %rdi
callq 0x69f0
leaq -0x4a8(%rbp), %rdi
jmp 0x5340
leaq -0x80(%rbp), %rdi
movq %rbx, %rsi
callq 0x6a08
jmp 0x54ee
leaq -0x2a0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x3380
leaq -0x3c0(%rbp), %r13
movq %r13, %rdi
leaq -0x138(%rbp), %rsi
callq 0x5e98
leaq -0x1c0(%rbp), %r15
movq %r15, %rdi
movq -0x30(%rbp), %rsi
leaq -0x2a0(%rbp), %r12
movq %r12, %rdx
movq %r13, %rcx
callq 0x9dc6
leaq -0x80(%rbp), %rdi
movq %r15, %rsi
callq 0x68e4
movq %r15, %rdi
callq 0x69f0
leaq -0x3b8(%rbp), %rdi
callq 0x34f8
movq %r12, %rdi
callq 0x34f8
jmp 0x54ee
leaq -0x2e0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x3380
leaq -0x420(%rbp), %r13
movq %r13, %rdi
leaq -0x138(%rbp), %rsi
callq 0x5e98
leaq -0x1c0(%rbp), %r15
movq %r15, %rdi
movq -0x30(%rbp), %rsi
leaq -0x2e0(%rbp), %r12
movq %r12, %rdx
movq %r13, %rcx
callq 0x9dc6
leaq -0x80(%rbp), %rdi
movq %r15, %rsi
callq 0x68e4
movq %r15, %rdi
callq 0x69f0
leaq -0x418(%rbp), %rdi
callq 0x34f8
movq %r12, %rdi
callq 0x34f8
leaq -0x2c0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x3380
leaq -0x3f0(%rbp), %r12
movq %r12, %rdi
leaq -0x138(%rbp), %rsi
callq 0x5e98
movq -0x30(%rbp), %rdi
leaq -0x2c0(%rbp), %r15
movq %r15, %rsi
movq %r12, %rdx
callq 0x9d16
movq %rax, -0x40(%rbp)
leaq -0x3e8(%rbp), %rdi
jmp 0x54cd
leaq -0x320(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x3380
leaq -0x480(%rbp), %r13
movq %r13, %rdi
leaq -0x138(%rbp), %rsi
callq 0x5e98
leaq -0x1c0(%rbp), %r15
movq %r15, %rdi
movq -0x30(%rbp), %rsi
leaq -0x320(%rbp), %r12
movq %r12, %rdx
movq %r13, %rcx
callq 0x9ecc
leaq -0x80(%rbp), %rdi
movq %r15, %rsi
callq 0x68e4
movq %r15, %rdi
callq 0x69f0
leaq -0x478(%rbp), %rdi
callq 0x34f8
movq %r12, %rdi
callq 0x34f8
leaq -0x300(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x3380
leaq -0x450(%rbp), %r12
movq %r12, %rdi
leaq -0x138(%rbp), %rsi
callq 0x5e98
movq -0x30(%rbp), %rdi
leaq -0x300(%rbp), %r15
movq %r15, %rsi
movq %r12, %rdx
callq 0x9d16
movq %rax, -0x40(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x34f8
movq %r15, %rdi
callq 0x34f8
cmpq $-0x1, -0x40(%rbp)
je 0x54ee
leaq -0x80(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x72f8
movq -0xc0(%rbp), %rsi
leaq -0x1c0(%rbp), %rdi
leaq -0x31(%rbp), %rdx
callq 0x5f08
movq -0xd0(%rbp), %rdi
movq -0x68(%rbp), %rdx
movq -0x1c0(%rbp), %r8
leaq -0xe0(%rbp), %rsi
leaq -0x78(%rbp), %rcx
callq 0x7525
movq %rax, %r13
movq %rbx, %rdi
callq 0x693a
movq -0x1c0(%rbp), %r12
cmpq %r13, %r12
je 0x554f
movq %rbx, %rdi
movq %r12, %rsi
callq 0x72f8
addq $0x8, %r12
jmp 0x5539
leaq -0x1c0(%rbp), %rdi
callq 0x7452
leaq -0x80(%rbp), %rdi
callq 0x69f0
leaq -0x108(%rbp), %rdi
callq 0x34f8
leaq -0x130(%rbp), %rdi
callq 0x34f8
leaq -0xa8(%rbp), %rdi
callq 0x34f8
movq -0x148(%rbp), %rdi
incq %rdi
jmp 0x518d
movq -0xd0(%rbp), %r13
leaq -0x390(%rbp), %rbx
leaq -0x190(%rbp), %r12
leaq -0xe0(%rbp), %rax
cmpq %rax, %r13
je 0x55f8
movq 0x20(%r13), %r15
movq %rbx, %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x3380
movq -0x140(%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %r12, %r8
callq 0xefdc
movq %rbx, %rdi
callq 0x34f8
movq %r13, %rdi
callq 0x32f0
movq %rax, %r13
jmp 0x55ac
movq -0x48(%rbp), %r12
movq %r12, %rdi
callq 0xfdbe
leaq -0xe8(%rbp), %rdi
callq 0x69f0
jmp 0x5714
leaq 0xbc8a(%rip), %rdx      # 0x112a6
leaq -0x138(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x5d7d
addq -0x168(%rbp), %r12
leaq -0xe8(%rbp), %rdi
leaq -0x138(%rbp), %rsi
movq %r12, %rdx
callq 0x5dd8
movq -0x30(%rbp), %r15
leaq -0x138(%rbp), %rdi
callq 0x34f8
leaq -0x240(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x3380
movq -0x48(%rbp), %r12
leaq -0x370(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x5e98
leaq 0x40(%r15), %rdi
leaq -0x240(%rbp), %rsi
leaq -0x370(%rbp), %rdx
callq 0x9d16
movq %rax, %rbx
leaq -0x368(%rbp), %rdi
callq 0x34f8
leaq -0x240(%rbp), %rdi
callq 0x34f8
leaq -0x220(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x3380
addq $0x30, %r15
leaq -0x220(%rbp), %rsi
leaq -0x190(%rbp), %r8
movq %r15, %rdi
movq %rbx, %rdx
movq %r14, %rcx
callq 0xefdc
leaq -0x220(%rbp), %rdi
callq 0x34f8
movq %r12, %rdi
callq 0xfdbe
leaq -0xe8(%rbp), %rdi
callq 0x34f8
leaq -0xb0(%rbp), %rdi
callq 0x34f8
leaq -0x1e0(%rbp), %rdi
callq 0x34f8
leaq -0x190(%rbp), %rdi
callq 0x5e26
movq %r12, %rax
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xba37(%rip), %rdi      # 0x1117f
leaq 0xb9ba(%rip), %rsi      # 0x11109
leaq 0xba69(%rip), %rcx      # 0x111bf
movl $0x10f, %edx            # imm = 0x10F
callq 0x31a0
leaq 0xba18(%rip), %rdi      # 0x1117f
leaq 0xb99b(%rip), %rsi      # 0x11109
leaq 0xba4a(%rip), %rcx      # 0x111bf
movl $0x11f, %edx            # imm = 0x11F
callq 0x31a0
movq %rax, %rbx
leaq -0x220(%rbp), %rdi
jmp 0x57a6
movq %rax, %rbx
leaq -0x368(%rbp), %rdi
callq 0x34f8
jmp 0x579f
movq %rax, %rbx
leaq -0x240(%rbp), %rdi
callq 0x34f8
jmp 0x57f3
movq %rax, %rbx
leaq -0x138(%rbp), %rdi
jmp 0x57fa
movq %rax, %rbx
jmp 0x57ff
jmp 0x5942
movq %rax, %rbx
leaq -0x200(%rbp), %rdi
jmp 0x5983
jmp 0x5942
movq %rax, %rbx
leaq -0x260(%rbp), %rdi
callq 0x34f8
jmp 0x57eb
movq %rax, %rbx
movq %r15, %rdi
jmp 0x5806
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
callq 0x34f8
leaq -0xb0(%rbp), %rdi
callq 0x34f8
jmp 0x5994
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
jmp 0x590c
movq %rax, %rbx
jmp 0x59a0
movq %rax, %rbx
jmp 0x59ac
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
jmp 0x590c
movq %rax, %rbx
jmp 0x58a8
movq %rax, %rbx
jmp 0x5857
movq %rax, %rbx
leaq -0x418(%rbp), %rdi
callq 0x34f8
leaq -0x2e0(%rbp), %rdi
jmp 0x58fb
movq %rax, %rbx
leaq -0x478(%rbp), %rdi
callq 0x34f8
jmp 0x5877
movq %rax, %rbx
leaq -0x320(%rbp), %rdi
jmp 0x58fb
movq %rax, %rbx
jmp 0x58f4
jmp 0x591f
movq %rax, %rbx
jmp 0x58c0
jmp 0x591f
movq %rax, %rbx
jmp 0x58da
movq %rax, %rbx
leaq -0x3b8(%rbp), %rdi
callq 0x34f8
leaq -0x2a0(%rbp), %rdi
jmp 0x58fb
movq %rax, %rbx
leaq -0x448(%rbp), %rdi
callq 0x34f8
leaq -0x300(%rbp), %rdi
jmp 0x58fb
jmp 0x591f
movq %rax, %rbx
leaq -0x3e8(%rbp), %rdi
callq 0x34f8
leaq -0x2c0(%rbp), %rdi
jmp 0x58fb
jmp 0x591f
movq %rax, %rbx
leaq -0x4a8(%rbp), %rdi
callq 0x34f8
leaq -0x340(%rbp), %rdi
callq 0x34f8
jmp 0x595b
movq %rax, %rbx
leaq -0x190(%rbp), %rdi
callq 0x34f8
jmp 0x59ac
jmp 0x5947
jmp 0x591f
movq %rax, %rbx
jmp 0x5970
movq %rax, %rbx
jmp 0x595b
jmp 0x594c
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
jmp 0x596b
movq %rax, %rbx
jmp 0x597c
jmp 0x5942
movq %rax, %rbx
leaq -0x390(%rbp), %rdi
jmp 0x5983
movq %rax, %rbx
jmp 0x5988
movq %rax, %rbx
jmp 0x5994
movq %rax, %rbx
leaq -0x1c0(%rbp), %rdi
callq 0x7452
leaq -0x80(%rbp), %rdi
callq 0x69f0
leaq -0x108(%rbp), %rdi
callq 0x34f8
leaq -0x130(%rbp), %rdi
callq 0x34f8
leaq -0xa8(%rbp), %rdi
callq 0x34f8
leaq -0xe8(%rbp), %rdi
callq 0x69f0
leaq -0x1e0(%rbp), %rdi
callq 0x34f8
leaq -0x190(%rbp), %rdi
callq 0x5e26
movq %rbx, %rdi
callq 0x3450