diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 81bc35e87c870f27d5d441686c5125f2d130a8d6..d1b5863c28e24a5c5eb03fa03ab1a0ea543262d3 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -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 diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 241c21cb9b40f40a52b62d6194a97bf8bc2cc215..3670c770abd520fdee6cd8c21d82f3c0939150db 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -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; } diff --git a/source/libs/tdb/src/db/tdbJournal.c b/source/libs/tdb/src/db/tdbJournal.c deleted file mode 100644 index 80b28a38559b878375233304faa339f69871e64e..0000000000000000000000000000000000000000 --- a/source/libs/tdb/src/db/tdbJournal.c +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#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 diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 06674496a9154182057d8cc69b318a90d4de7488..ac42e1500293f1c74fec9d6d89c253d772198fc5 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -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 diff --git a/source/libs/tdb/src/inc/tdbJournal.h b/source/libs/tdb/src/inc/tdbJournal.h deleted file mode 100644 index 918b2fa06de6fa25ef167c1babfb9162d29c13d6..0000000000000000000000000000000000000000 --- a/source/libs/tdb/src/inc/tdbJournal.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#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