// File: BU-DiSC[P]BF-Shared-Hashing/db.cpp
// Function: db::ReadSettings()
int db::ReadSettings()
{
	ifstream infile;
	infile.open( settings_file );
	 
	if( infile.fail() ){
		cout << "Error opening " << settings_file << endl;
	}

	bool bf_rebuild = false;

	int num = 0;
	while( infile.peek() != EOF ){
		//int line = 0;
		string line;
		getline( infile, line);
		int value = stoi(line);
		//cout << num << " " << value << endl;
		if ( num==0 ) {
			if (value != P)
				return -1;
		} else if ( num==1 ){
			if (value != B)
				return -1;
		} else if ( num==2 ){
				if (value != E)
					return -1;
		} else if ( num==3 ){
			if (value != K)
				return -1;
		} else if ( num==4 ){
			if (value != num_filter_units){
				bf_rebuild = true;
			}
		
		} else if ( num==5 ){
			if ( value > 0 ){
				num_levels = value;
				num_sstPerLevel.resize( num_levels, 0);
				fence_pointers.resize( num_levels );
			}
			else {
				return -1;
			}
		} else if ( num==6 ){
			if( value == 0){
				return -1;
			}
			last_sst_keys = value;
		} else if ( num>=7 && num<7+num_levels ){
			num_sstPerLevel[num-7] = value;
			num_sst += value;
		}
		num++;
	}
	infile.close();

	if ( num < 7+num_levels )
		return -1;

	if (bf_rebuild==true){
		return 1;
	}

	//cout << fence_file << endl;
	infile.open( fence_file );
	if( infile.fail() ){
		cout << "Error opening " << fence_file << endl;
	}
	for ( int i=0 ; i<num_levels ; i++ ){
		vector <string> fence_pointer_one_level ( num_sstPerLevel[i]+1 );
		for ( int j=0 ; j<=num_sstPerLevel[i] ; j++ ){
			string first, last;
			getline( infile, first );
			fence_pointer_one_level[j] = first;
		}
		fence_pointers[i] = fence_pointer_one_level;
	}

	return 0;
}

// Assembly:
subq $0x338, %rsp            # imm = 0x338
movq %rdi, 0x328(%rsp)
movq 0x328(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x120(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0x65a0
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdi
subq $-0x80, %rsi
movl $0x8, %edx
callq 0x6470
jmp 0xb958
movq 0x120(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x120(%rsp,%rax), %rdi
callq 0x6030
movb %al, 0x67(%rsp)
jmp 0xb977
movb 0x67(%rsp), %al
testb $0x1, %al
jne 0xb981
jmp 0xb9e3
movq 0x74638(%rip), %rdi     # 0x7ffc0
leaq 0x586ab(%rip), %rsi     # 0x6403a
callq 0x6500
movq %rax, 0x58(%rsp)
jmp 0xb99b
movq 0x58(%rsp), %rdi
movq 0x68(%rsp), %rsi
subq $-0x80, %rsi
callq 0x6490
movq %rax, 0x50(%rsp)
jmp 0xb9b5
movq 0x50(%rsp), %rdi
movq 0x745e7(%rip), %rsi     # 0x7ffa8
callq 0x6540
jmp 0xb9c8
jmp 0xb9e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
jmp 0xc076
movb $0x0, 0x113(%rsp)
movl $0x0, 0x10c(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x6220
movl %eax, 0x4c(%rsp)
jmp 0xba09
movl 0x4c(%rsp), %eax
cmpl $-0x1, %eax
je 0xbd3d
leaq 0xe8(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x6700
movq 0x40(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0x6110
jmp 0xba3c
xorl %eax, %eax
movl %eax, %esi
leaq 0xe8(%rsp), %rdi
movl $0xa, %edx
callq 0x11210
movl %eax, 0x3c(%rsp)
jmp 0xba58
movl 0x3c(%rsp), %eax
movl %eax, 0xe4(%rsp)
cmpl $0x0, 0x10c(%rsp)
jne 0xbac7
movq 0x68(%rsp), %rcx
movl 0xe4(%rsp), %eax
cmpl 0xc4(%rcx), %eax
je 0xbac2
movl $0xffffffff, 0x334(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0xe0(%rsp)
jmp 0xbd1a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x6340
jmp 0xc076
jmp 0xbcfe
cmpl $0x1, 0x10c(%rsp)
jne 0xbb05
movq 0x68(%rsp), %rcx
movl 0xe4(%rsp), %eax
cmpl 0xc8(%rcx), %eax
je 0xbb00
movl $0xffffffff, 0x334(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0xe0(%rsp)
jmp 0xbd1a
jmp 0xbcfc
cmpl $0x2, 0x10c(%rsp)
jne 0xbb43
movq 0x68(%rsp), %rcx
movl 0xe4(%rsp), %eax
cmpl 0xcc(%rcx), %eax
je 0xbb3e
movl $0xffffffff, 0x334(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0xe0(%rsp)
jmp 0xbd1a
jmp 0xbcfa
cmpl $0x3, 0x10c(%rsp)
jne 0xbb81
movq 0x68(%rsp), %rcx
movl 0xe4(%rsp), %eax
cmpl 0xd0(%rcx), %eax
je 0xbb7c
movl $0xffffffff, 0x334(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0xe0(%rsp)
jmp 0xbd1a
jmp 0xbcf8
cmpl $0x4, 0x10c(%rsp)
jne 0xbbac
movq 0x68(%rsp), %rcx
movl 0xe4(%rsp), %eax
cmpl 0x110(%rcx), %eax
je 0xbba7
movb $0x1, 0x113(%rsp)
jmp 0xbcf6
cmpl $0x5, 0x10c(%rsp)
jne 0xbc40
cmpl $0x0, 0xe4(%rsp)
jle 0xbc20
movq 0x68(%rsp), %rax
movl 0xe4(%rsp), %ecx
movl %ecx, 0xdc(%rax)
movq %rax, %rdi
addq $0xe8, %rdi
movslq 0xdc(%rax), %rsi
movl $0x0, 0xdc(%rsp)
leaq 0xdc(%rsp), %rdx
callq 0x11260
jmp 0xbc01
movq 0x68(%rsp), %rax
movq %rax, %rdi
addq $0x140, %rdi            # imm = 0x140
movslq 0xdc(%rax), %rsi
callq 0x11320
jmp 0xbc1e
jmp 0xbc3b
movl $0xffffffff, 0x334(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0xe0(%rsp)
jmp 0xbd1a
jmp 0xbcf4
cmpl $0x6, 0x10c(%rsp)
jne 0xbc83
cmpl $0x0, 0xe4(%rsp)
jne 0xbc6f
movl $0xffffffff, 0x334(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0xe0(%rsp)
jmp 0xbd1a
movq 0x68(%rsp), %rax
movl 0xe4(%rsp), %ecx
movl %ecx, 0x100(%rax)
jmp 0xbcf2
cmpl $0x7, 0x10c(%rsp)
jl 0xbcf0
movq 0x68(%rsp), %rcx
movl 0x10c(%rsp), %eax
movl 0xdc(%rcx), %ecx
addl $0x7, %ecx
cmpl %ecx, %eax
jge 0xbcf0
movq 0x68(%rsp), %rdi
movl 0xe4(%rsp), %eax
movl %eax, 0x38(%rsp)
addq $0xe8, %rdi
movl 0x10c(%rsp), %eax
subl $0x7, %eax
movslq %eax, %rsi
callq 0x113c0
movl 0x38(%rsp), %edx
movq %rax, %rcx
movq 0x68(%rsp), %rax
movl %edx, (%rcx)
movl 0xe4(%rsp), %ecx
addl 0xe0(%rax), %ecx
movl %ecx, 0xe0(%rax)
jmp 0xbcf2
jmp 0xbcf4
jmp 0xbcf6
jmp 0xbcf8
jmp 0xbcfa
jmp 0xbcfc
jmp 0xbcfe
movl 0x10c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10c(%rsp)
movl $0x0, 0xe0(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x6340
movl 0xe0(%rsp), %eax
testl %eax, %eax
jne 0xc05a
jmp 0xbd38
jmp 0xb9f6
leaq 0x120(%rsp), %rdi
callq 0x6140
jmp 0xbd4c
movq 0x68(%rsp), %rcx
movl 0x10c(%rsp), %eax
movl 0xdc(%rcx), %ecx
addl $0x7, %ecx
cmpl %ecx, %eax
jge 0xbd80
movl $0xffffffff, 0x334(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0xe0(%rsp)
jmp 0xc05a
movb 0x113(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xbdac
movl $0x1, 0x334(%rsp)
movl $0x1, 0xe0(%rsp)
jmp 0xc05a
movq 0x68(%rsp), %rsi
addq $0xa0, %rsi
leaq 0x120(%rsp), %rdi
movl $0x8, %edx
callq 0x6470
jmp 0xbdcc
movq 0x120(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x120(%rsp,%rax), %rdi
callq 0x6030
movb %al, 0x37(%rsp)
jmp 0xbdeb
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0xbdf5
jmp 0xbe41
movq 0x741c4(%rip), %rdi     # 0x7ffc0
leaq 0x58237(%rip), %rsi     # 0x6403a
callq 0x6500
movq %rax, 0x28(%rsp)
jmp 0xbe0f
movq 0x28(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq $0xa0, %rsi
callq 0x6490
movq %rax, 0x20(%rsp)
jmp 0xbe2c
movq 0x20(%rsp), %rdi
movq 0x74170(%rip), %rsi     # 0x7ffa8
callq 0x6540
jmp 0xbe3f
jmp 0xbe41
movl $0x0, 0xd8(%rsp)
movq 0x68(%rsp), %rcx
movl 0xd8(%rsp), %eax
cmpl 0xdc(%rcx), %eax
jge 0xc044
movq 0x68(%rsp), %rdi
addq $0xe8, %rdi
movslq 0xd8(%rsp), %rsi
callq 0x113c0
movl (%rax), %eax
incl %eax
cltq
movq %rax, 0x10(%rsp)
leaq 0xbf(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x81f0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
leaq 0xc0(%rsp), %rdi
callq 0x113e0
jmp 0xbeb3
leaq 0xbf(%rsp), %rdi
callq 0x8f60
movl $0x0, 0xb8(%rsp)
movq 0x68(%rsp), %rdi
movl 0xb8(%rsp), %eax
movl %eax, 0xc(%rsp)
addq $0xe8, %rdi
movslq 0xd8(%rsp), %rsi
callq 0x113c0
movq %rax, %rcx
movl 0xc(%rsp), %eax
cmpl (%rcx), %eax
jg 0xbfd3
leaq 0x98(%rsp), %rdi
movq %rdi, (%rsp)
callq 0x6700
leaq 0x78(%rsp), %rdi
callq 0x6700
movq (%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0x6110
jmp 0xbf2c
movslq 0xb8(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x8040
movq %rax, %rdi
leaq 0x98(%rsp), %rsi
callq 0x6080
jmp 0xbf53
leaq 0x78(%rsp), %rdi
callq 0x6340
leaq 0x98(%rsp), %rdi
callq 0x6340
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0xbecb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
leaq 0xbf(%rsp), %rdi
callq 0x8f60
jmp 0xc076
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x6340
leaq 0x98(%rsp), %rdi
callq 0x6340
jmp 0xc035
movq 0x68(%rsp), %rdi
addq $0x140, %rdi            # imm = 0x140
movslq 0xd8(%rsp), %rsi
callq 0x11460
movq %rax, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x11480
jmp 0xbffe
leaq 0xc0(%rsp), %rdi
callq 0x7e60
movl 0xd8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd8(%rsp)
jmp 0xbe4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x7e60
jmp 0xc076
movl $0x0, 0x334(%rsp)
movl $0x1, 0xe0(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x6180
movl 0x334(%rsp), %eax
addq $0x338, %rsp            # imm = 0x338
retq
leaq 0x120(%rsp), %rdi
callq 0x6180
movq 0x118(%rsp), %rdi
callq 0x6850