more TDB
This commit is contained in:
parent
7d5030bc7f
commit
7a5985ae46
|
@ -91,8 +91,8 @@ void tdbPageInit(SPage *pPage) {
|
||||||
|
|
||||||
int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell) {
|
int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell) {
|
||||||
int nFree;
|
int nFree;
|
||||||
int ret;
|
|
||||||
int nCells;
|
int nCells;
|
||||||
|
int iOvfl;
|
||||||
int lidx; // local idx
|
int lidx; // local idx
|
||||||
SCell *pNewCell;
|
SCell *pNewCell;
|
||||||
|
|
||||||
|
@ -100,32 +100,45 @@ int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell) {
|
||||||
|
|
||||||
nFree = TDB_PAGE_NFREE(pPage);
|
nFree = TDB_PAGE_NFREE(pPage);
|
||||||
nCells = TDB_PAGE_NCELLS(pPage);
|
nCells = TDB_PAGE_NCELLS(pPage);
|
||||||
|
iOvfl = 0;
|
||||||
|
|
||||||
|
for (; iOvfl < pPage->nOverflow; iOvfl++) {
|
||||||
|
if (pPage->aiOvfl[iOvfl] >= idx) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lidx = idx - iOvfl;
|
||||||
|
|
||||||
if (nFree >= szCell + TDB_PAGE_OFFSET_SIZE(pPage)) {
|
if (nFree >= szCell + TDB_PAGE_OFFSET_SIZE(pPage)) {
|
||||||
// page must has enough space to hold the cell locally
|
// page must has enough space to hold the cell locally
|
||||||
ret = tdbPageAllocate(pPage, szCell, &pNewCell);
|
tdbPageAllocate(pPage, szCell, &pNewCell);
|
||||||
ASSERT(ret == 0);
|
|
||||||
|
|
||||||
memcpy(pNewCell, pCell, szCell);
|
memcpy(pNewCell, pCell, szCell);
|
||||||
|
|
||||||
if (pPage->nOverflow == 0) {
|
|
||||||
lidx = idx;
|
|
||||||
} else {
|
|
||||||
// TODO
|
|
||||||
// lidx = ;
|
|
||||||
}
|
|
||||||
|
|
||||||
// no overflow cell exists in this page
|
// no overflow cell exists in this page
|
||||||
u8 *src = pPage->pCellIdx + TDB_PAGE_OFFSET_SIZE(pPage) * lidx;
|
u8 *src = pPage->pCellIdx + TDB_PAGE_OFFSET_SIZE(pPage) * lidx;
|
||||||
u8 *dest = src + TDB_PAGE_OFFSET_SIZE(pPage);
|
u8 *dest = src + TDB_PAGE_OFFSET_SIZE(pPage);
|
||||||
memmove(dest, src, pPage->pFreeStart - dest);
|
memmove(dest, src, pPage->pFreeStart - dest);
|
||||||
TDB_PAGE_CELL_OFFSET_AT_SET(pPage, lidx, TDB_PAGE_OFFSET_SIZE(pPage) * lidx);
|
TDB_PAGE_CELL_OFFSET_AT_SET(pPage, lidx, TDB_PAGE_OFFSET_SIZE(pPage) * lidx);
|
||||||
TDB_PAGE_NCELLS_SET(pPage, nCells + 1);
|
TDB_PAGE_NCELLS_SET(pPage, nCells + 1);
|
||||||
|
|
||||||
|
ASSERT(pPage->pFreeStart == pPage->pCellIdx + TDB_PAGE_OFFSET_SIZE(pPage) * (nCells + 1));
|
||||||
} else {
|
} else {
|
||||||
// TODO: page not has enough space
|
// add the cell as an overflow cell
|
||||||
pPage->apOvfl[pPage->nOverflow] = pCell;
|
for (int i = pPage->nOverflow; i > iOvfl; i--) {
|
||||||
pPage->aiOvfl[pPage->nOverflow] = idx;
|
pPage->apOvfl[i] = pPage->apOvfl[i - 1];
|
||||||
|
pPage->aiOvfl[i] = pPage->aiOvfl[i - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
pPage->apOvfl[iOvfl] = pCell;
|
||||||
|
pPage->aiOvfl[iOvfl] = idx;
|
||||||
pPage->nOverflow++;
|
pPage->nOverflow++;
|
||||||
|
iOvfl++;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; iOvfl < pPage->nOverflow; iOvfl++) {
|
||||||
|
pPage->aiOvfl[iOvfl]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue