提交 df12ae0d 编写于 作者: H Hongze Cheng

more TDB

上级 f3c10f88
......@@ -598,10 +598,56 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) {
}
}
}
// TODO: sort the page according to the page number
}
{
// Do the actual redistribute
{ // Do the actual cell distribution
SPage *pTPage[2];
int tPage, tIdx, iOld;
SCell *pCell;
int szCell;
SBtreeInitPageArg iarg = {.flags = TDB_BTREE_PAGE_GET_FLAGS(pOlds[0]), .pBt = pBt};
for (int i = 0; i < 2; i++) {
ret = tdbPageCreate(pOlds[0]->pageSize, &pTPage[i], NULL, NULL);
if (ret < 0) {
ASSERT(0);
}
}
tPage = 0;
tIdx = 0;
iOld = 0;
tdbBtreeZeroPage(pTPage[tPage], &iarg);
tdbPageCopy(pOlds[iOld++], pTPage[tPage]);
for (int iNew = 0; iNew < nNews; iNew++) {
// fill the iNew page
tdbBtreeZeroPage(pNews[iNew], &iarg);
for (int iCell = 0; iCell < infoNews[iNew].cnt; iCell++) {
while (tIdx >= TDB_PAGE_TOTAL_CELLS(pTPage[tPage])) {
tPage = (tPage + 1) % 2;
tIdx = 0;
tdbBtreeZeroPage(pTPage[tPage], &iarg);
tdbPageCopy(pOlds[iOld++], pTPage[tPage]);
}
pCell = tdbPageGetCell(pTPage[tPage], tIdx);
szCell = tdbBtreeCellSize(pTPage[tPage], pCell);
tdbPageInsertCell(pNews[iNew], iCell, pCell, szCell);
tIdx++;
}
}
for (int i = 0; i < 2; i++) {
tdbPageDestroy(pTPage[i], NULL, NULL);
}
}
return 0;
......
......@@ -57,8 +57,6 @@ static void tdbPCachePinPage(SPage *pPage);
static void tdbPCacheRemovePageFromHash(SPage *pPage);
static void tdbPCacheAddPageToHash(SPage *pPage);
static void tdbPCacheUnpinPage(SPage *pPage);
static void *tdbOsMalloc(void *arg, size_t size);
static void tdbOsFree(void *arg, void *ptr);
int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) {
SPCache *pCache;
......@@ -257,7 +255,7 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
pCache->nFree = 0;
pCache->pFree = NULL;
for (int i = 0; i < pCache->cacheSize; i++) {
ret = tdbPageCreate(pCache->pageSize, &pPage, tdbOsMalloc, NULL);
ret = tdbPageCreate(pCache->pageSize, &pPage, NULL, NULL);
if (ret < 0) {
// TODO: handle error
return -1;
......@@ -297,13 +295,3 @@ static int tdbPCacheOpenImpl(SPCache *pCache) {
}
int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->pageSize; }
static void *tdbOsMalloc(void *arg, size_t size) {
void *ptr;
ptr = malloc(size);
return ptr;
}
static void tdbOsFree(void *arg, void *ptr) { free(ptr); }
\ No newline at end of file
......@@ -39,6 +39,16 @@ int tdbGetFileSize(const char *fname, int pgSize, SPgno *pSize);
int tdbPRead(int fd, void *pData, int count, i64 offset);
static inline void *tdbOsMalloc(void *arg, size_t size) {
void *ptr;
ptr = malloc(size);
return ptr;
}
static inline void tdbOsFree(void *arg, void *ptr) { free(ptr); }
static inline int tdbPutVarInt(u8 *p, int v) {
int n = 0;
......
......@@ -48,6 +48,9 @@ int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t)
*ppPage = NULL;
size = pageSize + sizeof(*pPage);
if (xMalloc == NULL) {
xMalloc = tdbOsMalloc;
}
ptr = (u8 *)((*xMalloc)(arg, size));
if (pPage == NULL) {
......@@ -73,6 +76,10 @@ int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t)
int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg) {
u8 *ptr;
if (!xFree) {
xFree = tdbOsFree;
}
ptr = pPage->pData;
(*xFree)(arg, ptr);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册