more
This commit is contained in:
parent
fd6b2ad626
commit
21cec7b6d7
|
@ -45,13 +45,12 @@ int tdbDbGet(TDB *pDb, const void *pKey, int kLen, void **ppVal, int *vLen);
|
||||||
int tdbDbPGet(TDB *pDb, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen);
|
int tdbDbPGet(TDB *pDb, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen);
|
||||||
|
|
||||||
// TDBC
|
// TDBC
|
||||||
#define TDB_FLG_BACKWD 0x1 // backward search
|
#define TDB_FLG_CMP_LT 0x1 // less than
|
||||||
#define TDB_FLG_CMP_LT 0x2 // less than
|
#define TDB_FLG_CMP_EQ 0x2 // equal
|
||||||
#define TDB_FLG_CMP_EQ 0x4 // equal
|
#define TDB_FLG_CMP_GT 0x4 // greater than
|
||||||
#define TDB_FLG_CMP_GT 0x8 // greater than
|
|
||||||
|
|
||||||
int tdbDbcOpen(TDB *pDb, TDBC **ppDbc, TXN *pTxn);
|
int tdbDbcOpen(TDB *pDb, TDBC **ppDbc, TXN *pTxn);
|
||||||
int tdbDbcMoveTo(TDBC *pDbc, const void *pKey, int kLen, tdb_cmpr_fn_t cmprFn, int flags);
|
int tdbDbcMoveTo(TDBC *pDbc, const void *pKey, int kLen, int flags);
|
||||||
int tdbDbcPut(TDBC *pDbc, const void *pKey, int keyLen, const void *pVal, int valLen);
|
int tdbDbcPut(TDBC *pDbc, const void *pKey, int keyLen, const void *pVal, int valLen);
|
||||||
int tdbDbcUpdate(TDBC *pDbc, const void *pKey, int kLen, const void *pVal, int vLen);
|
int tdbDbcUpdate(TDBC *pDbc, const void *pKey, int kLen, const void *pVal, int vLen);
|
||||||
int tdbDbcDrop(TDBC *pDbc);
|
int tdbDbcDrop(TDBC *pDbc);
|
||||||
|
|
|
@ -1506,20 +1506,45 @@ void tdbBtPageInfo(SPage *pPage, int idx) {
|
||||||
#endif
|
#endif
|
||||||
// TDB_BTREE_DEBUG
|
// TDB_BTREE_DEBUG
|
||||||
|
|
||||||
int tdbBtcMoveTo2(SBTC *pBtc, const void *pKey, int kLen, tdb_cmpr_fn_t cmprFn, int flags) {
|
static void tdbBSearch(int *lidx, int *ridx, int midx, int c, int flags) {
|
||||||
SBTree *pBt = pBtc->pBt;
|
if (flags & TDB_FLG_CMP_EQ) {
|
||||||
SPager *pPager = pBt->pPager;
|
if (c < 0) {
|
||||||
SPgno root = pBt->root;
|
*lidx = midx + 1;
|
||||||
SCellDecoder cd = {0};
|
} else if (c == 0) {
|
||||||
int nCells = 0;
|
*lidx = *ridx + 1;
|
||||||
SCell *pCell = NULL;
|
} else {
|
||||||
int ret = 0;
|
*ridx = midx - 1;
|
||||||
int c;
|
}
|
||||||
int backward = flags & TDB_FLG_BACKWD;
|
} else if (flags & TDB_FLG_CMP_GT) {
|
||||||
|
if (c <= 0) {
|
||||||
if (cmprFn == NULL) {
|
*lidx = midx + 1;
|
||||||
cmprFn = pBt->kcmpr;
|
} else {
|
||||||
|
*ridx = midx - 1;
|
||||||
|
}
|
||||||
|
} else if (flags & TDB_FLG_CMP_LT) {
|
||||||
|
if (c < 0) {
|
||||||
|
*lidx = midx + 1;
|
||||||
|
} else {
|
||||||
|
*ridx = midx - 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ASSERT(0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int tdbBtcMoveTo2(SBTC *pBtc, const void *pKey, int kLen, int flags) {
|
||||||
|
SBTree *pBt = pBtc->pBt;
|
||||||
|
SPager *pPager = pBt->pPager;
|
||||||
|
SPgno root = pBt->root;
|
||||||
|
SCellDecoder cd = {0};
|
||||||
|
int nCells = 0;
|
||||||
|
SCell *pCell = NULL;
|
||||||
|
int ret = 0;
|
||||||
|
int c;
|
||||||
|
u8 leaf;
|
||||||
|
tdb_cmpr_fn_t cmprFn;
|
||||||
|
|
||||||
|
cmprFn = pBt->kcmpr;
|
||||||
|
|
||||||
// move cursor to a level
|
// move cursor to a level
|
||||||
if (pBtc->iPage < 0) {
|
if (pBtc->iPage < 0) {
|
||||||
|
@ -1538,6 +1563,7 @@ int tdbBtcMoveTo2(SBTC *pBtc, const void *pKey, int kLen, tdb_cmpr_fn_t cmprFn,
|
||||||
if (TDB_PAGE_TOTAL_CELLS(pBtc->pPage) == 0) return 0;
|
if (TDB_PAGE_TOTAL_CELLS(pBtc->pPage) == 0) return 0;
|
||||||
} else {
|
} else {
|
||||||
// move from a position (TODO)
|
// move from a position (TODO)
|
||||||
|
ASSERT(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// search downward
|
// search downward
|
||||||
|
@ -1553,28 +1579,21 @@ int tdbBtcMoveTo2(SBTC *pBtc, const void *pKey, int kLen, tdb_cmpr_fn_t cmprFn,
|
||||||
ASSERT(nCells > 0);
|
ASSERT(nCells > 0);
|
||||||
ASSERT(pBtc->idx == -1);
|
ASSERT(pBtc->idx == -1);
|
||||||
|
|
||||||
|
// search two ends
|
||||||
// compare first cell
|
// compare first cell
|
||||||
midx = lidx;
|
midx = lidx;
|
||||||
pCell = tdbPageGetCell(pPage, midx);
|
pCell = tdbPageGetCell(pPage, midx);
|
||||||
tdbBtreeDecodeCell(pPage, pCell, &cd);
|
tdbBtreeDecodeCell(pPage, pCell, &cd);
|
||||||
c = cmprFn(pKey, kLen, cd.pKey, cd.kLen);
|
c = cmprFn(cd.pKey, cd.kLen, pKey, kLen);
|
||||||
if (c <= 0) {
|
tdbBSearch(&lidx, &ridx, midx, c, flags);
|
||||||
ridx = lidx - 1;
|
|
||||||
} else {
|
|
||||||
lidx = lidx + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// compare last cell
|
// compare last cell
|
||||||
if (lidx <= ridx) {
|
if (lidx <= ridx) {
|
||||||
midx = ridx;
|
midx = ridx;
|
||||||
pCell = tdbPageGetCell(pPage, midx);
|
pCell = tdbPageGetCell(pPage, midx);
|
||||||
tdbBtreeDecodeCell(pPage, pCell, &cd);
|
tdbBtreeDecodeCell(pPage, pCell, &cd);
|
||||||
c = cmprFn(pKey, kLen, cd.pKey, cd.kLen);
|
c = cmprFn(cd.pKey, cd.kLen, pKey, kLen);
|
||||||
if (c >= 0) {
|
tdbBSearch(&lidx, &ridx, midx, c, flags);
|
||||||
lidx = ridx + 1;
|
|
||||||
} else {
|
|
||||||
ridx = ridx - 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// binary search
|
// binary search
|
||||||
|
@ -1582,41 +1601,47 @@ int tdbBtcMoveTo2(SBTC *pBtc, const void *pKey, int kLen, tdb_cmpr_fn_t cmprFn,
|
||||||
if (lidx > ridx) break;
|
if (lidx > ridx) break;
|
||||||
|
|
||||||
midx = (lidx + ridx) >> 1;
|
midx = (lidx + ridx) >> 1;
|
||||||
|
|
||||||
pCell = tdbPageGetCell(pPage, midx);
|
pCell = tdbPageGetCell(pPage, midx);
|
||||||
ret = tdbBtreeDecodeCell(pPage, pCell, &cd);
|
tdbBtreeDecodeCell(pPage, pCell, &cd);
|
||||||
if (ret < 0) {
|
|
||||||
// TODO: handle error
|
|
||||||
ASSERT(0);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare the key values
|
|
||||||
c = cmprFn(pKey, kLen, cd.pKey, cd.kLen);
|
c = cmprFn(pKey, kLen, cd.pKey, cd.kLen);
|
||||||
if (c < 0) {
|
tdbBSearch(&lidx, &ridx, midx, c, flags);
|
||||||
// pKey < cd.pKey
|
|
||||||
ridx = midx - 1;
|
|
||||||
} else if (c > 0) {
|
|
||||||
// pKey > cd.pKey
|
|
||||||
lidx = midx + 1;
|
|
||||||
} else {
|
|
||||||
// pKey == cd.pKey
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// keep search downward or break
|
// keep search downward or break
|
||||||
if (TDB_BTREE_PAGE_IS_LEAF(pPage)) {
|
leaf = TDB_BTREE_PAGE_IS_LEAF(pPage);
|
||||||
pBtc->idx = midx;
|
if (!leaf) {
|
||||||
// *pCRst = c;
|
if (flags & 0x7 == TDB_FLG_CMP_EQ) {
|
||||||
break;
|
if (c < 0) {
|
||||||
} else {
|
pBtc->idx = midx + 1;
|
||||||
if (c <= 0) {
|
} else {
|
||||||
pBtc->idx = midx;
|
pBtc->idx = midx;
|
||||||
} else {
|
}
|
||||||
pBtc->idx = midx + 1;
|
} else if (flags & 0x7 == TDB_FLG_CMP_LT) {
|
||||||
|
if (c < 0) {
|
||||||
|
pBtc->idx = midx;
|
||||||
|
} else if (c == 0) {
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
} else if (flags & 0x7 == TDB_FLG_CMP_GT) {
|
||||||
|
if (c < 0) {
|
||||||
|
} else if (c == 0) {
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
} else if (flags & 0x7 == TDB_FLG_CMP_LT | TDB_FLG_CMP_EQ) {
|
||||||
|
if (c < 0) {
|
||||||
|
} else if (c == 0) {
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
} else if (flags & 0x7 == TDB_FLG_CMP_GT | TDB_FLG_CMP_EQ) {
|
||||||
|
if (c < 0) {
|
||||||
|
} else if (c == 0) {
|
||||||
|
} else {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tdbBtcMoveDownward(pBtc);
|
tdbBtcMoveDownward(pBtc);
|
||||||
|
} else {
|
||||||
|
// non-leaf (TODO)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -111,18 +111,17 @@ int tdbDbcOpen(TDB *pDb, TDBC **ppDbc, TXN *pTxn) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tdbDbcMoveTo(TDBC *pDbc, const void *pKey, int kLen, tdb_cmpr_fn_t cmprFn, int flags) {
|
int tdbDbcMoveTo(TDBC *pDbc, const void *pKey, int kLen, int flags) {
|
||||||
int tflags;
|
int tflags;
|
||||||
|
|
||||||
// set/check flags
|
// set/check flags
|
||||||
if (flags == 0) {
|
if (flags == 0) {
|
||||||
flags |= TDB_FLG_CMP_EQ;
|
flags |= TDB_FLG_CMP_EQ;
|
||||||
} else {
|
} else {
|
||||||
tflags = flags & (TDB_FLG_CMP_LT | TDB_FLG_CMP_EQ | TDB_FLG_CMP_GT);
|
if (flags & TDB_FLG_CMP_LT && flags & TDB_FLG_CMP_GT) return -1;
|
||||||
if (tflags != TDB_FLG_CMP_LT && tflags != TDB_FLG_CMP_EQ && tflags != TDB_FLG_CMP_GT) return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return tdbBtcMoveTo2(&pDbc->btc, pKey, kLen, cmprFn, flags);
|
return tdbBtcMoveTo2(&pDbc->btc, pKey, kLen, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
int tdbDbcPut(TDBC *pDbc, const void *pKey, int keyLen, const void *pVal, int valLen) {
|
int tdbDbcPut(TDBC *pDbc, const void *pKey, int keyLen, const void *pVal, int valLen) {
|
||||||
|
|
|
@ -123,7 +123,7 @@ int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkL
|
||||||
|
|
||||||
// SBTC
|
// SBTC
|
||||||
int tdbBtcOpen(SBTC *pBtc, SBTree *pBt, TXN *pTxn);
|
int tdbBtcOpen(SBTC *pBtc, SBTree *pBt, TXN *pTxn);
|
||||||
int tdbBtcMoveTo2(SBTC *pBtc, const void *pKey, int kLen, tdb_cmpr_fn_t cmprFn, int flags);
|
int tdbBtcMoveTo2(SBTC *pBtc, const void *pKey, int kLen, int flags);
|
||||||
int tdbBtcMoveToFirst(SBTC *pBtc);
|
int tdbBtcMoveToFirst(SBTC *pBtc);
|
||||||
int tdbBtcMoveToLast(SBTC *pBtc);
|
int tdbBtcMoveToLast(SBTC *pBtc);
|
||||||
int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen);
|
int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen);
|
||||||
|
|
Loading…
Reference in New Issue