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