// File: yangwanyuan[P]HLKVDS/src/IndexManager.cc
// Function: hlkvds::IndexManager::Set(char*, unsigned long)
bool IndexManager::Set(char* buff, uint64_t length) {
    if (length != sizeOndisk_) {
        return false;
    }
    char* buf_ptr = buff;

    //Set TS
    int64_t time_len = KVTime::SizeOf();
    time_t t;
    memcpy((void *)&t, (const void*)buf_ptr, time_len);
    lastTime_->SetTime(t);
    buf_ptr += time_len;

    __DEBUG("memcpy timestamp: %s at %p, at %ld", KVTime::ToChar(*lastTime_), (void *)buf_ptr, (int64_t)(buf_ptr-buff));

    //Set Index
    uint64_t counter_table_len = sizeof(int) * htSize_;
    char * counter_table_ptr = buf_ptr;
    char * ht_ptr = buf_ptr + counter_table_len;

    uint32_t total_entry = 0;
    uint64_t counter_size = sizeof(int);
    uint64_t entry_ondisk_size = IndexManager::SizeOfHashEntryOnDisk();

    for (uint32_t i = 0; i < htSize_; i++) {

        int slot_num = 0;
        memcpy((void*)&slot_num, (const void *)counter_table_ptr, sizeof(int));

        if (slot_num > 0) {
            for (int j = 0; j < slot_num; j++) {
                HashEntryOnDisk entry_ondisk;
                memcpy((void*)&entry_ondisk, (const void *)ht_ptr, entry_ondisk_size);
                HashEntry entry(entry_ondisk, *lastTime_, 0);
                hashtable_[i].entryList_->put(entry);
                total_entry++;
                ht_ptr += entry_ondisk_size;
            }
        }

        counter_table_ptr += counter_size;
    }

    if (total_entry != keyCounter_) {
        __ERROR("The Key Number is conflit between superblock and index!!!!!");
        return false;
    }

    return true;
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xe8(%rsp)
movq %rsi, 0xe0(%rsp)
movq %rdx, 0xd8(%rsp)
movq 0xe8(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movq 0xd8(%rsp), %rax
cmpq 0x10(%rcx), %rax
je 0x25b77
movb $0x0, 0xf7(%rsp)
jmp 0x25e98
movq 0xe0(%rsp), %rax
movq %rax, 0xd0(%rsp)
callq 0x116e0
movq %rax, 0xc8(%rsp)
movq 0xd0(%rsp), %rsi
movq 0xc8(%rsp), %rdx
leaq 0xc0(%rsp), %rdi
callq 0x8410
movq 0x18(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0xc0(%rsp), %rsi
callq 0x506c0
movq 0x18(%rsp), %rax
movq 0xc8(%rsp), %rcx
addq 0xd0(%rsp), %rcx
movq %rcx, 0xd0(%rsp)
movl 0x8(%rax), %eax
shlq $0x2, %rax
movq %rax, 0xb8(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xd0(%rsp), %rax
addq 0xb8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
movq $0x4, 0x98(%rsp)
callq 0x166b0
movq %rax, 0x90(%rsp)
movl $0x0, 0x8c(%rsp)
movq 0x18(%rsp), %rcx
movl 0x8c(%rsp), %eax
cmpl 0x8(%rcx), %eax
jae 0x25e05
movl $0x0, 0x88(%rsp)
movq 0xb0(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x88(%rsp)
cmpl $0x0, 0x88(%rsp)
jle 0x25dd7
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
cmpl 0x88(%rsp), %eax
jge 0x25dd5
leaq 0x5a(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x24e50
movq 0x10(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0x90(%rsp), %rdx
callq 0x8410
movq 0x18(%rsp), %rax
movq 0x40(%rax), %rsi
leaq 0x30(%rsp), %rdi
callq 0x50750
jmp 0x25ce6
xorl %eax, %eax
movl %eax, %ecx
leaq 0x40(%rsp), %rdi
leaq 0x5a(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x250f0
jmp 0x25d00
leaq 0x30(%rsp), %rdi
callq 0x50920
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl 0x8c(%rsp), %ecx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rdi
leaq 0x40(%rsp), %rsi
callq 0x279d0
jmp 0x25d31
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
movq 0x90(%rsp), %rax
addq 0xa8(%rsp), %rax
movq %rax, 0xa8(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x253b0
leaq 0x5a(%rsp), %rdi
callq 0x24fa0
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x25c94
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x28(%rsp)
movl %eax, 0x24(%rsp)
jmp 0x25dc6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x28(%rsp)
movl %eax, 0x24(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x50920
jmp 0x25dc6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x28(%rsp)
movl %eax, 0x24(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x253b0
leaq 0x5a(%rsp), %rdi
callq 0x24fa0
jmp 0x25ea9
jmp 0x25dd7
movq 0x98(%rsp), %rax
addq 0xb0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x25c4a
movq 0x18(%rsp), %rcx
movl 0xa4(%rsp), %eax
cmpl 0x18(%rcx), %eax
je 0x25e90
jmp 0x25e18
movq 0x4b1b9(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x8(%rsp)
callq 0x8480
movq 0x8(%rsp), %rdi
movq %rax, %rdx
leaq 0x2c676(%rip), %rsi     # 0x524b1
leaq 0x2ce7b(%rip), %rcx     # 0x52cbd
leaq 0x2cdd5(%rip), %r8      # 0x52c1e
movl $0xee, %r9d
movb $0x0, %al
callq 0x86f0
movq 0x4b17b(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x2ce5a(%rip), %rsi     # 0x52cc1
movb $0x0, %al
callq 0x86f0
movq 0x4b163(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x2cf46(%rip), %rsi     # 0x52dc5
movb $0x0, %al
callq 0x86f0
movb $0x0, 0xf7(%rsp)
jmp 0x25e98
movb $0x1, 0xf7(%rsp)
movb 0xf7(%rsp), %al
andb $0x1, %al
addq $0xf8, %rsp
retq
movq 0x28(%rsp), %rdi
callq 0x8820
nopw %cs:(%rax,%rax)