diff --git a/include/util/talgo.h b/include/util/talgo.h index 3ce26526087bfe393234aad696601839280fc479..2ffca029bfbfa8a1f8599da3be66a6f690e3ae8e 100644 --- a/include/util/talgo.h +++ b/include/util/talgo.h @@ -65,7 +65,7 @@ void taosqsort(void *src, int64_t numOfElem, int64_t size, const void *param, __ * @param flags * @return */ -void *taosbsearch(const void *key, const void *base, int64_t nmemb, int64_t size, __compar_fn_t fn, int32_t flags); +void *taosbsearch(const void *key, const void *base, int32_t nmemb, int32_t size, __compar_fn_t compar, int32_t flags); /** * adjust heap @@ -82,7 +82,7 @@ void *taosbsearch(const void *key, const void *base, int64_t nmemb, int64_t size * @return */ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const void *parcompar, - __ext_compar_fn_t compar, char* buf, bool maxroot); + __ext_compar_fn_t compar, char *buf, bool maxroot); /** * sort heap to make sure it is a max/min root heap @@ -97,8 +97,7 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const * @param maxroot: if heap is max root heap * @return */ -void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, __ext_compar_fn_t compar, - bool maxroot); +void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, __ext_compar_fn_t compar, bool maxroot); #ifdef __cplusplus } diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit.c b/source/dnode/vnode/src/tsdb/tsdbCommit.c index 0a83fb126d31046548ea47d13b78dc3a0c5a02c7..8973083d789ba9a6cdcefb68f9a344f43967df91 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit.c @@ -61,8 +61,9 @@ typedef struct { static int32_t tsdbCommitData(SCommitH *pCommith); static int32_t tsdbCommitDel(SCommitH *pCommith); static int32_t tsdbCommitCache(SCommitH *pCommith); -static void tsdbStartCommit(STsdb *pRepo); -static void tsdbEndCommit(STsdb *pTsdb, int eno); +static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitH *pCHandle); +static int32_t tsdbEndCommit(SCommitH *pCHandle, int eno); + static int tsdbInitCommitH(SCommitH *pCommith, STsdb *pRepo); static void tsdbSeekCommitIter(SCommitH *pCommith, TSKEY key); static int tsdbNextCommitFid(SCommitH *pCommith); @@ -115,9 +116,9 @@ int32_t tsdbCommit(STsdb *pTsdb) { pTsdb->mem = NULL; // start commit - tsdbStartCommit(pTsdb); - if (tsdbInitCommitH(&commith, pTsdb) < 0) { - return -1; + code = tsdbStartCommit(pTsdb, &commith); + if (code) { + goto _err; } // commit impl @@ -137,18 +138,21 @@ int32_t tsdbCommit(STsdb *pTsdb) { } // end commit - tsdbDestroyCommitH(&commith); - tsdbEndCommit(pTsdb, TSDB_CODE_SUCCESS); + code = tsdbEndCommit(&commith, 0); + if (code) { + goto _err; + } return code; _err: + tsdbError("vgId:%d failed to commit since %s", TD_VID(pTsdb->pVnode), tstrerror(code)); return code; } static int32_t tsdbCommitData(SCommitH *pCommith) { int32_t fid; - SDFileSet *pSet; + SDFileSet *pSet = NULL; int32_t code = 0; STsdb *pTsdb = TSDB_COMMIT_REPO(pCommith); @@ -276,19 +280,32 @@ void tsdbGetRtnSnap(STsdb *pRepo, SRtn *pRtn) { pRtn->minFid, pRtn->midFid, pRtn->maxFid); } -static void tsdbStartCommit(STsdb *pRepo) { - SMemTable *pMem = pRepo->imem; +static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitH *pCHandle) { + int32_t code = 0; - tsdbInfo("vgId:%d, start to commit", REPO_ID(pRepo)); + tsdbInfo("vgId:%d, start to commit", REPO_ID(pTsdb)); - tsdbStartFSTxn(pRepo, 0, 0); + if (tsdbInitCommitH(pCHandle, pTsdb) < 0) { + return -1; + } + + tsdbStartFSTxn(pTsdb, 0, 0); + + return code; } -static void tsdbEndCommit(STsdb *pTsdb, int eno) { +static int32_t tsdbEndCommit(SCommitH *pCHandle, int eno) { + int32_t code = 0; + STsdb *pTsdb = TSDB_COMMIT_REPO(pCHandle); + + tsdbDestroyCommitH(pCHandle); tsdbEndFSTxn(pTsdb); tsdbMemTableDestroy(pTsdb->imem); pTsdb->imem = NULL; + tsdbInfo("vgId:%d, commit over, %s", REPO_ID(pTsdb), (eno == TSDB_CODE_SUCCESS) ? "succeed" : "failed"); + + return code; } static int tsdbInitCommitH(SCommitH *pCommith, STsdb *pRepo) { diff --git a/source/util/src/talgo.c b/source/util/src/talgo.c index 8675670cfe1e61da83e79e4e3a0ea665cec597ca..06e04732bb928152aeb8ce66f8736ab3146476e7 100644 --- a/source/util/src/talgo.c +++ b/source/util/src/talgo.c @@ -158,82 +158,68 @@ void taosqsort(void *src, int64_t numOfElem, int64_t size, const void *param, __ taosMemoryFreeClear(buf); } -void *taosbsearch(const void *key, const void *base, int64_t nmemb, int64_t size, __compar_fn_t compar, int32_t flags) { - // TODO: need to check the correctness of this function - int32_t l = 0; - int32_t r = (int32_t)nmemb; - int32_t idx = 0; - int32_t comparison; +void *taosbsearch(const void *key, const void *base, int32_t nmemb, int32_t size, __compar_fn_t compar, int32_t flags) { + uint8_t *p; + int32_t lidx; + int32_t ridx; + int32_t midx; + int32_t c; + + if (nmemb <= 0) return NULL; + + lidx = 0; + ridx = nmemb - 1; + while (lidx <= ridx) { + midx = (lidx + ridx) / 2; + p = (uint8_t *)base + size * midx; + + c = compar(key, p); + if (c == 0) { + break; + } else if (c < 0) { + ridx = midx - 1; + } else { + lidx = midx + 1; + } + } if (flags == TD_EQ) { - return bsearch(key, base, nmemb, size, compar); - } else if (flags == TD_GE) { - if (nmemb <= 0) return NULL; - if ((*compar)(key, elePtrAt(base, size, 0)) <= 0) return elePtrAt(base, size, 0); - if ((*compar)(key, elePtrAt(base, size, nmemb - 1)) > 0) return NULL; - - while (l < r) { - idx = (l + r) / 2; - comparison = (*compar)(key, elePtrAt(base, size, idx)); - if (comparison < 0) { - r = idx; - } else if (comparison > 0) { - l = idx + 1; - } else { - return elePtrAt(base, size, idx); - } + if (c == 0) { + return p; + } else { + return NULL; } - - if ((*compar)(key, elePtrAt(base, size, idx)) < 0) { - return elePtrAt(base, size, idx); + } else if (flags == TD_GE) { + if (c <= 0) { + return p; } else { - if (idx + 1 > nmemb - 1) { - return NULL; + if (midx + 1 < nmemb) { + return p + size; } else { - return elePtrAt(base, size, idx + 1); + return NULL; } } } else if (flags == TD_LE) { - if (nmemb <= 0) return NULL; - if ((*compar)(key, elePtrAt(base, size, nmemb - 1)) >= 0) return elePtrAt(base, size, nmemb - 1); - if ((*compar)(key, elePtrAt(base, size, 0)) < 0) return NULL; - - while (l < r) { - idx = (l + r) / 2; - comparison = (*compar)(key, elePtrAt(base, size, idx)); - if (comparison < 0) { - r = idx; - } else if (comparison > 0) { - l = idx + 1; - } else { - return elePtrAt(base, size, idx); - } - } - - if ((*compar)(key, elePtrAt(base, size, idx)) > 0) { - return elePtrAt(base, size, idx); + if (c >= 0) { + return p; } else { - if (idx == 0) { - return NULL; + if (midx > 0) { + return p - size; } else { - return elePtrAt(base, size, idx - 1); + return NULL; } } - } else { - assert(0); - return NULL; + ASSERT(0); } - - return NULL; } void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const void *parcompar, - __ext_compar_fn_t compar, char* buf, bool maxroot) { + __ext_compar_fn_t compar, char *buf, bool maxroot) { int32_t parent; int32_t child; - char* tmp = NULL; + char *tmp = NULL; if (buf == NULL) { tmp = taosMemoryMalloc(size); } else { @@ -288,7 +274,7 @@ void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, bool maxroot) { int32_t i; - char* buf = taosMemoryCalloc(1, size); + char *buf = taosMemoryCalloc(1, size); if (buf == NULL) { return; } diff --git a/source/util/test/CMakeLists.txt b/source/util/test/CMakeLists.txt index 8c0f0c76ef0f7e03be2edf2f36fde3b620c166cd..d2a503e6613909c39af8cfb961dd5b51f36120c3 100644 --- a/source/util/test/CMakeLists.txt +++ b/source/util/test/CMakeLists.txt @@ -67,4 +67,12 @@ target_link_libraries(bloomFilterTest os util gtest_main) add_test( NAME bloomFilterTest COMMAND bloomFilterTest +) + +# taosbsearchTest +add_executable(taosbsearchTest "taosbsearchTest.cpp") +target_link_libraries(taosbsearchTest os util gtest_main) +add_test( + NAME taosbsearchTest + COMMAND taosbsearchTest ) \ No newline at end of file