more TDB
This commit is contained in:
parent
2aa6d7e384
commit
4383b3b53a
|
@ -203,6 +203,34 @@ int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *p
|
|||
return 0;
|
||||
}
|
||||
|
||||
int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen) {
|
||||
SBtCursor btc;
|
||||
SCell *pCell;
|
||||
int cret;
|
||||
SCellDecoder cd;
|
||||
|
||||
tdbBtreeCursor(&btc, pBt);
|
||||
|
||||
tdbBtCursorMoveTo(&btc, pKey, kLen, &cret);
|
||||
|
||||
if (cret) {
|
||||
return cret;
|
||||
}
|
||||
|
||||
pCell = tdbPageGetCell(btc.pPage, btc.idx);
|
||||
tdbBtreeDecodeCell(btc.pPage, pCell, &cd);
|
||||
|
||||
*vLen = cd.vLen;
|
||||
// TODO: here may have memory leak
|
||||
*ppVal = realloc(*ppVal, *vLen);
|
||||
if (*ppVal == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(*ppVal, cd.pVal, cd.vLen);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tdbBtCursorMoveToChild(SBtCursor *pCur, SPgno pgno) {
|
||||
int ret;
|
||||
|
||||
|
|
|
@ -88,7 +88,6 @@ int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int v
|
|||
return 0;
|
||||
}
|
||||
|
||||
int tdbDbGet(STDb *pDb, const void *pKey, int kLen, void *pVal, int *vLen) {
|
||||
// TODO
|
||||
return 0;
|
||||
int tdbDbGet(STDb *pDb, const void *pKey, int kLen, void **ppVal, int *vLen) {
|
||||
return tdbBtreeGet(pDb->pBt, pKey, kLen, ppVal, vLen);
|
||||
}
|
|
@ -37,6 +37,7 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pFile, FKeyComparator kcmpr, SB
|
|||
int tdbBtreeClose(SBTree *pBt);
|
||||
int tdbBtreeCursor(SBtCursor *pCur, SBTree *pBt);
|
||||
int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen);
|
||||
int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprF
|
|||
int tdbDbClose(STDb *pDb);
|
||||
int tdbDbDrop(STDb *pDb);
|
||||
int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int valLen);
|
||||
int tdbDbGet(STDb *pDb, const void *pKey, int kLen, void *pVal, int *vLen);
|
||||
int tdbDbGet(STDb *pDb, const void *pKey, int kLen, void **ppVal, int *vLen);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ TEST(tdb_test, simple_test) {
|
|||
sprintf(key, "key%d", i);
|
||||
sprintf(val, "value%d", i);
|
||||
|
||||
ret = tdbDbGet(pDb, key, strlen(key), pVal, &vLen);
|
||||
ret = tdbDbGet(pDb, key, strlen(key), &pVal, &vLen);
|
||||
GTEST_ASSERT_EQ(ret, 0);
|
||||
|
||||
GTEST_ASSERT_EQ(vLen, strlen(val));
|
||||
|
|
Loading…
Reference in New Issue