uint8_t *SparseVector::GetValPtr(int32_t key) { int32_t st = 0, end = size_ - 1; int32_t found_idx; bool found = Search(key, st, end, &found_idx); if (found) return GetValPtrByIdx(found_idx); else if (capacity_ < size_ + 1) return 0; else if (found_idx < 0) { // empty buffer GetKeyByIdx(0) = key; ++size_; return GetValPtrByIdx(0); } else if (GetKeyByIdx(found_idx) > key) { InsertAtIndex(found_idx, key); return GetValPtrByIdx(found_idx); } else if (GetKeyByIdx(found_idx) < key) { if (found_idx + 1 < size_) { InsertAtIndex(found_idx + 1, key); } else { GetKeyByIdx(found_idx + 1) = key; ++size_; } return GetValPtrByIdx(found_idx + 1); } else { LOG(FATAL) << "Impossible to reach here!"; } return 0; }
//返回清除的记录数,写log ssize_t MemKeyCache::CleanNode(DUMP_MATCH_FUNC fMatchFunc,void* pArg) { char szKey[MAX_KEY_LEN]; ssize_t iCnt = 0; for (ssize_t iBucketIdx=0; iBucketIdx<m_pMemCacheHead->m_iBucketNum; iBucketIdx++) { ssize_t iNodeIdx = m_piBucket[iBucketIdx]; while( iNodeIdx >= 0 ) { ssize_t iKeyLen = GetKeyByIdx(iNodeIdx,szKey,MAX_KEY_LEN); ssize_t iDataFlag = GetFlag(iNodeIdx); //先指向下一个,然后删除 iNodeIdx = m_NodeObjMng.GetDsIdx(iNodeIdx,m_pMemCacheHead->m_iDSSuffix); //符合条件的 if(fMatchFunc(szKey,iKeyLen,iDataFlag,pArg) == 0) { Del(szKey,iKeyLen); iCnt++; } } } return iCnt; }
//返回dump的字节数 ssize_t MemKeyCache::CoreDumpMem(char* pBuffer,const ssize_t BUFFSIZE,DUMP_MATCH_FUNC fDumpMatchFunc,void* pArg) { ssize_t iBufferPos = 0; char* pTmpBuffer = new char[BUFFSIZE]; for (ssize_t iBucketIdx=0; iBucketIdx<m_pMemCacheHead->m_iBucketNum; iBucketIdx++) { ssize_t iNodeIdx = m_piBucket[iBucketIdx]; while( iNodeIdx >= 0 ) { ssize_t iKeyLen = GetKeyByIdx(iNodeIdx,pTmpBuffer,BUFFSIZE); ssize_t iDataFlag = GetFlag(iNodeIdx); char *pKey = pTmpBuffer; //符合条件的 if(fDumpMatchFunc(pKey,iKeyLen,iDataFlag,pArg) == 0) { ssize_t iBufferLen = m_BuffMng.GetBuffer(iNodeIdx,pTmpBuffer,BUFFSIZE); //存入总长度 memcpy(pBuffer+iBufferPos,&iBufferLen,sizeof(ssize_t)); iBufferPos += sizeof(ssize_t); //存入数据 memcpy(pBuffer+iBufferPos,pTmpBuffer,iBufferLen); iBufferPos += iBufferLen; } iNodeIdx = m_NodeObjMng.GetDsIdx(iNodeIdx,m_pMemCacheHead->m_iDSSuffix); } } delete []pTmpBuffer; return iBufferPos; }
void SparseVector::InsertAtIndex(int32_t index, int32_t key) { memmove(GetPtrByIdx(index + 1), GetPtrByIdx(index), (size_ - index)*(sizeof(int32_t) + value_size_)); GetKeyByIdx(index) = key; ++size_; }
uint8_t* SparseVector::GetByIdx(int32_t index, int32_t *key) { *key = GetKeyByIdx(index); return GetValPtrByIdx(index); }