refact
This commit is contained in:
parent
2d304989aa
commit
cad1902337
|
@ -28,15 +28,15 @@ struct SBTree {
|
||||||
struct SBtCursor {
|
struct SBtCursor {
|
||||||
SBTree * pBt;
|
SBTree * pBt;
|
||||||
i8 iPage;
|
i8 iPage;
|
||||||
SMemPage *pPage;
|
// SMemPage *pPage;
|
||||||
SMemPage *apPage[BTREE_MAX_DEPTH + 1];
|
// SMemPage *apPage[BTREE_MAX_DEPTH + 1];
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct SMemPage {
|
// typedef struct SMemPage {
|
||||||
u8 isInit;
|
// u8 isInit;
|
||||||
u8 isLeaf;
|
// u8 isLeaf;
|
||||||
SPgno pgno;
|
// SPgno pgno;
|
||||||
} SMemPage;
|
// } SMemPage;
|
||||||
|
|
||||||
int tdbBtreeOpen(SPgno root, SBTree **ppBt) {
|
int tdbBtreeOpen(SPgno root, SBTree **ppBt) {
|
||||||
*ppBt = NULL;
|
*ppBt = NULL;
|
||||||
|
@ -63,7 +63,7 @@ int tdbBtreeCursorMoveTo(SBtCursor *pCur) {
|
||||||
|
|
||||||
static int tdbBtreeCursorMoveToRoot(SBtCursor *pCur) {
|
static int tdbBtreeCursorMoveToRoot(SBtCursor *pCur) {
|
||||||
SPFile *pFile;
|
SPFile *pFile;
|
||||||
SPgHdr *pPage;
|
SPage * pPage;
|
||||||
|
|
||||||
pFile = pCur->pBt->pFile;
|
pFile = pCur->pBt->pFile;
|
||||||
|
|
||||||
|
|
|
@ -20,15 +20,15 @@ struct SPCache {
|
||||||
int extraSize;
|
int extraSize;
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
int nFree;
|
int nFree;
|
||||||
SPgHdr * pFree;
|
SPage * pFree;
|
||||||
int nPage;
|
int nPage;
|
||||||
int nHash;
|
int nHash;
|
||||||
SPgHdr ** pgHash;
|
SPage ** pgHash;
|
||||||
int nRecyclable;
|
int nRecyclable;
|
||||||
SPgHdr lru;
|
SPage lru;
|
||||||
int nDirty;
|
int nDirty;
|
||||||
SPgHdr * pDirty;
|
SPage * pDirty;
|
||||||
SPgHdr * pDirtyTail;
|
SPage * pDirtyTail;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define PCACHE_PAGE_HASH(pPgid) \
|
#define PCACHE_PAGE_HASH(pPgid) \
|
||||||
|
@ -38,21 +38,21 @@ struct SPCache {
|
||||||
})
|
})
|
||||||
#define PAGE_IS_PINNED(pPage) ((pPage)->pLruNext == NULL)
|
#define PAGE_IS_PINNED(pPage) ((pPage)->pLruNext == NULL)
|
||||||
|
|
||||||
static int tdbPCacheOpenImpl(SPCache *pCache);
|
static int tdbPCacheOpenImpl(SPCache *pCache);
|
||||||
static void tdbPCacheInitLock(SPCache *pCache);
|
static void tdbPCacheInitLock(SPCache *pCache);
|
||||||
static void tdbPCacheClearLock(SPCache *pCache);
|
static void tdbPCacheClearLock(SPCache *pCache);
|
||||||
static void tdbPCacheLock(SPCache *pCache);
|
static void tdbPCacheLock(SPCache *pCache);
|
||||||
static void tdbPCacheUnlock(SPCache *pCache);
|
static void tdbPCacheUnlock(SPCache *pCache);
|
||||||
static bool tdbPCacheLocked(SPCache *pCache);
|
static bool tdbPCacheLocked(SPCache *pCache);
|
||||||
static SPgHdr *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage);
|
static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage);
|
||||||
static void tdbPCachePinPage(SPgHdr *pPage);
|
static void tdbPCachePinPage(SPage *pPage);
|
||||||
static void tdbPCacheRemovePageFromHash(SPgHdr *pPage);
|
static void tdbPCacheRemovePageFromHash(SPage *pPage);
|
||||||
static void tdbPCacheAddPageToHash(SPgHdr *pPage);
|
static void tdbPCacheAddPageToHash(SPage *pPage);
|
||||||
|
|
||||||
int tdbPCacheOpen(int pageSize, int cacheSize, int extraSize, SPCache **ppCache) {
|
int tdbPCacheOpen(int pageSize, int cacheSize, int extraSize, SPCache **ppCache) {
|
||||||
SPCache *pCache;
|
SPCache *pCache;
|
||||||
void * pPtr;
|
void * pPtr;
|
||||||
SPgHdr * pPgHdr;
|
SPage * pPgHdr;
|
||||||
|
|
||||||
pCache = (SPCache *)calloc(1, sizeof(*pCache));
|
pCache = (SPCache *)calloc(1, sizeof(*pCache));
|
||||||
if (pCache == NULL) {
|
if (pCache == NULL) {
|
||||||
|
@ -76,8 +76,8 @@ int tdbPCacheClose(SPCache *pCache) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
SPgHdr *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
|
SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
|
||||||
SPgHdr *pPage;
|
SPage *pPage;
|
||||||
|
|
||||||
tdbPCacheLock(pCache);
|
tdbPCacheLock(pCache);
|
||||||
pPage = tdbPCacheFetchImpl(pCache, pPgid, alcNewPage);
|
pPage = tdbPCacheFetchImpl(pCache, pPgid, alcNewPage);
|
||||||
|
@ -86,12 +86,12 @@ SPgHdr *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
|
||||||
return pPage;
|
return pPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tdbPCacheFetchFinish(SPCache *pCache, SPgHdr *pPage) {
|
void tdbPCacheFetchFinish(SPCache *pCache, SPage *pPage) {
|
||||||
/* TODO */
|
/* TODO */
|
||||||
pPage->nRef++; // TODO: do we need atomic operation???
|
pPage->nRef++; // TODO: do we need atomic operation???
|
||||||
}
|
}
|
||||||
|
|
||||||
void tdbPCacheRelease(SPgHdr *pHdr) {
|
void tdbPCacheRelease(SPage *pHdr) {
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,8 +109,8 @@ static bool tdbPCacheLocked(SPCache *pCache) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static SPgHdr *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
|
static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
|
||||||
SPgHdr *pPage;
|
SPage *pPage;
|
||||||
|
|
||||||
// 1. Search the hash table
|
// 1. Search the hash table
|
||||||
pPage = pCache->pgHash[PCACHE_PAGE_HASH(pPgid) % pCache->nHash];
|
pPage = pCache->pgHash[PCACHE_PAGE_HASH(pPgid) % pCache->nHash];
|
||||||
|
@ -157,7 +157,7 @@ static SPgHdr *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcN
|
||||||
return pPage;
|
return pPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tdbPCachePinPage(SPgHdr *pPage) {
|
static void tdbPCachePinPage(SPage *pPage) {
|
||||||
SPCache *pCache;
|
SPCache *pCache;
|
||||||
|
|
||||||
pCache = pPage->pCache;
|
pCache = pPage->pCache;
|
||||||
|
@ -170,9 +170,9 @@ static void tdbPCachePinPage(SPgHdr *pPage) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tdbPCacheRemovePageFromHash(SPgHdr *pPage) {
|
static void tdbPCacheRemovePageFromHash(SPage *pPage) {
|
||||||
SPCache *pCache;
|
SPCache *pCache;
|
||||||
SPgHdr **ppPage;
|
SPage ** ppPage;
|
||||||
int h;
|
int h;
|
||||||
|
|
||||||
pCache = pPage->pCache;
|
pCache = pPage->pCache;
|
||||||
|
@ -185,7 +185,7 @@ static void tdbPCacheRemovePageFromHash(SPgHdr *pPage) {
|
||||||
pCache->nPage--;
|
pCache->nPage--;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tdbPCacheAddPageToHash(SPgHdr *pPage) {
|
static void tdbPCacheAddPageToHash(SPage *pPage) {
|
||||||
SPCache *pCache;
|
SPCache *pCache;
|
||||||
int h;
|
int h;
|
||||||
|
|
||||||
|
@ -199,9 +199,9 @@ static void tdbPCacheAddPageToHash(SPgHdr *pPage) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tdbPCacheOpenImpl(SPCache *pCache) {
|
static int tdbPCacheOpenImpl(SPCache *pCache) {
|
||||||
SPgHdr *pPage;
|
SPage *pPage;
|
||||||
u8 * pPtr;
|
u8 * pPtr;
|
||||||
int tsize;
|
int tsize;
|
||||||
|
|
||||||
tdbPCacheInitLock(pCache);
|
tdbPCacheInitLock(pCache);
|
||||||
|
|
||||||
|
@ -209,14 +209,14 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
|
||||||
pCache->nFree = 0;
|
pCache->nFree = 0;
|
||||||
pCache->pFree = NULL;
|
pCache->pFree = NULL;
|
||||||
for (int i = 0; i < pCache->cacheSize; i++) {
|
for (int i = 0; i < pCache->cacheSize; i++) {
|
||||||
tsize = pCache->pageSize + sizeof(SPgHdr) + pCache->extraSize;
|
tsize = pCache->pageSize + sizeof(SPage) + pCache->extraSize;
|
||||||
pPtr = (u8 *)calloc(1, tsize);
|
pPtr = (u8 *)calloc(1, tsize);
|
||||||
if (pPtr == NULL) {
|
if (pPtr == NULL) {
|
||||||
// TODO
|
// TODO
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
pPage = (SPgHdr *)(&(pPtr[pCache->pageSize]));
|
pPage = (SPage *)(&(pPtr[pCache->pageSize]));
|
||||||
pPage->pData = (void *)pPtr;
|
pPage->pData = (void *)pPtr;
|
||||||
pPage->pExtra = (void *)(&(pPage[1]));
|
pPage->pExtra = (void *)(&(pPage[1]));
|
||||||
// pPage->pgid = 0;
|
// pPage->pgid = 0;
|
||||||
|
@ -235,7 +235,7 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
|
||||||
// Open the hash table
|
// Open the hash table
|
||||||
pCache->nPage = 0;
|
pCache->nPage = 0;
|
||||||
pCache->nHash = pCache->cacheSize;
|
pCache->nHash = pCache->cacheSize;
|
||||||
pCache->pgHash = (SPgHdr **)calloc(pCache->nHash, sizeof(SPgHdr *));
|
pCache->pgHash = (SPage **)calloc(pCache->nHash, sizeof(SPage *));
|
||||||
if (pCache->pgHash == NULL) {
|
if (pCache->pgHash == NULL) {
|
||||||
// TODO
|
// TODO
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
@ -27,7 +27,7 @@ struct SPFile {
|
||||||
SPgno dbOrigSize;
|
SPgno dbOrigSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int tdbPFileReadPage(SPFile *pFile, SPgHdr *pPage);
|
static int tdbPFileReadPage(SPFile *pFile, SPage *pPage);
|
||||||
|
|
||||||
int tdbPFileOpen(SPCache *pCache, const char *fileName, SPFile **ppFile) {
|
int tdbPFileOpen(SPCache *pCache, const char *fileName, SPFile **ppFile) {
|
||||||
uint8_t *pPtr;
|
uint8_t *pPtr;
|
||||||
|
@ -73,9 +73,9 @@ int tdbPFileClose(SPFile *pFile) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
SPgHdr *tdbPFileGet(SPFile *pFile, SPgno pgno) {
|
SPage *tdbPFileGet(SPFile *pFile, SPgno pgno) {
|
||||||
SPgid pgid;
|
SPgid pgid;
|
||||||
SPgHdr *pPage;
|
SPage *pPage;
|
||||||
|
|
||||||
memcpy(pgid.fileid, pFile->fid, TDB_FILE_ID_LEN);
|
memcpy(pgid.fileid, pFile->fid, TDB_FILE_ID_LEN);
|
||||||
pgid.pgno = pgno;
|
pgid.pgno = pgno;
|
||||||
|
@ -120,7 +120,7 @@ int tdbPFileRollback(SPFile *pFile) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tdbPFileReadPage(SPFile *pFile, SPgHdr *pPage) {
|
static int tdbPFileReadPage(SPFile *pFile, SPage *pPage) {
|
||||||
i64 offset;
|
i64 offset;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
|
|
@ -21,9 +21,9 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct SPCache SPCache;
|
typedef struct SPCache SPCache;
|
||||||
typedef struct SPgHdr SPgHdr;
|
typedef struct SPage SPage;
|
||||||
|
|
||||||
struct SPgHdr {
|
struct SPage {
|
||||||
void * pData;
|
void * pData;
|
||||||
void * pExtra;
|
void * pExtra;
|
||||||
SPgid pgid;
|
SPgid pgid;
|
||||||
|
@ -32,17 +32,17 @@ struct SPgHdr {
|
||||||
u8 isLoad;
|
u8 isLoad;
|
||||||
i32 nRef;
|
i32 nRef;
|
||||||
SPCache *pCache;
|
SPCache *pCache;
|
||||||
SPgHdr * pFreeNext;
|
SPage * pFreeNext;
|
||||||
SPgHdr * pHashNext;
|
SPage * pHashNext;
|
||||||
SPgHdr * pLruNext;
|
SPage * pLruNext;
|
||||||
SPgHdr * pLruPrev;
|
SPage * pLruPrev;
|
||||||
};
|
};
|
||||||
|
|
||||||
int tdbPCacheOpen(int pageSize, int cacheSize, int extraSize, SPCache **ppCache);
|
int tdbPCacheOpen(int pageSize, int cacheSize, int extraSize, SPCache **ppCache);
|
||||||
int tdbPCacheClose(SPCache *pCache);
|
int tdbPCacheClose(SPCache *pCache);
|
||||||
SPgHdr *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage);
|
SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage);
|
||||||
void tdbPCacheFetchFinish(SPCache *pCache, SPgHdr *pPage);
|
void tdbPCacheFetchFinish(SPCache *pCache, SPage *pPage);
|
||||||
void tdbPCacheRelease(SPgHdr *pHdr);
|
void tdbPCacheRelease(SPage *pHdr);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,12 +22,12 @@ extern "C" {
|
||||||
|
|
||||||
typedef struct SPFile SPFile;
|
typedef struct SPFile SPFile;
|
||||||
|
|
||||||
int tdbPFileOpen(SPCache *pCache, const char *fileName, SPFile **ppFile);
|
int tdbPFileOpen(SPCache *pCache, const char *fileName, SPFile **ppFile);
|
||||||
int tdbPFileClose(SPFile *pFile);
|
int tdbPFileClose(SPFile *pFile);
|
||||||
SPgHdr *tdbPFileGet(SPFile *pFile, SPgno pgno);
|
SPage *tdbPFileGet(SPFile *pFile, SPgno pgno);
|
||||||
int tdbPFileBegin(SPFile *pFile);
|
int tdbPFileBegin(SPFile *pFile);
|
||||||
int tdbPFileCommit(SPFile *pFile);
|
int tdbPFileCommit(SPFile *pFile);
|
||||||
int tdbPFileRollback(SPFile *pFile);
|
int tdbPFileRollback(SPFile *pFile);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue