/****************************************************************************** **函数名称: xml_screat **功 能: 将XML字串转为XML树 **输入参数: ** str: XML字串 ** len: 字串长度限制(-1:表示遇到\0结束) ** opt: 选项信息 **输出参数: **返 回: XML树 **实现描述: ** 1. 初始化栈 ** 2. 初始化xml树 ** 3. 在内存中将文件解析为XML树 **注意事项: **作 者: # Qifeng.zou # 2013.02.05 # ******************************************************************************/ xml_tree_t *xml_screat(const char *str, size_t len, xml_opt_t *opt) { Stack_t stack; xml_tree_t *xml; if ((NULL == str) || ('\0' == str[0])) { return xml_creat_empty(opt); } do { /* 1. 初始化栈 */ if (stack_init(&stack, XML_MAX_DEPTH)) { log_error(opt->log, "Init xml stack failed!"); break; } /* 2. 初始化XML树 */ xml = xml_init(opt); if (NULL == xml) { log_error(opt->log, "Init xml tree failed!"); break; } /* 3. 解析XML文件缓存 */ if (xml_parse(xml, &stack, str, len)) { log_error(xml->log, "Parse xml failed!"); xml_destroy(xml); break; } stack_destroy(&stack); return xml; } while(0); /* 4. 释放内存空间 */ stack_destroy(&stack); return NULL; }
/****************************************************************************** **函数名称: mon_srch_set_body **功 能: 设置搜索报体 **输入参数: ** word: 搜索信息 ** size: 搜索请求的最大报体 **输出参数: NONE ** body: 搜索请求的报体 **返 回: 报体实际长度 **实现描述: **注意事项: **作 者: # Qifeng.zou # 2015.12.27 03:01:48 # ******************************************************************************/ static int mon_srch_set_body(const char *words, char *body, int size) { int len; xml_opt_t opt; xml_tree_t *xml; xml_node_t *node; memset(&opt, 0, sizeof(opt)); /* > 创建XML树 */ opt.log = NULL; opt.pool = NULL; opt.alloc = mem_alloc; opt.dealloc = mem_dealloc; xml = xml_creat_empty(&opt); if (NULL == xml) { fprintf(stderr, "Create xml failed!"); return -1; } node = xml_add_child(xml, xml->root, "SEARCH", NULL); xml_add_attr(xml, node, "WORDS", words); /* > 计算XML长度 */ len = xml_pack_len(xml); if (len >= size) { xml_destroy(xml); return -1; } /* > 输出XML至缓存 */ len = xml_spack(xml, body); xml_destroy(xml); return len; }
/****************************************************************************** **函数名称: invtd_search_word_query **功 能: 从倒排表中搜索关键字 **输入参数: ** ctx: 上下文 ** req: 搜索请求信息 **输出参数: NONE **返 回: 搜索结果(以XML树组织) **实现描述: 从倒排表中查询结果,并将结果以XML树组织. **注意事项: 完成发送后, 必须记得释放XML树的所有内存 **作 者: # Qifeng.zou # 2016.01.04 17:35:35 # ******************************************************************************/ static xml_tree_t *invtd_search_word_query(invtd_cntx_t *ctx, mesg_search_word_req_t *req) { int idx; xml_opt_t opt; xml_tree_t *xml; xml_node_t *root, *item; list_node_t *node; invt_word_doc_t *doc; invt_dic_word_t *word; char freq[SRCH_SEG_FREQ_LEN]; memset(&opt, 0, sizeof(opt)); /* > 构建XML树 */ opt.pool = NULL; opt.alloc = mem_alloc; opt.dealloc = mem_dealloc; opt.log = ctx->log; xml = xml_creat_empty(&opt); if (NULL == xml) { log_error(ctx->log, "Create xml failed!"); return NULL; } root = xml_set_root(xml, "SEARCH-RSP"); if (NULL == root) { log_error(ctx->log, "Set xml root failed!"); goto GOTO_SEARCH_ERR; } /* > 搜索倒排表 */ pthread_rwlock_rdlock(&ctx->invtab_lock); word = invtab_query(ctx->invtab, req->words); if (NULL == word || NULL == word->doc_list) { pthread_rwlock_unlock(&ctx->invtab_lock); log_warn(ctx->log, "Didn't search anything! words:%s", req->words); goto GOTO_SEARCH_NO_DATA; } /* > 构建搜索结果 */ idx = 0; node = word->doc_list->head; for (; NULL!=node; node=node->next, ++idx) { doc = (invt_word_doc_t *)node->data; snprintf(freq, sizeof(freq), "%d", doc->freq); item = xml_add_child(xml, root, "ITEM", NULL); if (NULL == item) { goto GOTO_SEARCH_ERR; } xml_add_attr(xml, item, "URL", doc->url.str); xml_add_attr(xml, item, "FREQ", freq); } pthread_rwlock_unlock(&ctx->invtab_lock); xml_add_attr(xml, root, "CODE", SRCH_CODE_OK); /* 设置返回码 */ return xml; GOTO_SEARCH_ERR: /* 异常错误处理 */ xml_destroy(xml); return NULL; GOTO_SEARCH_NO_DATA: /* 搜索结果为空的处理 */ xml_add_attr(xml, root, "CODE", SRCH_CODE_NO_DATA); /* 无数据 */ snprintf(freq, sizeof(freq), "%d", 0); item = xml_add_child(xml, root, "ITEM", NULL); if (NULL == item) { goto GOTO_SEARCH_ERR; } xml_add_attr(xml, item, "URL", "Sorry, Didn't search anything!"); xml_add_attr(xml, item, "FREQ", freq); return xml; }