Kouhei Sutou
null+****@clear*****
Thu Feb 19 13:48:06 JST 2015
Kouhei Sutou 2015-02-19 13:48:06 +0900 (Thu, 19 Feb 2015) New Revision: 7adb9c26cd55ad55a71476139e12f6213dfe2f1a https://github.com/groonga/groonga/commit/7adb9c26cd55ad55a71476139e12f6213dfe2f1a Message: Add grn_ii_estimate_size_for_query() Added files: lib/mrb/scripts/index_column.rb Modified files: include/groonga/ii.h lib/ii.c Modified: include/groonga/ii.h (+4 -0) =================================================================== --- include/groonga/ii.h 2015-02-19 13:39:57 +0900 (1bd8ce2) +++ include/groonga/ii.h 2015-02-19 13:48:06 +0900 (aeb9aa6) @@ -28,6 +28,10 @@ typedef struct _grn_ii grn_ii; typedef struct _grn_ii_buffer grn_ii_buffer; GRN_API unsigned int grn_ii_estimate_size(grn_ctx *ctx, grn_ii *ii, grn_id tid); +GRN_API unsigned int grn_ii_estimate_size_for_query(grn_ctx *ctx, grn_ii *ii, + const char *query, + unsigned int query_len, + grn_search_optarg *optarg); GRN_API grn_ii_buffer *grn_ii_buffer_open(grn_ctx *ctx, grn_ii *ii, long long unsigned int update_buffer_size); Modified: lib/ii.c (+64 -0) =================================================================== --- lib/ii.c 2015-02-19 13:39:57 +0900 (33bcd77) +++ lib/ii.c 2015-02-19 13:48:06 +0900 (e969ba8) @@ -6443,6 +6443,70 @@ exit : return rc; } +uint32_t +grn_ii_estimate_size_for_query(grn_ctx *ctx, grn_ii *ii, + const char *query, unsigned int query_len, + grn_search_optarg *optarg) +{ + grn_rc rc; + grn_obj *lexicon = ii->lexicon; + token_info **tis = NULL; + uint32_t i; + uint32_t n_tis = 0; + grn_bool only_skip_token = GRN_FALSE; + grn_operator mode = GRN_OP_EXACT; + double estimated_size = 0; + + if (query_len == 0) { + return 0; + } + + tis = GRN_MALLOC(sizeof(token_info *) * query_len * 2); + if (!tis) { + return 0; + } + + if (optarg) { + switch (optarg->mode) { + case GRN_OP_NEAR : + case GRN_OP_NEAR2 : + mode = optarg->mode; + break; + case GRN_OP_SIMILAR : + mode = optarg->mode; + break; + default : + break; + } + } + + rc = token_info_build(ctx, lexicon, ii, query, query_len, + tis, &n_tis, &only_skip_token, mode); + if (rc != GRN_SUCCESS) { + goto exit; + } + + for (i = 0; i < n_tis; i++) { + token_info *ti = tis[i]; + double term_estimated_size; + term_estimated_size = ((double)ti->size / ti->ntoken); + estimated_size += (term_estimated_size - estimated_size) / (i + 1); + } + +exit : + for (i = 0; i < n_tis; i++) { + token_info *ti = tis[i]; + if (ti) { + token_info_close(ctx, ti); + } + } + if (tis) { + GRN_FREE(tis); + } + + return estimated_size; +} + grn_rc grn_ii_sel(grn_ctx *ctx, grn_ii *ii, const char *string, unsigned int string_len, grn_hash *s, grn_operator op, grn_search_optarg *optarg) Added: lib/mrb/scripts/index_column.rb (+29 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/scripts/index_column.rb 2015-02-19 13:48:06 +0900 (7968671) @@ -0,0 +1,29 @@ +module Groonga + class IndexColumn + private :estimate_size_for_term_id + private :estimate_size_for_query + + # Estimate the number of matched records for term ID or query. + # + # @overload estimate_size(:term_id => term_id) + # @return [Integer] the number of matched records for the term ID. + # + # @overload estimate_size(:query => query) + # @return [Integer] the number of matched records for the query. + # + def estimate_size(parameters) + term_id = parameters[:term_id] + if term_id + return estimate_size_for_term_id(term_id) + end + + query = parameters[:query] + if query + return estimate_size_for_query(query, parameters) + end + + message = "must specify :term_id or :query: #{parameters.inspect}" + raise InvalidArgument, message + end + end +end -------------- next part -------------- HTML����������������������������... Download