refact TDB
This commit is contained in:
parent
67bc02d429
commit
0c4873381a
|
@ -1,9 +1,4 @@
|
||||||
# for tdb
|
# tdb
|
||||||
# set(TDB_SUBDIRS "db")
|
|
||||||
# foreach(TDB_SUBDIR ${TDB_SUBDIRS})
|
|
||||||
# aux_source_directory("src/${TDB_SUBDIR}" TDB_SRC)
|
|
||||||
# endforeach()
|
|
||||||
|
|
||||||
add_library(tdb "")
|
add_library(tdb "")
|
||||||
target_sources(tdb
|
target_sources(tdb
|
||||||
PRIVATE
|
PRIVATE
|
||||||
|
@ -11,7 +6,7 @@ target_sources(tdb
|
||||||
"src/db/tdbPFile.c"
|
"src/db/tdbPFile.c"
|
||||||
"src/db/tdbUtil.c"
|
"src/db/tdbUtil.c"
|
||||||
"src/db/tdbBtree.c"
|
"src/db/tdbBtree.c"
|
||||||
"src/db/tdb.c"
|
"src/db/tdbDb.c"
|
||||||
"src/db/tdbEnv.c"
|
"src/db/tdbEnv.c"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,5 +33,5 @@ target_include_directories(tdb_sqlite PUBLIC "src/sqliteinc")
|
||||||
|
|
||||||
# for test
|
# for test
|
||||||
if(${BUILD_TEST})
|
if(${BUILD_TEST})
|
||||||
# add_subdirectory(test)
|
add_subdirectory(test)
|
||||||
endif(${BUILD_TEST})
|
endif(${BUILD_TEST})
|
||||||
|
|
|
@ -22,44 +22,42 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct STDb TDB;
|
// typedef struct STDb TDB;
|
||||||
typedef struct STDbEnv TENV;
|
// typedef struct STDbEnv TENV;
|
||||||
typedef struct STDbCurosr TDBC;
|
// typedef struct STDbCurosr TDBC;
|
||||||
|
|
||||||
typedef int32_t pgsz_t;
|
// typedef int32_t pgsz_t;
|
||||||
typedef int32_t cachesz_t;
|
// typedef int32_t cachesz_t;
|
||||||
|
|
||||||
typedef int (*TdbKeyCmprFn)(int keyLen1, const void *pKey1, int keyLen2, const void *pKey2);
|
// typedef int (*TdbKeyCmprFn)(int keyLen1, const void *pKey1, int keyLen2, const void *pKey2);
|
||||||
|
|
||||||
// TEVN
|
// // TEVN
|
||||||
int tdbEnvCreate(TENV **ppEnv, const char *rootDir);
|
// int tdbEnvCreate(TENV **ppEnv, const char *rootDir);
|
||||||
int tdbEnvOpen(TENV *ppEnv);
|
// int tdbEnvOpen(TENV *ppEnv);
|
||||||
int tdbEnvClose(TENV *pEnv);
|
// int tdbEnvClose(TENV *pEnv);
|
||||||
|
|
||||||
int tdbEnvSetCache(TENV *pEnv, pgsz_t pgSize, cachesz_t cacheSize);
|
// int tdbEnvSetCache(TENV *pEnv, pgsz_t pgSize, cachesz_t cacheSize);
|
||||||
pgsz_t tdbEnvGetPageSize(TENV *pEnv);
|
// pgsz_t tdbEnvGetPageSize(TENV *pEnv);
|
||||||
cachesz_t tdbEnvGetCacheSize(TENV *pEnv);
|
// cachesz_t tdbEnvGetCacheSize(TENV *pEnv);
|
||||||
|
|
||||||
int tdbEnvBeginTxn(TENV *pEnv);
|
// int tdbEnvBeginTxn(TENV *pEnv);
|
||||||
int tdbEnvCommit(TENV *pEnv);
|
// int tdbEnvCommit(TENV *pEnv);
|
||||||
|
|
||||||
// TDB
|
// // TDB
|
||||||
int tdbCreate(TDB **ppDb);
|
// int tdbCreate(TDB **ppDb);
|
||||||
int tdbOpen(TDB *pDb, 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 tdbClose(TDB *pDb);
|
||||||
int tdbDrop(TDB *pDb);
|
// int tdbDrop(TDB *pDb);
|
||||||
|
|
||||||
int tdbSetKeyLen(TDB *pDb, int klen);
|
// int tdbSetKeyLen(TDB *pDb, int klen);
|
||||||
int tdbSetValLen(TDB *pDb, int vlen);
|
// int tdbSetValLen(TDB *pDb, int vlen);
|
||||||
int tdbSetDup(TDB *pDb, int dup);
|
// int tdbSetDup(TDB *pDb, int dup);
|
||||||
int tdbSetCmprFunc(TDB *pDb, TdbKeyCmprFn fn);
|
// int tdbSetCmprFunc(TDB *pDb, TdbKeyCmprFn fn);
|
||||||
int tdbGetKeyLen(TDB *pDb);
|
// int tdbGetKeyLen(TDB *pDb);
|
||||||
int tdbGetValLen(TDB *pDb);
|
// int tdbGetValLen(TDB *pDb);
|
||||||
int tdbGetDup(TDB *pDb);
|
// int tdbGetDup(TDB *pDb);
|
||||||
|
|
||||||
int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData);
|
// int tdbInsert(TDB *pDb, const void *pKey, int nKey, const void *pData, int nData);
|
||||||
|
|
||||||
// TDBC
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,29 @@
|
||||||
|
|
||||||
#include "tdbInt.h"
|
#include "tdbInt.h"
|
||||||
|
|
||||||
|
struct STDb {
|
||||||
|
STEnv *pEnv;
|
||||||
|
/* TODO */
|
||||||
|
};
|
||||||
|
|
||||||
|
int tdbDbOpen(STDb **ppDb) {
|
||||||
|
STDb *pDb;
|
||||||
|
|
||||||
|
*ppDb = NULL;
|
||||||
|
/* TODO */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tdbDbClose(STDb *pDb) {
|
||||||
|
// TODO
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int valLen) {
|
||||||
|
// TODO
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
struct STDb {
|
struct STDb {
|
||||||
char dbname[TDB_MAX_DBNAME_LEN];
|
char dbname[TDB_MAX_DBNAME_LEN];
|
||||||
|
@ -112,7 +135,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) {
|
||||||
ret = pgFileAllocatePage(pPgFile, &dbRootPgno);
|
ret = pgFileAllocatePage(pPgFile, &dbRootPgno);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
// TODO: handle error
|
// TODO: handle error
|
||||||
}
|
|
||||||
// tdbInsert(pPgFile->pMasterDB, dbname, strlen(dbname), &dbRootPgno, sizeof(dbRootPgno));
|
// tdbInsert(pPgFile->pMasterDB, dbname, strlen(dbname), &dbRootPgno, sizeof(dbRootPgno));
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,162 +55,3 @@ int tdbEnvClose(STEnv *pEnv) {
|
||||||
// TODO
|
// TODO
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
|
||||||
struct STDbEnv {
|
|
||||||
char * rootDir; // root directory of the environment
|
|
||||||
char * jname; // journal file name
|
|
||||||
int jfd; // journal file fd
|
|
||||||
pgsz_t pgSize; // page size
|
|
||||||
cachesz_t cacheSize; // total cache size
|
|
||||||
STDbList dbList; // TDB List
|
|
||||||
SPgFileList pgfList; // SPgFile List
|
|
||||||
SPgCache * pPgCache; // page cache
|
|
||||||
struct {
|
|
||||||
#define TDB_ENV_PGF_HASH_BUCKETS 17
|
|
||||||
SPgFileList buckets[TDB_ENV_PGF_HASH_BUCKETS];
|
|
||||||
} pgfht; // page file hash table;
|
|
||||||
};
|
|
||||||
|
|
||||||
#define TDB_ENV_PGF_HASH(fileid) \
|
|
||||||
({ \
|
|
||||||
uint8_t *tmp = (uint8_t *)(fileid); \
|
|
||||||
tmp[0] + tmp[1] + tmp[2]; \
|
|
||||||
})
|
|
||||||
|
|
||||||
static int tdbEnvDestroy(TENV *pEnv);
|
|
||||||
|
|
||||||
int tdbEnvCreate(TENV **ppEnv, const char *rootDir) {
|
|
||||||
TENV * pEnv;
|
|
||||||
size_t slen;
|
|
||||||
size_t jlen;
|
|
||||||
|
|
||||||
ASSERT(rootDir != NULL);
|
|
||||||
|
|
||||||
*ppEnv = NULL;
|
|
||||||
slen = strlen(rootDir);
|
|
||||||
jlen = slen + strlen(TDB_JOURNAL_NAME) + 1;
|
|
||||||
pEnv = (TENV *)calloc(1, sizeof(*pEnv) + slen + 1 + jlen + 1);
|
|
||||||
if (pEnv == NULL) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
pEnv->rootDir = (char *)(&pEnv[1]);
|
|
||||||
pEnv->jname = pEnv->rootDir + slen + 1;
|
|
||||||
pEnv->jfd = -1;
|
|
||||||
pEnv->pgSize = TDB_DEFAULT_PGSIZE;
|
|
||||||
pEnv->cacheSize = TDB_DEFAULT_CACHE_SIZE;
|
|
||||||
|
|
||||||
memcpy(pEnv->rootDir, rootDir, slen);
|
|
||||||
pEnv->rootDir[slen] = '\0';
|
|
||||||
sprintf(pEnv->jname, "%s/%s", rootDir, TDB_JOURNAL_NAME);
|
|
||||||
|
|
||||||
TD_DLIST_INIT(&(pEnv->dbList));
|
|
||||||
TD_DLIST_INIT(&(pEnv->pgfList));
|
|
||||||
|
|
||||||
/* TODO */
|
|
||||||
|
|
||||||
*ppEnv = pEnv;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int tdbEnvOpen(TENV *pEnv) {
|
|
||||||
SPgCache *pPgCache;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
ASSERT(pEnv != NULL);
|
|
||||||
|
|
||||||
/* TODO: here we do not need to create the root directory, more
|
|
||||||
* work should be done here
|
|
||||||
*/
|
|
||||||
mkdir(pEnv->rootDir, 0755);
|
|
||||||
|
|
||||||
ret = pgCacheOpen(&pPgCache, pEnv);
|
|
||||||
if (ret != 0) {
|
|
||||||
goto _err;
|
|
||||||
}
|
|
||||||
|
|
||||||
pEnv->pPgCache = pPgCache;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
_err:
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int tdbEnvClose(TENV *pEnv) {
|
|
||||||
if (pEnv == NULL) return 0;
|
|
||||||
pgCacheClose(pEnv->pPgCache);
|
|
||||||
tdbEnvDestroy(pEnv);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int tdbEnvSetCache(TENV *pEnv, pgsz_t pgSize, cachesz_t cacheSize) {
|
|
||||||
if (!TDB_IS_PGSIZE_VLD(pgSize) || cacheSize / pgSize < 10) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO */
|
|
||||||
|
|
||||||
pEnv->pgSize = pgSize;
|
|
||||||
pEnv->cacheSize = cacheSize;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pgsz_t tdbEnvGetPageSize(TENV *pEnv) { return pEnv->pgSize; }
|
|
||||||
|
|
||||||
cachesz_t tdbEnvGetCacheSize(TENV *pEnv) { return pEnv->cacheSize; }
|
|
||||||
|
|
||||||
SPgFile *tdbEnvGetPageFile(TENV *pEnv, const uint8_t fileid[]) {
|
|
||||||
SPgFileList *pBucket;
|
|
||||||
SPgFile * pPgFile;
|
|
||||||
|
|
||||||
pBucket = pEnv->pgfht.buckets + (TDB_ENV_PGF_HASH(fileid) % TDB_ENV_PGF_HASH_BUCKETS); // TODO
|
|
||||||
for (pPgFile = TD_DLIST_HEAD(pBucket); pPgFile != NULL; pPgFile = TD_DLIST_NODE_NEXT_WITH_FIELD(pPgFile, envHash)) {
|
|
||||||
if (memcmp(fileid, pPgFile->fileid, TDB_FILE_ID_LEN) == 0) break;
|
|
||||||
};
|
|
||||||
|
|
||||||
return pPgFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
SPgCache *tdbEnvGetPgCache(TENV *pEnv) { return pEnv->pPgCache; }
|
|
||||||
|
|
||||||
static int tdbEnvDestroy(TENV *pEnv) {
|
|
||||||
// TODO
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int tdbEnvBeginTxn(TENV *pEnv) {
|
|
||||||
pEnv->jfd = open(pEnv->jname, O_CREAT | O_RDWR, 0755);
|
|
||||||
if (pEnv->jfd < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int tdbEnvCommit(TENV *pEnv) {
|
|
||||||
/* TODO */
|
|
||||||
close(pEnv->jfd);
|
|
||||||
pEnv->jfd = -1;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *tdbEnvGetRootDir(TENV *pEnv) { return pEnv->rootDir; }
|
|
||||||
|
|
||||||
int tdbEnvRgstPageFile(TENV *pEnv, SPgFile *pPgFile) {
|
|
||||||
SPgFileList *pBucket;
|
|
||||||
|
|
||||||
TD_DLIST_APPEND_WITH_FIELD(&(pEnv->pgfList), pPgFile, envPgfList);
|
|
||||||
|
|
||||||
pBucket = pEnv->pgfht.buckets + (TDB_ENV_PGF_HASH(pPgFile->fileid) % TDB_ENV_PGF_HASH_BUCKETS); // TODO
|
|
||||||
TD_DLIST_APPEND_WITH_FIELD(pBucket, pPgFile, envHash);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int tdbEnvRgstDB(TENV *pEnv, TDB *pDb) {
|
|
||||||
// TODO
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can use, redistribute, and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License, version 3
|
||||||
|
* or later ("AGPL"), as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _TD_TDB_DB_H_
|
||||||
|
#define _TD_TDB_DB_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct STDb STDb;
|
||||||
|
|
||||||
|
int tdbDbOpen(STDb **ppDb);
|
||||||
|
int tdbDbClose(STDb *pDb);
|
||||||
|
int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int valLen);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /*_TD_TDB_DB_H_*/
|
|
@ -34,8 +34,6 @@ typedef uint16_t u16;
|
||||||
typedef uint32_t u32;
|
typedef uint32_t u32;
|
||||||
typedef uint64_t u64;
|
typedef uint64_t u64;
|
||||||
|
|
||||||
typedef struct SPgFile SPgFile;
|
|
||||||
|
|
||||||
// SPgno
|
// SPgno
|
||||||
typedef u32 SPgno;
|
typedef u32 SPgno;
|
||||||
#define TDB_IVLD_PGNO ((pgno_t)0)
|
#define TDB_IVLD_PGNO ((pgno_t)0)
|
||||||
|
@ -135,6 +133,8 @@ typedef TD_DLIST_NODE(SPgFile) SPgFileListNode;
|
||||||
|
|
||||||
#include "tdbEnv.h"
|
#include "tdbEnv.h"
|
||||||
|
|
||||||
|
#include "tdbDb.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,68 +1,28 @@
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
#include "tdb.h"
|
#include "tdbInt.h"
|
||||||
|
|
||||||
TEST(tdb_test, simple_test) {
|
TEST(tdb_test, simple_test) {
|
||||||
TENV * pEnv;
|
int ret;
|
||||||
TDB * pDb1, *pDb2, *pDb3;
|
STEnv *pEnv;
|
||||||
pgsz_t pgSize = 1024;
|
STDb *pDb;
|
||||||
cachesz_t cacheSize = 10240;
|
|
||||||
|
|
||||||
// ENV
|
// Open Env
|
||||||
GTEST_ASSERT_EQ(tdbEnvCreate(&pEnv, "./testtdb"), 0);
|
ret = tdbEnvOpen("tdb", 1024, 20, &pEnv);
|
||||||
|
GTEST_ASSERT_EQ(ret, 0);
|
||||||
|
|
||||||
GTEST_ASSERT_EQ(tdbEnvSetCache(pEnv, pgSize, cacheSize), 0);
|
// Create a database
|
||||||
|
ret = tdbDbOpen(&pDb);
|
||||||
|
GTEST_ASSERT_EQ(ret, 0);
|
||||||
|
|
||||||
GTEST_ASSERT_EQ(tdbEnvGetCacheSize(pEnv), cacheSize);
|
// Insert some data
|
||||||
|
ret = tdbDbInsert(pDb, "1", 1, "world", 5);
|
||||||
|
GTEST_ASSERT_EQ(ret, 0);
|
||||||
|
|
||||||
GTEST_ASSERT_EQ(tdbEnvGetPageSize(pEnv), pgSize);
|
// Close a database
|
||||||
|
tdbDbClose(pDb);
|
||||||
|
|
||||||
GTEST_ASSERT_EQ(tdbEnvOpen(pEnv), 0);
|
// Close Env
|
||||||
|
ret = tdbEnvClose(pEnv);
|
||||||
#if 1
|
GTEST_ASSERT_EQ(ret, 0);
|
||||||
// DB
|
|
||||||
GTEST_ASSERT_EQ(tdbCreate(&pDb1), 0);
|
|
||||||
|
|
||||||
// GTEST_ASSERT_EQ(tdbSetKeyLen(pDb1, 8), 0);
|
|
||||||
|
|
||||||
// GTEST_ASSERT_EQ(tdbGetKeyLen(pDb1), 8);
|
|
||||||
|
|
||||||
// GTEST_ASSERT_EQ(tdbSetValLen(pDb1, 3), 0);
|
|
||||||
|
|
||||||
// GTEST_ASSERT_EQ(tdbGetValLen(pDb1), 3);
|
|
||||||
|
|
||||||
// GTEST_ASSERT_EQ(tdbSetDup(pDb1, 1), 0);
|
|
||||||
|
|
||||||
// GTEST_ASSERT_EQ(tdbGetDup(pDb1), 1);
|
|
||||||
|
|
||||||
// GTEST_ASSERT_EQ(tdbSetCmprFunc(pDb1, NULL), 0);
|
|
||||||
|
|
||||||
tdbEnvBeginTxn(pEnv);
|
|
||||||
|
|
||||||
GTEST_ASSERT_EQ(tdbOpen(pDb1, "db.db", "db1", pEnv), 0);
|
|
||||||
|
|
||||||
// char *key = "key1";
|
|
||||||
// char *val = "value1";
|
|
||||||
// tdbInsert(pDb1, (void *)key, strlen(key), (void *)val, strlen(val));
|
|
||||||
|
|
||||||
tdbEnvCommit(pEnv);
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
// Insert
|
|
||||||
|
|
||||||
// Query
|
|
||||||
|
|
||||||
// Delete
|
|
||||||
|
|
||||||
// Query
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// GTEST_ASSERT_EQ(tdbOpen(&pDb2, "db.db", "db2", pEnv), 0);
|
|
||||||
// GTEST_ASSERT_EQ(tdbOpen(&pDb3, "index.db", NULL, pEnv), 0);
|
|
||||||
// tdbClose(pDb3);
|
|
||||||
// tdbClose(pDb2);
|
|
||||||
tdbClose(pDb1);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
tdbEnvClose(pEnv);
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue