// File: 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:
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rsi, 0xa8(%rsp)
movq %rdi, 0x2a0(%rsp)
movq %rsi, 0x298(%rsp)
movq 0x2a0(%rsp), %rdi
movq %rdi, 0xf8(%rsp)
movq %rdi, %rax
movq %rax, 0xb0(%rsp)
callq 0x6700
movq 0xf8(%rsp), %rdi
addq $0x20, %rdi
movq %rdi, 0xb8(%rsp)
callq 0x6700
movq 0xf8(%rsp), %rdi
addq $0x40, %rdi
movq %rdi, 0xc0(%rsp)
callq 0x6700
movq 0xf8(%rsp), %rdi
addq $0x60, %rdi
movq %rdi, 0xc8(%rsp)
callq 0x6700
movq 0xf8(%rsp), %rdi
subq $-0x80, %rdi
movq %rdi, 0xd0(%rsp)
callq 0x6700
movq 0xf8(%rsp), %rdi
addq $0xa0, %rdi
movq %rdi, 0xd8(%rsp)
callq 0x6700
movq 0xf8(%rsp), %rdi
movl $0x0, 0xdc(%rdi)
movl $0x0, 0xe0(%rdi)
addq $0xe8, %rdi
movq %rdi, 0xe0(%rsp)
callq 0x10ef0
movq 0xf8(%rsp), %rdi
addq $0x140, %rdi            # imm = 0x140
movq %rdi, 0xe8(%rsp)
callq 0x10f00
movq 0xf8(%rsp), %rdi
addq $0x170, %rdi            # imm = 0x170
movq %rdi, 0xf0(%rsp)
callq 0x10f10
movq 0xf8(%rsp), %rax
addq $0x188, %rax            # imm = 0x188
movq %rax, 0x100(%rsp)
callq 0x10f20
movq 0x100(%rsp), %rdi
movq %rax, 0x290(%rsp)
leaq 0x290(%rsp), %rsi
callq 0x7ab0
jmp 0xaef1
movq 0xb0(%rsp), %rax
addq $0x18c, %rax            # imm = 0x18C
movq %rax, 0xa0(%rsp)
callq 0x10f20
movq 0xa0(%rsp), %rdi
movq %rax, 0x278(%rsp)
leaq 0x278(%rsp), %rsi
callq 0x7ab0
jmp 0xaf2b
movq 0xb0(%rsp), %rax
addq $0x190, %rax            # imm = 0x190
movq %rax, 0x98(%rsp)
callq 0x10f20
movq 0x98(%rsp), %rdi
movq %rax, 0x270(%rsp)
leaq 0x270(%rsp), %rsi
callq 0x7ab0
jmp 0xaf65
movq 0xb0(%rsp), %rax
addq $0x194, %rax            # imm = 0x194
movq %rax, 0x90(%rsp)
callq 0x10f20
movq 0x90(%rsp), %rdi
movq %rax, 0x268(%rsp)
leaq 0x268(%rsp), %rsi
callq 0x7ab0
jmp 0xaf9f
movq 0xb0(%rsp), %rdi
movq $0x0, 0x198(%rdi)
movq $0x0, 0x1a0(%rdi)
movq $0x0, 0x1a8(%rdi)
addq $0x1b0, %rdi            # imm = 0x1B0
movq %rdi, 0x78(%rsp)
callq 0x10f10
movq 0xb0(%rsp), %rdi
addq $0x1c8, %rdi            # imm = 0x1C8
movq %rdi, 0x80(%rsp)
callq 0x10f10
movq 0xb0(%rsp), %rdi
movl $0x0, 0x1e0(%rdi)
movl $0x0, 0x1e4(%rdi)
movl $0x0, 0x1e8(%rdi)
movl $0x0, 0x1ec(%rdi)
movl $0x0, 0x1f0(%rdi)
addq $0x1f8, %rdi            # imm = 0x1F8
movq %rdi, 0x88(%rsp)
callq 0x10ef0
movq 0xa8(%rsp), %rsi
leaq 0x248(%rsp), %rdi
callq 0x6260
jmp 0xb05a
movq 0xb0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
addq $0x28, %rsi
callq 0x6080
jmp 0xb075
leaq 0x5928b(%rip), %rdx     # 0x64307
leaq 0x228(%rsp), %rdi
leaq 0x248(%rsp), %rsi
callq 0x10f60
jmp 0xb093
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
leaq 0x228(%rsp), %rsi
movq %rsi, 0x70(%rsp)
callq 0x65f0
movq 0x70(%rsp), %rdi
callq 0x6340
leaq 0x5924f(%rip), %rdx     # 0x64311
leaq 0x208(%rsp), %rdi
leaq 0x248(%rsp), %rsi
callq 0x10f60
jmp 0xb0d9
movq 0xb0(%rsp), %rdi
addq $0x40, %rdi
leaq 0x208(%rsp), %rsi
movq %rsi, 0x68(%rsp)
callq 0x65f0
movq 0x68(%rsp), %rdi
callq 0x6340
leaq 0x59211(%rip), %rdx     # 0x64319
leaq 0x1e8(%rsp), %rdi
leaq 0x248(%rsp), %rsi
callq 0x10f60
jmp 0xb11f
movq 0xb0(%rsp), %rdi
addq $0x60, %rdi
leaq 0x1e8(%rsp), %rsi
movq %rsi, 0x60(%rsp)
callq 0x65f0
movq 0x60(%rsp), %rdi
callq 0x6340
leaq 0x591d2(%rip), %rdx     # 0x64320
leaq 0x1c8(%rsp), %rdi
leaq 0x248(%rsp), %rsi
callq 0x10f60
jmp 0xb165
movq 0xb0(%rsp), %rdi
subq $-0x80, %rdi
leaq 0x1c8(%rsp), %rsi
movq %rsi, 0x58(%rsp)
callq 0x65f0
movq 0x58(%rsp), %rdi
callq 0x6340
leaq 0x59199(%rip), %rdx     # 0x6432d
leaq 0x1a8(%rsp), %rdi
leaq 0x248(%rsp), %rsi
callq 0x10f60
jmp 0xb1ab
movq 0xb0(%rsp), %rdi
addq $0xa0, %rdi
leaq 0x1a8(%rsp), %rsi
movq %rsi, 0x50(%rsp)
callq 0x65f0
movq 0x50(%rsp), %rdi
callq 0x6340
movq 0xb0(%rsp), %rdx
addq $0x20, %rdx
leaq 0x5914e(%rip), %rsi     # 0x64337
leaq 0x188(%rsp), %rdi
callq 0x10ff0
jmp 0xb1f8
movq 0xb0(%rsp), %rdx
addq $0x60, %rdx
leaq 0x5912c(%rip), %rsi     # 0x64337
leaq 0x168(%rsp), %rdi
callq 0x10ff0
jmp 0xb21a
movq 0xb0(%rsp), %rdx
addq $0x40, %rdx
leaq 0x5910a(%rip), %rsi     # 0x64337
leaq 0x148(%rsp), %rdi
callq 0x10ff0
jmp 0xb23c
leaq 0x188(%rsp), %rdi
callq 0x6250
movq %rax, %rdi
callq 0x6460
jmp 0xb253
leaq 0x168(%rsp), %rdi
callq 0x6250
movq %rax, %rdi
callq 0x6460
jmp 0xb26a
leaq 0x148(%rsp), %rdi
callq 0x6250
movq %rax, %rdi
callq 0x6460
jmp 0xb281
movq 0x74d38(%rip), %rdi     # 0x7ffc0
leaq 0x188(%rsp), %rsi
callq 0x6490
movq %rax, 0x48(%rsp)
jmp 0xb29c
movq 0x48(%rsp), %rdi
movq 0x74d00(%rip), %rsi     # 0x7ffa8
callq 0x6540
jmp 0xb2af
movq 0x74d0a(%rip), %rdi     # 0x7ffc0
leaq 0x168(%rsp), %rsi
callq 0x6490
movq %rax, 0x40(%rsp)
jmp 0xb2ca
movq 0x40(%rsp), %rdi
movq 0x74cd2(%rip), %rsi     # 0x7ffa8
callq 0x6540
jmp 0xb2dd
movq 0x74cdc(%rip), %rdi     # 0x7ffc0
leaq 0x148(%rsp), %rsi
callq 0x6490
movq %rax, 0x38(%rsp)
jmp 0xb2f8
movq 0x38(%rsp), %rdi
movq 0x74ca4(%rip), %rsi     # 0x7ffa8
callq 0x6540
jmp 0xb30b
movq 0xb0(%rsp), %rax
movq 0xa8(%rsp), %rcx
movl 0x8c(%rcx), %edx
movl %edx, 0xc4(%rax)
movl 0x90(%rcx), %edx
movl %edx, 0xc8(%rax)
movl 0x94(%rcx), %edx
movl %edx, 0xcc(%rax)
movl 0x98(%rcx), %edx
movl %edx, 0xd0(%rax)
movl 0x88(%rcx), %edx
movl %edx, 0xd4(%rax)
movb 0xa8(%rcx), %dl
andb $0x1, %dl
movb %dl, 0x10c(%rax)
movl 0xc4(%rax), %edx
movl 0xc8(%rax), %esi
imull %esi, %edx
movl %edx, 0xd8(%rax)
movl 0xc4(%rax), %edx
movl 0xd0(%rax), %esi
imull %esi, %edx
movl %edx, 0x134(%rax)
movl 0xc8(%rax), %edx
movl 0xcc(%rax), %esi
imull %esi, %edx
movl %edx, 0x138(%rax)
movl 0x9c(%rcx), %edx
movl %edx, 0x104(%rax)
movzbl 0xa0(%rcx), %edx
andl $0x1, %edx
movl %edx, 0x108(%rax)
movl 0xa4(%rcx), %ecx
movl %ecx, 0x110(%rax)
movslq 0x110(%rax), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x128(%rsp)
leaq 0x127(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x11120
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rcx
leaq 0x130(%rsp), %rdi
leaq 0x128(%rsp), %rdx
callq 0x11130
jmp 0xb418
movq 0xb0(%rsp), %rdi
addq $0x170, %rdi            # imm = 0x170
leaq 0x130(%rsp), %rsi
movq %rsi, 0x18(%rsp)
callq 0x111b0
movq 0x18(%rsp), %rdi
callq 0x8500
leaq 0x127(%rsp), %rdi
callq 0x88d0
movl $0x18, %edi
callq 0x6510
movq %rax, 0x20(%rsp)
jmp 0xb461
movq 0xb0(%rsp), %rax
movb $0x1, 0x116(%rsp)
movslq 0x110(%rax), %rax
movq %rax, 0x8(%rsp)
movq $0x0, 0x118(%rsp)
leaq 0x117(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x11120
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rcx
leaq 0x118(%rsp), %rdx
callq 0x11130
jmp 0xb4b9
movq 0x20(%rsp), %rcx
movb $0x0, 0x116(%rsp)
leaq 0x750cb(%rip), %rax     # 0x80598
movq %rcx, (%rax)
leaq 0x117(%rsp), %rdi
callq 0x88d0
movq 0xb0(%rsp), %rcx
movq 0xa8(%rsp), %rax
movl 0xd4(%rax), %edx
movl %edx, 0x230(%rcx)
movl 0xd8(%rax), %edx
movl %edx, 0x234(%rcx)
movl $0x101002, 0xc0(%rcx)   # imm = 0x101002
testb $0x1, 0xdc(%rax)
je 0xb62b
movq 0xb0(%rsp), %rax
movl $0x105002, 0xc0(%rax)   # imm = 0x105002
jmp 0xb62b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x288(%rsp)
movl %eax, 0x284(%rsp)
jmp 0xb87f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x288(%rsp)
movl %eax, 0x284(%rsp)
jmp 0xb85b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x288(%rsp)
movl %eax, 0x284(%rsp)
jmp 0xb84e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x288(%rsp)
movl %eax, 0x284(%rsp)
jmp 0xb841
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x288(%rsp)
movl %eax, 0x284(%rsp)
jmp 0xb834
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x288(%rsp)
movl %eax, 0x284(%rsp)
jmp 0xb827
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x288(%rsp)
movl %eax, 0x284(%rsp)
leaq 0x127(%rsp), %rdi
callq 0x88d0
jmp 0xb827
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x288(%rsp)
movl %eax, 0x284(%rsp)
leaq 0x117(%rsp), %rdi
callq 0x88d0
testb $0x1, 0x116(%rsp)
jne 0xb61c
jmp 0xb626
movq 0x20(%rsp), %rdi
callq 0x64c0
jmp 0xb827
movq 0xb0(%rsp), %rax
movl 0xd8(%rax), %ecx
movl 0x104(%rax), %edx
imull %edx, %ecx
movl %ecx, 0x110(%rsp)
cvtsi2sdl 0x104(%rax), %xmm0
movsd 0x58a87(%rip), %xmm1    # 0x640e0
mulsd %xmm1, %xmm0
movsd 0x58a83(%rip), %xmm1    # 0x640e8
addsd %xmm1, %xmm0
callq 0x6800
movq 0xb0(%rsp), %rcx
cvttsd2si %xmm0, %eax
movl %eax, 0x10c(%rsp)
movl 0x110(%rsp), %eax
movl 0x110(%rcx), %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq 0xb0(%rsp), %rax
movl %ecx, 0x114(%rax)
cvtsi2ssl 0x114(%rax), %xmm0
movss 0x58a8f(%rip), %xmm1    # 0x64140
mulss %xmm1, %xmm0
callq 0x111f0
movss %xmm0, 0x4(%rsp)
jmp 0xb6c2
movq 0xb0(%rsp), %rcx
movss 0x4(%rsp), %xmm0
cvttss2si %xmm0, %eax
movl %eax, 0x118(%rcx)
movl 0x10c(%rsp), %eax
cltd
idivl 0x110(%rcx)
movl %eax, %edx
movq 0xa8(%rsp), %rax
movl %edx, 0x11c(%rcx)
movl 0x11c(%rcx), %esi
leaq 0x74dcf(%rip), %rdx     # 0x804d4
movl %esi, (%rdx)
movl 0x110(%rcx), %edx
leaq 0x74dbc(%rip), %rcx     # 0x804d0
movl %edx, (%rcx)
movb 0xd0(%rax), %dl
andb $0x1, %dl
leaq 0x74da2(%rip), %rcx     # 0x804c8
movb %dl, (%rcx)
testb $0x1, 0xd0(%rax)
jne 0xb73b
leaq 0x74da0(%rip), %rax     # 0x804d8
movb $0x0, (%rax)
movq 0xa8(%rsp), %rdi
movb 0xd1(%rdi), %al
andb $0x1, %al
movzbl %al, %ecx
leaq 0x74d77(%rip), %rax     # 0x804cc
movl %ecx, (%rax)
addq $0xb0, %rdi
leaq 0x58bdc(%rip), %rsi     # 0x64341
callq 0x61a0
cmpl $0x0, %eax
jne 0xb77d
movl $0x4, %edi
callq 0x1dd70
jmp 0xb77b
jmp 0xb7eb
movq 0xa8(%rsp), %rdi
addq $0xb0, %rdi
leaq 0x58bb5(%rip), %rsi     # 0x64348
callq 0x61a0
cmpl $0x0, %eax
jne 0xb7ab
movl $0x6, %edi
callq 0x1dd70
jmp 0xb7a9
jmp 0xb7e9
movq 0xa8(%rsp), %rdi
addq $0xb0, %rdi
leaq 0x58b8c(%rip), %rsi     # 0x6434d
callq 0x61a0
cmpl $0x0, %eax
jne 0xb7d9
movl $0x5, %edi
callq 0x1dd70
jmp 0xb7d7
jmp 0xb7e7
movl $0x3, %edi
callq 0x1dd70
jmp 0xb7e5
jmp 0xb7e7
jmp 0xb7e9
jmp 0xb7eb
leaq 0x148(%rsp), %rdi
callq 0x6340
leaq 0x168(%rsp), %rdi
callq 0x6340
leaq 0x188(%rsp), %rdi
callq 0x6340
leaq 0x248(%rsp), %rdi
callq 0x6340
addq $0x2a8, %rsp            # imm = 0x2A8
retq
leaq 0x148(%rsp), %rdi
callq 0x6340
leaq 0x168(%rsp), %rdi
callq 0x6340
leaq 0x188(%rsp), %rdi
callq 0x6340
leaq 0x248(%rsp), %rdi
callq 0x6340
movq 0x88(%rsp), %rdi
callq 0x84a0
movq 0x80(%rsp), %rdi
callq 0x8500
movq 0x78(%rsp), %rdi
callq 0x8500
movq 0xf0(%rsp), %rdi
callq 0x8500
movq 0xe8(%rsp), %rdi
callq 0x8560
movq 0xe0(%rsp), %rdi
callq 0x84a0
movq 0xd8(%rsp), %rdi
callq 0x6340
movq 0xd0(%rsp), %rdi
callq 0x6340
movq 0xc8(%rsp), %rdi
callq 0x6340
movq 0xc0(%rsp), %rdi
callq 0x6340
movq 0xb8(%rsp), %rdi
callq 0x6340
movq 0xf8(%rsp), %rdi
callq 0x6340
movq 0x288(%rsp), %rdi
callq 0x6850
nopw %cs:(%rax,%rax)