refact btree encode and decode

This commit is contained in:
Hongze Cheng 2022-03-30 06:36:55 +00:00
parent 247ff626a8
commit 60844ee6c1
1 changed files with 16 additions and 14 deletions

View File

@ -62,7 +62,7 @@ typedef struct {
int vLen; int vLen;
u8 *pVal; u8 *pVal;
SPgno pgno; SPgno pgno;
u8 *pTmpSpace; u8 *pBuf;
} SCellDecoder; } SCellDecoder;
static int tdbBtCursorMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst); static int tdbBtCursorMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst);
@ -965,24 +965,26 @@ static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const vo
return 0; return 0;
} }
static int tdbBtreeDecodePayload(SPage *pPage, const u8 *pPayload, SCellDecoder *pDecoder) { static int tdbBtreeDecodePayload(SPage *pPage, const SCell *pCell, int nHeader, SCellDecoder *pDecoder) {
int nPayload; int nPayload;
ASSERT(pDecoder->pKey == NULL);
if (pDecoder->pVal) { if (pDecoder->pVal) {
nPayload = pDecoder->kLen + pDecoder->vLen; ASSERT(!TDB_BTREE_PAGE_IS_LEAF(pPage));
} else {
nPayload = pDecoder->kLen; nPayload = pDecoder->kLen;
} else {
nPayload = pDecoder->kLen + pDecoder->vLen;
} }
if (nPayload <= pPage->maxLocal) { if (nHeader + nPayload <= pPage->maxLocal) {
// General case without overflow // no over flow case
pDecoder->pKey = (void *)pPayload; pDecoder->pKey = pCell + nHeader;
if (!pDecoder->pVal) { if (pDecoder->pVal == NULL && pDecoder->vLen > 0) {
pDecoder->pVal = (void *)(pPayload + pDecoder->kLen); pDecoder->pVal = pCell + nHeader + pDecoder->kLen;
} }
} else { return 0;
}
{
// TODO: handle overflow case // TODO: handle overflow case
ASSERT(0); ASSERT(0);
} }
@ -990,7 +992,6 @@ static int tdbBtreeDecodePayload(SPage *pPage, const u8 *pPayload, SCellDecoder
return 0; return 0;
} }
// TODO: here has problem
static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pDecoder) { static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pDecoder) {
u8 leaf; u8 leaf;
int nHeader; int nHeader;
@ -1022,13 +1023,14 @@ static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pD
} }
if (pPage->vLen == TDB_VARIANT_LEN) { if (pPage->vLen == TDB_VARIANT_LEN) {
ASSERT(leaf);
nHeader += tdbGetVarInt(pCell + nHeader, &(pDecoder->vLen)); nHeader += tdbGetVarInt(pCell + nHeader, &(pDecoder->vLen));
} else { } else {
pDecoder->vLen = pPage->vLen; pDecoder->vLen = pPage->vLen;
} }
// 2. Decode payload part // 2. Decode payload part
ret = tdbBtreeDecodePayload(pPage, pCell + nHeader, pDecoder); ret = tdbBtreeDecodePayload(pPage, pCell, nHeader, pDecoder);
if (ret < 0) { if (ret < 0) {
return -1; return -1;
} }