diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index 7d42484488..845bddc8b5 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -16,7 +16,7 @@ #include "tdbInt.h" struct SBTree { - pgno_t rootPage; + pgno_t root; // TODO }; diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index 8fb54c8344..7ca4f4239d 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -125,13 +125,19 @@ int pgCacheClose(SPgCache *pPgCache) { return 0; } +#define PG_CACHE_HASH(fileid, pgno) \ + ({ \ + uint64_t *tmp = (uint64_t *)(fileid); \ + (tmp[0] + tmp[1] + tmp[2] + (pgno)); \ + }) + SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid) { SPage * pPage; SPgFile *pPgFile; SPgList *pBucket; // 1. Search the page hash table SPgCache.pght - pBucket = pPgCache->pght.buckets + ((0 /*TODO*/) % pPgCache->pght.nbucket); + pBucket = pPgCache->pght.buckets + (PG_CACHE_HASH(pgid.fileid, pgid.pgno) % pPgCache->pght.nbucket); pPage = TD_DLIST_HEAD(pBucket); while (pPage && tdbCmprPgId(&(pPage->pgid), &pgid)) { pPage = TD_DLIST_NODE_NEXT_WITH_FIELD(pPage, pghtNode); diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index 15c18b2950..2de0dd9cc5 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -76,7 +76,11 @@ SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) { memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN); pgid.pgno = pgno; - pPage = pgCacheFetch(pPgCache, pgid); + if (pgno > pPgFile->pgFileSize) { + // TODO + } else { + pPage = pgCacheFetch(pPgCache, pgid); + } return pPage; } diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 0a24331a36..e75c6da00b 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -15,59 +15,7 @@ #include "tdbInt.h" -// static int tdbOpenImpl(TDB *dbp); - -// int tdbOpen(TDB **dbpp, const char *fname, const char *dbname, uint32_t flags) { -// TDB * dbp; -// TDB_MPFILE *mpf; -// uint8_t fileid[TDB_FILE_ID_LEN]; - -// if ((dbp = (TDB *)calloc(1, sizeof(*dbp))) == NULL) { -// return -1; -// } - -// if ((dbp->fname = strdup(fname)) == NULL) { -// free(dbp); -// return -1; -// } - -// if ((dbname) && ((dbp->dbname = strdup(dbname)) == NULL)) { -// free(dbp->fname); -// free(dbp); -// return -1; -// } - -// // if (tdbGnrtFileID(fname, fileid) < 0) { -// // // todo -// // return -1; -// // } - -// // TODO: mpf = tdbGetMPFileByID(fileid); -// if (mpf == NULL) { -// // todoerr: maybe we need to create one -// return -1; -// } - -// if (tdbOpenImpl(dbp) < 0) { -// // todoerr -// return -1; -// } - -// *dbpp = dbp; -// return 0; -// } - -// int tdbClose(TDB *dbp, uint32_t flags) { -// // TODO -// return 0; -// } - -// static int tdbOpenImpl(TDB *dbp) { -// if (dbp->dbname == NULL) { -// // todo: open the DB as a master DB -// } else { -// // todo: open the DB as a sub-db -// } -// // TODO -// return 0; -// } \ No newline at end of file +struct STDb { + // TODO + SBTree *pBt; +}; diff --git a/source/libs/tdb/src/inc/btree.h b/source/libs/tdb/src/inc/btree.h index 757525d6a7..de143fa3a5 100644 --- a/source/libs/tdb/src/inc/btree.h +++ b/source/libs/tdb/src/inc/btree.h @@ -23,6 +23,12 @@ extern "C" { typedef struct SBTree SBTree; typedef struct SBtCursor SBtCursor; +// SBTree +int btreeOpen(SBTree **ppBt); +int btreeClose(SBTree *pBt); + +// SBtCursor + #ifdef __cplusplus } #endif