提交 8f6781a7 编写于 作者: H Hongze Cheng

more TDB

上级 4b5f00ca
...@@ -115,7 +115,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) { ...@@ -115,7 +115,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) {
} }
} }
pDb->pBt->root = dbRootPgno; // pDb->pBt->root = dbRootPgno;
// register // register
pDb->pPgFile = pPgFile; pDb->pPgFile = pPgFile;
......
...@@ -30,6 +30,7 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { ...@@ -30,6 +30,7 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) {
SPgFile * pPgFile; SPgFile * pPgFile;
SPgCache *pPgCache; SPgCache *pPgCache;
size_t fnameLen; size_t fnameLen;
pgno_t fsize;
*ppPgFile = NULL; *ppPgFile = NULL;
...@@ -55,6 +56,28 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) { ...@@ -55,6 +56,28 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) {
} }
tdbGnrtFileID(fname, pPgFile->fileid, false); tdbGnrtFileID(fname, pPgFile->fileid, false);
tdbGetFileSize(fname, tdbEnvGetPageSize(pEnv), &fsize);
pPgFile->fsize = fsize;
pPgFile->lsize = fsize;
if (pPgFile->fsize == 0) {
// A created file
pgno_t pgno;
pgid_t pgid;
pgFileAllocatePage(pPgFile, &pgno);
ASSERT(pgno == 1);
memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN);
pgid.pgno = pgno;
pgCacheFetch(pPgCache, pgid);
// Need to allocate the first page as a description page
} else {
// An existing file
}
/* TODO: other open operations */ /* TODO: other open operations */
...@@ -122,11 +145,15 @@ int pgFileWrite(SPage *pPage) { ...@@ -122,11 +145,15 @@ int pgFileWrite(SPage *pPage) {
int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) { int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) {
pgno_t pgno; pgno_t pgno;
if (pPgFile->lsize == 0) {
pgno = ++(pPgFile->lsize);
} else {
if (0) { if (0) {
// TODO: allocate from the free list // TODO: allocate from the free list
} else { } else {
pgno = ++(pPgFile->lsize); pgno = ++(pPgFile->lsize);
} }
}
*pPgno = pgno; *pPgno = pgno;
return 0; return 0;
......
...@@ -51,7 +51,17 @@ int tdbCheckFileAccess(const char *pathname, int mode) { ...@@ -51,7 +51,17 @@ int tdbCheckFileAccess(const char *pathname, int mode) {
return access(pathname, flags); return access(pathname, flags);
} }
int64_t tdbGetFileSize(const char *fname) { int tdbGetFileSize(const char *fname, pgsz_t pgSize, pgno_t *pSize) {
// TODO struct stat st;
int ret;
ret = stat(fname, &st);
if (ret != 0) {
return -1;
}
ASSERT(st.st_size % pgSize == 0);
*pSize = st.st_size / pgSize;
return 0; return 0;
} }
\ No newline at end of file
...@@ -35,7 +35,7 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique); ...@@ -35,7 +35,7 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique);
#define TDB_W_OK 0x4 #define TDB_W_OK 0x4
int tdbCheckFileAccess(const char *pathname, int mode); int tdbCheckFileAccess(const char *pathname, int mode);
int64_t tdbGetFileSize(const char *fname); int tdbGetFileSize(const char *fname, pgsz_t pgSize, pgno_t *pSize);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册