From 9e74ea9ed3cd21052576c9a2194518c54b105fc7 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Fri, 22 Oct 2021 22:47:26 +0800 Subject: [PATCH] add lucene test --- include/libs/index/index.h | 48 ++++-------- source/libs/index/inc/indexInt.h | 21 ++++++ source/libs/index/src/index.c | 102 +++++++++++++++++++++++--- source/libs/index/test/indexTests.cpp | 30 +++++++- 4 files changed, 158 insertions(+), 43 deletions(-) diff --git a/include/libs/index/index.h b/include/libs/index/index.h index bdd0905234..346214e0c8 100644 --- a/include/libs/index/index.h +++ b/include/libs/index/index.h @@ -25,54 +25,36 @@ extern "C" { typedef struct SIndex SIndex; typedef struct SIndexOpts SIndexOpts; +typedef struct SIndexMultiTermQuery SIndexMultiTermQuery; +typedef struct SArray SIndexMultiTerm; +//typedef struct SIndexMultiTerm SIndexMultiTerm; typedef enum { MUST = 0, SHOULD = 1, NOT = 2 } EIndexOperatorType; typedef enum { QUERY_POINT = 0, QUERY_PREFIX = 1, QUERY_SUFFIX = 2,QUERY_REGEX = 3} EIndexQueryType; - -typedef struct SIndexTermQuery { - EIndexQueryType opera; - SArray *querys; -} SIndexTermQuery; - -// tag and tag val; -typedef struct SIndexPair { - char *key; - char *val; -} SIndexPair; - -// -typedef struct SIndexTerm { - SIndexPair* field_value; - EIndexQueryType type; -} SIndexTerm; - - - /* * @param: oper * */ - -SIndexTermQuery *indexTermQueryCreate(EIndexOperatorType oper); -void indexTermQueryDestroy(SIndexTermQuery *pQuery); -int indexTermQueryAdd(SIndexTermQuery *pQuery, const char *field, int32_t nFields, const char *value, int32_t nValue, EIndexQueryType type); - - +SIndexMultiTermQuery *indexMultiTermQueryCreate(EIndexOperatorType oper); +void indexMultiTermQueryDestroy(SIndexMultiTermQuery *pQuery); +int indexMultiTermQueryAdd(SIndexMultiTermQuery *pQuery, const char *field, int32_t nFields, const char *value, int32_t nValue, EIndexQueryType type); /* * @param: * @param: */ SIndex* indexOpen(SIndexOpts *opt, const char *path); - -void indexClose(SIndex *index); -int indexPut(SIndex *index, SArray *pairs, int uid); -int indexDelete(SIndex *index, SIndexTermQuery *query); -int indexSearch(SIndex *index, SIndexTermQuery *query, SArray *result); -int indexRebuild(SIndex *index, SIndexOpts *opt); - +void indexClose(SIndex *index); +int indexPut(SIndex *index, SIndexMultiTerm *terms, int uid); +int indexDelete(SIndex *index, SIndexMultiTermQuery *query); +int indexSearch(SIndex *index, SIndexMultiTermQuery *query, SArray *result); +int indexRebuild(SIndex *index, SIndexOpts *opt); + +SIndexMultiTerm *indexMultiTermCreate(); +int indexMultiTermAdd(SIndexMultiTerm *terms, const char *field, int32_t nFields, const char *value, int32_t nValue); +void indexMultiTermDestroy(SIndexMultiTerm *terms); /* * @param: * @param: diff --git a/source/libs/index/inc/indexInt.h b/source/libs/index/inc/indexInt.h index 8d8c950075..742427bf94 100644 --- a/source/libs/index/inc/indexInt.h +++ b/source/libs/index/inc/indexInt.h @@ -39,6 +39,27 @@ struct SIndexOpts { #endif }; +struct SIndexMultiTermQuery { + EIndexOperatorType opera; + SArray *query; +}; + +// field and key; +typedef struct SIndexTerm { + char *key; + int32_t nKey; + char *val; + int32_t nVal; +} SIndexTerm; + +typedef struct SIndexTermQuery { + SIndexTerm* field_value; + EIndexQueryType type; +} SIndexTermQuery; + + +SIndexTerm *indexTermCreate(const char *key, int32_t nKey, const char *val, int32_t nVal); +void indexTermDestroy(SIndexTerm *p); #ifdef __cplusplus } #endif diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index 46039249c5..e4b2a4acc4 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -38,46 +38,130 @@ SIndex *indexOpen(SIndexOpts *opts, const char *path) { void indexClose(SIndex *index) { #ifdef USE_LUCENE index_close(index->index); + index->index = NULL; #endif free(index); return; } + +#ifdef USE_LUCENE +#endif int indexPut(SIndex *index, SArray* field_vals, int uid) { +#ifdef USE_LUCENE + index_document_t *doc = index_document_create(); + + char buf[16] = {0}; + sprintf(buf, "%d", uid); + + for (int i = 0; i < taosArrayGetSize(field_vals); i++) { + SIndexTerm *p = taosArrayGetP(field_vals, i); + index_document_add(doc, (const char *)(p->key), p->nKey, (const char *)(p->val), p->nVal, 1); + } + index_document_add(doc, NULL, 0, buf, strlen(buf), 0); + + index_put(index->index, doc); + index_document_destroy(doc); +#endif return 1; } -int indexSearch(SIndex *index, SIndexTermQuery *query, SArray *result) { +int indexSearch(SIndex *index, SIndexMultiTermQuery *multiQuerys, SArray *result) { +#ifdef USE_LUCENE + for (int i = 0; i < taosArrayGetSize(multiQuerys->query); i++) { + SIndexTermQuery *p = taosArrayGet(multiQuerys->query, i); + SIndexTerm *term = p->field_value; + EIndexQueryType qType = p->type; + int *tResult = NULL; + int32_t tsz = 0; + index_search(index->index, term->key, term->nKey, term->val, term->nVal, qType, &tResult, &tsz); + for (int i = 0; i < tsz; i++) { + taosArrayPush(result, &(tResult[i])); + } + + } +#endif return 1; } -int indexDelete(SIndex *index, SIndexTermQuery *query) { +int indexDelete(SIndex *index, SIndexMultiTermQuery *query) { return 1; } int indexRebuild(SIndex *index, SIndexOpts *opts); SIndexOpts *indexOptsCreate() { - return NULL; +#ifdef USE_LUCENE +#endif +return NULL; } void indexOptsDestroy(SIndexOpts *opts) { - +#ifdef USE_LUCENE +#endif } /* * @param: oper * */ -SIndexTermQuery *indexTermQueryCreate(EIndexOperatorType oper) { - return NULL; +SIndexMultiTermQuery *indexMultiTermQueryCreate(EIndexOperatorType opera) { + SIndexMultiTermQuery *p = (SIndexMultiTermQuery *)malloc(sizeof(SIndexMultiTermQuery)); + if (p == NULL) { return NULL; } + p->opera = opera; + p->query = taosArrayInit(1, sizeof(SIndexTermQuery)); + return p; +} +void indexMultiTermQueryDestroy(SIndexMultiTermQuery *pQuery) { + for (int i = 0; i < taosArrayGetSize(pQuery->query); i++) { + SIndexTermQuery *p = (SIndexTermQuery *)taosArrayGet(pQuery->query, i); + indexTermDestroy(p->field_value); + } + taosArrayDestroy(pQuery->query); + free(pQuery); +}; +int indexMultiTermQueryAdd(SIndexMultiTermQuery *pQuery, const char *field, int32_t nFields, const char *value, int32_t nValue, EIndexQueryType type){ + SIndexTerm *t = indexTermCreate(field, nFields, value, nValue); + if (t == NULL) {return -1;} + SIndexTermQuery q = {.type = type, .field_value = t}; + taosArrayPush(pQuery->query, &q); + return 0; } -void indexTermQueryDestroy(SIndexTermQuery *pQuery) { + +SIndexTerm *indexTermCreate(const char *key, int32_t nKey, const char *val, int32_t nVal) { + SIndexTerm *t = (SIndexTerm *)malloc(sizeof(SIndexTerm)); + t->key = (char *)calloc(nKey + 1, 1); + memcpy(t->key, key, nKey); + t->nKey = nKey; + + t->val = (char *)calloc(nVal + 1, 1); + memcpy(t->val, val, nVal); + t->nVal = nVal; + return t; } -int indexTermQueryAdd(SIndexTermQuery *pQuery, const char *field, int32_t nFields, const char *value, int32_t nValue, EIndexQueryType type){ - return 1; +void indexTermDestroy(SIndexTerm *p) { + free(p->key); + free(p->val); + free(p); +} + +SArray *indexMultiTermCreate() { + return taosArrayInit(4, sizeof(SIndexTerm *)); } +int indexMultiTermAdd(SArray *array, const char *field, int32_t nField, const char *val, int32_t nVal) { + SIndexTerm *term = indexTermCreate(field, nField, val, nVal); + if (term == NULL) { return -1; } + taosArrayPush(array, &term); + return 0; +} +void indexMultiTermDestroy(SArray *array) { + for (int32_t i = 0; i < taosArrayGetSize(array); i++) { + SIndexTerm *p = taosArrayGetP(array, i); + indexTermDestroy(p); + } + taosArrayDestroy(array); +} void indexInit() { //do nothing } diff --git a/source/libs/index/test/indexTests.cpp b/source/libs/index/test/indexTests.cpp index 047491838f..efa7f37a60 100644 --- a/source/libs/index/test/indexTests.cpp +++ b/source/libs/index/test/indexTests.cpp @@ -2,14 +2,42 @@ #include #include #include "index.h" +#include "indexInt.h" + TEST(IndexTest, index_create_test) { SIndexOpts *opts = indexOptsCreate(); - SIndex *index = indexOpen(opts, "./"); + SIndex *index = indexOpen(opts, "./test"); if (index == NULL) { std::cout << "index open failed" << std::endl; } + + + SArray* terms = indexMultiTermCreate(); + indexMultiTermAdd(terms, "tag1", strlen("tag1"), "field", strlen("field")); + for (int i = 0; i < 10; i++) { + indexPut(index, terms, i); + } + indexMultiTermDestroy(terms); + + + // query + SIndexMultiTermQuery *multiQuery = indexMultiTermQueryCreate(MUST); + indexMultiTermQueryAdd(multiQuery, "tag1", strlen("tag1"), "field", strlen("field"), QUERY_PREFIX); + + SArray *result = (SArray *)taosArrayInit(10, sizeof(int)); + indexSearch(index, multiQuery, result); + + std::cout << "taos'size : " << taosArrayGetSize(result) << std::endl; + for (int i = 0; i < taosArrayGetSize(result); i++) { + int *v = (int *)taosArrayGet(result, i); + std::cout << "value --->" << *v << std::endl; + } + indexMultiTermQueryDestroy(multiQuery); + indexOptsDestroy(opts); + indexClose(index); + // } -- GitLab