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

more TDB

上级 4ecbe41d
......@@ -54,13 +54,13 @@ int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, TENV **ppEnv) {
return -1;
}
pEnv->nHash = 8;
tsize = sizeof(SPager *) * pEnv->nHash;
pEnv->pagerHash = TDB_REALLOC(pEnv->pagerHash, tsize);
if (pEnv->pagerHash == NULL) {
pEnv->nPgrHash = 8;
tsize = sizeof(SPager *) * pEnv->nPgrHash;
pEnv->pgrHash = TDB_REALLOC(pEnv->pgrHash, tsize);
if (pEnv->pgrHash == NULL) {
return -1;
}
memset(pEnv->pagerHash, 0, tsize);
memset(pEnv->pgrHash, 0, tsize);
mkdir(rootDir, 0755);
......@@ -81,7 +81,7 @@ int tdbBegin(TENV *pEnv) {
int tdbCommit(TENV *pEnv) {
SPager *pPager;
pPager = pEnv->pagerList;
pPager = pEnv->pgrList;
while (pPager) {
tdbPagerCommit(pPager);
}
......@@ -95,10 +95,63 @@ int tdbRollback(TENV *pEnv) {
}
SPager *tdbEnvGetPager(TENV *pEnv, const char *fname) {
// TODO
return NULL;
int hash;
SPager **ppPager;
hash = tdbCstringHash(fname);
ppPager = &pEnv->pgrHash[hash % pEnv->nPgrHash];
for (; *ppPager && (strcmp(fname, (*ppPager)->dbFileName) != 0); ppPager = &((*ppPager)->pHashNext)) {
}
return *ppPager;
}
static void tdbEnvAddPager(TENV *pEnv, SPager *pPager) {
void tdbEnvAddPager(TENV *pEnv, SPager *pPager) {
int hash;
SPager **ppPager;
// rehash if neccessary
if (pEnv->nPager + 1 > pEnv->nPgrHash) {
// TODO
}
// add to list
pPager->pNext = pEnv->pgrList;
pEnv->pgrList = pPager;
// add to hash
hash = tdbCstringHash(pPager->dbFileName);
ppPager = &pEnv->pgrHash[hash % pEnv->nPgrHash];
pPager->pHashNext = *ppPager;
*ppPager = pPager;
// increase the counter
pEnv->nPager++;
}
void tdbEnvRemovePager(TENV *pEnv, SPager *pPager) {
int hash;
SPager **ppPager;
// remove from the list
for (ppPager = &pEnv->pgrList; *ppPager && (*ppPager != pPager); ppPager = &((*ppPager)->pNext)) {
}
ASSERT(*ppPager == pPager);
*ppPager = pPager->pNext;
// remove from hash
hash = tdbCstringHash(pPager->dbFileName);
ppPager = &pEnv->pgrHash[hash % pEnv->nPgrHash];
for (; *ppPager && *ppPager != pPager; ppPager = &((*ppPager)->pHashNext)) {
}
ASSERT(*ppPager == pPager);
*ppPager = pPager->pNext;
// decrease the counter
pEnv->nPager--;
// rehash if necessary
if (pEnv->nPgrHash > 8 && pEnv->nPager < pEnv->nPgrHash / 2) {
// TODO
}
}
\ No newline at end of file
......@@ -20,18 +20,15 @@
extern "C" {
#endif
#define TDB_PAGER_ENV_FIELDS \
SPager *pNext; \
SPager *pHashNext;
typedef struct STEnv {
char *rootDir;
char *jfname;
int jfd;
SPCache *pCache;
SPager *pagerList;
int nHash;
SPager **pagerHash;
SPager *pgrList;
int nPager;
int nPgrHash;
SPager **pgrHash;
} TENV;
int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, TENV **ppEnv);
......@@ -40,6 +37,8 @@ int tdbBegin(TENV *pEnv);
int tdbCommit(TENV *pEnv);
int tdbRollback(TENV *pEnv);
void tdbEnvAddPager(TENV *pEnv, SPager *pPager);
void tdbEnvRemovePager(TENV *pEnv, SPager *pPager);
SPager *tdbEnvGetPager(TENV *pEnv, const char *fname);
#ifdef __cplusplus
......
......@@ -32,6 +32,8 @@ struct SPager {
SPgno dbOrigSize;
SPage *pDirty;
u8 inTran;
SPager *pNext; // used by TENV
SPager *pHashNext; // used by TENV
};
int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager);
......
......@@ -101,6 +101,11 @@ static inline int tdbGetVarInt(const u8 *p, int *v) {
return n;
}
static inline int tdbCstringHash(const char *s) {
// TODO
return 0;
}
#ifdef __cplusplus
}
#endif
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册