提交 9e34770d 编写于 作者: H Hongze Cheng

more TDB

上级 87dfa6ce
......@@ -104,17 +104,19 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) {
}
}
// TODO: open the database (an existing or a new one)
if (0) {
// Search the page file master DB to check if the db exists
// If exists, run this branch (TODO)
} else {
// TODO: get the root page number from the master DB of the page file
// tdbGet(&dbRootPgno);
if (dbRootPgno == 0) {
// DB not exist, create one
ret = pgFileAllocatePage(pPgFile, &dbRootPgno);
if (ret != 0) {
// TODO: handle error
}
// tdbInsert(pPgFile->pMasterDB, dbname, strlen(dbname), &dbRootPgno, sizeof(dbRootPgno));
}
ASSERT(dbRootPgno > 1);
// pDb->pBt->root = dbRootPgno;
// register
......
......@@ -17,6 +17,8 @@
struct STDbEnv {
char * rootDir; // root directory of the environment
char * jname; // journal file name
int jfd; // journal file fd
pgsz_t pgSize; // page size
cachesz_t cacheSize; // total cache size
STDbList dbList; // TDB List
......@@ -26,7 +28,6 @@ struct STDbEnv {
#define TDB_ENV_PGF_HASH_BUCKETS 17
SPgFileList buckets[TDB_ENV_PGF_HASH_BUCKETS];
} pgfht; // page file hash table;
SJournal *pJournal;
};
#define TDB_ENV_PGF_HASH(fileid) \
......@@ -40,22 +41,27 @@ static int tdbEnvDestroy(TENV *pEnv);
int tdbEnvCreate(TENV **ppEnv, const char *rootDir) {
TENV * pEnv;
size_t slen;
size_t jlen;
ASSERT(rootDir != NULL);
*ppEnv = NULL;
slen = strlen(rootDir);
pEnv = (TENV *)calloc(1, sizeof(*pEnv) + sizeof(SJournal) + slen + 1);
jlen = slen + strlen(TDB_JOURNAL_NAME) + 1;
pEnv = (TENV *)calloc(1, sizeof(*pEnv) + slen + 1 + jlen + 1);
if (pEnv == NULL) {
return -1;
}
pEnv->rootDir = (char *)(&pEnv[1]) + sizeof(SJournal);
pEnv->rootDir = (char *)(&pEnv[1]);
pEnv->jname = pEnv->rootDir + slen + 1;
pEnv->jfd = -1;
pEnv->pgSize = TDB_DEFAULT_PGSIZE;
pEnv->cacheSize = TDB_DEFAULT_CACHE_SIZE;
memcpy(pEnv->rootDir, rootDir, slen);
pEnv->rootDir[slen] = '\0';
sprintf(pEnv->jname, "%s/%s", rootDir, TDB_JOURNAL_NAME);
TD_DLIST_INIT(&(pEnv->dbList));
TD_DLIST_INIT(&(pEnv->pgfList));
......@@ -133,30 +139,18 @@ static int tdbEnvDestroy(TENV *pEnv) {
}
int tdbEnvBeginTxn(TENV *pEnv) {
SJournal *pJournal;
int ret;
ASSERT(pEnv->pJournal == NULL);
pJournal = (SJournal *)(&(pEnv[1]));
ret = tdbOpenJournal(pJournal);
if (ret < 0) {
// TODO: handle error
pEnv->jfd = open(pEnv->jname, O_CREAT | O_RDWR, 0755);
if (pEnv->jfd < 0) {
return -1;
}
pEnv->pJournal = pJournal;
return 0;
}
int tdbEnvCommit(TENV *pEnv) {
SJournal *pJournal;
ASSERT(pEnv->pJournal != NULL);
pJournal = pEnv->pJournal;
tdbCloseJournal(pJournal);
/* TODO */
close(pEnv->jfd);
pEnv->jfd = -1;
return 0;
}
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tdbInt.h"
int tdbOpenJournal(SJournal *pJournal) {
TENV *pEnv;
char *jname;
pJournal->fd = open(jname, O_CREAT | O_RDWR, 0755);
if (pJournal->fd < 0) {
// TODO: handle error
return -1;
}
return 0;
}
int tdbCloseJournal(SJournal *pJournal) {
// TODO
return 0;
}
\ No newline at end of file
......@@ -115,6 +115,8 @@ typedef TD_DLIST_NODE(SPgFile) SPgFileListNode;
/* TODO */ \
} while (0)
#define TDB_JOURNAL_NAME "tdb.journal"
#include "tdbUtil.h"
#include "tdbBtree.h"
......@@ -123,8 +125,6 @@ typedef TD_DLIST_NODE(SPgFile) SPgFileListNode;
#include "tdbPgFile.h"
#include "tdbJournal.h"
#include "tdbEnv.h"
#ifdef __cplusplus
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _TDB_JOURNAL_H_
#define _TDB_JOURNAL_H_
#ifdef __cplusplus
extern "C" {
#endif
typedef struct SJournal SJournal;
struct SJournal {
char jname[64];
int fd;
};
int tdbOpenJournal(SJournal *pJournal);
int tdbCloseJournal(SJournal *pJournal);
#ifdef __cplusplus
}
#endif
#endif /*_TDB_JOURNAL_H_*/
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册