From 0c4873381ace5cb0d6b3a1f7af2d8c3224a8f762 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 28 Feb 2022 05:34:27 +0000 Subject: [PATCH] refact TDB --- source/libs/tdb/CMakeLists.txt | 11 +- source/libs/tdb/inc/tdb.h | 58 ++++---- source/libs/tdb/src/db/{tdb.c => tdbDb.c} | 25 +++- source/libs/tdb/src/db/tdbEnv.c | 161 +--------------------- source/libs/tdb/src/inc/tdbDb.h | 33 +++++ source/libs/tdb/src/inc/tdbInt.h | 4 +- source/libs/tdb/test/tdbTest.cpp | 76 +++------- 7 files changed, 109 insertions(+), 259 deletions(-) rename source/libs/tdb/src/db/{tdb.c => tdbDb.c} (93%) create mode 100644 source/libs/tdb/src/inc/tdbDb.h diff --git a/source/libs/tdb/CMakeLists.txt b/source/libs/tdb/CMakeLists.txt index c55847aa4d..bb655f5767 100644 --- a/source/libs/tdb/CMakeLists.txt +++ b/source/libs/tdb/CMakeLists.txt @@ -1,9 +1,4 @@ -# for tdb -# set(TDB_SUBDIRS "db") -# foreach(TDB_SUBDIR ${TDB_SUBDIRS}) -# aux_source_directory("src/${TDB_SUBDIR}" TDB_SRC) -# endforeach() - +# tdb add_library(tdb "") target_sources(tdb PRIVATE @@ -11,7 +6,7 @@ target_sources(tdb "src/db/tdbPFile.c" "src/db/tdbUtil.c" "src/db/tdbBtree.c" - "src/db/tdb.c" + "src/db/tdbDb.c" "src/db/tdbEnv.c" ) @@ -38,5 +33,5 @@ target_include_directories(tdb_sqlite PUBLIC "src/sqliteinc") # for test if(${BUILD_TEST}) - # add_subdirectory(test) + add_subdirectory(test) endif(${BUILD_TEST}) diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index cc0d20ef3c..467e40325e 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -22,44 +22,42 @@ extern "C" { #endif -typedef struct STDb TDB; -typedef struct STDbEnv TENV; -typedef struct STDbCurosr TDBC; +// typedef struct STDb TDB; +// typedef struct STDbEnv TENV; +// typedef struct STDbCurosr TDBC; -typedef int32_t pgsz_t; -typedef int32_t cachesz_t; +// typedef int32_t pgsz_t; +// typedef int32_t cachesz_t; -typedef int (*TdbKeyCmprFn)(int keyLen1, const void *pKey1, int keyLen2, const void *pKey2); +// typedef int (*TdbKeyCmprFn)(int keyLen1, const void *pKey1, int keyLen2, const void *pKey2); -// TEVN -int tdbEnvCreate(TENV **ppEnv, const char *rootDir); -int tdbEnvOpen(TENV *ppEnv); -int tdbEnvClose(TENV *pEnv); +// // TEVN +// int tdbEnvCreate(TENV **ppEnv, const char *rootDir); +// int tdbEnvOpen(TENV *ppEnv); +// int tdbEnvClose(TENV *pEnv); -int tdbEnvSetCache(TENV *pEnv, pgsz_t pgSize, cachesz_t cacheSize); -pgsz_t tdbEnvGetPageSize(TENV *pEnv); -cachesz_t tdbEnvGetCacheSize(TENV *pEnv); +// int tdbEnvSetCache(TENV *pEnv, pgsz_t pgSize, cachesz_t cacheSize); +// pgsz_t tdbEnvGetPageSize(TENV *pEnv); +// cachesz_t tdbEnvGetCacheSize(TENV *pEnv); -int tdbEnvBeginTxn(TENV *pEnv); -int tdbEnvCommit(TENV *pEnv); +// int tdbEnvBeginTxn(TENV *pEnv); +// int tdbEnvCommit(TENV *pEnv); -// TDB -int tdbCreate(TDB **ppDb); -int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv); -int tdbClose(TDB *pDb); -int tdbDrop(TDB *pDb); +// // TDB +// int tdbCreate(TDB **ppDb); +// int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv); +// int tdbClose(TDB *pDb); +// int tdbDrop(TDB *pDb); -int tdbSetKeyLen(TDB *pDb, int klen); -int tdbSetValLen(TDB *pDb, int vlen); -int tdbSetDup(TDB *pDb, int dup); -int tdbSetCmprFunc(TDB *pDb, TdbKeyCmprFn fn); -int tdbGetKeyLen(TDB *pDb); -int tdbGetValLen(TDB *pDb); -int tdbGetDup(TDB *pDb); +// int tdbSetKeyLen(TDB *pDb, int klen); +// int tdbSetValLen(TDB *pDb, int vlen); +// int tdbSetDup(TDB *pDb, int dup); +// int tdbSetCmprFunc(TDB *pDb, TdbKeyCmprFn fn); +// int tdbGetKeyLen(TDB *pDb); +// int tdbGetValLen(TDB *pDb); +// int tdbGetDup(TDB *pDb); -int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData); - -// TDBC +// int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData); #ifdef __cplusplus } diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdbDb.c similarity index 93% rename from source/libs/tdb/src/db/tdb.c rename to source/libs/tdb/src/db/tdbDb.c index b82ed66017..a85ff46e57 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdbDb.c @@ -15,6 +15,29 @@ #include "tdbInt.h" +struct STDb { + STEnv *pEnv; + /* TODO */ +}; + +int tdbDbOpen(STDb **ppDb) { + STDb *pDb; + + *ppDb = NULL; + /* TODO */ + return 0; +} + +int tdbDbClose(STDb *pDb) { + // TODO + return 0; +} + +int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int valLen) { + // TODO + return 0; +} + #if 0 struct STDb { char dbname[TDB_MAX_DBNAME_LEN]; @@ -112,7 +135,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { ret = pgFileAllocatePage(pPgFile, &dbRootPgno); if (ret != 0) { // TODO: handle error - } + // tdbInsert(pPgFile->pMasterDB, dbname, strlen(dbname), &dbRootPgno, sizeof(dbRootPgno)); } diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index c92ff0c320..ca36830b16 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -54,163 +54,4 @@ int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, STEnv **ppEnv) int tdbEnvClose(STEnv *pEnv) { // TODO return 0; -} - -#if 0 -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 - SPgFileList pgfList; // SPgFile List - SPgCache * pPgCache; // page cache - struct { -#define TDB_ENV_PGF_HASH_BUCKETS 17 - SPgFileList buckets[TDB_ENV_PGF_HASH_BUCKETS]; - } pgfht; // page file hash table; -}; - -#define TDB_ENV_PGF_HASH(fileid) \ - ({ \ - uint8_t *tmp = (uint8_t *)(fileid); \ - tmp[0] + tmp[1] + tmp[2]; \ - }) - -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); - 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]); - 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)); - - /* TODO */ - - *ppEnv = pEnv; - return 0; -} - -int tdbEnvOpen(TENV *pEnv) { - SPgCache *pPgCache; - int ret; - - ASSERT(pEnv != NULL); - - /* TODO: here we do not need to create the root directory, more - * work should be done here - */ - mkdir(pEnv->rootDir, 0755); - - ret = pgCacheOpen(&pPgCache, pEnv); - if (ret != 0) { - goto _err; - } - - pEnv->pPgCache = pPgCache; - return 0; - -_err: - return -1; -} - -int tdbEnvClose(TENV *pEnv) { - if (pEnv == NULL) return 0; - pgCacheClose(pEnv->pPgCache); - tdbEnvDestroy(pEnv); - return 0; -} - -int tdbEnvSetCache(TENV *pEnv, pgsz_t pgSize, cachesz_t cacheSize) { - if (!TDB_IS_PGSIZE_VLD(pgSize) || cacheSize / pgSize < 10) { - return -1; - } - - /* TODO */ - - pEnv->pgSize = pgSize; - pEnv->cacheSize = cacheSize; - - return 0; -} - -pgsz_t tdbEnvGetPageSize(TENV *pEnv) { return pEnv->pgSize; } - -cachesz_t tdbEnvGetCacheSize(TENV *pEnv) { return pEnv->cacheSize; } - -SPgFile *tdbEnvGetPageFile(TENV *pEnv, const uint8_t fileid[]) { - SPgFileList *pBucket; - SPgFile * pPgFile; - - pBucket = pEnv->pgfht.buckets + (TDB_ENV_PGF_HASH(fileid) % TDB_ENV_PGF_HASH_BUCKETS); // TODO - for (pPgFile = TD_DLIST_HEAD(pBucket); pPgFile != NULL; pPgFile = TD_DLIST_NODE_NEXT_WITH_FIELD(pPgFile, envHash)) { - if (memcmp(fileid, pPgFile->fileid, TDB_FILE_ID_LEN) == 0) break; - }; - - return pPgFile; -} - -SPgCache *tdbEnvGetPgCache(TENV *pEnv) { return pEnv->pPgCache; } - -static int tdbEnvDestroy(TENV *pEnv) { - // TODO - return 0; -} - -int tdbEnvBeginTxn(TENV *pEnv) { - pEnv->jfd = open(pEnv->jname, O_CREAT | O_RDWR, 0755); - if (pEnv->jfd < 0) { - return -1; - } - - return 0; -} - -int tdbEnvCommit(TENV *pEnv) { - /* TODO */ - close(pEnv->jfd); - pEnv->jfd = -1; - return 0; -} - -const char *tdbEnvGetRootDir(TENV *pEnv) { return pEnv->rootDir; } - -int tdbEnvRgstPageFile(TENV *pEnv, SPgFile *pPgFile) { - SPgFileList *pBucket; - - TD_DLIST_APPEND_WITH_FIELD(&(pEnv->pgfList), pPgFile, envPgfList); - - pBucket = pEnv->pgfht.buckets + (TDB_ENV_PGF_HASH(pPgFile->fileid) % TDB_ENV_PGF_HASH_BUCKETS); // TODO - TD_DLIST_APPEND_WITH_FIELD(pBucket, pPgFile, envHash); - - return 0; -} - -int tdbEnvRgstDB(TENV *pEnv, TDB *pDb) { - // TODO - return 0; -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbDb.h b/source/libs/tdb/src/inc/tdbDb.h new file mode 100644 index 0000000000..5356a406ea --- /dev/null +++ b/source/libs/tdb/src/inc/tdbDb.h @@ -0,0 +1,33 @@ +/* + * 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 _TD_TDB_DB_H_ +#define _TD_TDB_DB_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct STDb STDb; + +int tdbDbOpen(STDb **ppDb); +int tdbDbClose(STDb *pDb); +int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int valLen); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_TDB_DB_H_*/ \ 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 84b199c42b..1bf82d4b26 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -34,8 +34,6 @@ typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; -typedef struct SPgFile SPgFile; - // SPgno typedef u32 SPgno; #define TDB_IVLD_PGNO ((pgno_t)0) @@ -135,6 +133,8 @@ typedef TD_DLIST_NODE(SPgFile) SPgFileListNode; #include "tdbEnv.h" +#include "tdbDb.h" + #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 5ab0b4c0f1..fc135e5aef 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -1,68 +1,28 @@ #include "gtest/gtest.h" -#include "tdb.h" +#include "tdbInt.h" TEST(tdb_test, simple_test) { - TENV * pEnv; - TDB * pDb1, *pDb2, *pDb3; - pgsz_t pgSize = 1024; - cachesz_t cacheSize = 10240; + int ret; + STEnv *pEnv; + STDb *pDb; - // ENV - GTEST_ASSERT_EQ(tdbEnvCreate(&pEnv, "./testtdb"), 0); + // Open Env + ret = tdbEnvOpen("tdb", 1024, 20, &pEnv); + GTEST_ASSERT_EQ(ret, 0); - GTEST_ASSERT_EQ(tdbEnvSetCache(pEnv, pgSize, cacheSize), 0); + // Create a database + ret = tdbDbOpen(&pDb); + GTEST_ASSERT_EQ(ret, 0); - GTEST_ASSERT_EQ(tdbEnvGetCacheSize(pEnv), cacheSize); + // Insert some data + ret = tdbDbInsert(pDb, "1", 1, "world", 5); + GTEST_ASSERT_EQ(ret, 0); - GTEST_ASSERT_EQ(tdbEnvGetPageSize(pEnv), pgSize); + // Close a database + tdbDbClose(pDb); - GTEST_ASSERT_EQ(tdbEnvOpen(pEnv), 0); - -#if 1 - // DB - GTEST_ASSERT_EQ(tdbCreate(&pDb1), 0); - - // GTEST_ASSERT_EQ(tdbSetKeyLen(pDb1, 8), 0); - - // GTEST_ASSERT_EQ(tdbGetKeyLen(pDb1), 8); - - // GTEST_ASSERT_EQ(tdbSetValLen(pDb1, 3), 0); - - // GTEST_ASSERT_EQ(tdbGetValLen(pDb1), 3); - - // GTEST_ASSERT_EQ(tdbSetDup(pDb1, 1), 0); - - // GTEST_ASSERT_EQ(tdbGetDup(pDb1), 1); - - // GTEST_ASSERT_EQ(tdbSetCmprFunc(pDb1, NULL), 0); - - tdbEnvBeginTxn(pEnv); - - GTEST_ASSERT_EQ(tdbOpen(pDb1, "db.db", "db1", pEnv), 0); - - // char *key = "key1"; - // char *val = "value1"; - // tdbInsert(pDb1, (void *)key, strlen(key), (void *)val, strlen(val)); - - tdbEnvCommit(pEnv); - -#if 0 - // Insert - - // Query - - // Delete - - // Query -#endif - - // GTEST_ASSERT_EQ(tdbOpen(&pDb2, "db.db", "db2", pEnv), 0); - // GTEST_ASSERT_EQ(tdbOpen(&pDb3, "index.db", NULL, pEnv), 0); - // tdbClose(pDb3); - // tdbClose(pDb2); - tdbClose(pDb1); -#endif - - tdbEnvClose(pEnv); + // Close Env + ret = tdbEnvClose(pEnv); + GTEST_ASSERT_EQ(ret, 0); } \ No newline at end of file -- GitLab