Kouhei Sutou
null+****@clear*****
Wed Feb 18 17:45:44 JST 2015
Kouhei Sutou 2015-02-18 17:45:44 +0900 (Wed, 18 Feb 2015) New Revision: 7d5b288bcd6a7db70f576afb7fc77b6f5804e6b0 https://github.com/groonga/groonga/commit/7d5b288bcd6a7db70f576afb7fc77b6f5804e6b0 Message: Add grn_expr_estimate_size() Added files: lib/mrb/scripts/expression_size_estimator.rb Modified files: include/groonga/expr.h lib/expr.c lib/mrb/mrb_expr.c lib/mrb/mrb_expr.h lib/mrb/scripts/expression.rb lib/mrb/scripts/initialize/post.rb lib/mrb/scripts/sources.am Modified: include/groonga/expr.h (+2 -0) =================================================================== --- include/groonga/expr.h 2015-02-18 16:22:25 +0900 (cf8fb35) +++ include/groonga/expr.h 2015-02-18 17:45:44 +0900 (63cbb6e) @@ -101,6 +101,8 @@ GRN_API grn_rc grn_expr_snip_add_conditions(grn_ctx *ctx, const char **closetags, unsigned int *closetag_lens); +GRN_API unsigned int grn_expr_estimate_size(grn_ctx *ctx, grn_obj *expr); + #ifdef __cplusplus } #endif Modified: lib/expr.c (+37 -0) =================================================================== --- lib/expr.c 2015-02-18 16:22:25 +0900 (ef477eb) +++ lib/expr.c 2015-02-18 17:45:44 +0900 (f417e26) @@ -7195,3 +7195,40 @@ grn_expr_dump_plan(grn_ctx *ctx, grn_obj *expr, grn_obj *buffer) } GRN_API_RETURN(GRN_SUCCESS); } + +static unsigned int +grn_expr_estimate_size_raw(grn_ctx *ctx, grn_obj *expr, grn_obj *table) +{ + return grn_table_size(ctx, table); +} + +unsigned int +grn_expr_estimate_size(grn_ctx *ctx, grn_obj *expr) +{ + grn_obj *table; + grn_obj *variable; + unsigned int size; + + variable = grn_expr_get_var_by_offset(ctx, expr, 0); + if (!variable) { + ERR(GRN_INVALID_ARGUMENT, "at least one variable must be defined"); + return 0; + } + + table = grn_ctx_at(ctx, variable->header.domain); + if (!table) { + ERR(GRN_INVALID_ARGUMENT, + "variable refers unknown domain: <%u>", variable->header.domain); + return 0; + } + + GRN_API_ENTER; +#ifdef GRN_WITH_MRUBY + if (ctx->impl->mrb.state) { + size = grn_mrb_expr_estimate_size(ctx, expr, table); + } else { +#endif + size = grn_expr_estimate_size_raw(ctx, expr, table); + } + GRN_API_RETURN(size); +} Modified: lib/mrb/mrb_expr.c (+23 -0) =================================================================== --- lib/mrb/mrb_expr.c 2015-02-18 16:22:25 +0900 (7828fb9) +++ lib/mrb/mrb_expr.c 2015-02-18 17:45:44 +0900 (cbe5ebb) @@ -789,4 +789,27 @@ exit: return sis; } + +unsigned int +grn_mrb_expr_estimate_size(grn_ctx *ctx, grn_obj *expr, grn_obj *table) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + mrb_value mrb_expression; + mrb_value mrb_table; + mrb_value mrb_size; + unsigned int size; + int arena_index; + + arena_index = mrb_gc_arena_save(mrb); + + mrb_expression = grn_mrb_value_from_grn_obj(mrb, expr); + mrb_table = grn_mrb_value_from_grn_obj(mrb, table); + mrb_size = mrb_funcall(mrb, mrb_expression, "estimate_size", 1, mrb_table); + size = mrb_fixnum(mrb_size); + + mrb_gc_arena_restore(mrb, arena_index); + + return size; +} #endif Modified: lib/mrb/mrb_expr.h (+4 -1) =================================================================== --- lib/mrb/mrb_expr.h 2015-02-18 16:22:25 +0900 (687565d) +++ lib/mrb/mrb_expr.h 2015-02-18 17:45:44 +0900 (0564401) @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2013 Brazil + Copyright(C) 2013-2015 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -28,6 +28,9 @@ extern "C" { void grn_mrb_expr_init(grn_ctx *ctx); scan_info **grn_mrb_scan_info_build(grn_ctx *ctx, grn_obj *expr, int *n, grn_operator op, uint32_t size); +unsigned int grn_mrb_expr_estimate_size(grn_ctx *ctx, + grn_obj *expr, + grn_obj *table); #ifdef __cplusplus } Modified: lib/mrb/scripts/expression.rb (+18 -0) =================================================================== --- lib/mrb/scripts/expression.rb 2015-02-18 16:22:25 +0900 (29290e2) +++ lib/mrb/scripts/expression.rb 2015-02-18 17:45:44 +0900 (ae3559d) @@ -9,5 +9,23 @@ module Groonga nil end end + + def estimate_size(table) + begin + estimator = ExpressionSizeEstimator.new(self, table) + estimator.estimate + rescue GroongaError => groonga_error + context.set_groonga_error(groonga_error) + table.size + rescue => error + context.record_error(:unknown_error, error) + table.size + end + end + + private + def context + @context ||= Context.instance + end end end Added: lib/mrb/scripts/expression_size_estimator.rb (+12 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/scripts/expression_size_estimator.rb 2015-02-18 17:45:44 +0900 (51d726a) @@ -0,0 +1,12 @@ +module Groonga + class ExpressionSizeEstimator + def initialize(expression, table) + @expression = expression + @table = table + end + + def estimate + @table.size + end + end +end Modified: lib/mrb/scripts/initialize/post.rb (+2 -0) =================================================================== --- lib/mrb/scripts/initialize/post.rb 2015-02-18 16:22:25 +0900 (7110437) +++ lib/mrb/scripts/initialize/post.rb 2015-02-18 17:45:44 +0900 (6de3cf1) @@ -9,6 +9,8 @@ require "command" require "table_cursor" require "index_cursor" +require "expression_size_estimator" + require "plugin_loader" require "eval_context" Modified: lib/mrb/scripts/sources.am (+1 -0) =================================================================== --- lib/mrb/scripts/sources.am 2015-02-18 16:22:25 +0900 (fa90c59) +++ lib/mrb/scripts/sources.am 2015-02-18 17:45:44 +0900 (db04d1c) @@ -9,6 +9,7 @@ RUBY_SCRIPT_FILES = \ error.rb \ eval_context.rb \ expression.rb \ + expression_size_estimator.rb \ index_cursor.rb \ index_info.rb \ initialize/pre.rb \ -------------- next part -------------- HTML����������������������������...Download