// File: /workspace/github_cpp_makefile_star2_O3/BU-DiSC[P]BF-Shared-Hashing/db.cpp
// Function: db::db(options)
db::db( options op )
{
    string db_path = op.db_path;
    out_path = op.out_path;

	bf_dir    = db_path + "/bf_home/";
	index_dir = db_path + "/index/";
	data_dir  = db_path + "/data/";

	settings_file = db_path + "settings.txt";
	fence_file = db_path + "fence.txt";

	string bf_command = "mkdir -p " + bf_dir;
	string data_command = "mkdir -p " + data_dir;
	string index_command = "mkdir -p " + index_dir;
	system(bf_command.c_str());
	system(data_command.c_str());
	system(index_command.c_str());
	cout << bf_command << endl;
	cout << data_command << endl;
	cout << index_command << endl;

    // basic LSM setting
    P = op.buffer_size_in_pages;
    B = op.entries_per_page;
    E = op.entry_size;
	K = op.key_size;
    size_ratio = op.size_ratio;
    fastlocal_bf = op.fastlocal_bf;

    buffer_size = P * B;

	index_size = P*K;
	data_size = B*E;

	// BF related
    bpk = op.bits_per_key;
    mod_bf = op.elastic_filters;
    num_filter_units = op.num_filterunits;
    hash_digests = vector<uint64_t> (num_filter_units, 0);
    BFHash::hash_digests_ = new vector<uint64_t> (num_filter_units, 0);
	//experiment
    tries = op.tries;
    delay = op.delay;
    file_read_flags = O_RDWR | O_SYNC;
    if(op.directIO){
        file_read_flags = O_RDWR | O_DIRECT | O_SYNC;

    }

    int bf_size = (buffer_size * bpk);
	int bf_index = (int)floor(0.693*bpk + 0.5);
    filter_unit_size = bf_size/num_filter_units;
	filter_unit_byte = ceil(float(filter_unit_size)/BYTE);
    filter_unit_index = bf_index/num_filter_units;

    BFHash::num_hash_indexes_ = filter_unit_index;
    BFHash::num_filter_units_ = num_filter_units;
    BFHash::share_hash_across_levels_ = op.share_hash_across_levels;
    if(!op.share_hash_across_levels){
        BFHash::reset = false;
    }
    BFHash::share_hash_across_filter_units_ = op.share_hash_across_filter_units;
    if(op.hash_type.compare("XXHash") == 0){
       BFHash::prepareHashFuncs(XXhash);
    }else if(op.hash_type.compare("CITY") == 0){
       BFHash::prepareHashFuncs(CITY);
    }else if(op.hash_type.compare("CRC") == 0){
       BFHash::prepareHashFuncs(CRC);
    }else{
       BFHash::prepareHashFuncs(MurMur64);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, %rbp
movq %rdi, %rbx
leaq 0x10(%rdi), %r14
movq %r14, (%rdi)
xorl %eax, %eax
movq %rax, 0x8(%rdi)
movb %al, 0x10(%rdi)
leaq 0x20(%rdi), %r12
leaq 0x30(%rdi), %r13
movq %r13, 0x20(%rdi)
movq %rax, 0x28(%rdi)
movb %al, 0x30(%rdi)
leaq 0x40(%rdi), %rcx
movq %rcx, 0x38(%rsp)
leaq 0x50(%rdi), %rcx
movq %rcx, 0xb0(%rsp)
movq %rcx, 0x40(%rdi)
movq %rax, 0x48(%rdi)
movb %al, 0x50(%rdi)
leaq 0x60(%rdi), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x70(%rdi), %rcx
movq %rcx, 0xa8(%rsp)
movq %rcx, 0x60(%rdi)
movq %rax, 0x68(%rdi)
movb %al, 0x70(%rdi)
leaq 0x80(%rdi), %rcx
movq %rcx, 0x68(%rsp)
leaq 0x90(%rdi), %rcx
movq %rcx, 0xa0(%rsp)
movq %rcx, 0x80(%rdi)
movq %rax, 0x88(%rdi)
movb %al, 0x90(%rdi)
leaq 0xa0(%rdi), %rcx
movq %rcx, 0x60(%rsp)
leaq 0xb0(%rdi), %rcx
movq %rcx, 0x98(%rsp)
movq %rcx, 0xa0(%rdi)
movq %rax, 0xa8(%rdi)
movb %al, 0xb0(%rdi)
movq %rax, 0xdc(%rdi)
movq %rax, 0xf8(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0xe8(%rdi)
movq %rax, 0x150(%rdi)
movups %xmm0, 0x140(%rdi)
addq $0x170, %rdi            # imm = 0x170
movq %rax, 0x208(%rbx)
movups %xmm0, 0x1f8(%rbx)
movl $0x84, %edx
movq %rdi, 0x90(%rsp)
xorl %esi, %esi
callq 0x5240
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
movq %r14, 0xc8(%rsp)
movq %r12, 0xc0(%rsp)
movq %r13, 0xb8(%rsp)
callq 0x6c14
leaq 0x28(%rbp), %rsi
movq %rbx, %rdi
callq 0x5230
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %rdi
callq 0x6c14
leaq 0x21d10(%rip), %rsi     # 0x29396
leaq 0x10(%rsp), %rdi
callq 0x5720
leaq 0x10(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x54b0
movq (%r13), %rdi
cmpq %r15, %rdi
je 0x76ae
callq 0x53d0
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
callq 0x6c14
leaq 0x21cd1(%rip), %rsi     # 0x293a0
leaq 0x10(%rsp), %rdi
callq 0x5720
leaq 0x10(%rsp), %r13
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x54b0
movq (%r13), %rdi
cmpq %r15, %rdi
je 0x76f9
callq 0x53d0
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
callq 0x6c14
leaq 0x21c8e(%rip), %rsi     # 0x293a8
leaq 0x10(%rsp), %rdi
callq 0x5720
leaq 0x10(%rsp), %r13
movq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x54b0
movq (%r13), %rdi
cmpq %r15, %rdi
je 0x7744
callq 0x53d0
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
callq 0x6c14
leaq 0x21c4a(%rip), %rsi     # 0x293af
leaq 0x10(%rsp), %rdi
callq 0x5720
leaq 0x10(%rsp), %r13
movq 0x68(%rsp), %rdi
movq %r13, %rsi
callq 0x54b0
movq (%r13), %rdi
cmpq %r15, %rdi
je 0x778f
callq 0x53d0
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
callq 0x6c14
leaq 0x21c0c(%rip), %rsi     # 0x293bc
leaq 0x10(%rsp), %rdi
callq 0x5720
leaq 0x10(%rsp), %r13
movq 0x60(%rsp), %rdi
movq %r13, %rsi
callq 0x54b0
movq (%r13), %rdi
cmpq %r15, %rdi
je 0x77da
callq 0x53d0
leaq 0x21be5(%rip), %rsi     # 0x293c6
leaq 0x10(%rsp), %rdi
movq %r12, %rdx
callq 0xd04f
leaq 0x21bd1(%rip), %rsi     # 0x293c6
leaq 0xf8(%rsp), %rdi
movq 0x30(%rsp), %rdx
callq 0xd04f
leaq 0x21bb8(%rip), %rsi     # 0x293c6
leaq 0xd8(%rsp), %rdi
movq 0x38(%rsp), %rdx
callq 0xd04f
movq 0x10(%rsp), %rdi
callq 0x5360
movq 0xf8(%rsp), %rdi
callq 0x5360
movq 0xd8(%rsp), %rdi
callq 0x5360
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x3177b(%rip), %rdi     # 0x38fd0
callq 0x5470
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x53f0
movsbl %al, %esi
movq %r13, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x52a0
movq 0xf8(%rsp), %rsi
movq 0x100(%rsp), %rdx
movq 0x31735(%rip), %rdi     # 0x38fd0
callq 0x5470
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x53f0
movsbl %al, %esi
movq %r13, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x52a0
movq 0xd8(%rsp), %rsi
movq 0xe0(%rsp), %rdx
movq 0x316ef(%rip), %rdi     # 0x38fd0
callq 0x5470
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x53f0
movsbl %al, %esi
movq %r13, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x52a0
movl 0x8c(%rbp), %eax
movl %eax, 0xc4(%rbx)
movl 0x90(%rbp), %ecx
movl %ecx, 0xc8(%rbx)
movl 0x94(%rbp), %edx
movl %edx, 0xcc(%rbx)
movl 0x98(%rbp), %esi
movl %esi, 0xd0(%rbx)
movl 0x88(%rbp), %edi
movl %edi, 0xd4(%rbx)
movb 0xa8(%rbp), %dil
movb %dil, 0x10c(%rbx)
imull %ecx, %edx
imull %eax, %ecx
movl %ecx, 0xd8(%rbx)
imull %eax, %esi
movl %esi, 0x134(%rbx)
movl %edx, 0x138(%rbx)
movl 0x9c(%rbp), %eax
movl %eax, 0x104(%rbx)
movzbl 0xa0(%rbp), %eax
movl %eax, 0x108(%rbx)
movslq 0xa4(%rbp), %rsi
movl %esi, 0x110(%rbx)
leaq 0xd0(%rsp), %rdx
movq $0x0, (%rdx)
leaq 0x70(%rsp), %rdi
leaq 0xf(%rsp), %rcx
callq 0xd0d0
movq 0x170(%rbx), %rdi
movaps 0x70(%rsp), %xmm0
movups %xmm0, 0x170(%rbx)
movq 0x80(%rsp), %rax
movq %rax, 0x180(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movq $0x0, 0x80(%rsp)
testq %rdi, %rdi
je 0x7a08
callq 0x53d0
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x7a08
callq 0x53d0
movl $0x18, %edi
callq 0x5410
movq %rax, %r13
movslq 0x110(%rbx), %rsi
leaq 0x70(%rsp), %rdx
movq $0x0, (%rdx)
leaq 0xd0(%rsp), %rcx
movq %rax, %rdi
callq 0xd0d0
leaq 0x31a61(%rip), %rax     # 0x394a0
movq %r13, (%rax)
movq 0xd4(%rbp), %rax
movq %rax, 0x230(%rbx)
movzbl 0xdc(%rbp), %eax
shll $0xe, %eax
orl $0x101002, %eax         # imm = 0x101002
movl %eax, 0xc0(%rbx)
movl 0x104(%rbx), %r13d
movl 0x110(%rbx), %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
imull 0xd8(%rbx), %r13d
mulsd 0x216b5(%rip), %xmm0    # 0x29140
addsd 0x2169d(%rip), %xmm0    # 0x29130
callq 0x5660
cvttsd2si %xmm0, %r14d
movl %r13d, %eax
cltd
idivl %r12d
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movl %eax, 0x114(%rbx)
mulss 0x21717(%rip), %xmm0    # 0x291d0
callq 0x5250
cvttss2si %xmm0, %eax
movl %eax, 0x118(%rbx)
movl %r14d, %eax
cltd
idivl %r12d
movl %eax, 0x11c(%rbx)
leaq 0x318f8(%rip), %rcx     # 0x393d4
movl %eax, (%rcx)
leaq 0x318eb(%rip), %rax     # 0x393d0
movl %r12d, (%rax)
movb 0xd0(%rbp), %al
leaq 0x318d3(%rip), %rcx     # 0x393c8
movb %al, (%rcx)
testb %al, %al
jne 0x7b05
leaq 0x318d6(%rip), %rax     # 0x393d8
movb $0x0, (%rax)
movzbl 0xd1(%rbp), %eax
leaq 0x318b9(%rip), %rcx     # 0x393cc
movl %eax, (%rcx)
addq $0xb0, %rbp
leaq 0x218ad(%rip), %rsi     # 0x293d0
movq %rbp, %rdi
callq 0x5120
testl %eax, %eax
je 0x7b61
leaq 0x218a1(%rip), %rsi     # 0x293d7
movq %rbp, %rdi
callq 0x5120
testl %eax, %eax
je 0x7b68
leaq 0x21893(%rip), %rsi     # 0x293dc
movq %rbp, %rdi
callq 0x5120
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal 0x3(,%rcx,2), %edi
jmp 0x7b6d
movl $0x4, %edi
jmp 0x7b6d
movl $0x6, %edi
callq 0x113f0
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7b88
callq 0x53d0
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7b9e
callq 0x53d0
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x7bad
callq 0x53d0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7bc1
callq 0x53d0
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7c0e
movq %rax, %rbp
movq %r13, %rdi
callq 0x53d0
jmp 0x7c11
jmp 0x7c0e
movq %rax, %rbp
jmp 0x7c27
jmp 0x7bfd
jmp 0x7c04
jmp 0x7bfd
jmp 0x7c04
jmp 0x7bfd
jmp 0x7c04
jmp 0x7bfd
jmp 0x7c04
jmp 0x7bfd
jmp 0x7c04
movq %rax, %rbp
jmp 0x7c3d
jmp 0x7c04
movq %rax, %rbp
jmp 0x7c4c
movq %rax, %rbp
jmp 0x7c60
movq %rax, %rbp
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7c27
callq 0x53d0
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7c3d
callq 0x53d0
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x7c4c
callq 0x53d0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7c60
callq 0x53d0
leaq 0x1f8(%rbx), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x7c74
callq 0x53d0
movq 0x1c8(%rbx), %rdi
testq %rdi, %rdi
je 0x7c85
callq 0x53d0
movq 0x1b0(%rbx), %rdi
testq %rdi, %rdi
je 0x7c96
callq 0x53d0
leaq 0xe8(%rbx), %r14
leaq 0x140(%rbx), %r13
movq 0x90(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x7cb9
callq 0x53d0
movq %r13, %rdi
callq 0x6bc8
movq (%r14), %rdi
testq %rdi, %rdi
je 0x7cce
callq 0x53d0
movq 0x60(%rsp), %rax
movq (%rax), %rdi
cmpq 0x98(%rsp), %rdi
je 0x7ce5
callq 0x53d0
movq 0x68(%rsp), %rax
movq (%rax), %rdi
cmpq 0xa0(%rsp), %rdi
je 0x7cfc
callq 0x53d0
movq 0x30(%rsp), %rax
movq (%rax), %rdi
cmpq 0xa8(%rsp), %rdi
je 0x7d13
callq 0x53d0
movq 0x38(%rsp), %rax
movq (%rax), %rdi
cmpq 0xb0(%rsp), %rdi
je 0x7d2a
callq 0x53d0
movq 0xc0(%rsp), %rax
movq (%rax), %rdi
cmpq 0xb8(%rsp), %rdi
je 0x7d44
callq 0x53d0
movq (%rbx), %rdi
cmpq 0xc8(%rsp), %rdi
je 0x7d56
callq 0x53d0
movq %rbp, %rdi
callq 0x56b0