more TDB
This commit is contained in:
parent
f75cb89866
commit
af98aa4005
|
@ -78,6 +78,8 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD
|
||||||
static int tdbBtreeBalance(SBTC *pCur);
|
static int tdbBtreeBalance(SBTC *pCur);
|
||||||
static int tdbBtreeCellSize(const SPage *pPage, SCell *pCell);
|
static int tdbBtreeCellSize(const SPage *pPage, SCell *pCell);
|
||||||
static int tdbBtcMoveToNext(SBTC *pBtc);
|
static int tdbBtcMoveToNext(SBTC *pBtc);
|
||||||
|
static int tdbBtcMoveDownward(SBTC *pCur, SPgno pgno);
|
||||||
|
static int tdbBtcMoveUpward(SBTC *pBtc);
|
||||||
|
|
||||||
int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, SBTree **ppBt) {
|
int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, FKeyComparator kcmpr, SBTree **ppBt) {
|
||||||
SBTree *pBt;
|
SBTree *pBt;
|
||||||
|
@ -224,23 +226,6 @@ int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tdbBtCursorMoveToChild(SBTC *pCur, SPgno pgno) {
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
pCur->pgStack[pCur->iPage] = pCur->pPage;
|
|
||||||
pCur->idxStack[pCur->iPage] = pCur->idx;
|
|
||||||
pCur->iPage++;
|
|
||||||
pCur->pPage = NULL;
|
|
||||||
pCur->idx = -1;
|
|
||||||
|
|
||||||
ret = tdbPagerFetchPage(pCur->pBt->pPager, pgno, &pCur->pPage, tdbBtreeInitPage, pCur->pBt);
|
|
||||||
if (ret < 0) {
|
|
||||||
ASSERT(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int tdbBtCursorMoveTo(SBTC *pCur, const void *pKey, int kLen, int *pCRst) {
|
static int tdbBtCursorMoveTo(SBTC *pCur, const void *pKey, int kLen, int *pCRst) {
|
||||||
int ret;
|
int ret;
|
||||||
SBTree *pBt;
|
SBTree *pBt;
|
||||||
|
@ -318,16 +303,16 @@ static int tdbBtCursorMoveTo(SBTC *pCur, const void *pKey, int kLen, int *pCRst)
|
||||||
} else {
|
} else {
|
||||||
if (c <= 0) {
|
if (c <= 0) {
|
||||||
pCur->idx = midx;
|
pCur->idx = midx;
|
||||||
tdbBtCursorMoveToChild(pCur, cd.pgno);
|
tdbBtcMoveDownward(pCur, cd.pgno);
|
||||||
} else {
|
} else {
|
||||||
pCur->idx = midx + 1;
|
pCur->idx = midx + 1;
|
||||||
if (midx == nCells - 1) {
|
if (midx == nCells - 1) {
|
||||||
/* Move to right-most child */
|
/* Move to right-most child */
|
||||||
tdbBtCursorMoveToChild(pCur, ((SIntHdr *)pCur->pPage->pData)->pgno);
|
tdbBtcMoveDownward(pCur, ((SIntHdr *)pCur->pPage->pData)->pgno);
|
||||||
} else {
|
} else {
|
||||||
pCell = tdbPageGetCell(pPage, pCur->idx);
|
pCell = tdbPageGetCell(pPage, pCur->idx);
|
||||||
tdbBtreeDecodeCell(pPage, pCell, &cd);
|
tdbBtreeDecodeCell(pPage, pCell, &cd);
|
||||||
tdbBtCursorMoveToChild(pCur, cd.pgno);
|
tdbBtcMoveDownward(pCur, cd.pgno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1109,7 +1094,7 @@ int tdbBtcMoveToFirst(SBTC *pBtc) {
|
||||||
pCell = tdbPageGetCell(pBtc->pPage, 0);
|
pCell = tdbPageGetCell(pBtc->pPage, 0);
|
||||||
pgno = *(SPgno *)pCell;
|
pgno = *(SPgno *)pCell;
|
||||||
|
|
||||||
ret = tdbBtCursorMoveToChild(pBtc, pgno);
|
ret = tdbBtcMoveDownward(pBtc, pgno);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
ASSERT(0);
|
ASSERT(0);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -1158,7 +1143,7 @@ int tdbBtcMoveToLast(SBTC *pBtc) {
|
||||||
pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
|
pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
|
||||||
pgno = ((SIntHdr *)pBtc->pPage->pData)->pgno;
|
pgno = ((SIntHdr *)pBtc->pPage->pData)->pgno;
|
||||||
|
|
||||||
ret = tdbBtCursorMoveToChild(pBtc, pgno);
|
ret = tdbBtcMoveDownward(pBtc, pgno);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
ASSERT(0);
|
ASSERT(0);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -1243,3 +1228,25 @@ int tdbBtcClose(SBTC *pBtc) {
|
||||||
// TODO
|
// TODO
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int tdbBtcMoveDownward(SBTC *pCur, SPgno pgno) {
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
pCur->pgStack[pCur->iPage] = pCur->pPage;
|
||||||
|
pCur->idxStack[pCur->iPage] = pCur->idx;
|
||||||
|
pCur->iPage++;
|
||||||
|
pCur->pPage = NULL;
|
||||||
|
pCur->idx = -1;
|
||||||
|
|
||||||
|
ret = tdbPagerFetchPage(pCur->pBt->pPager, pgno, &pCur->pPage, tdbBtreeInitPage, pCur->pBt);
|
||||||
|
if (ret < 0) {
|
||||||
|
ASSERT(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int tdbBtcMoveUpward(SBTC *pBtc) {
|
||||||
|
// TODO
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -53,10 +53,10 @@ 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 SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage);
|
static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage);
|
||||||
static void tdbPCachePinPage(SPage *pPage);
|
static void tdbPCachePinPage(SPCache *pCache, SPage *pPage);
|
||||||
static void tdbPCacheRemovePageFromHash(SPage *pPage);
|
static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage);
|
||||||
static void tdbPCacheAddPageToHash(SPage *pPage);
|
static void tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage);
|
||||||
static void tdbPCacheUnpinPage(SPage *pPage);
|
static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage);
|
||||||
|
|
||||||
int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) {
|
int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) {
|
||||||
SPCache *pCache;
|
SPCache *pCache;
|
||||||
|
@ -100,7 +100,7 @@ SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) {
|
||||||
return pPage;
|
return pPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tdbPCacheRelease(SPage *pPage) {
|
void tdbPCacheRelease(SPCache *pCache, SPage *pPage) {
|
||||||
i32 nRef;
|
i32 nRef;
|
||||||
|
|
||||||
nRef = TDB_UNREF_PAGE(pPage);
|
nRef = TDB_UNREF_PAGE(pPage);
|
||||||
|
@ -108,7 +108,7 @@ void tdbPCacheRelease(SPage *pPage) {
|
||||||
|
|
||||||
if (nRef == 0) {
|
if (nRef == 0) {
|
||||||
if (1 /*TODO: page still clean*/) {
|
if (1 /*TODO: page still clean*/) {
|
||||||
tdbPCacheUnpinPage(pPage);
|
tdbPCacheUnpinPage(pCache, pPage);
|
||||||
} else {
|
} else {
|
||||||
// TODO
|
// TODO
|
||||||
ASSERT(0);
|
ASSERT(0);
|
||||||
|
@ -142,7 +142,7 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNe
|
||||||
|
|
||||||
if (pPage || !alcNewPage) {
|
if (pPage || !alcNewPage) {
|
||||||
if (pPage) {
|
if (pPage) {
|
||||||
tdbPCachePinPage(pPage);
|
tdbPCachePinPage(pCache, pPage);
|
||||||
}
|
}
|
||||||
return pPage;
|
return pPage;
|
||||||
}
|
}
|
||||||
|
@ -158,8 +158,8 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNe
|
||||||
// 3. Try to Recycle a page
|
// 3. Try to Recycle a page
|
||||||
if (!pPage && !pCache->lru.pLruPrev->isAnchor) {
|
if (!pPage && !pCache->lru.pLruPrev->isAnchor) {
|
||||||
pPage = pCache->lru.pLruPrev;
|
pPage = pCache->lru.pLruPrev;
|
||||||
tdbPCacheRemovePageFromHash(pPage);
|
tdbPCacheRemovePageFromHash(pCache, pPage);
|
||||||
tdbPCachePinPage(pPage);
|
tdbPCachePinPage(pCache, pPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Try a stress allocation (TODO)
|
// 4. Try a stress allocation (TODO)
|
||||||
|
@ -171,16 +171,13 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNe
|
||||||
memcpy(&(pPage->pgid), pPgid, sizeof(*pPgid));
|
memcpy(&(pPage->pgid), pPgid, sizeof(*pPgid));
|
||||||
pPage->pLruNext = NULL;
|
pPage->pLruNext = NULL;
|
||||||
pPage->pPager = NULL;
|
pPage->pPager = NULL;
|
||||||
tdbPCacheAddPageToHash(pPage);
|
tdbPCacheAddPageToHash(pCache, pPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
return pPage;
|
return pPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tdbPCachePinPage(SPage *pPage) {
|
static void tdbPCachePinPage(SPCache *pCache, SPage *pPage) {
|
||||||
SPCache *pCache;
|
|
||||||
|
|
||||||
pCache = pPage->pCache;
|
|
||||||
if (!PAGE_IS_PINNED(pPage)) {
|
if (!PAGE_IS_PINNED(pPage)) {
|
||||||
pPage->pLruPrev->pLruNext = pPage->pLruNext;
|
pPage->pLruPrev->pLruNext = pPage->pLruNext;
|
||||||
pPage->pLruNext->pLruPrev = pPage->pLruPrev;
|
pPage->pLruNext->pLruPrev = pPage->pLruPrev;
|
||||||
|
@ -190,12 +187,9 @@ static void tdbPCachePinPage(SPage *pPage) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tdbPCacheUnpinPage(SPage *pPage) {
|
static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage) {
|
||||||
SPCache *pCache;
|
|
||||||
i32 nRef;
|
i32 nRef;
|
||||||
|
|
||||||
pCache = pPage->pCache;
|
|
||||||
|
|
||||||
tdbPCacheLock(pCache);
|
tdbPCacheLock(pCache);
|
||||||
|
|
||||||
nRef = TDB_GET_PAGE_REF(pPage);
|
nRef = TDB_GET_PAGE_REF(pPage);
|
||||||
|
@ -215,12 +209,10 @@ static void tdbPCacheUnpinPage(SPage *pPage) {
|
||||||
tdbPCacheUnlock(pCache);
|
tdbPCacheUnlock(pCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tdbPCacheRemovePageFromHash(SPage *pPage) {
|
static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage) {
|
||||||
SPCache *pCache;
|
|
||||||
SPage **ppPage;
|
SPage **ppPage;
|
||||||
int h;
|
int h;
|
||||||
|
|
||||||
pCache = pPage->pCache;
|
|
||||||
h = PCACHE_PAGE_HASH(&(pPage->pgid));
|
h = PCACHE_PAGE_HASH(&(pPage->pgid));
|
||||||
for (ppPage = &(pCache->pgHash[h % pCache->nHash]); *ppPage != pPage; ppPage = &((*ppPage)->pHashNext))
|
for (ppPage = &(pCache->pgHash[h % pCache->nHash]); *ppPage != pPage; ppPage = &((*ppPage)->pHashNext))
|
||||||
;
|
;
|
||||||
|
@ -230,11 +222,9 @@ static void tdbPCacheRemovePageFromHash(SPage *pPage) {
|
||||||
pCache->nPage--;
|
pCache->nPage--;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tdbPCacheAddPageToHash(SPage *pPage) {
|
static void tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage) {
|
||||||
SPCache *pCache;
|
|
||||||
int h;
|
int h;
|
||||||
|
|
||||||
pCache = pPage->pCache;
|
|
||||||
h = PCACHE_PAGE_HASH(&(pPage->pgid)) % pCache->nHash;
|
h = PCACHE_PAGE_HASH(&(pPage->pgid)) % pCache->nHash;
|
||||||
|
|
||||||
pPage->pHashNext = pCache->pgHash[h];
|
pPage->pHashNext = pCache->pgHash[h];
|
||||||
|
@ -264,7 +254,6 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
|
||||||
// pPage->pgid = 0;
|
// pPage->pgid = 0;
|
||||||
pPage->isAnchor = 0;
|
pPage->isAnchor = 0;
|
||||||
pPage->isLocalPage = 1;
|
pPage->isLocalPage = 1;
|
||||||
pPage->pCache = pCache;
|
|
||||||
TDB_INIT_PAGE_REF(pPage);
|
TDB_INIT_PAGE_REF(pPage);
|
||||||
pPage->pHashNext = NULL;
|
pPage->pHashNext = NULL;
|
||||||
pPage->pLruNext = NULL;
|
pPage->pLruNext = NULL;
|
||||||
|
|
|
@ -255,6 +255,10 @@ int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tdbPagerReturnPage(SPager *pPager, SPage *pPage) {
|
||||||
|
tdbPCacheRelease(pPager->pCache, pPage);
|
||||||
|
}
|
||||||
|
|
||||||
static int tdbPagerAllocFreePage(SPager *pPager, SPgno *ppgno) {
|
static int tdbPagerAllocFreePage(SPager *pPager, SPgno *ppgno) {
|
||||||
// TODO: Allocate a page from the free list
|
// TODO: Allocate a page from the free list
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -25,7 +25,6 @@ extern "C" {
|
||||||
u8 isLocalPage; \
|
u8 isLocalPage; \
|
||||||
u8 isDirty; \
|
u8 isDirty; \
|
||||||
i32 nRef; \
|
i32 nRef; \
|
||||||
SPCache *pCache; \
|
|
||||||
SPage *pFreeNext; \
|
SPage *pFreeNext; \
|
||||||
SPage *pHashNext; \
|
SPage *pHashNext; \
|
||||||
SPage *pLruNext; \
|
SPage *pLruNext; \
|
||||||
|
@ -37,7 +36,7 @@ extern "C" {
|
||||||
int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache);
|
int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache);
|
||||||
int tdbPCacheClose(SPCache *pCache);
|
int tdbPCacheClose(SPCache *pCache);
|
||||||
SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage);
|
SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage);
|
||||||
void tdbPCacheRelease(SPage *pPage);
|
void tdbPCacheRelease(SPCache *pCache, SPage *pPage);
|
||||||
int tdbPCacheGetPageSize(SPCache *pCache);
|
int tdbPCacheGetPageSize(SPCache *pCache);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -29,6 +29,7 @@ int tdbPagerCommit(SPager *pPager);
|
||||||
int tdbPagerGetPageSize(SPager *pPager);
|
int tdbPagerGetPageSize(SPager *pPager);
|
||||||
int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg);
|
int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg);
|
||||||
int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg);
|
int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg);
|
||||||
|
void tdbPagerReturnPage(SPager *pPager, SPage *pPage);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue