diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 0059fba191..e79313d1ca 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -65,6 +65,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { SBTree * pBt; bool fileExist; size_t dbNameLen; + pgno_t dbRootPgno; char dbfname[128]; // TODO: make this as a macro or malloc on the heap ASSERT(pDb != NULL); @@ -104,10 +105,19 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { } // TODO: open the database (an existing or a new one) - // Search the page file master DB to check if the db exists - // If DB exists, get the root page number - // If DB not exists, create a new DB + if (0) { + // Search the page file master DB to check if the db exists + // If exists, run this branch (TODO) + } else { + ret = pgFileAllocatePage(pPgFile, &dbRootPgno); + if (ret != 0) { + // TODO: handle error + } + } + pDb->pBt->root = dbRootPgno; + + // register pDb->pPgFile = pPgFile; tdbEnvRgstDB(pEnv, pDb); pDb->pEnv = pEnv; diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index b993b68635..c6b7611818 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -110,6 +110,19 @@ int pgFileWrite(SPage *pPage) { return 0; } +int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) { + pgno_t pgno; + + if (0) { + // TODO: allocate from the free list + } else { + pgno = ++pPgFile->dbNewSize; + } + + *pPgno = pgno; + return 0; +} + static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) { pgsz_t pgSize; ssize_t rsize; diff --git a/source/libs/tdb/src/inc/tdbPgFile.h b/source/libs/tdb/src/inc/tdbPgFile.h index 97f3fe289d..d4c97e0e0d 100644 --- a/source/libs/tdb/src/inc/tdbPgFile.h +++ b/source/libs/tdb/src/inc/tdbPgFile.h @@ -46,10 +46,12 @@ struct SPgFile { int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv); int pgFileClose(SPgFile *pPgFile); + SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno); int pgFileRelease(SPage *pPage); int pgFileWrite(SPage *pPage); +int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno); #ifdef __cplusplus }