// File: /workspace/github_cpp_makefile_star2_O2/yangwanyuan[P]HLKVDS/src/MetaStor.cc
// Function: hlkvds::MetaStor::CreateMetaData()
bool MetaStor::CreateMetaData() {

    verifyMetaDevice();

    uint32_t index_ht_size          = 0;
    uint64_t index_region_offset    = 0;
    uint64_t index_region_length    = 0;
    uint32_t sst_total_num          = 0;
    uint64_t sst_region_offset      = 0;
    uint64_t sst_region_length      = 0;
    uint32_t data_store_type        = 0;
    uint32_t entry_count            = 0;
    uint64_t entry_theory_data_size = 0;
    bool grace_close_flag           = false;

    index_ht_size = options_.hashtable_size;

    //Init Block Device
    uint64_t meta_device_capacity = metaDev_->GetDeviceCapacity();

    //Init Superblock region
    uint64_t sb_region_length = SuperBlockManager::SuperBlockSizeOnDevice();

    if (meta_device_capacity < sb_region_length) {
        __ERROR("The capacity of device is too small, can't store sb region");
        return false;
    }

    sbOff_ = 0;
    if (!createSuperBlock()) {
        return false;
    }

    __DEBUG("Init super block region success.");

    //Init Index region
    if (index_ht_size == 0) {
        index_ht_size = (meta_device_capacity / ALIGNED_SIZE) * 2;
    }
    index_ht_size = IndexManager::CalcHashSizeForPower2(index_ht_size);

    index_region_length = IndexManager::CalcIndexSizeOnDevice(index_ht_size);
    __DEBUG("index region size; %ld", index_region_length);

    if (meta_device_capacity < (sb_region_length + index_region_length)) {
        __ERROR("The capacity of device is too small, can't store index region");
        return false;
    }

    index_region_offset = sb_region_length;
    idxOff_ = sb_region_length;
    if (!createIndex(index_ht_size, index_region_length)) {
        return false;
    }
    
    __DEBUG("Init index region success.");

    //Init SST region
    sst_region_offset = sb_region_length + index_region_length;

    sstOff_ = sst_region_offset;
    if (!createDataStor()) {
        __ERROR("Segment region init failed.");
        return false;
    }

    __DEBUG("Init segment region success.");

    //Set zero to device.
    sst_total_num = dataStor_->GetTotalSegNum();
    sst_region_length = dataStor_->GetSSTsLengthOnDisk();
    uint64_t db_meta_region_length = sb_region_length + index_region_length + sst_region_length;

    if ( !zeroDeviceMetaRegion(db_meta_region_length) ) {
        return false;
    }

    //Init reserve region
    data_store_type = options_.datastor_type;
    grace_close_flag = 0;

    //Set SuperBlock
    DBSuperBlock sb(MAGIC_NUMBER, index_ht_size, index_region_offset, index_region_length,
                    sst_total_num, sst_region_offset, sst_region_length, data_store_type,
                    entry_count, entry_theory_data_size, grace_close_flag);
    sbMgr_->SetSuperBlock(sb);

    //Set SuperBlock reserved region
    uint64_t reserved_region_length = SuperBlockManager::ReservedRegionLength();
    char * reserved_content = new char[reserved_region_length];
    if ( !dataStor_->GetSBReservedContent(reserved_content, reserved_region_length) ) {
        delete[] reserved_content;
        return false;
    }
    if ( !sbMgr_->SetReservedContent(reserved_content, reserved_region_length) ) {
        delete[] reserved_content;
        return false;
    }

    delete[] reserved_content;
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq 0x30(%rdi), %rcx
movq (%rax), %rax
movq (%rax), %rdi
movq %rdi, 0x10(%rbx)
movl 0x40(%rcx), %ebp
movq (%rdi), %rax
callq *0x30(%rax)
cmpq $0xfff, %rax            # imm = 0xFFF
jbe 0x1345a
movq %rax, %r15
andq $0x0, 0x38(%rbx)
movq %rbx, %rdi
callq 0x13550
testb %al, %al
je 0x13447
movq %r15, %rdi
shrq $0xb, %rdi
andl $-0x2, %edi
testl %ebp, %ebp
cmovnel %ebp, %edi
callq 0xf4a8
movl %eax, %ebp
movl %eax, %edi
callq 0xf378
leaq 0x1000(%rax), %r13
cmpq %r13, %r15
jb 0x134ae
movq %rax, %r14
movq $0x1000, 0x40(%rbx)     # imm = 0x1000
movq %rbx, %rdi
movl %ebp, %esi
movq %rax, %rdx
callq 0x135fc
testb %al, %al
je 0x13447
movq %r13, 0x48(%rbx)
movq %rbx, %rdi
callq 0x136b2
testb %al, %al
je 0x134f3
movq 0x28(%rbx), %rdi
movq (%rdi), %rax
callq *0x90(%rax)
movl %eax, %r15d
movq 0x28(%rbx), %rdi
movq (%rdi), %rax
callq *0x98(%rax)
movq %rax, %r12
leaq (%rax,%r13), %rsi
movq %rbx, %rdi
callq 0x137ea
testb %al, %al
je 0x13447
movq 0x30(%rbx), %rax
movl 0x3c(%rax), %eax
leaq 0x8(%rsp), %rsi
movl $0xffff0001, (%rsi)     # imm = 0xFFFF0001
movl %ebp, 0x4(%rsi)
movq $0x1000, 0x8(%rsi)      # imm = 0x1000
movq %r14, 0x10(%rsi)
movl %r15d, 0x18(%rsi)
movq %r13, 0x1c(%rsi)
movq %r12, 0x24(%rsi)
movl %eax, 0x2c(%rsi)
andl $0x0, 0x30(%rsi)
andq $0x0, 0x34(%rsi)
movb $0x0, 0x3c(%rsi)
movq 0x18(%rbx), %rdi
callq 0x1ac36
movl $0xfc3, %edi            # imm = 0xFC3
callq 0x6040
movq %rax, %r14
movq 0x28(%rbx), %rdi
movq (%rdi), %rax
movl $0xfc3, %edx            # imm = 0xFC3
movq %r14, %rsi
callq *0x60(%rax)
testb %al, %al
je 0x134a2
movq 0x18(%rbx), %rdi
movl $0xfc3, %edx            # imm = 0xFC3
movq %r14, %rsi
callq 0x1ad10
movl %eax, %ebx
jmp 0x134a4
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1eb77(%rip), %r15     # 0x31fd8
movq (%r15), %r14
callq 0x63c0
leaq 0xbe35(%rip), %rsi      # 0x1f2a5
leaq 0xcbf4(%rip), %rcx      # 0x2006b
leaq 0xcbc5(%rip), %r8       # 0x20043
xorl %ebx, %ebx
pushq $0x43
popq %r9
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6580
movq (%r15), %rcx
leaq 0xcbdf(%rip), %rdi      # 0x2007a
pushq $0x3a
jmp 0x13536
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x64c0
jmp 0x13449
movq 0x1eb23(%rip), %r15     # 0x31fd8
movq (%r15), %r14
callq 0x63c0
leaq 0xbde1(%rip), %rsi      # 0x1f2a5
leaq 0xcba0(%rip), %rcx      # 0x2006b
leaq 0xcb71(%rip), %r8       # 0x20043
xorl %ebx, %ebx
pushq $0x58
popq %r9
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6580
movq (%r15), %rcx
leaq 0xcbc6(%rip), %rdi      # 0x200b5
pushq $0x3d
jmp 0x13536
movq 0x1eade(%rip), %r15     # 0x31fd8
movq (%r15), %r14
callq 0x63c0
leaq 0xbd9c(%rip), %rsi      # 0x1f2a5
leaq 0xcb5b(%rip), %rcx      # 0x2006b
leaq 0xcb2c(%rip), %r8       # 0x20043
xorl %ebx, %ebx
pushq $0x69
popq %r9
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6580
movq (%r15), %rcx
leaq 0xcbbf(%rip), %rdi      # 0x200f3
pushq $0x1b
popq %rsi
pushq $0x1
popq %rdx
callq 0x6750
movq (%r15), %rsi
pushq $0xa
popq %rdi
callq 0x64b0
jmp 0x13449
nop