Kouhei Sutou
null+****@clear*****
Mon May 21 14:32:12 JST 2018
Kouhei Sutou 2018-05-21 14:32:12 +0900 (Mon, 21 May 2018) New Revision: 8f2c40459b3e6044b9a916b80693db781316bd9b https://github.com/groonga/groonga/commit/8f2c40459b3e6044b9a916b80693db781316bd9b Message: grn_expr_executor: support on stack Modified files: lib/expr.c lib/expr_executor.c lib/grn_expr_executor.h lib/table.c Modified: lib/expr.c (+8 -8) =================================================================== --- lib/expr.c 2018-05-21 12:18:20 +0900 (30c6be156) +++ lib/expr.c 2018-05-21 14:32:12 +0900 (37db7d84d) @@ -5813,10 +5813,10 @@ grn_table_select_sequential(grn_ctx *ctx, grn_obj *table, grn_obj *expr, grn_table_cursor *tc; grn_hash_cursor *hc; grn_hash *s = (grn_hash *)res; - grn_expr_executor *executor; + grn_expr_executor executor; - executor = grn_expr_executor_open(ctx, expr); - if (!executor) { + grn_expr_executor_init(ctx, &executor, expr); + if (ctx->rc != GRN_SUCCESS) { return; } GRN_INT32_INIT(&score_buffer, 0); @@ -5824,7 +5824,7 @@ grn_table_select_sequential(grn_ctx *ctx, grn_obj *table, grn_obj *expr, case GRN_OP_OR : if ((tc = grn_table_cursor_open(ctx, table, NULL, 0, NULL, 0, 0, -1, 0))) { while ((id = grn_table_cursor_next(ctx, tc))) { - result = grn_expr_executor_exec(ctx, executor, id); + result = grn_expr_executor_exec(ctx, &executor, id); if (ctx->rc) { break; } @@ -5843,7 +5843,7 @@ grn_table_select_sequential(grn_ctx *ctx, grn_obj *table, grn_obj *expr, if ((hc = grn_hash_cursor_open(ctx, s, NULL, 0, NULL, 0, 0, -1, 0))) { while (grn_hash_cursor_next(ctx, hc)) { grn_hash_cursor_get_key(ctx, hc, (void **) &idp); - result = grn_expr_executor_exec(ctx, executor, *idp); + result = grn_expr_executor_exec(ctx, &executor, *idp); if (ctx->rc) { break; } @@ -5863,7 +5863,7 @@ grn_table_select_sequential(grn_ctx *ctx, grn_obj *table, grn_obj *expr, if ((hc = grn_hash_cursor_open(ctx, s, NULL, 0, NULL, 0, 0, -1, 0))) { while (grn_hash_cursor_next(ctx, hc)) { grn_hash_cursor_get_key(ctx, hc, (void **) &idp); - result = grn_expr_executor_exec(ctx, executor, *idp); + result = grn_expr_executor_exec(ctx, &executor, *idp); if (ctx->rc) { break; } @@ -5879,7 +5879,7 @@ grn_table_select_sequential(grn_ctx *ctx, grn_obj *table, grn_obj *expr, if ((hc = grn_hash_cursor_open(ctx, s, NULL, 0, NULL, 0, 0, -1, 0))) { while (grn_hash_cursor_next(ctx, hc)) { grn_hash_cursor_get_key(ctx, hc, (void **) &idp); - result = grn_expr_executor_exec(ctx, executor, *idp); + result = grn_expr_executor_exec(ctx, &executor, *idp); if (ctx->rc) { break; } @@ -5897,7 +5897,7 @@ grn_table_select_sequential(grn_ctx *ctx, grn_obj *table, grn_obj *expr, break; } GRN_OBJ_FIN(ctx, &score_buffer); - grn_expr_executor_close(ctx, executor); + grn_expr_executor_fin(ctx, &executor); } static grn_inline void Modified: lib/expr_executor.c (+56 -76) =================================================================== --- lib/expr_executor.c 2018-05-21 12:18:20 +0900 (3d5e142e7) +++ lib/expr_executor.c 2018-05-21 14:32:12 +0900 (145fc0669) @@ -29,61 +29,6 @@ # include <onigmo.h> #endif -typedef union { - struct { - grn_obj *value; - } constant; - struct { - grn_obj *column; - grn_obj value_buffer; - } value; -#ifdef GRN_SUPPORT_REGEXP - struct { - grn_obj result_buffer; - OnigRegex regex; - grn_obj value_buffer; - grn_obj *normalizer; - } simple_regexp; -#endif /* GRN_SUPPORT_REGEXP */ - struct { - grn_proc_ctx proc_ctx; - int n_args; - } proc; - struct { - grn_obj result_buffer; - } simple_condition_constant; - struct { - grn_obj result_buffer; - grn_ra *ra; - grn_ra_cache ra_cache; - unsigned int ra_element_size; - grn_obj value_buffer; - grn_obj constant_buffer; - grn_operator_exec_func *exec; - } simple_condition_ra; - struct { - grn_bool need_exec; - grn_obj result_buffer; - grn_obj value_buffer; - grn_obj constant_buffer; - grn_operator_exec_func *exec; - } simple_condition; -} grn_expr_executor_data; - -typedef grn_obj *(*grn_expr_executor_exec_func)(grn_ctx *ctx, - grn_expr_executor *executor, - grn_id id); -typedef void (*grn_expr_executor_fin_func)(grn_ctx *ctx, - grn_expr_executor *executor); - -struct _grn_expr_executor { - grn_obj *expr; - grn_obj *variable; - grn_expr_executor_exec_func exec; - grn_expr_executor_fin_func fin; - grn_expr_executor_data data; -}; - static void grn_expr_executor_init_general(grn_ctx *ctx, grn_expr_executor *executor) @@ -216,6 +161,7 @@ grn_expr_executor_init_simple_regexp(grn_ctx *ctx, grn_expr *e = (grn_expr *)(executor->expr); grn_obj *result_buffer = &(executor->data.simple_regexp.result_buffer); OnigEncoding onig_encoding; + OnigRegex regex; int onig_result; OnigErrorInfo onig_error_info; grn_obj *pattern; @@ -250,7 +196,7 @@ grn_expr_executor_init_simple_regexp(grn_ctx *ctx, } pattern = e->codes[1].value; - onig_result = onig_new(&(executor->data.simple_regexp.regex), + onig_result = onig_new(®ex, GRN_TEXT_VALUE(pattern), GRN_TEXT_VALUE(pattern) + GRN_TEXT_LEN(pattern), ONIG_OPTION_ASCII_RANGE | @@ -268,6 +214,7 @@ grn_expr_executor_init_simple_regexp(grn_ctx *ctx, message); return; } + executor->data.simple_regexp.regex = regex; GRN_VOID_INIT(&(executor->data.simple_regexp.value_buffer)); @@ -1046,11 +993,12 @@ grn_expr_executor_fin_simple_condition(grn_ctx *ctx, GRN_OBJ_FIN(ctx, &(executor->data.simple_condition.constant_buffer)); } -grn_expr_executor * -grn_expr_executor_open(grn_ctx *ctx, grn_obj *expr) +grn_rc +grn_expr_executor_init(grn_ctx *ctx, + grn_expr_executor *executor, + grn_obj *expr) { grn_obj *variable; - grn_expr_executor *executor; GRN_API_ENTER; @@ -1063,7 +1011,7 @@ grn_expr_executor_open(grn_ctx *ctx, grn_obj *expr) (int)GRN_TEXT_LEN(&inspected), GRN_TEXT_VALUE(&inspected)); GRN_OBJ_FIN(ctx, &inspected); - GRN_API_RETURN(NULL); + GRN_API_RETURN(ctx->rc); } variable = grn_expr_get_var_by_offset(ctx, expr, 0); @@ -1076,15 +1024,7 @@ grn_expr_executor_open(grn_ctx *ctx, grn_obj *expr) (int)GRN_TEXT_LEN(&inspected), GRN_TEXT_VALUE(&inspected)); GRN_OBJ_FIN(ctx, &inspected); - GRN_API_RETURN(NULL); - } - - executor = GRN_CALLOC(sizeof(grn_expr_executor)); - if (!executor) { - ERR(ctx->rc, - "[expr-executor][open] failed to allocate: %s", - ctx->errbuf); - GRN_API_RETURN(NULL); + GRN_API_RETURN(ctx->rc); } executor->expr = expr; @@ -1118,23 +1058,47 @@ grn_expr_executor_open(grn_ctx *ctx, grn_obj *expr) executor->fin = grn_expr_executor_fin_general; } - GRN_API_RETURN(executor); + GRN_API_RETURN(ctx->rc); } -grn_obj * -grn_expr_executor_exec(grn_ctx *ctx, grn_expr_executor *executor, grn_id id) +grn_rc +grn_expr_executor_fin(grn_ctx *ctx, + grn_expr_executor *executor) { - grn_obj *value; + GRN_API_ENTER; + + if (!executor) { + GRN_API_RETURN(GRN_SUCCESS); + } + + executor->fin(ctx, executor); + + GRN_API_RETURN(GRN_SUCCESS); +} + +grn_expr_executor * +grn_expr_executor_open(grn_ctx *ctx, grn_obj *expr) +{ + grn_expr_executor *executor; GRN_API_ENTER; + executor = GRN_CALLOC(sizeof(grn_expr_executor)); if (!executor) { + ERR(ctx->rc, + "[expr-executor][open] failed to allocate: %s", + ctx->errbuf); GRN_API_RETURN(NULL); } - value = executor->exec(ctx, executor, id); + grn_expr_executor_init(ctx, executor, expr); - GRN_API_RETURN(value); + if (ctx->rc != GRN_SUCCESS) { + GRN_FREE(executor); + executor = NULL; + } + + GRN_API_RETURN(executor); } grn_rc @@ -1146,8 +1110,24 @@ grn_expr_executor_close(grn_ctx *ctx, grn_expr_executor *executor) GRN_API_RETURN(GRN_SUCCESS); } - executor->fin(ctx, executor); + grn_expr_executor_fin(ctx, executor); GRN_FREE(executor); GRN_API_RETURN(GRN_SUCCESS); } + +grn_obj * +grn_expr_executor_exec(grn_ctx *ctx, grn_expr_executor *executor, grn_id id) +{ + grn_obj *value; + + GRN_API_ENTER; + + if (!executor) { + GRN_API_RETURN(NULL); + } + + value = executor->exec(ctx, executor, id); + + GRN_API_RETURN(value); +} Modified: lib/grn_expr_executor.h (+73 -8) =================================================================== --- lib/grn_expr_executor.h 2018-05-21 12:18:20 +0900 (df00134cf) +++ lib/grn_expr_executor.h 2018-05-21 14:32:12 +0900 (eb331e84a) @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2017 Brazil + Copyright(C) 2017-2018 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -26,13 +26,78 @@ extern "C" { typedef struct _grn_expr_executor grn_expr_executor; -grn_expr_executor *grn_expr_executor_open(grn_ctx *ctx, - grn_obj *expr); -grn_obj *grn_expr_executor_exec(grn_ctx *ctx, - grn_expr_executor *executor, - grn_id id); -grn_rc grn_expr_executor_close(grn_ctx *ctx, - grn_expr_executor *executor); +typedef union { + struct { + grn_obj *value; + } constant; + struct { + grn_obj *column; + grn_obj value_buffer; + } value; + struct { + grn_obj result_buffer; + void *regex; + grn_obj value_buffer; + grn_obj *normalizer; + } simple_regexp; + struct { + grn_proc_ctx proc_ctx; + int n_args; + } proc; + struct { + grn_obj result_buffer; + } simple_condition_constant; + struct { + grn_obj result_buffer; + grn_ra *ra; + grn_ra_cache ra_cache; + unsigned int ra_element_size; + grn_obj value_buffer; + grn_obj constant_buffer; + grn_operator_exec_func *exec; + } simple_condition_ra; + struct { + grn_bool need_exec; + grn_obj result_buffer; + grn_obj value_buffer; + grn_obj constant_buffer; + grn_operator_exec_func *exec; + } simple_condition; +} grn_expr_executor_data; + +typedef grn_obj *(*grn_expr_executor_exec_func)(grn_ctx *ctx, + grn_expr_executor *executor, + grn_id id); +typedef void (*grn_expr_executor_fin_func)(grn_ctx *ctx, + grn_expr_executor *executor); + +struct _grn_expr_executor { + grn_obj *expr; + grn_obj *variable; + grn_expr_executor_exec_func exec; + grn_expr_executor_fin_func fin; + grn_expr_executor_data data; +}; + +grn_rc +grn_expr_executor_init(grn_ctx *ctx, + grn_expr_executor *executor, + grn_obj *expr); +grn_rc +grn_expr_executor_fin(grn_ctx *ctx, + grn_expr_executor *executor); + +grn_expr_executor * +grn_expr_executor_open(grn_ctx *ctx, + grn_obj *expr); +grn_rc +grn_expr_executor_close(grn_ctx *ctx, + grn_expr_executor *executor); + +grn_obj * +grn_expr_executor_exec(grn_ctx *ctx, + grn_expr_executor *executor, + grn_id id); #ifdef __cplusplus } Modified: lib/table.c (+5 -5) =================================================================== --- lib/table.c 2018-05-21 12:18:20 +0900 (776fdf81e) +++ lib/table.c 2018-05-21 14:32:12 +0900 (4cf6bf969) @@ -32,7 +32,7 @@ grn_table_apply_expr(grn_ctx *ctx, grn_obj *output_column, grn_obj *expr) { - grn_expr_executor *executor; + grn_expr_executor executor; GRN_API_ENTER; @@ -60,13 +60,13 @@ grn_table_apply_expr(grn_ctx *ctx, GRN_API_RETURN(ctx->rc); } - executor = grn_expr_executor_open(ctx, expr); - if (!executor) { + grn_expr_executor_init(ctx, &executor, expr); + if (ctx->rc != GRN_SUCCESS) { GRN_API_RETURN(ctx->rc); } GRN_TABLE_EACH_BEGIN_FLAGS(ctx, table, cursor, id, GRN_CURSOR_BY_ID) { grn_obj *value; - value = grn_expr_executor_exec(ctx, executor, id); + value = grn_expr_executor_exec(ctx, &executor, id); if (ctx->rc != GRN_SUCCESS) { break; } @@ -74,7 +74,7 @@ grn_table_apply_expr(grn_ctx *ctx, grn_obj_set_value(ctx, output_column, id, value, GRN_OBJ_SET); } } GRN_TABLE_EACH_END(ctx, cursor); - grn_expr_executor_close(ctx, executor); + grn_expr_executor_fin(ctx, &executor); GRN_API_RETURN(ctx->rc); } -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180521/e4cdbab6/attachment-0001.htm