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

more TDB

上级 24e51adb
......@@ -40,9 +40,11 @@ static int tdbBtCursorMoveToRoot(SBtCursor *pCur);
static int tdbInitBtPage(SPage *pPage, SBtPage **ppBtPage);
static int tdbCompareKeyAndCell(const void *pKey, int kLen, const void *pCell);
static int tdbDefaultKeyCmprFn(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2);
static int tdbBtreeOpenImpl(SBTree *pBt);
int tdbBtreeOpen(SPgno rtPgno, int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, SBTree **ppBt) {
int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, SBTree **ppBt) {
SBTree *pBt;
int ret;
*ppBt = NULL;
......@@ -51,8 +53,6 @@ int tdbBtreeOpen(SPgno rtPgno, int keyLen, int valLen, SPager *pPager, FKeyCompa
return -1;
}
// pBt->root
pBt->root = rtPgno;
// pBt->keyLen
pBt->keyLen = keyLen;
// pBt->valLen
......@@ -79,6 +79,13 @@ int tdbBtreeOpen(SPgno rtPgno, int keyLen, int valLen, SPager *pPager, FKeyCompa
// pBt->minLeaf
pBt->minLeaf = pBt->minLocal;
// TODO: pBt->root
ret = tdbBtreeOpenImpl(pBt);
if (ret < 0) {
free(pBt);
return -1;
}
*ppBt = pBt;
return 0;
}
......@@ -268,4 +275,33 @@ static int tdbDefaultKeyCmprFn(const void *pKey1, int keyLen1, const void *pKey2
}
}
return cret;
}
static int tdbBtreeOpenImpl(SBTree *pBt) {
// Try to get the root page of the an existing btree
SPgno pgno;
SPage *pPage;
int ret;
{
// TODO: Search the main DB to check if the DB exists
pgno = 0;
}
if (pgno != 0) {
pBt->root = pgno;
return 0;
}
// Try to create a new database
ret = tdbPagerNewPage(pBt->pPager, &pgno, &pPage);
if (ret < 0) {
return -1;
}
ASSERT(pgno != 0);
pBt->root = pgno;
return 0;
}
\ No newline at end of file
......@@ -50,7 +50,7 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF
ASSERT(pPager != NULL);
// pDb->pBt
ret = tdbBtreeOpen(pgno, keyLen, valLen, pPager, keyCmprFn, &(pDb->pBt));
ret = tdbBtreeOpen(keyLen, valLen, pPager, keyCmprFn, &(pDb->pBt));
if (ret < 0) {
return -1;
}
......
......@@ -187,24 +187,6 @@ int tdbPagerWrite(SPager *pPager, SPage *pPage) {
return 0;
}
// int tdbPagerAllocPage(SPager *pPager, SPage **ppPage, SPgno *ppgno) {
// SPage *pPage;
// SPgno pgno;
// if (1 /*TODO: no free page*/) {
// pgno = ++pPager->dbFileSize;
// pPage = tdbPagerGet(pPager, pgno, false);
// ASSERT(pPage != NULL);
// } else {
// /* TODO: allocate from the free list */
// ASSERT(0);
// }
// *ppPage = pPage;
// *ppgno = pgno;
// return 0;
// }
int tdbPagerBegin(SPager *pPager) {
if (pPager->inTran) {
return 0;
......
......@@ -34,7 +34,7 @@ struct SBtCursor {
void * pBuf;
};
int tdbBtreeOpen(SPgno rtPgno, int keyLen, int valLen, SPager *pFile, FKeyComparator kcmpr, SBTree **ppBt);
int tdbBtreeOpen(int keyLen, int valLen, SPager *pFile, FKeyComparator kcmpr, SBTree **ppBt);
int tdbBtreeClose(SBTree *pBt);
int tdbBtreeCursor(SBtCursor *pCur, SBTree *pBt);
int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen);
......
......@@ -28,6 +28,8 @@ int tdbPagerWrite(SPager *pPager, SPage *pPage);
int tdbPagerBegin(SPager *pPager);
int tdbPagerCommit(SPager *pPager);
int tdbPagerGetPageSize(SPager *pPager);
int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage);
int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage);
#ifdef __cplusplus
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册