add index test example
This commit is contained in:
parent
9e74ea9ed3
commit
551cfd5c35
|
@ -27,12 +27,9 @@ typedef struct SIndex SIndex;
|
||||||
typedef struct SIndexOpts SIndexOpts;
|
typedef struct SIndexOpts SIndexOpts;
|
||||||
typedef struct SIndexMultiTermQuery SIndexMultiTermQuery;
|
typedef struct SIndexMultiTermQuery SIndexMultiTermQuery;
|
||||||
typedef struct SArray SIndexMultiTerm;
|
typedef struct SArray SIndexMultiTerm;
|
||||||
//typedef struct SIndexMultiTerm SIndexMultiTerm;
|
|
||||||
|
|
||||||
typedef enum { MUST = 0, SHOULD = 1, NOT = 2 } EIndexOperatorType;
|
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 enum { QUERY_TERM = 0, QUERY_PREFIX = 1, QUERY_SUFFIX = 2,QUERY_REGEX = 3} EIndexQueryType;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @param: oper
|
* @param: oper
|
||||||
*
|
*
|
||||||
|
@ -40,7 +37,6 @@ typedef enum { QUERY_POINT = 0, QUERY_PREFIX = 1, QUERY_SUFFIX = 2,QUERY_REGEX
|
||||||
SIndexMultiTermQuery *indexMultiTermQueryCreate(EIndexOperatorType oper);
|
SIndexMultiTermQuery *indexMultiTermQueryCreate(EIndexOperatorType oper);
|
||||||
void indexMultiTermQueryDestroy(SIndexMultiTermQuery *pQuery);
|
void indexMultiTermQueryDestroy(SIndexMultiTermQuery *pQuery);
|
||||||
int indexMultiTermQueryAdd(SIndexMultiTermQuery *pQuery, const char *field, int32_t nFields, const char *value, int32_t nValue, EIndexQueryType type);
|
int indexMultiTermQueryAdd(SIndexMultiTermQuery *pQuery, const char *field, int32_t nFields, const char *value, int32_t nValue, EIndexQueryType type);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @param:
|
* @param:
|
||||||
* @param:
|
* @param:
|
||||||
|
@ -51,7 +47,10 @@ int indexPut(SIndex *index, SIndexMultiTerm *terms, int uid);
|
||||||
int indexDelete(SIndex *index, SIndexMultiTermQuery *query);
|
int indexDelete(SIndex *index, SIndexMultiTermQuery *query);
|
||||||
int indexSearch(SIndex *index, SIndexMultiTermQuery *query, SArray *result);
|
int indexSearch(SIndex *index, SIndexMultiTermQuery *query, SArray *result);
|
||||||
int indexRebuild(SIndex *index, SIndexOpts *opt);
|
int indexRebuild(SIndex *index, SIndexOpts *opt);
|
||||||
|
/*
|
||||||
|
* @param
|
||||||
|
* @param
|
||||||
|
*/
|
||||||
SIndexMultiTerm *indexMultiTermCreate();
|
SIndexMultiTerm *indexMultiTermCreate();
|
||||||
int indexMultiTermAdd(SIndexMultiTerm *terms, const char *field, int32_t nFields, const char *value, int32_t nValue);
|
int indexMultiTermAdd(SIndexMultiTerm *terms, const char *field, int32_t nFields, const char *value, int32_t nValue);
|
||||||
void indexMultiTermDestroy(SIndexMultiTerm *terms);
|
void indexMultiTermDestroy(SIndexMultiTerm *terms);
|
||||||
|
|
|
@ -68,18 +68,39 @@ int indexPut(SIndex *index, SArray* field_vals, int uid) {
|
||||||
}
|
}
|
||||||
int indexSearch(SIndex *index, SIndexMultiTermQuery *multiQuerys, SArray *result) {
|
int indexSearch(SIndex *index, SIndexMultiTermQuery *multiQuerys, SArray *result) {
|
||||||
#ifdef USE_LUCENE
|
#ifdef USE_LUCENE
|
||||||
for (int i = 0; i < taosArrayGetSize(multiQuerys->query); i++) {
|
EIndexOperatorType opera = multiQuerys->opera;
|
||||||
|
|
||||||
|
int nQuery = taosArrayGetSize(multiQuerys->query);
|
||||||
|
char **fields = malloc(sizeof(char *) * nQuery);
|
||||||
|
char **keys = malloc(sizeof(char *) * nQuery);
|
||||||
|
int *types = malloc(sizeof(int) * nQuery);
|
||||||
|
|
||||||
|
for (int i = 0; i < nQuery; i++) {
|
||||||
SIndexTermQuery *p = taosArrayGet(multiQuerys->query, i);
|
SIndexTermQuery *p = taosArrayGet(multiQuerys->query, i);
|
||||||
SIndexTerm *term = p->field_value;
|
SIndexTerm *term = p->field_value;
|
||||||
EIndexQueryType qType = p->type;
|
|
||||||
int *tResult = NULL;
|
fields[i] = calloc(1, term->nKey + 1);
|
||||||
int32_t tsz = 0;
|
keys[i] = calloc(1, term->nVal + 1);
|
||||||
index_search(index->index, term->key, term->nKey, term->val, term->nVal, qType, &tResult, &tsz);
|
|
||||||
for (int i = 0; i < tsz; i++) {
|
memcpy(fields[i], term->key, term->nKey);
|
||||||
taosArrayPush(result, &(tResult[i]));
|
memcpy(keys[i], term->val, term->nVal);
|
||||||
}
|
types[i] = (int)(p->type);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
int *tResult = NULL;
|
||||||
|
int tsz= 0;
|
||||||
|
index_multi_search(index->index, (const char **)fields, (const char **)keys, types, nQuery, opera, &tResult, &tsz);
|
||||||
|
|
||||||
|
for (int i = 0; i < tsz; i++) {
|
||||||
|
taosArrayPush(result, &tResult[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < nQuery; i++) {
|
||||||
|
free(fields[i]);
|
||||||
|
free(keys[i]);
|
||||||
|
}
|
||||||
|
free(fields);
|
||||||
|
free(keys);
|
||||||
|
free(types);
|
||||||
#endif
|
#endif
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,17 +15,32 @@ TEST(IndexTest, index_create_test) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SArray* terms = indexMultiTermCreate();
|
|
||||||
indexMultiTermAdd(terms, "tag1", strlen("tag1"), "field", strlen("field"));
|
// write
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 100000; i++) {
|
||||||
indexPut(index, terms, i);
|
SIndexMultiTerm* terms = indexMultiTermCreate();
|
||||||
|
std::string val = "field";
|
||||||
|
|
||||||
|
indexMultiTermAdd(terms, "tag1", strlen("tag1"), val.c_str(), val.size());
|
||||||
|
|
||||||
|
val.append(std::to_string(i));
|
||||||
|
indexMultiTermAdd(terms, "tag2", strlen("tag2"), val.c_str(), val.size());
|
||||||
|
|
||||||
|
val.insert(0, std::to_string(i));
|
||||||
|
indexMultiTermAdd(terms, "tag3", strlen("tag3"), val.c_str(), val.size());
|
||||||
|
|
||||||
|
val.append("const");
|
||||||
|
indexMultiTermAdd(terms, "tag4", strlen("tag4"), val.c_str(), val.size());
|
||||||
|
|
||||||
|
indexPut(index, terms, i);
|
||||||
|
indexMultiTermDestroy(terms);
|
||||||
}
|
}
|
||||||
indexMultiTermDestroy(terms);
|
|
||||||
|
|
||||||
|
|
||||||
// query
|
// query
|
||||||
SIndexMultiTermQuery *multiQuery = indexMultiTermQueryCreate(MUST);
|
SIndexMultiTermQuery *multiQuery = indexMultiTermQueryCreate(MUST);
|
||||||
indexMultiTermQueryAdd(multiQuery, "tag1", strlen("tag1"), "field", strlen("field"), QUERY_PREFIX);
|
indexMultiTermQueryAdd(multiQuery, "tag1", strlen("tag1"), "field", strlen("field"), QUERY_PREFIX);
|
||||||
|
indexMultiTermQueryAdd(multiQuery, "tag3", strlen("tag3"), "0field0", strlen("0field0"), QUERY_TERM);
|
||||||
|
|
||||||
SArray *result = (SArray *)taosArrayInit(10, sizeof(int));
|
SArray *result = (SArray *)taosArrayInit(10, sizeof(int));
|
||||||
indexSearch(index, multiQuery, result);
|
indexSearch(index, multiQuery, result);
|
||||||
|
|
Loading…
Reference in New Issue