Kouhei Sutou
null+****@clear*****
Thu Mar 16 09:36:48 JST 2017
Kouhei Sutou 2017-03-16 09:36:48 +0900 (Thu, 16 Mar 2017) New Revision: 4ecabaa7e3e35d2015057550663a9ac6db98f765 https://github.com/groonga/groonga/commit/4ecabaa7e3e35d2015057550663a9ac6db98f765 Message: Export a feature to store generated values to column New function: grn_table_apply_expression() Added files: lib/table.c Modified files: include/groonga/column.h include/groonga/table.h lib/proc/proc_select.c lib/sources.am Modified: include/groonga/column.h (+1 -1) =================================================================== --- include/groonga/column.h 2017-03-15 18:21:48 +0900 (753cdba) +++ include/groonga/column.h 2017-03-16 09:36:48 +0900 (434543c) @@ -1,5 +1,5 @@ /* - Copyright(C) 2009-2016 Brazil + Copyright(C) 2009-2017 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public Modified: include/groonga/table.h (+5 -0) =================================================================== --- include/groonga/table.h 2017-03-15 18:21:48 +0900 (12e04bc) +++ include/groonga/table.h 2017-03-16 09:36:48 +0900 (1e335aa) @@ -222,6 +222,11 @@ GRN_API grn_obj *grn_table_tokenize(grn_ctx *ctx, grn_obj *table, const char *str, unsigned int str_len, grn_obj *buf, grn_bool addp); +GRN_API grn_rc grn_table_apply_expression(grn_ctx *ctx, + grn_obj *table, + grn_obj *output_column, + grn_obj *expression); + #ifdef __cplusplus } #endif Modified: lib/proc/proc_select.c (+5 -19) =================================================================== --- lib/proc/proc_select.c 2017-03-15 18:21:48 +0900 (fefd65b) +++ lib/proc/proc_select.c 2017-03-16 09:36:48 +0900 (afc6918) @@ -1356,36 +1356,22 @@ grn_select_apply_columns(grn_ctx *ctx, break; } } else { - grn_table_cursor *table_cursor; - grn_id id; - - table_cursor = grn_table_cursor_open(ctx, table, - NULL, 0, - NULL, 0, - 0, -1, GRN_CURSOR_BY_ID); - if (!table_cursor) { + grn_rc rc; + rc = grn_table_apply_expression(ctx, table, column, expression); + if (rc != GRN_SUCCESS) { grn_obj_close(ctx, expression); grn_obj_close(ctx, column); GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, "[select][column][%s][%.*s] " - "failed to create cursor for getting records: %s", + "failed to apply expression to generate column values: " + "%s", grn_column_stage_name(column_data->stage), (int)(column_data->label.length), column_data->label.value, ctx->errbuf); break; } - - while ((id = grn_table_cursor_next(ctx, table_cursor)) != GRN_ID_NIL) { - grn_obj *value; - - GRN_RECORD_SET(ctx, record, id); - value = grn_expr_exec(ctx, expression, 0); - if (value) { - grn_obj_set_value(ctx, column, id, value, GRN_OBJ_SET); - } - } } grn_obj_close(ctx, expression); Modified: lib/sources.am (+1 -0) =================================================================== --- lib/sources.am 2017-03-15 18:21:48 +0900 (1a4e3b9) +++ lib/sources.am 2017-03-16 09:36:48 +0900 (8c37853) @@ -86,6 +86,7 @@ libgroonga_la_SOURCES = \ grn_str.h \ string.c \ grn_string.h \ + table.c \ thread.c \ time.c \ grn_time.h \ Added: lib/table.c (+43 -0) 100644 =================================================================== --- /dev/null +++ lib/table.c 2017-03-16 09:36:48 +0900 (a9e3c98) @@ -0,0 +1,43 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2017 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + 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 "grn.h" +#include "grn_ctx.h" + +grn_rc +grn_table_apply_expression(grn_ctx *ctx, + grn_obj *table, + grn_obj *output_column, + grn_obj *expression) +{ + grn_obj *record; + + GRN_API_ENTER; + + record = grn_expr_get_var_by_offset(ctx, expression, 0); + GRN_TABLE_EACH_BEGIN_FLAGS(ctx, table, cursor, id, GRN_CURSOR_BY_ID) { + grn_obj *value; + GRN_RECORD_SET(ctx, record, id); + value = grn_expr_exec(ctx, expression, 0); + if (value) { + grn_obj_set_value(ctx, output_column, id, value, GRN_OBJ_SET); + } + } GRN_TABLE_EACH_END(ctx, cursor); + + GRN_API_RETURN(ctx->rc); +} -------------- next part -------------- HTML����������������������������...Download