// File: /workspace/github_cpp_makefile_star2_O1/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 0x18(%rsp), %rdi
callq 0x11000
movq 0x10(%r12), %r15
movq %rbx, %rdi
callq 0x12d5e
xorl %edx, %edx
divl 0x8(%r14)
movl %edx, %ebx
testb %bpl, %bpl
je 0x11684
movq 0x38(%r12), %rsi
movq %rsp, %rdi
callq 0x11000
movq %rsp, %rsi
movq %r14, %rdi
callq 0x11916
testb %al, %al
je 0x11776
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 0x6590
testl %eax, %eax
jne 0x118a9
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 0x1223c
movq %rbx, %rdi
callq 0x6360
jmp 0x11788
leaq 0x48(%r14), %rax
movq %rax, (%rsp)
movb $0x0, 0x8(%rsp)
movq (%r14), %rax
movl %ebx, %ecx
leaq (%rcx,%rcx,2), %rbx
shlq $0x4, %rbx
leaq (%rax,%rbx), %r13
addq $0x8, %r13
movq %r13, %rdi
callq 0x6590
testl %eax, %eax
jne 0x118a2
movq %r13, 0x38(%rsp)
movq (%r14), %rax
movq (%rax,%rbx), %rax
movq %rax, 0x30(%rsp)
movq (%rax), %r13
testq %r13, %r13
setne %bl
je 0x116f2
leaq 0x18(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x110b6
testb %al, %al
jne 0x116f2
movq 0x18(%r13), %r13
testq %r13, %r13
setne %bl
jne 0x116d7
testb $0x1, %bl
je 0x11798
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x11000
movq 0x30(%rsp), %rax
movq (%rax), %r12
testq %r12, %r12
je 0x11735
leaq 0x40(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x110b6
testb %al, %al
jne 0x11738
movq 0x18(%r12), %r12
testq %r12, %r12
jne 0x1171c
xorl %r12d, %r12d
leaq 0x40(%rsp), %rdi
callq 0x11066
movq 0x20(%rsp), %rdi
movq 0x8(%r12), %rsi
callq 0x12306
movq 0x30(%r14), %rdi
movq (%rdi), %rcx
movq 0xa0(%rcx), %rcx
testb %al, %al
je 0x117fc
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
callq *%rcx
jmp 0x1186c
movq 0x30(%r14), %rdi
movq (%rdi), %rax
leaq 0x18(%rsp), %rsi
callq *0xa0(%rax)
movq %rsp, %rdi
callq 0x11066
movb $0x1, %bpl
jmp 0x1187e
testq %r15, %r15
je 0x11829
movq %rsp, %rdi
callq 0xe40a
movl 0x18(%r14), %eax
cmpl 0x8(%r14), %eax
je 0x11899
movq %rsp, %rdi
callq 0xe446
leaq 0x18(%rsp), %rsi
movq 0x30(%rsp), %rdi
callq 0x1223c
movq %rsp, %rdi
callq 0xe40a
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 0xe446
jmp 0x1186c
movq %r12, %rsi
callq *%rcx
movq 0x18(%rsp), %rax
movzwl 0x16(%rax), %r15d
movq (%r12), %rax
movzwl 0x16(%rax), %ebx
movq %rsp, %rdi
callq 0xe40a
testq %r15, %r15
je 0x11840
subq %rbx, %r15
addq %r15, 0x20(%r14)
jmp 0x11852
movq 0x30(%r14), %rdi
movq (%rdi), %rax
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
callq *0xa0(%rax)
jmp 0x1186c
movq 0x20(%r14), %rax
negq %rbx
addq %rbx, %rax
addq $-0x20, %rax
movq %rax, 0x20(%r14)
movq %rsp, %rdi
callq 0xe446
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
movq 0x30(%rsp), %rdi
callq 0x1223c
movq 0x38(%rsp), %rdi
callq 0x6360
movq %rsp, %rdi
callq 0xdf86
leaq 0x18(%rsp), %rdi
callq 0x11066
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x73d2
xorl %ebp, %ebp
jmp 0x1186c
movl %eax, %edi
callq 0x62c0
movl %eax, %edi
callq 0x62c0
jmp 0x118ef
jmp 0x118cc
jmp 0x118ef
jmp 0x118ef
movq %rax, %r14
movq %rbx, %rdi
callq 0x6360
jmp 0x118cf
movq %rax, %r14
jmp 0x118fc
jmp 0x118db
movq %rax, %r14
movq %rsp, %rdi
callq 0x11066
jmp 0x11904
jmp 0x118ef
movq %rax, %r14
jmp 0x11904
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x11066
jmp 0x118f2
movq %rax, %r14
movq 0x38(%rsp), %rdi
callq 0x6360
movq %rsp, %rdi
callq 0xdf86
leaq 0x18(%rsp), %rdi
callq 0x11066
movq %r14, %rdi
callq 0x6680