Update Mroonga to the latest version on 2014-09-21T00:33:44+0900

This commit is contained in:
Kentoku SHIBA 2014-09-21 00:33:45 +09:00
commit 0cc855cdc8
2027 changed files with 460307 additions and 0 deletions

View file

@ -0,0 +1,10 @@
AM_CPPFLAGS = \
$(MYSQL_INCLUDES) \
$(GROONGA_CFLAGS) \
-I$(top_srcdir) \
-I$(top_srcdir)/lib
noinst_LTLIBRARIES = \
libmrn_udf.la
include sources.am

View file

@ -0,0 +1,165 @@
/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Copyright(C) 2010 Tetsuro IKEDA
Copyright(C) 2010-2013 Kentoku SHIBA
Copyright(C) 2011-2013 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
*/
#include <mrn_mysql.h>
#include <mrn_mysql_compat.h>
#include <mrn_path_mapper.hpp>
#include <mrn_windows.hpp>
#include <mrn_macro.hpp>
MRN_BEGIN_DECLS
struct CommandInfo
{
grn_ctx ctx;
String result;
};
MRN_API my_bool mroonga_command_init(UDF_INIT *initid, UDF_ARGS *args,
char *message)
{
CommandInfo *info = NULL;
initid->ptr = NULL;
if (args->arg_count != 1) {
sprintf(message,
"mroonga_command(): Incorrect number of arguments: %u for 1",
args->arg_count);
goto error;
}
if (args->arg_type[0] != STRING_RESULT) {
strcpy(message,
"mroonga_command(): The 1st argument must be command as string");
goto error;
}
initid->maybe_null = 1;
initid->const_item = 1;
info = (CommandInfo *)my_malloc(sizeof(CommandInfo),
MYF(MY_WME | MY_ZEROFILL));
if (!info) {
strcpy(message, "mroonga_command(): out of memory");
goto error;
}
grn_ctx_init(&(info->ctx), 0);
{
const char *current_db_path = current_thd->db;
const char *action;
if (current_db_path) {
action = "open database";
mrn::PathMapper mapper(current_db_path);
grn_db_open(&(info->ctx), mapper.db_path());
} else {
action = "create anonymous database";
grn_db_create(&(info->ctx), NULL, NULL);
}
if (info->ctx.rc != GRN_SUCCESS) {
sprintf(message,
"mroonga_command(): failed to %s: %s",
action,
info->ctx.errbuf);
goto error;
}
}
initid->ptr = (char *)info;
return FALSE;
error:
if (info) {
grn_obj *db;
db = grn_ctx_db(&(info->ctx));
if (db) {
grn_obj_close(&(info->ctx), db);
}
grn_ctx_fin(&(info->ctx));
my_free(info, MYF(0));
}
return TRUE;
}
MRN_API char *mroonga_command(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *length, char *is_null, char *error)
{
CommandInfo *info = (CommandInfo *)initid->ptr;
grn_ctx *ctx = &(info->ctx);
char *command;
unsigned int command_length;
int flags = 0;
if (!args->args[0]) {
*is_null = 1;
return NULL;
}
*is_null = 0;
command = args->args[0];
command_length = args->lengths[0];
grn_ctx_send(ctx, command, command_length, 0);
if (ctx->rc) {
my_message(ER_ERROR_ON_WRITE, ctx->errbuf, MYF(0));
goto error;
}
info->result.length(0);
do {
char *buffer;
unsigned int buffer_length;
grn_ctx_recv(ctx, &buffer, &buffer_length, &flags);
if (ctx->rc) {
my_message(ER_ERROR_ON_READ, ctx->errbuf, MYF(0));
goto error;
}
if (buffer_length > 0) {
if (info->result.reserve(buffer_length)) {
my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
goto error;
}
info->result.q_append(buffer, buffer_length);
}
} while (flags & GRN_CTX_MORE);
*length = info->result.length();
return (char *)(info->result.ptr());
error:
*error = 1;
return NULL;
}
MRN_API void mroonga_command_deinit(UDF_INIT *initid)
{
CommandInfo *info = (CommandInfo *)initid->ptr;
if (info) {
grn_obj *db = grn_ctx_db(&(info->ctx));
if (db) {
grn_obj_close(&(info->ctx), db);
}
grn_ctx_fin(&(info->ctx));
info->result.free();
my_free(info, MYF(0));
}
}
MRN_END_DECLS

View file

@ -0,0 +1,154 @@
/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Copyright(C) 2013 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
*/
#include <mrn_mysql.h>
#include <mrn_mysql_compat.h>
#include <mrn_path_mapper.hpp>
#include <mrn_windows.hpp>
#include <mrn_macro.hpp>
MRN_BEGIN_DECLS
struct EscapeInfo
{
grn_ctx ctx;
grn_obj target_characters;
grn_obj escaped_query;
bool processed;
};
MRN_API my_bool mroonga_escape_init(UDF_INIT *initid, UDF_ARGS *args,
char *message)
{
EscapeInfo *info = NULL;
initid->ptr = NULL;
if (!(1 <= args->arg_count && args->arg_count <= 2)) {
sprintf(message,
"mroonga_escape(): Incorrect number of arguments: %u for 1..2",
args->arg_count);
goto error;
}
if (args->arg_type[0] != STRING_RESULT) {
strcpy(message,
"mroonga_escape(): The 1st argument must be query as string");
goto error;
}
if (args->arg_count == 2) {
if (args->arg_type[1] != STRING_RESULT) {
strcpy(message,
"mroonga_escape(): "
"The 2st argument must be escape target characters as string");
goto error;
}
}
initid->maybe_null = 1;
initid->const_item = 1;
info = (EscapeInfo *)my_malloc(sizeof(EscapeInfo),
MYF(MY_WME | MY_ZEROFILL));
if (!info) {
strcpy(message, "mroonga_escape(): out of memory");
goto error;
}
grn_ctx_init(&(info->ctx), 0);
GRN_TEXT_INIT(&(info->target_characters), 0);
GRN_TEXT_INIT(&(info->escaped_query), 0);
info->processed = false;
initid->ptr = (char *)info;
return FALSE;
error:
if (info) {
grn_ctx_fin(&(info->ctx));
my_free(info, MYF(0));
}
return TRUE;
}
static void escape(EscapeInfo *info, UDF_ARGS *args)
{
grn_ctx *ctx = &(info->ctx);
char *query = args->args[0];
unsigned int query_length = args->lengths[0];
if (args->arg_count == 2) {
char *target_characters = args->args[1];
unsigned int target_characters_length = args->lengths[1];
GRN_TEXT_PUT(ctx, &(info->target_characters),
target_characters,
target_characters_length);
GRN_TEXT_PUTC(ctx, &(info->target_characters), '\0');
grn_expr_syntax_escape(ctx, query, query_length,
GRN_TEXT_VALUE(&(info->target_characters)),
GRN_QUERY_ESCAPE,
&(info->escaped_query));
} else {
grn_expr_syntax_escape_query(ctx, query, query_length,
&(info->escaped_query));
}
}
MRN_API char *mroonga_escape(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *length, char *is_null, char *error)
{
EscapeInfo *info = (EscapeInfo *)initid->ptr;
grn_ctx *ctx = &(info->ctx);
if (!args->args[0]) {
*is_null = 1;
return NULL;
}
*is_null = 0;
if (!info->processed) {
escape(info, args);
info->processed = true;
}
if (ctx->rc) {
my_message(ER_ERROR_ON_WRITE, ctx->errbuf, MYF(0));
goto error;
}
*length = GRN_TEXT_LEN(&(info->escaped_query));
return (char *)(GRN_TEXT_VALUE(&(info->escaped_query)));
error:
*error = 1;
return NULL;
}
MRN_API void mroonga_escape_deinit(UDF_INIT *initid)
{
EscapeInfo *info = (EscapeInfo *)initid->ptr;
if (info) {
grn_obj_unlink(&(info->ctx), &(info->target_characters));
grn_obj_unlink(&(info->ctx), &(info->escaped_query));
grn_ctx_fin(&(info->ctx));
my_free(info, MYF(0));
}
}
MRN_END_DECLS

View file

@ -0,0 +1,54 @@
/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Copyright(C) 2010 Tetsuro IKEDA
Copyright(C) 2010-2013 Kentoku SHIBA
Copyright(C) 2011-2013 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
*/
#include <mrn_mysql.h>
#include <mrn_windows.hpp>
#include <mrn_table.hpp>
#include <mrn_macro.hpp>
MRN_BEGIN_DECLS
MRN_API my_bool last_insert_grn_id_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
if (args->arg_count != 0) {
strcpy(message, "last_insert_grn_id must not have arguments");
return 1;
}
initid->maybe_null = 0;
return 0;
}
MRN_API longlong last_insert_grn_id(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
THD *thd = current_thd;
st_mrn_slot_data *slot_data = mrn_get_slot_data(thd, false);
if (slot_data == NULL) {
return 0;
}
longlong last_insert_record_id = slot_data->last_insert_record_id;
return last_insert_record_id;
}
MRN_API void last_insert_grn_id_deinit(UDF_INIT *initid)
{
}
MRN_END_DECLS

View file

@ -0,0 +1,302 @@
/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Copyright(C) 2010 Tetsuro IKEDA
Copyright(C) 2010-2013 Kentoku SHIBA
Copyright(C) 2011-2014 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
*/
#include <mrn_mysql.h>
#include <mrn_mysql_compat.h>
#include <mrn_err.h>
#include <mrn_encoding.hpp>
#include <mrn_windows.hpp>
#include <mrn_table.hpp>
#include <mrn_macro.hpp>
MRN_BEGIN_DECLS
struct st_mrn_snip_info
{
grn_ctx ctx;
grn_obj *snippet;
String result_str;
};
static my_bool mrn_snippet_prepare(st_mrn_snip_info *snip_info, UDF_ARGS *args,
char *message, grn_obj **snippet)
{
unsigned int i;
CHARSET_INFO *cs;
grn_ctx *ctx = &snip_info->ctx;
long long snip_max_len;
long long snip_max_num;
long long skip_leading_spaces;
long long html_escape;
int flags = GRN_SNIP_COPY_TAG;
grn_snip_mapping *mapping = NULL;
grn_rc rc;
String *result_str = &snip_info->result_str;
*snippet = NULL;
snip_max_len = *((long long *) args->args[1]);
snip_max_num = *((long long *) args->args[2]);
if (args->arg_type[3] == STRING_RESULT) {
if (!(cs = get_charset_by_name(args->args[3], MYF(0)))) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"Unknown charset: <%s>", args->args[3]);
goto error;
}
} else {
uint charset_id = static_cast<uint>(*((long long *) args->args[3]));
if (!(cs = get_charset(charset_id, MYF(0)))) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"Unknown charset ID: <%u>", charset_id);
goto error;
}
}
if (!mrn::encoding::set(ctx, cs)) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"Unsupported charset: <%s>", cs->name);
goto error;
}
if (!(cs->state & (MY_CS_BINSORT | MY_CS_CSSORT))) {
flags |= GRN_SNIP_NORMALIZE;
}
skip_leading_spaces = *((long long *) args->args[4]);
if (skip_leading_spaces) {
flags |= GRN_SNIP_SKIP_LEADING_SPACES;
}
html_escape = *((long long *) args->args[5]);
if (html_escape) {
mapping = (grn_snip_mapping *) -1;
}
*snippet = grn_snip_open(ctx, flags, static_cast<unsigned int>(snip_max_len),
static_cast<unsigned int>(snip_max_num),
"", 0, "", 0, mapping);
if (ctx->rc) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"Failed to open grn_snip: <%s>", ctx->errbuf);
goto error;
}
for (i = 8; i < args->arg_count; i += 3) {
rc = grn_snip_add_cond(ctx, *snippet,
args->args[i], args->lengths[i],
args->args[i + 1], args->lengths[i + 1],
args->args[i + 2], args->lengths[i + 2]);
if (rc) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"Failed to add a condition to grn_snip: <%s>", ctx->errbuf);
goto error;
}
}
result_str->set_charset(cs);
return FALSE;
error:
if (*snippet) {
grn_obj_close(ctx, *snippet);
}
return TRUE;
}
MRN_API my_bool mroonga_snippet_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
uint i;
st_mrn_snip_info *snip_info = NULL;
bool can_open_snippet = TRUE;
initid->ptr = NULL;
if (args->arg_count < 11 || (args->arg_count - 11) % 3)
{
sprintf(message, "Incorrect number of arguments for mroonga_snippet(): %u",
args->arg_count);
goto error;
}
if (args->arg_type[0] != STRING_RESULT) {
strcpy(message, "mroonga_snippet() requires string for 1st argument");
goto error;
}
if (args->arg_type[1] != INT_RESULT) {
strcpy(message, "mroonga_snippet() requires int for 2nd argument");
goto error;
}
if (args->arg_type[2] != INT_RESULT) {
strcpy(message, "mroonga_snippet() requires int for 3rd argument");
goto error;
}
if (
args->arg_type[3] != STRING_RESULT &&
args->arg_type[3] != INT_RESULT
) {
strcpy(message,
"mroonga_snippet() requires string or int for 4th argument");
goto error;
}
if (args->arg_type[4] != INT_RESULT) {
strcpy(message, "mroonga_snippet() requires int for 5th argument");
goto error;
}
if (args->arg_type[5] != INT_RESULT) {
strcpy(message, "mroonga_snippet() requires int for 6th argument");
goto error;
}
for (i = 6; i < args->arg_count; i++) {
if (args->arg_type[i] != STRING_RESULT) {
sprintf(message, "mroonga_snippet() requires string for %uth argument",
i);
goto error;
}
}
initid->maybe_null = 1;
initid->const_item = 1;
if (!(snip_info = (st_mrn_snip_info *) my_malloc(sizeof(st_mrn_snip_info),
MYF(MY_WME | MY_ZEROFILL))))
{
strcpy(message, "mroonga_snippet() out of memory");
goto error;
}
grn_ctx_init(&snip_info->ctx, 0);
grn_db_create(&snip_info->ctx, NULL, 0);
for (i = 1; i < args->arg_count; i++) {
if (!args->args[i]) {
can_open_snippet = FALSE;
break;
}
}
if (can_open_snippet) {
if (mrn_snippet_prepare(snip_info, args, message, &snip_info->snippet)) {
goto error;
}
}
initid->ptr = (char *) snip_info;
return FALSE;
error:
if (snip_info) {
grn_obj_close(&snip_info->ctx, grn_ctx_db(&snip_info->ctx));
grn_ctx_fin(&snip_info->ctx);
my_free(snip_info, MYF(0));
}
return TRUE;
}
MRN_API char *mroonga_snippet(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *length, char *is_null, char *error)
{
st_mrn_snip_info *snip_info = (st_mrn_snip_info *) initid->ptr;
grn_ctx *ctx = &snip_info->ctx;
String *result_str = &snip_info->result_str;
char *target;
unsigned int target_length;
grn_obj *snippet = NULL;
grn_rc rc;
unsigned int i, n_results, max_tagged_length, result_length;
if (!args->args[0]) {
*is_null = 1;
return NULL;
}
*is_null = 0;
target = args->args[0];
target_length = args->lengths[0];
if (!snip_info->snippet) {
for (i = 1; i < args->arg_count; i++) {
if (!args->args[i]) {
my_printf_error(ER_MRN_INVALID_NULL_VALUE_NUM,
ER_MRN_INVALID_NULL_VALUE_STR, MYF(0),
"mroonga_snippet() arguments");
goto error;
}
}
if (mrn_snippet_prepare(snip_info, args, NULL, &snippet)) {
goto error;
}
} else {
snippet = snip_info->snippet;
}
rc = grn_snip_exec(ctx, snippet, target, target_length,
&n_results, &max_tagged_length);
if (rc) {
my_printf_error(ER_MRN_ERROR_FROM_GROONGA_NUM,
ER_MRN_ERROR_FROM_GROONGA_STR, MYF(0), ctx->errbuf);
goto error;
}
result_str->length(0);
if (result_str->reserve((args->lengths[6] + args->lengths[7] +
max_tagged_length) * n_results)) {
my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
goto error;
}
for (i = 0; i < n_results; i++) {
result_str->q_append(args->args[6], args->lengths[6]);
rc = grn_snip_get_result(ctx, snippet, i,
(char *) result_str->ptr() + result_str->length(),
&result_length);
if (rc) {
my_printf_error(ER_MRN_ERROR_FROM_GROONGA_NUM,
ER_MRN_ERROR_FROM_GROONGA_STR, MYF(0), ctx->errbuf);
goto error;
}
result_str->length(result_str->length() + result_length);
result_str->q_append(args->args[7], args->lengths[7]);
}
if (!snip_info->snippet) {
rc = grn_obj_close(ctx, snippet);
if (rc) {
my_printf_error(ER_MRN_ERROR_FROM_GROONGA_NUM,
ER_MRN_ERROR_FROM_GROONGA_STR, MYF(0), ctx->errbuf);
goto error;
}
}
*length = result_str->length();
return (char *) result_str->ptr();
error:
*error = 1;
return NULL;
}
MRN_API void mroonga_snippet_deinit(UDF_INIT *initid)
{
st_mrn_snip_info *snip_info = (st_mrn_snip_info *) initid->ptr;
if (snip_info) {
if (snip_info->snippet) {
grn_obj_close(&snip_info->ctx, snip_info->snippet);
}
snip_info->result_str.free();
grn_obj_close(&snip_info->ctx, grn_ctx_db(&snip_info->ctx));
grn_ctx_fin(&snip_info->ctx);
my_free(snip_info, MYF(0));
}
}
MRN_END_DECLS

View file

@ -0,0 +1,5 @@
libmrn_udf_la_SOURCES = \
mrn_udf_last_insert_grn_id.cpp \
mrn_udf_snippet.cpp \
mrn_udf_command.cpp \
mrn_udf_escape.cpp