提交 708fef9a 编写于 作者: H Hongze Cheng

more

上级 dac6531b
......@@ -65,7 +65,7 @@ int tdbOpen(TDB *pDb, const char *fname, const char *dbname, TENV *pEnv) {
SBTree * pBt;
bool fileExist;
size_t dbNameLen;
pgno_t dbRootPgno;
SPgno dbRootPgno;
char dbfname[128]; // TODO: make this as a macro or malloc on the heap
ASSERT(pDb != NULL);
......
......@@ -17,12 +17,12 @@
struct SBtCursor {
SBTree *pBtree;
pgno_t pgno;
SPgno pgno;
SPage * pPage; // current page traversing
};
typedef struct {
pgno_t pgno;
SPgno pgno;
pgsz_t offset;
} SBtIdx;
......@@ -34,7 +34,7 @@ typedef struct __attribute__((__packed__)) {
pgoff_t freeOff; // free payload offset
pgsz_t fragSize; // total fragment size
pgoff_t offPayload; // payload offset
pgno_t rChildPgno; // right most child page number
SPgno rChildPgno; // right most child page number
} SBtPgHdr;
typedef int (*BtreeCmprFn)(const void *, const void *);
......@@ -45,7 +45,7 @@ typedef int (*BtreeCmprFn)(const void *, const void *);
static int btreeCreate(SBTree **ppBt);
static int btreeDestroy(SBTree *pBt);
static int btreeCursorMoveToChild(SBtCursor *pBtCur, pgno_t pgno);
static int btreeCursorMoveToChild(SBtCursor *pBtCur, SPgno pgno);
int btreeOpen(SBTree **ppBt, SPgFile *pPgFile) {
SBTree *pBt;
......@@ -98,8 +98,8 @@ int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey) {
SPage * pPage;
SBtPgHdr * pBtPgHdr;
SPgFile * pPgFile;
pgno_t childPgno;
pgno_t rootPgno;
SPgno childPgno;
SPgno rootPgno;
int nPayloads;
void * pPayload;
BtreeCmprFn cmpFn;
......@@ -157,7 +157,7 @@ int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey) {
return 0;
}
static int btreeCursorMoveToChild(SBtCursor *pBtCur, pgno_t pgno) {
static int btreeCursorMoveToChild(SBtCursor *pBtCur, SPgno pgno) {
SPgFile *pPgFile;
// TODO
return 0;
......
......@@ -24,20 +24,19 @@ struct SPCache {
SPgHdr * lru;
int nRecyclable;
int nHash;
SPgHdr * pgHash;
SPgHdr ** pgHash;
int nFree;
SPgHdr * pFree;
};
struct SPgHdr {
void * pData;
SPgid pgid;
SPgHdr *pFreeNext;
};
#define PCACHE_PAGE_HASH(pgid) 0 // TODO
static void tdbPCacheLock(SPCache *pCache);
static void tdbPCacheUnlock(SPCache *pCache);
static bool tdbPCacheLocked(SPCache *pCache);
static void tdbPCacheInitLock(SPCache *pCache);
static void tdbPCacheClearLock(SPCache *pCache);
static void tdbPCacheLock(SPCache *pCache);
static void tdbPCacheUnlock(SPCache *pCache);
static bool tdbPCacheLocked(SPCache *pCache);
static SPgHdr *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage);
int tdbOpenPCache(int pageSize, int cacheSize, int extraSize, SPCache **ppCache) {
SPCache *pCache;
......@@ -53,7 +52,7 @@ int tdbOpenPCache(int pageSize, int cacheSize, int extraSize, SPCache **ppCache)
pCache->cacheSize = cacheSize;
pCache->extraSize = extraSize;
pthread_mutex_init(&pCache->mutex, NULL);
tdbPCacheInitLock(pCache);
for (int i = 0; i < cacheSize; i++) {
pPtr = calloc(1, pageSize + extraSize + sizeof(SPgHdr));
......@@ -76,17 +75,24 @@ int tdbClosePCache(SPCache *pCache) {
return 0;
}
void *tdbPCacheFetch(SPCache *pCache, SPgid *pPgid) {
SPgHdr *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
SPgHdr *pPage;
tdbPCacheLock(pCache);
// 1. search the hash table
pPage = tdbPCacheFetchImpl(pCache, pPgid, alcNewPage);
tdbPCacheUnlock(pCache);
return NULL;
return pPage;
}
void tdbPCacheRelease(void *pHdr) {
void tdbPCacheRelease(SPgHdr *pHdr) {
// TODO
}
static void tdbPCacheInitLock(SPCache *pCache) { pthread_mutex_init(&(pCache->mutex), NULL); }
static void tdbPCacheClearLock(SPCache *pCache) { pthread_mutex_destroy(&(pCache->mutex)); }
static void tdbPCacheLock(SPCache *pCache) { pthread_mutex_lock(&(pCache->mutex)); }
static void tdbPCacheUnlock(SPCache *pCache) { pthread_mutex_unlock(&(pCache->mutex)); }
......@@ -95,4 +101,26 @@ static bool tdbPCacheLocked(SPCache *pCache) {
assert(0);
// TODO
return true;
}
static SPgHdr *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
SPgHdr *pPage;
// 1. Search the hash table
pPage = pCache->pgHash[PCACHE_PAGE_HASH(pPgid) % pCache->nHash];
while (pPage) {
if (memcmp(pPgid, &(pPage->pgid), sizeof(*pPgid)) == 0) break;
pPage = pPage->pHashNext;
}
if (pPage) {
// TODO: pin the page and return the page
return pPage;
} else if (!alcNewPage) {
return pPage;
}
// Try other methods
return pPage;
}
\ No newline at end of file
......@@ -17,8 +17,8 @@
typedef struct SPage1 {
char magic[64];
pgno_t mdbRootPgno; // master DB root page number
pgno_t freePgno; // free list page number
SPgno mdbRootPgno; // master DB root page number
SPgno freePgno; // free list page number
uint32_t nFree; // number of free pages
} SPage1;
......@@ -28,13 +28,13 @@ typedef struct SFreePage {
TDB_STATIC_ASSERT(sizeof(SPage1) <= TDB_MIN_PGSIZE, "TDB Page1 definition too large");
static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData);
static int pgFileRead(SPgFile *pPgFile, SPgno pgno, uint8_t *pData);
int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) {
SPgFile * pPgFile;
SPgCache *pPgCache;
size_t fnameLen;
pgno_t fsize;
SPgno fsize;
*ppPgFile = NULL;
......@@ -67,7 +67,7 @@ int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) {
if (pPgFile->fsize == 0) {
// A created file
pgno_t pgno;
SPgno pgno;
pgid_t pgid;
pgFileAllocatePage(pPgFile, &pgno);
......@@ -106,7 +106,7 @@ int pgFileClose(SPgFile *pPgFile) {
return 0;
}
SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) {
SPage *pgFileFetch(SPgFile *pPgFile, SPgno pgno) {
SPgCache *pPgCache;
SPage * pPage;
pgid_t pgid;
......@@ -161,8 +161,8 @@ int pgFileWrite(SPage *pPage) {
return 0;
}
int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) {
pgno_t pgno;
int pgFileAllocatePage(SPgFile *pPgFile, SPgno *pPgno) {
SPgno pgno;
SPage1 * pPage1;
SPgCache *pPgCache;
pgid_t pgid;
......@@ -189,7 +189,7 @@ int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) {
return 0;
}
static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) {
static int pgFileRead(SPgFile *pPgFile, SPgno pgno, uint8_t *pData) {
pgsz_t pgSize;
ssize_t rsize;
uint8_t *pTData;
......
......@@ -51,7 +51,7 @@ int tdbCheckFileAccess(const char *pathname, int mode) {
return access(pathname, flags);
}
int tdbGetFileSize(const char *fname, pgsz_t pgSize, pgno_t *pSize) {
int tdbGetFileSize(const char *fname, pgsz_t pgSize, SPgno *pSize) {
struct stat st;
int ret;
......
......@@ -34,7 +34,7 @@ int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey);
int btreeCursorNext(SBtCursor *pBtCur);
struct SBTree {
pgno_t root;
SPgno root;
};
#ifdef __cplusplus
......
......@@ -27,8 +27,8 @@ extern "C" {
typedef struct SPgFile SPgFile;
// pgno_t
typedef int32_t pgno_t;
// SPgno
typedef int32_t SPgno;
#define TDB_IVLD_PGNO ((pgno_t)0)
// fileid
......@@ -37,7 +37,7 @@ typedef int32_t pgno_t;
// pgid_t
typedef struct {
uint8_t fileid[TDB_FILE_ID_LEN];
pgno_t pgno;
SPgno pgno;
} pgid_t, SPgid;
#define TDB_IVLD_PGID (pgid_t){0, TDB_IVLD_PGNO};
......
......@@ -23,10 +23,18 @@ extern "C" {
typedef struct SPCache SPCache;
typedef struct SPgHdr SPgHdr;
int tdbOpenPCache(int pageSize, int cacheSize, int extraSize, SPCache **ppCache);
int tdbPCacheClose(SPCache *pCache);
void *tdbPCacheFetch(SPCache *pCache, SPgid *pPgid);
void tdbPCacheRelease(void *pHdr);
struct SPgHdr {
void * pData;
void * pExtra;
SPgid pgid;
SPgHdr *pFreeNext;
SPgHdr *pHashNext;
};
int tdbOpenPCache(int pageSize, int cacheSize, int extraSize, SPCache **ppCache);
int tdbPCacheClose(SPCache *pCache);
SPgHdr *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage);
void tdbPCacheRelease(SPgHdr *pHdr);
#ifdef __cplusplus
}
......
......@@ -24,7 +24,7 @@ typedef struct __attribute__((__packed__)) {
char hdrInfo[16]; // info string
pgsz_t szPage; // page size of current file
int32_t cno; // commit number counter
pgno_t freePgno; // freelist page number
SPgno freePgno; // freelist page number
uint8_t resv[100]; // reserved space
} SPgFileHdr;
......@@ -36,8 +36,8 @@ struct SPgFile {
TENV * pEnv; // env containing this page file
char * fname; // backend file name
uint8_t fileid[TDB_FILE_ID_LEN]; // file id
pgno_t lsize; // page file logical size (for count)
pgno_t fsize; // real file size on disk (for rollback)
SPgno lsize; // page file logical size (for count)
SPgno fsize; // real file size on disk (for rollback)
int fd;
SPgFileListNode envHash;
SPgFileListNode envPgfList;
......@@ -46,11 +46,11 @@ struct SPgFile {
int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv);
int pgFileClose(SPgFile *pPgFile);
SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno);
SPage *pgFileFetch(SPgFile *pPgFile, SPgno pgno);
int pgFileRelease(SPage *pPage);
int pgFileWrite(SPage *pPage);
int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno);
int pgFileAllocatePage(SPgFile *pPgFile, SPgno *pPgno);
#ifdef __cplusplus
}
......
......@@ -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);
int tdbGetFileSize(const char *fname, pgsz_t pgSize, pgno_t *pSize);
int tdbGetFileSize(const char *fname, pgsz_t pgSize, SPgno *pSize);
#ifdef __cplusplus
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册