From b45884c15135ad04b4eb2bc5e094667d168f7e6f Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 12 Oct 2021 14:45:59 +0800 Subject: [PATCH] more --- include/server/vnode/meta/meta.h | 10 ++ source/server/vnode/meta/inc/metaUid.h | 34 ++++++ source/server/vnode/meta/src/meta.c | 106 +++++++++++++++++-- source/server/vnode/meta/src/metaUid.c | 23 ++++ source/server/vnode/meta/test/CMakeLists.txt | 1 + 5 files changed, 164 insertions(+), 10 deletions(-) create mode 100644 source/server/vnode/meta/inc/metaUid.h create mode 100644 source/server/vnode/meta/src/metaUid.c diff --git a/include/server/vnode/meta/meta.h b/include/server/vnode/meta/meta.h index f639ed243f..9797ed8168 100644 --- a/include/server/vnode/meta/meta.h +++ b/include/server/vnode/meta/meta.h @@ -56,6 +56,16 @@ void metaQueryHandleDestroy(SMetaQueryHandle *); SMetaQueryOpts *metaQueryOptionsCreate(); void metaQueryOptionsDestroy(SMetaQueryOpts *); +// STableOpts +void metaTableOptsInit(int8_t type, const char *name, const STSchema *pSchema); + +/* -------------------------------- Hided implementations -------------------------------- */ +struct STableOpts { + int8_t type; + char * name; + STSchema *pSchema; +}; + #ifdef __cplusplus } #endif diff --git a/source/server/vnode/meta/inc/metaUid.h b/source/server/vnode/meta/inc/metaUid.h new file mode 100644 index 0000000000..b01492cbf7 --- /dev/null +++ b/source/server/vnode/meta/inc/metaUid.h @@ -0,0 +1,34 @@ +/* + * 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_META_UID_H_ +#define _TD_META_UID_H_ + +#include "os.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef uint64_t tb_uid_t; +tb_uid_t metaGenerateUid(); + +#define IVLD_TB_UID 0 + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_META_UID_H_*/ \ No newline at end of file diff --git a/source/server/vnode/meta/src/meta.c b/source/server/vnode/meta/src/meta.c index a035fef205..9cf9d7ba78 100644 --- a/source/server/vnode/meta/src/meta.c +++ b/source/server/vnode/meta/src/meta.c @@ -21,12 +21,14 @@ #include "ttypes.h" #include "meta.h" +#include "metaUid.h" /* -------------------- Structures -------------------- */ + typedef struct STable { - uint64_t uid; - tstr * name; - uint64_t suid; + tb_uid_t uid; + char * name; + tb_uid_t suid; SArray * schema; } STable; @@ -51,11 +53,8 @@ struct SMeta { size_t totalUsed; }; -struct STableOpts { - int8_t type; - char * name; - STSchema *pSchema; -}; +static STable * metaTableNew(tb_uid_t uid, const char *name, int32_t sver); +static STableObj *metaTableObjNew(); /* -------------------- Methods -------------------- */ @@ -111,10 +110,97 @@ void metaClose(SMeta *pMeta) { } int metaCreateTable(SMeta *pMeta, STableOpts *pTableOpts) { - // TODO + size_t vallen; + char * err = NULL; + rocksdb_readoptions_t * ropt; + STableObj * pTableObj = NULL; + rocksdb_writeoptions_t *wopt; + + // Check if table already exists + ropt = rocksdb_readoptions_create(); + + char *uidStr = rocksdb_get(pMeta->tbnameDb, ropt, pTableOpts->name, strlen(pTableOpts->name), &vallen, &err); + if (uidStr != NULL) { + // Has duplicate named table + return -1; + } + + rocksdb_readoptions_destroy(ropt); + + // Create table obj + pTableObj = metaTableObjNew(); + if (pTableObj == NULL) { + // TODO + return -1; + } + + // Create table object + pTableObj->pTable = metaTableNew(metaGenerateUid(), pTableOpts->name, schemaVersion(pTableOpts->pSchema)); + if (pTableObj->pTable == NULL) { + // TODO + } + + pthread_rwlock_rdlock(&pMeta->rwLock); + + taosHashPut(pMeta->pTableObjHash, &(pTableObj->pTable->uid), sizeof(tb_uid_t), &pTableObj, sizeof(pTableObj)); + + wopt = rocksdb_writeoptions_create(); + + rocksdb_put(pMeta->tbnameDb, wopt, pTableOpts->name, strlen(pTableOpts->name), &pTableObj->pTable->uid, + sizeof(tb_uid_t), &err); + rocksdb_put(pMeta->schemaDb, wopt, pTableOpts->name, strlen(pTableOpts->name), &pTableObj->pTable->uid, + sizeof(tb_uid_t), &err); + + rocksdb_writeoptions_destroy(wopt); + + pthread_rwlock_unlock(&pMeta->rwLock); + return 0; } void metaDestroy(const char *path) { taosRemoveDir(path); } -int metaCommit(SMeta *meta) { return 0; } \ No newline at end of file +int metaCommit(SMeta *meta) { return 0; } + +/* -------------------- Static Methods -------------------- */ + +static STable *metaTableNew(tb_uid_t uid, const char *name, int32_t sver) { + STable *pTable = NULL; + + pTable = (STable *)malloc(sizeof(*pTable)); + if (pTable == NULL) { + // TODO + return NULL; + } + + pTable->schema = taosArrayInit(0, sizeof(int32_t)); + if (pTable->schema == NULL) { + // TODO + return NULL; + } + + pTable->uid = uid; + pTable->name = strdup(name); + pTable->suid = IVLD_TB_UID; + taosArrayPush(pTable->schema, &sver); + + return pTable; +} + +static STableObj *metaTableObjNew() { + STableObj *pTableObj = NULL; + + pTableObj = (STableObj *)malloc(sizeof(*pTableObj)); + if (pTableObj == NULL) { + return NULL; + } + + pTableObj->pin = true; + pTableObj->ref = 1; + taosInitRWLatch(&(pTableObj->latch)); + pTableObj->offset = UINT64_MAX; + pTableObj->ctbList = NULL; + pTableObj->pTable = NULL; + + return pTableObj; +} \ No newline at end of file diff --git a/source/server/vnode/meta/src/metaUid.c b/source/server/vnode/meta/src/metaUid.c new file mode 100644 index 0000000000..4662969a2e --- /dev/null +++ b/source/server/vnode/meta/src/metaUid.c @@ -0,0 +1,23 @@ +/* + * 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 "metaUid.h" + +static tb_uid_t nuid = IVLD_TB_UID; + +tb_uid_t metaGenerateUid() { + // TODO: need a more complex UID generator + return ++nuid; +} \ No newline at end of file diff --git a/source/server/vnode/meta/test/CMakeLists.txt b/source/server/vnode/meta/test/CMakeLists.txt index ce00dc0a66..ee16a28687 100644 --- a/source/server/vnode/meta/test/CMakeLists.txt +++ b/source/server/vnode/meta/test/CMakeLists.txt @@ -2,6 +2,7 @@ add_executable(metaTest "") target_sources(metaTest PRIVATE "../src/meta.c" + "../src/metaUid.c" "metaTests.cpp" ) target_include_directories(metaTest