more TDB
This commit is contained in:
parent
4b5f00ca3b
commit
8f6781a7bb
|
@ -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
|
||||
pDb->pPgFile = pPgFile;
|
||||
|
|
|
@ -30,6 +30,7 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) {
|
|||
SPgFile * pPgFile;
|
||||
SPgCache *pPgCache;
|
||||
size_t fnameLen;
|
||||
pgno_t fsize;
|
||||
|
||||
*ppPgFile = NULL;
|
||||
|
||||
|
@ -55,6 +56,28 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) {
|
|||
}
|
||||
|
||||
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 */
|
||||
|
||||
|
@ -122,10 +145,14 @@ int pgFileWrite(SPage *pPage) {
|
|||
int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) {
|
||||
pgno_t pgno;
|
||||
|
||||
if (0) {
|
||||
// TODO: allocate from the free list
|
||||
} else {
|
||||
if (pPgFile->lsize == 0) {
|
||||
pgno = ++(pPgFile->lsize);
|
||||
} else {
|
||||
if (0) {
|
||||
// TODO: allocate from the free list
|
||||
} else {
|
||||
pgno = ++(pPgFile->lsize);
|
||||
}
|
||||
}
|
||||
|
||||
*pPgno = pgno;
|
||||
|
|
|
@ -51,7 +51,17 @@ int tdbCheckFileAccess(const char *pathname, int mode) {
|
|||
return access(pathname, flags);
|
||||
}
|
||||
|
||||
int64_t tdbGetFileSize(const char *fname) {
|
||||
// TODO
|
||||
int tdbGetFileSize(const char *fname, pgsz_t pgSize, pgno_t *pSize) {
|
||||
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;
|
||||
}
|
|
@ -35,7 +35,7 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique);
|
|||
#define TDB_W_OK 0x4
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue