// File: 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
movq %rsp, %rbp
subq $0x410, %rsp            # imm = 0x410
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x388(%rbp)
movq %rdi, %rax
movq %rax, -0x398(%rbp)
callq 0x34270
movq -0x388(%rbp), %rdi
movl -0xc(%rbp), %eax
movl %eax, 0x30(%rdi)
movl -0x10(%rbp), %eax
movl %eax, 0x34(%rdi)
movl -0x14(%rbp), %eax
movl %eax, 0x38(%rdi)
addq $0x40, %rdi
movq %rdi, -0x390(%rbp)
callq 0x66e0
movq -0x388(%rbp), %rdi
addq $0x60, %rdi
movq %rdi, -0x380(%rbp)
callq 0x6850
jmp 0x339b4
movl -0x14(%rbp), %esi
leaq -0x88(%rbp), %rdi
callq 0x34330
jmp 0x339c5
leaq 0xd84d(%rip), %rsi      # 0x41219
leaq -0x68(%rbp), %rdi
leaq -0x88(%rbp), %rdx
callq 0x342e0
jmp 0x339de
leaq 0xd841(%rip), %rdx      # 0x41226
leaq -0x48(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x34290
jmp 0x339f4
movq -0x398(%rbp), %rdi
addq $0x40, %rdi
leaq -0x48(%rbp), %rsi
callq 0x6630
leaq -0x48(%rbp), %rdi
callq 0x6340
leaq -0x68(%rbp), %rdi
callq 0x6340
leaq -0x88(%rbp), %rdi
callq 0x6340
movq -0x398(%rbp), %rdi
addq $0x40, %rdi
callq 0x61f0
movq %rax, %rdi
leaq -0x118(%rbp), %rsi
callq 0x6490
cmpl $-0x1, %eax
je 0x33dff
leaq -0x320(%rbp), %rdi
callq 0x65e0
jmp 0x33a5c
movq -0x398(%rbp), %rsi
addq $0x40, %rsi
leaq -0x320(%rbp), %rdi
movl $0x8, %edx
callq 0x6450
jmp 0x33a7a
leaq 0x9cf(%rip), %rsi       # 0x34450
leaq -0x320(%rbp), %rdi
callq 0x6830
jmp 0x33a8f
movq 0x2b52a(%rip), %rdi     # 0x5efc0
leaq 0xd78e(%rip), %rsi      # 0x4122b
callq 0x64e0
movq %rax, -0x3a0(%rbp)
jmp 0x33aab
movq -0x3a0(%rbp), %rdi
movq -0x398(%rbp), %rsi
addq $0x40, %rsi
callq 0x6480
movq %rax, -0x3a8(%rbp)
jmp 0x33acb
movq -0x3a8(%rbp), %rdi
movq 0x2b4b7(%rip), %rsi     # 0x5ef90
callq 0x6540
jmp 0x33ae0
jmp 0x33ae2
leaq -0x320(%rbp), %rdi
callq 0x61d0
movl %eax, -0x3ac(%rbp)
jmp 0x33af6
movl -0x3ac(%rbp), %eax
cmpl $-0x1, %eax
je 0x33dd5
leaq -0x320(%rbp), %rdi
leaq -0x321(%rbp), %rsi
callq 0x65d0
jmp 0x33b1a
movzbl -0x321(%rbp), %eax
movq %rax, -0x3c0(%rbp)
leaq -0x349(%rbp), %rdi
movq %rdi, -0x3b8(%rbp)
callq 0x68d0
movq -0x3c0(%rbp), %rsi
movq -0x3b8(%rbp), %rcx
leaq -0x348(%rbp), %rdi
xorl %edx, %edx
callq 0x17140
jmp 0x33b59
leaq -0x349(%rbp), %rdi
callq 0x6560
xorl %eax, %eax
movl %eax, %esi
leaq -0x348(%rbp), %rdi
callq 0x69b0
movq %rax, -0x3c8(%rbp)
jmp 0x33b7e
movq -0x3c8(%rbp), %rsi
movzbl -0x321(%rbp), %eax
movl %eax, %edx
leaq -0x320(%rbp), %rdi
callq 0x6660
jmp 0x33b9c
leaq -0x320(%rbp), %rdi
leaq -0x321(%rbp), %rsi
callq 0x65d0
jmp 0x33bb1
movzbl -0x321(%rbp), %eax
movq %rax, -0x3d8(%rbp)
leaq -0x371(%rbp), %rdi
movq %rdi, -0x3d0(%rbp)
callq 0x68d0
movq -0x3d8(%rbp), %rsi
movq -0x3d0(%rbp), %rcx
leaq -0x370(%rbp), %rdi
xorl %edx, %edx
callq 0x17140
jmp 0x33bf0
leaq -0x371(%rbp), %rdi
callq 0x6560
xorl %eax, %eax
movl %eax, %esi
leaq -0x370(%rbp), %rdi
callq 0x69b0
movq %rax, -0x3e0(%rbp)
jmp 0x33c15
movq -0x3e0(%rbp), %rsi
movzbl -0x321(%rbp), %eax
movl %eax, %edx
leaq -0x320(%rbp), %rdi
callq 0x6660
jmp 0x33c33
movq -0x398(%rbp), %rdi
leaq -0x348(%rbp), %rsi
callq 0x34480
movq %rax, -0x3e8(%rbp)
jmp 0x33c4f
movq -0x3e8(%rbp), %rdi
leaq -0x370(%rbp), %rsi
callq 0x6070
jmp 0x33c64
movq 0x2b355(%rip), %rdi     # 0x5efc0
leaq 0xd5cc(%rip), %rsi      # 0x4123e
callq 0x64e0
movq %rax, -0x3f0(%rbp)
jmp 0x33c80
movq -0x3f0(%rbp), %rdi
leaq -0x348(%rbp), %rsi
callq 0x6480
movq %rax, -0x3f8(%rbp)
jmp 0x33c9c
movq -0x3f8(%rbp), %rdi
leaq 0xd5a0(%rip), %rsi      # 0x4124a
callq 0x64e0
movq %rax, -0x400(%rbp)
jmp 0x33cb8
movq -0x400(%rbp), %rdi
leaq -0x370(%rbp), %rsi
callq 0x6480
movq %rax, -0x408(%rbp)
jmp 0x33cd4
movq -0x408(%rbp), %rdi
movq 0x2b2ae(%rip), %rsi     # 0x5ef90
callq 0x6540
jmp 0x33ce9
leaq -0x370(%rbp), %rdi
callq 0x6340
leaq -0x348(%rbp), %rdi
callq 0x6340
jmp 0x33ae2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x33e32
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x33e26
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x33d4b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x6340
leaq -0x88(%rbp), %rdi
callq 0x6340
jmp 0x33e26
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x33df1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x349(%rbp), %rdi
callq 0x6560
jmp 0x33df1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x33dc7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x371(%rbp), %rdi
callq 0x6560
jmp 0x33dc7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x370(%rbp), %rdi
callq 0x6340
leaq -0x348(%rbp), %rdi
callq 0x6340
jmp 0x33df1
leaq -0x320(%rbp), %rdi
callq 0x60f0
jmp 0x33de3
leaq -0x320(%rbp), %rdi
callq 0x6130
jmp 0x33dff
leaq -0x320(%rbp), %rdi
callq 0x6130
jmp 0x33e26
movq -0x398(%rbp), %rsi
movq %rsi, %rdi
addq $0x60, %rdi
addq $0x40, %rsi
movl $0x1, %edx
callq 0x6270
jmp 0x33e1d
addq $0x410, %rsp            # imm = 0x410
popq %rbp
retq
movq -0x380(%rbp), %rdi
callq 0x6760
movq -0x390(%rbp), %rdi
callq 0x6340
movq -0x388(%rbp), %rdi
callq 0x34560
movq -0x20(%rbp), %rdi
callq 0x68c0
nopw %cs:(%rax,%rax)