refact more
This commit is contained in:
parent
d7cf7e791c
commit
b5e9f2aca0
|
@ -29,7 +29,7 @@ struct SBTree {
|
||||||
int minLocal;
|
int minLocal;
|
||||||
int maxLeaf;
|
int maxLeaf;
|
||||||
int minLeaf;
|
int minLeaf;
|
||||||
u8 *pTmp;
|
void *pBuf;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define TDB_BTREE_PAGE_COMMON_HDR u8 flags;
|
#define TDB_BTREE_PAGE_COMMON_HDR u8 flags;
|
||||||
|
@ -127,66 +127,80 @@ int tdbBtreeClose(SBTree *pBt) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tdbBtCursorInsert(SBTC *pBtc, const void *pKey, int kLen, const void *pVal, int vLen) {
|
int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, int vLen) {
|
||||||
int ret;
|
SBTC btc;
|
||||||
int idx;
|
SCell *pCell;
|
||||||
SPager *pPager;
|
void *pBuf;
|
||||||
SCell *pCell;
|
int szCell;
|
||||||
int szCell;
|
int szBuf;
|
||||||
int cret;
|
int ret;
|
||||||
SBTree *pBt;
|
int idx;
|
||||||
|
int c;
|
||||||
|
|
||||||
ret = tdbBtcMoveTo(pBtc, pKey, kLen, &cret);
|
tdbBtcOpen(&btc, pBt);
|
||||||
|
|
||||||
|
// move to the position to insert
|
||||||
|
ret = tdbBtcMoveTo(&btc, pKey, kLen, &c);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
// TODO: handle error
|
tdbBtcClose(&btc);
|
||||||
|
ASSERT(0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pBtc->idx == -1) {
|
if (btc.idx == -1) {
|
||||||
ASSERT(TDB_PAGE_TOTAL_CELLS(pBtc->pPage) == 0);
|
|
||||||
idx = 0;
|
idx = 0;
|
||||||
} else {
|
} else {
|
||||||
if (cret > 0) {
|
if (c > 0) {
|
||||||
idx = pBtc->idx + 1;
|
idx = btc.idx + 1;
|
||||||
} else if (cret < 0) {
|
} else if (c < 0) {
|
||||||
idx = pBtc->idx;
|
idx = btc.idx;
|
||||||
} else {
|
} else {
|
||||||
/* TODO */
|
// TDB does NOT allow same key
|
||||||
|
tdbBtcClose(&btc);
|
||||||
ASSERT(0);
|
ASSERT(0);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: refact code here
|
|
||||||
pBt = pBtc->pBt;
|
|
||||||
if (!pBt->pTmp) {
|
|
||||||
pBt->pTmp = (u8 *)tdbOsMalloc(pBt->pageSize);
|
|
||||||
if (pBt->pTmp == NULL) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pCell = pBt->pTmp;
|
// make sure enough space to hold the space
|
||||||
|
szBuf = kLen + vLen + 14;
|
||||||
|
pBuf = TDB_REALLOC(pBt->pBuf, pBt->pageSize > szBuf ? szBuf : pBt->pageSize);
|
||||||
|
if (pBuf == NULL) {
|
||||||
|
tdbBtcClose(&btc);
|
||||||
|
ASSERT(0);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
pBt->pBuf = pBuf;
|
||||||
|
pCell = (SCell *)pBt->pBuf;
|
||||||
|
|
||||||
// Encode the cell
|
// encode cell
|
||||||
ret = tdbBtreeEncodeCell(pBtc->pPage, pKey, kLen, pVal, vLen, pCell, &szCell);
|
ret = tdbBtreeEncodeCell(btc.pPage, pKey, kLen, pVal, vLen, pCell, &szCell);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
|
tdbBtcClose(&btc);
|
||||||
|
ASSERT(0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert the cell to the index
|
// insert the cell
|
||||||
ret = tdbPageInsertCell(pBtc->pPage, idx, pCell, szCell, 0);
|
ret = tdbPageInsertCell(btc.pPage, idx, pCell, szCell, 0);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
|
tdbBtcClose(&btc);
|
||||||
|
ASSERT(0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If page is overflow, balance the tree
|
// check if need balance
|
||||||
if (pBtc->pPage->nOverflow > 0) {
|
if (btc.pPage->nOverflow > 0) {
|
||||||
ret = tdbBtreeBalance(pBtc);
|
ret = tdbBtreeBalance(&btc);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
|
tdbBtcClose(&btc);
|
||||||
|
ASSERT(0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tdbBtcClose(&btc);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,22 +74,7 @@ int tdbDbDrop(TDB *pDb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int tdbDbInsert(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen) {
|
int tdbDbInsert(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen) {
|
||||||
SBTC btc;
|
return tdbBtreeInsert(pDb->pBt, pKey, keyLen, pVal, valLen);
|
||||||
SBTC *pCur;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
pCur = &btc;
|
|
||||||
ret = tdbBtcOpen(pCur, pDb->pBt);
|
|
||||||
if (ret < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = tdbBtCursorInsert(pCur, pKey, keyLen, pVal, valLen);
|
|
||||||
if (ret < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int tdbDbGet(TDB *pDb, const void *pKey, int kLen, void **ppVal, int *vLen) {
|
int tdbDbGet(TDB *pDb, const void *pKey, int kLen, void **ppVal, int *vLen) {
|
||||||
|
|
|
@ -40,7 +40,7 @@ struct SBTC {
|
||||||
// SBTree
|
// SBTree
|
||||||
int tdbBtreeOpen(int keyLen, int valLen, SPager *pFile, FKeyComparator kcmpr, SBTree **ppBt);
|
int tdbBtreeOpen(int keyLen, int valLen, SPager *pFile, FKeyComparator kcmpr, SBTree **ppBt);
|
||||||
int tdbBtreeClose(SBTree *pBt);
|
int tdbBtreeClose(SBTree *pBt);
|
||||||
int tdbBtCursorInsert(SBTC *pCur, const void *pKey, int kLen, const void *pVal, int vLen);
|
int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, int vLen);
|
||||||
int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen);
|
int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen);
|
||||||
int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen);
|
int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue