diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 65b79ea588..2c6cbfa91d 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -15,8 +15,6 @@ #include "tdbInt.h" -#define BTREE_MAX_DEPTH 20 - struct SBTree { SPgno root; int keyLen; @@ -36,20 +34,10 @@ typedef struct SPgHdr { SPgno rightChild; } SPgHdr; -typedef struct SBtPage { +struct SBtPage { SPgHdr *pHdr; u16 * aCellIdx; u8 * aData; -} SBtPage; - -struct SBtCursor { - SBTree * pBt; - i8 iPage; - SBtPage *pPage; - u16 idx; - u16 idxStack[BTREE_MAX_DEPTH + 1]; - SBtPage *pgStack[BTREE_MAX_DEPTH + 1]; - void * pBuf; }; typedef struct SFreeCell { diff --git a/source/libs/tdb/src/db/tdbDb.c b/source/libs/tdb/src/db/tdbDb.c index 3532379433..43dc8a2f81 100644 --- a/source/libs/tdb/src/db/tdbDb.c +++ b/source/libs/tdb/src/db/tdbDb.c @@ -75,6 +75,20 @@ int tdbDbDrop(STDb *pDb) { } int tdbDbInsert(STDb *pDb, const void *pKey, int keyLen, const void *pVal, int valLen) { - // TODO + SBtCursor btc; + SBtCursor *pCur; + int ret; + + pCur = &btc; + ret = tdbBtreeCursor(pCur, pDb->pBt); + if (ret < 0) { + return -1; + } + + ret = tdbBtCursorInsert(pCur, pKey, keyLen, pVal, valLen); + if (ret < 0) { + return -1; + } + return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbBtree.h b/source/libs/tdb/src/inc/tdbBtree.h index 745a336e89..d101bb5e94 100644 --- a/source/libs/tdb/src/inc/tdbBtree.h +++ b/source/libs/tdb/src/inc/tdbBtree.h @@ -22,9 +22,21 @@ extern "C" { typedef struct SBTree SBTree; typedef struct SBtCursor SBtCursor; +typedef struct SBtPage SBtPage; + +struct SBtCursor { + SBTree * pBt; + i8 iPage; + SBtPage *pPage; + u16 idx; + u16 idxStack[BTREE_MAX_DEPTH + 1]; + SBtPage *pgStack[BTREE_MAX_DEPTH + 1]; + void * pBuf; +}; int tdbBtreeOpen(SPgno rtPgno, int keyLen, int valLen, SPFile *pFile, FKeyComparator kcmpr, SBTree **ppBt); int tdbBtreeClose(SBTree *pBt); +int tdbBtreeCursor(SBtCursor *pCur, SBTree *pBt); int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen); #ifdef __cplusplus diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 1e66e6ca40..3d7b4db389 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -129,6 +129,8 @@ typedef int (*FKeyComparator)(const void *pKey1, int kLen1, const void *pKey2, i #define TDB_DEFAULT_FANOUT 6 +#define BTREE_MAX_DEPTH 20 + #include "tdbUtil.h" #include "tdbPCache.h" diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 952233a683..eb4f2a6cc0 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -16,13 +16,11 @@ TEST(tdb_test, simple_test) { GTEST_ASSERT_EQ(ret, 0); // // Insert some data - // ret = tdbDbInsert(pDb, "1", 1, "world", 5); - // GTEST_ASSERT_EQ(ret, 0); + ret = tdbDbInsert(pDb, "1", 1, "world", 5); + GTEST_ASSERT_EQ(ret, 0); ret = tdbDbDrop(pDb); - if (pDb == NULL) { - return -1; - } + GTEST_ASSERT_EQ(ret, 0); // Close a database tdbDbClose(pDb);