Beispiel #1
0
/******************************************************************************
 **函数名称: invtd_search_query
 **功    能: 从倒排表中搜索关键字
 **输入参数:
 **     ctx: 上下文
 **     req: 搜索请求信息
 **输出参数: NONE
 **返    回: 搜索结果(以XML树组织)
 **实现描述: 从倒排表中查询结果,并将结果以XML树组织.
 **注意事项: 完成发送后, 必须记得释放XML树的所有内存
 **作    者: # Qifeng.zou # 2016.01.04 17:35:35 #
 ******************************************************************************/
static xml_tree_t *invtd_search_query(invtd_cntx_t *ctx, mesg_search_req_t *req)
{
    xml_opt_t opt;
    xml_tree_t *xml;
    xml_node_t *root;
    invt_dic_word_t *word;

    memset(&opt, 0, sizeof(opt));

    /* > 构建XML树 */
    opt.pool = NULL;
    opt.alloc = mem_alloc;
    opt.dealloc = mem_dealloc;
    opt.log = ctx->log;

    xml = xml_empty(&opt);
    if (NULL == xml) {
        log_error(ctx->log, "Create xml failed!");
        return NULL;
    }

    root = xml_set_root(xml, "SEARCH-RSP");

    do {
        pthread_rwlock_rdlock(&ctx->invtab_lock);

        /* > 搜索倒排表 */
        word = (invt_dic_word_t *)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);
            if (invtd_search_no_data_hdl(xml)) {
                break;
            }
            return xml;
        }

        /* > 构建搜索结果 */
        if (list_trav(word->doc_list, (trav_cb_t)invtd_search_list_trav, (void *)xml)) {
            pthread_rwlock_unlock(&ctx->invtab_lock);
            log_error(ctx->log, "Contribute respone list failed! words:%s", req->words);
            break;
        }
        pthread_rwlock_unlock(&ctx->invtab_lock);

        xml_add_attr(xml, root, "CODE", SRCH_CODE_OK); /* 设置返回码 */
        return xml;
    } while(0);

    xml_destroy(xml);
    return NULL;
}
Beispiel #2
0
/******************************************************************************
 **函数名称: 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;
}