// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r14
movq 0x20(%rsi), %rbx
movq 0x28(%rsi), %rsi
leaq 0x20(%rsp), %rdi
callq 0x11134
movq 0x10(%r12), %r15
movq %rbx, %rdi
callq 0x12e82
xorl %edx, %edx
divl 0x8(%r14)
movl %edx, %ebx
testb %bpl, %bpl
je 0x117bc
movq 0x38(%r12), %rsi
leaq 0x8(%rsp), %rdi
callq 0x11134
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x11a44
testb %al, %al
je 0x1188e
movq (%r14), %rax
movl %ebx, %ecx
leaq (%rcx,%rcx,2), %r15
shlq $0x4, %r15
leaq (%rax,%r15), %rbx
addq $0x8, %rbx
movq %rbx, %rdi
callq 0x6580
testl %eax, %eax
jne 0x119d3
movq (%r14), %rax
movq 0x30(%r14), %rdi
movq (%rax,%r15), %r14
movq (%rdi), %rax
leaq 0x8(%rsp), %rsi
callq *0xa0(%rax)
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x12376
movq %rbx, %rdi
callq 0x6350
jmp 0x118a0
leaq 0x48(%r14), %rax
movq %rax, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
movq (%r14), %rax
movl %ebx, %ecx
leaq (%rcx,%rcx,2), %rbx
shlq $0x4, %rbx
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
movq %rdi, 0x38(%rsp)
callq 0x6580
testl %eax, %eax
jne 0x119cc
movq (%r14), %rax
movq (%rax,%rbx), %rbx
movq (%rbx), %r13
testq %r13, %r13
je 0x11821
leaq 0x20(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x111ea
testb %al, %al
jne 0x118b2
movq 0x18(%r13), %r13
testq %r13, %r13
jne 0x11805
testq %r15, %r15
je 0x11921
leaq 0x8(%rsp), %rdi
callq 0xe58e
movl 0x18(%r14), %eax
cmpl 0x8(%r14), %eax
je 0x119c3
leaq 0x8(%rsp), %rdi
callq 0xe5ca
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x12376
leaq 0x8(%rsp), %rdi
callq 0xe58e
incl 0x18(%r14)
movzwl 0x18(%r12), %eax
movq 0x20(%r14), %rcx
addq %rcx, %rax
addq $0x20, %rax
movq %rax, 0x20(%r14)
movb $0x1, %bpl
leaq 0x8(%rsp), %rdi
callq 0xe5ca
jmp 0x11994
movq 0x30(%r14), %rdi
movq (%rdi), %rax
leaq 0x20(%rsp), %rsi
callq *0xa0(%rax)
leaq 0x8(%rsp), %rdi
callq 0x1119a
movb $0x1, %bpl
jmp 0x119a8
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x11134
movq (%rbx), %r12
testq %r12, %r12
je 0x118e7
leaq 0x40(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x111ea
testb %al, %al
jne 0x118ea
movq 0x18(%r12), %r12
testq %r12, %r12
jne 0x118ce
xorl %r12d, %r12d
leaq 0x40(%rsp), %rdi
callq 0x1119a
movq 0x28(%rsp), %rdi
movq 0x8(%r12), %rsi
callq 0x1243a
movq 0x30(%r14), %rdi
movq (%rdi), %rcx
movq 0xa0(%rcx), %rcx
testb %al, %al
je 0x11938
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
callq *%rcx
jmp 0x11994
movq 0x30(%r14), %rdi
movq (%rdi), %rax
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
callq *0xa0(%rax)
jmp 0x11994
movq %r12, %rsi
callq *%rcx
movq 0x20(%rsp), %rax
movzwl 0x16(%rax), %r13d
movq (%r12), %rax
movzwl 0x16(%rax), %r15d
leaq 0x8(%rsp), %rdi
callq 0xe58e
testq %r13, %r13
je 0x11968
subq %r15, %r13
addq %r13, 0x20(%r14)
jmp 0x1197a
movq 0x20(%r14), %rax
negq %r15
addq %r15, %rax
addq $-0x20, %rax
movq %rax, 0x20(%r14)
leaq 0x8(%rsp), %rdi
callq 0xe5ca
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x12376
movq 0x38(%rsp), %rdi
callq 0x6350
leaq 0x8(%rsp), %rdi
callq 0xe10e
leaq 0x20(%rsp), %rdi
callq 0x1119a
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x738e
xorl %ebp, %ebp
jmp 0x11994
movl %eax, %edi
callq 0x62b0
movl %eax, %edi
callq 0x62b0
jmp 0x11a1b
jmp 0x11a1b
jmp 0x119f6
jmp 0x11a1b
movq %rax, %r14
movq %rbx, %rdi
callq 0x6350
jmp 0x119f9
movq %rax, %r14
jmp 0x11a28
jmp 0x11a05
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0x1119a
jmp 0x11a32
movq %rax, %r14
jmp 0x11a32
jmp 0x11a1b
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x1119a
jmp 0x11a1e
movq %rax, %r14
movq 0x38(%rsp), %rdi
callq 0x6350
leaq 0x8(%rsp), %rdi
callq 0xe10e
leaq 0x20(%rsp), %rdi
callq 0x1119a
movq %r14, %rdi
callq 0x6670