opti:json parser

This commit is contained in:
wangmm0220 2022-12-15 17:17:18 +08:00
parent 5ec92e7dc4
commit 284acb1f34
3 changed files with 84 additions and 3 deletions

View File

@ -178,6 +178,7 @@ typedef struct {
SSmlMsgBuf msgBuf; SSmlMsgBuf msgBuf;
// cJSON *root; // for parse json // cJSON *root; // for parse json
int8_t offset[4];
SSmlLineInfo *lines; // element is SSmlLineInfo SSmlLineInfo *lines; // element is SSmlLineInfo
// //

View File

@ -233,12 +233,19 @@ static char* smlJsonGetObj(char *payload){
return NULL; return NULL;
} }
static inline void smlJsonParseObj(char **start, SSmlLineInfo *element){ static inline void smlJsonParseObjFirst(char **start, SSmlLineInfo *element, int8_t *offset){
int index = 0;
while(*(*start)){ while(*(*start)){
if((*start)[0] != '"'){ if((*start)[0] != '"'){
(*start)++; (*start)++;
continue; continue;
} }
if(unlikely(index >= 4)) {
uError("index >= 4, %s", *start)
break;
}
char *sTmp = *start;
if((*start)[1] == 'm' && (*start)[2] == 'e' && (*start)[3] == 't' if((*start)[1] == 'm' && (*start)[2] == 'e' && (*start)[3] == 't'
&& (*start)[4] == 'r' && (*start)[5] == 'i' && (*start)[6] == 'c' && (*start)[7] == '"'){ && (*start)[4] == 'r' && (*start)[5] == 'i' && (*start)[6] == 'c' && (*start)[7] == '"'){
@ -247,6 +254,7 @@ static inline void smlJsonParseObj(char **start, SSmlLineInfo *element){
while(*(*start)){ while(*(*start)){
if(unlikely(!isInQuote && *(*start) == '"')){ if(unlikely(!isInQuote && *(*start) == '"')){
(*start)++; (*start)++;
offset[index++] = *start - sTmp;
element->measure = (*start); element->measure = (*start);
isInQuote = true; isInQuote = true;
continue; continue;
@ -267,6 +275,7 @@ static inline void smlJsonParseObj(char **start, SSmlLineInfo *element){
if(unlikely(!hasColon && *(*start) == ':')){ if(unlikely(!hasColon && *(*start) == ':')){
(*start)++; (*start)++;
JUMP_JSON_SPACE((*start)) JUMP_JSON_SPACE((*start))
offset[index++] = *start - sTmp;
element->timestamp = (*start); element->timestamp = (*start);
hasColon = true; hasColon = true;
continue; continue;
@ -287,6 +296,7 @@ static inline void smlJsonParseObj(char **start, SSmlLineInfo *element){
if(unlikely(!hasColon && *(*start) == ':')){ if(unlikely(!hasColon && *(*start) == ':')){
(*start)++; (*start)++;
JUMP_JSON_SPACE((*start)) JUMP_JSON_SPACE((*start))
offset[index++] = *start - sTmp;
element->cols = (*start); element->cols = (*start);
hasColon = true; hasColon = true;
continue; continue;
@ -305,6 +315,7 @@ static inline void smlJsonParseObj(char **start, SSmlLineInfo *element){
if(unlikely(*(*start) == ':')){ if(unlikely(*(*start) == ':')){
(*start)++; (*start)++;
JUMP_JSON_SPACE((*start)) JUMP_JSON_SPACE((*start))
offset[index++] = *start - sTmp;
element->tags = (*start); element->tags = (*start);
char* tmp = smlJsonGetObj((*start)); char* tmp = smlJsonGetObj((*start));
if(tmp){ if(tmp){
@ -324,10 +335,74 @@ static inline void smlJsonParseObj(char **start, SSmlLineInfo *element){
} }
} }
static inline void smlJsonParseObj(char **start, SSmlLineInfo *element, int8_t *offset){
int index = 0;
while(*(*start)){
if((*start)[0] != '"'){
(*start)++;
continue;
}
if(unlikely(index >= 4)) {
uError("index >= 4, %s", *start)
break;
}
if((*start)[1] == 'm'){
(*start) += offset[index++];
element->measure = *start;
while(*(*start)){
if(unlikely(*(*start) == '"')){
element->measureLen = (*start) - element->measure;
break;
}
(*start)++;
}
}else if((*start)[1] == 't' && (*start)[2] == 'i'){
(*start) += offset[index++];
element->timestamp = *start;
while(*(*start)){
if(unlikely(*(*start) == ',' || *(*start) == '}' || (*(*start)) <= 32)){
element->timestampLen = (*start) - element->timestamp;
break;
}
(*start)++;
}
}else if((*start)[1] == 'v'){
(*start) += offset[index++];
element->cols = *start;
while(*(*start)){
if(unlikely( *(*start) == ',' || *(*start) == '}' || (*(*start)) <= 32)){
element->colsLen = (*start) - element->cols;
break;
}
(*start)++;
}
}else if((*start)[1] == 't' && (*start)[2] == 'a'){
(*start) += offset[index++];
element->tags = (*start);
char* tmp = smlJsonGetObj((*start));
if(tmp){
element->tagsLen = tmp - (*start);
*start = tmp;
}
break;
}
if(*(*start) == '}'){
(*start)++;
break;
}
(*start)++;
}
}
static int32_t smlParseJSONString(SSmlHandle *info, char **start, SSmlLineInfo *elements) { static int32_t smlParseJSONString(SSmlHandle *info, char **start, SSmlLineInfo *elements) {
int32_t ret = TSDB_CODE_SUCCESS; int32_t ret = TSDB_CODE_SUCCESS;
smlJsonParseObj(start, elements); if(info->offset[0] == 0){
smlJsonParseObjFirst(start, elements, info->offset);
}else{
smlJsonParseObj(start, elements, info->offset);
}
if(**start == '\0') return TSDB_CODE_SUCCESS; if(**start == '\0') return TSDB_CODE_SUCCESS;
SSmlKv kv = {.key = VALUE, .keyLen = VALUE_LEN, .value = elements->cols, .length = (size_t)elements->colsLen}; SSmlKv kv = {.key = VALUE, .keyLen = VALUE_LEN, .value = elements->cols, .length = (size_t)elements->colsLen};

View File

@ -497,9 +497,14 @@ TEST(testCase, smlParseTelnetLine_diff_json_type2_Test) {
}; };
for (int i = 0; i < sizeof(sql) / sizeof(sql[0]); i++) { for (int i = 0; i < sizeof(sql) / sizeof(sql[0]); i++) {
char *dataPointStart = (char *)sql[i]; char *dataPointStart = (char *)sql[i];
int8_t offset[4] = {0};
while (1) { while (1) {
SSmlLineInfo elements = {0}; SSmlLineInfo elements = {0};
smlJsonParseObj(&dataPointStart, &elements); if(offset[0] == 0){
smlJsonParseObjFirst(&dataPointStart, &elements, offset);
}else{
smlJsonParseObj(&dataPointStart, &elements, offset);
}
if(*dataPointStart == '\0') break; if(*dataPointStart == '\0') break;
SArray *tags = smlJsonParseTags(elements.tags, elements.tags + elements.tagsLen); SArray *tags = smlJsonParseTags(elements.tags, elements.tags + elements.tagsLen);