| 1 | /* |
| 2 | Copyright(C) 2015 Brazil |
| 3 | Copyright(C) 2020-2022 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "../grn_ctx_impl.h" |
| 21 | #include "../grn_ii.h" |
| 22 | #include "../grn_db.h" |
| 23 | #include "../grn_expr_executor.h" |
| 24 | #include "../grn_posting.h" |
| 25 | |
| 26 | #ifdef GRN_WITH_MRUBY |
| 27 | #include <mruby.h> |
| 28 | #include <mruby/class.h> |
| 29 | #include <mruby/data.h> |
| 30 | #include <mruby/string.h> |
| 31 | #include <mruby/hash.h> |
| 32 | #include <mruby/variable.h> |
| 33 | |
| 34 | #include "mrb_ctx.h" |
| 35 | #include "mrb_index_cursor.h" |
| 36 | #include "mrb_converter.h" |
| 37 | #include "mrb_options.h" |
| 38 | |
| 39 | static struct mrb_data_type mrb_grn_index_cursor_type = { |
| 40 | "Groonga::IndexCursor", |
| 41 | NULL |
| 42 | }; |
| 43 | |
| 44 | static mrb_value |
| 45 | mrb_grn_index_cursor_class_open_raw(mrb_state *mrb, mrb_value klass) |
| 46 | { |
| 47 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 48 | mrb_value mrb_table_cursor; |
| 49 | mrb_value mrb_index; |
| 50 | mrb_value mrb_options = mrb_nil_value(); |
| 51 | grn_obj *index_cursor; |
| 52 | grn_table_cursor *table_cursor; |
| 53 | grn_obj *index; |
| 54 | grn_id rid_min = GRN_ID_NIL; |
| 55 | grn_id rid_max = GRN_ID_MAX; |
| 56 | int flags = 0; |
| 57 | mrb_value mrb_index_cursor; |
| 58 | |
| 59 | mrb_get_args(mrb, "oo|H", &mrb_table_cursor, &mrb_index, &mrb_options); |
| 60 | |
| 61 | table_cursor = DATA_PTR(mrb_table_cursor); |
| 62 | index = DATA_PTR(mrb_index); |
| 63 | if (!mrb_nil_p(mrb_options)) { |
| 64 | /* TODO */ |
| 65 | } |
| 66 | index_cursor = grn_index_cursor_open(ctx, table_cursor, index, |
| 67 | rid_min, rid_max, flags); |
| 68 | grn_mrb_ctx_check(mrb); |
| 69 | |
| 70 | mrb_index_cursor = mrb_funcall(mrb, klass, "new", 1, |
| 71 | mrb_cptr_value(mrb, index_cursor)); |
| 72 | mrb_iv_set(mrb, mrb_index_cursor, mrb_intern_lit(mrb, "@index"), mrb_index); |
| 73 | return mrb_index_cursor; |
| 74 | } |
| 75 | |
| 76 | static mrb_value |
| 77 | mrb_grn_index_cursor_initialize(mrb_state *mrb, mrb_value self) |
| 78 | { |
| 79 | mrb_value mrb_index_cursor_ptr; |
| 80 | |
| 81 | mrb_get_args(mrb, "o", &mrb_index_cursor_ptr); |
| 82 | DATA_TYPE(self) = &mrb_grn_index_cursor_type; |
| 83 | DATA_PTR(self) = mrb_cptr(mrb_index_cursor_ptr); |
| 84 | |
| 85 | return self; |
| 86 | } |
| 87 | |
| 88 | static mrb_value |
| 89 | mrb_grn_index_cursor_close(mrb_state *mrb, mrb_value self) |
| 90 | { |
| 91 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 92 | grn_obj *index_cursor; |
| 93 | |
| 94 | index_cursor = DATA_PTR(self); |
| 95 | if (index_cursor) { |
| 96 | DATA_PTR(self) = NULL; |
| 97 | grn_obj_close(ctx, index_cursor); |
| 98 | grn_mrb_ctx_check(mrb); |
| 99 | } |
| 100 | |
| 101 | return mrb_nil_value(); |
| 102 | } |
| 103 | |
| 104 | static mrb_value |
| 105 | mrb_grn_index_cursor_count(mrb_state *mrb, mrb_value self) |
| 106 | { |
| 107 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 108 | grn_id term_id; |
| 109 | int n_records = 0; |
| 110 | |
| 111 | while (grn_index_cursor_next(ctx, DATA_PTR(self), &term_id)) { |
| 112 | n_records++; |
| 113 | } |
| 114 | |
| 115 | return mrb_int_value(mrb, n_records); |
| 116 | } |
| 117 | |
| 118 | static mrb_value |
| 119 | mrb_grn_index_cursor_select(mrb_state *mrb, mrb_value self) |
| 120 | { |
| 121 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 122 | mrb_value mrb_result_set; |
| 123 | mrb_value mrb_options; |
| 124 | grn_obj *index_cursor; |
| 125 | grn_obj *expr = NULL; |
| 126 | grn_obj *expr_variable = NULL; |
| 127 | int offset = 0; |
| 128 | int limit = 10; |
| 129 | int max_n_unmatched_records = -1; |
| 130 | grn_expr_executor executor; |
| 131 | int n_matched_records = 0; |
| 132 | int n_unmatched_records = 0; |
| 133 | mrb_value mrb_index; |
| 134 | grn_obj *index; |
| 135 | grn_obj *lexicon; |
| 136 | grn_obj *data_table; |
| 137 | grn_hash *result_set; |
| 138 | grn_posting *posting; |
| 139 | grn_id term_id; |
| 140 | grn_operator op = GRN_OP_OR; |
| 141 | |
| 142 | mrb_get_args(mrb, "o|H", &mrb_result_set, &mrb_options); |
| 143 | |
| 144 | index_cursor = DATA_PTR(self); |
| 145 | result_set = DATA_PTR(mrb_result_set); |
| 146 | |
| 147 | if (!mrb_nil_p(mrb_options)) { |
| 148 | mrb_value mrb_expr; |
| 149 | mrb_value mrb_offset; |
| 150 | mrb_value mrb_limit; |
| 151 | mrb_value mrb_max_n_unmatched_records; |
| 152 | |
| 153 | mrb_expr = grn_mrb_options_get_lit(mrb, mrb_options, "expression"); |
| 154 | if (!mrb_nil_p(mrb_expr)) { |
| 155 | expr = DATA_PTR(mrb_expr); |
| 156 | expr_variable = grn_expr_get_var_by_offset(ctx, expr, 0); |
| 157 | } |
| 158 | |
| 159 | mrb_offset = grn_mrb_options_get_lit(mrb, mrb_options, "offset"); |
| 160 | if (!mrb_nil_p(mrb_offset)) { |
| 161 | offset = (int)mrb_integer(mrb_offset); |
| 162 | } |
| 163 | |
| 164 | mrb_limit = grn_mrb_options_get_lit(mrb, mrb_options, "limit"); |
| 165 | if (!mrb_nil_p(mrb_limit)) { |
| 166 | limit = (int)mrb_integer(mrb_limit); |
| 167 | } |
| 168 | |
| 169 | mrb_max_n_unmatched_records = |
| 170 | grn_mrb_options_get_lit(mrb, mrb_options, "max_n_unmatched_records"); |
| 171 | if (!mrb_nil_p(mrb_max_n_unmatched_records)) { |
| 172 | max_n_unmatched_records = (int)mrb_integer(mrb_max_n_unmatched_records); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (limit <= 0) { |
| 177 | return mrb_int_value(mrb, n_matched_records); |
| 178 | } |
| 179 | |
| 180 | mrb_index = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@index")); |
| 181 | index = DATA_PTR(mrb_index); |
| 182 | lexicon = ((grn_ii *)index)->lexicon; |
| 183 | data_table = grn_ctx_at(ctx, grn_obj_get_range(ctx, index)); |
| 184 | |
| 185 | if (max_n_unmatched_records < 0) { |
| 186 | max_n_unmatched_records = INT32_MAX; |
| 187 | } |
| 188 | if (expr) { |
| 189 | grn_expr_executor_init(ctx, &executor, expr); |
| 190 | grn_mrb_ctx_check(mrb); |
| 191 | } |
| 192 | while ((posting = grn_index_cursor_next(ctx, index_cursor, &term_id))) { |
| 193 | if (expr) { |
| 194 | bool matched_raw = false; |
| 195 | grn_obj *matched; |
| 196 | |
| 197 | matched = grn_expr_executor_exec(ctx, &executor, posting->rid); |
| 198 | if (matched) { |
| 199 | matched_raw = grn_obj_is_true(ctx, matched); |
| 200 | } else { |
| 201 | grn_mrb_ctx_check(mrb); |
| 202 | } |
| 203 | |
| 204 | if (!matched_raw) { |
| 205 | n_unmatched_records++; |
| 206 | if (n_unmatched_records > max_n_unmatched_records) { |
| 207 | grn_expr_executor_fin(ctx, &executor); |
| 208 | grn_obj_unref(ctx, data_table); |
| 209 | return mrb_int_value(mrb, -1); |
| 210 | } |
| 211 | continue; |
| 212 | } |
| 213 | } |
| 214 | n_matched_records++; |
| 215 | if (offset > 0) { |
| 216 | offset--; |
| 217 | continue; |
| 218 | } |
| 219 | grn_posting_internal add_posting = *((grn_posting_internal *)posting); |
| 220 | add_posting.weight_float += 1; |
| 221 | grn_ii_posting_add_float(ctx, |
| 222 | (grn_posting *)(&add_posting), |
| 223 | result_set, |
| 224 | op); |
| 225 | limit--; |
| 226 | if (limit == 0) { |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | if (expr) { |
| 231 | grn_expr_executor_fin(ctx, &executor); |
| 232 | } |
| 233 | grn_ii_resolve_sel_and(ctx, result_set, op); |
| 234 | |
| 235 | grn_obj_unref(ctx, data_table); |
| 236 | |
| 237 | return mrb_int_value(mrb, n_matched_records); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | grn_mrb_index_cursor_init(grn_ctx *ctx) |
| 242 | { |
| 243 | grn_mrb_data *data = &(ctx->impl->mrb); |
| 244 | mrb_state *mrb = data->state; |
| 245 | struct RClass *module = data->module; |
| 246 | struct RClass *klass; |
| 247 | |
| 248 | klass = mrb_define_class_under(mrb, module, "IndexCursor", mrb->object_class); |
| 249 | MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); |
| 250 | |
| 251 | mrb_define_class_method(mrb, klass, "open_raw", |
| 252 | mrb_grn_index_cursor_class_open_raw, |
| 253 | MRB_ARGS_ARG(2, 1)); |
| 254 | |
| 255 | mrb_define_method(mrb, klass, "initialize", |
| 256 | mrb_grn_index_cursor_initialize, MRB_ARGS_REQ(1)); |
| 257 | mrb_define_method(mrb, klass, "close", |
| 258 | mrb_grn_index_cursor_close, MRB_ARGS_NONE()); |
| 259 | mrb_define_method(mrb, klass, "count", |
| 260 | mrb_grn_index_cursor_count, MRB_ARGS_NONE()); |
| 261 | mrb_define_method(mrb, klass, "select", |
| 262 | mrb_grn_index_cursor_select, MRB_ARGS_ARG(1, 1)); |
| 263 | } |
| 264 | #endif |