// File: /workspace/github_cpp_makefile_star2_O2/yangwanyuan[P]HLKVDS/src/IndexManager.cc
// Function: hlkvds::IndexManager::UpdateIndex(hlkvds::KVSlice*, bool)
bool IndexManager::UpdateIndex(KVSlice* slice, bool gc_update) {
    const Kvdb_Digest *digest = &slice->GetDigest();

    HashEntry entry = slice->GetHashEntry();
    const char* data = slice->GetData();

    uint32_t hash_index = KeyDigestHandle::Hash(digest) % htSize_;


    if (gc_update) {
        HashEntry entry_before_gc = slice->GetHashEntryBeforeGC();
        if (!IsSameInMem(entry_before_gc)) {
            dataStor_->ModifyDeathEntry(entry);
            return true;
        }
        else {
            std::lock_guard<std::mutex> l(hashtable_[hash_index].slotMtx_);
            LinkedList<HashEntry> *entry_list = hashtable_[hash_index].entryList_;

            dataStor_->ModifyDeathEntry(entry_before_gc);
            entry_list->put(entry);
            return true;
        }
    }

    std::unique_lock<std::mutex> meta_lck(mtx_, std::defer_lock);
    std::lock_guard<std::mutex> l(hashtable_[hash_index].slotMtx_);
    LinkedList<HashEntry> *entry_list = hashtable_[hash_index].entryList_;

    bool is_exist = entry_list->search(entry);
    if (!is_exist) {
        if (data) {
            //It's insert a new entry operation
            meta_lck.lock();
            if (keyCounter_ == htSize_) {
                __WARN("UpdateIndex Failed, because the hashtable is full!");
                return false;
            }
            meta_lck.unlock();

            entry_list->put(entry);

            meta_lck.lock();
            keyCounter_++;
            dataTheorySize_ += SizeOfDataHeader() + slice->GetDataLen();
            meta_lck.unlock();

            __DEBUG("UpdateIndex request, because this entry is not exist! Now dataTheorySize_ is %ld", dataTheorySize_);
        }
        else {
            //It's a invalid delete operation
            dataStor_->ModifyDeathEntry(entry);
            __DEBUG("Ignore the UpdateIndex request, because this is a delete operation but not exist in Memory");
        }
    }
    else {
        HashEntry *entry_inMem = entry_list->getRef(entry);
        HashEntry::LogicStamp *lts = entry.GetLogicStamp();
        HashEntry::LogicStamp *lts_inMem = entry_inMem->GetLogicStamp();

        if ( *lts < *lts_inMem) {
            dataStor_->ModifyDeathEntry(entry);
            __DEBUG("Ignore the UpdateIndex request, because request is expired!");
        }
        else {
            //this operation is need to do
            dataStor_->ModifyDeathEntry(*entry_inMem);

            uint16_t data_size = entry.GetDataSize() ;
            uint16_t data_inMem_size = entry_inMem->GetDataSize();

            meta_lck.lock();
            if (data_size == 0) {
                dataTheorySize_ -= (uint64_t)(SizeOfDataHeader() + data_inMem_size);
            }
            else {
                dataTheorySize_ += ( data_size > data_inMem_size? 
                        ((uint64_t)(data_size - data_inMem_size)) : 
                        -((uint64_t)(data_inMem_size - data_size)) );
            }
            meta_lck.unlock();

            entry_list->put(entry);

            __DEBUG("UpdateIndex request, because request is new than in memory!Now dataTheorySize_ is %ld", dataTheorySize_);
        }
        return true;
    }

    __DEBUG("Update Index: key:%s, data_len:%u, seg_id:%u, data_offset:%u",
            slice->GetKeyStr().c_str(), slice->GetDataLen(),
            slice->GetSegId(), slice->GetHashEntry().GetDataOffsetInSeg());

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebx
movq %rsi, %r12
movq %rdi, %r14
movq 0x20(%rsi), %r15
movq 0x28(%rsi), %rsi
leaq 0x18(%rsp), %rdi
callq 0xe5d4
movq 0x10(%r12), %r13
movq %r15, %rdi
callq 0x103a8
xorl %edx, %edx
divl 0x8(%r14)
movl %edx, %r15d
testb %bl, %bl
je 0xec70
movq 0x38(%r12), %rsi
movq %rsp, %rdi
callq 0xe5d4
movq %rsp, %rsi
movq %r14, %rdi
callq 0xeee8
testb %al, %al
je 0xed09
movq (%r14), %rax
imulq $0x30, %r15, %r15
leaq (%rax,%r15), %rbx
addq $0x8, %rbx
movq %rbx, %rdi
callq 0xbc3a
movq (%r14), %rax
movq 0x30(%r14), %rdi
movq (%rax,%r15), %r14
movq (%rdi), %rax
movq %rsp, %rsi
callq *0xa0(%rax)
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0xf6c4
movq %rbx, %rdi
callq 0x6340
jmp 0xed1b
leaq 0x48(%r14), %rax
movq %rax, (%rsp)
movb $0x0, 0x8(%rsp)
movq (%r14), %rax
imulq $0x30, %r15, %r15
leaq (%rax,%r15), %rbx
addq $0x8, %rbx
movq %rbx, %rdi
callq 0xbc3a
movq (%r14), %rax
movq (%rax,%r15), %r15
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0xf786
testb %al, %al
je 0xed2b
leaq 0x30(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xe5d4
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
callq 0xf7b6
movq %rax, %r12
leaq 0x30(%rsp), %rdi
callq 0xe636
movq 0x20(%rsp), %rdi
movq 0x8(%r12), %rsi
callq 0xf7e8
movq 0x30(%r14), %rdi
movq (%rdi), %rcx
movq 0xa0(%rcx), %rcx
testb %al, %al
je 0xed8d
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
callq *%rcx
jmp 0xedfc
movq 0x30(%r14), %rdi
movq (%rdi), %rax
leaq 0x18(%rsp), %rsi
callq *0xa0(%rax)
movq %rsp, %rdi
callq 0xe636
movb $0x1, %bpl
jmp 0xee0c
testq %r13, %r13
je 0xedbb
movq %rsp, %rdi
callq 0xb49c
movl 0x18(%r14), %eax
cmpl 0x8(%r14), %eax
je 0xee27
movq %rsp, %rdi
callq 0xb4c6
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0xf6c4
movq %rsp, %rdi
callq 0xb49c
incl 0x18(%r14)
movzwl 0x18(%r12), %eax
movq 0x20(%r14), %rcx
addq %rcx, %rax
addq $0x20, %rax
movq %rax, 0x20(%r14)
movb $0x1, %bpl
movq %rsp, %rdi
callq 0xb4c6
jmp 0xedfc
movq %r12, %rsi
callq *%rcx
movq 0x18(%rsp), %rax
movzwl 0x16(%rax), %r13d
movq (%r12), %rax
movzwl 0x16(%rax), %r12d
movq %rsp, %rdi
callq 0xb49c
testq %r13, %r13
je 0xedd2
subq %r12, %r13
addq %r13, 0x20(%r14)
jmp 0xede4
movq 0x30(%r14), %rdi
movq (%rdi), %rax
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
callq *0xa0(%rax)
jmp 0xedfc
movq 0x20(%r14), %rax
negq %r12
addq %r12, %rax
addq $-0x20, %rax
movq %rax, 0x20(%r14)
movq %rsp, %rdi
callq 0xb4c6
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0xf6c4
movq %rbx, %rdi
callq 0x6340
movq %rsp, %rdi
callq 0xafd0
leaq 0x18(%rsp), %rdi
callq 0xe636
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x231aa(%rip), %r15     # 0x31fd8
movq (%r15), %r14
callq 0x63c0
leaq 0x10cf7(%rip), %rsi     # 0x1fb34
leaq 0x10d10(%rip), %rcx     # 0x1fb54
leaq 0x10c0a(%rip), %r8      # 0x1fa55
xorl %ebp, %ebp
movq %r14, %rdi
movq %rax, %rdx
movl $0x118, %r9d            # imm = 0x118
xorl %eax, %eax
callq 0x6580
movq (%r15), %rcx
leaq 0x10cf6(%rip), %rdi     # 0x1fb60
pushq $0x32
popq %rsi
pushq $0x1
popq %rdx
callq 0x6750
movq (%r15), %rsi
pushq $0xa
popq %rdi
callq 0x64b0
jmp 0xedfc
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0xe636
jmp 0xeec5
jmp 0xeec2
jmp 0xeeb0
jmp 0xeec2
jmp 0xeec2
movq %rax, %r14
movq %rbx, %rdi
callq 0x6340
jmp 0xeeb3
movq %rax, %r14
jmp 0xeecd
jmp 0xeebd
movq %rax, %r14
movq %rsp, %rdi
callq 0xe636
jmp 0xeed5
movq %rax, %r14
jmp 0xeed5
movq %rax, %r14
movq %rbx, %rdi
callq 0x6340
movq %rsp, %rdi
callq 0xafd0
leaq 0x18(%rsp), %rdi
callq 0xe636
movq %r14, %rdi
callq 0x6680
nop