From bc36f14e1d84cdf0820d49dfdc6b3b7e77cb9d10 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 28 Feb 2022 07:56:26 +0000 Subject: [PATCH] more TDB --- source/libs/tdb/src/db/tdbBtree.c | 44 ++++++++++++++++++++++++++++-- source/libs/tdb/src/db/tdbDb.c | 23 ++-------------- source/libs/tdb/src/db/tdbPFile.c | 6 ++++ source/libs/tdb/src/inc/tdbBtree.h | 2 +- source/libs/tdb/src/inc/tdbInt.h | 4 +-- 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 11c293af74..65b79ea588 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -62,10 +62,30 @@ static int tdbEncodeLength(u8 *pBuf, uint len); 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); + +int tdbBtreeOpen(SPgno rtPgno, int keyLen, int valLen, SPFile *pFile, FKeyComparator kcmpr, SBTree **ppBt) { + SBTree *pBt; -int tdbBtreeOpen(SBTree **ppBt) { *ppBt = NULL; - /* TODO */ + + pBt = (SBTree *)calloc(1, sizeof(*pBt)); + if (pBt == NULL) { + return -1; + } + + // pBt->root + pBt->root = rtPgno; + // pBt->keyLen + pBt->keyLen = keyLen; + // pBt->valLen + pBt->valLen = valLen; + // pBt->pFile + pBt->pFile = pFile; + // pBt->kcmpr + pBt->kcmpr = kcmpr ? kcmpr : tdbDefaultKeyCmprFn; + + *ppBt = pBt; return 0; } @@ -230,4 +250,24 @@ static int tdbInitBtPage(SPage *pPage, SBtPage **ppBtPage) { static int tdbCompareKeyAndCell(const void *pKey, int kLen, const void *pCell) { /* TODO */ return 0; +} + +static int tdbDefaultKeyCmprFn(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2) { + int mlen; + int cret; + + ASSERT(keyLen1 > 0 && keyLen2 > 0 && pKey1 != NULL && pKey2 != NULL); + + mlen = keyLen1 < keyLen2 ? keyLen1 : keyLen2; + cret = memcmp(pKey1, pKey2, mlen); + if (cret == 0) { + if (keyLen1 < keyLen2) { + cret = -1; + } else if (keyLen1 > keyLen2) { + cret = 1; + } else { + cret = 0; + } + } + return cret; } \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdbDb.c b/source/libs/tdb/src/db/tdbDb.c index d8978b7608..a9670e24de 100644 --- a/source/libs/tdb/src/db/tdbDb.c +++ b/source/libs/tdb/src/db/tdbDb.c @@ -60,10 +60,12 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF if (ret < 0) { return -1; } + } else { + ASSERT(0); } // pDb->pBt - ret = tdbBtreeOpen(&(pDb->pBt)); + ret = tdbBtreeOpen(pgno, keyLen, valLen, pFile, keyCmprFn, &(pDb->pBt)); if (ret < 0) { return -1; } @@ -252,23 +254,4 @@ int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData return 0; } -static int tdbDefaultKeyCmprFn(int keyLen1, const void *pKey1, int keyLen2, const void *pKey2) { - int mlen; - int cret; - - ASSERT(keyLen1 > 0 && keyLen2 > 0 && pKey1 != NULL && pKey2 != NULL); - - mlen = keyLen1 < keyLen2 ? keyLen1 : keyLen2; - cret = memcmp(pKey1, pKey2, mlen); - if (cret == 0) { - if (keyLen1 < keyLen2) { - cret = -1; - } else if (keyLen1 > keyLen2) { - cret = 1; - } else { - cret = 0; - } - } - return cret; -} #endif \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdbPFile.c b/source/libs/tdb/src/db/tdbPFile.c index 42ea7769e7..31047807bf 100644 --- a/source/libs/tdb/src/db/tdbPFile.c +++ b/source/libs/tdb/src/db/tdbPFile.c @@ -34,6 +34,7 @@ int tdbPFileOpen(SPCache *pCache, const char *fileName, SPFile **ppFile) { SPFile * pFile; int fsize; int zsize; + int ret; *ppFile = NULL; @@ -66,6 +67,11 @@ int tdbPFileOpen(SPCache *pCache, const char *fileName, SPFile **ppFile) { return -1; } + ret = tdbGnrtFileID(pFile->dbFileName, pFile->fid, false); + if (ret < 0) { + return -1; + } + pFile->jfd = -1; *ppFile = pFile; diff --git a/source/libs/tdb/src/inc/tdbBtree.h b/source/libs/tdb/src/inc/tdbBtree.h index 0961ac1664..745a336e89 100644 --- a/source/libs/tdb/src/inc/tdbBtree.h +++ b/source/libs/tdb/src/inc/tdbBtree.h @@ -23,7 +23,7 @@ extern "C" { typedef struct SBTree SBTree; typedef struct SBtCursor SBtCursor; -int tdbBtreeOpen(SBTree **ppBt); +int tdbBtreeOpen(SPgno rtPgno, int keyLen, int valLen, SPFile *pFile, FKeyComparator kcmpr, SBTree **ppBt); int tdbBtreeClose(SBTree *pBt); int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen); diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 8f4e32bcd9..1e66e6ca40 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -131,12 +131,12 @@ typedef int (*FKeyComparator)(const void *pKey1, int kLen1, const void *pKey2, i #include "tdbUtil.h" -#include "tdbBtree.h" - #include "tdbPCache.h" #include "tdbPFile.h" +#include "tdbBtree.h" + #include "tdbEnv.h" #include "tdbDb.h" -- GitLab