Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #47 from naoa/support-index-column-flags
Browse files Browse the repository at this point in the history
Support specifying index column flags by index comment

Patch by Naoya Murakami. Thanks!!!
  • Loading branch information
kou committed Mar 13, 2015
2 parents 706faaa + 6abec87 commit 2dd816a
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 8 deletions.
90 changes: 82 additions & 8 deletions ha_mroonga.cpp
Expand Up @@ -1357,6 +1357,52 @@ grn_obj_flags mrn_parse_grn_column_create_flags(THD *thd,
return flags;
}

bool mrn_parse_grn_index_column_flags(THD *thd,
grn_ctx *ctx,
const char *flag_names,
uint flag_names_length,
grn_obj_flags *index_column_flags)
{
const char *flag_names_end = flag_names + flag_names_length;
bool found = false;

while (flag_names < flag_names_end) {
uint rest_length = flag_names_end - flag_names;

if (*flag_names == '|' || *flag_names == ' ') {
flag_names += 1;
continue;
}
if (rest_length >= 4 && !memcmp(flag_names, "NONE", 4)) {
flag_names += 4;
found = true;
} else if (rest_length >= 13 && !memcmp(flag_names, "WITH_POSITION", 13)) {
*index_column_flags |= GRN_OBJ_WITH_POSITION;
flag_names += 13;
found = true;
} else if (rest_length >= 12 && !memcmp(flag_names, "WITH_SECTION", 12)) {
*index_column_flags |= GRN_OBJ_WITH_SECTION;
flag_names += 12;
found = true;
} else if (rest_length >= 11 && !memcmp(flag_names, "WITH_WEIGHT", 11)) {
*index_column_flags |= GRN_OBJ_WITH_WEIGHT;
flag_names += 11;
found = true;
} else {
char invalid_flag_name[MRN_MESSAGE_BUFFER_SIZE];
snprintf(invalid_flag_name, MRN_MESSAGE_BUFFER_SIZE,
"%.*s",
static_cast<int>(rest_length),
flag_names);
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
ER_MRN_INVALID_INDEX_FLAG_NUM,
ER_MRN_INVALID_INDEX_FLAG_STR,
invalid_flag_name);
}
}
return found;
}

#ifdef HAVE_SPATIAL
static int mrn_set_geometry(grn_ctx *ctx, grn_obj *buf,
const char *wkb, uint wkb_size)
Expand Down Expand Up @@ -2739,10 +2785,13 @@ int ha_mroonga::wrapper_create_index_fulltext(const char *grn_table_name,
GRN_OBJ_PERSISTENT;
grn_obj *index_table;

grn_obj_flags index_column_flags =
GRN_OBJ_COLUMN_INDEX | GRN_OBJ_WITH_POSITION | GRN_OBJ_PERSISTENT;
if (KEY_N_KEY_PARTS(key_info) > 1) {
index_column_flags |= GRN_OBJ_WITH_SECTION;
grn_obj_flags index_column_flags = GRN_OBJ_COLUMN_INDEX | GRN_OBJ_PERSISTENT;

if (!find_index_column_flags(key_info, &index_column_flags)) {
index_column_flags |= GRN_OBJ_WITH_POSITION;
if (KEY_N_KEY_PARTS(key_info) > 1) {
index_column_flags |= GRN_OBJ_WITH_SECTION;
}
}

mrn::SmartGrnObj lexicon_key_type(ctx, GRN_DB_SHORT_TEXT);
Expand Down Expand Up @@ -3544,10 +3593,13 @@ int ha_mroonga::storage_create_index(TABLE *table, const char *grn_table_name,
if (error)
DBUG_RETURN(error);

grn_obj_flags index_column_flags =
GRN_OBJ_COLUMN_INDEX | GRN_OBJ_WITH_POSITION | GRN_OBJ_PERSISTENT;
if (is_multiple_column_index) {
index_column_flags |= GRN_OBJ_WITH_SECTION;
grn_obj_flags index_column_flags = GRN_OBJ_COLUMN_INDEX | GRN_OBJ_PERSISTENT;

if (!find_index_column_flags(key_info, &index_column_flags)) {
index_column_flags |= GRN_OBJ_WITH_POSITION;
if (is_multiple_column_index) {
index_column_flags |= GRN_OBJ_WITH_SECTION;
}
}

index_table = index_tables[i];
Expand Down Expand Up @@ -8674,6 +8726,28 @@ grn_obj *ha_mroonga::find_normalizer(KEY *key_info)
DBUG_RETURN(normalizer);
}

bool ha_mroonga::find_index_column_flags(KEY *key_info, grn_obj_flags *index_column_flags)
{
MRN_DBUG_ENTER_METHOD();
bool found = false;
#if MYSQL_VERSION_ID >= 50500
if (key_info->comment.length > 0) {
mrn::ParametersParser parser(key_info->comment.str,
key_info->comment.length);
parser.parse();
const char *names = parser["index_flags"];
if (names) {
found = mrn_parse_grn_index_column_flags(ha_thd(),
ctx,
names,
strlen(names),
index_column_flags);
}
}
#endif
DBUG_RETURN(found);
}

bool ha_mroonga::find_token_filters(KEY *key_info, grn_obj *token_filters)
{
MRN_DBUG_ENTER_METHOD();
Expand Down
1 change: 1 addition & 0 deletions ha_mroonga.hpp
Expand Up @@ -574,6 +574,7 @@ class ha_mroonga: public handler
int drop_index(MRN_SHARE *target_share, uint key_index);
grn_obj *find_tokenizer(const char *name, int name_length);
grn_obj *find_normalizer(KEY *key_info);
bool find_index_column_flags(KEY *key_info, grn_obj_flags *index_column_flags);
bool find_token_filters(KEY *key_info, grn_obj *token_filters);
bool find_token_filters_put(grn_obj *token_filters,
const char *token_filter_name,
Expand Down
3 changes: 3 additions & 0 deletions mrn_err.h
Expand Up @@ -34,5 +34,8 @@
#define ER_MRN_INVALID_COLUMN_FLAG_NUM 16507
#define ER_MRN_INVALID_COLUMN_FLAG_STR \
"The column flag '%-.64s' is invalid. '%-64s' is used instead"
#define ER_MRN_INVALID_INDEX_FLAG_NUM 16508
#define ER_MRN_INVALID_INDEX_FLAG_STR \
"The index flag '%-.64s' is invalid. It is ignored"

#endif /* MRN_ERR_H_ */
12 changes: 12 additions & 0 deletions mysql-test/mroonga/storage/create/table/index_flags/r/none.result
@@ -0,0 +1,12 @@
SET NAMES utf8;
CREATE TABLE memos (
content VARCHAR(64) NOT NULL,
FULLTEXT INDEX (content) COMMENT 'index_flags "NONE"'
) DEFAULT CHARSET=utf8;
SELECT mroonga_command("dump");
mroonga_command("dump")
table_create memos TABLE_NO_KEY
column_create memos content COLUMN_SCALAR ShortText
table_create memos-content TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerMySQLGeneralCI
column_create memos-content index COLUMN_INDEX memos content
DROP TABLE memos;
@@ -0,0 +1,12 @@
SET NAMES utf8;
CREATE TABLE memos (
content VARCHAR(64) NOT NULL,
FULLTEXT INDEX (content) COMMENT 'index_flags "WITH_POSITION|WITH_WEIGHT"'
) DEFAULT CHARSET=utf8;
SELECT mroonga_command("dump");
mroonga_command("dump")
table_create memos TABLE_NO_KEY
column_create memos content COLUMN_SCALAR ShortText
table_create memos-content TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerMySQLGeneralCI
column_create memos-content index COLUMN_INDEX|WITH_WEIGHT|WITH_POSITION memos content
DROP TABLE memos;
38 changes: 38 additions & 0 deletions mysql-test/mroonga/storage/create/table/index_flags/t/none.test
@@ -0,0 +1,38 @@
# Copyright(C) 2015 Naoya Murakami <naoya@createfield.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/mroonga/have_mroonga.inc
--source ../../../../../include/mroonga/load_mroonga_functions.inc

--disable_query_log
DROP DATABASE test;
CREATE DATABASE test;
USE test;
--enable_query_log

SET NAMES utf8;

CREATE TABLE memos (
content VARCHAR(64) NOT NULL,
FULLTEXT INDEX (content) COMMENT 'index_flags "NONE"'
) DEFAULT CHARSET=utf8;

SELECT mroonga_command("dump");

DROP TABLE memos;

--source ../../../../../include/mroonga/unload_mroonga_functions.inc
--source ../../../../../include/mroonga/have_mroonga_deinit.inc
@@ -0,0 +1,38 @@
# Copyright(C) 2015 Naoya Murakami <naoya@createfield.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/mroonga/have_mroonga.inc
--source ../../../../../include/mroonga/load_mroonga_functions.inc

--disable_query_log
DROP DATABASE test;
CREATE DATABASE test;
USE test;
--enable_query_log

SET NAMES utf8;

CREATE TABLE memos (
content VARCHAR(64) NOT NULL,
FULLTEXT INDEX (content) COMMENT 'index_flags "WITH_POSITION|WITH_WEIGHT"'
) DEFAULT CHARSET=utf8;

SELECT mroonga_command("dump");

DROP TABLE memos;

--source ../../../../../include/mroonga/unload_mroonga_functions.inc
--source ../../../../../include/mroonga/have_mroonga_deinit.inc
11 changes: 11 additions & 0 deletions mysql-test/mroonga/wrapper/create/table/index_flags/r/none.result
@@ -0,0 +1,11 @@
SET NAMES utf8;
CREATE TABLE memos (
content VARCHAR(64) NOT NULL PRIMARY KEY,
FULLTEXT INDEX (content) COMMENT 'index_flags "NONE"'
) COMMENT='engine "InnoDB"' DEFAULT CHARSET=utf8;
SELECT mroonga_command("dump");
mroonga_command("dump")
table_create memos TABLE_HASH_KEY ShortText
table_create memos-content TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerMySQLGeneralCI
column_create memos-content index COLUMN_INDEX memos
DROP TABLE memos;
@@ -0,0 +1,11 @@
SET NAMES utf8;
CREATE TABLE memos (
content VARCHAR(64) NOT NULL PRIMARY KEY,
FULLTEXT INDEX (content) COMMENT 'index_flags "WITH_POSITION|WITH_WEIGHT"'
) COMMENT='engine "InnoDB"' DEFAULT CHARSET=utf8;
SELECT mroonga_command("dump");
mroonga_command("dump")
table_create memos TABLE_HASH_KEY ShortText
table_create memos-content TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerMySQLGeneralCI
column_create memos-content index COLUMN_INDEX|WITH_WEIGHT|WITH_POSITION memos
DROP TABLE memos;
39 changes: 39 additions & 0 deletions mysql-test/mroonga/wrapper/create/table/index_flags/t/none.test
@@ -0,0 +1,39 @@
# Copyright(C) 2015 Naoya Murakami <naoya@createfield.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/have_innodb.inc
--source ../../../../../include/mroonga/have_mroonga.inc
--source ../../../../../include/mroonga/load_mroonga_functions.inc

--disable_query_log
DROP DATABASE test;
CREATE DATABASE test;
USE test;
--enable_query_log

SET NAMES utf8;

CREATE TABLE memos (
content VARCHAR(64) NOT NULL PRIMARY KEY,
FULLTEXT INDEX (content) COMMENT 'index_flags "NONE"'
) COMMENT='engine "InnoDB"' DEFAULT CHARSET=utf8;

SELECT mroonga_command("dump");

DROP TABLE memos;

--source ../../../../../include/mroonga/unload_mroonga_functions.inc
--source ../../../../../include/mroonga/have_mroonga_deinit.inc
@@ -0,0 +1,39 @@
# Copyright(C) 2015 Naoya Murakami <naoya@createfield.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/have_innodb.inc
--source ../../../../../include/mroonga/have_mroonga.inc
--source ../../../../../include/mroonga/load_mroonga_functions.inc

--disable_query_log
DROP DATABASE test;
CREATE DATABASE test;
USE test;
--enable_query_log

SET NAMES utf8;

CREATE TABLE memos (
content VARCHAR(64) NOT NULL PRIMARY KEY,
FULLTEXT INDEX (content) COMMENT 'index_flags "WITH_POSITION|WITH_WEIGHT"'
) COMMENT='engine "InnoDB"' DEFAULT CHARSET=utf8;

SELECT mroonga_command("dump");

DROP TABLE memos;

--source ../../../../../include/mroonga/unload_mroonga_functions.inc
--source ../../../../../include/mroonga/have_mroonga_deinit.inc

0 comments on commit 2dd816a

Please sign in to comment.