This commit is contained in:
Hongze Cheng 2022-03-21 06:34:34 +00:00
parent a84155b951
commit 0a6be10ff1
3 changed files with 27 additions and 27 deletions

View File

@ -40,7 +40,9 @@ struct SBTree {
}; };
#define TDB_BTREE_PAGE_COMMON_HDR u8 flags; #define TDB_BTREE_PAGE_COMMON_HDR u8 flags;
#define TDB_BTREE_PAGE_FLAGS(PAGE) (PAGE)->pAmHdr[0]
#define TDB_BTREE_PAGE_GET_FLAGS(PAGE) (PAGE)->pAmHdr[0]
#define TDB_BTREE_PAGE_SET_FLAGS(PAGE, flags) ((PAGE)->pAmHdr[0] = (flags))
typedef struct __attribute__((__packed__)) { typedef struct __attribute__((__packed__)) {
TDB_BTREE_PAGE_COMMON_HDR TDB_BTREE_PAGE_COMMON_HDR
@ -274,7 +276,7 @@ static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen, int *p
} }
// Move downward or break // Move downward or break
u16 flags = 0; // TODO: TDB_PAGE_FLAGS(pPage); u8 flags = TDB_BTREE_PAGE_GET_FLAGS(pPage);
u8 leaf = TDB_BTREE_PAGE_IS_LEAF(flags); u8 leaf = TDB_BTREE_PAGE_IS_LEAF(flags);
if (leaf) { if (leaf) {
pCur->idx = midx; pCur->idx = midx;
@ -388,25 +390,23 @@ static int tdbBtreeOpenImpl(SBTree *pBt) {
static int tdbBtreeInitPage(SPage *pPage, void *arg) { static int tdbBtreeInitPage(SPage *pPage, void *arg) {
SBTree *pBt; SBTree *pBt;
u16 flags; u8 flags;
u8 isLeaf; u8 isLeaf;
u8 szAmHdr; u8 szAmHdr;
pBt = (SBTree *)arg; pBt = ((SBtreeInitPageArg *)arg)->pBt;
flags = ((SBtreeInitPageArg *)arg)->flags;
ASSERT(0);
// TODO: here has problem
flags = 0; // TODO: TDB_PAGE_FLAGS(pPage);
isLeaf = TDB_BTREE_PAGE_IS_LEAF(flags); isLeaf = TDB_BTREE_PAGE_IS_LEAF(flags);
ASSERT(flags == TDB_BTREE_PAGE_GET_FLAGS(pPage));
if (isLeaf) { if (isLeaf) {
szAmHdr = sizeof(SLeafHdr); szAmHdr = sizeof(SLeafHdr);
} else { } else {
szAmHdr = sizeof(SIntHdr); szAmHdr = sizeof(SIntHdr);
} }
pPage->xCellSize = NULL; // TODO
tdbPageInit(pPage, szAmHdr); tdbPageInit(pPage, szAmHdr, tdbBtreeCellSize);
TDB_BTREE_ASSERT_FLAG(flags); TDB_BTREE_ASSERT_FLAG(flags);
@ -426,7 +426,7 @@ static int tdbBtreeInitPage(SPage *pPage, void *arg) {
} }
static int tdbBtreeZeroPage(SPage *pPage, void *arg) { static int tdbBtreeZeroPage(SPage *pPage, void *arg) {
u16 flags; u8 flags;
SBTree *pBt; SBTree *pBt;
u8 isLeaf; u8 isLeaf;
u8 szAmHdr; u8 szAmHdr;
@ -440,10 +440,8 @@ static int tdbBtreeZeroPage(SPage *pPage, void *arg) {
} else { } else {
szAmHdr = sizeof(SIntHdr); szAmHdr = sizeof(SIntHdr);
} }
pPage->xCellSize = NULL; // TODO
tdbPageZero(pPage, szAmHdr); tdbPageZero(pPage, szAmHdr, tdbBtreeCellSize);
// TDB_PAGE_FLAGS_SET(pPage, flags);
if (isLeaf) { if (isLeaf) {
pPage->kLen = pBt->keyLen; pPage->kLen = pBt->keyLen;
@ -749,7 +747,7 @@ static int tdbBtreeBalance(SBtCursor *pCur) {
SPage *pParent; SPage *pParent;
SPage *pPage; SPage *pPage;
int ret; int ret;
u16 flags; u8 flags;
u8 leaf; u8 leaf;
u8 root; u8 root;
@ -757,7 +755,7 @@ static int tdbBtreeBalance(SBtCursor *pCur) {
for (;;) { for (;;) {
iPage = pCur->iPage; iPage = pCur->iPage;
pPage = pCur->pPage; pPage = pCur->pPage;
flags = 0; // TODO: TDB_PAGE_FLAGS(pPage); flags = TDB_BTREE_PAGE_GET_FLAGS(pPage);
leaf = TDB_BTREE_PAGE_IS_LEAF(flags); leaf = TDB_BTREE_PAGE_IS_LEAF(flags);
root = TDB_BTREE_PAGE_IS_ROOT(flags); root = TDB_BTREE_PAGE_IS_ROOT(flags);
@ -840,7 +838,7 @@ static int tdbBtreeEncodePayload(SPage *pPage, u8 *pPayload, const void *pKey, i
static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const void *pVal, int vLen, SCell *pCell, static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const void *pVal, int vLen, SCell *pCell,
int *szCell) { int *szCell) {
u16 flags; u8 flags;
u8 leaf; u8 leaf;
int nHeader; int nHeader;
int nPayload; int nPayload;
@ -851,7 +849,7 @@ static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const vo
nPayload = 0; nPayload = 0;
nHeader = 0; nHeader = 0;
flags = 0; // TODO: TDB_PAGE_FLAGS(pPage); flags = TDB_BTREE_PAGE_GET_FLAGS(pPage);
leaf = TDB_BTREE_PAGE_IS_LEAF(flags); leaf = TDB_BTREE_PAGE_IS_LEAF(flags);
// 1. Encode Header part // 1. Encode Header part
@ -914,13 +912,13 @@ static int tdbBtreeDecodePayload(SPage *pPage, const u8 *pPayload, SCellDecoder
} }
static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pDecoder) { static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pDecoder) {
u16 flags; u8 flags;
u8 leaf; u8 leaf;
int nHeader; int nHeader;
int ret; int ret;
nHeader = 0; nHeader = 0;
flags = 0; // TODO: TDB_PAGE_FLAGS(pPage); flags = TDB_BTREE_PAGE_GET_FLAGS(pPage);
leaf = TDB_BTREE_PAGE_IS_LEAF(flags); leaf = TDB_BTREE_PAGE_IS_LEAF(flags);
// Clear the state of decoder // Clear the state of decoder

View File

@ -103,8 +103,8 @@ struct SPage {
int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg); int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg);
int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg); int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg);
void tdbPageZero(SPage *pPage, u8 szAmHdr); void tdbPageZero(SPage *pPage, u8 szAmHdr, int (*xCellSize)(const SPage *, SCell *));
void tdbPageInit(SPage *pPage, u8 szAmHdr); void tdbPageInit(SPage *pPage, u8 szAmHdr, int (*xCellSize)(const SPage *, SCell *));
int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell); int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell);
int tdbPageDropCell(SPage *pPage, int idx); int tdbPageDropCell(SPage *pPage, int idx);

View File

@ -79,7 +79,7 @@ int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg)
return 0; return 0;
} }
void tdbPageZero(SPage *pPage, u8 szAmHdr) { void tdbPageZero(SPage *pPage, u8 szAmHdr, int (*xCellSize)(const SPage *, SCell *)) {
pPage->pAmHdr = pPage->pData; pPage->pAmHdr = pPage->pData;
pPage->pPageHdr = pPage->pAmHdr + szAmHdr; pPage->pPageHdr = pPage->pAmHdr + szAmHdr;
TDB_PAGE_NCELLS_SET(pPage, 0); TDB_PAGE_NCELLS_SET(pPage, 0);
@ -91,11 +91,12 @@ void tdbPageZero(SPage *pPage, u8 szAmHdr) {
pPage->pFreeEnd = pPage->pData + TDB_PAGE_CCELLS(pPage); pPage->pFreeEnd = pPage->pData + TDB_PAGE_CCELLS(pPage);
pPage->pPageFtr = (SPageFtr *)(pPage->pData + pPage->pageSize - sizeof(SPageFtr)); pPage->pPageFtr = (SPageFtr *)(pPage->pData + pPage->pageSize - sizeof(SPageFtr));
pPage->nOverflow = 0; pPage->nOverflow = 0;
pPage->xCellSize = xCellSize;
ASSERT((u8 *)pPage->pPageFtr == pPage->pFreeEnd); ASSERT((u8 *)pPage->pPageFtr == pPage->pFreeEnd);
} }
void tdbPageInit(SPage *pPage, u8 szAmHdr) { void tdbPageInit(SPage *pPage, u8 szAmHdr, int (*xCellSize)(const SPage *, SCell *)) {
pPage->pAmHdr = pPage->pData; pPage->pAmHdr = pPage->pData;
pPage->pPageHdr = pPage->pAmHdr + szAmHdr; pPage->pPageHdr = pPage->pAmHdr + szAmHdr;
pPage->pCellIdx = pPage->pPageHdr + TDB_PAGE_HDR_SIZE(pPage); pPage->pCellIdx = pPage->pPageHdr + TDB_PAGE_HDR_SIZE(pPage);
@ -103,6 +104,7 @@ void tdbPageInit(SPage *pPage, u8 szAmHdr) {
pPage->pFreeEnd = pPage->pData + TDB_PAGE_CCELLS(pPage); pPage->pFreeEnd = pPage->pData + TDB_PAGE_CCELLS(pPage);
pPage->pPageFtr = (SPageFtr *)(pPage->pData + pPage->pageSize - sizeof(SPageFtr)); pPage->pPageFtr = (SPageFtr *)(pPage->pData + pPage->pageSize - sizeof(SPageFtr));
pPage->nOverflow = 0; pPage->nOverflow = 0;
pPage->xCellSize = xCellSize;
ASSERT(pPage->pFreeEnd >= pPage->pFreeStart); ASSERT(pPage->pFreeEnd >= pPage->pFreeStart);
ASSERT(pPage->pFreeEnd - pPage->pFreeStart <= TDB_PAGE_NFREE(pPage)); ASSERT(pPage->pFreeEnd - pPage->pFreeStart <= TDB_PAGE_NFREE(pPage));