This commit is contained in:
Hongze Cheng 2022-02-15 10:11:07 +00:00
parent f77d33dcb1
commit c9cd5fce3b
4 changed files with 32 additions and 28 deletions

View File

@ -87,17 +87,20 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) {
pDb->dbname[dbNameLen] = '\0'; pDb->dbname[dbNameLen] = '\0';
// open pPgFile or get from the env // open pPgFile or get from the env
pPgFile = NULL;
snprintf(dbfname, 128, "%s/%s", tdbEnvGetRootDir(pEnv), fname); snprintf(dbfname, 128, "%s/%s", tdbEnvGetRootDir(pEnv), fname);
fileExist = (tdbCheckFileAccess(fname, TDB_F_OK) == 0); fileExist = (tdbCheckFileAccess(fname, TDB_F_OK) == 0);
if (fileExist) { if (fileExist) {
// TODO tdbGnrtFileID(dbfname, fileid, false);
} else { pPgFile = tdbEnvGetPageFile(pEnv, fileid);
}
if (pPgFile == NULL) {
ret = pgFileOpen(&pPgFile, dbfname, pEnv); ret = pgFileOpen(&pPgFile, dbfname, pEnv);
if (ret != 0) { if (ret != 0) {
// TODO: handle error // TODO: handle error
return -1; return -1;
} }
// Create and open the page file
} }
#if 0 #if 0

View File

@ -47,6 +47,7 @@ int tdbEnvCreate(TENV **ppEnv, const char *rootDir) {
pEnv->cacheSize = TDB_DEFAULT_CACHE_SIZE; pEnv->cacheSize = TDB_DEFAULT_CACHE_SIZE;
memcpy(pEnv->rootDir, rootDir, slen); memcpy(pEnv->rootDir, rootDir, slen);
pEnv->rootDir[slen] = '\0';
TD_DLIST_INIT(&(pEnv->dbList)); TD_DLIST_INIT(&(pEnv->dbList));
TD_DLIST_INIT(&(pEnv->pgfList)); TD_DLIST_INIT(&(pEnv->pgfList));

View File

@ -20,38 +20,35 @@ static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData);
int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) {
SPgFile * pPgFile; SPgFile * pPgFile;
SPgCache *pPgCache; SPgCache *pPgCache;
size_t fnameLen;
*ppPgFile = NULL; *ppPgFile = NULL;
pPgFile = (SPgFile *)calloc(1, sizeof(*pPgFile)); // create the handle
fnameLen = strlen(fname);
pPgFile = (SPgFile *)calloc(1, sizeof(*pPgFile) + fnameLen + 1);
if (pPgFile == NULL) { if (pPgFile == NULL) {
return -1; return -1;
} }
ASSERT(pEnv != NULL);
// init the handle
pPgFile->pEnv = pEnv;
pPgFile->fname = (char *)(&(pPgFile[1]));
memcpy(pPgFile->fname, fname, fnameLen);
pPgFile->fname[fnameLen] = '\0';
pPgFile->fd = -1; pPgFile->fd = -1;
pPgFile->fname = strdup(fname); pPgFile->fd = open(fname, O_CREAT | O_RDWR, 0755);
if (pPgFile->fname == NULL) {
pgFileClose(pPgFile);
return -1;
}
pPgFile->pPgCache = pPgCache;
// pPgFile->pgSize = ; (TODO)
pPgFile->fd = open(fname, O_RDWR, 0755);
if (pPgFile->fd < 0) { if (pPgFile->fd < 0) {
pgFileClose(pPgFile); // TODO: handle error
return -1; return -1;
} }
if (tdbGnrtFileID(fname, pPgFile->fileid, false) < 0) { tdbGnrtFileID(fname, pPgFile->fileid, false);
pgFileClose(pPgFile);
return -1;
}
// TODO: get file size /* TODO */
pPgFile->pgFileSize = 0;
*ppPgFile = pPgFile; *ppPgFile = pPgFile;
return 0; return 0;
@ -75,6 +72,7 @@ SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) {
SPage * pPage; SPage * pPage;
pgid_t pgid; pgid_t pgid;
#if 0
pPgCache = pPgFile->pPgCache; pPgCache = pPgFile->pPgCache;
pPage = NULL; pPage = NULL;
memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN); memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN);
@ -94,6 +92,7 @@ SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) {
return pPage; return pPage;
} }
} }
#endif
return pPage; return pPage;
} }
@ -114,6 +113,8 @@ static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) {
uint8_t *pTData; uint8_t *pTData;
size_t szToRead; size_t szToRead;
#if 0
// pgSize = ; (TODO) // pgSize = ; (TODO)
pTData = pData; pTData = pData;
szToRead = pgSize; szToRead = pgSize;
@ -132,6 +133,7 @@ static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) {
szToRead -= rsize; szToRead -= rsize;
pTData += rsize; pTData += rsize;
} }
#endif
return 0; return 0;
} }

View File

@ -33,13 +33,11 @@ typedef struct __attribute__((__packed__)) {
TDB_STATIC_ASSERT(sizeof(SPgFileHdr) == TDB_PG_FILE_HDR_SIZE, "Page file header size if not 128"); TDB_STATIC_ASSERT(sizeof(SPgFileHdr) == TDB_PG_FILE_HDR_SIZE, "Page file header size if not 128");
struct SPgFile { struct SPgFile {
TENV * pEnv; // env containing this page file
char * fname; // backend file name char * fname; // backend file name
uint8_t fileid[TDB_FILE_ID_LEN]; // file id uint8_t fileid[TDB_FILE_ID_LEN]; // file id
SPgCache *pPgCache; // page cache underline
pgsz_t pgSize;
int fd; int fd;
pgno_t pgFileSize; // TDB * pDb; // For a SPgFile for multiple databases, this is the <dbname, pgno> mapping DB.
TDB * pDb; // For a SPgFile for multiple databases, this is the <dbname, pgno> mapping DB.
}; };
int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv); int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv);