diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 11c293af74ccba2ccab4739e85e30492e028d525..65b79ea588e893eb69a58319a34e848b27f6cb1d 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 d8978b7608065d633d4e4f8428e729cc0f3acabd..a9670e24dee6be6f04fbc3614c5b1700154b022e 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 42ea7769e7208fecd5895cec6a9825c4a2de8624..31047807bf83a3c2cb88dc15f0a4f2c55c4ce228 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 0961ac1664bd16a8b968480c551ec4f0a8c96a98..745a336e8925b53eda75535dfcffabbb27eddebb 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 8f4e32bcd90066e6cd56782fd31a9a65f1525b77..1e66e6ca403910d394f205edd1f06454d85d8f9e 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"