提交 976cf60b 编写于 作者: H Hongze Cheng

more TDB

上级 f9c09351
...@@ -8,6 +8,7 @@ target_sources(tdb ...@@ -8,6 +8,7 @@ target_sources(tdb
"src/db/tdbBtree.c" "src/db/tdbBtree.c"
"src/db/tdbDb.c" "src/db/tdbDb.c"
"src/db/tdbEnv.c" "src/db/tdbEnv.c"
"src/db/tdbPage.c"
) )
target_include_directories( target_include_directories(
......
...@@ -405,6 +405,16 @@ static int tdbBtreeInitPage(SPage *pPage, void *arg) { ...@@ -405,6 +405,16 @@ static int tdbBtreeInitPage(SPage *pPage, void *arg) {
} }
#ifndef TDB_BTREE_BALANCE #ifndef TDB_BTREE_BALANCE
typedef struct {
SBTree *pBt;
SPage *pParent;
int idx;
i8 nOldPages;
SPage *pOldPages[3];
i8 nNewPages;
SPage *pNewPages[5];
} SBtreeBalanceHelper;
static int tdbBtreeCopyPageContent(SPage *pFrom, SPage *pTo) { static int tdbBtreeCopyPageContent(SPage *pFrom, SPage *pTo) {
/* TODO */ /* TODO */
...@@ -450,92 +460,138 @@ static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild) { ...@@ -450,92 +460,138 @@ static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild) {
return 0; return 0;
} }
static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { static int tdbBtreeBalanceStep1(SBtreeBalanceHelper *pBlh) {
int nOldPages; #if 0
SPage *pOldPages[3]; // TODO: Find three or less sibling pages on either side
int nNewPages; i = pParent->pPageHdr->nCells + pParent->nOverflow;
SPage *pNewPages[5]; if (i < 1) {
void *pCell; nDiv = 0;
SPgno pgno; } else {
int i; if (idx == 0) {
int nDiv;
int ret;
SPage *pPage;
void *pCellDiv[2];
{
// TODO: Find three or less sibling pages on either side
i = pParent->pPageHdr->nCells + pParent->nOverflow;
if (i < 1) {
nDiv = 0; nDiv = 0;
} else if (idx == i) {
nDiv = i - 2;
} else { } else {
if (idx == 0) { nDiv = idx - 1;
nDiv = 0;
} else if (idx == i) {
nDiv = i - 2;
} else {
nDiv = idx - 1;
}
i = 2;
} }
nOldPages = i + 1; i = 2;
}
nOldPages = i + 1;
if (i + nDiv - pParent->nOverflow == pParent->pPageHdr->nCells) { if (i + nDiv - pParent->nOverflow == pParent->pPageHdr->nCells) {
pgno = pParent->pPageHdr->rChild; pgno = pParent->pPageHdr->rChild;
} else { } else {
ASSERT(0);
// TODO
pgno = 0;
}
for (;;) {
ret = tdbPagerFetchPage(pBt->pPager, pgno, &pPage, tdbBtreeInitPage, pBt);
if (ret < 0) {
ASSERT(0); ASSERT(0);
// TODO return -1;
pgno = 0;
} }
for (;;) {
ret = tdbPagerFetchPage(pBt->pPager, pgno, &pPage, tdbBtreeInitPage, pBt);
if (ret < 0) {
ASSERT(0);
return -1;
}
pOldPages[i] = pPage; pOldPages[i] = pPage;
if ((i--) == 0) break; if ((i--) == 0) break;
if (pParent->nOverflow && i + nDiv == pParent->aiOvfl[0]) { if (pParent->nOverflow && i + nDiv == pParent->aiOvfl[0]) {
pCellDiv[i] = pParent->apOvfl[0]; pCellDiv[i] = pParent->apOvfl[0];
// pgno = 0; // pgno = 0;
// szNew[i] = tdbPageCellSize(pPage, pCell); // szNew[i] = tdbPageCellSize(pPage, pCell);
pParent->nOverflow = 0; pParent->nOverflow = 0;
} else { } else {
// pCellDiv[i] = TDB_PAGE_CELL_AT(pPage, i + nDiv - pParent->nOverflow); // pCellDiv[i] = TDB_PAGE_CELL_AT(pPage, i + nDiv - pParent->nOverflow);
// pgno = 0; // pgno = 0;
// szNew[i] = tdbPageCellSize(pPage, pCell); // szNew[i] = tdbPageCellSize(pPage, pCell);
// Drop the cell from the page // Drop the cell from the page
// ret = tdbPageDropCell(pPage, i + nDiv - pParent->nOverflow, szNew[i]); // ret = tdbPageDropCell(pPage, i + nDiv - pParent->nOverflow, szNew[i]);
// if (ret < 0) { // if (ret < 0) {
// return -1; // return -1;
// } // }
}
/* code */
} }
/* code */
} }
{ #endif
// TODO: Load all cells on the old page and the divider cells return 0;
}
static int tdbBtreeBalanceStep2(SBtreeBalanceHelper *pBlh) {
// TODO
return 0;
}
static int tdbBtreeBalanceStep3(SBtreeBalanceHelper *pBlh) {
// TODO
return 0;
}
static int tdbBtreeBalanceStep4(SBtreeBalanceHelper *pBlh) {
// TODO
return 0;
}
static int tdbBtreeBalanceStep5(SBtreeBalanceHelper *pBlh) {
// TODO
return 0;
}
static int tdbBtreeBalanceStep6(SBtreeBalanceHelper *pBlh) {
// TODO
return 0;
}
static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) {
int ret;
SBtreeBalanceHelper blh;
blh.pBt = pBt;
blh.pParent = pParent;
blh.idx = idx;
// Step 1: find two sibling pages and get engough info about the old pages
ret = tdbBtreeBalanceStep1(&blh);
if (ret < 0) {
ASSERT(0);
return -1;
} }
{ // Step 2: Load all cells on the old page and the divider cells
// TODO: Get the number of pages needed to hold all cells ret = tdbBtreeBalanceStep2(&blh);
if (ret < 0) {
ASSERT(0);
return -1;
} }
{ // Step 3: Get the number of pages needed to hold all cells
// TODO: Allocate enough new pages. Reuse old pages as much as possible ret = tdbBtreeBalanceStep3(&blh);
if (ret < 0) {
ASSERT(0);
return -1;
} }
{ // Step 4: Allocate enough new pages. Reuse old pages as much as possible
// TODO: Insert new divider cells into pParent ret = tdbBtreeBalanceStep4(&blh);
if (ret < 0) {
ASSERT(0);
return -1;
} }
{ // Step 5: Insert new divider cells into pParent
// TODO: Update the sibling pages ret = tdbBtreeBalanceStep5(&blh);
if (ret < 0) {
ASSERT(0);
return -1;
}
// Step 6: Update the sibling pages
ret = tdbBtreeBalanceStep6(&blh);
if (ret < 0) {
ASSERT(0);
return -1;
} }
{ {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册