From cb9ca05b036a3c3408e6bcac5818bbe583027cf0 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 24 Jan 2022 09:00:39 +0000 Subject: [PATCH 01/79] more tdb --- source/libs/tdb/src/db/tdb_db.c | 49 ++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/source/libs/tdb/src/db/tdb_db.c b/source/libs/tdb/src/db/tdb_db.c index 3675844535..112fade741 100644 --- a/source/libs/tdb/src/db/tdb_db.c +++ b/source/libs/tdb/src/db/tdb_db.c @@ -15,12 +15,59 @@ #include "tdb_db.h" +static int tdbOpenImpl(TDB *dbp); + int tdbOpen(TDB **dbpp, const char *fname, const char *dbname, uint32_t flags) { - // TODO + TDB * dbp; + TDB_MPFILE *mpf; + uint8_t fileid[TDB_FILE_ID_LEN]; + + if ((dbp = (TDB *)calloc(1, sizeof(*dbp))) == NULL) { + return -1; + } + + if ((dbp->fname = strdup(fname)) == NULL) { + free(dbp); + return -1; + } + + if ((dbname) && ((dbp->dbname = strdup(dbname)) == NULL)) { + free(dbp->fname); + free(dbp); + return -1; + } + + // if (tdbGnrtFileID(fname, fileid) < 0) { + // // todo + // return -1; + // } + + // TODO: mpf = tdbGetMPFileByID(fileid); + if (mpf == NULL) { + // todoerr: maybe we need to create one + return -1; + } + + if (tdbOpenImpl(dbp) < 0) { + // todoerr + return -1; + } + + *dbpp = dbp; return 0; } int tdbClose(TDB *dbp, uint32_t flags) { // TODO return 0; +} + +static int tdbOpenImpl(TDB *dbp) { + if (dbp->dbname == NULL) { + // todo: open the DB as a master DB + } else { + // todo: open the DB as a sub-db + } + // TODO + return 0; } \ No newline at end of file From 065f3d96820fcdb3301aab6bed14a91416a3ed14 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 24 Jan 2022 13:27:32 +0000 Subject: [PATCH 02/79] more tdb --- source/libs/tdb/src/db/tdb_mpool.c | 17 ------------- source/libs/tdb/src/db/tdb_util.c | 33 +++++++++++++++++++++++++ source/libs/tdb/src/inc/tdb_btree.h | 33 +++++++++++++++++++++++++ source/libs/tdb/src/inc/tdb_inc.h | 3 +++ source/libs/tdb/src/inc/tdb_page.h | 37 +++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 17 deletions(-) create mode 100644 source/libs/tdb/src/db/tdb_util.c create mode 100644 source/libs/tdb/src/inc/tdb_btree.h create mode 100644 source/libs/tdb/src/inc/tdb_page.h diff --git a/source/libs/tdb/src/db/tdb_mpool.c b/source/libs/tdb/src/db/tdb_mpool.c index 9b67f405a9..1b591a5c3f 100644 --- a/source/libs/tdb/src/db/tdb_mpool.c +++ b/source/libs/tdb/src/db/tdb_mpool.c @@ -15,7 +15,6 @@ #include "tdb_mpool.h" -static int tdbGnrtFileID(const char *fname, uint8_t *fileid); static void tdbMPoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf); static void tdbMPoolUnregFile(TDB_MPOOL *mp, TDB_MPFILE *mpf); static TDB_MPFILE *tdbMPoolGetFile(TDB_MPOOL *mp, uint8_t *fileid); @@ -230,22 +229,6 @@ int tdbMPoolFilePutPage(TDB_MPFILE *mpf, pgno_t pgno, void *addr) { return 0; } -static int tdbGnrtFileID(const char *fname, uint8_t *fileid) { - struct stat statbuf; - - if (stat(fname, &statbuf) < 0) { - return -1; - } - - memset(fileid, 0, TDB_FILE_ID_LEN); - - ((uint64_t *)fileid)[0] = (uint64_t)statbuf.st_ino; - ((uint64_t *)fileid)[1] = (uint64_t)statbuf.st_dev; - ((uint64_t *)fileid)[2] = rand(); - - return 0; -} - #define MPF_GET_BUCKETID(fileid) \ ({ \ uint64_t *tmp = (uint64_t *)fileid; \ diff --git a/source/libs/tdb/src/db/tdb_util.c b/source/libs/tdb/src/db/tdb_util.c new file mode 100644 index 0000000000..9a5df604c4 --- /dev/null +++ b/source/libs/tdb/src/db/tdb_util.c @@ -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 . + */ + +#include "tdb_inc.h" + +int tdbGnrtFileID(const char *fname, uint8_t *fileid) { + struct stat statbuf; + + if (stat(fname, &statbuf) < 0) { + return -1; + } + + memset(fileid, 0, TDB_FILE_ID_LEN); + + ((uint64_t *)fileid)[0] = (uint64_t)statbuf.st_ino; + ((uint64_t *)fileid)[1] = (uint64_t)statbuf.st_dev; + ((uint64_t *)fileid)[2] = rand(); + + return 0; +} + diff --git a/source/libs/tdb/src/inc/tdb_btree.h b/source/libs/tdb/src/inc/tdb_btree.h new file mode 100644 index 0000000000..a593c3b4e8 --- /dev/null +++ b/source/libs/tdb/src/inc/tdb_btree.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 _TDB_BTREE_H_ +#define _TDB_BTREE_H_ + +#include "tdb_inc.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct SBTree { + pgno_t root; // root page number +}; + +#ifdef __cplusplus +} +#endif + +#endif /*_TDB_BTREE_H_*/ \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdb_inc.h b/source/libs/tdb/src/inc/tdb_inc.h index 885191477c..9e13f779e7 100644 --- a/source/libs/tdb/src/inc/tdb_inc.h +++ b/source/libs/tdb/src/inc/tdb_inc.h @@ -51,6 +51,9 @@ typedef int32_t pgsize_t; // tdb_log #define tdbError(var) +// tdb util +int tdbGnrtFileID(const char *fname, uint8_t *fileid); + #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/src/inc/tdb_page.h b/source/libs/tdb/src/inc/tdb_page.h new file mode 100644 index 0000000000..42981a5e8b --- /dev/null +++ b/source/libs/tdb/src/inc/tdb_page.h @@ -0,0 +1,37 @@ +/* + * 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_PAGE_H_ +#define _TDB_PAGE_H_ + +#include "tdb_inc.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// Page header +typedef struct { + uint32_t magic; + pgno_t pgno; // current page number + pgno_t npgno; // next page number + pgno_t ppgno; // prev page number +} SPgHdr; + +#ifdef __cplusplus +} +#endif + +#endif /*_TDB_PAGE_H_*/ \ No newline at end of file From d1d587937bbdbaca319137bb3cc0b4517990078a Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 02:06:53 +0000 Subject: [PATCH 03/79] refact TDB --- source/libs/tdb/inc/tdb.h | 36 --------------- source/libs/tdb/src/db/btree.c | 14 ++++++ source/libs/tdb/src/db/pgcache.c | 0 source/libs/tdb/src/db/pgfile.c | 14 ++++++ source/libs/tdb/src/db/{tdb_db.c => tdb.c} | 0 source/libs/tdb/src/db/tdb_util.c | 2 +- .../libs/tdb/src/inc/{tdb_btree.h => btree.h} | 12 ++--- source/libs/tdb/src/inc/pgcache.h | 46 +++++++++++++++++++ .../libs/tdb/src/inc/{tdb_page.h => pgfile.h} | 16 ++----- .../libs/tdb/src/inc/{tdb_inc.h => tdbInt.h} | 6 +-- source/libs/tdb/src/inc/tdb_mpool.h | 2 +- 11 files changed, 89 insertions(+), 59 deletions(-) create mode 100644 source/libs/tdb/src/db/btree.c create mode 100644 source/libs/tdb/src/db/pgcache.c create mode 100644 source/libs/tdb/src/db/pgfile.c rename source/libs/tdb/src/db/{tdb_db.c => tdb.c} (100%) rename source/libs/tdb/src/inc/{tdb_btree.h => btree.h} (85%) create mode 100644 source/libs/tdb/src/inc/pgcache.h rename source/libs/tdb/src/inc/{tdb_page.h => pgfile.h} (72%) rename source/libs/tdb/src/inc/{tdb_inc.h => tdbInt.h} (93%) diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index faf9208f8f..71ac3d97ed 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -22,42 +22,6 @@ extern "C" { #endif -// #define TDB_EXTERN -// #define TDB_PUBLIC -// #define TDB_STATIC static - -// typedef enum { TDB_BTREE_T = 0, TDB_HASH_T = 1, TDB_HEAP_T = 2 } tdb_db_t; - -// // Forward declarations -// typedef struct TDB TDB; -// // typedef struct TDB_MPOOL TDB_MPOOL; -// // typedef struct TDB_MPFILE TDB_MPFILE; -// // typedef struct TDB_CURSOR TDB_CURSOR; - -// typedef struct { -// void* bdata; -// uint32_t size; -// } TDB_KEY, TDB_VALUE; - -// // TDB Operations -// int tdbCreateDB(TDB** dbpp, tdb_db_t type); -// int tdbOpenDB(TDB* dbp, const char* fname, const char* dbname, uint32_t flags); -// int tdbCloseDB(TDB* dbp, uint32_t flags); -// int tdbPut(TDB* dbp, const TDB_KEY* key, const TDB_VALUE* value, uint32_t flags); -// int tdbGet(TDB* dbp, const TDB_KEY* key, TDB_VALUE* value, uint32_t flags); - -// // TDB_MPOOL -// int tdbOpenMPool(TDB_MPOOL** mp); -// int tdbCloseMPool(TDB_MPOOL* mp); - -// // TDB_MPFILE -// int tdbOpenMPFile(TDB_MPFILE** mpf, TDB_MPOOL* mp); -// int tdbCloseMPFile(TDB_MPFILE** mpf); - -// // TDB_CURSOR -// int tdbOpenCursor(TDB* dbp, TDB_CURSOR** tdbcpp); -// int tdbCloseCurosr(TDB_CURSOR* tdbcp); - #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c new file mode 100644 index 0000000000..6dea4a4e57 --- /dev/null +++ b/source/libs/tdb/src/db/btree.c @@ -0,0 +1,14 @@ +/* + * 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 . + */ \ No newline at end of file diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c new file mode 100644 index 0000000000..6dea4a4e57 --- /dev/null +++ b/source/libs/tdb/src/db/pgfile.c @@ -0,0 +1,14 @@ +/* + * 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 . + */ \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdb_db.c b/source/libs/tdb/src/db/tdb.c similarity index 100% rename from source/libs/tdb/src/db/tdb_db.c rename to source/libs/tdb/src/db/tdb.c diff --git a/source/libs/tdb/src/db/tdb_util.c b/source/libs/tdb/src/db/tdb_util.c index 9a5df604c4..6391397efd 100644 --- a/source/libs/tdb/src/db/tdb_util.c +++ b/source/libs/tdb/src/db/tdb_util.c @@ -13,7 +13,7 @@ * along with this program. If not, see . */ -#include "tdb_inc.h" +#include "tdbInt.h" int tdbGnrtFileID(const char *fname, uint8_t *fileid) { struct stat statbuf; diff --git a/source/libs/tdb/src/inc/tdb_btree.h b/source/libs/tdb/src/inc/btree.h similarity index 85% rename from source/libs/tdb/src/inc/tdb_btree.h rename to source/libs/tdb/src/inc/btree.h index a593c3b4e8..7b1b6cd2ef 100644 --- a/source/libs/tdb/src/inc/tdb_btree.h +++ b/source/libs/tdb/src/inc/btree.h @@ -13,21 +13,21 @@ * along with this program. If not, see . */ -#ifndef _TDB_BTREE_H_ -#define _TDB_BTREE_H_ - -#include "tdb_inc.h" +#ifndef _TD_BTREE_H_ +#define _TD_BTREE_H_ #ifdef __cplusplus extern "C" { #endif +typedef struct SBTree SBTree; + struct SBTree { - pgno_t root; // root page number + // TODO }; #ifdef __cplusplus } #endif -#endif /*_TDB_BTREE_H_*/ \ No newline at end of file +#endif /*_TD_BTREE_H_*/ \ No newline at end of file diff --git a/source/libs/tdb/src/inc/pgcache.h b/source/libs/tdb/src/inc/pgcache.h new file mode 100644 index 0000000000..0bf5d22f3f --- /dev/null +++ b/source/libs/tdb/src/inc/pgcache.h @@ -0,0 +1,46 @@ +/* + * 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_PAGE_CACHE_H_ +#define _TD_PAGE_CACHE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct SPgCache SPgCache; +typedef struct SPage SPage; + +// SPgCache +int pgCacheCreate(SPgCache **ppPgCache); +int pgCacheDestroy(SPgCache *pPgCache); +int pgCacheOpen(SPgCache *pPgCache); +int pgCacheClose(SPgCache *pPgCache); + +SPage *pgCacheFetch(SPgCache *pPgCache); +int pgCacheRelease(SPage *pPage); + +// SPage + +// Impl +struct SPgCache { + // TODO +}; + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_PAGE_CACHE_H_*/ \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdb_page.h b/source/libs/tdb/src/inc/pgfile.h similarity index 72% rename from source/libs/tdb/src/inc/tdb_page.h rename to source/libs/tdb/src/inc/pgfile.h index 42981a5e8b..f9a7435b3d 100644 --- a/source/libs/tdb/src/inc/tdb_page.h +++ b/source/libs/tdb/src/inc/pgfile.h @@ -13,25 +13,17 @@ * along with this program. If not, see . */ -#ifndef _TDB_PAGE_H_ -#define _TDB_PAGE_H_ - -#include "tdb_inc.h" +#ifndef _TD_PAGE_FILE_H_ +#define _TD_PAGE_FILE_H_ #ifdef __cplusplus extern "C" { #endif -// Page header -typedef struct { - uint32_t magic; - pgno_t pgno; // current page number - pgno_t npgno; // next page number - pgno_t ppgno; // prev page number -} SPgHdr; +typedef struct SPgFile SPgFile; #ifdef __cplusplus } #endif -#endif /*_TDB_PAGE_H_*/ \ No newline at end of file +#endif /*_TD_PAGE_FILE_H_*/ \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdb_inc.h b/source/libs/tdb/src/inc/tdbInt.h similarity index 93% rename from source/libs/tdb/src/inc/tdb_inc.h rename to source/libs/tdb/src/inc/tdbInt.h index 9e13f779e7..c2ca7b0cfb 100644 --- a/source/libs/tdb/src/inc/tdb_inc.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_TDB_INC_H_ -#define _TD_TDB_INC_H_ +#ifndef _TD_TDB_INTERNAL_H_ +#define _TD_TDB_INTERNAL_H_ #include "os.h" #include "tlist.h" @@ -58,4 +58,4 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid); } #endif -#endif /*_TD_TDB_INC_H_*/ +#endif /*_TD_TDB_INTERNAL_H_*/ diff --git a/source/libs/tdb/src/inc/tdb_mpool.h b/source/libs/tdb/src/inc/tdb_mpool.h index 37c82f3833..55754484c0 100644 --- a/source/libs/tdb/src/inc/tdb_mpool.h +++ b/source/libs/tdb/src/inc/tdb_mpool.h @@ -16,7 +16,7 @@ #ifndef _TD_TDB_MPOOL_H_ #define _TD_TDB_MPOOL_H_ -#include "tdb_inc.h" +#include "tdbInt.h" #ifdef __cplusplus extern "C" { From 64cb09cc3a08447ed710160f3c5908bac785cbfc Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 02:15:43 +0000 Subject: [PATCH 04/79] refact --- source/libs/tdb/src/db/{tdb_util.c => tdbUtil.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename source/libs/tdb/src/db/{tdb_util.c => tdbUtil.c} (100%) diff --git a/source/libs/tdb/src/db/tdb_util.c b/source/libs/tdb/src/db/tdbUtil.c similarity index 100% rename from source/libs/tdb/src/db/tdb_util.c rename to source/libs/tdb/src/db/tdbUtil.c From fd94249ffdb649800e114d6ce216e34c9a91155d Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 02:55:26 +0000 Subject: [PATCH 05/79] refact --- source/libs/tdb/src/db/btree.c | 4 +- source/libs/tdb/src/db/pgcache.c | 16 ++++++ source/libs/tdb/src/db/pgfile.c | 4 +- source/libs/tdb/src/db/tdb.c | 94 ++++++++++++++++---------------- source/libs/tdb/src/inc/btree.h | 2 + source/libs/tdb/src/inc/pgfile.h | 9 +++ source/libs/tdb/src/inc/tdbInt.h | 4 ++ source/libs/tdb/src/inc/tdb_db.h | 45 --------------- 8 files changed, 84 insertions(+), 94 deletions(-) delete mode 100644 source/libs/tdb/src/inc/tdb_db.h diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index 6dea4a4e57..2ce1e62c16 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -11,4 +11,6 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - */ \ No newline at end of file + */ + +#include "tdbInt.h" \ No newline at end of file diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index e69de29bb2..2ce1e62c16 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -0,0 +1,16 @@ +/* + * 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" \ No newline at end of file diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index 6dea4a4e57..2ce1e62c16 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -11,4 +11,6 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - */ \ No newline at end of file + */ + +#include "tdbInt.h" \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 112fade741..0a24331a36 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -13,61 +13,61 @@ * along with this program. If not, see . */ -#include "tdb_db.h" +#include "tdbInt.h" -static int tdbOpenImpl(TDB *dbp); +// static int tdbOpenImpl(TDB *dbp); -int tdbOpen(TDB **dbpp, const char *fname, const char *dbname, uint32_t flags) { - TDB * dbp; - TDB_MPFILE *mpf; - uint8_t fileid[TDB_FILE_ID_LEN]; +// int tdbOpen(TDB **dbpp, const char *fname, const char *dbname, uint32_t flags) { +// TDB * dbp; +// TDB_MPFILE *mpf; +// uint8_t fileid[TDB_FILE_ID_LEN]; - if ((dbp = (TDB *)calloc(1, sizeof(*dbp))) == NULL) { - return -1; - } +// if ((dbp = (TDB *)calloc(1, sizeof(*dbp))) == NULL) { +// return -1; +// } - if ((dbp->fname = strdup(fname)) == NULL) { - free(dbp); - return -1; - } +// if ((dbp->fname = strdup(fname)) == NULL) { +// free(dbp); +// return -1; +// } - if ((dbname) && ((dbp->dbname = strdup(dbname)) == NULL)) { - free(dbp->fname); - free(dbp); - return -1; - } +// if ((dbname) && ((dbp->dbname = strdup(dbname)) == NULL)) { +// free(dbp->fname); +// free(dbp); +// return -1; +// } - // if (tdbGnrtFileID(fname, fileid) < 0) { - // // todo - // return -1; - // } +// // if (tdbGnrtFileID(fname, fileid) < 0) { +// // // todo +// // return -1; +// // } - // TODO: mpf = tdbGetMPFileByID(fileid); - if (mpf == NULL) { - // todoerr: maybe we need to create one - return -1; - } +// // TODO: mpf = tdbGetMPFileByID(fileid); +// if (mpf == NULL) { +// // todoerr: maybe we need to create one +// return -1; +// } - if (tdbOpenImpl(dbp) < 0) { - // todoerr - return -1; - } +// if (tdbOpenImpl(dbp) < 0) { +// // todoerr +// return -1; +// } - *dbpp = dbp; - return 0; -} +// *dbpp = dbp; +// return 0; +// } -int tdbClose(TDB *dbp, uint32_t flags) { - // TODO - return 0; -} +// int tdbClose(TDB *dbp, uint32_t flags) { +// // TODO +// return 0; +// } -static int tdbOpenImpl(TDB *dbp) { - if (dbp->dbname == NULL) { - // todo: open the DB as a master DB - } else { - // todo: open the DB as a sub-db - } - // TODO - return 0; -} \ No newline at end of file +// static int tdbOpenImpl(TDB *dbp) { +// if (dbp->dbname == NULL) { +// // todo: open the DB as a master DB +// } else { +// // todo: open the DB as a sub-db +// } +// // TODO +// return 0; +// } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/btree.h b/source/libs/tdb/src/inc/btree.h index 7b1b6cd2ef..01658d5e00 100644 --- a/source/libs/tdb/src/inc/btree.h +++ b/source/libs/tdb/src/inc/btree.h @@ -16,6 +16,8 @@ #ifndef _TD_BTREE_H_ #define _TD_BTREE_H_ +#include "tdbInt.h" + #ifdef __cplusplus extern "C" { #endif diff --git a/source/libs/tdb/src/inc/pgfile.h b/source/libs/tdb/src/inc/pgfile.h index f9a7435b3d..9c502fed9e 100644 --- a/source/libs/tdb/src/inc/pgfile.h +++ b/source/libs/tdb/src/inc/pgfile.h @@ -16,12 +16,21 @@ #ifndef _TD_PAGE_FILE_H_ #define _TD_PAGE_FILE_H_ +#include "tdbInt.h" + +#include "pgcache.h" + #ifdef __cplusplus extern "C" { #endif typedef struct SPgFile SPgFile; +struct SPgFile { + char * fname; // backend file name + SPgCache *pPgCache; // page cache underline +}; + #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index c2ca7b0cfb..3746c5f077 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -20,6 +20,8 @@ #include "tlist.h" #include "tlockfree.h" +#include "tdb.h" + #ifdef __cplusplus extern "C" { #endif @@ -51,6 +53,8 @@ typedef int32_t pgsize_t; // tdb_log #define tdbError(var) +#include "pgcache.h" + // tdb util int tdbGnrtFileID(const char *fname, uint8_t *fileid); diff --git a/source/libs/tdb/src/inc/tdb_db.h b/source/libs/tdb/src/inc/tdb_db.h deleted file mode 100644 index 5f0529462b..0000000000 --- a/source/libs/tdb/src/inc/tdb_db.h +++ /dev/null @@ -1,45 +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 _TD_TDB_DB_H_ -#define _TD_TDB_DB_H_ - -#include "tdb_mpool.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct TDB TDB; - -struct TDB { - char * fname; - char * dbname; - TDB_MPFILE *mpf; - // union { - // TDB_BTREE *btree; - // TDB_HASH * hash; - // TDB_HEAP * heap; - // } dbam; // db access method -}; - -int tdbOpen(TDB **dbpp, const char *fname, const char *dbname, uint32_t flags); -int tdbClose(TDB *dbp, uint32_t flags); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_TDB_DB_H_*/ \ No newline at end of file From 64be05208e4c641897611630f16b9ae0738f0261 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 03:02:34 +0000 Subject: [PATCH 06/79] refact --- source/libs/tdb/src/inc/btree.h | 2 -- source/libs/tdb/src/inc/pgfile.h | 4 ---- source/libs/tdb/src/inc/tdbInt.h | 2 ++ 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/source/libs/tdb/src/inc/btree.h b/source/libs/tdb/src/inc/btree.h index 01658d5e00..7b1b6cd2ef 100644 --- a/source/libs/tdb/src/inc/btree.h +++ b/source/libs/tdb/src/inc/btree.h @@ -16,8 +16,6 @@ #ifndef _TD_BTREE_H_ #define _TD_BTREE_H_ -#include "tdbInt.h" - #ifdef __cplusplus extern "C" { #endif diff --git a/source/libs/tdb/src/inc/pgfile.h b/source/libs/tdb/src/inc/pgfile.h index 9c502fed9e..15196ef619 100644 --- a/source/libs/tdb/src/inc/pgfile.h +++ b/source/libs/tdb/src/inc/pgfile.h @@ -16,10 +16,6 @@ #ifndef _TD_PAGE_FILE_H_ #define _TD_PAGE_FILE_H_ -#include "tdbInt.h" - -#include "pgcache.h" - #ifdef __cplusplus extern "C" { #endif diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 3746c5f077..41672538f8 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -54,6 +54,8 @@ typedef int32_t pgsize_t; #define tdbError(var) #include "pgcache.h" +#include "pgfile.h" +#include "btree.h" // tdb util int tdbGnrtFileID(const char *fname, uint8_t *fileid); From 34355a49e4650020921fac0f20837fca277677d1 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 03:30:59 +0000 Subject: [PATCH 07/79] more TDB --- source/libs/tdb/src/db/btree.c | 10 +++++- source/libs/tdb/src/db/pgcache.c | 53 ++++++++++++++++++++++++++++++- source/libs/tdb/src/inc/btree.h | 7 ++-- source/libs/tdb/src/inc/pgcache.h | 5 --- 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index 2ce1e62c16..e87a25c2c3 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -13,4 +13,12 @@ * along with this program. If not, see . */ -#include "tdbInt.h" \ No newline at end of file +#include "tdbInt.h" + +struct SBTree { + // TODO +}; + +struct SBtCursor { + // TODO +}; \ No newline at end of file diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index 2ce1e62c16..25ce90e772 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -13,4 +13,55 @@ * along with this program. If not, see . */ -#include "tdbInt.h" \ No newline at end of file +#include "tdbInt.h" + +struct SPage { + pgid_t pgid; // page id + // TODO +}; + +typedef TD_DLIST(SPage) SPgList; + +struct SPgCache { + SPage *pages; + + SPgList freeList; + + struct { + int32_t nbucket; + struct { + SRWLatch latch; + TD_DLIST(SPage) ht; + } * buckets; + } pght; // page hash table +}; + +int pgCacheCreate(SPgCache **ppPgCache) { + // TODO + return 0; +} + +int pgCacheDestroy(SPgCache *pPgCache) { + // TODO + return 0; +} + +int pgCacheOpen(SPgCache *pPgCache) { + // TODO + return 0; +} + +int pgCacheClose(SPgCache *pPgCache) { + // TODO + return 0; +} + +SPage *pgCacheFetch(SPgCache *pPgCache) { + // TODO + return NULL; +} + +int pgCacheRelease(SPage *pPage) { + // TODO + return 0; +} \ No newline at end of file diff --git a/source/libs/tdb/src/inc/btree.h b/source/libs/tdb/src/inc/btree.h index 7b1b6cd2ef..757525d6a7 100644 --- a/source/libs/tdb/src/inc/btree.h +++ b/source/libs/tdb/src/inc/btree.h @@ -20,11 +20,8 @@ extern "C" { #endif -typedef struct SBTree SBTree; - -struct SBTree { - // TODO -}; +typedef struct SBTree SBTree; +typedef struct SBtCursor SBtCursor; #ifdef __cplusplus } diff --git a/source/libs/tdb/src/inc/pgcache.h b/source/libs/tdb/src/inc/pgcache.h index 0bf5d22f3f..af85e0de63 100644 --- a/source/libs/tdb/src/inc/pgcache.h +++ b/source/libs/tdb/src/inc/pgcache.h @@ -34,11 +34,6 @@ int pgCacheRelease(SPage *pPage); // SPage -// Impl -struct SPgCache { - // TODO -}; - #ifdef __cplusplus } #endif From 218ab32666709e0740e25720264a3350cd4a5342 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 03:38:22 +0000 Subject: [PATCH 08/79] more tdb --- source/libs/tdb/src/db/pgcache.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index 25ce90e772..5ca65e366e 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -12,7 +12,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - #include "tdbInt.h" struct SPage { @@ -37,12 +36,22 @@ struct SPgCache { }; int pgCacheCreate(SPgCache **ppPgCache) { - // TODO + SPgCache *pPgCache; + + pPgCache = (SPgCache *)calloc(1, sizeof(*pPgCache)); + if (pPgCache == NULL) { + return -1; + } + + *ppPgCache = pPgCache; return 0; } int pgCacheDestroy(SPgCache *pPgCache) { - // TODO + if (pPgCache) { + free(pPgCache); + } + return 0; } From a195e51c9f5246e6e3ba45891b215a6449a09ae4 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 03:51:02 +0000 Subject: [PATCH 09/79] more work --- source/libs/tdb/src/db/pgcache.c | 21 +++++++++++---------- source/libs/tdb/src/inc/pgcache.h | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index 5ca65e366e..be26d77f80 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -22,20 +22,17 @@ struct SPage { typedef TD_DLIST(SPage) SPgList; struct SPgCache { - SPage *pages; - - SPgList freeList; - + SRWLatch mutex; + pgsize_t pgsize; + SPage * pages; + SPgList freeList; struct { - int32_t nbucket; - struct { - SRWLatch latch; - TD_DLIST(SPage) ht; - } * buckets; + int32_t nbucket; + SPgList *buckets; } pght; // page hash table }; -int pgCacheCreate(SPgCache **ppPgCache) { +int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgsize) { SPgCache *pPgCache; pPgCache = (SPgCache *)calloc(1, sizeof(*pPgCache)); @@ -43,6 +40,10 @@ int pgCacheCreate(SPgCache **ppPgCache) { return -1; } + pPgCache->pgsize = pgsize; + + taosInitRWLatch(&(pPgCache->mutex)); + *ppPgCache = pPgCache; return 0; } diff --git a/source/libs/tdb/src/inc/pgcache.h b/source/libs/tdb/src/inc/pgcache.h index af85e0de63..022198b246 100644 --- a/source/libs/tdb/src/inc/pgcache.h +++ b/source/libs/tdb/src/inc/pgcache.h @@ -24,7 +24,7 @@ typedef struct SPgCache SPgCache; typedef struct SPage SPage; // SPgCache -int pgCacheCreate(SPgCache **ppPgCache); +int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgsize); int pgCacheDestroy(SPgCache *pPgCache); int pgCacheOpen(SPgCache *pPgCache); int pgCacheClose(SPgCache *pPgCache); From 6d446e6134c8e1c273936bc755e30969b0204082 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 06:52:43 +0000 Subject: [PATCH 10/79] more TDB --- source/libs/tdb/src/db/pgcache.c | 68 ++++++++++++++++++++++++++++--- source/libs/tdb/src/inc/pgcache.h | 6 +-- source/libs/tdb/src/inc/tdbInt.h | 3 ++ 3 files changed, 68 insertions(+), 9 deletions(-) diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index be26d77f80..5e107c4930 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -14,9 +14,12 @@ */ #include "tdbInt.h" +typedef TD_DLIST_NODE(SPage) SPgListNode; struct SPage { - pgid_t pgid; // page id - // TODO + pgid_t pgid; // page id + frame_id_t frameid; // frame id + SPgListNode freeNode; // for SPgCache.freeList + uint8_t * pData; // real data }; typedef TD_DLIST(SPage) SPgList; @@ -24,6 +27,7 @@ typedef TD_DLIST(SPage) SPgList; struct SPgCache { SRWLatch mutex; pgsize_t pgsize; + int32_t npage; SPage * pages; SPgList freeList; struct { @@ -32,31 +36,83 @@ struct SPgCache { } pght; // page hash table }; -int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgsize) { +int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgSize, int32_t npage) { SPgCache *pPgCache; + SPage * pPage; + + *ppPgCache = NULL; + + if (!TDB_IS_PGSIZE_VLD(pgSize)) { + return -1; + } pPgCache = (SPgCache *)calloc(1, sizeof(*pPgCache)); if (pPgCache == NULL) { return -1; } - pPgCache->pgsize = pgsize; - taosInitRWLatch(&(pPgCache->mutex)); + pPgCache->pgsize = pgSize; + pPgCache->npage = npage; + + pPgCache->pages = (SPage *)calloc(npage, sizeof(SPage)); + if (pPgCache->pages == NULL) { + pgCacheDestroy(pPgCache); + return -1; + } + + TD_DLIST_INIT(&(pPgCache->freeList)); + + for (int32_t i = 0; i < npage; i++) { + pPage = pPgCache->pages + i; + + pPage->pgid = TDB_IVLD_PGID; + pPage->frameid = i; + + pPage->pData = (uint8_t *)calloc(1, pgSize); + if (pPage->pData == NULL) { + pgCacheDestroy(pPgCache); + return -1; + } + + pPgCache->pght.nbucket = npage; + pPgCache->pght.buckets = (SPgList *)calloc(pPgCache->pght.nbucket, sizeof(SPgList)); + if (pPgCache->pght.buckets == NULL) { + pgCacheDestroy(pPgCache); + return -1; + } + + TD_DLIST_APPEND_WITH_FIELD(&(pPgCache->freeList), pPage, freeNode); + } *ppPgCache = pPgCache; return 0; } int pgCacheDestroy(SPgCache *pPgCache) { + SPage *pPage; if (pPgCache) { + tfree(pPgCache->pght.buckets); + if (pPgCache->pages) { + for (int32_t i = 0; i < pPgCache->npage; i++) { + pPage = pPgCache->pages + i; + tfree(pPage->pData); + } + + free(pPgCache->pages); + } free(pPgCache); } return 0; } -int pgCacheOpen(SPgCache *pPgCache) { +int pgCacheOpen(SPgCache **ppPgCache) { + if (*ppPgCache == NULL) { + if (pgCacheCreate(ppPgCache, TDB_DEFAULT_PGSIZE, TDB_DEFAULT_CACHE_SIZE / TDB_DEFAULT_PGSIZE) < 0) { + return -1; + } + } // TODO return 0; } diff --git a/source/libs/tdb/src/inc/pgcache.h b/source/libs/tdb/src/inc/pgcache.h index 022198b246..5a88cf3072 100644 --- a/source/libs/tdb/src/inc/pgcache.h +++ b/source/libs/tdb/src/inc/pgcache.h @@ -24,9 +24,9 @@ typedef struct SPgCache SPgCache; typedef struct SPage SPage; // SPgCache -int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgsize); -int pgCacheDestroy(SPgCache *pPgCache); -int pgCacheOpen(SPgCache *pPgCache); +int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgSize, int32_t npage); +int pgCacheOpen(SPgCache **ppPgCache); +int pgCacheClose(SPgCache *pPgCache); int pgCacheClose(SPgCache *pPgCache); SPage *pgCacheFetch(SPgCache *pPgCache); diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 41672538f8..e9cfde5e59 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -50,6 +50,9 @@ typedef int32_t pgsize_t; #define TDB_DEFAULT_PGSIZE 4096 #define TDB_IS_PGSIZE_VLD(s) (((s) >= TDB_MIN_PGSIZE) && ((s) <= TDB_MAX_PGSIZE)) +// cache +#define TDB_DEFAULT_CACHE_SIZE (256 * 1024) // 256K + // tdb_log #define tdbError(var) From 211b6ad47cf39fa9c73459cc6bafcff44e39634d Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 07:07:27 +0000 Subject: [PATCH 11/79] more TDB --- source/libs/tdb/src/db/pgcache.c | 2 +- source/libs/tdb/src/db/pgfile.c | 28 +++++++++++++++++++++++++++- source/libs/tdb/src/inc/pgcache.h | 4 ++-- source/libs/tdb/src/inc/pgfile.h | 16 +++++++++++++--- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index 5e107c4930..88d932ba22 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -122,7 +122,7 @@ int pgCacheClose(SPgCache *pPgCache) { return 0; } -SPage *pgCacheFetch(SPgCache *pPgCache) { +SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid) { // TODO return NULL; } diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index 2ce1e62c16..1c017657c1 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -13,4 +13,30 @@ * along with this program. If not, see . */ -#include "tdbInt.h" \ No newline at end of file +#include "tdbInt.h" + +int pgFileOpen(const char *fname, SPgCache *pPgCache, SPgFile **ppPgFile) { + SPgFile *pPgFile; + // TODO + return 0; +} + +int pgFileClose(SPgFile *pPgFile) { + // TODO + return 0; +} + +SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) { + // TODO + return NULL; +} + +int pgFileRelease(SPage *pPage) { + // TODO + return 0; +} + +int pgFileWrite(SPage *pPage) { + // TODO + return 0; +} \ No newline at end of file diff --git a/source/libs/tdb/src/inc/pgcache.h b/source/libs/tdb/src/inc/pgcache.h index 5a88cf3072..139cece153 100644 --- a/source/libs/tdb/src/inc/pgcache.h +++ b/source/libs/tdb/src/inc/pgcache.h @@ -25,11 +25,11 @@ typedef struct SPage SPage; // SPgCache int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgSize, int32_t npage); +int pgCacheDestroy(SPgCache *pPgCache); int pgCacheOpen(SPgCache **ppPgCache); int pgCacheClose(SPgCache *pPgCache); -int pgCacheClose(SPgCache *pPgCache); -SPage *pgCacheFetch(SPgCache *pPgCache); +SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid); int pgCacheRelease(SPage *pPage); // SPage diff --git a/source/libs/tdb/src/inc/pgfile.h b/source/libs/tdb/src/inc/pgfile.h index 15196ef619..e951d59da0 100644 --- a/source/libs/tdb/src/inc/pgfile.h +++ b/source/libs/tdb/src/inc/pgfile.h @@ -21,12 +21,22 @@ extern "C" { #endif typedef struct SPgFile SPgFile; - struct SPgFile { - char * fname; // backend file name - SPgCache *pPgCache; // page cache underline + char * fname; // backend file name + uint8_t fileid[TDB_FILE_ID_LEN]; // file id + SPgCache *pPgCache; // page cache underline + int fd; + pgno_t pgFileSize; }; +int pgFileOpen(const char *fname, SPgCache *pPgCache, SPgFile **ppPgFile); +int pgFileClose(SPgFile *pPgFile); + +SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno); +int pgFileRelease(SPage *pPage); + +int pgFileWrite(SPage *pPage); + #ifdef __cplusplus } #endif From f60c0729dab0f55a3531681b95cca37f6b9d6b80 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 07:20:56 +0000 Subject: [PATCH 12/79] more work --- source/libs/tdb/src/db/pgfile.c | 44 +++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index 1c017657c1..b98d10e6a6 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -17,12 +17,52 @@ int pgFileOpen(const char *fname, SPgCache *pPgCache, SPgFile **ppPgFile) { SPgFile *pPgFile; - // TODO + + *ppPgFile = NULL; + + pPgFile = (SPgFile *)calloc(1, sizeof(*pPgFile)); + if (pPgFile == NULL) { + return -1; + } + + pPgFile->fd = -1; + + pPgFile->fname = strdup(fname); + if (pPgFile->fname == NULL) { + pgFileClose(pPgFile); + return -1; + } + + pPgFile->pPgCache = pPgCache; + + pPgFile->fd = open(fname, O_RDWR, 0755); + if (pPgFile->fd < 0) { + pgFileClose(pPgFile); + return -1; + } + + if (tdbGnrtFileID(fname, pPgFile->fileid) < 0) { + pgFileClose(pPgFile); + return -1; + } + + // TODO: get file size + pPgFile->pgFileSize = 0; + + *ppPgFile = pPgFile; return 0; } int pgFileClose(SPgFile *pPgFile) { - // TODO + if (pPgFile) { + if (pPgFile->fd >= 0) { + close(pPgFile->fd); + } + + tfree(pPgFile->fname); + free(pPgFile); + } + return 0; } From add314fa3831cc99b8d13e6ce00c9d7a9e2f78dc Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 07:41:17 +0000 Subject: [PATCH 13/79] more --- source/libs/tdb/src/db/btree.c | 1 + source/libs/tdb/src/db/pgcache.c | 4 +++- source/libs/tdb/src/db/pgfile.c | 16 +++++++++++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index e87a25c2c3..7d42484488 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -16,6 +16,7 @@ #include "tdbInt.h" struct SBTree { + pgno_t rootPage; // TODO }; diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index 88d932ba22..fb9385af98 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -123,7 +123,9 @@ int pgCacheClose(SPgCache *pPgCache) { } SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid) { - // TODO + SPage *pPage; + + // 1. Check if the page is cached return NULL; } diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index b98d10e6a6..15c18b2950 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -67,12 +67,22 @@ int pgFileClose(SPgFile *pPgFile) { } SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) { - // TODO - return NULL; + SPgCache *pPgCache; + SPage * pPage; + pgid_t pgid; + + pPgCache = pPgFile->pPgCache; + pPage = NULL; + memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN); + pgid.pgno = pgno; + + pPage = pgCacheFetch(pPgCache, pgid); + + return pPage; } int pgFileRelease(SPage *pPage) { - // TODO + pgCacheRelease(pPage); return 0; } From 2f320dc23b4ab636f8719760ad75d7b95649fdaa Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 08:21:35 +0000 Subject: [PATCH 14/79] more TDB --- source/libs/tdb/src/db/pgcache.c | 31 +++++++++++++++++++++++++++++-- source/libs/tdb/src/inc/tdbInt.h | 24 ++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index fb9385af98..8fb54c8344 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -19,6 +19,7 @@ struct SPage { pgid_t pgid; // page id frame_id_t frameid; // frame id SPgListNode freeNode; // for SPgCache.freeList + SPgListNode pghtNode; // for pght uint8_t * pData; // real data }; @@ -36,6 +37,8 @@ struct SPgCache { } pght; // page hash table }; +static void pgCachePinPage(SPage *pPage); + int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgSize, int32_t npage) { SPgCache *pPgCache; SPage * pPage; @@ -123,13 +126,37 @@ int pgCacheClose(SPgCache *pPgCache) { } SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid) { - SPage *pPage; + SPage * pPage; + SPgFile *pPgFile; + SPgList *pBucket; + + // 1. Search the page hash table SPgCache.pght + pBucket = pPgCache->pght.buckets + ((0 /*TODO*/) % pPgCache->pght.nbucket); + pPage = TD_DLIST_HEAD(pBucket); + while (pPage && tdbCmprPgId(&(pPage->pgid), &pgid)) { + pPage = TD_DLIST_NODE_NEXT_WITH_FIELD(pPage, pghtNode); + } + + if (pPage) { + // Page is found, pin the page (TODO) and return the page + pgCachePinPage(pPage); + return pPage; + } + + // TODO - // 1. Check if the page is cached return NULL; } int pgCacheRelease(SPage *pPage) { // TODO return 0; +} + +static void pgCachePinPage(SPage *pPage) { + // TODO +} + +static void pgCacheUnpinPage(SPage *pPage) { + // TODO } \ 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 e9cfde5e59..a2bbcbd562 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -38,8 +38,28 @@ typedef struct { uint8_t fileid[TDB_FILE_ID_LEN]; pgno_t pgno; } pgid_t; + #define TDB_IVLD_PGID (pgid_t){0, TDB_IVLD_PGNO}; +static FORCE_INLINE int tdbCmprPgId(const void *p1, const void *p2) { + pgid_t *pgid1 = (pgid_t *)p1; + pgid_t *pgid2 = (pgid_t *)p2; + int rcode; + + rcode = memcmp(pgid1->fileid, pgid2->fileid, TDB_FILE_ID_LEN); + if (rcode) { + return rcode; + } else { + if (pgid1->pgno > pgid2->pgno) { + return 1; + } else if (pgid1->pgno < pgid2->pgno) { + return -1; + } else { + return 0; + } + } +} + // framd_id_t typedef int32_t frame_id_t; @@ -51,14 +71,14 @@ typedef int32_t pgsize_t; #define TDB_IS_PGSIZE_VLD(s) (((s) >= TDB_MIN_PGSIZE) && ((s) <= TDB_MAX_PGSIZE)) // cache -#define TDB_DEFAULT_CACHE_SIZE (256 * 1024) // 256K +#define TDB_DEFAULT_CACHE_SIZE (256 * 1024) // 256K // tdb_log #define tdbError(var) +#include "btree.h" #include "pgcache.h" #include "pgfile.h" -#include "btree.h" // tdb util int tdbGnrtFileID(const char *fname, uint8_t *fileid); From f3d1bd4340ab178566f99948a34208b09c1d9971 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 09:01:10 +0000 Subject: [PATCH 15/79] more TDB --- source/libs/tdb/src/db/btree.c | 2 +- source/libs/tdb/src/db/pgcache.c | 8 ++++- source/libs/tdb/src/db/pgfile.c | 6 +++- source/libs/tdb/src/db/tdb.c | 60 +++----------------------------- source/libs/tdb/src/inc/btree.h | 6 ++++ 5 files changed, 23 insertions(+), 59 deletions(-) diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index 7d42484488..845bddc8b5 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -16,7 +16,7 @@ #include "tdbInt.h" struct SBTree { - pgno_t rootPage; + pgno_t root; // TODO }; diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index 8fb54c8344..7ca4f4239d 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -125,13 +125,19 @@ int pgCacheClose(SPgCache *pPgCache) { return 0; } +#define PG_CACHE_HASH(fileid, pgno) \ + ({ \ + uint64_t *tmp = (uint64_t *)(fileid); \ + (tmp[0] + tmp[1] + tmp[2] + (pgno)); \ + }) + SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid) { SPage * pPage; SPgFile *pPgFile; SPgList *pBucket; // 1. Search the page hash table SPgCache.pght - pBucket = pPgCache->pght.buckets + ((0 /*TODO*/) % pPgCache->pght.nbucket); + pBucket = pPgCache->pght.buckets + (PG_CACHE_HASH(pgid.fileid, pgid.pgno) % pPgCache->pght.nbucket); pPage = TD_DLIST_HEAD(pBucket); while (pPage && tdbCmprPgId(&(pPage->pgid), &pgid)) { pPage = TD_DLIST_NODE_NEXT_WITH_FIELD(pPage, pghtNode); diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index 15c18b2950..2de0dd9cc5 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -76,7 +76,11 @@ SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) { memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN); pgid.pgno = pgno; - pPage = pgCacheFetch(pPgCache, pgid); + if (pgno > pPgFile->pgFileSize) { + // TODO + } else { + pPage = pgCacheFetch(pPgCache, pgid); + } return pPage; } diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 0a24331a36..e75c6da00b 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -15,59 +15,7 @@ #include "tdbInt.h" -// static int tdbOpenImpl(TDB *dbp); - -// int tdbOpen(TDB **dbpp, const char *fname, const char *dbname, uint32_t flags) { -// TDB * dbp; -// TDB_MPFILE *mpf; -// uint8_t fileid[TDB_FILE_ID_LEN]; - -// if ((dbp = (TDB *)calloc(1, sizeof(*dbp))) == NULL) { -// return -1; -// } - -// if ((dbp->fname = strdup(fname)) == NULL) { -// free(dbp); -// return -1; -// } - -// if ((dbname) && ((dbp->dbname = strdup(dbname)) == NULL)) { -// free(dbp->fname); -// free(dbp); -// return -1; -// } - -// // if (tdbGnrtFileID(fname, fileid) < 0) { -// // // todo -// // return -1; -// // } - -// // TODO: mpf = tdbGetMPFileByID(fileid); -// if (mpf == NULL) { -// // todoerr: maybe we need to create one -// return -1; -// } - -// if (tdbOpenImpl(dbp) < 0) { -// // todoerr -// return -1; -// } - -// *dbpp = dbp; -// return 0; -// } - -// int tdbClose(TDB *dbp, uint32_t flags) { -// // TODO -// return 0; -// } - -// static int tdbOpenImpl(TDB *dbp) { -// if (dbp->dbname == NULL) { -// // todo: open the DB as a master DB -// } else { -// // todo: open the DB as a sub-db -// } -// // TODO -// return 0; -// } \ No newline at end of file +struct STDb { + // TODO + SBTree *pBt; +}; diff --git a/source/libs/tdb/src/inc/btree.h b/source/libs/tdb/src/inc/btree.h index 757525d6a7..de143fa3a5 100644 --- a/source/libs/tdb/src/inc/btree.h +++ b/source/libs/tdb/src/inc/btree.h @@ -23,6 +23,12 @@ extern "C" { typedef struct SBTree SBTree; typedef struct SBtCursor SBtCursor; +// SBTree +int btreeOpen(SBTree **ppBt); +int btreeClose(SBTree *pBt); + +// SBtCursor + #ifdef __cplusplus } #endif From 24fe6cb2df0e1c19cef50543562dd1d703d68558 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 09:38:13 +0000 Subject: [PATCH 16/79] more TDB --- source/libs/tdb/src/db/pgcache.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index 7ca4f4239d..bb4d81dd61 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -20,6 +20,7 @@ struct SPage { frame_id_t frameid; // frame id SPgListNode freeNode; // for SPgCache.freeList SPgListNode pghtNode; // for pght + SPgListNode lruNode; // for LRU uint8_t * pData; // real data }; @@ -31,6 +32,7 @@ struct SPgCache { int32_t npage; SPage * pages; SPgList freeList; + SPgList lru; struct { int32_t nbucket; SPgList *buckets; @@ -38,6 +40,7 @@ struct SPgCache { }; static void pgCachePinPage(SPage *pPage); +static void pgCacheUnpinPage(SPage *pPage); int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgSize, int32_t npage) { SPgCache *pPgCache; @@ -144,12 +147,29 @@ SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid) { } if (pPage) { - // Page is found, pin the page (TODO) and return the page + // Page is found, pin the page and return the page pgCachePinPage(pPage); return pPage; } - // TODO + // 2. Check the free list + pPage = TD_DLIST_HEAD(&(pPgCache->freeList)); + if (pPage) { + TD_DLIST_POP_WITH_FIELD(&(pPgCache->freeList), pPage, freeNode); + pgCachePinPage(pPage); + return pPage; + } + + // 3. Try to recycle a page from the LRU list + pPage = TD_DLIST_HEAD(&(pPgCache->lru)); + if (pPage) { + TD_DLIST_POP_WITH_FIELD(&(pPgCache->lru), pPage, lruNode); + // TODO: remove from the hash table + pgCachePinPage(pPage); + return pPage; + } + + // 4. If a memory allocator is set, try to allocate from the allocator (TODO) return NULL; } From a5834073542c3d3694521c2c66a89a6c8dd3c975 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 09:46:21 +0000 Subject: [PATCH 17/79] more TDB --- source/libs/tdb/src/db/pgcache.c | 10 ---------- source/libs/tdb/src/db/pgfile.c | 15 +++++++++++++++ source/libs/tdb/src/inc/pgcache.h | 10 ++++++++++ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index bb4d81dd61..1d7fe0c32f 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -14,16 +14,6 @@ */ #include "tdbInt.h" -typedef TD_DLIST_NODE(SPage) SPgListNode; -struct SPage { - pgid_t pgid; // page id - frame_id_t frameid; // frame id - SPgListNode freeNode; // for SPgCache.freeList - SPgListNode pghtNode; // for pght - SPgListNode lruNode; // for LRU - uint8_t * pData; // real data -}; - typedef TD_DLIST(SPage) SPgList; struct SPgCache { diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index 2de0dd9cc5..9e4795dc08 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -15,6 +15,8 @@ #include "tdbInt.h" +static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData); + int pgFileOpen(const char *fname, SPgCache *pPgCache, SPgFile **ppPgFile) { SPgFile *pPgFile; @@ -80,6 +82,14 @@ SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) { // TODO } else { pPage = pgCacheFetch(pPgCache, pgid); + if (1 /*Page is cached, no need to load from file*/) { + return pPage; + } else { + if (pgFileRead(pPgFile, pgno, pPage->pData) < 0) { + // todoerr + } + return pPage; + } } return pPage; @@ -93,4 +103,9 @@ int pgFileRelease(SPage *pPage) { int pgFileWrite(SPage *pPage) { // TODO return 0; +} + +static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) { + // TODO + return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/pgcache.h b/source/libs/tdb/src/inc/pgcache.h index 139cece153..d25877e8ff 100644 --- a/source/libs/tdb/src/inc/pgcache.h +++ b/source/libs/tdb/src/inc/pgcache.h @@ -34,6 +34,16 @@ int pgCacheRelease(SPage *pPage); // SPage +typedef TD_DLIST_NODE(SPage) SPgListNode; +struct SPage { + pgid_t pgid; // page id + frame_id_t frameid; // frame id + SPgListNode freeNode; // for SPgCache.freeList + SPgListNode pghtNode; // for pght + SPgListNode lruNode; // for LRU + uint8_t * pData; // real data +}; + #ifdef __cplusplus } #endif From 73e32ddf7757016f2d3530b6e28d8b652b431d12 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 10:12:09 +0000 Subject: [PATCH 18/79] more TDB --- source/libs/tdb/src/db/pgfile.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index 9e4795dc08..670fc3a667 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -106,6 +106,29 @@ int pgFileWrite(SPage *pPage) { } static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) { - // TODO + pgsize_t pgSize; + ssize_t rsize; + uint8_t *pTData; + size_t szToRead; + + // pgSize = ; (TODO) + pTData = pData; + szToRead = pgSize; + for (; szToRead > 0;) { + rsize = pread(pPgFile->fd, pTData, szToRead, pgno * pgSize); + if (rsize < 0) { + if (errno == EINTR) { + continue; + } else { + return -1; + } + } else if (rsize == 0) { + return -1; + } + + szToRead -= rsize; + pTData += rsize; + } + return 0; } \ No newline at end of file From 73f52e9be9c6f600ef997199b99d703ec41ae659 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 8 Feb 2022 09:15:12 +0000 Subject: [PATCH 19/79] more TDB --- source/libs/tdb/src/db/pgfile.c | 1 + source/libs/tdb/src/inc/pgfile.h | 1 + 2 files changed, 2 insertions(+) diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index 670fc3a667..f7d4eef799 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -36,6 +36,7 @@ int pgFileOpen(const char *fname, SPgCache *pPgCache, SPgFile **ppPgFile) { } pPgFile->pPgCache = pPgCache; + // pPgFile->pgSize = ; (TODO) pPgFile->fd = open(fname, O_RDWR, 0755); if (pPgFile->fd < 0) { diff --git a/source/libs/tdb/src/inc/pgfile.h b/source/libs/tdb/src/inc/pgfile.h index e951d59da0..ad59fc711c 100644 --- a/source/libs/tdb/src/inc/pgfile.h +++ b/source/libs/tdb/src/inc/pgfile.h @@ -25,6 +25,7 @@ struct SPgFile { char * fname; // backend file name uint8_t fileid[TDB_FILE_ID_LEN]; // file id SPgCache *pPgCache; // page cache underline + pgsize_t pgSize; int fd; pgno_t pgFileSize; }; From d4cb8e981d2304cdca11a927abb374ab4b5a0b20 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 9 Feb 2022 02:22:46 +0000 Subject: [PATCH 20/79] more TDB --- source/dnode/vnode/inc/vnode.h | 23 ++++++++++++----------- source/dnode/vnode/src/inc/vnd.h | 3 +-- source/libs/tdb/inc/tdb.h | 7 +++++++ source/libs/tdb/src/db/tdb.c | 31 +++++++++++++++++++++++++++++++ source/libs/tdb/src/inc/btree.h | 3 +++ 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index 7549772613..26e10f9c5c 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -42,8 +42,9 @@ typedef struct STqCfg { typedef struct SVnodeCfg { int32_t vgId; - SDnode *pDnode; - STfs *pTfs; + int64_t dbId; + SDnode * pDnode; + STfs * pTfs; uint64_t wsize; uint64_t ssize; uint64_t lsize; @@ -59,9 +60,9 @@ typedef struct SVnodeCfg { typedef struct { int32_t sver; - const char *timezone; - const char *locale; - const char *charset; + const char * timezone; + const char * locale; + const char * charset; uint16_t nthreads; // number of commit threads. 0 for no threads and a schedule queue should be given (TODO) PutReqToVQueryQFp putReqToVQueryQFp; SendReqToDnodeFp sendReqToDnodeFp; @@ -70,16 +71,16 @@ typedef struct { typedef struct STqReadHandle { int64_t ver; uint64_t tbUid; - SHashObj *tbIdHash; + SHashObj * tbIdHash; const SSubmitMsg *pMsg; - SSubmitBlk *pBlock; + SSubmitBlk * pBlock; SSubmitMsgIter msgIter; SSubmitBlkIter blkIter; - SMeta *pVnodeMeta; - SArray *pColIdList; // SArray + SMeta * pVnodeMeta; + SArray * pColIdList; // SArray int32_t sver; - SSchemaWrapper *pSchemaWrapper; - STSchema *pSchema; + SSchemaWrapper * pSchemaWrapper; + STSchema * pSchema; } STqReadHandle; /* ------------------------ SVnode ------------------------ */ diff --git a/source/dnode/vnode/src/inc/vnd.h b/source/dnode/vnode/src/inc/vnd.h index f442697fb0..fb81ddbc5c 100644 --- a/source/dnode/vnode/src/inc/vnd.h +++ b/source/dnode/vnode/src/inc/vnd.h @@ -23,8 +23,8 @@ #include "tlist.h" #include "tlockfree.h" #include "tmacro.h" -#include "wal.h" #include "tq.h" +#include "wal.h" #include "vnode.h" @@ -175,7 +175,6 @@ void* vmaMalloc(SVMemAllocator* pVMA, uint64_t size); void vmaFree(SVMemAllocator* pVMA, void* ptr); bool vmaIsFull(SVMemAllocator* pVMA); - #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 71ac3d97ed..de83824170 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -22,6 +22,13 @@ extern "C" { #endif +typedef struct STDb TDB; + +int tdbCreate(TDB **ppDb); +int tdbDestroy(TDB *pDb); +int tdbOpen(TDB **pDb); +int tdbClose(TDB *pDb); + #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index e75c6da00b..ea819b34e7 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -19,3 +19,34 @@ struct STDb { // TODO SBTree *pBt; }; + +int tdbCreate(TDB **ppDb) { + TDB *pDb; + + pDb = (TDB *)calloc(1, sizeof(*pDb)); + if (pDb == NULL) { + return -1; + } + + /* TODO */ + + return 0; +} + +int tdbDestroy(TDB *pDb) { + if (pDb) { + free(pDb); + } + /* TODO */ + return 0; +} + +int tdbOpen(TDB **pDb) { + // TODO + return 0; +} + +int tdbClose(TDB *pDb) { + // TODO + return 0; +} \ No newline at end of file diff --git a/source/libs/tdb/src/inc/btree.h b/source/libs/tdb/src/inc/btree.h index de143fa3a5..4b6fe589a9 100644 --- a/source/libs/tdb/src/inc/btree.h +++ b/source/libs/tdb/src/inc/btree.h @@ -28,6 +28,9 @@ int btreeOpen(SBTree **ppBt); int btreeClose(SBTree *pBt); // SBtCursor +int btreeCursorOpen(SBtCursor *pBtCur, SBTree *pBt); +int btreeCursorClose(SBtCursor *pBtCur); +int btreeCursorMoveTo(SBtCursor *pBtCur); #ifdef __cplusplus } From 8d04fb64713cbdb86953d1d63b889327c095f48c Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 9 Feb 2022 02:50:28 +0000 Subject: [PATCH 21/79] more TDB --- source/libs/tdb/inc/tdb.h | 3 ++- source/libs/tdb/src/db/btree.c | 5 ----- source/libs/tdb/src/db/tdb.c | 6 +++--- source/libs/tdb/src/db/tdbEnv.c | 21 +++++++++++++++++++++ source/libs/tdb/src/inc/btree.h | 4 ++++ 5 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 source/libs/tdb/src/db/tdbEnv.c diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index de83824170..e02f0f32b7 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -23,10 +23,11 @@ extern "C" { #endif typedef struct STDb TDB; +typedef struct STDbEnv TENV; int tdbCreate(TDB **ppDb); int tdbDestroy(TDB *pDb); -int tdbOpen(TDB **pDb); +int tdbOpen(TDB **pDb, const char *fname, const char *dbname); int tdbClose(TDB *pDb); #ifdef __cplusplus diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index 845bddc8b5..746ca6a99a 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -15,11 +15,6 @@ #include "tdbInt.h" -struct SBTree { - pgno_t root; - // TODO -}; - struct SBtCursor { // TODO }; \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index ea819b34e7..f922a04ceb 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -16,8 +16,8 @@ #include "tdbInt.h" struct STDb { - // TODO - SBTree *pBt; + SBTree btree; // current access method + SPgFile *pPgFile; // backend page file this DB is using }; int tdbCreate(TDB **ppDb) { @@ -41,7 +41,7 @@ int tdbDestroy(TDB *pDb) { return 0; } -int tdbOpen(TDB **pDb) { +int tdbOpen(TDB **pDb, const char *fname, const char *dbname) { // TODO return 0; } diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c new file mode 100644 index 0000000000..aae92f2bed --- /dev/null +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -0,0 +1,21 @@ +/* + * 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" + +struct STDbEnv { + TDB * dbList; // TDB list + SPgFile *pgFileList; // SPgFile list +}; \ No newline at end of file diff --git a/source/libs/tdb/src/inc/btree.h b/source/libs/tdb/src/inc/btree.h index 4b6fe589a9..2aa2f4528f 100644 --- a/source/libs/tdb/src/inc/btree.h +++ b/source/libs/tdb/src/inc/btree.h @@ -32,6 +32,10 @@ int btreeCursorOpen(SBtCursor *pBtCur, SBTree *pBt); int btreeCursorClose(SBtCursor *pBtCur); int btreeCursorMoveTo(SBtCursor *pBtCur); +struct SBTree { + pgno_t root; +}; + #ifdef __cplusplus } #endif From f571942846f1f1bf6df4191aed2cfba0ddfd213c Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 9 Feb 2022 03:38:22 +0000 Subject: [PATCH 22/79] more TDB --- source/libs/tdb/src/db/pgcache.c | 15 --------------- source/libs/tdb/src/db/tdbEnv.c | 1 + source/libs/tdb/src/inc/pgcache.h | 15 +++++++++++++++ source/libs/tdb/src/inc/tdbEnv.h | 27 +++++++++++++++++++++++++++ source/libs/tdb/src/inc/tdbInt.h | 1 + 5 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 source/libs/tdb/src/inc/tdbEnv.h diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index 1d7fe0c32f..b0c4406a3c 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -14,21 +14,6 @@ */ #include "tdbInt.h" -typedef TD_DLIST(SPage) SPgList; - -struct SPgCache { - SRWLatch mutex; - pgsize_t pgsize; - int32_t npage; - SPage * pages; - SPgList freeList; - SPgList lru; - struct { - int32_t nbucket; - SPgList *buckets; - } pght; // page hash table -}; - static void pgCachePinPage(SPage *pPage); static void pgCacheUnpinPage(SPage *pPage); diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index aae92f2bed..7de52f3659 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -18,4 +18,5 @@ struct STDbEnv { TDB * dbList; // TDB list SPgFile *pgFileList; // SPgFile list + SPgCache pgc; // page cache }; \ No newline at end of file diff --git a/source/libs/tdb/src/inc/pgcache.h b/source/libs/tdb/src/inc/pgcache.h index d25877e8ff..9296e0bea1 100644 --- a/source/libs/tdb/src/inc/pgcache.h +++ b/source/libs/tdb/src/inc/pgcache.h @@ -44,6 +44,21 @@ struct SPage { uint8_t * pData; // real data }; +typedef TD_DLIST(SPage) SPgList; +struct SPgCache { + TENV * pEnv; // TENV containing this page cache + SRWLatch mutex; + pgsize_t pgsize; + int32_t npage; + SPage * pages; + SPgList freeList; + SPgList lru; + struct { + int32_t nbucket; + SPgList *buckets; + } pght; // page hash table +}; + #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/src/inc/tdbEnv.h b/source/libs/tdb/src/inc/tdbEnv.h new file mode 100644 index 0000000000..2bd93fc530 --- /dev/null +++ b/source/libs/tdb/src/inc/tdbEnv.h @@ -0,0 +1,27 @@ +/* + * 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_ENV_H_ +#define _TDB_ENV_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /*_TDB_ENV_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 a2bbcbd562..cbab052049 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -79,6 +79,7 @@ typedef int32_t pgsize_t; #include "btree.h" #include "pgcache.h" #include "pgfile.h" +#include "tdbEnv.h" // tdb util int tdbGnrtFileID(const char *fname, uint8_t *fileid); From 494d55999d367036f9d23f523860aa3cd5a92bd4 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 9 Feb 2022 03:41:34 +0000 Subject: [PATCH 23/79] more TDB --- source/dnode/vnode/src/meta/metaTDBImpl.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 source/dnode/vnode/src/meta/metaTDBImpl.c diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c new file mode 100644 index 0000000000..6dea4a4e57 --- /dev/null +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -0,0 +1,14 @@ +/* + * 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 . + */ \ No newline at end of file From 5831222f8137842d11a7dcca2533fcf611890dd2 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 9 Feb 2022 09:44:23 +0000 Subject: [PATCH 24/79] more TDB --- source/libs/tdb/inc/tdb.h | 5 ++++- source/libs/tdb/src/db/tdb.c | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index e02f0f32b7..136617322d 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -22,9 +22,12 @@ extern "C" { #endif -typedef struct STDb TDB; +typedef struct STDb TDB; typedef struct STDbEnv TENV; +// TEVN + +// TDB int tdbCreate(TDB **ppDb); int tdbDestroy(TDB *pDb); int tdbOpen(TDB **pDb, const char *fname, const char *dbname); diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index f922a04ceb..11f86dd17f 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -18,6 +18,7 @@ struct STDb { SBTree btree; // current access method SPgFile *pPgFile; // backend page file this DB is using + TENV * pEnv; // TENV containing the DB }; int tdbCreate(TDB **ppDb) { From 3dc82d1b85285dcba866e9dc72bbb6650136392a Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 9 Feb 2022 10:11:36 +0000 Subject: [PATCH 25/79] more TDB --- source/libs/tdb/inc/tdb.h | 3 +-- source/libs/tdb/src/db/tdb.c | 31 +++++++++++++++++++++++++------ source/libs/tdb/src/db/tdbEnv.c | 4 +++- source/libs/tdb/src/inc/tdbInt.h | 4 +--- source/libs/tdb/src/inc/tdbUtil.h | 29 +++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 source/libs/tdb/src/inc/tdbUtil.h diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 136617322d..7784282a24 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -29,8 +29,7 @@ typedef struct STDbEnv TENV; // TDB int tdbCreate(TDB **ppDb); -int tdbDestroy(TDB *pDb); -int tdbOpen(TDB **pDb, const char *fname, const char *dbname); +int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv); int tdbClose(TDB *pDb); #ifdef __cplusplus diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 11f86dd17f..b5b248eac3 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -34,20 +34,39 @@ int tdbCreate(TDB **ppDb) { return 0; } -int tdbDestroy(TDB *pDb) { +static int tdbDestroy(TDB *pDb) { if (pDb) { free(pDb); } - /* TODO */ return 0; } -int tdbOpen(TDB **pDb, const char *fname, const char *dbname) { - // TODO +int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { + TDB *pDb; + int ret; + + // Create DB if DB handle is not created yet + if (ppDb == NULL) { + if ((ret = tdbCreate(ppDb)) != 0) { + return -1; + } + } + + pDb = *ppDb; + + // Create a default ENV if pEnv is not set + if (pEnv == NULL) { + // if ((ret = tenvOpen(&pEnv)) != 0) { + // return -1; + // } + } + + /* TODO */ + return 0; } int tdbClose(TDB *pDb) { - // TODO - return 0; + if (pDb == NULL) return 0; + return tdbDestroy(pDb); } \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 7de52f3659..c738b691c8 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -18,5 +18,7 @@ struct STDbEnv { TDB * dbList; // TDB list SPgFile *pgFileList; // SPgFile list - SPgCache pgc; // page cache + struct { + } pgfht; // page file hash table; + SPgCache pgc; // page cache }; \ 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 cbab052049..c368b68465 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -80,9 +80,7 @@ typedef int32_t pgsize_t; #include "pgcache.h" #include "pgfile.h" #include "tdbEnv.h" - -// tdb util -int tdbGnrtFileID(const char *fname, uint8_t *fileid); +#include "tdbUtil.h" #ifdef __cplusplus } diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h new file mode 100644 index 0000000000..eacd9e4a53 --- /dev/null +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -0,0 +1,29 @@ +/* + * 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_UTIL_H_ +#define _TDB_UTIL_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +int tdbGnrtFileID(const char *fname, uint8_t *fileid); + +#ifdef __cplusplus +} +#endif + +#endif /*_TDB_UTIL_H_*/ \ No newline at end of file From f6b593e297b08c8878aa765c204ff3f19d755e71 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 9 Feb 2022 10:46:27 +0000 Subject: [PATCH 26/79] more --- source/libs/tdb/src/inc/tdbUtil.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index eacd9e4a53..6ffb9dd3d4 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -20,6 +20,8 @@ extern "C" { #endif +#define TDB_ROUND8(x) (((x) + 7) & ~7) + int tdbGnrtFileID(const char *fname, uint8_t *fileid); #ifdef __cplusplus From 0a39e8fadb61dc5a5085176003ec9962233e6d4c Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 9 Feb 2022 11:02:07 +0000 Subject: [PATCH 27/79] integrate TDB --- source/dnode/vnode/CMakeLists.txt | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/source/dnode/vnode/CMakeLists.txt b/source/dnode/vnode/CMakeLists.txt index e625c56db1..bd633fa70a 100644 --- a/source/dnode/vnode/CMakeLists.txt +++ b/source/dnode/vnode/CMakeLists.txt @@ -1,7 +1,24 @@ +set(META_DB_IMPL_LIST "BDB" "TDB") +set(META_DB_IMPL "BDB" CACHE STRING "Use BDB as the default META implementation") +set_property(CACHE META_DB_IMPL PROPERTY STRINGS ${META_DB_IMPL_LIST}) + +if(META_DB_IMPL IN_LIST META_DB_IMPL_LIST) + message(STATUS "META DB Impl: ${META_DB_IMPL}==============") +else() + message(FATAL_ERROR "Invalid META DB IMPL: ${META_DB_IMPL}==============") +endif() + aux_source_directory(src/meta META_SRC) +if(${META_DB_IMPL} STREQUAL "BDB") + list(REMOVE_ITEM META_SRC "src/meta/metaTDBImpl.c") +elseif(${META_DB_IMPL} STREQUAL "TDB") + list(REMOVE_ITEM META_SRC "src/meta/metaBDBImpl.c") +endif() + aux_source_directory(src/tq TQ_SRC) aux_source_directory(src/tsdb TSDB_SRC) aux_source_directory(src/vnd VND_SRC) + list(APPEND VNODE_SRC ${META_SRC} @@ -22,7 +39,6 @@ target_link_libraries( PUBLIC util PUBLIC common PUBLIC transport - PUBLIC bdb PUBLIC tfs PUBLIC wal PUBLIC scheduler @@ -30,6 +46,12 @@ target_link_libraries( PUBLIC qworker ) +if(${META_DB_IMPL} STREQUAL "BDB") + target_link_libraries(vnode PUBLIC bdb) +elseif(${META_DB_IMPL} STREQUAL "TDB") + target_link_libraries(vnode PUBLIC tdb) +endif() + if(${BUILD_TEST}) # add_subdirectory(test) endif(${BUILD_TEST}) From 123dc0cd119d20e12199f979d6456a3f05e7f0c3 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 9 Feb 2022 11:12:27 +0000 Subject: [PATCH 28/79] start to write meta TDB app --- source/dnode/vnode/src/meta/metaTDBImpl.c | 77 ++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index 6dea4a4e57..61c84740af 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -11,4 +11,79 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - */ \ No newline at end of file + */ + +#include "meta.h" + +#include "tdb.h" + +struct SMetaDB { + // TODO +}; + +int metaOpenDB(SMeta *pMeta) { + // TODO + return 0; +} + +void metaCloseDB(SMeta *pMeta) { + // TODO +} + +int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { + // TODO + return 0; +} + +int metaRemoveTableFromDb(SMeta *pMeta, tb_uid_t uid) { + // TODO + return 0; +} + +STbCfg *metaGetTbInfoByUid(SMeta *pMeta, tb_uid_t uid) { + // TODO + return NULL; +} + +STbCfg *metaGetTbInfoByName(SMeta *pMeta, char *tbname, tb_uid_t *uid) { + // TODO + return NULL; +} + +SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline) { + // TODO + return NULL; +} + +STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver) { + // TODO + return NULL; +} + +SMTbCursor *metaOpenTbCursor(SMeta *pMeta) { + // TODO + return NULL; +} + +void metaCloseTbCursor(SMTbCursor *pTbCur) { + // TODO +} + +char *metaTbCursorNext(SMTbCursor *pTbCur) { + // TODO + return NULL; +} + +SMCtbCursor *metaOpenCtbCursor(SMeta *pMeta, tb_uid_t uid) { + // TODO + return NULL; +} + +void metaCloseCtbCurosr(SMCtbCursor *pCtbCur) { + // TODO +} + +tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) { + // TODO + return 0; +} \ No newline at end of file From 3989a061c69c5bd3eb49c60c0d7120f29e4281d3 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 9 Feb 2022 11:40:44 +0000 Subject: [PATCH 29/79] more TDB --- source/dnode/vnode/CMakeLists.txt | 2 +- source/libs/tdb/inc/tdb.h | 9 ++++++-- source/libs/tdb/src/db/pgfile.c | 2 +- source/libs/tdb/src/db/tdb.c | 34 +++++++++++++++++++++++++------ source/libs/tdb/src/db/tdbEnv.c | 19 ++++++++++++++++- source/libs/tdb/src/inc/pgfile.h | 2 +- source/libs/tdb/src/inc/tdbEnv.h | 3 +++ 7 files changed, 59 insertions(+), 12 deletions(-) diff --git a/source/dnode/vnode/CMakeLists.txt b/source/dnode/vnode/CMakeLists.txt index bd633fa70a..429bd2143f 100644 --- a/source/dnode/vnode/CMakeLists.txt +++ b/source/dnode/vnode/CMakeLists.txt @@ -1,5 +1,5 @@ set(META_DB_IMPL_LIST "BDB" "TDB") -set(META_DB_IMPL "BDB" CACHE STRING "Use BDB as the default META implementation") +set(META_DB_IMPL "TDB" CACHE STRING "Use BDB as the default META implementation") set_property(CACHE META_DB_IMPL PROPERTY STRINGS ${META_DB_IMPL_LIST}) if(META_DB_IMPL IN_LIST META_DB_IMPL_LIST) diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 7784282a24..63da7999c9 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -22,16 +22,21 @@ extern "C" { #endif -typedef struct STDb TDB; -typedef struct STDbEnv TENV; +typedef struct STDb TDB; +typedef struct STDbEnv TENV; +typedef struct STDbCurosr TDBC; // TEVN +int tdbEnvOpen(TENV **ppEnv); +int tdbEnvClose(TENV *pEnv); // TDB int tdbCreate(TDB **ppDb); int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv); int tdbClose(TDB *pDb); +// TDBC + #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index f7d4eef799..1c3e2700b4 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -17,7 +17,7 @@ static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData); -int pgFileOpen(const char *fname, SPgCache *pPgCache, SPgFile **ppPgFile) { +int pgFileOpen(SPgFile **ppPgFile, const char *fname, SPgCache *pPgCache) { SPgFile *pPgFile; *ppPgFile = NULL; diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index b5b248eac3..ffaef0addd 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -42,8 +42,11 @@ static int tdbDestroy(TDB *pDb) { } int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { - TDB *pDb; - int ret; + TDB * pDb; + int ret; + uint8_t fileid[TDB_FILE_ID_LEN]; + SPgFile * pPgFile; + SPgCache *pPgCache; // Create DB if DB handle is not created yet if (ppDb == NULL) { @@ -56,12 +59,31 @@ int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { // Create a default ENV if pEnv is not set if (pEnv == NULL) { - // if ((ret = tenvOpen(&pEnv)) != 0) { - // return -1; - // } + if ((ret = tdbEnvOpen(&pEnv)) != 0) { + return -1; + } } - /* TODO */ + pDb->pEnv = pEnv; + + // register DB to ENV + + ASSERT(fname != NULL); + + // Check if file exists (TODO) + + // Check if the SPgFile already opened + pPgFile = tdbEnvGetPageFile(pEnv, fileid); + if (pPgFile == NULL) { + pPgCache = tdbEnvGetPgCache(pEnv); + if ((ret = pgFileOpen(&pPgFile, fname, pPgCache)) != 0) { + return -1; + } + } + + pDb->pPgFile = pPgFile; + + // open the access method (TODO) return 0; } diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index c738b691c8..302c238927 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -21,4 +21,21 @@ struct STDbEnv { struct { } pgfht; // page file hash table; SPgCache pgc; // page cache -}; \ No newline at end of file +}; + +int tdbEnvOpen(TENV **ppEnv) { + // TODO + return 0; +} + +int tdbEnvClose(TENV *pEnv) { + // TODO + return 0; +} + +SPgFile *tdbEnvGetPageFile(TENV *pEnv, const uint8_t fileid[]) { + // TODO + return NULL; +} + +SPgCache *tdbEnvGetPgCache(TENV *pEnv) { return &(pEnv->pgc); } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/pgfile.h b/source/libs/tdb/src/inc/pgfile.h index ad59fc711c..cb954ba946 100644 --- a/source/libs/tdb/src/inc/pgfile.h +++ b/source/libs/tdb/src/inc/pgfile.h @@ -30,7 +30,7 @@ struct SPgFile { pgno_t pgFileSize; }; -int pgFileOpen(const char *fname, SPgCache *pPgCache, SPgFile **ppPgFile); +int pgFileOpen(SPgFile **ppPgFile, const char *fname, SPgCache *pPgCache); int pgFileClose(SPgFile *pPgFile); SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno); diff --git a/source/libs/tdb/src/inc/tdbEnv.h b/source/libs/tdb/src/inc/tdbEnv.h index 2bd93fc530..a68ae0c7e9 100644 --- a/source/libs/tdb/src/inc/tdbEnv.h +++ b/source/libs/tdb/src/inc/tdbEnv.h @@ -20,6 +20,9 @@ extern "C" { #endif +SPgFile* tdbEnvGetPageFile(TENV* pEnv, const uint8_t fileid[]); +SPgCache* tdbEnvGetPgCache(TENV* pEnv); + #ifdef __cplusplus } #endif From e0e9f088c4e9daf3709c1fb3ffc1a44db4cb3ac6 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 9 Feb 2022 15:02:38 +0000 Subject: [PATCH 30/79] more TDB --- source/dnode/vnode/src/meta/metaTDBImpl.c | 23 ++++++++++++++++++++--- source/libs/tdb/inc/tdb.h | 1 + source/libs/tdb/src/db/tdbEnv.c | 18 ++++++++++++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index 61c84740af..56dbc9efc3 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -13,16 +13,33 @@ * along with this program. If not, see . */ -#include "meta.h" +#include "metaDef.h" #include "tdb.h" struct SMetaDB { - // TODO + TENV *pEnv; + TDB * pTbDB; + TDB * pSchemaDB; + TDB * pNameIdx; + TDB * pStbIdx; + TDB * pNtbIdx; + TDB * pCtbIdx; + // tag index hash table + // suid+colid --> TDB * + struct { + } tagIdxHt; }; int metaOpenDB(SMeta *pMeta) { - // TODO + SMetaDB *pDb; + + pDb = (SMetaDB *)calloc(1, sizeof(*pDb)); + if (pDb == NULL) { + return -1; + } + + pMeta->pDB = pDb; return 0; } diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 63da7999c9..5bbaf2ac88 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -27,6 +27,7 @@ typedef struct STDbEnv TENV; typedef struct STDbCurosr TDBC; // TEVN +int tdbEnvCreate(TENV **ppEnv); int tdbEnvOpen(TENV **ppEnv); int tdbEnvClose(TENV *pEnv); diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 302c238927..03eb0f82ed 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -23,13 +23,22 @@ struct STDbEnv { SPgCache pgc; // page cache }; +static int tdbEnvDestroy(TENV *pEnv); + +int tdbEnvCreate(TENV **ppEnv) { + // TODO + return 0; +} + int tdbEnvOpen(TENV **ppEnv) { // TODO return 0; } int tdbEnvClose(TENV *pEnv) { - // TODO + if (pEnv == NULL) return 0; + /* TODO */ + tdbEnvDestroy(pEnv); return 0; } @@ -38,4 +47,9 @@ SPgFile *tdbEnvGetPageFile(TENV *pEnv, const uint8_t fileid[]) { return NULL; } -SPgCache *tdbEnvGetPgCache(TENV *pEnv) { return &(pEnv->pgc); } \ No newline at end of file +SPgCache *tdbEnvGetPgCache(TENV *pEnv) { return &(pEnv->pgc); } + +static int tdbEnvDestroy(TENV *pEnv) { + // TODO + return 0; +} \ No newline at end of file From 5dca9744df7755ff3a51f82d1f59253e026f8964 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 10 Feb 2022 03:14:53 +0000 Subject: [PATCH 31/79] more TDB --- source/dnode/vnode/src/meta/metaTDBImpl.c | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index 56dbc9efc3..3a5b55925b 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -31,16 +31,50 @@ struct SMetaDB { } tagIdxHt; }; +#define A(op, flag) \ + do { \ + if ((ret = op) != 0) goto flag; \ + } while (0) + int metaOpenDB(SMeta *pMeta) { SMetaDB *pDb; + TENV * pEnv; + TDB * pTbDB; + TDB * pSchemaDB; + TDB * pNameIdx; + TDB * pStbIdx; + TDB * pNtbIdx; + TDB * pCtbIdx; + int ret; pDb = (SMetaDB *)calloc(1, sizeof(*pDb)); if (pDb == NULL) { return -1; } + // Create and open the ENV + A((tdbEnvCreate(&pEnv)), _err); + A((tdbEnvOpen(&pEnv)), _err); + + // Create and open each DB + A(tdbCreate(&pTbDB), _err); + A(tdbOpen(&pTbDB, "table.db", NULL, pEnv), _err); + + A(tdbCreate(&pSchemaDB), _err); + A(tdbOpen(&pSchemaDB, "schema.db", NULL, pEnv), _err); + + A(tdbCreate(&pNameIdx), _err); + A(tdbOpen(&pNameIdx, "name.db", NULL, pEnv), _err); + // tdbAssociate(); + + pDb->pEnv = pEnv; + pDb->pTbDB = pTbDB; + pDb->pSchemaDB = pSchemaDB; pMeta->pDB = pDb; return 0; + +_err: + return -1; } void metaCloseDB(SMeta *pMeta) { From b34752fb070bb015d0fb9eaad5308a632fc78ab0 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 10 Feb 2022 03:50:02 +0000 Subject: [PATCH 32/79] more TDB --- source/libs/tdb/src/db/tdbEnv.c | 42 +++++++++++++++++++++++++++----- source/libs/tdb/src/inc/tdbInt.h | 18 ++++++++++++++ 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 03eb0f82ed..3d7e98f477 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -16,23 +16,53 @@ #include "tdbInt.h" struct STDbEnv { - TDB * dbList; // TDB list - SPgFile *pgFileList; // SPgFile list + pgsize_t pgSize; // Page size + cachesz_t cacheSize; // Total cache size + STDbList dbList; // TDB List + SPgFileList pgfList; // SPgFile List + SPgCache * pPgCache; // page cache struct { - } pgfht; // page file hash table; - SPgCache pgc; // page cache + } pgfht; // page file hash table; }; static int tdbEnvDestroy(TENV *pEnv); int tdbEnvCreate(TENV **ppEnv) { + TENV *pEnv; + + pEnv = (TENV *)calloc(1, sizeof(*pEnv)); + if (pEnv == NULL) { + return -1; + } + + pEnv->pgSize = TDB_DEFAULT_PGSIZE; + pEnv->cacheSize = TDB_DEFAULT_CACHE_SIZE; + + TD_DLIST_INIT(&(pEnv->dbList)); + TD_DLIST_INIT(&(pEnv->pgfList)); // TODO return 0; } int tdbEnvOpen(TENV **ppEnv) { - // TODO + TENV * pEnv; + SPgCache *pPgCache; + int ret; + + // Create the ENV with default setting + if (ppEnv == NULL) { + TERR_A(ret, tdbEnvCreate(&pEnv), _err); + } + + pEnv = *ppEnv; + + TERR_A(ret, pgCacheCreate(&pPgCache, pEnv->pgSize, pEnv->cacheSize / pEnv->pgSize), _err); + pEnv->pPgCache = pPgCache; + return 0; + +_err: + return -1; } int tdbEnvClose(TENV *pEnv) { @@ -47,7 +77,7 @@ SPgFile *tdbEnvGetPageFile(TENV *pEnv, const uint8_t fileid[]) { return NULL; } -SPgCache *tdbEnvGetPgCache(TENV *pEnv) { return &(pEnv->pgc); } +SPgCache *tdbEnvGetPgCache(TENV *pEnv) { return pEnv->pPgCache; } static int tdbEnvDestroy(TENV *pEnv) { // TODO diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index c368b68465..1500fee6dc 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -71,11 +71,29 @@ typedef int32_t pgsize_t; #define TDB_IS_PGSIZE_VLD(s) (((s) >= TDB_MIN_PGSIZE) && ((s) <= TDB_MAX_PGSIZE)) // cache +typedef int32_t cachesz_t; #define TDB_DEFAULT_CACHE_SIZE (256 * 1024) // 256K // tdb_log #define tdbError(var) +typedef TD_DLIST(STDb) STDbList; +typedef TD_DLIST(SPgFile) SPgFileList; + +#define TERR_A(val, op, flag) \ + do { \ + if (((val) = (op)) != 0) { \ + goto flag; \ + } \ + } while (0) + +#define TERR_B(val, op, flag) \ + do { \ + if (((val) = (op)) == NULL) { \ + goto flag; \ + } \ + } while (0) + #include "btree.h" #include "pgcache.h" #include "pgfile.h" From 2849a62ced26b6c2479452709c253541837340da Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 10 Feb 2022 06:36:24 +0000 Subject: [PATCH 33/79] more TDB --- source/dnode/vnode/src/meta/metaTDBImpl.c | 5 +++++ source/libs/tdb/inc/tdb.h | 8 ++++++++ source/libs/tdb/src/db/pgfile.c | 2 +- source/libs/tdb/src/db/tdb.c | 7 ++++++- source/libs/tdb/src/db/tdbEnv.c | 18 ++++++++++++++++++ source/libs/tdb/src/db/tdbUtil.c | 23 +++++++++++++++++++++-- source/libs/tdb/src/db/tdb_mpool.c | 2 +- source/libs/tdb/src/inc/tdbInt.h | 3 --- source/libs/tdb/src/inc/tdbUtil.h | 7 ++++++- 9 files changed, 66 insertions(+), 9 deletions(-) diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index 3a5b55925b..4a65cf277b 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -54,6 +54,11 @@ int metaOpenDB(SMeta *pMeta) { // Create and open the ENV A((tdbEnvCreate(&pEnv)), _err); +#if 0 + // Set options of the environment + A(tdbEnvSetPageSize(pEnv, 8192), _err); + A(tdbEnvSetCacheSize(pEnv, 16 * 1024 * 1024), _err); +#endif A((tdbEnvOpen(&pEnv)), _err); // Create and open each DB diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 5bbaf2ac88..9a168c5311 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -26,11 +26,19 @@ typedef struct STDb TDB; typedef struct STDbEnv TENV; typedef struct STDbCurosr TDBC; +typedef int32_t pgsize_t; +typedef int32_t cachesz_t; + // TEVN int tdbEnvCreate(TENV **ppEnv); int tdbEnvOpen(TENV **ppEnv); int tdbEnvClose(TENV *pEnv); +int tdbEnvSetPageSize(TENV *pEnv, pgsize_t szPage); +int tdbEnvSetCacheSize(TENV *pEnv, cachesz_t szCache); +pgsize_t tdbEnvGetPageSize(TENV *pEnv); +cachesz_t tdbEnvGetCacheSize(TENV *pEnv); + // TDB int tdbCreate(TDB **ppDb); int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv); diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index 1c3e2700b4..7f0ab55b00 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -44,7 +44,7 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, SPgCache *pPgCache) { return -1; } - if (tdbGnrtFileID(fname, pPgFile->fileid) < 0) { + if (tdbGnrtFileID(fname, pPgFile->fileid, false) < 0) { pgFileClose(pPgFile); return -1; } diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index ffaef0addd..bd8d7ec1f7 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -70,7 +70,12 @@ int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { ASSERT(fname != NULL); - // Check if file exists (TODO) + // Check if file exists + if (tdbCheckFileAccess(fname, TDB_F_OK) != 0) { + if (1) { + // create the file + } + } // Check if the SPgFile already opened pPgFile = tdbEnvGetPageFile(pEnv, fileid); diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 3d7e98f477..fe9238e1eb 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -57,6 +57,8 @@ int tdbEnvOpen(TENV **ppEnv) { pEnv = *ppEnv; TERR_A(ret, pgCacheCreate(&pPgCache, pEnv->pgSize, pEnv->cacheSize / pEnv->pgSize), _err); + TERR_A(ret, pgCacheOpen(&pPgCache), _err); + pEnv->pPgCache = pPgCache; return 0; @@ -72,6 +74,22 @@ int tdbEnvClose(TENV *pEnv) { return 0; } +int tdbEnvSetPageSize(TENV *pEnv, pgsize_t szPage) { + /* TODO */ + pEnv->pgSize = szPage; + return 0; +} + +int tdbEnvSetCacheSize(TENV *pEnv, cachesz_t szCache) { + /* TODO */ + pEnv->cacheSize = szCache; + return 0; +} + +pgsize_t tdbEnvGetPageSize(TENV *pEnv) { return pEnv->pgSize; } + +cachesz_t tdbEnvGetCacheSize(TENV *pEnv) { return pEnv->cacheSize; } + SPgFile *tdbEnvGetPageFile(TENV *pEnv, const uint8_t fileid[]) { // TODO return NULL; diff --git a/source/libs/tdb/src/db/tdbUtil.c b/source/libs/tdb/src/db/tdbUtil.c index 6391397efd..856a54a2da 100644 --- a/source/libs/tdb/src/db/tdbUtil.c +++ b/source/libs/tdb/src/db/tdbUtil.c @@ -15,7 +15,7 @@ #include "tdbInt.h" -int tdbGnrtFileID(const char *fname, uint8_t *fileid) { +int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique) { struct stat statbuf; if (stat(fname, &statbuf) < 0) { @@ -26,8 +26,27 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid) { ((uint64_t *)fileid)[0] = (uint64_t)statbuf.st_ino; ((uint64_t *)fileid)[1] = (uint64_t)statbuf.st_dev; - ((uint64_t *)fileid)[2] = rand(); + if (unique) { + ((uint64_t *)fileid)[2] = rand(); + } return 0; } +int tdbCheckFileAccess(const char *pathname, int mode) { + int flags = 0; + + if (mode & TDB_F_OK) { + flags |= F_OK; + } + + if (mode & TDB_R_OK) { + flags |= R_OK; + } + + if (mode & TDB_W_OK) { + flags |= W_OK; + } + + return access(pathname, flags); +} \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdb_mpool.c b/source/libs/tdb/src/db/tdb_mpool.c index 1b591a5c3f..cc7927d51b 100644 --- a/source/libs/tdb/src/db/tdb_mpool.c +++ b/source/libs/tdb/src/db/tdb_mpool.c @@ -119,7 +119,7 @@ int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) { goto _err; } - if (tdbGnrtFileID(fname, mpf->fileid) < 0) { + if (tdbGnrtFileID(fname, mpf->fileid, false) < 0) { goto _err; } diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 1500fee6dc..74fed019e7 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -16,7 +16,6 @@ #ifndef _TD_TDB_INTERNAL_H_ #define _TD_TDB_INTERNAL_H_ -#include "os.h" #include "tlist.h" #include "tlockfree.h" @@ -64,14 +63,12 @@ static FORCE_INLINE int tdbCmprPgId(const void *p1, const void *p2) { typedef int32_t frame_id_t; // pgsize_t -typedef int32_t pgsize_t; #define TDB_MIN_PGSIZE 512 #define TDB_MAX_PGSIZE 16384 #define TDB_DEFAULT_PGSIZE 4096 #define TDB_IS_PGSIZE_VLD(s) (((s) >= TDB_MIN_PGSIZE) && ((s) <= TDB_MAX_PGSIZE)) // cache -typedef int32_t cachesz_t; #define TDB_DEFAULT_CACHE_SIZE (256 * 1024) // 256K // tdb_log diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 6ffb9dd3d4..8e1fe013e8 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -22,7 +22,12 @@ extern "C" { #define TDB_ROUND8(x) (((x) + 7) & ~7) -int tdbGnrtFileID(const char *fname, uint8_t *fileid); +int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique); + +#define TDB_F_OK 0x1 +#define TDB_R_OK 0x2 +#define TDB_W_OK 0x4 +int tdbCheckFileAccess(const char *pathname, int mode); #ifdef __cplusplus } From 0a372a735909d05c9b03f0ceb6339eda8d822949 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 10 Feb 2022 07:08:26 +0000 Subject: [PATCH 34/79] more TDB --- source/libs/tdb/src/db/btree.c | 33 +++++++++++++++++++++++++++++++- source/libs/tdb/src/db/tdb.c | 9 ++++++++- source/libs/tdb/src/inc/btree.h | 2 +- source/libs/tdb/src/inc/pgfile.h | 1 - source/libs/tdb/src/inc/tdbInt.h | 2 ++ 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index 746ca6a99a..a37711e399 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -17,4 +17,35 @@ struct SBtCursor { // TODO -}; \ No newline at end of file +}; + +static int btreeCreate(SBTree **pBt); +static int btreeDestroy(SBTree *pBt); + +int btreeOpen(SBTree **ppBt, SPgFile *pPgFile) { + SBTree *pBt; + int ret; + + ret = btreeCreate(&pBt); + if (ret != 0) { + return -1; + } + + *ppBt = pBt; + return 0; +} + +int btreeClose(SBTree *pBt) { + // TODO + return 0; +} + +static int btreeCreate(SBTree **pBt) { + // TODO + return 0; +} + +static int btreeDestroy(SBTree *pBt) { + // TODO + return 0; +} \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index bd8d7ec1f7..2e2e772336 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -16,7 +16,7 @@ #include "tdbInt.h" struct STDb { - SBTree btree; // current access method + SBTree * pBt; // current access method SPgFile *pPgFile; // backend page file this DB is using TENV * pEnv; // TENV containing the DB }; @@ -47,6 +47,7 @@ int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { uint8_t fileid[TDB_FILE_ID_LEN]; SPgFile * pPgFile; SPgCache *pPgCache; + SBTree * pBt; // Create DB if DB handle is not created yet if (ppDb == NULL) { @@ -78,6 +79,7 @@ int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { } // Check if the SPgFile already opened + tdbGnrtFileID(fname, fileid, false); pPgFile = tdbEnvGetPageFile(pEnv, fileid); if (pPgFile == NULL) { pPgCache = tdbEnvGetPgCache(pEnv); @@ -89,6 +91,11 @@ int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { pDb->pPgFile = pPgFile; // open the access method (TODO) + if (btreeOpen(&pBt, pPgFile) != 0) { + return -1; + } + + pDb->pBt = pBt; return 0; } diff --git a/source/libs/tdb/src/inc/btree.h b/source/libs/tdb/src/inc/btree.h index 2aa2f4528f..ec73f3651b 100644 --- a/source/libs/tdb/src/inc/btree.h +++ b/source/libs/tdb/src/inc/btree.h @@ -24,7 +24,7 @@ typedef struct SBTree SBTree; typedef struct SBtCursor SBtCursor; // SBTree -int btreeOpen(SBTree **ppBt); +int btreeOpen(SBTree **ppBt, SPgFile *pPgFile); int btreeClose(SBTree *pBt); // SBtCursor diff --git a/source/libs/tdb/src/inc/pgfile.h b/source/libs/tdb/src/inc/pgfile.h index cb954ba946..f248f3c953 100644 --- a/source/libs/tdb/src/inc/pgfile.h +++ b/source/libs/tdb/src/inc/pgfile.h @@ -20,7 +20,6 @@ extern "C" { #endif -typedef struct SPgFile SPgFile; struct SPgFile { char * fname; // backend file name uint8_t fileid[TDB_FILE_ID_LEN]; // file id diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 74fed019e7..879a4f4f66 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -25,6 +25,8 @@ extern "C" { #endif +typedef struct SPgFile SPgFile; + // pgno_t typedef int32_t pgno_t; #define TDB_IVLD_PGNO ((pgno_t)-1) From fa72df133e720e07cb54c541d0e549d7bd465d6a Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 10 Feb 2022 09:57:53 +0000 Subject: [PATCH 35/79] more TDB --- source/libs/tdb/src/db/btree.c | 42 ++++++++++++++++++++++++++++++- source/libs/tdb/src/db/tdbUtil.c | 5 ++++ source/libs/tdb/src/inc/tdbUtil.h | 2 ++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index a37711e399..53f2fa6c77 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -16,11 +16,14 @@ #include "tdbInt.h" struct SBtCursor { - // TODO + SBTree *pBtree; + pgno_t pgno; + SPage * pPage; // current page traversing }; static int btreeCreate(SBTree **pBt); static int btreeDestroy(SBTree *pBt); +static int btreeCursorMoveToChild(SBtCursor *pBtCur, pgno_t pgno); int btreeOpen(SBTree **ppBt, SPgFile *pPgFile) { SBTree *pBt; @@ -48,4 +51,41 @@ static int btreeCreate(SBTree **pBt) { static int btreeDestroy(SBTree *pBt) { // TODO return 0; +} + +int btreeCursorOpen(SBtCursor *pBtCur, SBTree *pBt) { + // TODO + return 0; +} + +int btreeCursorClose(SBtCursor *pBtCur) { + // TODO + return 0; +} + +int btreeCursorMoveTo(SBtCursor *pBtCur) { + SPage *pPage; + pgno_t childPgno; + + // 1. Move the cursor to the root page + + // 2. Loop to search over the whole tree + for (;;) { + pPage = pBtCur->pPage; + + // Loop to search in current page + for (;;) { + /* code */ + } + + btreeCursorMoveToChild(pBtCur, childPgno); + } + + return 0; +} + +static int btreeCursorMoveToChild(SBtCursor *pBtCur, pgno_t pgno) { + SPgFile *pPgFile; + // TODO + return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdbUtil.c b/source/libs/tdb/src/db/tdbUtil.c index 856a54a2da..591e7eedd4 100644 --- a/source/libs/tdb/src/db/tdbUtil.c +++ b/source/libs/tdb/src/db/tdbUtil.c @@ -49,4 +49,9 @@ int tdbCheckFileAccess(const char *pathname, int mode) { } return access(pathname, flags); +} + +int64_t tdbGetFileSize(const char *fname) { + // TODO + return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 8e1fe013e8..ccd3d0b793 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -29,6 +29,8 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique); #define TDB_W_OK 0x4 int tdbCheckFileAccess(const char *pathname, int mode); +int64_t tdbGetFileSize(const char *fname); + #ifdef __cplusplus } #endif From d83540ae744750988981f4d6dcbbda72c6e51154 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 11 Feb 2022 01:30:26 +0000 Subject: [PATCH 36/79] more TDB --- source/libs/tdb/src/db/btree.c | 8 +++++++- source/libs/tdb/src/inc/btree.h | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index 53f2fa6c77..8e308b63b9 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -21,6 +21,11 @@ struct SBtCursor { SPage * pPage; // current page traversing }; +typedef struct { + pgno_t pgno; + pgsize_t offset; +} SBtIdx; + static int btreeCreate(SBTree **pBt); static int btreeDestroy(SBTree *pBt); static int btreeCursorMoveToChild(SBtCursor *pBtCur, pgno_t pgno); @@ -63,9 +68,10 @@ int btreeCursorClose(SBtCursor *pBtCur) { return 0; } -int btreeCursorMoveTo(SBtCursor *pBtCur) { +int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey) { SPage *pPage; pgno_t childPgno; + int idx; // 1. Move the cursor to the root page diff --git a/source/libs/tdb/src/inc/btree.h b/source/libs/tdb/src/inc/btree.h index ec73f3651b..343093edc9 100644 --- a/source/libs/tdb/src/inc/btree.h +++ b/source/libs/tdb/src/inc/btree.h @@ -30,7 +30,7 @@ int btreeClose(SBTree *pBt); // SBtCursor int btreeCursorOpen(SBtCursor *pBtCur, SBTree *pBt); int btreeCursorClose(SBtCursor *pBtCur); -int btreeCursorMoveTo(SBtCursor *pBtCur); +int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey); struct SBTree { pgno_t root; From a10e0649a0e38d74e7ff104e899abf1febbfd8b5 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 11 Feb 2022 02:04:19 +0000 Subject: [PATCH 37/79] refact TDB --- source/libs/tdb/inc/tdb.h | 6 +++--- source/libs/tdb/src/db/btree.c | 4 ++-- source/libs/tdb/src/db/pgcache.c | 2 +- source/libs/tdb/src/db/pgfile.c | 2 +- source/libs/tdb/src/db/tdbEnv.c | 6 +++--- source/libs/tdb/src/db/tdb_mpool.c | 6 +++--- source/libs/tdb/src/inc/pgcache.h | 6 +++--- source/libs/tdb/src/inc/pgfile.h | 2 +- source/libs/tdb/src/inc/tdbInt.h | 4 ++-- source/libs/tdb/src/inc/tdb_mpool.h | 4 ++-- 10 files changed, 21 insertions(+), 21 deletions(-) diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 9a168c5311..daffad3b79 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -26,7 +26,7 @@ typedef struct STDb TDB; typedef struct STDbEnv TENV; typedef struct STDbCurosr TDBC; -typedef int32_t pgsize_t; +typedef int32_t pgsz_t; typedef int32_t cachesz_t; // TEVN @@ -34,9 +34,9 @@ int tdbEnvCreate(TENV **ppEnv); int tdbEnvOpen(TENV **ppEnv); int tdbEnvClose(TENV *pEnv); -int tdbEnvSetPageSize(TENV *pEnv, pgsize_t szPage); +int tdbEnvSetPageSize(TENV *pEnv, pgsz_t szPage); int tdbEnvSetCacheSize(TENV *pEnv, cachesz_t szCache); -pgsize_t tdbEnvGetPageSize(TENV *pEnv); +pgsz_t tdbEnvGetPageSize(TENV *pEnv); cachesz_t tdbEnvGetCacheSize(TENV *pEnv); // TDB diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index 8e308b63b9..400d91514d 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -22,8 +22,8 @@ struct SBtCursor { }; typedef struct { - pgno_t pgno; - pgsize_t offset; + pgno_t pgno; + pgsz_t offset; } SBtIdx; static int btreeCreate(SBTree **pBt); diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index b0c4406a3c..9e66a5a960 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -17,7 +17,7 @@ static void pgCachePinPage(SPage *pPage); static void pgCacheUnpinPage(SPage *pPage); -int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgSize, int32_t npage) { +int pgCacheCreate(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage) { SPgCache *pPgCache; SPage * pPage; diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index 7f0ab55b00..c03303da38 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -107,7 +107,7 @@ int pgFileWrite(SPage *pPage) { } static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) { - pgsize_t pgSize; + pgsz_t pgSize; ssize_t rsize; uint8_t *pTData; size_t szToRead; diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index fe9238e1eb..cbc1ef3417 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -16,7 +16,7 @@ #include "tdbInt.h" struct STDbEnv { - pgsize_t pgSize; // Page size + pgsz_t pgSize; // Page size cachesz_t cacheSize; // Total cache size STDbList dbList; // TDB List SPgFileList pgfList; // SPgFile List @@ -74,7 +74,7 @@ int tdbEnvClose(TENV *pEnv) { return 0; } -int tdbEnvSetPageSize(TENV *pEnv, pgsize_t szPage) { +int tdbEnvSetPageSize(TENV *pEnv, pgsz_t szPage) { /* TODO */ pEnv->pgSize = szPage; return 0; @@ -86,7 +86,7 @@ int tdbEnvSetCacheSize(TENV *pEnv, cachesz_t szCache) { return 0; } -pgsize_t tdbEnvGetPageSize(TENV *pEnv) { return pEnv->pgSize; } +pgsz_t tdbEnvGetPageSize(TENV *pEnv) { return pEnv->pgSize; } cachesz_t tdbEnvGetCacheSize(TENV *pEnv) { return pEnv->cacheSize; } diff --git a/source/libs/tdb/src/db/tdb_mpool.c b/source/libs/tdb/src/db/tdb_mpool.c index cc7927d51b..2049019970 100644 --- a/source/libs/tdb/src/db/tdb_mpool.c +++ b/source/libs/tdb/src/db/tdb_mpool.c @@ -22,7 +22,7 @@ static int tdbMPoolFileReadPage(TDB_MPFILE *mpf, pgno_t pgno, void *p); static int tdbMPoolFileWritePage(TDB_MPFILE *mpf, pgno_t pgno, const void *p); static void tdbMPoolClockEvictPage(TDB_MPOOL *mp, pg_t **pagepp); -int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) { +int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsz_t pgsize) { TDB_MPOOL *mp = NULL; size_t tsize; pg_t * pagep; @@ -300,7 +300,7 @@ static void tdbMPoolUnregFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) { } static int tdbMPoolFileReadPage(TDB_MPFILE *mpf, pgno_t pgno, void *p) { - pgsize_t pgsize; + pgsz_t pgsize; TDB_MPOOL *mp; off_t offset; size_t rsize; @@ -317,7 +317,7 @@ static int tdbMPoolFileReadPage(TDB_MPFILE *mpf, pgno_t pgno, void *p) { } static int tdbMPoolFileWritePage(TDB_MPFILE *mpf, pgno_t pgno, const void *p) { - pgsize_t pgsize; + pgsz_t pgsize; TDB_MPOOL *mp; off_t offset; diff --git a/source/libs/tdb/src/inc/pgcache.h b/source/libs/tdb/src/inc/pgcache.h index 9296e0bea1..4cea86dc6d 100644 --- a/source/libs/tdb/src/inc/pgcache.h +++ b/source/libs/tdb/src/inc/pgcache.h @@ -24,7 +24,7 @@ typedef struct SPgCache SPgCache; typedef struct SPage SPage; // SPgCache -int pgCacheCreate(SPgCache **ppPgCache, pgsize_t pgSize, int32_t npage); +int pgCacheCreate(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage); int pgCacheDestroy(SPgCache *pPgCache); int pgCacheOpen(SPgCache **ppPgCache); int pgCacheClose(SPgCache *pPgCache); @@ -46,9 +46,9 @@ struct SPage { typedef TD_DLIST(SPage) SPgList; struct SPgCache { - TENV * pEnv; // TENV containing this page cache + TENV * pEnv; // TENV containing this page cache SRWLatch mutex; - pgsize_t pgsize; + pgsz_t pgsize; int32_t npage; SPage * pages; SPgList freeList; diff --git a/source/libs/tdb/src/inc/pgfile.h b/source/libs/tdb/src/inc/pgfile.h index f248f3c953..f2dba417f6 100644 --- a/source/libs/tdb/src/inc/pgfile.h +++ b/source/libs/tdb/src/inc/pgfile.h @@ -24,7 +24,7 @@ struct SPgFile { char * fname; // backend file name uint8_t fileid[TDB_FILE_ID_LEN]; // file id SPgCache *pPgCache; // page cache underline - pgsize_t pgSize; + pgsz_t pgSize; int fd; pgno_t pgFileSize; }; diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 879a4f4f66..5b95e16203 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -64,9 +64,9 @@ static FORCE_INLINE int tdbCmprPgId(const void *p1, const void *p2) { // framd_id_t typedef int32_t frame_id_t; -// pgsize_t +// pgsz_t #define TDB_MIN_PGSIZE 512 -#define TDB_MAX_PGSIZE 16384 +#define TDB_MAX_PGSIZE 65536 #define TDB_DEFAULT_PGSIZE 4096 #define TDB_IS_PGSIZE_VLD(s) (((s) >= TDB_MIN_PGSIZE) && ((s) <= TDB_MAX_PGSIZE)) diff --git a/source/libs/tdb/src/inc/tdb_mpool.h b/source/libs/tdb/src/inc/tdb_mpool.h index 55754484c0..ba5d5f132e 100644 --- a/source/libs/tdb/src/inc/tdb_mpool.h +++ b/source/libs/tdb/src/inc/tdb_mpool.h @@ -46,7 +46,7 @@ typedef struct { } mpf_bucket_t; struct TDB_MPOOL { int64_t cachesize; - pgsize_t pgsize; + pgsz_t pgsize; int32_t npages; pg_t * pages; pg_list_t freeList; @@ -74,7 +74,7 @@ struct TDB_MPFILE { /*=================================================== Exposed apis ==================================================*/ // TDB_MPOOL -int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize); +int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsz_t pgsize); int tdbMPoolClose(TDB_MPOOL *mp); int tdbMPoolSync(TDB_MPOOL *mp); From 49edc62e6dd2a8715a9d6728c246475c95c6d755 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 11 Feb 2022 06:33:09 +0000 Subject: [PATCH 38/79] more TDB --- source/libs/tdb/src/db/btree.c | 15 ++++++++++++--- source/libs/tdb/src/inc/pgfile.h | 12 ++++++++++++ source/libs/tdb/src/inc/tdbInt.h | 12 ++++++++---- source/libs/tdb/src/inc/tdbUtil.h | 6 ++++++ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index 400d91514d..3174bfa0b9 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -26,6 +26,14 @@ typedef struct { pgsz_t offset; } SBtIdx; +// Btree page header definition +typedef struct { + uint8_t flags; + uint16_t ncells; + pgsz_t pldOffset; // payload offset + /* TODO */ +} SBtPgHdr; + static int btreeCreate(SBTree **pBt); static int btreeDestroy(SBTree *pBt); static int btreeCursorMoveToChild(SBtCursor *pBtCur, pgno_t pgno); @@ -69,9 +77,10 @@ int btreeCursorClose(SBtCursor *pBtCur) { } int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey) { - SPage *pPage; - pgno_t childPgno; - int idx; + SPage * pPage; + SBtPgHdr *pBtPgHdr; + pgno_t childPgno; + int idx; // 1. Move the cursor to the root page diff --git a/source/libs/tdb/src/inc/pgfile.h b/source/libs/tdb/src/inc/pgfile.h index f2dba417f6..5a42199c53 100644 --- a/source/libs/tdb/src/inc/pgfile.h +++ b/source/libs/tdb/src/inc/pgfile.h @@ -20,6 +20,18 @@ extern "C" { #endif +typedef struct __attribute__((__packed__)) { + char hdrInfo[16]; // info string + pgsz_t szPage; // page size of current file + int32_t cno; // commit number counter + pgno_t freePgno; // freelist page number + uint8_t resv[100]; // reserved space +} SPgFileHdr; + +#define TDB_PG_FILE_HDR_SIZE 128 + +TD_STATIC_ASSERT(sizeof(SPgFileHdr) == TDB_PG_FILE_HDR_SIZE, "Page file header size if not 128"); + struct SPgFile { char * fname; // backend file name uint8_t fileid[TDB_FILE_ID_LEN]; // file id diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 5b95e16203..2f9cd5af4c 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -93,12 +93,16 @@ typedef TD_DLIST(SPgFile) SPgFileList; } \ } while (0) -#include "btree.h" -#include "pgcache.h" -#include "pgfile.h" -#include "tdbEnv.h" #include "tdbUtil.h" +#include "btree.h" + +#include "pgcache.h" + +#include "pgfile.h" + +#include "tdbEnv.h" + #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index ccd3d0b793..2eeda286ec 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -20,6 +20,12 @@ extern "C" { #endif +#if __STDC_VERSION__ >= 201112L +#define TD_STATIC_ASSERT(op, info) static_assert(op, info) +#else +#define TD_STATIC_ASSERT(op, info) +#endif + #define TDB_ROUND8(x) (((x) + 7) & ~7) int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique); From e19fdafda7c14ad05b526ce193b466c056b07f32 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 11 Feb 2022 07:24:14 +0000 Subject: [PATCH 39/79] more TDB --- source/libs/tdb/src/db/btree.c | 64 ++++++++++++++++++++++++++----- source/libs/tdb/src/inc/pgfile.h | 2 +- source/libs/tdb/src/inc/tdbInt.h | 2 +- source/libs/tdb/src/inc/tdbUtil.h | 4 +- 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index 3174bfa0b9..3ed6667c92 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -27,13 +27,19 @@ typedef struct { } SBtIdx; // Btree page header definition -typedef struct { +typedef struct __attribute__((__packed__)) { uint8_t flags; uint16_t ncells; pgsz_t pldOffset; // payload offset /* TODO */ } SBtPgHdr; +typedef int (*BtreeCmprFn)(const void *, const void *); + +#define BTREE_PAGE_HDR(pPage) NULL /* TODO */ +#define BTREE_PAGE_PAYLOAD_AT(pPage, idx) NULL /*TODO*/ +#define BTREE_PAGE_IS_LEAF(pPage) 0 /* TODO */ + static int btreeCreate(SBTree **pBt); static int btreeDestroy(SBTree *pBt); static int btreeCursorMoveToChild(SBtCursor *pBtCur, pgno_t pgno); @@ -77,23 +83,63 @@ int btreeCursorClose(SBtCursor *pBtCur) { } int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey) { - SPage * pPage; - SBtPgHdr *pBtPgHdr; - pgno_t childPgno; - int idx; + SPage * pPage; + SBtPgHdr * pBtPgHdr; + SPgFile * pPgFile; + pgno_t childPgno; + pgno_t rootPgno; + int nPayload; + void * pPayload; + BtreeCmprFn cmpFn; // 1. Move the cursor to the root page + if (rootPgno == TDB_IVLD_PGNO) { + // No any data in this btree, just return not found (TODO) + return 0; + } else { + // Load the page from the file by the SPgFile handle + pPage = pgFileFetch(pPgFile, rootPgno); + + pBtCur->pPage = pPage; + } // 2. Loop to search over the whole tree for (;;) { + int lidx, ridx, midx, cret; + pPage = pBtCur->pPage; + pBtPgHdr = BTREE_PAGE_HDR(pPage); + nPayload = pBtPgHdr->ncells; - // Loop to search in current page + // Binary search the page + lidx = 0; + ridx = nPayload - 1; + midx = (lidx + ridx) >> 1; for (;;) { - /* code */ - } + // get the payload ptr at midx + pPayload = BTREE_PAGE_PAYLOAD_AT(pPage, midx); - btreeCursorMoveToChild(pBtCur, childPgno); + // the payload and the key + cret = cmpFn(pKey, pPayload); + + if (cret < 0) { + /* TODO */ + } else if (cret > 0) { + /* TODO */ + } else { + /* TODO */ + } + + if (lidx > ridx) break; + midx = (lidx + ridx) >> 1; + } + if (BTREE_PAGE_IS_LEAF(pPage)) { + /* TODO */ + break; + } else { + /* TODO */ + btreeCursorMoveToChild(pBtCur, childPgno); + } } return 0; diff --git a/source/libs/tdb/src/inc/pgfile.h b/source/libs/tdb/src/inc/pgfile.h index 5a42199c53..67d81ffbb1 100644 --- a/source/libs/tdb/src/inc/pgfile.h +++ b/source/libs/tdb/src/inc/pgfile.h @@ -30,7 +30,7 @@ typedef struct __attribute__((__packed__)) { #define TDB_PG_FILE_HDR_SIZE 128 -TD_STATIC_ASSERT(sizeof(SPgFileHdr) == TDB_PG_FILE_HDR_SIZE, "Page file header size if not 128"); +TDB_STATIC_ASSERT(sizeof(SPgFileHdr) == TDB_PG_FILE_HDR_SIZE, "Page file header size if not 128"); struct SPgFile { char * fname; // backend file name diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 2f9cd5af4c..5c532c23f2 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -29,7 +29,7 @@ typedef struct SPgFile SPgFile; // pgno_t typedef int32_t pgno_t; -#define TDB_IVLD_PGNO ((pgno_t)-1) +#define TDB_IVLD_PGNO ((pgno_t)0) // fileid #define TDB_FILE_ID_LEN 24 diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 2eeda286ec..f3e00a5ba5 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -21,9 +21,9 @@ extern "C" { #endif #if __STDC_VERSION__ >= 201112L -#define TD_STATIC_ASSERT(op, info) static_assert(op, info) +#define TDB_STATIC_ASSERT(op, info) static_assert(op, info) #else -#define TD_STATIC_ASSERT(op, info) +#define TDB_STATIC_ASSERT(op, info) #endif #define TDB_ROUND8(x) (((x) + 7) & ~7) From 98a1762aba041d6532bd10d8d544dae340c944bb Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 11 Feb 2022 09:11:30 +0000 Subject: [PATCH 40/79] more TDB --- source/libs/tdb/inc/tdb.h | 10 ++++++++++ source/libs/tdb/src/inc/btree.h | 1 + source/libs/tdb/src/inc/tdbInt.h | 13 +++++++++++++ 3 files changed, 24 insertions(+) diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index daffad3b79..bc1eb06078 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -34,6 +34,8 @@ int tdbEnvCreate(TENV **ppEnv); int tdbEnvOpen(TENV **ppEnv); int tdbEnvClose(TENV *pEnv); +int tdbEnvCommit(TENV *pEnv); + int tdbEnvSetPageSize(TENV *pEnv, pgsz_t szPage); int tdbEnvSetCacheSize(TENV *pEnv, cachesz_t szCache); pgsz_t tdbEnvGetPageSize(TENV *pEnv); @@ -44,6 +46,14 @@ int tdbCreate(TDB **ppDb); int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv); int tdbClose(TDB *pDb); +int tdbSetKeyLen(TDB *pDb, int klen); +int tdbSetValLen(TDB *pDb, int vlen); +int tdbSetDup(TDB *pDb, int dup); + +int tdbGetKeyLen(TDB *pDb, int *pklen); +int tdbGetValLen(TDB *pDb, int *pvlen); +int tdbGetDup(TDB *pDb, int *pdup); + // TDBC #ifdef __cplusplus diff --git a/source/libs/tdb/src/inc/btree.h b/source/libs/tdb/src/inc/btree.h index 343093edc9..f12184b3eb 100644 --- a/source/libs/tdb/src/inc/btree.h +++ b/source/libs/tdb/src/inc/btree.h @@ -31,6 +31,7 @@ int btreeClose(SBTree *pBt); int btreeCursorOpen(SBtCursor *pBtCur, SBTree *pBt); int btreeCursorClose(SBtCursor *pBtCur); int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey); +int btreeCursorNext(SBtCursor *pBtCur); struct SBTree { pgno_t root; diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 5c532c23f2..18ab940517 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -93,6 +93,19 @@ typedef TD_DLIST(SPgFile) SPgFileList; } \ } while (0) +#define TDB_VARIANT_LEN -1 + +#define TDB_DECODE_PAYLOAD(pPayload, keyLen, pKey, valLen, pVal) \ + do { \ + if ((keyLen) == TDB_VARIANT_LEN) { \ + /* TODO */ \ + } \ + if ((valLen) == TDB_VARIANT_LEN) { \ + /* TODO */ \ + } \ + /* TODO */ \ + } while (0) + #include "tdbUtil.h" #include "btree.h" From da7851f2e9dcbf399a62f2633a6c73808f42d59e Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 11 Feb 2022 11:41:16 +0000 Subject: [PATCH 41/79] more TDB --- source/libs/tdb/src/db/btree.c | 17 ++++++++++------- source/libs/tdb/src/inc/tdbInt.h | 11 ++++++++--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index 3ed6667c92..c38a6db8c2 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -28,10 +28,13 @@ typedef struct { // Btree page header definition typedef struct __attribute__((__packed__)) { - uint8_t flags; - uint16_t ncells; - pgsz_t pldOffset; // payload offset - /* TODO */ + uint8_t flag; // page flag + int32_t vlen; // value length of current page, TDB_VARIANT_LEN for variant length + uint16_t nPayloads; // number of total payloads + pgoff_t freeOff; // free payload offset + pgsz_t fragSize; // total fragment size + pgoff_t offPayload; // payload offset + pgno_t rChildPgno; // right most child page number } SBtPgHdr; typedef int (*BtreeCmprFn)(const void *, const void *); @@ -88,7 +91,7 @@ int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey) { SPgFile * pPgFile; pgno_t childPgno; pgno_t rootPgno; - int nPayload; + int nPayloads; void * pPayload; BtreeCmprFn cmpFn; @@ -109,11 +112,11 @@ int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey) { pPage = pBtCur->pPage; pBtPgHdr = BTREE_PAGE_HDR(pPage); - nPayload = pBtPgHdr->ncells; + nPayloads = pBtPgHdr->nPayloads; // Binary search the page lidx = 0; - ridx = nPayload - 1; + ridx = nPayloads - 1; midx = (lidx + ridx) >> 1; for (;;) { // get the payload ptr at midx diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 18ab940517..cc79ccaf39 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -70,6 +70,9 @@ typedef int32_t frame_id_t; #define TDB_DEFAULT_PGSIZE 4096 #define TDB_IS_PGSIZE_VLD(s) (((s) >= TDB_MIN_PGSIZE) && ((s) <= TDB_MAX_PGSIZE)) +// pgoff_t +typedef pgsz_t pgoff_t; + // cache #define TDB_DEFAULT_CACHE_SIZE (256 * 1024) // 256K @@ -93,15 +96,17 @@ typedef TD_DLIST(SPgFile) SPgFileList; } \ } while (0) -#define TDB_VARIANT_LEN -1 +#define TDB_VARIANT_LEN (int32_t) - 1 +// page payload format +// + + [key] + [value] #define TDB_DECODE_PAYLOAD(pPayload, keyLen, pKey, valLen, pVal) \ do { \ if ((keyLen) == TDB_VARIANT_LEN) { \ - /* TODO */ \ + /* TODO: decode the keyLen */ \ } \ if ((valLen) == TDB_VARIANT_LEN) { \ - /* TODO */ \ + /* TODO: decode the valLen */ \ } \ /* TODO */ \ } while (0) From 83d516af4cce21e4f1284f65a5ba5d29a0c28b42 Mon Sep 17 00:00:00 2001 From: Xiaoyu Wang Date: Sun, 13 Feb 2022 13:31:18 -0500 Subject: [PATCH 42/79] TD-13495 planner refactoring --- include/libs/nodes/nodes.h | 4 +- include/libs/nodes/plannodes.h | 65 ++++++ include/libs/nodes/querynodes.h | 14 +- include/libs/parser/newParser.h | 43 ++++ include/util/tjson.h | 50 +++++ source/libs/nodes/CMakeLists.txt | 2 +- source/libs/nodes/src/nodesCodeFuncs.c | 231 +++++++++++++++++++- source/libs/nodes/src/nodesUtilFuncs.c | 56 +++-- source/libs/parser/inc/parserImpl.h | 20 +- source/libs/parser/src/parserImpl.c | 9 +- source/libs/planner/inc/plannerImpl.h | 37 +--- source/libs/planner/src/plannerImpl.c | 182 +++++++++------ source/libs/planner/test/CMakeLists.txt | 2 +- source/libs/planner/test/newPlannerTest.cpp | 91 ++++++++ source/util/CMakeLists.txt | 2 +- source/util/src/tjson.c | 76 +++++++ 16 files changed, 744 insertions(+), 140 deletions(-) create mode 100644 include/libs/nodes/plannodes.h create mode 100644 include/libs/parser/newParser.h create mode 100644 include/util/tjson.h create mode 100644 source/libs/planner/test/newPlannerTest.cpp create mode 100644 source/util/src/tjson.c diff --git a/include/libs/nodes/nodes.h b/include/libs/nodes/nodes.h index 302e5b03ac..6a0422cd80 100644 --- a/include/libs/nodes/nodes.h +++ b/include/libs/nodes/nodes.h @@ -62,6 +62,7 @@ typedef enum ENodeType { QUERY_NODE_NODE_LIST, QUERY_NODE_FILL, QUERY_NODE_COLUMN_REF, + QUERY_NODE_TARGET, // Only be used in parser module. QUERY_NODE_RAW_EXPR, @@ -93,7 +94,7 @@ typedef struct SListCell { } SListCell; typedef struct SNodeList { - int16_t length; + int32_t length; SListCell* pHead; SListCell* pTail; } SNodeList; @@ -103,6 +104,7 @@ void nodesDestroyNode(SNode* pNode); SNodeList* nodesMakeList(); int32_t nodesListAppend(SNodeList* pList, SNode* pNode); +int32_t nodesListAppendList(SNodeList* pTarget, SNodeList* pSrc); SListCell* nodesListErase(SNodeList* pList, SListCell* pCell); SNode* nodesListGetNode(SNodeList* pList, int32_t index); void nodesDestroyList(SNodeList* pList); diff --git a/include/libs/nodes/plannodes.h b/include/libs/nodes/plannodes.h new file mode 100644 index 0000000000..3668f256eb --- /dev/null +++ b/include/libs/nodes/plannodes.h @@ -0,0 +1,65 @@ +/* + * 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_PLANN_NODES_H_ +#define _TD_PLANN_NODES_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "querynodes.h" + +typedef struct SLogicNode { + ENodeType type; + int32_t id; + SNodeList* pTargets; // SColumnNode + SNode* pConditions; + SNodeList* pChildren; + struct SLogicNode* pParent; +} SLogicNode; + +typedef struct SScanLogicNode { + SLogicNode node; + SNodeList* pScanCols; + struct STableMeta* pMeta; +} SScanLogicNode; + +typedef struct SJoinLogicNode { + SLogicNode node; + EJoinType joinType; + SNode* pOnConditions; +} SJoinLogicNode; + +typedef struct SFilterLogicNode { + SLogicNode node; +} SFilterLogicNode; + +typedef struct SAggLogicNode { + SLogicNode node; + SNodeList* pGroupKeys; + SNodeList* pAggFuncs; +} SAggLogicNode; + +typedef struct SProjectLogicNode { + SLogicNode node; + SNodeList* pProjections; +} SProjectLogicNode; + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_PLANN_NODES_H_*/ diff --git a/include/libs/nodes/querynodes.h b/include/libs/nodes/querynodes.h index 6046770ed1..024967289a 100644 --- a/include/libs/nodes/querynodes.h +++ b/include/libs/nodes/querynodes.h @@ -62,8 +62,10 @@ typedef struct SColumnNode { typedef struct SColumnRefNode { ENodeType type; - int32_t tupleId; - int32_t slotId; + SDataType dataType; + int16_t tupleId; + int16_t slotId; + int16_t columnId; } SColumnRefNode; typedef struct SValueNode { @@ -106,6 +108,12 @@ typedef enum EOperatorType { OP_TYPE_NMATCH, OP_TYPE_IS_NULL, OP_TYPE_IS_NOT_NULL, + OP_TYPE_IS_TRUE, + OP_TYPE_IS_FALSE, + OP_TYPE_IS_UNKNOWN, + OP_TYPE_IS_NOT_TRUE, + OP_TYPE_IS_NOT_FALSE, + OP_TYPE_IS_NOT_UNKNOWN, // json operator OP_TYPE_JSON_GET_VALUE, @@ -285,7 +293,7 @@ typedef enum ESqlClause { void nodesWalkSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeWalker walker, void* pContext); void nodesRewriteSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeRewriter rewriter, void* pContext); -int32_t nodesCollectColumns(SSelectStmt* pSelect, ESqlClause clause, uint64_t tableId, bool realCol, SNodeList** pCols); +int32_t nodesCollectColumns(SSelectStmt* pSelect, ESqlClause clause, const char* pTableAlias, SNodeList** pCols); typedef bool (*FFuncClassifier)(int32_t funcId); int32_t nodesCollectFuncs(SSelectStmt* pSelect, FFuncClassifier classifier, SNodeList** pFuncs); diff --git a/include/libs/parser/newParser.h b/include/libs/parser/newParser.h new file mode 100644 index 0000000000..fd631087db --- /dev/null +++ b/include/libs/parser/newParser.h @@ -0,0 +1,43 @@ +/* + * 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_NEW_PARSER_H_ +#define _TD_NEW_PARSER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "parser.h" + +typedef enum EStmtType { + STMT_TYPE_CMD = 1, + STMT_TYPE_QUERY +} EStmtType; + +typedef struct SQuery { + EStmtType stmtType; + SNode* pRoot; + int32_t numOfResCols; + SSchema* pResSchema; +} SQuery; + +int32_t parser(SParseContext* pParseCxt, SQuery* pQuery); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_NEW_PARSER_H_*/ diff --git a/include/util/tjson.h b/include/util/tjson.h new file mode 100644 index 0000000000..a4eb6e5385 --- /dev/null +++ b/include/util/tjson.h @@ -0,0 +1,50 @@ +/* + * 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_UTIL_JSON_H_ +#define _TD_UTIL_JSON_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "os.h" + +typedef void SJson; + +SJson* tjsonCreateObject(); +void tjsonDelete(SJson* pJson); + +SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName); + +int32_t tjsonAddIntegerToObject(SJson* pJson, const char* pName, const uint64_t number); +int32_t tjsonAddStringToObject(SJson* pJson, const char* pName, const char* pVal); +int32_t tjsonAddItemToObject(SJson* pJson, const char* pName, SJson* pItem); +int32_t tjsonAddItemToArray(SJson* pJson, SJson* pItem); + +typedef int32_t (*FToJson)(const void* pObj, SJson* pJson); + +int32_t tjsonAddObject(SJson* pJson, const char* pName, FToJson func, const void* pObj); +int32_t tjsonAddItem(SJson* pJson, FToJson func, const void* pObj); + +typedef int32_t (*FFromJson)(const SJson* pJson, void* pObj); + +char* tjsonToString(const SJson* pJson); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_UTIL_JSON_H_*/ diff --git a/source/libs/nodes/CMakeLists.txt b/source/libs/nodes/CMakeLists.txt index 9a826e034c..e20cdc39ba 100644 --- a/source/libs/nodes/CMakeLists.txt +++ b/source/libs/nodes/CMakeLists.txt @@ -7,7 +7,7 @@ target_include_directories( ) target_link_libraries( nodes - PRIVATE os util + PRIVATE os util common qcom ) if(${BUILD_TEST}) diff --git a/source/libs/nodes/src/nodesCodeFuncs.c b/source/libs/nodes/src/nodesCodeFuncs.c index ad83bdbd19..d1f32ae314 100644 --- a/source/libs/nodes/src/nodesCodeFuncs.c +++ b/source/libs/nodes/src/nodesCodeFuncs.c @@ -13,10 +13,179 @@ * along with this program. If not, see . */ -#include "nodes.h" +#include "plannodes.h" +#include "querynodes.h" +#include "query.h" +#include "taoserror.h" +#include "tjson.h" -int32_t nodesNodeToString(const SNode* pNode, char** pStr, int32_t* pLen) { - switch (nodeType(pNode)) { +static int32_t nodeToJson(const void* pObj, SJson* pJson); + +static char* nodeName(ENodeType type) { + switch (type) { + case QUERY_NODE_COLUMN: + return "Column"; + case QUERY_NODE_VALUE: + case QUERY_NODE_OPERATOR: + case QUERY_NODE_LOGIC_CONDITION: + case QUERY_NODE_FUNCTION: + case QUERY_NODE_REAL_TABLE: + case QUERY_NODE_TEMP_TABLE: + case QUERY_NODE_JOIN_TABLE: + case QUERY_NODE_GROUPING_SET: + case QUERY_NODE_ORDER_BY_EXPR: + case QUERY_NODE_LIMIT: + case QUERY_NODE_STATE_WINDOW: + case QUERY_NODE_SESSION_WINDOW: + case QUERY_NODE_INTERVAL_WINDOW: + case QUERY_NODE_NODE_LIST: + case QUERY_NODE_FILL: + case QUERY_NODE_COLUMN_REF: + case QUERY_NODE_TARGET: + case QUERY_NODE_RAW_EXPR: + case QUERY_NODE_SET_OPERATOR: + case QUERY_NODE_SELECT_STMT: + case QUERY_NODE_SHOW_STMT: + break; + case QUERY_NODE_LOGIC_PLAN_SCAN: + return "LogicScan"; + case QUERY_NODE_LOGIC_PLAN_JOIN: + return "LogicJoin"; + case QUERY_NODE_LOGIC_PLAN_FILTER: + return "LogicFilter"; + case QUERY_NODE_LOGIC_PLAN_AGG: + return "LogicAgg"; + case QUERY_NODE_LOGIC_PLAN_PROJECT: + return "LogicProject"; + default: + break; + } + return "Unknown"; +} + +static int32_t addNodeList(SJson* pJson, const char* pName, FToJson func, const SNodeList* pList) { + if (LIST_LENGTH(pList) > 0) { + SJson* jList = tjsonAddArrayToObject(pJson, pName); + if (NULL == jList) { + return TSDB_CODE_OUT_OF_MEMORY; + } + SNode* pNode; + FOREACH(pNode, pList) { + int32_t code = tjsonAddItem(jList, func, pNode); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + } + } + return TSDB_CODE_SUCCESS; +} + +static const char* jkTableMetaUid = "TableMetaUid"; +static const char* jkTableMetaSuid = "TableMetaSuid"; + +static int32_t tableMetaToJson(const void* pObj, SJson* pJson) { + const STableMeta* pNode = (const STableMeta*)pObj; + + int32_t code = tjsonAddIntegerToObject(pJson, jkTableMetaUid, pNode->uid); + if (TSDB_CODE_SUCCESS == code) { + code = tjsonAddIntegerToObject(pJson, jkTableMetaSuid, pNode->suid); + } + + return code; +} + +static const char* jkLogicPlanId = "Id"; +static const char* jkLogicPlanTargets = "Targets"; +static const char* jkLogicPlanConditions = "Conditions"; +static const char* jkLogicPlanChildren = "Children"; + +static int32_t logicPlanNodeToJson(const void* pObj, SJson* pJson) { + const SLogicNode* pNode = (const SLogicNode*)pObj; + + int32_t code = tjsonAddIntegerToObject(pJson, jkLogicPlanId, pNode->id); + if (TSDB_CODE_SUCCESS == code) { + code = addNodeList(pJson, jkLogicPlanTargets, nodeToJson, pNode->pTargets); + } + if (TSDB_CODE_SUCCESS == code) { + code = tjsonAddObject(pJson, jkLogicPlanConditions, nodeToJson, pNode->pConditions); + } + if (TSDB_CODE_SUCCESS == code) { + code = addNodeList(pJson, jkLogicPlanChildren, nodeToJson, pNode->pChildren); + } + + return code; +} + +static const char* jkScanLogicPlanScanCols = "ScanCols"; +static const char* jkScanLogicPlanTableMeta = "TableMeta"; + +static int32_t logicScanToJson(const void* pObj, SJson* pJson) { + const SScanLogicNode* pNode = (const SScanLogicNode*)pObj; + + int32_t code = logicPlanNodeToJson(pObj, pJson); + if (TSDB_CODE_SUCCESS == code) { + code = addNodeList(pJson, jkScanLogicPlanScanCols, nodeToJson, pNode->pScanCols); + } + if (TSDB_CODE_SUCCESS == code) { + code = tjsonAddObject(pJson, jkScanLogicPlanTableMeta, tableMetaToJson, pNode->pMeta); + } + + return code; +} + +static const char* jkProjectLogicPlanProjections = "Projections"; + +static int32_t logicProjectToJson(const void* pObj, SJson* pJson) { + const SProjectLogicNode* pNode = (const SProjectLogicNode*)pObj; + + int32_t code = logicPlanNodeToJson(pObj, pJson); + if (TSDB_CODE_SUCCESS == code) { + code = addNodeList(pJson, jkProjectLogicPlanProjections, nodeToJson, pNode->pProjections); + } + + return code; +} + +static const char* jkJoinLogicPlanJoinType = "JoinType"; +static const char* jkJoinLogicPlanOnConditions = "OnConditions"; + +static int32_t logicJoinToJson(const void* pObj, SJson* pJson) { + const SJoinLogicNode* pNode = (const SJoinLogicNode*)pObj; + + int32_t code = logicPlanNodeToJson(pObj, pJson); + if (TSDB_CODE_SUCCESS == code) { + code = tjsonAddIntegerToObject(pJson, jkJoinLogicPlanJoinType, pNode->joinType); + } + if (TSDB_CODE_SUCCESS == code) { + code = tjsonAddObject(pJson, jkJoinLogicPlanOnConditions, nodeToJson, pNode->pOnConditions); + } + + return code; +} + +static int32_t logicFilterToJson(const void* pObj, SJson* pJson) { + return logicPlanNodeToJson(pObj, pJson); +} + +static const char* jkAggLogicPlanGroupKeys = "GroupKeys"; +static const char* jkAggLogicPlanAggFuncs = "AggFuncs"; + +static int32_t logicAggToJson(const void* pObj, SJson* pJson) { + const SAggLogicNode* pNode = (const SAggLogicNode*)pObj; + + int32_t code = logicPlanNodeToJson(pObj, pJson); + if (TSDB_CODE_SUCCESS == code) { + code = addNodeList(pJson, jkAggLogicPlanGroupKeys, nodeToJson, pNode->pGroupKeys); + } + if (TSDB_CODE_SUCCESS == code) { + code = addNodeList(pJson, jkAggLogicPlanAggFuncs, nodeToJson, pNode->pAggFuncs); + } + + return code; +} + +static int32_t specificNodeToJson(const void* pObj, SJson* pJson) { + switch (nodeType(pObj)) { case QUERY_NODE_COLUMN: case QUERY_NODE_VALUE: case QUERY_NODE_OPERATOR: @@ -31,14 +200,68 @@ int32_t nodesNodeToString(const SNode* pNode, char** pStr, int32_t* pLen) { case QUERY_NODE_STATE_WINDOW: case QUERY_NODE_SESSION_WINDOW: case QUERY_NODE_INTERVAL_WINDOW: + case QUERY_NODE_NODE_LIST: + case QUERY_NODE_FILL: + case QUERY_NODE_COLUMN_REF: + case QUERY_NODE_TARGET: + case QUERY_NODE_RAW_EXPR: case QUERY_NODE_SET_OPERATOR: case QUERY_NODE_SELECT_STMT: case QUERY_NODE_SHOW_STMT: + break; + case QUERY_NODE_LOGIC_PLAN_SCAN: + return logicScanToJson(pObj, pJson); + case QUERY_NODE_LOGIC_PLAN_JOIN: + return logicJoinToJson(pObj, pJson); + case QUERY_NODE_LOGIC_PLAN_FILTER: + return logicFilterToJson(pObj, pJson); + case QUERY_NODE_LOGIC_PLAN_AGG: + return logicAggToJson(pObj, pJson); + case QUERY_NODE_LOGIC_PLAN_PROJECT: + return logicProjectToJson(pObj, pJson); default: break; } + return TSDB_CODE_SUCCESS; +} + +static const char* jkNodeType = "Type"; +static int32_t nodeToJson(const void* pObj, SJson* pJson) { + const SNode* pNode = (const SNode*)pObj; + + char* pNodeName = nodeName(nodeType(pNode)); + int32_t code = tjsonAddStringToObject(pJson, jkNodeType, pNodeName); + if (TSDB_CODE_SUCCESS == code) { + code = tjsonAddObject(pJson, pNodeName, specificNodeToJson, pNode); + } + + return code; +} + +int32_t nodesNodeToString(const SNode* pNode, char** pStr, int32_t* pLen) { + if (NULL == pNode || NULL == pStr || NULL == pLen) { + return TSDB_CODE_SUCCESS; + } + + SJson* pJson = tjsonCreateObject(); + if (NULL == pJson) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_OUT_OF_MEMORY; + } + + int32_t code = nodeToJson(pNode, pJson); + if (TSDB_CODE_SUCCESS != code) { + terrno = code; + return code; + } + + *pStr = tjsonToString(pJson); + tjsonDelete(pJson); + + *pLen = strlen(*pStr) + 1; + return TSDB_CODE_SUCCESS; } int32_t nodesStringToNode(const char* pStr, SNode** pNode) { - + return TSDB_CODE_SUCCESS; } diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index 50ddd14bf5..868af93c92 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -14,7 +14,7 @@ */ #include "querynodes.h" -#include "nodesShowStmts.h" +#include "plannodes.h" #include "taos.h" #include "taoserror.h" #include "thash.h" @@ -68,8 +68,18 @@ SNode* nodesMakeNode(ENodeType type) { return makeNode(type, sizeof(SSetOperator)); case QUERY_NODE_SELECT_STMT: return makeNode(type, sizeof(SSelectStmt)); - case QUERY_NODE_SHOW_STMT: - return makeNode(type, sizeof(SShowStmt)); + // case QUERY_NODE_SHOW_STMT: + // return makeNode(type, sizeof(SShowStmt)); + case QUERY_NODE_LOGIC_PLAN_SCAN: + return makeNode(type, sizeof(SScanLogicNode)); + case QUERY_NODE_LOGIC_PLAN_JOIN: + return makeNode(type, sizeof(SJoinLogicNode)); + case QUERY_NODE_LOGIC_PLAN_FILTER: + return makeNode(type, sizeof(SFilterLogicNode)); + case QUERY_NODE_LOGIC_PLAN_AGG: + return makeNode(type, sizeof(SAggLogicNode)); + case QUERY_NODE_LOGIC_PLAN_PROJECT: + return makeNode(type, sizeof(SProjectLogicNode)); default: break; } @@ -121,6 +131,15 @@ int32_t nodesListAppend(SNodeList* pList, SNode* pNode) { return TSDB_CODE_SUCCESS; } +int32_t nodesListAppendList(SNodeList* pTarget, SNodeList* pSrc) { + pTarget->pTail->pNext = pSrc->pHead; + pSrc->pHead->pPrev = pTarget->pTail; + pTarget->pTail = pSrc->pTail; + pTarget->length += pSrc->length; + tfree(pSrc); + return TSDB_CODE_SUCCESS; +} + SListCell* nodesListErase(SNodeList* pList, SListCell* pCell) { if (NULL == pCell->pPrev) { pList->pHead = pCell->pNext; @@ -129,6 +148,7 @@ SListCell* nodesListErase(SNodeList* pList, SListCell* pCell) { pCell->pNext->pPrev = pCell->pPrev; } SListCell* pNext = pCell->pNext; + nodesDestroyNode(pCell->pNode); tfree(pCell); --(pList->length); return pNext; @@ -185,6 +205,14 @@ bool nodesIsComparisonOp(const SOperatorNode* pOp) { case OP_TYPE_NOT_LIKE: case OP_TYPE_MATCH: case OP_TYPE_NMATCH: + case OP_TYPE_IS_NULL: + case OP_TYPE_IS_NOT_NULL: + case OP_TYPE_IS_TRUE: + case OP_TYPE_IS_FALSE: + case OP_TYPE_IS_UNKNOWN: + case OP_TYPE_IS_NOT_TRUE: + case OP_TYPE_IS_NOT_FALSE: + case OP_TYPE_IS_NOT_UNKNOWN: return true; default: break; @@ -213,8 +241,7 @@ bool nodesIsTimelineQuery(const SNode* pQuery) { typedef struct SCollectColumnsCxt { int32_t errCode; - uint64_t tableId; - bool realCol; + const char* pTableAlias; SNodeList* pCols; SHashObj* pColIdHash; } SCollectColumnsCxt; @@ -232,27 +259,24 @@ static EDealRes doCollect(SCollectColumnsCxt* pCxt, int32_t id, SNode* pNode) { static EDealRes collectColumns(SNode* pNode, void* pContext) { SCollectColumnsCxt* pCxt = (SCollectColumnsCxt*)pContext; - - if (pCxt->realCol && QUERY_NODE_COLUMN == nodeType(pNode)) { + if (QUERY_NODE_COLUMN == nodeType(pNode)) { SColumnNode* pCol = (SColumnNode*)pNode; int32_t colId = pCol->colId; - if (pCxt->tableId == pCol->tableId && colId > 0) { + if (0 == strcmp(pCxt->pTableAlias, pCol->tableAlias)) { return doCollect(pCxt, colId, pNode); } - } else if (!pCxt->realCol && QUERY_NODE_COLUMN_REF == nodeType(pNode)) { - return doCollect(pCxt, ((SColumnRefNode*)pNode)->slotId, pNode); } return DEAL_RES_CONTINUE; } -int32_t nodesCollectColumns(SSelectStmt* pSelect, ESqlClause clause, uint64_t tableId, bool realCol, SNodeList** pCols) { +int32_t nodesCollectColumns(SSelectStmt* pSelect, ESqlClause clause, const char* pTableAlias, SNodeList** pCols) { if (NULL == pSelect || NULL == pCols) { return TSDB_CODE_SUCCESS; } SCollectColumnsCxt cxt = { .errCode = TSDB_CODE_SUCCESS, - .realCol = realCol, + .pTableAlias = pTableAlias, .pCols = nodesMakeList(), .pColIdHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK) }; @@ -303,6 +327,12 @@ int32_t nodesCollectFuncs(SSelectStmt* pSelect, FFuncClassifier classifier, SNod nodesDestroyList(cxt.pFuncs); return cxt.errCode; } - *pFuncs = cxt.pFuncs; + if (LIST_LENGTH(cxt.pFuncs) > 0) { + *pFuncs = cxt.pFuncs; + } else { + nodesDestroyList(cxt.pFuncs); + *pFuncs = NULL; + } + return TSDB_CODE_SUCCESS; } diff --git a/source/libs/parser/inc/parserImpl.h b/source/libs/parser/inc/parserImpl.h index 4f6f0cf387..f2777a2368 100644 --- a/source/libs/parser/inc/parserImpl.h +++ b/source/libs/parser/inc/parserImpl.h @@ -13,27 +13,15 @@ * along with this program. If not, see . */ -#ifndef _TD_AST_CREATE_FUNCS_H_ -#define _TD_AST_CREATE_FUNCS_H_ +#ifndef _TD_PARSER_IMPL_H_ +#define _TD_PARSER_IMPL_H_ #ifdef __cplusplus extern "C" { #endif #include "querynodes.h" -#include "parser.h" - -typedef enum EStmtType { - STMT_TYPE_CMD = 1, - STMT_TYPE_QUERY -} EStmtType; - -typedef struct SQuery { - EStmtType stmtType; - SNode* pRoot; - int32_t numOfResCols; - SSchema* pResSchema; -} SQuery; +#include "newParser.h" int32_t doParse(SParseContext* pParseCxt, SQuery* pQuery); int32_t doTranslate(SParseContext* pParseCxt, SQuery* pQuery); @@ -42,4 +30,4 @@ int32_t doTranslate(SParseContext* pParseCxt, SQuery* pQuery); } #endif -#endif /*_TD_AST_CREATE_FUNCS_H_*/ +#endif /*_TD_PARSER_IMPL_H_*/ diff --git a/source/libs/parser/src/parserImpl.c b/source/libs/parser/src/parserImpl.c index 6f80412bfc..d353c245cb 100644 --- a/source/libs/parser/src/parserImpl.c +++ b/source/libs/parser/src/parserImpl.c @@ -880,7 +880,6 @@ static int32_t translateOrderByPosition(STranslateContext* pCxt, SNodeList* pPro int32_t pos = getPositionValue(pVal); if (pos < 0) { ERASE_NODE(pOrderByList); - nodesDestroyNode(pNode); continue; } else if (0 == pos || pos > LIST_LENGTH(pProjectionList)) { return generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT); @@ -1058,3 +1057,11 @@ int32_t doTranslate(SParseContext* pParseCxt, SQuery* pQuery) { } return code; } + +int32_t parser(SParseContext* pParseCxt, SQuery* pQuery) { + int32_t code = doParse(pParseCxt, pQuery); + if (TSDB_CODE_SUCCESS == code) { + code = doTranslate(pParseCxt, pQuery); + } + return code; +} diff --git a/source/libs/planner/inc/plannerImpl.h b/source/libs/planner/inc/plannerImpl.h index 1960eb52fb..d89cc26700 100644 --- a/source/libs/planner/inc/plannerImpl.h +++ b/source/libs/planner/inc/plannerImpl.h @@ -20,43 +20,10 @@ extern "C" { #endif -#include "querynodes.h" +#include "plannodes.h" #include "planner.h" -typedef struct SLogicNode { - ENodeType type; - int32_t id; - SNodeList* pTargets; - SNode* pConditions; - SNodeList* pChildren; - struct SLogicNode* pParent; -} SLogicNode; - -typedef struct SScanLogicNode { - SLogicNode node; - SNodeList* pScanCols; - struct STableMeta* pMeta; -} SScanLogicNode; - -typedef struct SJoinLogicNode { - SLogicNode node; - EJoinType joinType; - SNode* pOnConditions; -} SJoinLogicNode; - -typedef struct SFilterLogicNode { - SLogicNode node; -} SFilterLogicNode; - -typedef struct SAggLogicNode { - SLogicNode node; - SNodeList* pGroupKeys; - SNodeList* pAggFuncs; -} SAggLogicNode; - -typedef struct SProjectLogicNode { - SLogicNode node; -} SProjectLogicNode; +int32_t createLogicPlan(SNode* pNode, SLogicNode** pLogicNode); #ifdef __cplusplus } diff --git a/source/libs/planner/src/plannerImpl.c b/source/libs/planner/src/plannerImpl.c index 11f721bda5..40a82f4175 100644 --- a/source/libs/planner/src/plannerImpl.c +++ b/source/libs/planner/src/plannerImpl.c @@ -19,6 +19,7 @@ #define CHECK_ALLOC(p, res) \ do { \ if (NULL == p) { \ + printf("%s : %d\n", __FUNCTION__, __LINE__); \ pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY; \ return res; \ } \ @@ -28,6 +29,7 @@ do { \ int32_t code = exec; \ if (TSDB_CODE_SUCCESS != code) { \ + printf("%s : %d\n", __FUNCTION__, __LINE__); \ pCxt->errCode = code; \ return res; \ } \ @@ -44,8 +46,7 @@ static SLogicNode* createLogicNodeByTable(SPlanContext* pCxt, SSelectStmt* pSele typedef struct SRewriteExprCxt { int32_t errCode; - int32_t planNodeId; - SNodeList* pTargets; + SNodeList* pExprs; } SRewriteExprCxt; static EDealRes doRewriteExpr(SNode** pNode, void* pContext) { @@ -53,34 +54,44 @@ static EDealRes doRewriteExpr(SNode** pNode, void* pContext) { case QUERY_NODE_OPERATOR: case QUERY_NODE_LOGIC_CONDITION: case QUERY_NODE_FUNCTION: { + SRewriteExprCxt* pCxt = (SRewriteExprCxt*)pContext; + SNode* pExpr; + int32_t index = 0; + FOREACH(pExpr, pCxt->pExprs) { + if (nodesEqualNode(pExpr, *pNode)) { + SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN); + if (NULL == pCol) { + pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY; + return DEAL_RES_ERROR; + } + SExprNode* pToBeRewrittenExpr = (SExprNode*)(*pNode); + pCol->node.resType = pToBeRewrittenExpr->resType; + strcpy(pCol->node.aliasName, pToBeRewrittenExpr->aliasName); + strcpy(pCol->colName, ((SExprNode*)pExpr)->aliasName); + nodesDestroyNode(*pNode); + *pNode = (SNode*)pCol; + return DEAL_RES_IGNORE_CHILD; + } + ++index; + } break; } default: break; } - SRewriteExprCxt* pCxt = (SRewriteExprCxt*)pContext; - SNode* pTarget; - int32_t index = 0; - FOREACH(pTarget, pCxt->pTargets) { - if (nodesEqualNode(pTarget, *pNode)) { - SColumnRefNode* pCol = (SColumnRefNode*)nodesMakeNode(QUERY_NODE_COLUMN_REF); - if (NULL == pCol) { - pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY; - return DEAL_RES_ERROR; - } - pCol->tupleId = pCxt->planNodeId; - pCol->slotId = index; - nodesDestroyNode(*pNode); - *pNode = (SNode*)pCol; - return DEAL_RES_IGNORE_CHILD; - } - ++index; - } + return DEAL_RES_CONTINUE; } -static int32_t rewriteExpr(int32_t planNodeId, SNodeList* pTargets, SSelectStmt* pSelect, ESqlClause clause) { - SRewriteExprCxt cxt = { .errCode = TSDB_CODE_SUCCESS, .planNodeId = planNodeId, .pTargets = pTargets }; +static int32_t rewriteExpr(int32_t planNodeId, int32_t rewriteId, SNodeList* pExprs, SSelectStmt* pSelect, ESqlClause clause) { + SNode* pNode; + FOREACH(pNode, pExprs) { + if (QUERY_NODE_COLUMN == nodeType(pNode) || QUERY_NODE_VALUE == nodeType(pNode)) { + continue; + } + sprintf(((SExprNode*)pNode)->aliasName, "#expr_%d_%d", planNodeId, rewriteId); + } + SRewriteExprCxt cxt = { .errCode = TSDB_CODE_SUCCESS, .pExprs = pExprs }; nodesRewriteSelectStmt(pSelect, clause, doRewriteExpr, &cxt); return cxt.errCode; } @@ -114,23 +125,6 @@ error: return pRoot; } -static SNodeList* createScanTargets(int32_t planNodeId, int32_t numOfScanCols) { - SNodeList* pTargets = nodesMakeList(); - if (NULL == pTargets) { - return NULL; - } - for (int32_t i = 0; i < numOfScanCols; ++i) { - SColumnRefNode* pCol = (SColumnRefNode*)nodesMakeNode(QUERY_NODE_COLUMN_REF); - if (NULL == pCol || TSDB_CODE_SUCCESS != nodesListAppend(pTargets, (SNode*)pCol)) { - nodesDestroyList(pTargets); - return NULL; - } - pCol->tupleId = planNodeId; - pCol->slotId = i; - } - return pTargets; -} - static SLogicNode* createScanLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect, SRealTableNode* pRealTable) { SScanLogicNode* pScan = (SScanLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SCAN); CHECK_ALLOC(pScan, NULL); @@ -140,22 +134,25 @@ static SLogicNode* createScanLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect, // set columns to scan SNodeList* pCols = NULL; - CHECK_CODE(nodesCollectColumns(pSelect, SQL_CLAUSE_FROM, pScan->pMeta->uid, true, &pCols), (SLogicNode*)pScan); + CHECK_CODE(nodesCollectColumns(pSelect, SQL_CLAUSE_FROM, pRealTable->table.tableAlias, &pCols), (SLogicNode*)pScan); pScan->pScanCols = nodesCloneList(pCols); CHECK_ALLOC(pScan->pScanCols, (SLogicNode*)pScan); - // pScanCols of SScanLogicNode is equivalent to pTargets of other logic nodes - CHECK_CODE(rewriteExpr(pScan->node.id, pScan->pScanCols, pSelect, SQL_CLAUSE_FROM), (SLogicNode*)pScan); - // set output - pScan->node.pTargets = createScanTargets(pScan->node.id, LIST_LENGTH(pScan->pScanCols)); + pScan->node.pTargets = nodesCloneList(pCols); CHECK_ALLOC(pScan->node.pTargets, (SLogicNode*)pScan); return (SLogicNode*)pScan; } static SLogicNode* createSubqueryLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect, STempTableNode* pTable) { - return createQueryLogicNode(pCxt, pTable->pSubquery); + SLogicNode* pRoot = createQueryLogicNode(pCxt, pTable->pSubquery); + CHECK_ALLOC(pRoot, NULL); + SNode* pNode; + FOREACH(pNode, pRoot->pTargets) { + strcpy(((SColumnNode*)pNode)->tableAlias, pTable->table.tableAlias); + } + return pRoot; } static SLogicNode* createJoinLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect, SJoinTableNode* pJoinTable) { @@ -179,12 +176,12 @@ static SLogicNode* createJoinLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect, pJoin->pOnConditions = nodesCloneNode(pJoinTable->pOnCond); CHECK_ALLOC(pJoin->pOnConditions, (SLogicNode*)pJoin); - // set the output and rewrite the expression in subsequent clauses with the output - SNodeList* pCols = NULL; - CHECK_CODE(nodesCollectColumns(pSelect, SQL_CLAUSE_FROM, 0, false, &pCols), (SLogicNode*)pJoin); - pJoin->node.pTargets = nodesCloneList(pCols); + // set the output + pJoin->node.pTargets = nodesCloneList(pLeft->pTargets); CHECK_ALLOC(pJoin->node.pTargets, (SLogicNode*)pJoin); - CHECK_CODE(rewriteExpr(pJoin->node.id, pJoin->node.pTargets, pSelect, SQL_CLAUSE_FROM), (SLogicNode*)pJoin); + SNodeList* pTargets = nodesCloneList(pRight->pTargets); + CHECK_ALLOC(pTargets, (SLogicNode*)pJoin); + nodesListAppendList(pJoin->node.pTargets, pTargets); return (SLogicNode*)pJoin; } @@ -203,7 +200,7 @@ static SLogicNode* createLogicNodeByTable(SPlanContext* pCxt, SSelectStmt* pSele return NULL; } -static SLogicNode* createWhereFilterLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect) { +static SLogicNode* createWhereFilterLogicNode(SPlanContext* pCxt, SLogicNode* pChild, SSelectStmt* pSelect) { if (NULL == pSelect->pWhere) { return NULL; } @@ -216,16 +213,44 @@ static SLogicNode* createWhereFilterLogicNode(SPlanContext* pCxt, SSelectStmt* p pFilter->node.pConditions = nodesCloneNode(pSelect->pWhere); CHECK_ALLOC(pFilter->node.pConditions, (SLogicNode*)pFilter); - // set the output and rewrite the expression in subsequent clauses with the output - SNodeList* pCols = NULL; - CHECK_CODE(nodesCollectColumns(pSelect, SQL_CLAUSE_WHERE, 0, false, &pCols), (SLogicNode*)pFilter); - pFilter->node.pTargets = nodesCloneList(pCols); + // set the output + pFilter->node.pTargets = nodesCloneList(pChild->pTargets); CHECK_ALLOC(pFilter->node.pTargets, (SLogicNode*)pFilter); - CHECK_CODE(rewriteExpr(pFilter->node.id, pFilter->node.pTargets, pSelect, SQL_CLAUSE_WHERE), (SLogicNode*)pFilter); return (SLogicNode*)pFilter; } +static SNodeList* createColumnByRewriteExps(SPlanContext* pCxt, SNodeList* pExprs) { + SNodeList* pList = nodesMakeList(); + CHECK_ALLOC(pList, NULL); + SNode* pNode; + FOREACH(pNode, pExprs) { + if (QUERY_NODE_VALUE == nodeType(pNode)) { + continue; + } else if (QUERY_NODE_COLUMN == nodeType(pNode)) { + SNode* pCol = nodesCloneNode(pNode); + if (NULL == pCol) { + goto error; + } + if (TSDB_CODE_SUCCESS != nodesListAppend(pList, pCol)) { + goto error; + } + } else { + SExprNode* pExpr = (SExprNode*)pNode; + SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN); + if (NULL == pCol) { + goto error; + } + pCol->node.resType = pExpr->resType; + strcpy(pCol->colName, pExpr->aliasName); + } + } + return pList; +error: + nodesDestroyList(pList); + return NULL; +} + static SLogicNode* createAggLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect) { SNodeList* pAggFuncs = NULL; CHECK_CODE(nodesCollectFuncs(pSelect, fmIsAggFunc, &pAggFuncs), NULL); @@ -242,25 +267,54 @@ static SLogicNode* createAggLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect) CHECK_ALLOC(pAgg->pGroupKeys, (SLogicNode*)pAgg); pAgg->pAggFuncs = nodesCloneList(pAggFuncs); CHECK_ALLOC(pAgg->pAggFuncs, (SLogicNode*)pAgg); + + // rewrite the expression in subsequent clauses + CHECK_CODE(rewriteExpr(pAgg->node.id, 1, pAgg->pGroupKeys, pSelect, SQL_CLAUSE_GROUP_BY), (SLogicNode*)pAgg); + CHECK_CODE(rewriteExpr(pAgg->node.id, 1 + LIST_LENGTH(pAgg->pGroupKeys), pAgg->pAggFuncs, pSelect, SQL_CLAUSE_GROUP_BY), (SLogicNode*)pAgg); + pAgg->node.pConditions = nodesCloneNode(pSelect->pHaving); CHECK_ALLOC(pAgg->node.pConditions, (SLogicNode*)pAgg); - // set the output and rewrite the expression in subsequent clauses with the output - SNodeList* pCols = NULL; - CHECK_CODE(nodesCollectColumns(pSelect, SQL_CLAUSE_HAVING, 0, false, &pCols), (SLogicNode*)pAgg); - pAgg->node.pTargets = nodesCloneList(pCols); + // set the output + pAgg->node.pTargets = createColumnByRewriteExps(pCxt, pAgg->pGroupKeys); CHECK_ALLOC(pAgg->node.pTargets, (SLogicNode*)pAgg); - CHECK_CODE(rewriteExpr(pAgg->node.id, pAgg->node.pTargets, pSelect, SQL_CLAUSE_HAVING), (SLogicNode*)pAgg); - + SNodeList* pTargets = createColumnByRewriteExps(pCxt, pAgg->pAggFuncs); + CHECK_ALLOC(pTargets, (SLogicNode*)pAgg); + nodesListAppendList(pAgg->node.pTargets, pTargets); + return (SLogicNode*)pAgg; } +static SNodeList* createColumnByProjections(SPlanContext* pCxt, SNodeList* pExprs) { + SNodeList* pList = nodesMakeList(); + CHECK_ALLOC(pList, NULL); + SNode* pNode; + FOREACH(pNode, pExprs) { + SExprNode* pExpr = (SExprNode*)pNode; + SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN); + if (NULL == pCol) { + goto error; + } + pCol->node.resType = pExpr->resType; + strcpy(pCol->colName, pExpr->aliasName); + if (TSDB_CODE_SUCCESS != nodesListAppend(pList, (SNode*)pCol)) { + goto error; + } + } + return pList; +error: + nodesDestroyList(pList); + return NULL; +} + static SLogicNode* createProjectLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect) { SProjectLogicNode* pProject = (SProjectLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PROJECT); CHECK_ALLOC(pProject, NULL); pProject->node.id = pCxt->planNodeId++; - pProject->node.pTargets = nodesCloneList(pSelect->pProjectionList); + pProject->pProjections = nodesCloneList(pSelect->pProjectionList); + + pProject->node.pTargets = createColumnByProjections(pCxt,pSelect->pProjectionList); CHECK_ALLOC(pProject->node.pTargets, (SLogicNode*)pProject); return (SLogicNode*)pProject; @@ -269,7 +323,7 @@ static SLogicNode* createProjectLogicNode(SPlanContext* pCxt, SSelectStmt* pSele static SLogicNode* createSelectLogicNode(SPlanContext* pCxt, SSelectStmt* pSelect) { SLogicNode* pRoot = createLogicNodeByTable(pCxt, pSelect, pSelect->pFromTable); if (TSDB_CODE_SUCCESS == pCxt->errCode) { - pRoot = pushLogicNode(pCxt, pRoot, createWhereFilterLogicNode(pCxt, pSelect)); + pRoot = pushLogicNode(pCxt, pRoot, createWhereFilterLogicNode(pCxt, pRoot, pSelect)); } if (TSDB_CODE_SUCCESS == pCxt->errCode) { pRoot = pushLogicNode(pCxt, pRoot, createAggLogicNode(pCxt, pSelect)); diff --git a/source/libs/planner/test/CMakeLists.txt b/source/libs/planner/test/CMakeLists.txt index 7fbfcfe7ef..cd60b503b9 100644 --- a/source/libs/planner/test/CMakeLists.txt +++ b/source/libs/planner/test/CMakeLists.txt @@ -13,7 +13,7 @@ ADD_EXECUTABLE(plannerTest TARGET_LINK_LIBRARIES( plannerTest - PUBLIC os util common planner parser catalog transport gtest function qcom + PUBLIC os util common nodes planner parser catalog transport gtest function qcom ) TARGET_INCLUDE_DIRECTORIES( diff --git a/source/libs/planner/test/newPlannerTest.cpp b/source/libs/planner/test/newPlannerTest.cpp new file mode 100644 index 0000000000..c227bf88ba --- /dev/null +++ b/source/libs/planner/test/newPlannerTest.cpp @@ -0,0 +1,91 @@ +/* + * 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 + +#include + +#include "plannerImpl.h" +#include "newParser.h" + +using namespace std; +using namespace testing; + +class NewPlannerTest : public Test { +protected: + void setDatabase(const string& acctId, const string& db) { + acctId_ = acctId; + db_ = db; + } + + void bind(const char* sql) { + reset(); + cxt_.acctId = atoi(acctId_.c_str()); + cxt_.db = db_.c_str(); + sqlBuf_ = string(sql); + transform(sqlBuf_.begin(), sqlBuf_.end(), sqlBuf_.begin(), ::tolower); + cxt_.sqlLen = strlen(sql); + cxt_.pSql = sqlBuf_.c_str(); + } + + bool run() { + int32_t code = parser(&cxt_, &query_); + // cout << "parser return " << code << endl; + if (code != TSDB_CODE_SUCCESS) { + cout << "sql:[" << cxt_.pSql << "] parser code:" << tstrerror(code) << ", msg:" << errMagBuf_ << endl; + return false; + } + SLogicNode* pLogicPlan = nullptr; + code = createLogicPlan(query_.pRoot, &pLogicPlan); + if (code != TSDB_CODE_SUCCESS) { + cout << "sql:[" << cxt_.pSql << "] plan code:" << tstrerror(code) << endl; + return false; + } + char* pStr = NULL; + int32_t len = 0; + code = nodesNodeToString((const SNode*)pLogicPlan, &pStr, &len); + if (code != TSDB_CODE_SUCCESS) { + cout << "sql:[" << cxt_.pSql << "] toString code:" << tstrerror(code) << endl; + return false; + } + cout << "logic plan : " << endl; + cout << pStr << endl; + return true; + } + +private: + static const int max_err_len = 1024; + + void reset() { + memset(&cxt_, 0, sizeof(cxt_)); + memset(errMagBuf_, 0, max_err_len); + cxt_.pMsg = errMagBuf_; + cxt_.msgLen = max_err_len; + } + + string acctId_; + string db_; + char errMagBuf_[max_err_len]; + string sqlBuf_; + SParseContext cxt_; + SQuery query_; +}; + +TEST_F(NewPlannerTest, simple) { + setDatabase("root", "test"); + + bind("SELECT * FROM t1"); + ASSERT_TRUE(run()); +} diff --git a/source/util/CMakeLists.txt b/source/util/CMakeLists.txt index 760a66a4fb..7a47639e75 100644 --- a/source/util/CMakeLists.txt +++ b/source/util/CMakeLists.txt @@ -10,7 +10,7 @@ target_link_libraries( util PRIVATE os PUBLIC lz4_static - PUBLIC api + PUBLIC api cjson ) if(${BUILD_TEST}) diff --git a/source/util/src/tjson.c b/source/util/src/tjson.c new file mode 100644 index 0000000000..556e8f8060 --- /dev/null +++ b/source/util/src/tjson.c @@ -0,0 +1,76 @@ +/* + * 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 "tjson.h" + +#include "taoserror.h" +#include "cJSON.h" + +SJson* tjsonCreateObject() { + return cJSON_CreateObject(); +} + +void tjsonDelete(SJson* pJson) { + cJSON_Delete((cJSON*)pJson); +} + +int32_t tjsonAddIntegerToObject(SJson* pJson, const char* pName, const uint64_t number) { + char tmp[40] = {0}; + snprintf(tmp, tListLen(tmp), "%"PRId64, number); + return tjsonAddStringToObject(pJson, pName, tmp); +} + +int32_t tjsonAddStringToObject(SJson* pJson, const char* pName, const char* pVal) { + return (NULL == cJSON_AddStringToObject((cJSON*)pJson, pName, pVal) ? TSDB_CODE_FAILED : TSDB_CODE_SUCCESS); +} + +SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName) { + return cJSON_AddArrayToObject((cJSON*)pJson, pName); +} + +int32_t tjsonAddItemToObject(SJson *pJson, const char* pName, SJson* pItem) { + return (cJSON_AddItemToObject((cJSON*)pJson, pName, pItem) ? TSDB_CODE_SUCCESS : TSDB_CODE_FAILED); +} + +int32_t tjsonAddItemToArray(SJson* pJson, SJson* pItem) { + return (cJSON_AddItemToArray((cJSON*)pJson, pItem) ? TSDB_CODE_SUCCESS : TSDB_CODE_FAILED); +} + +int32_t tjsonAddObject(SJson* pJson, const char* pName, FToJson func, const void* pObj) { + if (NULL == pObj) { + return TSDB_CODE_SUCCESS; + } + + SJson* pJobj = tjsonCreateObject(); + if (NULL == pJobj || TSDB_CODE_SUCCESS != func(pObj, pJobj)) { + printf("%s:%d code = %d\n", __FUNCTION__, __LINE__, TSDB_CODE_FAILED); + tjsonDelete(pJobj); + return TSDB_CODE_FAILED; + } + return tjsonAddItemToObject(pJson, pName, pJobj); +} + +int32_t tjsonAddItem(SJson* pJson, FToJson func, const void* pObj) { + SJson* pJobj = tjsonCreateObject(); + if (NULL == pJobj || TSDB_CODE_SUCCESS != func(pObj, pJobj)) { + tjsonDelete(pJobj); + return TSDB_CODE_FAILED; + } + return tjsonAddItemToArray(pJson, pJobj); +} + +char* tjsonToString(const SJson* pJson) { + return cJSON_Print((cJSON*)pJson); +} From 74f73e6bab20c86f2e6dca433eb5951be33895a5 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 14 Feb 2022 09:01:33 +0000 Subject: [PATCH 43/79] more TDB --- source/libs/tdb/src/db/btree.c | 11 ++++++++++- source/libs/tdb/src/inc/btree.h | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index c38a6db8c2..01db2a8403 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -66,12 +66,21 @@ int btreeClose(SBTree *pBt) { } static int btreeCreate(SBTree **pBt) { + SBTree *pBt; + + pBt = (SBTree *)calloc(1, sizeof(*pBt)); + if (pBt == NULL) { + return -1; + } + // TODO return 0; } static int btreeDestroy(SBTree *pBt) { - // TODO + if (pBt) { + free(pBt); + } return 0; } diff --git a/source/libs/tdb/src/inc/btree.h b/source/libs/tdb/src/inc/btree.h index f12184b3eb..94af3331ba 100644 --- a/source/libs/tdb/src/inc/btree.h +++ b/source/libs/tdb/src/inc/btree.h @@ -34,7 +34,7 @@ int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey); int btreeCursorNext(SBtCursor *pBtCur); struct SBTree { - pgno_t root; + pgno_t root; }; #ifdef __cplusplus From 6c314f45f7872ff7c9a922565dcd887b727e687a Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 14 Feb 2022 09:48:20 +0000 Subject: [PATCH 44/79] more TDB --- source/libs/tdb/inc/tdb.h | 1 + source/libs/tdb/src/db/btree.c | 4 +- source/libs/tdb/src/db/pgcache.c | 418 +++++++++++++++++++++++++- source/libs/tdb/src/db/tdbJournal.c | 19 ++ source/libs/tdb/src/db/tdb_mpool.c | 362 ---------------------- source/libs/tdb/src/inc/tdbJournal.h | 29 ++ source/libs/tdb/src/inc/tdb_mpool.h | 94 ------ source/libs/tdb/test/CMakeLists.txt | 10 +- source/libs/tdb/test/tdbMPoolTest.cpp | 31 -- 9 files changed, 471 insertions(+), 497 deletions(-) create mode 100644 source/libs/tdb/src/db/tdbJournal.c delete mode 100644 source/libs/tdb/src/db/tdb_mpool.c create mode 100644 source/libs/tdb/src/inc/tdbJournal.h delete mode 100644 source/libs/tdb/src/inc/tdb_mpool.h delete mode 100644 source/libs/tdb/test/tdbMPoolTest.cpp diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index bc1eb06078..1df194e24a 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -34,6 +34,7 @@ int tdbEnvCreate(TENV **ppEnv); int tdbEnvOpen(TENV **ppEnv); int tdbEnvClose(TENV *pEnv); +int tdbEnvBeginTxn(TENV *pEnv); int tdbEnvCommit(TENV *pEnv); int tdbEnvSetPageSize(TENV *pEnv, pgsz_t szPage); diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/btree.c index 01db2a8403..86e7980733 100644 --- a/source/libs/tdb/src/db/btree.c +++ b/source/libs/tdb/src/db/btree.c @@ -43,7 +43,7 @@ typedef int (*BtreeCmprFn)(const void *, const void *); #define BTREE_PAGE_PAYLOAD_AT(pPage, idx) NULL /*TODO*/ #define BTREE_PAGE_IS_LEAF(pPage) 0 /* TODO */ -static int btreeCreate(SBTree **pBt); +static int btreeCreate(SBTree **ppBt); static int btreeDestroy(SBTree *pBt); static int btreeCursorMoveToChild(SBtCursor *pBtCur, pgno_t pgno); @@ -65,7 +65,7 @@ int btreeClose(SBTree *pBt) { return 0; } -static int btreeCreate(SBTree **pBt) { +static int btreeCreate(SBTree **ppBt) { SBTree *pBt; pBt = (SBTree *)calloc(1, sizeof(*pBt)); diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/pgcache.c index 9e66a5a960..3d0d770f38 100644 --- a/source/libs/tdb/src/db/pgcache.c +++ b/source/libs/tdb/src/db/pgcache.c @@ -160,4 +160,420 @@ static void pgCachePinPage(SPage *pPage) { static void pgCacheUnpinPage(SPage *pPage) { // TODO -} \ No newline at end of file +} + + + +#if 0 +// Exposed handle +typedef struct TDB_MPOOL TDB_MPOOL; +typedef struct TDB_MPFILE TDB_MPFILE; + +typedef TD_DLIST_NODE(pg_t) pg_free_dlist_node_t, pg_hash_dlist_node_t; +typedef struct pg_t { + SRWLatch rwLatch; + frame_id_t frameid; + pgid_t pgid; + uint8_t dirty; + uint8_t rbit; + int32_t pinRef; + pg_free_dlist_node_t free; + pg_hash_dlist_node_t hash; + void * p; +} pg_t; + +typedef TD_DLIST(pg_t) pg_list_t; +typedef struct { + SRWLatch latch; + TD_DLIST(TDB_MPFILE); +} mpf_bucket_t; +struct TDB_MPOOL { + int64_t cachesize; + pgsz_t pgsize; + int32_t npages; + pg_t * pages; + pg_list_t freeList; + frame_id_t clockHand; + struct { + int32_t nbucket; + pg_list_t *hashtab; + } pgtab; // page table, hash + struct { +#define MPF_HASH_BUCKETS 16 + mpf_bucket_t buckets[MPF_HASH_BUCKETS]; + } mpfht; // MPF hash table. MPFs using this MP will be put in this hash table +}; + +#define MP_PAGE_AT(mp, idx) (mp)->pages[idx] + +typedef TD_DLIST_NODE(TDB_MPFILE) td_mpf_dlist_node_t; +struct TDB_MPFILE { + char * fname; // file name + int fd; // fd + uint8_t fileid[TDB_FILE_ID_LEN]; // file ID + TDB_MPOOL * mp; // underlying memory pool + td_mpf_dlist_node_t node; +}; + +/*=================================================== Exposed apis ==================================================*/ +// TDB_MPOOL +int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsz_t pgsize); +int tdbMPoolClose(TDB_MPOOL *mp); +int tdbMPoolSync(TDB_MPOOL *mp); + +// TDB_MPFILE +int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp); +int tdbMPoolFileClose(TDB_MPFILE *mpf); +int tdbMPoolFileNewPage(TDB_MPFILE *mpf, pgno_t *pgno, void *addr); +int tdbMPoolFileFreePage(TDB_MPOOL *mpf, pgno_t *pgno, void *addr); +int tdbMPoolFileGetPage(TDB_MPFILE *mpf, pgno_t pgno, void *addr); +int tdbMPoolFilePutPage(TDB_MPFILE *mpf, pgno_t pgno, void *addr); +int tdbMPoolFileSync(TDB_MPFILE *mpf); + +static void tdbMPoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf); +static void tdbMPoolUnregFile(TDB_MPOOL *mp, TDB_MPFILE *mpf); +static TDB_MPFILE *tdbMPoolGetFile(TDB_MPOOL *mp, uint8_t *fileid); +static int tdbMPoolFileReadPage(TDB_MPFILE *mpf, pgno_t pgno, void *p); +static int tdbMPoolFileWritePage(TDB_MPFILE *mpf, pgno_t pgno, const void *p); +static void tdbMPoolClockEvictPage(TDB_MPOOL *mp, pg_t **pagepp); + +int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsz_t pgsize) { + TDB_MPOOL *mp = NULL; + size_t tsize; + pg_t * pagep; + + // check parameters + if (!TDB_IS_PGSIZE_VLD(pgsize)) { + tdbError("invalid page size"); + return -1; + } + + // allocate handle + mp = (TDB_MPOOL *)calloc(1, sizeof(*mp)); + if (mp == NULL) { + tdbError("failed to malloc memory pool handle"); + goto _err; + } + + // initialize the handle + mp->cachesize = cachesize; + mp->pgsize = pgsize; + mp->npages = cachesize / pgsize; + mp->clockHand = 0; + + TD_DLIST_INIT(&mp->freeList); + + mp->pages = (pg_t *)calloc(mp->npages, sizeof(pg_t)); + if (mp->pages == NULL) { + tdbError("failed to malloc memory pool pages"); + goto _err; + } + + for (frame_id_t i = 0; i < mp->npages; i++) { + mp->pages[i].p = malloc(pgsize); + if (mp->pages[i].p == NULL) { + goto _err; + } + + taosInitRWLatch(&mp->pages[i].rwLatch); + mp->pages[i].frameid = i; + mp->pages[i].pgid = TDB_IVLD_PGID; + + // add new page to the free list + TD_DLIST_APPEND_WITH_FIELD(&(mp->freeList), &(mp->pages[i]), free); + } + +#define PGTAB_FACTOR 1.0 + mp->pgtab.nbucket = mp->npages / PGTAB_FACTOR; + mp->pgtab.hashtab = (pg_list_t *)calloc(mp->pgtab.nbucket, sizeof(pg_list_t)); + if (mp->pgtab.hashtab == NULL) { + tdbError("failed to malloc memory pool hash table"); + goto _err; + } + + // return + *mpp = mp; + return 0; + +_err: + tdbMPoolClose(mp); + *mpp = NULL; + return -1; +} + +int tdbMPoolClose(TDB_MPOOL *mp) { + if (mp) { + tfree(mp->pgtab.hashtab); + if (mp->pages) { + for (int i = 0; i < mp->npages; i++) { + tfree(mp->pages[i].p); + } + + free(mp->pages); + } + + free(mp); + } + return 0; +} + +int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) { + TDB_MPFILE *mpf; + + if ((mpf = (TDB_MPFILE *)calloc(1, sizeof(*mpf))) == NULL) { + return -1; + } + + mpf->fd = -1; + + if ((mpf->fname = strdup(fname)) == NULL) { + goto _err; + } + + if ((mpf->fd = open(fname, O_CREAT | O_RDWR, 0755)) < 0) { + goto _err; + } + + if (tdbGnrtFileID(fname, mpf->fileid, false) < 0) { + goto _err; + } + + // Register current MPF to MP + tdbMPoolRegFile(mp, mpf); + + *mpfp = mpf; + return 0; + +_err: + tdbMPoolFileClose(mpf); + *mpfp = NULL; + return -1; +} + +int tdbMPoolFileClose(TDB_MPFILE *mpf) { + if (mpf) { + if (mpf->fd > 0) { + close(mpf->fd); + } + tfree(mpf->fname); + free(mpf); + } + return 0; +} + +#define MPF_GET_PAGE_BUCKETID(fileid, pgno, nbuckets) \ + ({ \ + uint64_t *tmp = (uint64_t *)fileid; \ + (tmp[0] + tmp[1] + tmp[2] + (pgno)) % (nbuckets); \ + }) + +int tdbMPoolFileNewPage(TDB_MPFILE *mpf, pgno_t *pgno, void *addr) { + // TODO + return 0; +} + +int tdbMPoolFileFreePage(TDB_MPOOL *mpf, pgno_t *pgno, void *addr) { + // TODO + return 0; +} + +int tdbMPoolFileGetPage(TDB_MPFILE *mpf, pgno_t pgno, void *addr) { + pg_t * pagep; + TDB_MPOOL *mp; + pg_list_t *pglist; + + mp = mpf->mp; + + // check if the page already in pool + pglist = mp->pgtab.hashtab + MPF_GET_PAGE_BUCKETID(mpf->fileid, pgno, mp->pgtab.nbucket); + pagep = TD_DLIST_HEAD(pglist); + while (pagep) { + if (memcmp(mpf->fileid, pagep->pgid.fileid, TDB_FILE_ID_LEN) == 0 && pgno == pagep->pgid.pgno) { + break; + } + + pagep = TD_DLIST_NODE_NEXT_WITH_FIELD(pagep, hash); + } + + if (pagep) { + // page is found + // todo: pin the page and return + *(void **)addr = pagep->p; + return 0; + } + + // page not found + pagep = TD_DLIST_HEAD(&mp->freeList); + if (pagep) { + // has free page + TD_DLIST_POP_WITH_FIELD(&(mp->freeList), pagep, free); + } else { + // no free page available + tdbMPoolClockEvictPage(mp, &pagep); + if (pagep) { + if (pagep->dirty) { + // TODO: Handle dirty page eviction + } + } + } + + if (pagep == NULL) { + // no available container page + return -1; + } + + // load page from the disk if a container page is available + // TODO: load the page from the disk + if (tdbMPoolFileReadPage(mpf, pgno, pagep->p) < 0) { + return -1; + } + + memcpy(pagep->pgid.fileid, mpf->fileid, TDB_FILE_ID_LEN); + pagep->pgid.pgno = pgno; + pagep->dirty = 0; + pagep->pinRef = 1; + + // add current page to page table + TD_DLIST_APPEND_WITH_FIELD(pglist, pagep, hash); + + return 0; +} + +int tdbMPoolFilePutPage(TDB_MPFILE *mpf, pgno_t pgno, void *addr) { + // TODO + return 0; +} + +#define MPF_GET_BUCKETID(fileid) \ + ({ \ + uint64_t *tmp = (uint64_t *)fileid; \ + (tmp[0] + tmp[1] + tmp[2]) % MPF_HASH_BUCKETS; \ + }) + +static void tdbMPoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) { + mpf_bucket_t *bktp; + + bktp = mp->mpfht.buckets + MPF_GET_BUCKETID(mpf->fileid); + + taosWLockLatch(&(bktp->latch)); + + TD_DLIST_APPEND_WITH_FIELD(bktp, mpf, node); + + taosWUnLockLatch(&(bktp->latch)); + + mpf->mp = mp; +} + +static TDB_MPFILE *tdbMPoolGetFile(TDB_MPOOL *mp, uint8_t *fileid) { + TDB_MPFILE * mpf = NULL; + mpf_bucket_t *bktp; + + bktp = mp->mpfht.buckets + MPF_GET_BUCKETID(fileid); + + taosRLockLatch(&(bktp->latch)); + + mpf = TD_DLIST_HEAD(bktp); + while (mpf) { + if (memcmp(fileid, mpf->fileid, TDB_FILE_ID_LEN) == 0) { + break; + } + + mpf = TD_DLIST_NODE_NEXT_WITH_FIELD(mpf, node); + } + + taosRUnLockLatch(&(bktp->latch)); + + return mpf; +} + +static void tdbMPoolUnregFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) { + mpf_bucket_t *bktp; + TDB_MPFILE * tmpf; + + if (mpf->mp == NULL) return; + + ASSERT(mpf->mp == mp); + + bktp = mp->mpfht.buckets + MPF_GET_BUCKETID(mpf->fileid); + + taosWLockLatch(&(bktp->latch)); + + tmpf = TD_DLIST_HEAD(bktp); + + while (tmpf) { + if (memcmp(mpf->fileid, tmpf->fileid, TDB_FILE_ID_LEN) == 0) { + TD_DLIST_POP_WITH_FIELD(bktp, tmpf, node); + break; + } + + tmpf = TD_DLIST_NODE_NEXT_WITH_FIELD(tmpf, node); + } + + taosWUnLockLatch(&(bktp->latch)); + + ASSERT(tmpf == mpf); +} + +static int tdbMPoolFileReadPage(TDB_MPFILE *mpf, pgno_t pgno, void *p) { + pgsz_t pgsize; + TDB_MPOOL *mp; + off_t offset; + size_t rsize; + + mp = mpf->mp; + pgsize = mp->pgsize; + offset = pgno * pgsize; + + // TODO: use loop to read all data + rsize = pread(mpf->fd, p, pgsize, offset); + // TODO: error handle + + return 0; +} + +static int tdbMPoolFileWritePage(TDB_MPFILE *mpf, pgno_t pgno, const void *p) { + pgsz_t pgsize; + TDB_MPOOL *mp; + off_t offset; + + mp = mpf->mp; + pgsize = mp->pgsize; + offset = pgno * pgsize; + + lseek(mpf->fd, offset, SEEK_SET); + // TODO: handle error + + write(mpf->fd, p, pgsize); + // TODO: handle error + + return 0; +} + +static void tdbMPoolClockEvictPage(TDB_MPOOL *mp, pg_t **pagepp) { + pg_t * pagep; + frame_id_t och; + + *pagepp = NULL; + och = mp->clockHand; + + do { + pagep = mp->pages + mp->clockHand; + mp->clockHand = (mp->clockHand + 1) % mp->npages; + + if (pagep->pinRef == 0) { + if (pagep->rbit == 1) { + pagep->rbit = 0; + } else { + break; + } + } + + if (mp->clockHand == och) { + return; + } + } while (1); + + *pagepp = pagep; +} + +#endif \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdbJournal.c b/source/libs/tdb/src/db/tdbJournal.c new file mode 100644 index 0000000000..ace622fd72 --- /dev/null +++ b/source/libs/tdb/src/db/tdbJournal.c @@ -0,0 +1,19 @@ +/* + * 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 . + */ + +struct SJournal { + char *jname; + int fd; +}; \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdb_mpool.c b/source/libs/tdb/src/db/tdb_mpool.c deleted file mode 100644 index 2049019970..0000000000 --- a/source/libs/tdb/src/db/tdb_mpool.c +++ /dev/null @@ -1,362 +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 "tdb_mpool.h" - -static void tdbMPoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf); -static void tdbMPoolUnregFile(TDB_MPOOL *mp, TDB_MPFILE *mpf); -static TDB_MPFILE *tdbMPoolGetFile(TDB_MPOOL *mp, uint8_t *fileid); -static int tdbMPoolFileReadPage(TDB_MPFILE *mpf, pgno_t pgno, void *p); -static int tdbMPoolFileWritePage(TDB_MPFILE *mpf, pgno_t pgno, const void *p); -static void tdbMPoolClockEvictPage(TDB_MPOOL *mp, pg_t **pagepp); - -int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsz_t pgsize) { - TDB_MPOOL *mp = NULL; - size_t tsize; - pg_t * pagep; - - // check parameters - if (!TDB_IS_PGSIZE_VLD(pgsize)) { - tdbError("invalid page size"); - return -1; - } - - // allocate handle - mp = (TDB_MPOOL *)calloc(1, sizeof(*mp)); - if (mp == NULL) { - tdbError("failed to malloc memory pool handle"); - goto _err; - } - - // initialize the handle - mp->cachesize = cachesize; - mp->pgsize = pgsize; - mp->npages = cachesize / pgsize; - mp->clockHand = 0; - - TD_DLIST_INIT(&mp->freeList); - - mp->pages = (pg_t *)calloc(mp->npages, sizeof(pg_t)); - if (mp->pages == NULL) { - tdbError("failed to malloc memory pool pages"); - goto _err; - } - - for (frame_id_t i = 0; i < mp->npages; i++) { - mp->pages[i].p = malloc(pgsize); - if (mp->pages[i].p == NULL) { - goto _err; - } - - taosInitRWLatch(&mp->pages[i].rwLatch); - mp->pages[i].frameid = i; - mp->pages[i].pgid = TDB_IVLD_PGID; - - // add new page to the free list - TD_DLIST_APPEND_WITH_FIELD(&(mp->freeList), &(mp->pages[i]), free); - } - -#define PGTAB_FACTOR 1.0 - mp->pgtab.nbucket = mp->npages / PGTAB_FACTOR; - mp->pgtab.hashtab = (pg_list_t *)calloc(mp->pgtab.nbucket, sizeof(pg_list_t)); - if (mp->pgtab.hashtab == NULL) { - tdbError("failed to malloc memory pool hash table"); - goto _err; - } - - // return - *mpp = mp; - return 0; - -_err: - tdbMPoolClose(mp); - *mpp = NULL; - return -1; -} - -int tdbMPoolClose(TDB_MPOOL *mp) { - if (mp) { - tfree(mp->pgtab.hashtab); - if (mp->pages) { - for (int i = 0; i < mp->npages; i++) { - tfree(mp->pages[i].p); - } - - free(mp->pages); - } - - free(mp); - } - return 0; -} - -int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) { - TDB_MPFILE *mpf; - - if ((mpf = (TDB_MPFILE *)calloc(1, sizeof(*mpf))) == NULL) { - return -1; - } - - mpf->fd = -1; - - if ((mpf->fname = strdup(fname)) == NULL) { - goto _err; - } - - if ((mpf->fd = open(fname, O_CREAT | O_RDWR, 0755)) < 0) { - goto _err; - } - - if (tdbGnrtFileID(fname, mpf->fileid, false) < 0) { - goto _err; - } - - // Register current MPF to MP - tdbMPoolRegFile(mp, mpf); - - *mpfp = mpf; - return 0; - -_err: - tdbMPoolFileClose(mpf); - *mpfp = NULL; - return -1; -} - -int tdbMPoolFileClose(TDB_MPFILE *mpf) { - if (mpf) { - if (mpf->fd > 0) { - close(mpf->fd); - } - tfree(mpf->fname); - free(mpf); - } - return 0; -} - -#define MPF_GET_PAGE_BUCKETID(fileid, pgno, nbuckets) \ - ({ \ - uint64_t *tmp = (uint64_t *)fileid; \ - (tmp[0] + tmp[1] + tmp[2] + (pgno)) % (nbuckets); \ - }) - -int tdbMPoolFileNewPage(TDB_MPFILE *mpf, pgno_t *pgno, void *addr) { - // TODO - return 0; -} - -int tdbMPoolFileFreePage(TDB_MPOOL *mpf, pgno_t *pgno, void *addr) { - // TODO - return 0; -} - -int tdbMPoolFileGetPage(TDB_MPFILE *mpf, pgno_t pgno, void *addr) { - pg_t * pagep; - TDB_MPOOL *mp; - pg_list_t *pglist; - - mp = mpf->mp; - - // check if the page already in pool - pglist = mp->pgtab.hashtab + MPF_GET_PAGE_BUCKETID(mpf->fileid, pgno, mp->pgtab.nbucket); - pagep = TD_DLIST_HEAD(pglist); - while (pagep) { - if (memcmp(mpf->fileid, pagep->pgid.fileid, TDB_FILE_ID_LEN) == 0 && pgno == pagep->pgid.pgno) { - break; - } - - pagep = TD_DLIST_NODE_NEXT_WITH_FIELD(pagep, hash); - } - - if (pagep) { - // page is found - // todo: pin the page and return - *(void **)addr = pagep->p; - return 0; - } - - // page not found - pagep = TD_DLIST_HEAD(&mp->freeList); - if (pagep) { - // has free page - TD_DLIST_POP_WITH_FIELD(&(mp->freeList), pagep, free); - } else { - // no free page available - tdbMPoolClockEvictPage(mp, &pagep); - if (pagep) { - if (pagep->dirty) { - // TODO: Handle dirty page eviction - } - } - } - - if (pagep == NULL) { - // no available container page - return -1; - } - - // load page from the disk if a container page is available - // TODO: load the page from the disk - if (tdbMPoolFileReadPage(mpf, pgno, pagep->p) < 0) { - return -1; - } - - memcpy(pagep->pgid.fileid, mpf->fileid, TDB_FILE_ID_LEN); - pagep->pgid.pgno = pgno; - pagep->dirty = 0; - pagep->pinRef = 1; - - // add current page to page table - TD_DLIST_APPEND_WITH_FIELD(pglist, pagep, hash); - - return 0; -} - -int tdbMPoolFilePutPage(TDB_MPFILE *mpf, pgno_t pgno, void *addr) { - // TODO - return 0; -} - -#define MPF_GET_BUCKETID(fileid) \ - ({ \ - uint64_t *tmp = (uint64_t *)fileid; \ - (tmp[0] + tmp[1] + tmp[2]) % MPF_HASH_BUCKETS; \ - }) - -static void tdbMPoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) { - mpf_bucket_t *bktp; - - bktp = mp->mpfht.buckets + MPF_GET_BUCKETID(mpf->fileid); - - taosWLockLatch(&(bktp->latch)); - - TD_DLIST_APPEND_WITH_FIELD(bktp, mpf, node); - - taosWUnLockLatch(&(bktp->latch)); - - mpf->mp = mp; -} - -static TDB_MPFILE *tdbMPoolGetFile(TDB_MPOOL *mp, uint8_t *fileid) { - TDB_MPFILE * mpf = NULL; - mpf_bucket_t *bktp; - - bktp = mp->mpfht.buckets + MPF_GET_BUCKETID(fileid); - - taosRLockLatch(&(bktp->latch)); - - mpf = TD_DLIST_HEAD(bktp); - while (mpf) { - if (memcmp(fileid, mpf->fileid, TDB_FILE_ID_LEN) == 0) { - break; - } - - mpf = TD_DLIST_NODE_NEXT_WITH_FIELD(mpf, node); - } - - taosRUnLockLatch(&(bktp->latch)); - - return mpf; -} - -static void tdbMPoolUnregFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) { - mpf_bucket_t *bktp; - TDB_MPFILE * tmpf; - - if (mpf->mp == NULL) return; - - ASSERT(mpf->mp == mp); - - bktp = mp->mpfht.buckets + MPF_GET_BUCKETID(mpf->fileid); - - taosWLockLatch(&(bktp->latch)); - - tmpf = TD_DLIST_HEAD(bktp); - - while (tmpf) { - if (memcmp(mpf->fileid, tmpf->fileid, TDB_FILE_ID_LEN) == 0) { - TD_DLIST_POP_WITH_FIELD(bktp, tmpf, node); - break; - } - - tmpf = TD_DLIST_NODE_NEXT_WITH_FIELD(tmpf, node); - } - - taosWUnLockLatch(&(bktp->latch)); - - ASSERT(tmpf == mpf); -} - -static int tdbMPoolFileReadPage(TDB_MPFILE *mpf, pgno_t pgno, void *p) { - pgsz_t pgsize; - TDB_MPOOL *mp; - off_t offset; - size_t rsize; - - mp = mpf->mp; - pgsize = mp->pgsize; - offset = pgno * pgsize; - - // TODO: use loop to read all data - rsize = pread(mpf->fd, p, pgsize, offset); - // TODO: error handle - - return 0; -} - -static int tdbMPoolFileWritePage(TDB_MPFILE *mpf, pgno_t pgno, const void *p) { - pgsz_t pgsize; - TDB_MPOOL *mp; - off_t offset; - - mp = mpf->mp; - pgsize = mp->pgsize; - offset = pgno * pgsize; - - lseek(mpf->fd, offset, SEEK_SET); - // TODO: handle error - - write(mpf->fd, p, pgsize); - // TODO: handle error - - return 0; -} - -static void tdbMPoolClockEvictPage(TDB_MPOOL *mp, pg_t **pagepp) { - pg_t * pagep; - frame_id_t och; - - *pagepp = NULL; - och = mp->clockHand; - - do { - pagep = mp->pages + mp->clockHand; - mp->clockHand = (mp->clockHand + 1) % mp->npages; - - if (pagep->pinRef == 0) { - if (pagep->rbit == 1) { - pagep->rbit = 0; - } else { - break; - } - } - - if (mp->clockHand == och) { - return; - } - } while (1); - - *pagepp = pagep; -} \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbJournal.h b/source/libs/tdb/src/inc/tdbJournal.h new file mode 100644 index 0000000000..685e2bcb16 --- /dev/null +++ b/source/libs/tdb/src/inc/tdbJournal.h @@ -0,0 +1,29 @@ +/* + * 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; + +#ifdef __cplusplus +} +#endif + +#endif /*_TDB_JOURNAL_H_*/ \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdb_mpool.h b/source/libs/tdb/src/inc/tdb_mpool.h deleted file mode 100644 index ba5d5f132e..0000000000 --- a/source/libs/tdb/src/inc/tdb_mpool.h +++ /dev/null @@ -1,94 +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 _TD_TDB_MPOOL_H_ -#define _TD_TDB_MPOOL_H_ - -#include "tdbInt.h" - -#ifdef __cplusplus -extern "C" { -#endif - -// Exposed handle -typedef struct TDB_MPOOL TDB_MPOOL; -typedef struct TDB_MPFILE TDB_MPFILE; - -typedef TD_DLIST_NODE(pg_t) pg_free_dlist_node_t, pg_hash_dlist_node_t; -typedef struct pg_t { - SRWLatch rwLatch; - frame_id_t frameid; - pgid_t pgid; - uint8_t dirty; - uint8_t rbit; - int32_t pinRef; - pg_free_dlist_node_t free; - pg_hash_dlist_node_t hash; - void * p; -} pg_t; - -typedef TD_DLIST(pg_t) pg_list_t; -typedef struct { - SRWLatch latch; - TD_DLIST(TDB_MPFILE); -} mpf_bucket_t; -struct TDB_MPOOL { - int64_t cachesize; - pgsz_t pgsize; - int32_t npages; - pg_t * pages; - pg_list_t freeList; - frame_id_t clockHand; - struct { - int32_t nbucket; - pg_list_t *hashtab; - } pgtab; // page table, hash - struct { -#define MPF_HASH_BUCKETS 16 - mpf_bucket_t buckets[MPF_HASH_BUCKETS]; - } mpfht; // MPF hash table. MPFs using this MP will be put in this hash table -}; - -#define MP_PAGE_AT(mp, idx) (mp)->pages[idx] - -typedef TD_DLIST_NODE(TDB_MPFILE) td_mpf_dlist_node_t; -struct TDB_MPFILE { - char * fname; // file name - int fd; // fd - uint8_t fileid[TDB_FILE_ID_LEN]; // file ID - TDB_MPOOL * mp; // underlying memory pool - td_mpf_dlist_node_t node; -}; - -/*=================================================== Exposed apis ==================================================*/ -// TDB_MPOOL -int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsz_t pgsize); -int tdbMPoolClose(TDB_MPOOL *mp); -int tdbMPoolSync(TDB_MPOOL *mp); - -// TDB_MPFILE -int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp); -int tdbMPoolFileClose(TDB_MPFILE *mpf); -int tdbMPoolFileNewPage(TDB_MPFILE *mpf, pgno_t *pgno, void *addr); -int tdbMPoolFileFreePage(TDB_MPOOL *mpf, pgno_t *pgno, void *addr); -int tdbMPoolFileGetPage(TDB_MPFILE *mpf, pgno_t pgno, void *addr); -int tdbMPoolFilePutPage(TDB_MPFILE *mpf, pgno_t pgno, void *addr); -int tdbMPoolFileSync(TDB_MPFILE *mpf); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_TDB_MPOOL_H_*/ \ No newline at end of file diff --git a/source/libs/tdb/test/CMakeLists.txt b/source/libs/tdb/test/CMakeLists.txt index 7fbfaf5506..5cac816493 100644 --- a/source/libs/tdb/test/CMakeLists.txt +++ b/source/libs/tdb/test/CMakeLists.txt @@ -1,7 +1,3 @@ -# tdbMPoolTest -add_executable(tdbMPoolTest "tdbMPoolTest.cpp") -target_link_libraries(tdbMPoolTest tdb gtest gtest_main) - -# tdbTest -add_executable(tdbTest "tdbTest.cpp") -target_link_libraries(tdbTest tdb gtest gtest_main) \ No newline at end of file +# # tdbTest +# add_executable(tdbTest "tdbTest.cpp") +# target_link_libraries(tdbTest tdb gtest gtest_main) \ No newline at end of file diff --git a/source/libs/tdb/test/tdbMPoolTest.cpp b/source/libs/tdb/test/tdbMPoolTest.cpp deleted file mode 100644 index 17381759fb..0000000000 --- a/source/libs/tdb/test/tdbMPoolTest.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "gtest/gtest.h" - -#include - -#include "tdb_mpool.h" - -TEST(tdb_mpool_test, test1) { - TDB_MPOOL * mp; - TDB_MPFILE *mpf; - pgno_t pgno; - void * pgdata; - - // open mp - tdbMPoolOpen(&mp, 16384, 4096); - - // open mpf - tdbMPoolFileOpen(&mpf, "test.db", mp); - -#define TEST1_TOTAL_PAGES 100 - for (int i = 0; i < TEST1_TOTAL_PAGES; i++) { - tdbMPoolFileNewPage(mpf, &pgno, pgdata); - - *(pgno_t *)pgdata = i; - } - - // close mpf - tdbMPoolFileClose(mpf); - - // close mp - tdbMPoolClose(mp); -} From 2d610f125b03c9439d6390f5e962ac93bb5d10be Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 14 Feb 2022 10:10:47 +0000 Subject: [PATCH 45/79] refact TDB --- source/libs/tdb/src/db/{btree.c => tdbBtree.c} | 0 source/libs/tdb/src/db/tdbEnv.c | 10 ++++++++++ source/libs/tdb/src/db/{pgcache.c => tdbPgCache.c} | 0 source/libs/tdb/src/db/{pgfile.c => tdbPgFile.c} | 0 source/libs/tdb/src/inc/{btree.h => tdbBtree.h} | 0 source/libs/tdb/src/inc/tdbInt.h | 8 +++++--- source/libs/tdb/src/inc/{pgcache.h => tdbPgCache.h} | 0 source/libs/tdb/src/inc/{pgfile.h => tdbPgFile.h} | 0 8 files changed, 15 insertions(+), 3 deletions(-) rename source/libs/tdb/src/db/{btree.c => tdbBtree.c} (100%) rename source/libs/tdb/src/db/{pgcache.c => tdbPgCache.c} (100%) rename source/libs/tdb/src/db/{pgfile.c => tdbPgFile.c} (100%) rename source/libs/tdb/src/inc/{btree.h => tdbBtree.h} (100%) rename source/libs/tdb/src/inc/{pgcache.h => tdbPgCache.h} (100%) rename source/libs/tdb/src/inc/{pgfile.h => tdbPgFile.h} (100%) diff --git a/source/libs/tdb/src/db/btree.c b/source/libs/tdb/src/db/tdbBtree.c similarity index 100% rename from source/libs/tdb/src/db/btree.c rename to source/libs/tdb/src/db/tdbBtree.c diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index cbc1ef3417..77063c9b99 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -100,4 +100,14 @@ SPgCache *tdbEnvGetPgCache(TENV *pEnv) { return pEnv->pPgCache; } static int tdbEnvDestroy(TENV *pEnv) { // TODO return 0; +} + +int tdbEnvBeginTxn(TENV *pEnv) { + // TODO + return 0; +} + +int tdbEnvCommit(TENV *pEnv) { + // TODO + return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/db/pgcache.c b/source/libs/tdb/src/db/tdbPgCache.c similarity index 100% rename from source/libs/tdb/src/db/pgcache.c rename to source/libs/tdb/src/db/tdbPgCache.c diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/tdbPgFile.c similarity index 100% rename from source/libs/tdb/src/db/pgfile.c rename to source/libs/tdb/src/db/tdbPgFile.c diff --git a/source/libs/tdb/src/inc/btree.h b/source/libs/tdb/src/inc/tdbBtree.h similarity index 100% rename from source/libs/tdb/src/inc/btree.h rename to source/libs/tdb/src/inc/tdbBtree.h diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index cc79ccaf39..6cb8f3891b 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -113,11 +113,13 @@ typedef TD_DLIST(SPgFile) SPgFileList; #include "tdbUtil.h" -#include "btree.h" +#include "tdbBtree.h" -#include "pgcache.h" +#include "tdbPgCache.h" -#include "pgfile.h" +#include "tdbPgFile.h" + +#include "tdbJournal.h" #include "tdbEnv.h" diff --git a/source/libs/tdb/src/inc/pgcache.h b/source/libs/tdb/src/inc/tdbPgCache.h similarity index 100% rename from source/libs/tdb/src/inc/pgcache.h rename to source/libs/tdb/src/inc/tdbPgCache.h diff --git a/source/libs/tdb/src/inc/pgfile.h b/source/libs/tdb/src/inc/tdbPgFile.h similarity index 100% rename from source/libs/tdb/src/inc/pgfile.h rename to source/libs/tdb/src/inc/tdbPgFile.h From b3575d7c8959681fb807bf97cbbd0ad8933479be Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 14 Feb 2022 11:05:35 +0000 Subject: [PATCH 46/79] more TDB --- source/libs/tdb/inc/tdb.h | 3 ++- source/libs/tdb/src/db/tdb.c | 4 ++++ source/libs/tdb/src/db/tdbEnv.c | 1 + source/libs/tdb/src/inc/tdbPgFile.h | 1 + 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 1df194e24a..39a62fcc64 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -50,11 +50,12 @@ int tdbClose(TDB *pDb); int tdbSetKeyLen(TDB *pDb, int klen); int tdbSetValLen(TDB *pDb, int vlen); int tdbSetDup(TDB *pDb, int dup); - int tdbGetKeyLen(TDB *pDb, int *pklen); int tdbGetValLen(TDB *pDb, int *pvlen); int tdbGetDup(TDB *pDb, int *pdup); +int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData); + // TDBC #ifdef __cplusplus diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 2e2e772336..3febe1a4e4 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -21,6 +21,10 @@ struct STDb { TENV * pEnv; // TENV containing the DB }; +struct STDbCurosr { + SBtCursor *pBtCur; +}; + int tdbCreate(TDB **ppDb) { TDB *pDb; diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 77063c9b99..68a4212003 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -23,6 +23,7 @@ struct STDbEnv { SPgCache * pPgCache; // page cache struct { } pgfht; // page file hash table; + SJournal *pJournal; }; static int tdbEnvDestroy(TENV *pEnv); diff --git a/source/libs/tdb/src/inc/tdbPgFile.h b/source/libs/tdb/src/inc/tdbPgFile.h index 67d81ffbb1..84d8319c61 100644 --- a/source/libs/tdb/src/inc/tdbPgFile.h +++ b/source/libs/tdb/src/inc/tdbPgFile.h @@ -39,6 +39,7 @@ struct SPgFile { pgsz_t pgSize; int fd; pgno_t pgFileSize; + TDB * pDb; // For a SPgFile for multiple databases, this is the mapping DB. }; int pgFileOpen(SPgFile **ppPgFile, const char *fname, SPgCache *pPgCache); From e835a6cc3992e70c396c3b66c7662cb990d7ef55 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 03:54:25 +0000 Subject: [PATCH 47/79] more TDB --- source/libs/tdb/test/CMakeLists.txt | 6 +++--- source/libs/tdb/test/tdbTest.cpp | 31 +++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/source/libs/tdb/test/CMakeLists.txt b/source/libs/tdb/test/CMakeLists.txt index 5cac816493..2d77c1f4e9 100644 --- a/source/libs/tdb/test/CMakeLists.txt +++ b/source/libs/tdb/test/CMakeLists.txt @@ -1,3 +1,3 @@ -# # tdbTest -# add_executable(tdbTest "tdbTest.cpp") -# target_link_libraries(tdbTest tdb gtest gtest_main) \ No newline at end of file +# tdbTest +add_executable(tdbTest "tdbTest.cpp") +target_link_libraries(tdbTest tdb gtest gtest_main) \ No newline at end of file diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 113bb2560f..93eeef7b32 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -2,13 +2,32 @@ #include "tdb.h" -TEST(tdb_api_test, tdb_create_open_close_db_test) { - // int ret; - // TDB *dbp; +#define A_ASSERT(op) GTEST_ASSERT_EQ(op, 0) - // tdbCreateDB(&dbp, TDB_BTREE_T); +TEST(tdb_test, simple_test) { + TENV *pEnv; + TDB * pDb1, *pDb2; - // tdbOpenDB(dbp, 0); + // ENV + tdbEnvCreate(&pEnv); + tdbEnvSetPageSize(pEnv, 1024); + tdbEnvSetCacheSize(pEnv, 10240); + tdbEnvOpen(&pEnv); - // tdbCloseDB(dbp, 0); + // DB + tdbOpen(&pDb1, "db.db", "db1", pEnv); + tdbOpen(&pDb2, "db.db", "db2", pEnv); + + // Insert + + // Query + + // Delete + + // Query + + // Close + tdbClose(pDb1); + tdbClose(pDb2); + tdbEnvClose(pEnv); } \ No newline at end of file From 7c939b929cfaa65fd050d1c41a83217c3245d606 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 05:50:30 +0000 Subject: [PATCH 48/79] more TDB --- source/dnode/vnode/CMakeLists.txt | 2 +- source/libs/tdb/inc/tdb.h | 7 ++--- source/libs/tdb/src/db/tdb.c | 6 ++-- source/libs/tdb/src/db/tdbEnv.c | 49 ++++++++++++++++++------------- source/libs/tdb/src/inc/tdbInt.h | 2 +- source/libs/tdb/test/tdbTest.cpp | 24 ++++++++++----- 6 files changed, 52 insertions(+), 38 deletions(-) diff --git a/source/dnode/vnode/CMakeLists.txt b/source/dnode/vnode/CMakeLists.txt index 429bd2143f..bd633fa70a 100644 --- a/source/dnode/vnode/CMakeLists.txt +++ b/source/dnode/vnode/CMakeLists.txt @@ -1,5 +1,5 @@ set(META_DB_IMPL_LIST "BDB" "TDB") -set(META_DB_IMPL "TDB" CACHE STRING "Use BDB as the default META implementation") +set(META_DB_IMPL "BDB" CACHE STRING "Use BDB as the default META implementation") set_property(CACHE META_DB_IMPL PROPERTY STRINGS ${META_DB_IMPL_LIST}) if(META_DB_IMPL IN_LIST META_DB_IMPL_LIST) diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 39a62fcc64..443c02ff32 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -30,15 +30,14 @@ typedef int32_t pgsz_t; typedef int32_t cachesz_t; // TEVN -int tdbEnvCreate(TENV **ppEnv); -int tdbEnvOpen(TENV **ppEnv); +int tdbEnvCreate(TENV **ppEnv, const char *rootDir); +int tdbEnvOpen(TENV *ppEnv); int tdbEnvClose(TENV *pEnv); int tdbEnvBeginTxn(TENV *pEnv); int tdbEnvCommit(TENV *pEnv); -int tdbEnvSetPageSize(TENV *pEnv, pgsz_t szPage); -int tdbEnvSetCacheSize(TENV *pEnv, cachesz_t szCache); +int tdbEnvSetCache(TENV *pEnv, pgsz_t pgSize, cachesz_t cacheSize); pgsz_t tdbEnvGetPageSize(TENV *pEnv); cachesz_t tdbEnvGetCacheSize(TENV *pEnv); diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 3febe1a4e4..b983ce43dd 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -64,9 +64,9 @@ int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { // Create a default ENV if pEnv is not set if (pEnv == NULL) { - if ((ret = tdbEnvOpen(&pEnv)) != 0) { - return -1; - } + // if ((ret = tdbEnvOpen(&pEnv)) != 0) { + // return -1; + // } } pDb->pEnv = pEnv; diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 68a4212003..0f1002c4d1 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -16,8 +16,9 @@ #include "tdbInt.h" struct STDbEnv { - pgsz_t pgSize; // Page size - cachesz_t cacheSize; // Total cache size + char * rootDir; // root directory of the environment + pgsz_t pgSize; // page size + cachesz_t cacheSize; // total cache size STDbList dbList; // TDB List SPgFileList pgfList; // SPgFile List SPgCache * pPgCache; // page cache @@ -28,34 +29,39 @@ struct STDbEnv { static int tdbEnvDestroy(TENV *pEnv); -int tdbEnvCreate(TENV **ppEnv) { - TENV *pEnv; +int tdbEnvCreate(TENV **ppEnv, const char *rootDir) { + TENV * pEnv; + size_t slen; - pEnv = (TENV *)calloc(1, sizeof(*pEnv)); + ASSERT(rootDir != NULL); + + *ppEnv = NULL; + slen = strlen(rootDir); + pEnv = (TENV *)calloc(1, sizeof(*pEnv) + slen + 1); if (pEnv == NULL) { return -1; } + pEnv->rootDir = (char *)(&pEnv[1]); pEnv->pgSize = TDB_DEFAULT_PGSIZE; pEnv->cacheSize = TDB_DEFAULT_CACHE_SIZE; + memcpy(pEnv->rootDir, rootDir, slen); + TD_DLIST_INIT(&(pEnv->dbList)); TD_DLIST_INIT(&(pEnv->pgfList)); - // TODO + + /* TODO */ + + *ppEnv = pEnv; return 0; } -int tdbEnvOpen(TENV **ppEnv) { - TENV * pEnv; +int tdbEnvOpen(TENV *pEnv) { SPgCache *pPgCache; int ret; - // Create the ENV with default setting - if (ppEnv == NULL) { - TERR_A(ret, tdbEnvCreate(&pEnv), _err); - } - - pEnv = *ppEnv; + ASSERT(pEnv != NULL); TERR_A(ret, pgCacheCreate(&pPgCache, pEnv->pgSize, pEnv->cacheSize / pEnv->pgSize), _err); TERR_A(ret, pgCacheOpen(&pPgCache), _err); @@ -75,15 +81,16 @@ int tdbEnvClose(TENV *pEnv) { return 0; } -int tdbEnvSetPageSize(TENV *pEnv, pgsz_t szPage) { - /* TODO */ - pEnv->pgSize = szPage; - return 0; -} +int tdbEnvSetCache(TENV *pEnv, pgsz_t pgSize, cachesz_t cacheSize) { + if (!TDB_IS_PGSIZE_VLD(pgSize) || cacheSize / pgSize < 10) { + return -1; + } -int tdbEnvSetCacheSize(TENV *pEnv, cachesz_t szCache) { /* TODO */ - pEnv->cacheSize = szCache; + + pEnv->pgSize = pgSize; + pEnv->cacheSize = cacheSize; + return 0; } diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 6cb8f3891b..cafd1356cf 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -74,7 +74,7 @@ typedef int32_t frame_id_t; typedef pgsz_t pgoff_t; // cache -#define TDB_DEFAULT_CACHE_SIZE (256 * 1024) // 256K +#define TDB_DEFAULT_CACHE_SIZE (256 * 4096) // 1M // tdb_log #define tdbError(var) diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 93eeef7b32..a458ea3e84 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -2,21 +2,28 @@ #include "tdb.h" -#define A_ASSERT(op) GTEST_ASSERT_EQ(op, 0) - TEST(tdb_test, simple_test) { - TENV *pEnv; - TDB * pDb1, *pDb2; + TENV* pEnv; + TDB * pDb1, *pDb2, *pDb3; + pgsz_t pgSize = 1024; + cachesz_t cacheSize = 10240; // ENV - tdbEnvCreate(&pEnv); - tdbEnvSetPageSize(pEnv, 1024); - tdbEnvSetCacheSize(pEnv, 10240); - tdbEnvOpen(&pEnv); + GTEST_ASSERT_EQ(tdbEnvCreate(&pEnv, "./tdbtest"), 0); + GTEST_ASSERT_EQ(tdbEnvSetCache(pEnv, pgSize, cacheSize), 0); + + GTEST_ASSERT_EQ(tdbEnvGetCacheSize(pEnv), cacheSize); + + GTEST_ASSERT_EQ(tdbEnvGetPageSize(pEnv), pgSize); + + GTEST_ASSERT_EQ(tdbEnvOpen(pEnv), 0); + +#if 0 // DB tdbOpen(&pDb1, "db.db", "db1", pEnv); tdbOpen(&pDb2, "db.db", "db2", pEnv); + tdbOpen(&pDb3, "index.db", NULL, pEnv); // Insert @@ -29,5 +36,6 @@ TEST(tdb_test, simple_test) { // Close tdbClose(pDb1); tdbClose(pDb2); +#endif tdbEnvClose(pEnv); } \ No newline at end of file From cc893cf96d259b6b98efdef21f8f3b75b1920df9 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 06:24:58 +0000 Subject: [PATCH 49/79] more TDB --- source/libs/tdb/src/db/tdbEnv.c | 11 +++++++++-- source/libs/tdb/src/db/tdbPgCache.c | 15 --------------- source/libs/tdb/src/inc/tdbPgCache.h | 2 -- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 0f1002c4d1..4057bd86e6 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -63,8 +63,15 @@ int tdbEnvOpen(TENV *pEnv) { ASSERT(pEnv != NULL); - TERR_A(ret, pgCacheCreate(&pPgCache, pEnv->pgSize, pEnv->cacheSize / pEnv->pgSize), _err); - TERR_A(ret, pgCacheOpen(&pPgCache), _err); + /* TODO: here we do not need to create the root directory, more + * work should be done here + */ + mkdir(pEnv->rootDir, 0755); + + ret = pgCacheCreate(&pPgCache, pEnv->pgSize, pEnv->cacheSize / pEnv->pgSize); + if (ret != 0) { + goto _err; + } pEnv->pPgCache = pPgCache; diff --git a/source/libs/tdb/src/db/tdbPgCache.c b/source/libs/tdb/src/db/tdbPgCache.c index 3d0d770f38..466a9b2eaf 100644 --- a/source/libs/tdb/src/db/tdbPgCache.c +++ b/source/libs/tdb/src/db/tdbPgCache.c @@ -88,21 +88,6 @@ int pgCacheDestroy(SPgCache *pPgCache) { return 0; } -int pgCacheOpen(SPgCache **ppPgCache) { - if (*ppPgCache == NULL) { - if (pgCacheCreate(ppPgCache, TDB_DEFAULT_PGSIZE, TDB_DEFAULT_CACHE_SIZE / TDB_DEFAULT_PGSIZE) < 0) { - return -1; - } - } - // TODO - return 0; -} - -int pgCacheClose(SPgCache *pPgCache) { - // TODO - return 0; -} - #define PG_CACHE_HASH(fileid, pgno) \ ({ \ uint64_t *tmp = (uint64_t *)(fileid); \ diff --git a/source/libs/tdb/src/inc/tdbPgCache.h b/source/libs/tdb/src/inc/tdbPgCache.h index 4cea86dc6d..de9e254a92 100644 --- a/source/libs/tdb/src/inc/tdbPgCache.h +++ b/source/libs/tdb/src/inc/tdbPgCache.h @@ -26,8 +26,6 @@ typedef struct SPage SPage; // SPgCache int pgCacheCreate(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage); int pgCacheDestroy(SPgCache *pPgCache); -int pgCacheOpen(SPgCache **ppPgCache); -int pgCacheClose(SPgCache *pPgCache); SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid); int pgCacheRelease(SPage *pPage); From 933fbaffe8ccda4c4a21fea615730c216540ad28 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 06:27:27 +0000 Subject: [PATCH 50/79] more TDB --- source/libs/tdb/src/db/tdbEnv.c | 2 +- source/libs/tdb/src/db/tdbPgCache.c | 10 +++++----- source/libs/tdb/src/inc/tdbPgCache.h | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 4057bd86e6..3e693e1753 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -68,7 +68,7 @@ int tdbEnvOpen(TENV *pEnv) { */ mkdir(pEnv->rootDir, 0755); - ret = pgCacheCreate(&pPgCache, pEnv->pgSize, pEnv->cacheSize / pEnv->pgSize); + ret = pgCacheOpen(&pPgCache, pEnv->pgSize, pEnv->cacheSize / pEnv->pgSize); if (ret != 0) { goto _err; } diff --git a/source/libs/tdb/src/db/tdbPgCache.c b/source/libs/tdb/src/db/tdbPgCache.c index 466a9b2eaf..0a222c152e 100644 --- a/source/libs/tdb/src/db/tdbPgCache.c +++ b/source/libs/tdb/src/db/tdbPgCache.c @@ -17,7 +17,7 @@ static void pgCachePinPage(SPage *pPage); static void pgCacheUnpinPage(SPage *pPage); -int pgCacheCreate(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage) { +int pgCacheOpen(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage) { SPgCache *pPgCache; SPage * pPage; @@ -38,7 +38,7 @@ int pgCacheCreate(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage) { pPgCache->pages = (SPage *)calloc(npage, sizeof(SPage)); if (pPgCache->pages == NULL) { - pgCacheDestroy(pPgCache); + pgCacheClose(pPgCache); return -1; } @@ -52,14 +52,14 @@ int pgCacheCreate(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage) { pPage->pData = (uint8_t *)calloc(1, pgSize); if (pPage->pData == NULL) { - pgCacheDestroy(pPgCache); + pgCacheClose(pPgCache); return -1; } pPgCache->pght.nbucket = npage; pPgCache->pght.buckets = (SPgList *)calloc(pPgCache->pght.nbucket, sizeof(SPgList)); if (pPgCache->pght.buckets == NULL) { - pgCacheDestroy(pPgCache); + pgCacheClose(pPgCache); return -1; } @@ -70,7 +70,7 @@ int pgCacheCreate(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage) { return 0; } -int pgCacheDestroy(SPgCache *pPgCache) { +int pgCacheClose(SPgCache *pPgCache) { SPage *pPage; if (pPgCache) { tfree(pPgCache->pght.buckets); diff --git a/source/libs/tdb/src/inc/tdbPgCache.h b/source/libs/tdb/src/inc/tdbPgCache.h index de9e254a92..d1a01903d5 100644 --- a/source/libs/tdb/src/inc/tdbPgCache.h +++ b/source/libs/tdb/src/inc/tdbPgCache.h @@ -24,8 +24,8 @@ typedef struct SPgCache SPgCache; typedef struct SPage SPage; // SPgCache -int pgCacheCreate(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage); -int pgCacheDestroy(SPgCache *pPgCache); +int pgCacheOpen(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage); +int pgCacheClose(SPgCache *pPgCache); SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid); int pgCacheRelease(SPage *pPage); From 3ccb52e424adb4d7d8c130ae2e61875123498597 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 06:35:35 +0000 Subject: [PATCH 51/79] more TDB --- source/libs/tdb/src/db/tdbEnv.c | 9 ++++++--- source/libs/tdb/src/db/tdbPgCache.c | 4 +--- source/libs/tdb/src/inc/tdbPgCache.h | 2 +- source/libs/tdb/test/tdbTest.cpp | 1 + 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 3e693e1753..9cecd82987 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -59,6 +59,8 @@ int tdbEnvCreate(TENV **ppEnv, const char *rootDir) { int tdbEnvOpen(TENV *pEnv) { SPgCache *pPgCache; + pgsz_t pgSize; + int npage; int ret; ASSERT(pEnv != NULL); @@ -68,13 +70,14 @@ int tdbEnvOpen(TENV *pEnv) { */ mkdir(pEnv->rootDir, 0755); - ret = pgCacheOpen(&pPgCache, pEnv->pgSize, pEnv->cacheSize / pEnv->pgSize); + pgSize = pEnv->pgSize; + npage = pEnv->cacheSize / pEnv->pgSize; + ret = pgCacheOpen(&pPgCache, pgSize, npage, pEnv); if (ret != 0) { goto _err; } pEnv->pPgCache = pPgCache; - return 0; _err: @@ -83,7 +86,7 @@ _err: int tdbEnvClose(TENV *pEnv) { if (pEnv == NULL) return 0; - /* TODO */ + pgCacheClose(pEnv->pPgCache); tdbEnvDestroy(pEnv); return 0; } diff --git a/source/libs/tdb/src/db/tdbPgCache.c b/source/libs/tdb/src/db/tdbPgCache.c index 0a222c152e..9a8ea62863 100644 --- a/source/libs/tdb/src/db/tdbPgCache.c +++ b/source/libs/tdb/src/db/tdbPgCache.c @@ -17,7 +17,7 @@ static void pgCachePinPage(SPage *pPage); static void pgCacheUnpinPage(SPage *pPage); -int pgCacheOpen(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage) { +int pgCacheOpen(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage, TENV *pEnv) { SPgCache *pPgCache; SPage * pPage; @@ -147,8 +147,6 @@ static void pgCacheUnpinPage(SPage *pPage) { // TODO } - - #if 0 // Exposed handle typedef struct TDB_MPOOL TDB_MPOOL; diff --git a/source/libs/tdb/src/inc/tdbPgCache.h b/source/libs/tdb/src/inc/tdbPgCache.h index d1a01903d5..35a8b9d1fc 100644 --- a/source/libs/tdb/src/inc/tdbPgCache.h +++ b/source/libs/tdb/src/inc/tdbPgCache.h @@ -24,7 +24,7 @@ typedef struct SPgCache SPgCache; typedef struct SPage SPage; // SPgCache -int pgCacheOpen(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage); +int pgCacheOpen(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage, TENV *pEnv); int pgCacheClose(SPgCache *pPgCache); SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid); diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index a458ea3e84..ccaddb4e14 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -37,5 +37,6 @@ TEST(tdb_test, simple_test) { tdbClose(pDb1); tdbClose(pDb2); #endif + tdbEnvClose(pEnv); } \ No newline at end of file From f6f6a69160c9371e4f7193405a8326e72767b9f9 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 06:40:55 +0000 Subject: [PATCH 52/79] refact TDB --- source/libs/tdb/src/db/tdbPgCache.c | 26 ++++++++++++++++++++++++++ source/libs/tdb/src/db/tdbPgFile.c | 3 ++- source/libs/tdb/src/inc/tdbPgCache.h | 25 ------------------------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPgCache.c b/source/libs/tdb/src/db/tdbPgCache.c index 9a8ea62863..273b1abe4b 100644 --- a/source/libs/tdb/src/db/tdbPgCache.c +++ b/source/libs/tdb/src/db/tdbPgCache.c @@ -14,6 +14,32 @@ */ #include "tdbInt.h" +typedef TD_DLIST_NODE(SPage) SPgListNode; +struct SPage { + pgid_t pgid; // page id + frame_id_t frameid; // frame id + SPgListNode freeNode; // for SPgCache.freeList + SPgListNode pghtNode; // for pght + SPgListNode lruNode; // for LRU + uint8_t * pData; // real data +}; + +typedef TD_DLIST(SPage) SPgList; +struct SPgCache { + TENV * pEnv; // TENV containing this page cache + SRWLatch mutex; + pgsz_t pgsize; + int32_t npage; + SPage * pages; + SPgList freeList; + SPgList lru; + struct { + int32_t nbucket; + SPgList *buckets; + } pght; // page hash table +}; + + static void pgCachePinPage(SPage *pPage); static void pgCacheUnpinPage(SPage *pPage); diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index c03303da38..576d6f3da4 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -86,7 +86,8 @@ SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) { if (1 /*Page is cached, no need to load from file*/) { return pPage; } else { - if (pgFileRead(pPgFile, pgno, pPage->pData) < 0) { + // TODO: handle error + if (pgFileRead(pPgFile, pgno, (void *)pPage) < 0) { // todoerr } return pPage; diff --git a/source/libs/tdb/src/inc/tdbPgCache.h b/source/libs/tdb/src/inc/tdbPgCache.h index 35a8b9d1fc..791d5148fc 100644 --- a/source/libs/tdb/src/inc/tdbPgCache.h +++ b/source/libs/tdb/src/inc/tdbPgCache.h @@ -32,31 +32,6 @@ int pgCacheRelease(SPage *pPage); // SPage -typedef TD_DLIST_NODE(SPage) SPgListNode; -struct SPage { - pgid_t pgid; // page id - frame_id_t frameid; // frame id - SPgListNode freeNode; // for SPgCache.freeList - SPgListNode pghtNode; // for pght - SPgListNode lruNode; // for LRU - uint8_t * pData; // real data -}; - -typedef TD_DLIST(SPage) SPgList; -struct SPgCache { - TENV * pEnv; // TENV containing this page cache - SRWLatch mutex; - pgsz_t pgsize; - int32_t npage; - SPage * pages; - SPgList freeList; - SPgList lru; - struct { - int32_t nbucket; - SPgList *buckets; - } pght; // page hash table -}; - #ifdef __cplusplus } #endif From 1bcb109800bcdd6d10a17900efc265dd3cadfc40 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 07:01:30 +0000 Subject: [PATCH 53/79] more TDB --- source/libs/tdb/src/db/tdbEnv.c | 2 +- source/libs/tdb/src/db/tdbPgCache.c | 35 +++++++++++++++++----------- source/libs/tdb/src/inc/tdbPgCache.h | 2 +- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 9cecd82987..9eb7a74b1e 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -72,7 +72,7 @@ int tdbEnvOpen(TENV *pEnv) { pgSize = pEnv->pgSize; npage = pEnv->cacheSize / pEnv->pgSize; - ret = pgCacheOpen(&pPgCache, pgSize, npage, pEnv); + ret = pgCacheOpen(&pPgCache, pEnv); if (ret != 0) { goto _err; } diff --git a/source/libs/tdb/src/db/tdbPgCache.c b/source/libs/tdb/src/db/tdbPgCache.c index 273b1abe4b..c62db61746 100644 --- a/source/libs/tdb/src/db/tdbPgCache.c +++ b/source/libs/tdb/src/db/tdbPgCache.c @@ -26,42 +26,48 @@ struct SPage { typedef TD_DLIST(SPage) SPgList; struct SPgCache { - TENV * pEnv; // TENV containing this page cache - SRWLatch mutex; - pgsz_t pgsize; - int32_t npage; - SPage * pages; - SPgList freeList; - SPgList lru; + TENV * pEnv; // TENV containing this page cache + pgsz_t pgsize; + int32_t npage; + SPage * pages; + SPgList freeList; + SPgList lru; struct { int32_t nbucket; SPgList *buckets; } pght; // page hash table }; - static void pgCachePinPage(SPage *pPage); static void pgCacheUnpinPage(SPage *pPage); -int pgCacheOpen(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage, TENV *pEnv) { +int pgCacheOpen(SPgCache **ppPgCache, TENV *pEnv) { SPgCache *pPgCache; SPage * pPage; + pgsz_t pgSize; + cachesz_t cacheSize; + int32_t npage; *ppPgCache = NULL; + pgSize = tdbEnvGetPageSize(pEnv); + cacheSize = tdbEnvGetCacheSize(pEnv); + npage = cacheSize / pgSize; - if (!TDB_IS_PGSIZE_VLD(pgSize)) { - return -1; - } - + // Allocate the handle pPgCache = (SPgCache *)calloc(1, sizeof(*pPgCache)); if (pPgCache == NULL) { return -1; } - taosInitRWLatch(&(pPgCache->mutex)); + pPgCache->pEnv = pEnv; pPgCache->pgsize = pgSize; pPgCache->npage = npage; + for (int32_t i = 0; i < npage; i++) { + /* code */ + } + +#if 0 pPgCache->pages = (SPage *)calloc(npage, sizeof(SPage)); if (pPgCache->pages == NULL) { pgCacheClose(pPgCache); @@ -91,6 +97,7 @@ int pgCacheOpen(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage, TENV *pEnv) TD_DLIST_APPEND_WITH_FIELD(&(pPgCache->freeList), pPage, freeNode); } +#endif *ppPgCache = pPgCache; return 0; diff --git a/source/libs/tdb/src/inc/tdbPgCache.h b/source/libs/tdb/src/inc/tdbPgCache.h index 791d5148fc..5bcac688af 100644 --- a/source/libs/tdb/src/inc/tdbPgCache.h +++ b/source/libs/tdb/src/inc/tdbPgCache.h @@ -24,7 +24,7 @@ typedef struct SPgCache SPgCache; typedef struct SPage SPage; // SPgCache -int pgCacheOpen(SPgCache **ppPgCache, pgsz_t pgSize, int32_t npage, TENV *pEnv); +int pgCacheOpen(SPgCache **ppPgCache, TENV *pEnv); int pgCacheClose(SPgCache *pPgCache); SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid); From da49669a2bfc3192a6bb685bda09d7705b43f756 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 07:37:39 +0000 Subject: [PATCH 54/79] refact TDB --- source/libs/tdb/src/db/tdbPgCache.c | 67 +++++++++++++---------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPgCache.c b/source/libs/tdb/src/db/tdbPgCache.c index c62db61746..e6fef996cf 100644 --- a/source/libs/tdb/src/db/tdbPgCache.c +++ b/source/libs/tdb/src/db/tdbPgCache.c @@ -18,10 +18,10 @@ typedef TD_DLIST_NODE(SPage) SPgListNode; struct SPage { pgid_t pgid; // page id frame_id_t frameid; // frame id + uint8_t * pData; // real data SPgListNode freeNode; // for SPgCache.freeList SPgListNode pghtNode; // for pght SPgListNode lruNode; // for LRU - uint8_t * pData; // real data }; typedef TD_DLIST(SPage) SPgList; @@ -29,7 +29,7 @@ struct SPgCache { TENV * pEnv; // TENV containing this page cache pgsz_t pgsize; int32_t npage; - SPage * pages; + SPage **pages; SPgList freeList; SPgList lru; struct { @@ -44,59 +44,58 @@ static void pgCacheUnpinPage(SPage *pPage); int pgCacheOpen(SPgCache **ppPgCache, TENV *pEnv) { SPgCache *pPgCache; SPage * pPage; + void * pData; pgsz_t pgSize; cachesz_t cacheSize; int32_t npage; + int32_t nbucket; + size_t msize; *ppPgCache = NULL; pgSize = tdbEnvGetPageSize(pEnv); cacheSize = tdbEnvGetCacheSize(pEnv); npage = cacheSize / pgSize; + nbucket = npage; + msize = sizeof(*pPgCache) + sizeof(SPage *) * npage + sizeof(SPgList) * nbucket; // Allocate the handle - pPgCache = (SPgCache *)calloc(1, sizeof(*pPgCache)); + pPgCache = (SPgCache *)calloc(1, msize); if (pPgCache == NULL) { return -1; } + // Init the handle pPgCache->pEnv = pEnv; pPgCache->pgsize = pgSize; pPgCache->npage = npage; - - for (int32_t i = 0; i < npage; i++) { - /* code */ - } - -#if 0 - pPgCache->pages = (SPage *)calloc(npage, sizeof(SPage)); - if (pPgCache->pages == NULL) { - pgCacheClose(pPgCache); - return -1; - } + pPgCache->pages = (SPage **)(&pPgCache[1]); + pPgCache->pght.nbucket = nbucket; + pPgCache->pght.buckets = (SPgList *)(&(pPgCache->pages[npage])); TD_DLIST_INIT(&(pPgCache->freeList)); for (int32_t i = 0; i < npage; i++) { - pPage = pPgCache->pages + i; + pData = malloc(pgSize + sizeof(SPage)); + if (pData == NULL) { + return -1; + // TODO: handle error + } + + pPage = POINTER_SHIFT(pData, pgSize); pPage->pgid = TDB_IVLD_PGID; pPage->frameid = i; + pPage->pData = pData; - pPage->pData = (uint8_t *)calloc(1, pgSize); - if (pPage->pData == NULL) { - pgCacheClose(pPgCache); - return -1; - } - - pPgCache->pght.nbucket = npage; - pPgCache->pght.buckets = (SPgList *)calloc(pPgCache->pght.nbucket, sizeof(SPgList)); - if (pPgCache->pght.buckets == NULL) { - pgCacheClose(pPgCache); - return -1; - } - + // add current page to the page cache + pPgCache->pages[i] = pPage; TD_DLIST_APPEND_WITH_FIELD(&(pPgCache->freeList), pPage, freeNode); } + +#if 0 + for (int32_t i = 0; i < nbucket; i++) { + TD_DLIST_INIT(pPgCache->pght.buckets + i); + } #endif *ppPgCache = pPgCache; @@ -106,15 +105,11 @@ int pgCacheOpen(SPgCache **ppPgCache, TENV *pEnv) { int pgCacheClose(SPgCache *pPgCache) { SPage *pPage; if (pPgCache) { - tfree(pPgCache->pght.buckets); - if (pPgCache->pages) { - for (int32_t i = 0; i < pPgCache->npage; i++) { - pPage = pPgCache->pages + i; - tfree(pPage->pData); - } - - free(pPgCache->pages); + for (int32_t i = 0; i < pPgCache->npage; i++) { + pPage = pPgCache->pages[i]; + tfree(pPage->pData); } + free(pPgCache); } From 698377b8302640ff44cb4a44d2af1cda41da419a Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 07:38:26 +0000 Subject: [PATCH 55/79] refact TDB --- source/libs/tdb/src/db/tdbEnv.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 9eb7a74b1e..58612b2165 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -59,8 +59,6 @@ int tdbEnvCreate(TENV **ppEnv, const char *rootDir) { int tdbEnvOpen(TENV *pEnv) { SPgCache *pPgCache; - pgsz_t pgSize; - int npage; int ret; ASSERT(pEnv != NULL); @@ -70,8 +68,6 @@ int tdbEnvOpen(TENV *pEnv) { */ mkdir(pEnv->rootDir, 0755); - pgSize = pEnv->pgSize; - npage = pEnv->cacheSize / pEnv->pgSize; ret = pgCacheOpen(&pPgCache, pEnv); if (ret != 0) { goto _err; From 8050546d7d36cc0c2c3997fdb6294fe05d4a272b Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 08:44:02 +0000 Subject: [PATCH 56/79] more TDB --- source/libs/tdb/inc/tdb.h | 17 ++++---- source/libs/tdb/src/db/tdb.c | 72 +++++++++++++++++++++++++------- source/libs/tdb/src/inc/tdbInt.h | 2 +- source/libs/tdb/test/tdbTest.cpp | 22 +++++++--- 4 files changed, 85 insertions(+), 28 deletions(-) diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 443c02ff32..85af417e76 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -29,29 +29,32 @@ typedef struct STDbCurosr TDBC; typedef int32_t pgsz_t; typedef int32_t cachesz_t; +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); -int tdbEnvBeginTxn(TENV *pEnv); -int tdbEnvCommit(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); + // TDB int tdbCreate(TDB **ppDb); -int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv); +int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv); int tdbClose(TDB *pDb); int tdbSetKeyLen(TDB *pDb, int klen); int tdbSetValLen(TDB *pDb, int vlen); int tdbSetDup(TDB *pDb, int dup); -int tdbGetKeyLen(TDB *pDb, int *pklen); -int tdbGetValLen(TDB *pDb, int *pvlen); -int tdbGetDup(TDB *pDb, int *pdup); +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); diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index b983ce43dd..714ea23317 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -16,9 +16,14 @@ #include "tdbInt.h" struct STDb { - SBTree * pBt; // current access method - SPgFile *pPgFile; // backend page file this DB is using - TENV * pEnv; // TENV containing the DB + char * dbname; // dbname; + SBTree * pBt; // current access method (may extend) + SPgFile * pPgFile; // backend page file this DB is using + TENV * pEnv; // TENV containing the DB + int klen; // key length if know + int vlen; // value length if know + bool dup; // dup mode + TdbKeyCmprFn cFn; // compare function }; struct STDbCurosr { @@ -28,13 +33,18 @@ struct STDbCurosr { int tdbCreate(TDB **ppDb) { TDB *pDb; + // create the handle pDb = (TDB *)calloc(1, sizeof(*pDb)); if (pDb == NULL) { return -1; } - /* TODO */ + pDb->klen = TDB_VARIANT_LEN; + pDb->vlen = TDB_VARIANT_LEN; + pDb->dup = false; + pDb->cFn = NULL /*TODO*/; + *ppDb = pDb; return 0; } @@ -45,22 +55,14 @@ static int tdbDestroy(TDB *pDb) { return 0; } -int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { - TDB * pDb; +int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { int ret; uint8_t fileid[TDB_FILE_ID_LEN]; SPgFile * pPgFile; SPgCache *pPgCache; SBTree * pBt; - // Create DB if DB handle is not created yet - if (ppDb == NULL) { - if ((ret = tdbCreate(ppDb)) != 0) { - return -1; - } - } - - pDb = *ppDb; + ASSERT(pDb != NULL); // Create a default ENV if pEnv is not set if (pEnv == NULL) { @@ -107,4 +109,46 @@ int tdbOpen(TDB **ppDb, const char *fname, const char *dbname, TENV *pEnv) { int tdbClose(TDB *pDb) { if (pDb == NULL) return 0; return tdbDestroy(pDb); +} + +int tdbSetKeyLen(TDB *pDb, int klen) { + // TODO: check `klen` + pDb->klen = klen; + return 0; +} + +int tdbSetValLen(TDB *pDb, int vlen) { + // TODO: check `vlen` + pDb->vlen = vlen; + return 0; +} + +int tdbSetDup(TDB *pDb, int dup) { + if (dup) { + pDb->dup = true; + } else { + pDb->dup = false; + } + return 0; +} + +int tdbSetCmprFunc(TDB *pDb, TdbKeyCmprFn fn) { + if (fn == NULL) { + return -1; + } else { + pDb->cFn = fn; + } + return 0; +} + +int tdbGetKeyLen(TDB *pDb) { return pDb->klen; } + +int tdbGetValLen(TDB *pDb) { return pDb->vlen; } + +int tdbGetDup(TDB *pDb) { + if (pDb->dup) { + return 1; + } else { + 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 cafd1356cf..996d07b956 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -96,7 +96,7 @@ typedef TD_DLIST(SPgFile) SPgFileList; } \ } while (0) -#define TDB_VARIANT_LEN (int32_t) - 1 +#define TDB_VARIANT_LEN (int)-1 // page payload format // + + [key] + [value] diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index ccaddb4e14..065b83b2c2 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -19,12 +19,19 @@ TEST(tdb_test, simple_test) { GTEST_ASSERT_EQ(tdbEnvOpen(pEnv), 0); -#if 0 +#if 1 // DB - tdbOpen(&pDb1, "db.db", "db1", pEnv); - tdbOpen(&pDb2, "db.db", "db2", pEnv); - tdbOpen(&pDb3, "index.db", NULL, pEnv); + GTEST_ASSERT_EQ(tdbCreate(&pDb1), 0); + GTEST_ASSERT_EQ(tdbSetKeyLen(pDb1, 8), 0); + + // GTEST_ASSERT_EQ(tdbSetValLen(pDb1, 3), 0); + + // GTEST_ASSERT_EQ(tdbSetDup(pDb1, 3), 0); + + GTEST_ASSERT_EQ(tdbOpen(pDb1, "db.db", "db1", pEnv), 0); + +#if 0 // Insert // Query @@ -32,10 +39,13 @@ TEST(tdb_test, simple_test) { // Delete // Query +#endif - // Close + // 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); - tdbClose(pDb2); #endif tdbEnvClose(pEnv); From f44954b326a230de5e1febf7ddf2395b00b503cc Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 08:59:29 +0000 Subject: [PATCH 57/79] more TDB --- source/libs/tdb/src/db/tdb.c | 24 +++++++++++++++++++++++- source/libs/tdb/test/tdbTest.cpp | 10 +++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 714ea23317..3b7156fbd4 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -30,6 +30,8 @@ struct STDbCurosr { SBtCursor *pBtCur; }; +static int tdbDefaultKeyCmprFn(int keyLen1, const void *pKey1, int keyLen2, const void *pKey2); + int tdbCreate(TDB **ppDb) { TDB *pDb; @@ -42,7 +44,7 @@ int tdbCreate(TDB **ppDb) { pDb->klen = TDB_VARIANT_LEN; pDb->vlen = TDB_VARIANT_LEN; pDb->dup = false; - pDb->cFn = NULL /*TODO*/; + pDb->cFn = tdbDefaultKeyCmprFn; *ppDb = pDb; return 0; @@ -151,4 +153,24 @@ int tdbGetDup(TDB *pDb) { } else { 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; } \ No newline at end of file diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 065b83b2c2..db6a27b3e9 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -25,9 +25,17 @@ TEST(tdb_test, simple_test) { GTEST_ASSERT_EQ(tdbSetKeyLen(pDb1, 8), 0); + GTEST_ASSERT_EQ(tdbGetKeyLen(pDb1), 8); + // GTEST_ASSERT_EQ(tdbSetValLen(pDb1, 3), 0); - // GTEST_ASSERT_EQ(tdbSetDup(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); GTEST_ASSERT_EQ(tdbOpen(pDb1, "db.db", "db1", pEnv), 0); From f77d33dcb170d7eb956a48caf8e5a8a8b760dcf5 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 09:44:55 +0000 Subject: [PATCH 58/79] more TDB --- source/libs/tdb/src/db/tdb.c | 41 ++++++++++++++++++++++++----- source/libs/tdb/src/db/tdbEnv.c | 4 ++- source/libs/tdb/src/db/tdbPgFile.c | 5 ++-- source/libs/tdb/src/inc/tdbEnv.h | 5 ++-- source/libs/tdb/src/inc/tdbInt.h | 3 +++ source/libs/tdb/src/inc/tdbPgFile.h | 2 +- 6 files changed, 48 insertions(+), 12 deletions(-) diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 3b7156fbd4..df25fe20b8 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -16,7 +16,7 @@ #include "tdbInt.h" struct STDb { - char * dbname; // dbname; + char dbname[TDB_MAX_DBNAME_LEN]; SBTree * pBt; // current access method (may extend) SPgFile * pPgFile; // backend page file this DB is using TENV * pEnv; // TENV containing the DB @@ -63,16 +63,44 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { SPgFile * pPgFile; SPgCache *pPgCache; SBTree * pBt; + bool fileExist; + size_t dbNameLen; + char dbfname[128]; // TODO: make this as a macro or malloc on the heap ASSERT(pDb != NULL); + ASSERT(fname != NULL); + // TODO: Here we simply put an assert here. In the future, make `pEnv` + // can be set as NULL. + ASSERT(pEnv != NULL); - // Create a default ENV if pEnv is not set - if (pEnv == NULL) { - // if ((ret = tdbEnvOpen(&pEnv)) != 0) { - // return -1; - // } + // check the DB name + dbNameLen = 0; + if (dbname) { + dbNameLen = strlen(dbname); + if (dbNameLen >= TDB_MAX_DBNAME_LEN) { + return -1; + } + + memcpy(pDb->dbname, dbname, dbNameLen); } + pDb->dbname[dbNameLen] = '\0'; + + // open pPgFile or get from the env + snprintf(dbfname, 128, "%s/%s", tdbEnvGetRootDir(pEnv), fname); + fileExist = (tdbCheckFileAccess(fname, TDB_F_OK) == 0); + if (fileExist) { + // TODO + } else { + ret = pgFileOpen(&pPgFile, dbfname, pEnv); + if (ret != 0) { + // TODO: handle error + return -1; + } + // Create and open the page file + } + +#if 0 pDb->pEnv = pEnv; // register DB to ENV @@ -104,6 +132,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { } pDb->pBt = pBt; +#endif return 0; } diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 58612b2165..d88b816a34 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -124,4 +124,6 @@ int tdbEnvBeginTxn(TENV *pEnv) { int tdbEnvCommit(TENV *pEnv) { // TODO return 0; -} \ No newline at end of file +} + +const char *tdbEnvGetRootDir(TENV *pEnv) { return pEnv->rootDir; } diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index 576d6f3da4..07c036d623 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -17,8 +17,9 @@ static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData); -int pgFileOpen(SPgFile **ppPgFile, const char *fname, SPgCache *pPgCache) { - SPgFile *pPgFile; +int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { + SPgFile * pPgFile; + SPgCache *pPgCache; *ppPgFile = NULL; diff --git a/source/libs/tdb/src/inc/tdbEnv.h b/source/libs/tdb/src/inc/tdbEnv.h index a68ae0c7e9..ef2eab87ef 100644 --- a/source/libs/tdb/src/inc/tdbEnv.h +++ b/source/libs/tdb/src/inc/tdbEnv.h @@ -20,8 +20,9 @@ extern "C" { #endif -SPgFile* tdbEnvGetPageFile(TENV* pEnv, const uint8_t fileid[]); -SPgCache* tdbEnvGetPgCache(TENV* pEnv); +const char* tdbEnvGetRootDir(TENV* pEnv); +SPgFile* tdbEnvGetPageFile(TENV* pEnv, const uint8_t fileid[]); +SPgCache* tdbEnvGetPgCache(TENV* pEnv); #ifdef __cplusplus } diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 996d07b956..782206a4aa 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -76,6 +76,9 @@ typedef pgsz_t pgoff_t; // cache #define TDB_DEFAULT_CACHE_SIZE (256 * 4096) // 1M +// dbname +#define TDB_MAX_DBNAME_LEN 24 + // tdb_log #define tdbError(var) diff --git a/source/libs/tdb/src/inc/tdbPgFile.h b/source/libs/tdb/src/inc/tdbPgFile.h index 84d8319c61..afe3511fb0 100644 --- a/source/libs/tdb/src/inc/tdbPgFile.h +++ b/source/libs/tdb/src/inc/tdbPgFile.h @@ -42,7 +42,7 @@ struct SPgFile { TDB * pDb; // For a SPgFile for multiple databases, this is the mapping DB. }; -int pgFileOpen(SPgFile **ppPgFile, const char *fname, SPgCache *pPgCache); +int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv); int pgFileClose(SPgFile *pPgFile); SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno); From c9cd5fce3b2e17f2fa8c1d5f5b3bacd0ab5a0c39 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 10:11:07 +0000 Subject: [PATCH 59/79] more TDB --- source/libs/tdb/src/db/tdb.c | 9 ++++--- source/libs/tdb/src/db/tdbEnv.c | 1 + source/libs/tdb/src/db/tdbPgFile.c | 38 +++++++++++++++-------------- source/libs/tdb/src/inc/tdbPgFile.h | 12 ++++----- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index df25fe20b8..eab3db13b7 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -87,17 +87,20 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { pDb->dbname[dbNameLen] = '\0'; // open pPgFile or get from the env + pPgFile = NULL; snprintf(dbfname, 128, "%s/%s", tdbEnvGetRootDir(pEnv), fname); fileExist = (tdbCheckFileAccess(fname, TDB_F_OK) == 0); if (fileExist) { - // TODO - } else { + tdbGnrtFileID(dbfname, fileid, false); + pPgFile = tdbEnvGetPageFile(pEnv, fileid); + } + + if (pPgFile == NULL) { ret = pgFileOpen(&pPgFile, dbfname, pEnv); if (ret != 0) { // TODO: handle error return -1; } - // Create and open the page file } #if 0 diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index d88b816a34..f668d3e047 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -47,6 +47,7 @@ int tdbEnvCreate(TENV **ppEnv, const char *rootDir) { pEnv->cacheSize = TDB_DEFAULT_CACHE_SIZE; memcpy(pEnv->rootDir, rootDir, slen); + pEnv->rootDir[slen] = '\0'; TD_DLIST_INIT(&(pEnv->dbList)); TD_DLIST_INIT(&(pEnv->pgfList)); diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index 07c036d623..9e006aa2b4 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -20,38 +20,35 @@ static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData); int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { SPgFile * pPgFile; SPgCache *pPgCache; + size_t fnameLen; *ppPgFile = NULL; - pPgFile = (SPgFile *)calloc(1, sizeof(*pPgFile)); + // create the handle + fnameLen = strlen(fname); + pPgFile = (SPgFile *)calloc(1, sizeof(*pPgFile) + fnameLen + 1); if (pPgFile == NULL) { return -1; } + ASSERT(pEnv != NULL); + + // init the handle + pPgFile->pEnv = pEnv; + pPgFile->fname = (char *)(&(pPgFile[1])); + memcpy(pPgFile->fname, fname, fnameLen); + pPgFile->fname[fnameLen] = '\0'; pPgFile->fd = -1; - pPgFile->fname = strdup(fname); - if (pPgFile->fname == NULL) { - pgFileClose(pPgFile); - return -1; - } - - pPgFile->pPgCache = pPgCache; - // pPgFile->pgSize = ; (TODO) - - pPgFile->fd = open(fname, O_RDWR, 0755); + pPgFile->fd = open(fname, O_CREAT | O_RDWR, 0755); if (pPgFile->fd < 0) { - pgFileClose(pPgFile); + // TODO: handle error return -1; } - if (tdbGnrtFileID(fname, pPgFile->fileid, false) < 0) { - pgFileClose(pPgFile); - return -1; - } + tdbGnrtFileID(fname, pPgFile->fileid, false); - // TODO: get file size - pPgFile->pgFileSize = 0; + /* TODO */ *ppPgFile = pPgFile; return 0; @@ -75,6 +72,7 @@ SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) { SPage * pPage; pgid_t pgid; +#if 0 pPgCache = pPgFile->pPgCache; pPage = NULL; memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN); @@ -94,6 +92,7 @@ SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) { return pPage; } } +#endif return pPage; } @@ -114,6 +113,8 @@ static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) { uint8_t *pTData; size_t szToRead; +#if 0 + // pgSize = ; (TODO) pTData = pData; szToRead = pgSize; @@ -132,6 +133,7 @@ static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) { szToRead -= rsize; pTData += rsize; } +#endif return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbPgFile.h b/source/libs/tdb/src/inc/tdbPgFile.h index afe3511fb0..d0b51442d1 100644 --- a/source/libs/tdb/src/inc/tdbPgFile.h +++ b/source/libs/tdb/src/inc/tdbPgFile.h @@ -33,13 +33,11 @@ typedef struct __attribute__((__packed__)) { TDB_STATIC_ASSERT(sizeof(SPgFileHdr) == TDB_PG_FILE_HDR_SIZE, "Page file header size if not 128"); struct SPgFile { - char * fname; // backend file name - uint8_t fileid[TDB_FILE_ID_LEN]; // file id - SPgCache *pPgCache; // page cache underline - pgsz_t pgSize; - int fd; - pgno_t pgFileSize; - TDB * pDb; // For a SPgFile for multiple databases, this is the mapping DB. + TENV * pEnv; // env containing this page file + char * fname; // backend file name + uint8_t fileid[TDB_FILE_ID_LEN]; // file id + int fd; + // TDB * pDb; // For a SPgFile for multiple databases, this is the mapping DB. }; int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv); From 5c1cad5a3acb91eeee5e414ea8687eee0f9326ca Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 11:04:31 +0000 Subject: [PATCH 60/79] more TDB --- source/libs/tdb/src/db/tdbEnv.c | 11 +++++++++++ source/libs/tdb/src/db/tdbPgFile.c | 7 +++++-- source/libs/tdb/src/inc/tdbEnv.h | 1 + source/libs/tdb/src/inc/tdbInt.h | 1 + source/libs/tdb/src/inc/tdbPgFile.h | 9 +++++---- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index f668d3e047..daf74e07e3 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -23,6 +23,8 @@ struct STDbEnv { 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; SJournal *pJournal; }; @@ -128,3 +130,12 @@ int tdbEnvCommit(TENV *pEnv) { } const char *tdbEnvGetRootDir(TENV *pEnv) { return pEnv->rootDir; } + +int tdbEnvRgstPageFile(TENV *pEnv, SPgFile *pPgFile) { + SPgFileList *pBucket; + + pBucket = pEnv->pgfht.buckets + (0 % TDB_ENV_PGF_HASH_BUCKETS); // TODO + TD_DLIST_APPEND_WITH_FIELD(pBucket, pPgFile, envHash); + + return 0; +} \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index 9e006aa2b4..b993b68635 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -34,7 +34,6 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { ASSERT(pEnv != NULL); // init the handle - pPgFile->pEnv = pEnv; pPgFile->fname = (char *)(&(pPgFile[1])); memcpy(pPgFile->fname, fname, fnameLen); pPgFile->fname[fnameLen] = '\0'; @@ -48,7 +47,11 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { tdbGnrtFileID(fname, pPgFile->fileid, false); - /* TODO */ + /* TODO: other open operations */ + + // add the page file to the environment + tdbEnvRgstPageFile(pEnv, pPgFile); + pPgFile->pEnv = pEnv; *ppPgFile = pPgFile; return 0; diff --git a/source/libs/tdb/src/inc/tdbEnv.h b/source/libs/tdb/src/inc/tdbEnv.h index ef2eab87ef..4f9f32b8ea 100644 --- a/source/libs/tdb/src/inc/tdbEnv.h +++ b/source/libs/tdb/src/inc/tdbEnv.h @@ -23,6 +23,7 @@ extern "C" { const char* tdbEnvGetRootDir(TENV* pEnv); SPgFile* tdbEnvGetPageFile(TENV* pEnv, const uint8_t fileid[]); SPgCache* tdbEnvGetPgCache(TENV* pEnv); +int tdbEnvRgstPageFile(TENV* pEnv, SPgFile* pPgFile); #ifdef __cplusplus } diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 782206a4aa..06674496a9 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -84,6 +84,7 @@ typedef pgsz_t pgoff_t; typedef TD_DLIST(STDb) STDbList; typedef TD_DLIST(SPgFile) SPgFileList; +typedef TD_DLIST_NODE(SPgFile) SPgFileListNode; #define TERR_A(val, op, flag) \ do { \ diff --git a/source/libs/tdb/src/inc/tdbPgFile.h b/source/libs/tdb/src/inc/tdbPgFile.h index d0b51442d1..90136ef886 100644 --- a/source/libs/tdb/src/inc/tdbPgFile.h +++ b/source/libs/tdb/src/inc/tdbPgFile.h @@ -33,10 +33,11 @@ typedef struct __attribute__((__packed__)) { TDB_STATIC_ASSERT(sizeof(SPgFileHdr) == TDB_PG_FILE_HDR_SIZE, "Page file header size if not 128"); struct SPgFile { - TENV * pEnv; // env containing this page file - char * fname; // backend file name - uint8_t fileid[TDB_FILE_ID_LEN]; // file id - int fd; + TENV * pEnv; // env containing this page file + char * fname; // backend file name + uint8_t fileid[TDB_FILE_ID_LEN]; // file id + int fd; + SPgFileListNode envHash; // TDB * pDb; // For a SPgFile for multiple databases, this is the mapping DB. }; From baf5ceed55c68d61c3afef7ae115fcbb6f85c8db Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 11:12:29 +0000 Subject: [PATCH 61/79] more TDB --- source/libs/tdb/src/db/tdbEnv.c | 10 +++++++++- source/libs/tdb/src/inc/tdbPgFile.h | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index daf74e07e3..9954770046 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -131,10 +131,18 @@ int tdbEnvCommit(TENV *pEnv) { const char *tdbEnvGetRootDir(TENV *pEnv) { return pEnv->rootDir; } +#define TDB_ENV_PGF_HASH(fileid) \ + ({ \ + uint8_t *tmp = (uint8_t *)(fileid); \ + tmp[0] + tmp[1] + tmp[2]; \ + }) + int tdbEnvRgstPageFile(TENV *pEnv, SPgFile *pPgFile) { SPgFileList *pBucket; - pBucket = pEnv->pgfht.buckets + (0 % TDB_ENV_PGF_HASH_BUCKETS); // TODO + 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; diff --git a/source/libs/tdb/src/inc/tdbPgFile.h b/source/libs/tdb/src/inc/tdbPgFile.h index 90136ef886..99fb811c9e 100644 --- a/source/libs/tdb/src/inc/tdbPgFile.h +++ b/source/libs/tdb/src/inc/tdbPgFile.h @@ -38,6 +38,7 @@ struct SPgFile { uint8_t fileid[TDB_FILE_ID_LEN]; // file id int fd; SPgFileListNode envHash; + SPgFileListNode envPgfList; // TDB * pDb; // For a SPgFile for multiple databases, this is the mapping DB. }; From fb0b534074257e15e8e3c79d5654c202cde0dcb0 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 11:36:02 +0000 Subject: [PATCH 62/79] more TDB --- source/libs/tdb/src/db/tdb.c | 36 ++++---------------------------- source/libs/tdb/src/db/tdbEnv.c | 28 ++++++++++++++++++------- source/libs/tdb/src/inc/tdbEnv.h | 1 + 3 files changed, 25 insertions(+), 40 deletions(-) diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index eab3db13b7..9fe15ce156 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -86,7 +86,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { pDb->dbname[dbNameLen] = '\0'; - // open pPgFile or get from the env + // get page file from the env, if not opened yet, open it pPgFile = NULL; snprintf(dbfname, 128, "%s/%s", tdbEnvGetRootDir(pEnv), fname); fileExist = (tdbCheckFileAccess(fname, TDB_F_OK) == 0); @@ -103,39 +103,11 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { } } -#if 0 - pDb->pEnv = pEnv; - - // register DB to ENV - - ASSERT(fname != NULL); - - // Check if file exists - if (tdbCheckFileAccess(fname, TDB_F_OK) != 0) { - if (1) { - // create the file - } - } - - // Check if the SPgFile already opened - tdbGnrtFileID(fname, fileid, false); - pPgFile = tdbEnvGetPageFile(pEnv, fileid); - if (pPgFile == NULL) { - pPgCache = tdbEnvGetPgCache(pEnv); - if ((ret = pgFileOpen(&pPgFile, fname, pPgCache)) != 0) { - return -1; - } - } + // TODO: open the database (an existing or a new one) pDb->pPgFile = pPgFile; - - // open the access method (TODO) - if (btreeOpen(&pBt, pPgFile) != 0) { - return -1; - } - - pDb->pBt = pBt; -#endif + tdbEnvRgstDB(pEnv, pDb); + pDb->pEnv = pEnv; return 0; } diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 9954770046..237220d155 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -29,6 +29,12 @@ struct STDbEnv { SJournal *pJournal; }; +#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) { @@ -108,8 +114,15 @@ pgsz_t tdbEnvGetPageSize(TENV *pEnv) { return pEnv->pgSize; } cachesz_t tdbEnvGetCacheSize(TENV *pEnv) { return pEnv->cacheSize; } SPgFile *tdbEnvGetPageFile(TENV *pEnv, const uint8_t fileid[]) { - // TODO - return NULL; + 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; } @@ -131,12 +144,6 @@ int tdbEnvCommit(TENV *pEnv) { const char *tdbEnvGetRootDir(TENV *pEnv) { return pEnv->rootDir; } -#define TDB_ENV_PGF_HASH(fileid) \ - ({ \ - uint8_t *tmp = (uint8_t *)(fileid); \ - tmp[0] + tmp[1] + tmp[2]; \ - }) - int tdbEnvRgstPageFile(TENV *pEnv, SPgFile *pPgFile) { SPgFileList *pBucket; @@ -145,5 +152,10 @@ int tdbEnvRgstPageFile(TENV *pEnv, SPgFile *pPgFile) { 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; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbEnv.h b/source/libs/tdb/src/inc/tdbEnv.h index 4f9f32b8ea..6cb5c7a2cd 100644 --- a/source/libs/tdb/src/inc/tdbEnv.h +++ b/source/libs/tdb/src/inc/tdbEnv.h @@ -24,6 +24,7 @@ const char* tdbEnvGetRootDir(TENV* pEnv); SPgFile* tdbEnvGetPageFile(TENV* pEnv, const uint8_t fileid[]); SPgCache* tdbEnvGetPgCache(TENV* pEnv); int tdbEnvRgstPageFile(TENV* pEnv, SPgFile* pPgFile); +int tdbEnvRgstDB(TENV* pEnv, TDB* pDb); #ifdef __cplusplus } From 727db57799cd516db9c3e7b3011fed937c211dba Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 15 Feb 2022 11:50:15 +0000 Subject: [PATCH 63/79] more TDB --- source/libs/tdb/src/db/tdb.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 9fe15ce156..0059fba191 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -104,6 +104,9 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { } // TODO: open the database (an existing or a new one) + // Search the page file master DB to check if the db exists + // If DB exists, get the root page number + // If DB not exists, create a new DB pDb->pPgFile = pPgFile; tdbEnvRgstDB(pEnv, pDb); From f1e2ca50749bce43dc466c014acbcb0e57a1a2d4 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 16 Feb 2022 03:29:35 +0000 Subject: [PATCH 64/79] more TDB --- source/libs/tdb/src/db/tdbEnv.c | 22 ++++++++++++++++++++-- source/libs/tdb/src/db/tdbJournal.c | 19 +++++++++++++++---- source/libs/tdb/src/inc/tdbJournal.h | 7 +++++++ source/libs/tdb/src/inc/tdbPgFile.h | 3 ++- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 237220d155..8a4cab1e29 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -133,12 +133,30 @@ static int tdbEnvDestroy(TENV *pEnv) { } int tdbEnvBeginTxn(TENV *pEnv) { - // TODO + SJournal *pJournal; + int ret; + + ASSERT(pEnv->pJournal == NULL); + + pJournal = (SJournal *)(&(pEnv[1])); + ret = tdbOpenJournal(pJournal); + if (ret < 0) { + // TODO: handle error + return -1; + } + + pEnv->pJournal = pJournal; return 0; } int tdbEnvCommit(TENV *pEnv) { - // TODO + SJournal *pJournal; + + ASSERT(pEnv->pJournal != NULL); + + pJournal = pEnv->pJournal; + tdbCloseJournal(pJournal); + /* TODO */ return 0; } diff --git a/source/libs/tdb/src/db/tdbJournal.c b/source/libs/tdb/src/db/tdbJournal.c index ace622fd72..80e29dd09e 100644 --- a/source/libs/tdb/src/db/tdbJournal.c +++ b/source/libs/tdb/src/db/tdbJournal.c @@ -13,7 +13,18 @@ * along with this program. If not, see . */ -struct SJournal { - char *jname; - int fd; -}; \ No newline at end of file +#include "tdbInt.h" + +int tdbOpenJournal(SJournal *pJournal) { + // pJournal->fd = open(); + 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/tdbJournal.h b/source/libs/tdb/src/inc/tdbJournal.h index 685e2bcb16..918b2fa06d 100644 --- a/source/libs/tdb/src/inc/tdbJournal.h +++ b/source/libs/tdb/src/inc/tdbJournal.h @@ -21,6 +21,13 @@ extern "C" { #endif typedef struct SJournal SJournal; +struct SJournal { + char jname[64]; + int fd; +}; + +int tdbOpenJournal(SJournal *pJournal); +int tdbCloseJournal(SJournal *pJournal); #ifdef __cplusplus } diff --git a/source/libs/tdb/src/inc/tdbPgFile.h b/source/libs/tdb/src/inc/tdbPgFile.h index 99fb811c9e..97f3fe289d 100644 --- a/source/libs/tdb/src/inc/tdbPgFile.h +++ b/source/libs/tdb/src/inc/tdbPgFile.h @@ -37,9 +37,10 @@ struct SPgFile { char * fname; // backend file name uint8_t fileid[TDB_FILE_ID_LEN]; // file id int fd; + pgno_t dbSize; + pgno_t dbNewSize; SPgFileListNode envHash; SPgFileListNode envPgfList; - // TDB * pDb; // For a SPgFile for multiple databases, this is the mapping DB. }; int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv); From 6e995780b5b982e6e2220250b523ff30a9986d8c Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 16 Feb 2022 05:35:52 +0000 Subject: [PATCH 65/79] more TDB --- source/libs/tdb/src/db/tdb.c | 16 +++++++++++++--- source/libs/tdb/src/db/tdbPgFile.c | 13 +++++++++++++ source/libs/tdb/src/inc/tdbPgFile.h | 2 ++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 0059fba191..e79313d1ca 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -65,6 +65,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { SBTree * pBt; bool fileExist; size_t dbNameLen; + pgno_t dbRootPgno; char dbfname[128]; // TODO: make this as a macro or malloc on the heap ASSERT(pDb != NULL); @@ -104,10 +105,19 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { } // TODO: open the database (an existing or a new one) - // Search the page file master DB to check if the db exists - // If DB exists, get the root page number - // If DB not exists, create a new DB + if (0) { + // Search the page file master DB to check if the db exists + // If exists, run this branch (TODO) + } else { + ret = pgFileAllocatePage(pPgFile, &dbRootPgno); + if (ret != 0) { + // TODO: handle error + } + } + pDb->pBt->root = dbRootPgno; + + // register pDb->pPgFile = pPgFile; tdbEnvRgstDB(pEnv, pDb); pDb->pEnv = pEnv; diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index b993b68635..c6b7611818 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -110,6 +110,19 @@ int pgFileWrite(SPage *pPage) { return 0; } +int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) { + pgno_t pgno; + + if (0) { + // TODO: allocate from the free list + } else { + pgno = ++pPgFile->dbNewSize; + } + + *pPgno = pgno; + return 0; +} + static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) { pgsz_t pgSize; ssize_t rsize; diff --git a/source/libs/tdb/src/inc/tdbPgFile.h b/source/libs/tdb/src/inc/tdbPgFile.h index 97f3fe289d..d4c97e0e0d 100644 --- a/source/libs/tdb/src/inc/tdbPgFile.h +++ b/source/libs/tdb/src/inc/tdbPgFile.h @@ -46,10 +46,12 @@ struct SPgFile { int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv); int pgFileClose(SPgFile *pPgFile); + SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno); int pgFileRelease(SPage *pPage); int pgFileWrite(SPage *pPage); +int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno); #ifdef __cplusplus } From 4b5f00ca3bbebedb82c079b97ae6d235273f4694 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 16 Feb 2022 06:30:17 +0000 Subject: [PATCH 66/79] more TDB --- source/libs/tdb/src/db/tdbPgFile.c | 11 ++++++++++- source/libs/tdb/src/inc/tdbPgFile.h | 5 ++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index c6b7611818..d6223a5788 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -15,6 +15,15 @@ #include "tdbInt.h" +typedef struct SPage1 { + char magic[64]; + pgno_t mdbRootPgno; // master DB root page number + pgno_t freePgno; // free list page number + uint32_t nFree; // number of free pages +} SPage1; + +TDB_STATIC_ASSERT(sizeof(SPage1) <= TDB_MIN_PGSIZE, "TDB Page1 definition too large"); + static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData); int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { @@ -116,7 +125,7 @@ int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) { if (0) { // TODO: allocate from the free list } else { - pgno = ++pPgFile->dbNewSize; + pgno = ++(pPgFile->lsize); } *pPgno = pgno; diff --git a/source/libs/tdb/src/inc/tdbPgFile.h b/source/libs/tdb/src/inc/tdbPgFile.h index d4c97e0e0d..2a7116a0dd 100644 --- a/source/libs/tdb/src/inc/tdbPgFile.h +++ b/source/libs/tdb/src/inc/tdbPgFile.h @@ -36,9 +36,9 @@ struct SPgFile { TENV * pEnv; // env containing this page file char * fname; // backend file name uint8_t fileid[TDB_FILE_ID_LEN]; // file id + pgno_t lsize; // page file logical size (for count) + pgno_t fsize; // real file size on disk (for rollback) int fd; - pgno_t dbSize; - pgno_t dbNewSize; SPgFileListNode envHash; SPgFileListNode envPgfList; }; @@ -46,7 +46,6 @@ struct SPgFile { int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv); int pgFileClose(SPgFile *pPgFile); - SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno); int pgFileRelease(SPage *pPage); From 8f6781a7bb0a884a7abab33ab3c907ff3bbcc680 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 16 Feb 2022 07:32:34 +0000 Subject: [PATCH 67/79] more TDB --- source/libs/tdb/src/db/tdb.c | 2 +- source/libs/tdb/src/db/tdbPgFile.c | 33 +++++++++++++++++++++++++++--- source/libs/tdb/src/db/tdbUtil.c | 14 +++++++++++-- source/libs/tdb/src/inc/tdbUtil.h | 2 +- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index e79313d1ca..6fcb2686fd 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -115,7 +115,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { } } - pDb->pBt->root = dbRootPgno; + // pDb->pBt->root = dbRootPgno; // register pDb->pPgFile = pPgFile; diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index d6223a5788..030f57de4c 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -30,6 +30,7 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { SPgFile * pPgFile; SPgCache *pPgCache; size_t fnameLen; + pgno_t fsize; *ppPgFile = NULL; @@ -55,6 +56,28 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { } tdbGnrtFileID(fname, pPgFile->fileid, false); + tdbGetFileSize(fname, tdbEnvGetPageSize(pEnv), &fsize); + + pPgFile->fsize = fsize; + pPgFile->lsize = fsize; + + if (pPgFile->fsize == 0) { + // A created file + pgno_t pgno; + pgid_t pgid; + + pgFileAllocatePage(pPgFile, &pgno); + + ASSERT(pgno == 1); + + memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN); + pgid.pgno = pgno; + + pgCacheFetch(pPgCache, pgid); + // Need to allocate the first page as a description page + } else { + // An existing file + } /* TODO: other open operations */ @@ -122,10 +145,14 @@ int pgFileWrite(SPage *pPage) { int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) { pgno_t pgno; - if (0) { - // TODO: allocate from the free list - } else { + if (pPgFile->lsize == 0) { pgno = ++(pPgFile->lsize); + } else { + if (0) { + // TODO: allocate from the free list + } else { + pgno = ++(pPgFile->lsize); + } } *pPgno = pgno; diff --git a/source/libs/tdb/src/db/tdbUtil.c b/source/libs/tdb/src/db/tdbUtil.c index 591e7eedd4..fa9a3297da 100644 --- a/source/libs/tdb/src/db/tdbUtil.c +++ b/source/libs/tdb/src/db/tdbUtil.c @@ -51,7 +51,17 @@ int tdbCheckFileAccess(const char *pathname, int mode) { return access(pathname, flags); } -int64_t tdbGetFileSize(const char *fname) { - // TODO +int tdbGetFileSize(const char *fname, pgsz_t pgSize, pgno_t *pSize) { + struct stat st; + int ret; + + ret = stat(fname, &st); + if (ret != 0) { + return -1; + } + + ASSERT(st.st_size % pgSize == 0); + + *pSize = st.st_size / pgSize; return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index f3e00a5ba5..8108e5aba6 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -35,7 +35,7 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique); #define TDB_W_OK 0x4 int tdbCheckFileAccess(const char *pathname, int mode); -int64_t tdbGetFileSize(const char *fname); +int tdbGetFileSize(const char *fname, pgsz_t pgSize, pgno_t *pSize); #ifdef __cplusplus } From cada7c3912ea9688dbcddc41bbdde878be0e03bc Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 16 Feb 2022 07:54:11 +0000 Subject: [PATCH 68/79] more TDB --- source/libs/tdb/src/db/tdbEnv.c | 4 ++-- source/libs/tdb/src/db/tdbJournal.c | 6 +++++- source/libs/tdb/test/tdbTest.cpp | 4 ++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 8a4cab1e29..241c21cb9b 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -45,12 +45,12 @@ int tdbEnvCreate(TENV **ppEnv, const char *rootDir) { *ppEnv = NULL; slen = strlen(rootDir); - pEnv = (TENV *)calloc(1, sizeof(*pEnv) + slen + 1); + pEnv = (TENV *)calloc(1, sizeof(*pEnv) + sizeof(SJournal) + slen + 1); if (pEnv == NULL) { return -1; } - pEnv->rootDir = (char *)(&pEnv[1]); + pEnv->rootDir = (char *)(&pEnv[1]) + sizeof(SJournal); pEnv->pgSize = TDB_DEFAULT_PGSIZE; pEnv->cacheSize = TDB_DEFAULT_CACHE_SIZE; diff --git a/source/libs/tdb/src/db/tdbJournal.c b/source/libs/tdb/src/db/tdbJournal.c index 80e29dd09e..80b28a3855 100644 --- a/source/libs/tdb/src/db/tdbJournal.c +++ b/source/libs/tdb/src/db/tdbJournal.c @@ -16,11 +16,15 @@ #include "tdbInt.h" int tdbOpenJournal(SJournal *pJournal) { - // pJournal->fd = open(); + TENV *pEnv; + char *jname; + + pJournal->fd = open(jname, O_CREAT | O_RDWR, 0755); if (pJournal->fd < 0) { // TODO: handle error return -1; } + return 0; } diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index db6a27b3e9..4433d3288d 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -20,6 +20,8 @@ TEST(tdb_test, simple_test) { GTEST_ASSERT_EQ(tdbEnvOpen(pEnv), 0); #if 1 + + tdbEnvBeginTxn(pEnv); // DB GTEST_ASSERT_EQ(tdbCreate(&pDb1), 0); @@ -39,6 +41,8 @@ TEST(tdb_test, simple_test) { GTEST_ASSERT_EQ(tdbOpen(pDb1, "db.db", "db1", pEnv), 0); + tdbEnvCommit(pEnv); + #if 0 // Insert From c1e556e7e1d24ef3b05a6c08b0ea283c7c49e60d Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 16 Feb 2022 08:48:48 +0000 Subject: [PATCH 69/79] more TDB --- source/libs/tdb/src/db/tdbPgFile.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index 030f57de4c..5db0c4dcc3 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -107,6 +107,21 @@ SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) { SPage * pPage; pgid_t pgid; + // 1. Fetch from the page cache + // pgCacheFetch(pPgCache, pgid); + + // 2. If only get a page frame, no content, maybe + // need to load from the file + if (1 /*page not initialized*/) { + if (pgno < pPgFile->fsize) { + // load the page content from the disk + // ?? How about the freed pages ?? + } else { + // zero the page, make the page as a empty + // page with zero records. + } + } + #if 0 pPgCache = pPgFile->pPgCache; pPage = NULL; From c561879625c252d06b09c5c5b0d0409d211ada84 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 16 Feb 2022 09:33:40 +0000 Subject: [PATCH 70/79] more TDB --- source/libs/tdb/src/db/tdb.c | 5 +++++ source/libs/tdb/src/db/tdbPgFile.c | 4 ++++ source/libs/tdb/test/tdbTest.cpp | 14 +++++++++----- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 6fcb2686fd..81bc35e87c 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -172,6 +172,11 @@ int tdbGetDup(TDB *pDb) { } } +int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData) { + // TODO + return 0; +} + static int tdbDefaultKeyCmprFn(int keyLen1, const void *pKey1, int keyLen2, const void *pKey2) { int mlen; int cret; diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index 5db0c4dcc3..55a89aae63 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -22,6 +22,10 @@ typedef struct SPage1 { uint32_t nFree; // number of free pages } SPage1; +typedef struct SFreePage { + /* TODO */ +} SFreePage; + TDB_STATIC_ASSERT(sizeof(SPage1) <= TDB_MIN_PGSIZE, "TDB Page1 definition too large"); static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData); diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 4433d3288d..fc4cc31cda 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -3,7 +3,7 @@ #include "tdb.h" TEST(tdb_test, simple_test) { - TENV* pEnv; + TENV * pEnv; TDB * pDb1, *pDb2, *pDb3; pgsz_t pgSize = 1024; cachesz_t cacheSize = 10240; @@ -20,14 +20,12 @@ TEST(tdb_test, simple_test) { GTEST_ASSERT_EQ(tdbEnvOpen(pEnv), 0); #if 1 - - tdbEnvBeginTxn(pEnv); // DB GTEST_ASSERT_EQ(tdbCreate(&pDb1), 0); - GTEST_ASSERT_EQ(tdbSetKeyLen(pDb1, 8), 0); + // GTEST_ASSERT_EQ(tdbSetKeyLen(pDb1, 8), 0); - GTEST_ASSERT_EQ(tdbGetKeyLen(pDb1), 8); + // GTEST_ASSERT_EQ(tdbGetKeyLen(pDb1), 8); // GTEST_ASSERT_EQ(tdbSetValLen(pDb1, 3), 0); @@ -39,8 +37,14 @@ TEST(tdb_test, simple_test) { // 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 From 10108f34fcf7cde0cb71c63f7ee9125c6eb66808 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 16 Feb 2022 09:41:09 +0000 Subject: [PATCH 71/79] more TDB --- source/libs/tdb/src/db/tdbPgCache.c | 10 ---------- source/libs/tdb/src/inc/tdbPgCache.h | 9 +++++++++ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPgCache.c b/source/libs/tdb/src/db/tdbPgCache.c index e6fef996cf..1b000aa6d6 100644 --- a/source/libs/tdb/src/db/tdbPgCache.c +++ b/source/libs/tdb/src/db/tdbPgCache.c @@ -14,16 +14,6 @@ */ #include "tdbInt.h" -typedef TD_DLIST_NODE(SPage) SPgListNode; -struct SPage { - pgid_t pgid; // page id - frame_id_t frameid; // frame id - uint8_t * pData; // real data - SPgListNode freeNode; // for SPgCache.freeList - SPgListNode pghtNode; // for pght - SPgListNode lruNode; // for LRU -}; - typedef TD_DLIST(SPage) SPgList; struct SPgCache { TENV * pEnv; // TENV containing this page cache diff --git a/source/libs/tdb/src/inc/tdbPgCache.h b/source/libs/tdb/src/inc/tdbPgCache.h index 5bcac688af..c25ef27c10 100644 --- a/source/libs/tdb/src/inc/tdbPgCache.h +++ b/source/libs/tdb/src/inc/tdbPgCache.h @@ -31,6 +31,15 @@ SPage *pgCacheFetch(SPgCache *pPgCache, pgid_t pgid); int pgCacheRelease(SPage *pPage); // SPage +typedef TD_DLIST_NODE(SPage) SPgListNode; +struct SPage { + pgid_t pgid; // page id + frame_id_t frameid; // frame id + uint8_t * pData; // real data + SPgListNode freeNode; // for SPgCache.freeList + SPgListNode pghtNode; // for pght + SPgListNode lruNode; // for LRU +}; #ifdef __cplusplus } From 87dfa6cee11279603b7b168c80adba21072e46ef Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 16 Feb 2022 09:52:41 +0000 Subject: [PATCH 72/79] more TDB --- source/libs/tdb/src/db/tdbPgFile.c | 13 ++++++++++++- source/libs/tdb/test/tdbTest.cpp | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPgFile.c b/source/libs/tdb/src/db/tdbPgFile.c index 55a89aae63..ee5b486f7b 100644 --- a/source/libs/tdb/src/db/tdbPgFile.c +++ b/source/libs/tdb/src/db/tdbPgFile.c @@ -162,13 +162,24 @@ int pgFileWrite(SPage *pPage) { } int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) { - pgno_t pgno; + pgno_t pgno; + SPage1 * pPage1; + SPgCache *pPgCache; + pgid_t pgid; + SPage * pPage; if (pPgFile->lsize == 0) { pgno = ++(pPgFile->lsize); } else { if (0) { // TODO: allocate from the free list + pPage = pgCacheFetch(pPgCache, pgid); + + if (pPage1->nFree > 0) { + // TODO + } else { + pgno = ++(pPgFile->lsize); + } } else { pgno = ++(pPgFile->lsize); } diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index fc4cc31cda..5ab0b4c0f1 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -9,7 +9,7 @@ TEST(tdb_test, simple_test) { cachesz_t cacheSize = 10240; // ENV - GTEST_ASSERT_EQ(tdbEnvCreate(&pEnv, "./tdbtest"), 0); + GTEST_ASSERT_EQ(tdbEnvCreate(&pEnv, "./testtdb"), 0); GTEST_ASSERT_EQ(tdbEnvSetCache(pEnv, pgSize, cacheSize), 0); From 9e34770df9af81cbe2f52081ad469b4a5e9144aa Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 16 Feb 2022 10:16:17 +0000 Subject: [PATCH 73/79] more TDB --- source/libs/tdb/src/db/tdb.c | 12 ++++++---- source/libs/tdb/src/db/tdbEnv.c | 32 ++++++++++--------------- source/libs/tdb/src/db/tdbJournal.c | 34 -------------------------- source/libs/tdb/src/inc/tdbInt.h | 4 ++-- source/libs/tdb/src/inc/tdbJournal.h | 36 ---------------------------- 5 files changed, 22 insertions(+), 96 deletions(-) delete mode 100644 source/libs/tdb/src/db/tdbJournal.c delete mode 100644 source/libs/tdb/src/inc/tdbJournal.h diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index 81bc35e87c..d1b5863c28 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 241c21cb9b..3670c770ab 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 80b28a3855..0000000000 --- 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 06674496a9..ac42e15002 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 918b2fa06d..0000000000 --- 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 From c18883bc03d120fb02b09d64768d9608cc1cc148 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 18 Feb 2022 14:23:12 +0800 Subject: [PATCH 74/79] trans type string --- include/util/tdef.h | 2 +- source/dnode/mnode/impl/src/mndTrans.c | 55 +++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/include/util/tdef.h b/include/util/tdef.h index 9b18c2dfb8..1dd4d4b5d8 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -215,7 +215,7 @@ do { \ #define TSDB_SLOW_QUERY_SQL_LEN 512 #define TSDB_TRANS_STAGE_LEN 12 -#define TSDB_TRANS_DESC_LEN 16 +#define TSDB_TRANS_TYPE_LEN 16 #define TSDB_TRANS_ERROR_LEN 128 #define TSDB_STEP_NAME_LEN 32 diff --git a/source/dnode/mnode/impl/src/mndTrans.c b/source/dnode/mnode/impl/src/mndTrans.c index f7226d9df7..5190d66920 100644 --- a/source/dnode/mnode/impl/src/mndTrans.c +++ b/source/dnode/mnode/impl/src/mndTrans.c @@ -221,7 +221,6 @@ static SSdbRow *mndTransActionDecode(SSdbRaw *pRaw) { pTrans = sdbGetRowObj(pRow); if (pTrans == NULL) goto TRANS_DECODE_OVER; - SDB_GET_INT32(pRaw, dataPos, &pTrans->id, TRANS_DECODE_OVER) int16_t type = 0; @@ -353,8 +352,60 @@ static const char *mndTransStr(ETrnStage stage) { static const char *mndTransType(ETrnType type) { switch (type) { + case TRN_TYPE_CREATE_USER: + return "create-user"; + case TRN_TYPE_ALTER_USER: + return "alter-user"; + case TRN_TYPE_DROP_USER: + return "drop-user"; + case TRN_TYPE_CREATE_FUNC: + return "create-func"; + case TRN_TYPE_DROP_FUNC: + return "drop-func"; + case TRN_TYPE_CREATE_SNODE: + return "create-snode"; + case TRN_TYPE_DROP_SNODE: + return "drop-snode"; + case TRN_TYPE_CREATE_QNODE: + return "create-qnode"; + case TRN_TYPE_DROP_QNODE: + return "drop-qnode"; + case TRN_TYPE_CREATE_BNODE: + return "create-bnode"; + case TRN_TYPE_DROP_BNODE: + return "drop-bnode"; + case TRN_TYPE_CREATE_MNODE: + return "create-mnode"; + case TRN_TYPE_DROP_MNODE: + return "drop-mnode"; + case TRN_TYPE_CREATE_TOPIC: + return "create-topic"; + case TRN_TYPE_DROP_TOPIC: + return "drop-topic"; + case TRN_TYPE_SUBSCRIBE: + return "subscribe"; + case TRN_TYPE_REBALANCE: + return "rebalance"; + case TRN_TYPE_CREATE_DNODE: + return "create-qnode"; + case TRN_TYPE_DROP_DNODE: + return "drop-qnode"; case TRN_TYPE_CREATE_DB: return "create-db"; + case TRN_TYPE_ALTER_DB: + return "alter-db"; + case TRN_TYPE_DROP_DB: + return "drop-db"; + case TRN_TYPE_SPLIT_VGROUP: + return "split-vgroup"; + case TRN_TYPE_MERGE_VGROUP: + return "merge-vgroup"; + case TRN_TYPE_CREATE_STB: + return "create-stb"; + case TRN_TYPE_ALTER_STB: + return "alter-stb"; + case TRN_TYPE_DROP_STB: + return "drop-stb"; default: return "invalid"; } @@ -1228,7 +1279,7 @@ static int32_t mndGetTransMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp * pSchema[cols].bytes = pShow->bytes[cols]; cols++; - pShow->bytes[cols] = (TSDB_TRANS_DESC_LEN - 1) + VARSTR_HEADER_SIZE; + pShow->bytes[cols] = TSDB_TRANS_TYPE_LEN + VARSTR_HEADER_SIZE; pSchema[cols].type = TSDB_DATA_TYPE_BINARY; strcpy(pSchema[cols].name, "type"); pSchema[cols].bytes = pShow->bytes[cols]; From 8977e08fb14a9c27a141864c2b1b9297b67b5cbc Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 18 Feb 2022 14:26:49 +0800 Subject: [PATCH 75/79] trans error --- include/util/tdef.h | 2 +- source/dnode/mnode/impl/src/mndTrans.c | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/util/tdef.h b/include/util/tdef.h index 1dd4d4b5d8..35655c8eaf 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -216,7 +216,7 @@ do { \ #define TSDB_TRANS_STAGE_LEN 12 #define TSDB_TRANS_TYPE_LEN 16 -#define TSDB_TRANS_ERROR_LEN 128 +#define TSDB_TRANS_ERROR_LEN 64 #define TSDB_STEP_NAME_LEN 32 #define TSDB_STEP_DESC_LEN 128 diff --git a/source/dnode/mnode/impl/src/mndTrans.c b/source/dnode/mnode/impl/src/mndTrans.c index 5190d66920..433aade549 100644 --- a/source/dnode/mnode/impl/src/mndTrans.c +++ b/source/dnode/mnode/impl/src/mndTrans.c @@ -805,6 +805,9 @@ void mndTransProcessRsp(SMnodeMsg *pRsp) { if (pAction != NULL) { pAction->msgReceived = 1; pAction->errCode = pRsp->rpcMsg.code; + if (pAction->errCode != 0) { + tstrncpy(pTrans->lastError, tstrerror(pAction->errCode), TSDB_TRANS_ERROR_LEN); + } } mDebug("trans:%d, action:%d response is received, code:0x%x, accept:0x%x", transId, action, pRsp->rpcMsg.code, @@ -1110,6 +1113,7 @@ static void mndTransExecute(SMnode *pMnode, STrans *pTrans) { bool continueExec = true; while (continueExec) { + pTrans->lastExecTime = taosGetTimestampMs(); switch (pTrans->stage) { case TRN_STAGE_PREPARE: continueExec = mndTransPerformPrepareStage(pMnode, pTrans); From 44eb20628ef3132f95f4effded53e9e16ed8a2da Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 18 Feb 2022 15:08:26 +0800 Subject: [PATCH 76/79] trans test --- source/dnode/mnode/impl/src/mndTrans.c | 7 +-- source/dnode/mnode/impl/test/trans/trans.cpp | 49 +++++++++++++++++++- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/source/dnode/mnode/impl/src/mndTrans.c b/source/dnode/mnode/impl/src/mndTrans.c index 433aade549..5b1abdeb74 100644 --- a/source/dnode/mnode/impl/src/mndTrans.c +++ b/source/dnode/mnode/impl/src/mndTrans.c @@ -474,6 +474,7 @@ STrans *mndTransCreate(SMnode *pMnode, ETrnPolicy policy, ETrnType type, const S pTrans->stage = TRN_STAGE_PREPARE; pTrans->policy = policy; pTrans->transType = type; + pTrans->createdTime = taosGetTimestampMs(); pTrans->rpcHandle = pReq->handle; pTrans->rpcAHandle = pReq->ahandle; pTrans->redoLogs = taosArrayInit(MND_TRANS_ARRAY_SIZE, sizeof(void *)); @@ -1343,11 +1344,7 @@ static int32_t mndRetrieveTrans(SMnodeMsg *pReq, SShowObj *pShow, char *data, in pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; char *name = mnGetDbStr(pTrans->dbname); - if (name != NULL) { - STR_WITH_MAXSIZE_TO_VARSTR(pWrite, name, pShow->bytes[cols]); - } else { - STR_TO_VARSTR(pWrite, "-"); - } + STR_WITH_MAXSIZE_TO_VARSTR(pWrite, name, pShow->bytes[cols]); cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; diff --git a/source/dnode/mnode/impl/test/trans/trans.cpp b/source/dnode/mnode/impl/test/trans/trans.cpp index 8a62ed639a..d29c4fd658 100644 --- a/source/dnode/mnode/impl/test/trans/trans.cpp +++ b/source/dnode/mnode/impl/test/trans/trans.cpp @@ -61,6 +61,22 @@ class MndTestTrans : public ::testing::Test { Testbase MndTestTrans::test; TestServer MndTestTrans::server2; +TEST_F(MndTestTrans, 00_Create_User_Crash) { + test.SendShowMetaReq(TSDB_MGMT_TABLE_TRANS, ""); + CHECK_META("show trans", 7); + + CHECK_SCHEMA(0, TSDB_DATA_TYPE_INT, 4, "id"); + CHECK_SCHEMA(1, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time"); + CHECK_SCHEMA(2, TSDB_DATA_TYPE_BINARY, TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE, "stage"); + CHECK_SCHEMA(3, TSDB_DATA_TYPE_BINARY, TSDB_DB_NAME_LEN - 1 + VARSTR_HEADER_SIZE, "db"); + CHECK_SCHEMA(4, TSDB_DATA_TYPE_BINARY, TSDB_TRANS_TYPE_LEN + VARSTR_HEADER_SIZE, "type"); + CHECK_SCHEMA(5, TSDB_DATA_TYPE_TIMESTAMP, 8, "last_exec_time"); + CHECK_SCHEMA(6, TSDB_DATA_TYPE_BINARY, TSDB_TRANS_ERROR_LEN - 1 + VARSTR_HEADER_SIZE, "last_error"); + + test.SendShowRetrieveReq(); + EXPECT_EQ(test.GetShowRows(), 0); +} + TEST_F(MndTestTrans, 01_Create_User_Crash) { { SCreateUserReq createReq = {0}; @@ -171,6 +187,28 @@ TEST_F(MndTestTrans, 03_Create_Qnode2_Crash) { ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL); } + { + // show trans + test.SendShowMetaReq(TSDB_MGMT_TABLE_TRANS, ""); + CHECK_META("show trans", 7); + test.SendShowRetrieveReq(); + + EXPECT_EQ(test.GetShowRows(), 1); + CheckInt32(4); + CheckTimestamp(); + CheckBinary("undoAction", TSDB_TRANS_STAGE_LEN); + CheckBinary("", TSDB_DB_NAME_LEN - 1); + CheckBinary("create-qnode", TSDB_TRANS_TYPE_LEN); + CheckTimestamp(); + CheckBinary("Unable to establish connection", TSDB_TRANS_ERROR_LEN - 1); + } + + //kill trans + + // show trans + + // re-create trans + KillThenRestartServer(); server2.DoStart(); @@ -200,4 +238,13 @@ TEST_F(MndTestTrans, 03_Create_Qnode2_Crash) { test.SendShowRetrieveReq(); EXPECT_EQ(test.GetShowRows(), 2); } -} \ No newline at end of file +} + + +// create db +// partial create stb +// drop db failed +// create stb failed +// start +// create stb success +// drop db success From 1db73685709e0ab9afaf5efd8ed1b0646f4658b2 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 18 Feb 2022 16:22:06 +0800 Subject: [PATCH 77/79] when wal is restored, the stage of trans is changed from rollback to finish --- source/dnode/mgmt/impl/src/dndTransport.c | 1 + source/dnode/mnode/impl/src/mndSync.c | 2 + source/dnode/mnode/impl/src/mndTrans.c | 8 ++- source/dnode/mnode/impl/test/trans/trans.cpp | 75 +++++++++++++++----- 4 files changed, 68 insertions(+), 18 deletions(-) diff --git a/source/dnode/mgmt/impl/src/dndTransport.c b/source/dnode/mgmt/impl/src/dndTransport.c index 931cda475c..a0ba71a1eb 100644 --- a/source/dnode/mgmt/impl/src/dndTransport.c +++ b/source/dnode/mgmt/impl/src/dndTransport.c @@ -104,6 +104,7 @@ static void dndInitMsgFp(STransMgmt *pMgmt) { pMgmt->msgFp[TMSG_INDEX(TDMT_MND_SHOW_RETRIEVE)] = dndProcessMnodeReadMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_MND_STATUS)] = dndProcessMnodeReadMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_MND_STATUS_RSP)] = dndProcessMgmtMsg; + pMgmt->msgFp[TMSG_INDEX(TDMT_MND_KILL_TRANS)] = dndProcessMnodeWriteMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_MND_GRANT)] = dndProcessMnodeWriteMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_MND_GRANT_RSP)] = dndProcessMgmtMsg; pMgmt->msgFp[TMSG_INDEX(TDMT_MND_AUTH)] = dndProcessMnodeReadMsg; diff --git a/source/dnode/mnode/impl/src/mndSync.c b/source/dnode/mnode/impl/src/mndSync.c index 777492697a..8a14d882da 100644 --- a/source/dnode/mnode/impl/src/mndSync.c +++ b/source/dnode/mnode/impl/src/mndSync.c @@ -85,6 +85,8 @@ static int32_t mndRestoreWal(SMnode *pMnode) { mDebug("restore sdb wal finished, sdb ver:%" PRId64, sdbVer); mndTransPullup(pMnode); + sdbVer = sdbUpdateVer(pSdb, 0); + mDebug("pullup trans finished, sdb ver:%" PRId64, sdbVer); if (sdbVer != lastSdbVer) { mInfo("sdb restored from %" PRId64 " to %" PRId64 ", write file", lastSdbVer, sdbVer); diff --git a/source/dnode/mnode/impl/src/mndTrans.c b/source/dnode/mnode/impl/src/mndTrans.c index 5b1abdeb74..7d684cd542 100644 --- a/source/dnode/mnode/impl/src/mndTrans.c +++ b/source/dnode/mnode/impl/src/mndTrans.c @@ -442,6 +442,11 @@ static int32_t mndTransActionUpdate(SSdb *pSdb, STrans *pOld, STrans *pNew) { mTrace("trans:%d, stage from %s to %s", pNew->id, mndTransStr(TRN_STAGE_COMMIT), mndTransStr(TRN_STAGE_COMMIT_LOG)); } + if (pNew->stage == TRN_STAGE_ROLLBACK) { + pNew->stage = TRN_STAGE_FINISHED; + mTrace("trans:%d, stage from %s to %s", pNew->id, mndTransStr(TRN_STAGE_ROLLBACK), mndTransStr(TRN_STAGE_FINISHED)); + } + mTrace("trans:%d, perform update action, old row:%p stage:%s, new row:%p stage:%s", pOld->id, pOld, mndTransStr(pOld->stage), pNew, mndTransStr(pNew->stage)); pOld->stage = pNew->stage; @@ -1226,10 +1231,9 @@ static int32_t mndProcessKillTransReq(SMnodeMsg *pReq) { } code = mndKillTrans(pMnode, pTrans); - if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS; KILL_OVER: - if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) { + if (code != 0) { mError("trans:%d, failed to kill since %s", killReq.transId, terrstr()); return -1; } diff --git a/source/dnode/mnode/impl/test/trans/trans.cpp b/source/dnode/mnode/impl/test/trans/trans.cpp index d29c4fd658..d4c40dd428 100644 --- a/source/dnode/mnode/impl/test/trans/trans.cpp +++ b/source/dnode/mnode/impl/test/trans/trans.cpp @@ -28,7 +28,7 @@ class MndTestTrans : public ::testing::Test { static void KillThenRestartServer() { char file[PATH_MAX] = "/tmp/mnode_test_trans/mnode/data/sdb.data"; FileFd fd = taosOpenFileRead(file); - int32_t size = 1024 * 1024; + int32_t size = 3 * 1024 * 1024; void* buffer = malloc(size); int32_t readLen = taosReadFile(fd, buffer, size); if (readLen < 0 || readLen == size) { @@ -62,19 +62,34 @@ Testbase MndTestTrans::test; TestServer MndTestTrans::server2; TEST_F(MndTestTrans, 00_Create_User_Crash) { - test.SendShowMetaReq(TSDB_MGMT_TABLE_TRANS, ""); - CHECK_META("show trans", 7); + { + test.SendShowMetaReq(TSDB_MGMT_TABLE_TRANS, ""); + CHECK_META("show trans", 7); - CHECK_SCHEMA(0, TSDB_DATA_TYPE_INT, 4, "id"); - CHECK_SCHEMA(1, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time"); - CHECK_SCHEMA(2, TSDB_DATA_TYPE_BINARY, TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE, "stage"); - CHECK_SCHEMA(3, TSDB_DATA_TYPE_BINARY, TSDB_DB_NAME_LEN - 1 + VARSTR_HEADER_SIZE, "db"); - CHECK_SCHEMA(4, TSDB_DATA_TYPE_BINARY, TSDB_TRANS_TYPE_LEN + VARSTR_HEADER_SIZE, "type"); - CHECK_SCHEMA(5, TSDB_DATA_TYPE_TIMESTAMP, 8, "last_exec_time"); - CHECK_SCHEMA(6, TSDB_DATA_TYPE_BINARY, TSDB_TRANS_ERROR_LEN - 1 + VARSTR_HEADER_SIZE, "last_error"); + CHECK_SCHEMA(0, TSDB_DATA_TYPE_INT, 4, "id"); + CHECK_SCHEMA(1, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time"); + CHECK_SCHEMA(2, TSDB_DATA_TYPE_BINARY, TSDB_TRANS_STAGE_LEN + VARSTR_HEADER_SIZE, "stage"); + CHECK_SCHEMA(3, TSDB_DATA_TYPE_BINARY, TSDB_DB_NAME_LEN - 1 + VARSTR_HEADER_SIZE, "db"); + CHECK_SCHEMA(4, TSDB_DATA_TYPE_BINARY, TSDB_TRANS_TYPE_LEN + VARSTR_HEADER_SIZE, "type"); + CHECK_SCHEMA(5, TSDB_DATA_TYPE_TIMESTAMP, 8, "last_exec_time"); + CHECK_SCHEMA(6, TSDB_DATA_TYPE_BINARY, TSDB_TRANS_ERROR_LEN - 1 + VARSTR_HEADER_SIZE, "last_error"); - test.SendShowRetrieveReq(); - EXPECT_EQ(test.GetShowRows(), 0); + test.SendShowRetrieveReq(); + EXPECT_EQ(test.GetShowRows(), 0); + } + + { + SKillTransReq killReq = {0}; + killReq.transId = 3; + + int32_t contLen = tSerializeSKillTransReq(NULL, 0, &killReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSKillTransReq(pReq, contLen, &killReq); + + SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_TRANS, pReq, contLen); + ASSERT_NE(pRsp, nullptr); + ASSERT_EQ(pRsp->code, TSDB_CODE_MND_TRANS_NOT_EXIST); + } } TEST_F(MndTestTrans, 01_Create_User_Crash) { @@ -192,7 +207,7 @@ TEST_F(MndTestTrans, 03_Create_Qnode2_Crash) { test.SendShowMetaReq(TSDB_MGMT_TABLE_TRANS, ""); CHECK_META("show trans", 7); test.SendShowRetrieveReq(); - + EXPECT_EQ(test.GetShowRows(), 1); CheckInt32(4); CheckTimestamp(); @@ -202,12 +217,41 @@ TEST_F(MndTestTrans, 03_Create_Qnode2_Crash) { CheckTimestamp(); CheckBinary("Unable to establish connection", TSDB_TRANS_ERROR_LEN - 1); } - - //kill trans + + // kill trans + { + SKillTransReq killReq = {0}; + killReq.transId = 4; + + int32_t contLen = tSerializeSKillTransReq(NULL, 0, &killReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSKillTransReq(pReq, contLen, &killReq); + + SRpcMsg* pRsp = test.SendReq(TDMT_MND_KILL_TRANS, pReq, contLen); + ASSERT_NE(pRsp, nullptr); + ASSERT_EQ(pRsp->code, 0); + } // show trans + { + test.SendShowMetaReq(TSDB_MGMT_TABLE_TRANS, ""); + test.SendShowRetrieveReq(); + EXPECT_EQ(test.GetShowRows(), 0); + } // re-create trans + { + SMCreateQnodeReq createReq = {0}; + createReq.dnodeId = 2; + + int32_t contLen = tSerializeSMCreateDropQSBNodeReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSMCreateDropQSBNodeReq(pReq, contLen, &createReq); + + SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_QNODE, pReq, contLen); + ASSERT_NE(pRsp, nullptr); + ASSERT_EQ(pRsp->code, TSDB_CODE_RPC_NETWORK_UNAVAIL); + } KillThenRestartServer(); @@ -240,7 +284,6 @@ TEST_F(MndTestTrans, 03_Create_Qnode2_Crash) { } } - // create db // partial create stb // drop db failed From e5786ab798de2cd3f0ab3a99c4d6ec5aeabe9076 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 19 Feb 2022 13:09:14 +0000 Subject: [PATCH 78/79] more --- source/libs/tdb/inc/tdb.h | 1 + source/libs/tdb/src/db/tdb.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/source/libs/tdb/inc/tdb.h b/source/libs/tdb/inc/tdb.h index 85af417e76..cc0d20ef3c 100644 --- a/source/libs/tdb/inc/tdb.h +++ b/source/libs/tdb/inc/tdb.h @@ -47,6 +47,7 @@ int tdbEnvCommit(TENV *pEnv); 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); diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index d1b5863c28..cc3b7fa6b9 100644 --- a/source/libs/tdb/src/db/tdb.c +++ b/source/libs/tdb/src/db/tdb.c @@ -132,6 +132,11 @@ int tdbClose(TDB *pDb) { return tdbDestroy(pDb); } +int tdbDrop(TDB *pDb) { + // TODO + return 0; +} + int tdbSetKeyLen(TDB *pDb, int klen) { // TODO: check `klen` pDb->klen = klen; From 48e7becedfaf997f21ed51bb1f5ea4d2cf5808fa Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 19 Feb 2022 13:34:14 +0000 Subject: [PATCH 79/79] make compile --- source/dnode/vnode/inc/vnode.h | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index a689c1a500..1fb0b05f9e 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -42,9 +42,9 @@ typedef struct { typedef struct { int32_t vgId; - int64_t dbId; - SDnode * pDnode; - STfs * pTfs; + uint64_t dbId; + SDnode *pDnode; + STfs *pTfs; uint64_t wsize; uint64_t ssize; uint64_t lsize; @@ -61,9 +61,9 @@ typedef struct { typedef struct { int32_t sver; - const char * timezone; - const char * locale; - const char * charset; + const char *timezone; + const char *locale; + const char *charset; uint16_t nthreads; // number of commit threads. 0 for no threads and a schedule queue should be given (TODO) PutReqToVQueryQFp putReqToVQueryQFp; SendReqToDnodeFp sendReqToDnodeFp; @@ -71,17 +71,17 @@ typedef struct { typedef struct { int64_t ver; - uint64_t tbUid; - SHashObj * tbIdHash; - const SSubmitMsg *pMsg; - SSubmitBlk * pBlock; + int64_t tbUid; + SHashObj *tbIdHash; + const SSubmitReq *pMsg; + SSubmitBlk *pBlock; SSubmitMsgIter msgIter; SSubmitBlkIter blkIter; - SMeta * pVnodeMeta; - SArray * pColIdList; // SArray + SMeta *pVnodeMeta; + SArray *pColIdList; // SArray int32_t sver; - SSchemaWrapper * pSchemaWrapper; - STSchema * pSchema; + SSchemaWrapper *pSchemaWrapper; + STSchema *pSchema; } STqReadHandle; /* ------------------------ SVnode ------------------------ */