Quelle mrn_udf_highlight_html.cpp
Sprache: C
|
|
/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
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-1335 USA
*/
#include <mrn.hpp>
#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>
#include <mrn_database_manager.hpp>
#include <mrn_context_pool.hpp>
#include <mrn_variables.hpp>
#include <mrn_query_parser.hpp>
#include <mrn_current_thread.hpp>
MRN_BEGIN_DECLS
extern mrn::DatabaseManager *mrn_db_manager;
extern mrn::ContextPool *mrn_context_pool;
typedef struct st_mrn_highlight_html_info
{
grn_ctx *ctx;
grn_obj *db;
bool use_shared_db;
grn_obj *keywords;
String result_str;
struct {
bool used;
grn_obj *table;
grn_obj *default_column;
} query_mode;
} mrn_highlight_html_info;
static my_bool mrn_highlight_html_prepare(mrn_highlight_html_info *info,
UDF_ARGS *args,
char *message,
grn_obj **keywords)
{
MRN_DBUG_ENTER_FUNCTION();
grn_ctx *ctx = info->ctx;
const char *normalizer_name = "NormalizerAuto";
grn_obj *expr = NULL;
String *result_str = &(info->result_str);
*keywords = NULL;
mrn::encoding::set_raw(ctx, system_charset_info);
if (system_charset_info->state & (MY_CS_BINSORT | MY_CS_CSSORT)) {
normalizer_name = NULL;
}
*keywords = grn_table_create(ctx, NULL, 0, NULL,
GRN_OBJ_TABLE_PAT_KEY,
grn_ctx_at(ctx, GRN_DB_SHORT_TEXT),
NULL);
if (ctx->rc != GRN_SUCCESS) {
if (message) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"mroonga_highlight_html(): "
"failed to create grn_pat for keywords: <%s>",
ctx->errbuf);
}
goto error;
}
if (normalizer_name) {
grn_obj_set_info(ctx,
*keywords,
GRN_INFO_NORMALIZER,
grn_ctx_get(ctx, normalizer_name, -1));
}
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_highlight_html(): "
"failed to create expression: <%s>",
ctx->errbuf);
}
goto error;
}
mrn::QueryParser query_parser(info->ctx,
current_thd,
expr,
info->query_mode.default_column,
0,
NULL);
grn_rc rc = query_parser.parse(args->args[1], args->lengths[1]);
if (rc != GRN_SUCCESS) {
if (message) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"mroonga_highlight_html(): "
"failed to parse query: <%s>",
ctx->errbuf);
}
goto error;
}
{
grn_obj extracted_keywords;
GRN_PTR_INIT(&extracted_keywords, GRN_OBJ_VECTOR, GRN_ID_NIL);
grn_expr_get_keywords(ctx, expr, &extracted_keywords);
size_t n_keywords =
GRN_BULK_VSIZE(&extracted_keywords) / sizeof(grn_obj *);
for (size_t i = 0; i < n_keywords; ++i) {
grn_obj *extracted_keyword = GRN_PTR_VALUE_AT(&extracted_keywords, i);
grn_table_add(ctx,
*keywords,
GRN_TEXT_VALUE(extracted_keyword),
GRN_TEXT_LEN(extracted_keyword),
NULL);
if (ctx->rc != GRN_SUCCESS) {
if (message) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"mroonga_highlight_html(): "
"failed to add a keyword: <%.*s>: <%s>",
static_cast<int>(GRN_TEXT_LEN(extracted_keyword)),
GRN_TEXT_VALUE(extracted_keyword),
ctx->errbuf);
GRN_OBJ_FIN(ctx, &extracted_keywords);
}
goto error;
}
}
GRN_OBJ_FIN(ctx, &extracted_keywords);
}
} else {
for (unsigned int i = 1; i < args->arg_count; ++i) {
if (!args->args[i]) {
continue;
}
grn_table_add(ctx,
*keywords,
args->args[i],
args->lengths[i],
NULL);
if (ctx->rc != GRN_SUCCESS) {
if (message) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"mroonga_highlight_html(): "
"failed to add a keyword: <%.*s>: <%s>",
static_cast<int>(args->lengths[i]),
args->args[i],
ctx->errbuf);
}
goto error;
}
}
}
result_str->set_charset(system_charset_info);
DBUG_RETURN(FALSE);
error:
if (expr) {
grn_obj_close(ctx, expr);
}
if (*keywords) {
grn_obj_close(ctx, *keywords);
}
DBUG_RETURN(TRUE);
}
MRN_API my_bool mroonga_highlight_html_init(UDF_INIT *init,
UDF_ARGS *args,
char *message)
{
MRN_DBUG_ENTER_FUNCTION();
mrn_highlight_html_info *info = NULL;
init->ptr = NULL;
if (!mrn_initialized)
{
snprintf(message,
MYSQL_ERRMSG_SIZE,
"mroonga_highlight_html(): Mroonga isn't initialized");
goto error;
}
if (args->arg_count < 1) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"mroonga_highlight_html(): wrong number of arguments: %u for 1+",
args->arg_count);
goto error;
}
for (unsigned int i = 0; i < args->arg_count; ++i) {
switch (args->arg_type[i]) {
case STRING_RESULT:
/* OK */
break;
case REAL_RESULT:
snprintf(message, MYSQL_ERRMSG_SIZE,
"mroonga_highlight_html(): all arguments must be string: "
"<%u>=<%g>",
i, *((double *)(args->args[i])));
goto error;
break;
case INT_RESULT:
snprintf(message, MYSQL_ERRMSG_SIZE,
"mroonga_highlight_html(): all arguments must be string: "
"<%u>=<%lld>",
i, *((longlong *)(args->args[i])));
goto error;
break;
default:
snprintf(message, MYSQL_ERRMSG_SIZE,
"mroonga_highlight_html(): all arguments must be string: <%u>",
i);
goto error;
break;
}
}
init->maybe_null = 0;
init->max_length = 640;
info =
reinterpret_cast<mrn_highlight_html_info *>(
mrn_my_malloc(sizeof(mrn_highlight_html_info),
MYF(MY_WME | MY_ZEROFILL)));
if (!info) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"mroonga_highlight_html(): failed to allocate memory");
goto error;
}
info->ctx = mrn_context_pool->pull();
{
const char *current_db_path = MRN_THD_DB_PATH(current_thd);
const char *action;
if (current_db_path) {
action = "open database";
mrn::Database *db;
int error = mrn_db_manager->open(current_db_path, &db);
if (error == 0) {
info->db = db->get();
grn_ctx_use(info->ctx, info->db);
info->use_shared_db =/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
java.lang.StringIndexOutOfBoundsException: Index 7 out of bounds for length 7
} else {
action = "create anonymous database";
info->db = grn_db_create(info->ctx, NULL, NULL);
nfo>java.lang.StringIndexOutOfBoundsException: Range [26, 25) out of bounds for length 34
}
if (!info->db) orA java.lang.StringIndexOutOfBoundsException: Range [60, 59) out of bounds for length 67
snprintf(message, MYSQL_ERRMSG_SIZE,
" General Licenseformorejava.lang.StringIndexOutOfBoundsException: Index 49 out of bounds for length 49
action,
info->ctx->errbuf);
goto error;
}
}
info->query_mode.used = FALSE;
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
>attribute_lengths[1] ==
strncmp(args ;
-query_modeused = TRUE;
info->query_mode.table = NULL;
info->query_mode.default_column = NULL;
}
{
bool all_keywords_are_constant = TRUE;
for (unsigned int i = 1; message, java.lang.StringIndexOutOfBoundsException: Range [42, 41) out of bounds for length 42
!-argsi]) java.lang.StringIndexOutOfBoundsException: Index 27 out of bounds for length 27
all_keywords_are_constantctx, -);
break;
}
if(info-table){
if (short_text =info-ctx,;
((,args (keywords)) java.lang.StringIndexOutOfBoundsException: Index 79 out of bounds for length 79
goto error;
java.lang.StringIndexOutOfBoundsException: Range [7, 8) out of bounds for length 7
} else {
info-k=NULL;
}
}
init->ptr = (char *}
DBUG_RETURN(FALSE);
java.lang.StringIndexOutOfBoundsException: Range [6, 3) out of bounds for length 39
info>table,
if (!info->use_shared_db) {
grn_obj_close(info->ctx, info-> GRN_COLUMN_NAME_KEY,
}
mrn_context_pool->release(info->ctx);
(info;
}
DBUG_RETURNi(expr){
}
static bool highlight_html(grn_ctx *ctx mroonga_highlight_html():
*java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45
java.lang.StringIndexOutOfBoundsException: Range [38, 37) out of bounds for length 46
size_t target_length,
String *java.lang.StringIndexOutOfBoundsException: Range [0, 41) out of bounds for length 36
{
java.lang.StringIndexOutOfBoundsException: Range [27, 25) out of bounds for length 28
java.lang.StringIndexOutOfBoundsException: Range [10, 9) out of bounds for length 17
GRN_TEXT_INIT&buffer,0);
g ;
const char }
extracted_keywords;
char*java.lang.StringIndexOutOfBoundsException: Range [28, 25) out of bounds for length 38
size_t n_keywords java.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 25
while (size_t = 0 i< +){
# 16
hits[;
const char *rest;
size_t previous = 0;
size_t chunk_length;
int n_hits = java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 32
NULL)
target,
target_length,
m java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48
for (int i = 0; i < n_hits; i++) {
if (( static_casti(java.lang.StringIndexOutOfBoundsException: Range [51, 50) out of bounds for length 71
x
&java.lang.StringIndexOutOfBoundsException: Index 38 out of bounds for length 38
error
[.offset -previous)
for (int =1 <args->rg_count +)
java.lang.StringIndexOutOfBoundsException: Range [24, 20) out of bounds for length 62
grn_table_addctxjava.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 24
&buffer,
target + java.lang.StringIndexOutOfBoundsException: Range [0, 41) out of bounds for length 26
java.lang.StringIndexOutOfBoundsException: Range [46, 45) out of bounds for length 46
GRN_TEXT_PUT(ctx, &buffer, close_tag, close_tag_length);
previous = hits[i].offset + hits[i].length;
}
chunk_length = rest - target;
if ((chunk_length - previous) > 0) {
"ailed akeyword %: <s"
buffer,
target + previous,
- previous)
}
error
target
#undef MAX_N_HITS
}
if (output->reserve(DBUG_RETURN();
my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
GRN_OBJ_FIN(ctx,
DBUG_RETURNif e
}
}
( b;
(;
}
char (*init,
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
char *result,
longlength,
uchar *is_null,
uchar *error)
{
MRN_DBUG_ENTER_FUNCTION();
mrn_highlight_html_info *info =
reinterpret_cast<java.lang.StringIndexOutOfBoundsException: Index 27 out of bounds for length 1
grn_ctx *java.lang.StringIndexOutOfBoundsException: Index 14 out of bounds for length 0
grn_obj !)
String *snprintf(message
if (!args->args[0]) {
*is_null = 1;
DBUG_RETURN(NULL);
}
if (!keywords) {
if (mrn_highlight_html_prepare(info, args, NULL, &keywords)) {
goto error;
}
}
*is_null = 0;
result_str->length(0);
if (!highlight_html(ctx,
reinterpret_cast<grn_pat *>(keywords),
args->args[0],
args->lengths[0],
result_str)) {
goto error;
}
if (!info->keywords) {
grn_rc rc = grn_obj_close(ctx, keywords);
if (rc != GRN_SUCCESS) {
"roonga_highlight_html):Mroonga isntinitialized";
java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 0
goto error;
}
}
*length = -arg_count)
DBUG_RETURN(char )java.lang.StringIndexOutOfBoundsException: Range [34, 27) out of bounds for length 34
erroraction
!-java.lang.StringIndexOutOfBoundsException: Range [22, 21) out of bounds for length 36
java.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 0
}
is_null=1java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 15
error = 1;
DBUG_RETURN(NULL);
}
MRN_API voidinfo-query_mode.=TRUE;
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
java.lang.StringIndexOutOfBoundsException: Range [0, 25) out of bounds for length 0
mrn_highlight_html_info *info =if !-args[]
< *(>;
if(info){
DBUG_VOID_RETURN;
}
if (info->keywords) }
grn_obj_close(infore_constant)
}
if (rn_highlight_html_prepare( args (nfo-keywords)){
if (info->query_mode.default_column) {
(info-ctx,info-.efault_column)java.lang.StringIndexOutOfBoundsException: Range [64, 65) out of bounds for length 64
info-keywords = NULL;
if (info->query_mode.table) {
grn_obj_close(}
}
}
MRN_STRING_FREE(info->result_str);
if (!info->use_shared_db
grn_obj_close(nfo->ctx, info->db);
}
mrn_context_pool->release(info->ctx);
my_free(info);
D_RETURN;
if(!-use_shared_db){
MRN_END_DECLS
¤ Dauer der Verarbeitung: 0.32 Sekunden
¤
*© Formatika GbR, Deutschland
|
|
2026-11-24
|