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

more TDB

上级 c4dd34b1
...@@ -62,10 +62,30 @@ static int tdbEncodeLength(u8 *pBuf, uint len); ...@@ -62,10 +62,30 @@ static int tdbEncodeLength(u8 *pBuf, uint len);
static int tdbBtCursorMoveToRoot(SBtCursor *pCur); static int tdbBtCursorMoveToRoot(SBtCursor *pCur);
static int tdbInitBtPage(SPage *pPage, SBtPage **ppBtPage); static int tdbInitBtPage(SPage *pPage, SBtPage **ppBtPage);
static int tdbCompareKeyAndCell(const void *pKey, int kLen, const void *pCell); 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; *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; return 0;
} }
...@@ -230,4 +250,24 @@ static int tdbInitBtPage(SPage *pPage, SBtPage **ppBtPage) { ...@@ -230,4 +250,24 @@ static int tdbInitBtPage(SPage *pPage, SBtPage **ppBtPage) {
static int tdbCompareKeyAndCell(const void *pKey, int kLen, const void *pCell) { static int tdbCompareKeyAndCell(const void *pKey, int kLen, const void *pCell) {
/* TODO */ /* TODO */
return 0; 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
...@@ -60,10 +60,12 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF ...@@ -60,10 +60,12 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF
if (ret < 0) { if (ret < 0) {
return -1; return -1;
} }
} else {
ASSERT(0);
} }
// pDb->pBt // pDb->pBt
ret = tdbBtreeOpen(&(pDb->pBt)); ret = tdbBtreeOpen(pgno, keyLen, valLen, pFile, keyCmprFn, &(pDb->pBt));
if (ret < 0) { if (ret < 0) {
return -1; return -1;
} }
...@@ -252,23 +254,4 @@ int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData ...@@ -252,23 +254,4 @@ int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData
return 0; 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 #endif
\ No newline at end of file
...@@ -34,6 +34,7 @@ int tdbPFileOpen(SPCache *pCache, const char *fileName, SPFile **ppFile) { ...@@ -34,6 +34,7 @@ int tdbPFileOpen(SPCache *pCache, const char *fileName, SPFile **ppFile) {
SPFile * pFile; SPFile * pFile;
int fsize; int fsize;
int zsize; int zsize;
int ret;
*ppFile = NULL; *ppFile = NULL;
...@@ -66,6 +67,11 @@ int tdbPFileOpen(SPCache *pCache, const char *fileName, SPFile **ppFile) { ...@@ -66,6 +67,11 @@ int tdbPFileOpen(SPCache *pCache, const char *fileName, SPFile **ppFile) {
return -1; return -1;
} }
ret = tdbGnrtFileID(pFile->dbFileName, pFile->fid, false);
if (ret < 0) {
return -1;
}
pFile->jfd = -1; pFile->jfd = -1;
*ppFile = pFile; *ppFile = pFile;
......
...@@ -23,7 +23,7 @@ extern "C" { ...@@ -23,7 +23,7 @@ extern "C" {
typedef struct SBTree SBTree; typedef struct SBTree SBTree;
typedef struct SBtCursor SBtCursor; 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 tdbBtreeClose(SBTree *pBt);
int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen); int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen);
......
...@@ -131,12 +131,12 @@ typedef int (*FKeyComparator)(const void *pKey1, int kLen1, const void *pKey2, i ...@@ -131,12 +131,12 @@ typedef int (*FKeyComparator)(const void *pKey1, int kLen1, const void *pKey2, i
#include "tdbUtil.h" #include "tdbUtil.h"
#include "tdbBtree.h"
#include "tdbPCache.h" #include "tdbPCache.h"
#include "tdbPFile.h" #include "tdbPFile.h"
#include "tdbBtree.h"
#include "tdbEnv.h" #include "tdbEnv.h"
#include "tdbDb.h" #include "tdbDb.h"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册