Skip to content

Commit

Permalink
mroonga_snippet_html: support query format as keywords
Browse files Browse the repository at this point in the history
    mroonga_snippet_html(column, '...query format...' AS query);

"AS query" is important.
  • Loading branch information
kou committed May 26, 2016
1 parent 01ea4a1 commit e9fc0bc
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
@@ -0,0 +1,9 @@
SET NAMES utf8;
SELECT mroonga_snippet_html('Mroongaには2つの動作モードがあります。

1つが「ストレージモード」で、データストアも検索機能もすべてGroongaを使うモードです。これがデフォルトのモードです。上述の参照ロックフリーなGroongaの性能特性をフルに活かした高速なデータ更新・全文検索・位置情報検索が特長です。一方、トランザクションなどの機能は提供されません。

もう1つが「ラッパーモード」で、MyISAMやInnoDBといった他のストレージエンジンに 全文検索機能だけ を追加するモードです。このモードではトランザクションなど他のストレージエンジンがサポートしている機能に加えてGroongaの高速な全文検索機能を利用することができます。一方、Groongaの参照ロックフリーな特性は活かすことができません。また、更新処理は他のストレージエンジンがボトルネックになることが多いでしょう。',
'ロック 更新 -ボトルネック' AS query) as snippet;
snippet
<div class="snippet">がデフォルトのモードです。上述の参照<span class="keyword">ロック</span>フリーなGroongaの性能特性をフルに活かした高速なデータ<span class="keyword">更新</span>・全文検索・位置情報検索が特長です。</div><div class="snippet">用することができます。一方、Groongaの参照<span class="keyword">ロック</span>フリーな特性は活かすことができません。また、<span class="keyword">更新</span>処理は他のストレージエンジンがボトルネッ</div>
31 changes: 31 additions & 0 deletions mysql-test/mroonga/storage/function/snippet_html/t/query.test
@@ -0,0 +1,31 @@
# Copyright(C) 2016 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

--source include/not_embedded.inc
--source ../../../../include/mroonga/have_mroonga.inc
--source ../../../../include/mroonga/load_mroonga_functions.inc

SET NAMES utf8;

SELECT mroonga_snippet_html('Mroongaには2つの動作モードがあります。

1つが「ストレージモード」で、データストアも検索機能もすべてGroongaを使うモードです。これがデフォルトのモードです。上述の参照ロックフリーなGroongaの性能特性をフルに活かした高速なデータ更新・全文検索・位置情報検索が特長です。一方、トランザクションなどの機能は提供されません。

もう1つが「ラッパーモード」で、MyISAMやInnoDBといった他のストレージエンジンに 全文検索機能だけ を追加するモードです。このモードではトランザクションなど他のストレージエンジンがサポートしている機能に加えてGroongaの高速な全文検索機能を利用することができます。一方、Groongaの参照ロックフリーな特性は活かすことができません。また、更新処理は他のストレージエンジンがボトルネックになることが多いでしょう。',
'ロック 更新 -ボトルネック' AS query) as snippet;

--source ../../../../include/mroonga/unload_mroonga_functions.inc
--source ../../../../include/mroonga/have_mroonga_deinit.inc
97 changes: 95 additions & 2 deletions udf/mrn_udf_snippet_html.cpp
@@ -1,6 +1,6 @@
/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Copyright(C) 2015 Kouhei Sutou <kou@clear-code.com>
Copyright(C) 2015-2016 Kouhei Sutou <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -40,6 +40,11 @@ typedef struct st_mrn_snippet_html_info
bool use_shared_db;
grn_obj *snippet;
String result_str;
struct {
bool used;
grn_obj *table;
grn_obj *default_column;
} query_mode;
} mrn_snippet_html_info;

static my_bool mrn_snippet_html_prepare(mrn_snippet_html_info *info,
Expand All @@ -56,6 +61,7 @@ static my_bool mrn_snippet_html_prepare(mrn_snippet_html_info *info,
const char *open_tag = "<span class=\"keyword\">";
const char *close_tag = "</span>";
grn_snip_mapping *mapping = GRN_SNIP_MAPPING_HTML_ESCAPE;
grn_obj *expr = NULL;
grn_rc rc;
String *result_str = &(info->result_str);

Expand All @@ -80,7 +86,73 @@ static my_bool mrn_snippet_html_prepare(mrn_snippet_html_info *info,
goto error;
}

{
if (info->query_mode.used) {
if (!info->query_mode.table) {
grn_obj *short_text;
short_text = grn_ctx_at(info->ctx, GRN_DB_SHORT_TEXT);
info->query_mode.table = grn_table_create(info->ctx,
NULL, 0, NULL,
GRN_TABLE_HASH_KEY,
short_text,
NULL);
}
if (!info->query_mode.default_column) {
info->query_mode.default_column =
grn_obj_column(info->ctx,
info->query_mode.table,
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN);
}

grn_obj *record = NULL;
GRN_EXPR_CREATE_FOR_QUERY(info->ctx, info->query_mode.table, expr, record);
if (!expr) {
if (message) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"mroonga_snippet_html(): "
"failed to create expression: <%s>",
ctx->errbuf);
}
goto error;
}

// TODO: Use the same flags get logic in
// ha_mroonga::generic_ft_init_ext_parse_pragma().
grn_expr_flags flags = GRN_EXPR_SYNTAX_QUERY | GRN_EXPR_ALLOW_LEADING_NOT;
rc = grn_expr_parse(info->ctx,
expr,
args->args[1],
args->lengths[1],
info->query_mode.default_column,
GRN_OP_MATCH,
GRN_OP_OR,
flags);
if (rc != GRN_SUCCESS) {
if (message) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"mroonga_snippet_html(): "
"failed to parse query: <%s>",
ctx->errbuf);
}
goto error;
}

rc = grn_expr_snip_add_conditions(info->ctx,
expr,
*snippet,
0,
NULL, NULL,
NULL, NULL);
if (rc != GRN_SUCCESS) {
if (message) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"mroonga_snippet_html(): "
"failed to add conditions: <%s>",
ctx->errbuf);
}
goto error;
}
} else {
unsigned int i;
for (i = 1; i < args->arg_count; ++i) {
if (!args->args[i]) {
Expand All @@ -106,6 +178,9 @@ static my_bool mrn_snippet_html_prepare(mrn_snippet_html_info *info,
DBUG_RETURN(FALSE);

error:
if (expr) {
grn_obj_close(ctx, expr);
}
if (*snippet) {
grn_obj_close(ctx, *snippet);
}
Expand Down Expand Up @@ -195,6 +270,16 @@ MRN_API my_bool mroonga_snippet_html_init(UDF_INIT *init,
}
}

info->query_mode.used = FALSE;

if (args->arg_count == 2 &&
args->attribute_lengths[1] == strlen("query") &&
strncmp(args->attributes[1], "query", strlen("query")) == 0) {
info->query_mode.used = TRUE;
info->query_mode.table = NULL;
info->query_mode.default_column = NULL;
}

{
bool all_keywords_are_constant = TRUE;
for (unsigned int i = 1; i < args->arg_count; ++i) {
Expand Down Expand Up @@ -341,6 +426,14 @@ MRN_API void mroonga_snippet_html_deinit(UDF_INIT *init)
if (info->snippet) {
grn_obj_close(info->ctx, info->snippet);
}
if (info->query_mode.used) {
if (info->query_mode.default_column) {
grn_obj_close(info->ctx, info->query_mode.default_column);
}
if (info->query_mode.table) {
grn_obj_close(info->ctx, info->query_mode.table);
}
}
MRN_STRING_FREE(info->result_str);
if (!info->use_shared_db) {
grn_obj_close(info->ctx, info->db);
Expand Down

0 comments on commit e9fc0bc

Please sign in to comment.