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);