Navigation Menu

Skip to content

Commit

Permalink
wrapper: support table level "flags" option
Browse files Browse the repository at this point in the history
You can specify KEY_LARGE table option.

[groonga-dev,04494]

Reported by Masanori Miyashita. Thanks!!!
  • Loading branch information
kou committed Oct 16, 2017
1 parent 9a3c532 commit dba406d
Show file tree
Hide file tree
Showing 13 changed files with 401 additions and 21 deletions.
147 changes: 128 additions & 19 deletions ha_mroonga.cpp
Expand Up @@ -1564,6 +1564,54 @@ static grn_builtin_type mrn_grn_type_from_field(grn_ctx *ctx, Field *field,
return type;
}

static bool mrn_parse_grn_table_create_flags(THD *thd,
grn_ctx *ctx,
const char *flag_names,
uint flag_names_length,
grn_table_flags *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 >= 14 && !memcmp(flag_names, "TABLE_HASH_KEY", 14)) {
*flags |= GRN_OBJ_TABLE_HASH_KEY;
flag_names += 14;
found = true;
} else if (rest_length >= 13 && !memcmp(flag_names, "TABLE_PAT_KEY", 13)) {
*flags |= GRN_OBJ_TABLE_PAT_KEY;
flag_names += 13;
found = true;
} else if (rest_length >= 13 && !memcmp(flag_names, "TABLE_DAT_KEY", 13)) {
*flags |= GRN_OBJ_TABLE_DAT_KEY;
flag_names += 13;
found = true;
} else if (rest_length >= 9 && !memcmp(flag_names, "KEY_LARGE", 9)) {
*flags |= GRN_OBJ_KEY_LARGE;
flag_names += 9;
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, MRN_SEVERITY_WARNING,
ER_MRN_INVALID_TABLE_FLAG_NUM,
ER_MRN_INVALID_TABLE_FLAG_STR,
invalid_flag_name);
break;
}
}
return found;
}

static bool mrn_parse_grn_column_create_flags(THD *thd,
grn_ctx *ctx,
const char *flag_names,
Expand Down Expand Up @@ -1766,6 +1814,17 @@ static uint mrn_alter_table_flags(uint flags)
#endif

#ifdef MRN_SUPPORT_CUSTOM_OPTIONS
struct ha_table_option_struct
{
const char *flags;
};

static ha_create_table_option mrn_table_options[] =
{
HA_TOPTION_STRING("FLAGS", flags),
HA_TOPTION_END
};

static ha_create_table_option mrn_field_options[] =
{
HA_FOPTION_STRING("GROONGA_TYPE", groonga_type),
Expand Down Expand Up @@ -1801,6 +1860,7 @@ static int mrn_init(void *p)
hton->alter_table_flags = mrn_alter_table_flags;
#endif
#ifdef MRN_SUPPORT_CUSTOM_OPTIONS
hton->table_options = mrn_table_options;
hton->field_options = mrn_field_options;
hton->index_options = mrn_index_options;
#endif
Expand Down Expand Up @@ -3081,7 +3141,7 @@ int ha_mroonga::wrapper_create(const char *name, TABLE *table,
if (error)
DBUG_RETURN(error);

error = wrapper_create_index(name, table, tmp_share);
error = wrapper_create_index(name, table, info, tmp_share);
if (error)
DBUG_RETURN(error);

Expand Down Expand Up @@ -3329,7 +3389,9 @@ int ha_mroonga::wrapper_create_index_geo(const char *grn_table_name,
DBUG_RETURN(error);
}

int ha_mroonga::wrapper_create_index(const char *name, TABLE *table,
int ha_mroonga::wrapper_create_index(const char *name,
TABLE *table,
HA_CREATE_INFO *info,
MRN_SHARE *tmp_share)
{
MRN_DBUG_ENTER_METHOD();
Expand All @@ -3339,26 +3401,33 @@ int ha_mroonga::wrapper_create_index(const char *name, TABLE *table,
if (error)
DBUG_RETURN(error);

grn_obj *grn_index_table;
mrn::PathMapper mapper(name);
const char *grn_table_name = mapper.table_name();
char *grn_table_path = NULL; // we don't specify path
grn_obj *pkey_type = grn_ctx_at(ctx, GRN_DB_SHORT_TEXT);
grn_obj *pkey_value_type = NULL; // we don't use this
grn_obj_flags grn_table_flags = GRN_OBJ_PERSISTENT | GRN_OBJ_TABLE_HASH_KEY;

grn_index_table = grn_table_create(ctx, grn_table_name, strlen(grn_table_name),
grn_table_path, grn_table_flags,
pkey_type, pkey_value_type);
if (ctx->rc) {
error = ER_CANT_CREATE_TABLE;
my_message(error, ctx->errbuf, MYF(0));
DBUG_RETURN(error);
}
if (grn_table) {
grn_obj_unlink(ctx, grn_table);
{
char *path = NULL; // we don't specify path
grn_obj *pkey_type = grn_ctx_at(ctx, GRN_DB_SHORT_TEXT);
grn_obj *pkey_value_type = NULL; // we don't use this
grn_table_flags flags = GRN_OBJ_PERSISTENT;
if (!find_table_flags(info, tmp_share, &flags)) {
flags |= GRN_OBJ_TABLE_HASH_KEY;
}
grn_obj *table = grn_table_create(ctx,
grn_table_name, strlen(grn_table_name),
path,
flags,
pkey_type,
pkey_value_type);
if (ctx->rc) {
error = ER_CANT_CREATE_TABLE;
my_message(error, ctx->errbuf, MYF(0));
DBUG_RETURN(error);
}
if (grn_table) {
grn_obj_unlink(ctx, grn_table);
}
grn_table = table;
}
grn_table = grn_index_table;

uint i;
uint n_keys = table->s->keys;
Expand Down Expand Up @@ -9509,6 +9578,39 @@ int ha_mroonga::drop_indexes(const char *table_name)
DBUG_RETURN(error);
}

bool ha_mroonga::find_table_flags(HA_CREATE_INFO *info,
MRN_SHARE *mrn_share,
grn_table_flags *flags)
{
MRN_DBUG_ENTER_METHOD();
bool found = false;

#ifdef MRN_SUPPORT_CUSTOM_OPTIONS
if (info->option_struct) {
const char *names = info->option_struct->flags;
if (names) {
found = mrn_parse_grn_table_create_flags(ha_thd(),
ctx,
names,
strlen(names),
flags);
DBUG_RETURN(found);
}
}
#endif

if (mrn_share->table_flags) {
found = mrn_parse_grn_table_create_flags(ha_thd(),
ctx,
mrn_share->table_flags,
mrn_share->table_flags_length,
flags);
DBUG_RETURN(found);
}

DBUG_RETURN(found);
}

bool ha_mroonga::find_column_flags(Field *field, MRN_SHARE *mrn_share, int i,
grn_obj_flags *column_flags)
{
Expand Down Expand Up @@ -14101,7 +14203,14 @@ int ha_mroonga::wrapper_recreate_indexes(THD *thd)

mrn_set_bitmap_by_key(table->read_set, &key_info[i]);
}
error = wrapper_create_index(table_share->normalized_path.str, table, share);
HA_CREATE_INFO info;
#ifdef MRN_SUPPORT_CUSTOM_OPTIONS
info.option_struct = table_share->option_struct;
#endif
error = wrapper_create_index(table_share->normalized_path.str,
table,
&info,
share);
if (error)
DBUG_RETURN(error);
error = wrapper_open_indexes(table_share->normalized_path.str);
Expand Down
8 changes: 7 additions & 1 deletion ha_mroonga.hpp
Expand Up @@ -672,6 +672,9 @@ class ha_mroonga: public handler
int drop_indexes_multiple(const char *table_name, grn_obj *table,
const char *index_table_name_separator);
int drop_indexes(const char *table_name);
bool find_table_flags(HA_CREATE_INFO *info,
MRN_SHARE *mrn_share,
grn_table_flags *table_flags);
bool find_column_flags(Field *field, MRN_SHARE *mrn_share, int i,
grn_obj_flags *column_flags);
grn_obj *find_column_type(Field *field, MRN_SHARE *mrn_share, int i,
Expand Down Expand Up @@ -851,7 +854,10 @@ class ha_mroonga: public handler
grn_obj **index_tables,
grn_obj **index_columns,
MRN_SHARE *tmp_share);
int wrapper_create_index(const char *name, TABLE *table, MRN_SHARE *tmp_share);
int wrapper_create_index(const char *name,
TABLE *table,
HA_CREATE_INFO *info,
MRN_SHARE *tmp_share);
int storage_create_validate_pseudo_column(TABLE *table);
#ifdef MRN_SUPPORT_FOREIGN_KEYS
bool storage_create_foreign_key(TABLE *table, const char *grn_table_name,
Expand Down
5 changes: 4 additions & 1 deletion mrn_err.h
@@ -1,6 +1,6 @@
/*
Copyright(C) 2011 Kentoku SHIBA
Copyright(C) 2014-2015 Kouhei Sutou <kou@clear-code.com>
Copyright(C) 2014-2017 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 @@ -42,5 +42,8 @@
#define ER_MRN_KEY_BASED_ON_GENERATED_VIRTUAL_COLUMN_NUM 16509
#define ER_MRN_KEY_BASED_ON_GENERATED_VIRTUAL_COLUMN_STR \
"Index for virtual generated column is not supported: %s"
#define ER_MRN_INVALID_TABLE_FLAG_NUM 16510
#define ER_MRN_INVALID_TABLE_FLAG_STR \
"The table flag '%-.64s' is invalid. It is ignored"

#endif /* MRN_ERR_H_ */
5 changes: 5 additions & 0 deletions mrn_table.cpp
Expand Up @@ -443,6 +443,9 @@ int mrn_parse_table_param(MRN_SHARE *share, TABLE *table)

switch (title_length)
{
case 5:
MRN_PARAM_STR("flags", table_flags);
break;
case 6:
MRN_PARAM_STR("engine", engine);
break;
Expand Down Expand Up @@ -759,6 +762,8 @@ int mrn_free_share_alloc(
) {
uint i;
MRN_DBUG_ENTER_FUNCTION();
if (share->table_flags)
my_free(share->table_flags);
if (share->engine)
my_free(share->engine);
if (share->default_tokenizer)
Expand Down
2 changes: 2 additions & 0 deletions mrn_table.hpp
Expand Up @@ -49,6 +49,8 @@ typedef struct st_mroonga_share
TABLE_SHARE *wrap_table_share;
MRN_LONG_TERM_SHARE *long_term_share;

char *table_flags;
int table_flags_length;
char *engine;
int engine_length;
char *default_tokenizer;
Expand Down
@@ -0,0 +1,17 @@
SET NAMES utf8;
CREATE TABLE memos (
content VARCHAR(64) NOT NULL PRIMARY KEY,
FULLTEXT INDEX (content)
) COMMENT='engine "InnoDB", flags "TABLE_HASH_KEY|KEY_LARGE"'
DEFAULT CHARSET=utf8;
SELECT mroonga_command("dump --dump_plugins no --dump_indexes no");
mroonga_command("dump --dump_plugins no --dump_indexes no")
table_create memos TABLE_HASH_KEY|KEY_LARGE ShortText

table_create memos#content TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerMySQLGeneralCI

table_create mroonga_operations TABLE_NO_KEY
column_create mroonga_operations record COLUMN_SCALAR UInt32
column_create mroonga_operations table COLUMN_SCALAR ShortText
column_create mroonga_operations type COLUMN_SCALAR ShortText
DROP TABLE memos;
@@ -0,0 +1,19 @@
SET NAMES utf8;
CREATE TABLE memos (
content VARCHAR(64) NOT NULL PRIMARY KEY,
FULLTEXT INDEX (content)
) COMMENT='engine "InnoDB", flags "INVALID"'
DEFAULT CHARSET=utf8;
Warnings:
Warning 16510 The table flag 'INVALID' is invalid. It is ignored
SELECT mroonga_command("dump --dump_plugins no --dump_indexes no");
mroonga_command("dump --dump_plugins no --dump_indexes no")
table_create memos TABLE_HASH_KEY ShortText

table_create memos#content TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerMySQLGeneralCI

table_create mroonga_operations TABLE_NO_KEY
column_create mroonga_operations record COLUMN_SCALAR UInt32
column_create mroonga_operations table COLUMN_SCALAR ShortText
column_create mroonga_operations type COLUMN_SCALAR ShortText
DROP TABLE memos;
@@ -0,0 +1,18 @@
SET NAMES utf8;
CREATE TABLE memos (
content VARCHAR(64) NOT NULL PRIMARY KEY,
FULLTEXT INDEX (content)
) COMMENT='engine "InnoDB"'
FLAGS="TABLE_HASH_KEY|KEY_LARGE"
DEFAULT CHARSET=utf8;
SELECT mroonga_command("dump --dump_plugins no --dump_indexes no");
mroonga_command("dump --dump_plugins no --dump_indexes no")
table_create memos TABLE_HASH_KEY|KEY_LARGE ShortText

table_create memos#content TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerMySQLGeneralCI

table_create mroonga_operations TABLE_NO_KEY
column_create mroonga_operations record COLUMN_SCALAR UInt32
column_create mroonga_operations table COLUMN_SCALAR ShortText
column_create mroonga_operations type COLUMN_SCALAR ShortText
DROP TABLE memos;
40 changes: 40 additions & 0 deletions mysql-test/mroonga/wrapper/create/table/table/flags/t/comment.test
@@ -0,0 +1,40 @@
# Copyright(C) 2017 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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='engine "InnoDB", flags "TABLE_HASH_KEY|KEY_LARGE"'
DEFAULT CHARSET=utf8;

SELECT mroonga_command("dump --dump_plugins no --dump_indexes no");

DROP TABLE memos;

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

0 comments on commit dba406d

Please sign in to comment.