void* CIndexTreeBlockFile::Get(void* pvKey, int iKeySize) { CIndexTreeNodeFile* pcNode; void* pv; pcNode = GetIndexNode(pvKey, iKeySize); if (pcNode == NULL) { return NULL; } else { if (pcNode->GetObjectSize() == 0) { return NULL; } pv = pcNode->GetObjectPtr(); return pv; } }
/*----------------------------------------------------------------------------- * 获取data cache by position *-----------------------------------------------------------------------------*/ int GetCacheDataByPos(HashTable *pstHashTable, uint32_t dwUin, char *sDataBuf, uint32_t *pdwDataLen) { int ret = 0; IndexNode *pstIndexNode = NULL; if(dwUin == 0 || sDataBuf == NULL || pdwDataLen == NULL) { return -1; } DD("GetCacheData"); DD("dwUin = %d dwDataLen = %d\n", dwUin, *pdwDataLen); DD("******************************\n"); if((ret = GetIndexNode(pstHashTable, dwUin, &pstIndexNode)) < 0) { printf("Get dwUin = %d IndexNode error. ret = %d\n", dwUin, ret); if(pstIndexNode != NULL) { if(pstIndexNode->cFlags == INODE_LOCKED) { fprintf(stderr, "dwUin = %d Index Node Locked \n", dwUin); return -2; } } *pdwDataLen = 0; return UIN_NOT_EXIST; } printf("startpos = %d\n", pstIndexNode->dwPosition); DD("dwUin = %d dwDataLen = %d ddwUnitSeq = %ld ddwUinSeq = %ld\n", dwUin, *pdwDataLen, pstIndexNode->ddwUnitSeq, pstIndexNode->ddwUinSeq); if((ret = GetLinkTableDataByPos(dwUin, sDataBuf, pdwDataLen,pstIndexNode->dwPosition)) < 0) { DD("GetLinkTableDataByPos error. ret = %d error = %s\n", ret, strerror(errno)); return -4; } *pdwDataLen = pstIndexNode->dwDatalen; return 0; }
/*----------------------------------------------------------------------------- * 设置data cache * 先获取indexnode,进行时间对比 * 然后获取旧的数据,并在linktable中分配新的空间 * 然后修改indexnode中的相关值 *-----------------------------------------------------------------------------*/ int SetCacheDataByKey(HashTable *pstHashTable, IndexNode *pstIndexNode, char *sDataBuf, uint32_t dwDataLen) /*int SetCacheDataByKey(HashTable *pstHashTable, uint32_t dwUin, uint64_t ddwTime,char *sDataBuf, uint32_t dwDataLen)*/ { int ret = 0; IndexNode *pstOldIndexNode = NULL, *pstNewIndexNode = NULL; uint32_t dwStartPos = 0,dwNextPos = 0, dwUin = 0; uint64_t ddwTime = 0; if(pstHashTable == NULL || pstIndexNode == NULL || sDataBuf == NULL || dwDataLen == 0) { return -1; } dwUin = pstIndexNode->dwUin; ddwTime = pstIndexNode->ddwLastPublicTime; if((ret = GetIndexNode(pstHashTable,dwUin, &pstOldIndexNode)) < 0) { DD("Get dwUin = %d IndexNode error. ret = %d\n", dwUin, ret); /*return -2;*/ } dwUinDataLen = 0; /*判断顺序时间*/ if(pstOldIndexNode != NULL) { DD("pstOldIndexNode->ddwLastPublicTime = %ld\t ddwTime = %ld.\n", pstOldIndexNode->ddwLastPublicTime, ddwTime); if(pstOldIndexNode->ddwLastPublicTime >= ddwTime) { DD("pstOldIndexNode->ddwLastPublicTime = %ld\t ddwTime = %ld.\n", pstOldIndexNode->ddwLastPublicTime, ddwTime); return -2; } printf("--------------获取旧数据-------------\n"); dwUinDataLen = MAX_UIN_CACHED_BUFLEN; if((ret = GetCacheDataByPos(pstHashTable, dwUin, szUinData,&dwUinDataLen)) < 0) { /*第一次写入*/ if(ret == UIN_NOT_EXIST) { } else { DD("GetCacheDataByPos error. ret = %d\n",ret); return -3; } } } printf("len = %d\t data = %s\n", dwUinDataLen, szUinData); memcpy(szUinData + dwUinDataLen, sDataBuf, dwDataLen); dwUinDataLen += dwDataLen; /*if((ret = SetLinkTableDataByKey(dwUin, sDataBuf, dwDataLen,&dwStartPos)) < 0)*/ if((ret = SetLinkTableDataByKey(dwUin, szUinData, dwUinDataLen,&dwStartPos)) < 0) { fprintf(stderr, "SetLinkTableDataByKey error. ret = %d error = %s\n", ret, strerror(errno)); return -4; } printf("startpos = %d\n", dwStartPos); /*如果没有索引节点*/ if(pstOldIndexNode == NULL) { DD("get new index node\n"); /*获取一个新的索引节点*/ if((pstNewIndexNode = GetEmptyNode(pstHashTable,dwUin)) == NULL) { /*如果失败 则释放之前申请的linktable资源*/ ClearOldElement(dwUin,dwStartPos); return -5; } pstNewIndexNode->dwPosition = dwStartPos; /*pstNewIndexNode->dwDatalen = dwDataLen;*/ pstNewIndexNode->dwDatalen = dwUinDataLen; pstNewIndexNode->ddwLastPublicTime = ddwTime; pstNewIndexNode->ddwUnitSeq = pstIndexNode->ddwUnitSeq; pstNewIndexNode->ddwUinSeq = pstIndexNode->ddwUinSeq; printf("SetCacheDataByKey pstIndexNode->dwUin = %d\n",pstNewIndexNode->dwUin); printf("SetCacheDataByKey pstIndexNode->dwPosition = %d\n",pstNewIndexNode->dwPosition); printf("SetCacheDataByKey pstIndexNode->dwDatalen = %d\n",pstNewIndexNode->dwDatalen); printf("SetCacheDataByKey pstIndexNode->ddwLastPublicTime = %ld\n",pstNewIndexNode->ddwLastPublicTime); } /*有索引节点 修改起始块号 释放之前申请的块*/ else { dwNextPos = pstOldIndexNode->dwPosition; pstOldIndexNode->dwPosition = dwStartPos; pstOldIndexNode->dwDatalen = dwUinDataLen; pstOldIndexNode->ddwLastPublicTime = ddwTime; pstOldIndexNode->ddwUnitSeq = pstIndexNode->ddwUnitSeq; pstOldIndexNode->ddwUinSeq = pstIndexNode->ddwUinSeq; printf("SetCacheDataByKey pstIndexNode->dwUin = %d\n",pstOldIndexNode->dwUin); printf("SetCacheDataByKey pstIndexNode->dwPosition = %d\n",pstOldIndexNode->dwPosition); printf("SetCacheDataByKey pstIndexNode->dwDatalen = %d\n",pstOldIndexNode->dwDatalen); printf("SetCacheDataByKey pstIndexNode->ddwLastPublicTime = %ld\n",pstOldIndexNode->ddwLastPublicTime); ClearOldElement(dwUin, dwNextPos); } return 0; }