// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd8, %rsp
movq %rdi, %rbx
leaq 0x10(%rdi), %rax
movq %rax, (%rdi)
andq $0x0, 0x8(%rdi)
xorl %eax, %eax
movb %al, 0x10(%rdi)
leaq 0x30(%rdi), %rcx
movq %rcx, 0x20(%rdi)
andq $0x0, 0x28(%rdi)
movb %al, 0x30(%rdi)
leaq 0x50(%rdi), %rcx
movq %rcx, 0x40(%rdi)
andq $0x0, 0x48(%rdi)
movb %al, 0x50(%rdi)
leaq 0x70(%rdi), %rcx
movq %rcx, 0x60(%rdi)
andq $0x0, 0x68(%rdi)
movq %rsi, %rbp
leaq 0x20(%rdi), %r15
movb %al, 0x70(%rdi)
leaq 0x90(%rdi), %rcx
movq %rcx, 0x80(%rdi)
andq $0x0, 0x88(%rdi)
leaq 0x40(%rdi), %r12
leaq 0x60(%rdi), %rcx
movq %rcx, 0x8(%rsp)
movb %al, 0x90(%rdi)
leaq 0xb0(%rdi), %rcx
movq %rcx, 0xa0(%rdi)
andq $0x0, 0xa8(%rdi)
movb %al, 0xb0(%rdi)
andq $0x0, 0xdc(%rdi)
leaq 0x80(%rdi), %rax
movq %rax, 0x38(%rsp)
leaq 0xa0(%rdi), %rax
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0xe8(%rdi)
andq $0x0, 0xf8(%rdi)
movups %xmm0, 0x140(%rdi)
andq $0x0, 0x150(%rdi)
leaq 0x170(%rdi), %r13
movups %xmm0, 0x1f8(%rdi)
andq $0x0, 0x208(%rdi)
movl $0x84, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x6260
leaq 0x60(%rsp), %rdi
movq %rbp, %rsi
movq %r15, 0x50(%rsp)
movq %r12, 0x48(%rsp)
callq 0x6570
leaq 0x28(%rbp), %rsi
movq %rbx, %rdi
callq 0x6250
leaq 0x1a499(%rip), %rdx     # 0x22316
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xb278
leaq 0x10(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x64c0
movq %r14, %rdi
callq 0x61f0
leaq 0x1a475(%rip), %rdx     # 0x22320
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xb278
leaq 0x10(%rsp), %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x64c0
movq %r14, %rdi
callq 0x61f0
leaq 0x1a44f(%rip), %rdx     # 0x22328
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xb278
leaq 0x10(%rsp), %r14
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x64c0
movq %r14, %rdi
callq 0x61f0
leaq 0x1a426(%rip), %rdx     # 0x2232f
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xb278
leaq 0x10(%rsp), %r14
movq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x64c0
movq %r14, %rdi
callq 0x61f0
leaq 0x1a403(%rip), %rdx     # 0x2233c
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xb278
leaq 0x10(%rsp), %r14
movq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x64c0
movq %r14, %rdi
callq 0x61f0
leaq 0x1a3dd(%rip), %rsi     # 0x22346
leaq 0x10(%rsp), %rdi
movq %r15, %rdx
callq 0xb2b0
leaq 0x1a3c9(%rip), %rsi     # 0x22346
leaq 0xa0(%rsp), %rdi
movq 0x8(%rsp), %rdx
callq 0xb2b0
leaq 0x1a3b0(%rip), %rsi     # 0x22346
leaq 0x80(%rsp), %rdi
movq %r12, %rdx
callq 0xb2b0
movq 0x10(%rsp), %rdi
callq 0x6370
movq 0xa0(%rsp), %rdi
callq 0x6370
movq 0x80(%rsp), %rdi
callq 0x6370
movq 0x2cff7(%rip), %rdi     # 0x34fc8
leaq 0x10(%rsp), %rsi
callq 0x63b0
movq %rax, %rdi
callq 0x6210
movq 0x2cfde(%rip), %rdi     # 0x34fc8
leaq 0xa0(%rsp), %rsi
callq 0x63b0
movq %rax, %rdi
callq 0x6210
movq 0x2cfc2(%rip), %rdi     # 0x34fc8
leaq 0x80(%rsp), %rsi
callq 0x63b0
movq %rax, %rdi
callq 0x6210
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 0x58(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0xc0(%rsp), %rdi
leaq 0x7(%rsp), %rcx
callq 0xb326
leaq 0xc0(%rsp), %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0xd66c
movq %r14, %rdi
callq 0x752a
pushq $0x18
popq %rdi
callq 0x6420
movq %rax, %r14
movslq 0x110(%rbx), %rsi
leaq 0xc0(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x58(%rsp), %rcx
movq %rax, %rdi
callq 0xb326
leaq 0x2d39a(%rip), %rax     # 0x354a8
movq %r14, (%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), %r14d
movl 0x110(%rbx), %r12d
cvtsi2sd %r14d, %xmm0
imull 0xd8(%rbx), %r14d
mulsd 0x19f89(%rip), %xmm0    # 0x220e0
addsd 0x19f89(%rip), %xmm0    # 0x220e8
callq 0x6670
cvttsd2si %xmm0, %r15d
movl %r14d, %eax
cltd
idivl %r12d
cvtsi2ss %eax, %xmm0
movl %eax, 0x114(%rbx)
mulss 0x19fce(%rip), %xmm0    # 0x22150
callq 0x6270
cvttss2si %xmm0, %eax
movl %eax, 0x118(%rbx)
movl %r15d, %eax
cltd
idivl %r12d
movl %eax, 0x11c(%rbx)
leaq 0x2d23f(%rip), %rcx     # 0x353e4
movl %eax, (%rcx)
leaq 0x2d232(%rip), %rax     # 0x353e0
movl %r12d, (%rax)
movb 0xd0(%rbp), %al
leaq 0x2d21a(%rip), %rcx     # 0x353d8
movb %al, (%rcx)
testb %al, %al
jne 0x81ce
leaq 0x2d21d(%rip), %rax     # 0x353e8
movb $0x0, (%rax)
movzbl 0xd1(%rbp), %eax
leaq 0x2d200(%rip), %rcx     # 0x353dc
movl %eax, (%rcx)
addq $0xb0, %rbp
leaq 0x1a164(%rip), %rsi     # 0x22350
movq %rbp, %rdi
callq 0x6140
testl %eax, %eax
je 0x822a
leaq 0x1a158(%rip), %rsi     # 0x22357
movq %rbp, %rdi
callq 0x6140
testl %eax, %eax
je 0x822e
leaq 0x1a14a(%rip), %rsi     # 0x2235c
movq %rbp, %rdi
callq 0x6140
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal 0x3(,%rcx,2), %edi
jmp 0x8231
pushq $0x4
jmp 0x8230
pushq $0x6
popq %rdi
callq 0xea80
leaq 0x80(%rsp), %rdi
callq 0x61f0
leaq 0xa0(%rsp), %rdi
callq 0x61f0
leaq 0x10(%rsp), %rdi
callq 0x61f0
leaq 0x60(%rsp), %rdi
callq 0x61f0
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x82a7
movq %rax, %r12
movq %r14, %rdi
callq 0x63f0
jmp 0x82aa
jmp 0x82a7
movq %rax, %r12
jmp 0x82b7
movq %rax, %r12
jmp 0x82c4
jmp 0x829d
jmp 0x829d
jmp 0x829d
jmp 0x829d
jmp 0x829d
jmp 0x829d
movq %rax, %r12
jmp 0x82ce
movq %rax, %r12
jmp 0x82d8
movq %rax, %r12
leaq 0x80(%rsp), %rdi
callq 0x61f0
leaq 0xa0(%rsp), %rdi
callq 0x61f0
leaq 0x10(%rsp), %rdi
callq 0x61f0
leaq 0x60(%rsp), %rdi
callq 0x61f0
leaq 0xe8(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0x140(%rbx), %r15
leaq 0x1f8(%rbx), %rdi
leaq 0x1c8(%rbx), %rbp
leaq 0x1b0(%rbx), %r14
callq 0x751c
movq %rbp, %rdi
callq 0x752a
movq %r14, %rdi
callq 0x752a
movq %r13, %rdi
callq 0x752a
movq %r15, %rdi
callq 0x74f0
movq 0x40(%rsp), %rdi
callq 0x751c
movq 0x30(%rsp), %rdi
callq 0x61f0
movq 0x38(%rsp), %rdi
callq 0x61f0
movq 0x8(%rsp), %rdi
callq 0x61f0
movq 0x48(%rsp), %rdi
callq 0x61f0
movq 0x50(%rsp), %rdi
callq 0x61f0
movq %rbx, %rdi
callq 0x61f0
movq %r12, %rdi
callq 0x66b0
nop