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"