diff --git a/source/libs/tdb/src/db/tdb.c b/source/libs/tdb/src/db/tdb.c index eab3db13b7d0df95989545c3fd372cd6473d6f68..9fe15ce156445c20675f075846fdbd93ef3a42c3 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 9954770046148c187ff90df15f820693e6e9f4f1..237220d155a60d83fc249be39aa3449b2df4f04d 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 4f9f32b8ea0554a7416b544fabd09c6d2b8d8c1c..6cb5c7a2cda8537ee07a4820e9cb9d5ae6df93a9 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 }