// File: /workspace/github_cpp_makefile_star2_O2/prakhar2808[P]DBEngine/dbengine/memtable.cc
// Function: memtable::memtable(int, int, int)
memtable::memtable(int size, int capacity, int memtableID)
  :size(size),
  capacity(capacity),
  memtableID(memtableID) {
  
  journalFilePath = "journalsDir/" + std::to_string(memtableID) + ".txt";
  // Check if the journal file is already present, need to restore it in memory
  struct stat buff;
  if(stat(journalFilePath.c_str(), &buff) != -1) {
    std::ifstream journalReadfd;
    journalReadfd.open(journalFilePath);
    journalReadfd >> std::noskipws;
    unsigned char ch;
    std::cout << "Reading journal : " << journalFilePath << std::endl;
    while(journalReadfd.peek() != EOF) {
      // Length of the key
      journalReadfd >> ch;
      std::string key((int)ch, '\0');
      // The key
      journalReadfd.read(&key[0], (int)ch);
      // Length of the value
      journalReadfd >> ch;
      std::string value((int)ch, '\0');
      // The value
      journalReadfd.read(&value[0], (int)ch);
      // Putting in the in-memory table.
      table[key] = value;
      std::cout << "Read key : " << key << ", value : " << value << std::endl; 
    }
    journalReadfd.close();
  }
  // Opening the journal in append mode.
  journalWritefd.open(journalFilePath, std::ios_base::app);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movl %ecx, %ebp
movq %rdi, %rbx
leaq 0x8(%rdi), %rax
andl $0x0, 0x8(%rdi)
andq $0x0, 0x10(%rdi)
movq %rax, 0x18(%rdi)
movq %rax, 0x20(%rdi)
andq $0x0, 0x28(%rdi)
movl %esi, 0x30(%rdi)
movl %edx, 0x34(%rdi)
movl %ecx, 0x38(%rdi)
leaq 0x50(%rdi), %rax
movq %rax, 0x40(%rdi)
andq $0x0, 0x48(%rdi)
leaq 0x40(%rdi), %rax
movq %rax, 0x10(%rsp)
movb $0x0, 0x50(%rdi)
addq $0x60, %rdi
movq %rdi, 0x18(%rsp)
callq 0x5670
leaq 0x20(%rsp), %rdi
movl %ebp, %esi
callq 0x133b8
leaq 0x52fd(%rip), %rsi      # 0x181b1
leaq 0x60(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x13394
leaq 0x52f4(%rip), %rdx      # 0x181be
leaq 0xf0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x13375
leaq 0xf0(%rsp), %r14
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x54b0
movq %r14, %rdi
callq 0x51d0
leaq 0x60(%rsp), %rdi
callq 0x51d0
leaq 0x20(%rsp), %rdi
callq 0x51d0
movq (%r15), %rdi
leaq 0x60(%rsp), %rsi
callq 0x5380
cmpl $-0x1, %eax
je 0x130a3
leaq 0xf0(%rsp), %rdi
callq 0x5450
leaq 0xf0(%rsp), %rdi
pushq $0x8
popq %rdx
movq 0x10(%rsp), %rsi
callq 0x5350
movq 0xf0(%rsp), %rax
movq -0x18(%rax), %rax
andl $0xffffefff, 0x108(%rsp,%rax) # imm = 0xFFFFEFFF
movq 0x1205a(%rip), %rdi     # 0x24fc0
leaq 0x5256(%rip), %rsi      # 0x181c3
callq 0x53b0
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x5370
movq %rax, %rdi
callq 0x5210
leaq 0xf0(%rsp), %r12
leaq 0xf(%rsp), %r13
leaq 0x20(%rsp), %rbp
leaq 0x40(%rsp), %r15
leaq 0x523d(%rip), %r14      # 0x181e2
movq %r12, %rdi
callq 0x5180
cmpl $-0x1, %eax
je 0x13089
movq %r12, %rdi
movq %r13, %rsi
callq 0x5510
movzbl 0xf(%rsp), %esi
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rbp, %rdi
xorl %edx, %edx
callq 0x5470
movq 0x20(%rsp), %rsi
movzbl 0xf(%rsp), %edx
movq %r12, %rdi
callq 0x54d0
movq %r12, %rdi
movq %r13, %rsi
callq 0x5510
movzbl 0xf(%rsp), %esi
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %r15, %rdi
xorl %edx, %edx
callq 0x5470
movq 0x40(%rsp), %rsi
movzbl 0xf(%rsp), %edx
movq %r12, %rdi
callq 0x54d0
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x13412
movq %rax, %rdi
movq %r15, %rsi
callq 0x5260
movq 0x11f81(%rip), %rdi     # 0x24fc0
leaq 0x5190(%rip), %rsi      # 0x181d6
callq 0x53b0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x5370
movq %rax, %rdi
movq %r14, %rsi
callq 0x53b0
movq %rax, %rdi
movq %r15, %rsi
callq 0x5370
movq %rax, %rdi
callq 0x5210
movq %r15, %rdi
callq 0x51d0
movq %rbp, %rdi
callq 0x51d0
jmp 0x12fa5
leaq 0xf0(%rsp), %rdi
callq 0x50a0
leaq 0xf0(%rsp), %rdi
callq 0x50e0
pushq $0x1
popq %rdx
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x5200
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x13100
jmp 0x130eb
jmp 0x130eb
movq %rax, %r14
leaq 0x60(%rsp), %rdi
callq 0x51d0
jmp 0x130df
movq %rax, %r14
leaq 0x20(%rsp), %rdi
callq 0x51d0
jmp 0x13129
movq %rax, %r14
jmp 0x13129
movq %rax, %r14
jmp 0x13133
jmp 0x13100
jmp 0x130fb
jmp 0x13100
movq %rax, %r14
jmp 0x13112
movq %rax, %r14
jmp 0x1311c
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x51d0
leaq 0x20(%rsp), %rdi
callq 0x51d0
leaq 0xf0(%rsp), %rdi
callq 0x50e0
movq 0x18(%rsp), %rdi
callq 0x55b0
movq 0x10(%rsp), %rdi
callq 0x51d0
movq %rbx, %rdi
callq 0x13568
movq %r14, %rdi
callq 0x56c0
nop