// File: /workspace/github_cpp_makefile_star2_O1/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, 0x88(%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, 0x80(%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 0x6bf6
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 0x6bf6
leaq 0x21cf2(%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 0x76cc
callq 0x53d0
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
callq 0x6bf6
leaq 0x21cb3(%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 0x7717
callq 0x53d0
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
callq 0x6bf6
leaq 0x21c70(%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 0x7762
callq 0x53d0
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
callq 0x6bf6
leaq 0x21c2c(%rip), %rsi     # 0x293af
leaq 0x10(%rsp), %rdi
callq 0x5720
leaq 0x10(%rsp), %r13
movq 0x88(%rsp), %rdi
movq %r13, %rsi
callq 0x54b0
movq (%r13), %rdi
cmpq %r15, %rdi
je 0x77b0
callq 0x53d0
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
callq 0x6bf6
leaq 0x21beb(%rip), %rsi     # 0x293bc
leaq 0x10(%rsp), %rdi
callq 0x5720
leaq 0x10(%rsp), %r13
movq 0x80(%rsp), %rdi
movq %r13, %rsi
callq 0x54b0
movq (%r13), %rdi
cmpq %r15, %rdi
je 0x77fe
callq 0x53d0
leaq 0x21bc1(%rip), %rsi     # 0x293c6
leaq 0x10(%rsp), %rdi
movq %r12, %rdx
callq 0xd1cf
leaq 0x21bad(%rip), %rsi     # 0x293c6
leaq 0xf8(%rsp), %rdi
movq 0x30(%rsp), %rdx
callq 0xd1cf
leaq 0x21b94(%rip), %rsi     # 0x293c6
leaq 0xd8(%rsp), %rdi
movq 0x38(%rsp), %rdx
callq 0xd1cf
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 0x31757(%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 0x31711(%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 0x316cb(%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 0x60(%rsp), %rdi
leaq 0xf(%rsp), %rcx
callq 0xd250
movq 0x170(%rbx), %rdi
movaps 0x60(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq %rdi, 0x60(%rsp)
movups 0x178(%rbx), %xmm1
movups %xmm0, 0x170(%rbx)
movq %rax, 0x180(%rbx)
movups %xmm1, 0x68(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
testq %rdi, %rdi
je 0x7a28
callq 0x53d0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x7a37
callq 0x53d0
movl $0x18, %edi
callq 0x5410
movq %rax, %r13
movslq 0x110(%rbx), %rsi
leaq 0x60(%rsp), %rdx
movq $0x0, (%rdx)
leaq 0xd0(%rsp), %rcx
movq %rax, %rdi
callq 0xd250
leaq 0x31a32(%rip), %rax     # 0x394a0
movq %r13, (%rax)
movq 0xd4(%rbp), %rax
movq %rax, 0x230(%rbx)
movl $0x101002, %eax         # imm = 0x101002
movl %eax, 0xc0(%rbx)
movzbl 0xdc(%rbp), %ecx
shll $0xe, %ecx
orl %eax, %ecx
movl %ecx, 0xc0(%rbx)
movl 0x104(%rbx), %r13d
movl 0x110(%rbx), %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
imull 0xd8(%rbx), %r13d
mulsd 0x2167e(%rip), %xmm0    # 0x29140
addsd 0x21666(%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 0x216e0(%rip), %xmm0    # 0x291d0
callq 0x5250
cvttss2si %xmm0, %eax
movl %eax, 0x118(%rbx)
movl %r14d, %eax
cltd
idivl %r12d
movl %eax, 0x11c(%rbx)
leaq 0x318c1(%rip), %rcx     # 0x393d4
movl %eax, (%rcx)
leaq 0x318b4(%rip), %rax     # 0x393d0
movl %r12d, (%rax)
movb 0xd0(%rbp), %al
leaq 0x3189c(%rip), %rcx     # 0x393c8
movb %al, (%rcx)
testb %al, %al
jne 0x7b3c
leaq 0x3189f(%rip), %rax     # 0x393d8
movb $0x0, (%rax)
movzbl 0xd1(%rbp), %eax
leaq 0x31882(%rip), %rcx     # 0x393cc
movl %eax, (%rcx)
addq $0xb0, %rbp
leaq 0x21876(%rip), %rsi     # 0x293d0
movq %rbp, %rdi
callq 0x5120
testl %eax, %eax
je 0x7b98
leaq 0x2186a(%rip), %rsi     # 0x293d7
movq %rbp, %rdi
callq 0x5120
testl %eax, %eax
je 0x7b9f
leaq 0x2185c(%rip), %rsi     # 0x293dc
movq %rbp, %rdi
callq 0x5120
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal 0x3(,%rcx,2), %edi
jmp 0x7ba4
movl $0x4, %edi
jmp 0x7ba4
movl $0x6, %edi
callq 0x1151c
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7bbf
callq 0x53d0
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7bd5
callq 0x53d0
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x7be4
callq 0x53d0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7bf8
callq 0x53d0
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7c45
movq %rax, %rbp
movq %r13, %rdi
callq 0x53d0
jmp 0x7c48
jmp 0x7c45
movq %rax, %rbp
jmp 0x7c5e
jmp 0x7c34
jmp 0x7c3b
jmp 0x7c34
jmp 0x7c3b
jmp 0x7c34
jmp 0x7c3b
jmp 0x7c34
jmp 0x7c3b
jmp 0x7c34
jmp 0x7c3b
movq %rax, %rbp
jmp 0x7c74
jmp 0x7c3b
movq %rax, %rbp
jmp 0x7c83
movq %rax, %rbp
jmp 0x7c97
movq %rax, %rbp
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7c5e
callq 0x53d0
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7c74
callq 0x53d0
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x7c83
callq 0x53d0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7c97
callq 0x53d0
leaq 0x1f8(%rbx), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x7cab
callq 0x53d0
movq 0x1c8(%rbx), %rdi
testq %rdi, %rdi
je 0x7cbc
callq 0x53d0
movq 0x1b0(%rbx), %rdi
testq %rdi, %rdi
je 0x7ccd
callq 0x53d0
leaq 0xe8(%rbx), %r14
leaq 0x140(%rbx), %r13
movq 0x90(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x7cf0
callq 0x53d0
movq %r13, %rdi
callq 0x6bb0
movq (%r14), %rdi
testq %rdi, %rdi
je 0x7d05
callq 0x53d0
movq 0x80(%rsp), %rax
movq (%rax), %rdi
cmpq 0x98(%rsp), %rdi
je 0x7d1f
callq 0x53d0
movq 0x88(%rsp), %rax
movq (%rax), %rdi
cmpq 0xa0(%rsp), %rdi
je 0x7d39
callq 0x53d0
movq 0x30(%rsp), %rax
movq (%rax), %rdi
cmpq 0xa8(%rsp), %rdi
je 0x7d50
callq 0x53d0
movq 0x38(%rsp), %rax
movq (%rax), %rdi
cmpq 0xb0(%rsp), %rdi
je 0x7d67
callq 0x53d0
movq 0xc0(%rsp), %rax
movq (%rax), %rdi
cmpq 0xb8(%rsp), %rdi
je 0x7d81
callq 0x53d0
movq (%rbx), %rdi
cmpq 0xc8(%rsp), %rdi
je 0x7d93
callq 0x53d0
movq %rbp, %rdi
callq 0x56b0
nop