From 30d49687e9a7e24d22f863dbdbd3dbad29681962 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Mon, 28 Mar 2022 19:01:57 +0800 Subject: [PATCH] update index query --- source/libs/index/inc/index_fst.h | 14 +++---- source/libs/index/src/index_fst.c | 5 +-- source/libs/index/test/fstTest.cc | 67 +++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 14 deletions(-) diff --git a/source/libs/index/inc/index_fst.h b/source/libs/index/inc/index_fst.h index cf5c3f306b..b131aa0d9d 100644 --- a/source/libs/index/inc/index_fst.h +++ b/source/libs/index/inc/index_fst.h @@ -21,9 +21,9 @@ extern "C" { #endif #include "indexInt.h" -#include "index_fst_node.h" #include "index_fst_automation.h" #include "index_fst_counting_writer.h" +#include "index_fst_node.h" #include "index_fst_registry.h" #include "index_fst_util.h" @@ -257,9 +257,9 @@ typedef struct FstMeta { } FstMeta; typedef struct Fst { - FstMeta* meta; - FstSlice* data; // - FstNode* root; // + FstMeta* meta; + FstSlice* data; // + FstNode* root; // TdThreadMutex mtx; } Fst; @@ -325,10 +325,10 @@ StreamWithStateResult* streamWithStateNextWith(StreamWithState* sws, StreamCallb FstStreamBuilder* fstStreamBuilderCreate(Fst* fst, AutomationCtx* aut); void fstStreamBuilderDestroy(FstStreamBuilder* b); -// set up bound range -// refator, simple code by marco -FstStreamBuilder* fstStreamBuilderRange(FstStreamBuilder* b, FstSlice* val, RangeType type); +// set up bound range +// refator later: to simple code by marco +void fstStreamBuilderSetRange(FstStreamBuilder* b, FstSlice* val, RangeType type); #ifdef __cplusplus } diff --git a/source/libs/index/src/index_fst.c b/source/libs/index/src/index_fst.c index f35d7d39f2..5fd8865b21 100644 --- a/source/libs/index/src/index_fst.c +++ b/source/libs/index/src/index_fst.c @@ -1425,9 +1425,9 @@ void fstStreamBuilderDestroy(FstStreamBuilder* b) { taosMemoryFreeClear(b->max); taosMemoryFree(b); } -FstStreamBuilder* fstStreamBuilderRange(FstStreamBuilder* b, FstSlice* val, RangeType type) { +void fstStreamBuilderSetRange(FstStreamBuilder* b, FstSlice* val, RangeType type) { if (b == NULL) { - return NULL; + return; } if (type == GE) { b->min->type = Included; @@ -1446,5 +1446,4 @@ FstStreamBuilder* fstStreamBuilderRange(FstStreamBuilder* b, FstSlice* val, Rang fstSliceDestroy(&(b->max->data)); b->max->data = fstSliceDeepCopy(val, 0, FST_SLICE_LEN(val) - 1); } - return b; } diff --git a/source/libs/index/test/fstTest.cc b/source/libs/index/test/fstTest.cc index e7e5070d14..1b9acbd67e 100644 --- a/source/libs/index/test/fstTest.cc +++ b/source/libs/index/test/fstTest.cc @@ -96,12 +96,36 @@ class FstReadMemory { char* ch = (char*)fstSliceData(s, &sz); std::string key(ch, sz); printf("key: %s, val: %" PRIu64 "\n", key.c_str(), (uint64_t)(rt->out.out)); - swsResultDestroy(rt); result.push_back(rt->out.out); + swsResultDestroy(rt); } - for (size_t i = 0; i < result.size(); i++) { + return true; + } + bool SearchRange(AutomationCtx* ctx, const std::string& low, const std::string& high, std::vector& result) { + FstStreamBuilder* sb = fstSearch(_fst, ctx); + + FstSlice l = fstSliceCreate((uint8_t*)low.c_str(), low.size()); + FstSlice h = fstSliceCreate((uint8_t*)high.c_str(), high.size()); + + // range [low, high); + fstStreamBuilderSetRange(sb, &l, GE); + fstStreamBuilderSetRange(sb, &h, LT); + + fstSliceDestroy(&l); + fstSliceDestroy(&h); + + StreamWithState* st = streamBuilderIntoStream(sb); + StreamWithStateResult* rt = NULL; + while ((rt = streamWithStateNextWith(st, NULL)) != NULL) { + // result.push_back((uint64_t)(rt->out.out)); + FstSlice* s = &rt->data; + int32_t sz = 0; + char* ch = (char*)fstSliceData(s, &sz); + std::string key(ch, sz); + printf("key: %s, val: %" PRIu64 "\n", key.c_str(), (uint64_t)(rt->out.out)); + result.push_back(rt->out.out); + swsResultDestroy(rt); } - std::cout << std::endl; return true; } bool SearchWithTimeCostUs(AutomationCtx* ctx, std::vector& result) { @@ -322,6 +346,40 @@ void checkFstCheckIteratorPrefix() { } delete m; } +void checkFstCheckIteratorRange() { + FstWriter* fw = new FstWriter; + int64_t s = taosGetTimestampUs(); + int count = 2; + // Performance_fstWriteRecords(fw); + int64_t e = taosGetTimestampUs(); + + std::cout << "insert data count : " << count << "elapas time: " << e - s << std::endl; + + fw->Put("a", 1); + fw->Put("b", 2); + fw->Put("c", 3); + fw->Put("d", 4); + fw->Put("e", 5); + delete fw; + + FstReadMemory* m = new FstReadMemory(1024 * 64); + if (m->init() == false) { + std::cout << "init readMemory failed" << std::endl; + delete m; + return; + } + { + // prefix search + std::vector result; + + AutomationCtx* ctx = automCtxCreate((void*)"he", AUTOMATION_ALWAYS); + + // [b, e) + m->SearchRange(ctx, "b", "e", result); + // assert(result.size() == 1); + taosMemoryFree(ctx); + } +} void fst_get(Fst* fst) { for (int i = 0; i < 10000; i++) { @@ -386,7 +444,8 @@ int main(int argc, char* argv[]) { // iterTFileReader(argv[1], argv[2], argv[3], argv[4]); //} // checkFstCheckIterator(); - checkFstCheckIteratorPrefix(); + // checkFstCheckIteratorPrefix(); + checkFstCheckIteratorRange(); // checkFstLongTerm(); // checkFstPrefixSearch();