提交 a086f83c 编写于 作者: H Hongze Cheng

fix: slow TDB page defragment

上级 e3ba07e0
......@@ -404,7 +404,52 @@ static int tdbPageFree(SPage *pPage, int idx, SCell *pCell, int szCell) {
return 0;
}
typedef struct {
int32_t iCell;
int32_t offset;
} SCellIdx;
static int32_t tCellIdxCmprFn(const void *p1, const void *p2) {
if (((SCellIdx *)p1)->offset < ((SCellIdx *)p2)->offset) {
return -1;
} else if (((SCellIdx *)p1)->offset > ((SCellIdx *)p2)->offset) {
return 1;
} else {
return 0;
}
}
static int tdbPageDefragment(SPage *pPage) {
#if 1
int32_t nFree = TDB_PAGE_NFREE(pPage);
int32_t nCell = TDB_PAGE_NCELLS(pPage);
SCellIdx *aCellIdx = (SCellIdx *)tdbOsMalloc(sizeof(SCellIdx) * nCell);
if (aCellIdx == NULL) return -1;
for (int32_t iCell = 0; iCell < nCell; iCell++) {
aCellIdx[iCell].iCell = iCell;
aCellIdx[iCell].offset = TDB_PAGE_CELL_OFFSET_AT(pPage, iCell);
}
taosSort(aCellIdx, nCell, sizeof(SCellIdx), tCellIdxCmprFn);
SCell *pNextCell = (u8 *)pPage->pPageFtr;
for (int32_t iCell = nCell - 1; iCell >= 0; iCell--) {
SCell *pCell = TDB_PAGE_CELL_AT(pPage, aCellIdx[iCell].iCell);
int32_t szCell = pPage->xCellSize(pPage, pCell, 0, NULL, NULL);
ASSERT(pNextCell - szCell >= pCell);
pNextCell -= szCell;
if (pNextCell > pCell) {
memmove(pNextCell, pCell, szCell);
TDB_PAGE_CELL_OFFSET_AT_SET(pPage, aCellIdx[iCell].iCell, pNextCell - pPage->pData);
}
}
pPage->pFreeEnd = pNextCell;
TDB_PAGE_CCELLS_SET(pPage, pPage->pFreeEnd - pPage->pData);
TDB_PAGE_FCELL_SET(pPage, 0);
tdbOsFree(aCellIdx);
ASSERT(pPage->pFreeEnd - pPage->pFreeStart == nFree);
#else
int nFree;
int nCells;
SCell *pCell;
......@@ -457,10 +502,9 @@ static int tdbPageDefragment(SPage *pPage) {
TDB_PAGE_CELL_OFFSET_AT_SET(pPage, idx, pNextCell - pPage->pData);
}
ASSERT(pPage->pFreeEnd - pPage->pFreeStart == nFree);
TDB_PAGE_CCELLS_SET(pPage, pPage->pFreeEnd - pPage->pData);
TDB_PAGE_FCELL_SET(pPage, 0);
#endif
return 0;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册