Debian Codegraph @

lib/mrb/mrb_index_column.c

raw symbols SCIP
1 /*
2 Copyright (C) 2014-2015 Brazil
3 Copyright (C) 2020-2023 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 <string.h>
23
24 #ifdef GRN_WITH_MRUBY
25 #include <mruby.h>
26 #include <mruby/array.h>
27 #include <mruby/class.h>
28 #include <mruby/data.h>
29
30 #include "mrb_ctx.h"
31 #include "mrb_converter.h"
32 #include "mrb_index_column.h"
33 #include "mrb_operator.h"
34 #include "mrb_options.h"
35
36 static struct mrb_data_type mrb_grn_index_column_type = {
37 "Groonga::IndexColumn",
38 NULL
39 };
40
41 static mrb_value
42 mrb_grn_index_column_initialize(mrb_state *mrb, mrb_value self)
43 {
44 mrb_value mrb_index_column_ptr;
45
46 mrb_get_args(mrb, "o", &mrb_index_column_ptr);
47 DATA_TYPE(self) = &mrb_grn_index_column_type;
48 DATA_PTR(self) = mrb_cptr(mrb_index_column_ptr);
49 return self;
50 }
51
52 static mrb_value
53 mrb_grn_index_column_get_lexicon(mrb_state *mrb, mrb_value self)
54 {
55 grn_obj *index_column;
56 grn_obj *lexicon;
57
58 index_column = DATA_PTR(self);
59 lexicon = ((grn_ii *)index_column)->lexicon;
60
61 return grn_mrb_value_from_grn_obj(mrb, lexicon);
62 }
63
64 static mrb_value
65 mrb_grn_index_column_get_source_ids(mrb_state *mrb, mrb_value self)
66 {
67 grn_ctx *ctx = (grn_ctx *)mrb->ud;
68 grn_obj *index_column;
69 grn_obj source_ids;
70 unsigned int i, n_ids;
71 mrb_value mrb_source_ids;
72
73 index_column = DATA_PTR(self);
74 GRN_RECORD_INIT(&source_ids, GRN_OBJ_VECTOR, GRN_DB_VOID);
75 grn_obj_get_info(ctx, index_column, GRN_INFO_SOURCE, &source_ids);
76 n_ids = GRN_BULK_VSIZE(&source_ids) / sizeof(grn_id);
77
78 mrb_source_ids = mrb_ary_new_capa(mrb, n_ids);
79 for (i = 0; i < n_ids; i++) {
80 grn_id source_id = GRN_RECORD_VALUE_AT(&source_ids, i);
81 mrb_ary_push(mrb, mrb_source_ids, mrb_int_value(mrb, source_id));
82 }
83
84 GRN_OBJ_FIN(ctx, &source_ids);
85
86 return mrb_source_ids;
87 }
88
89 static mrb_value
90 mrb_grn_index_column_estimate_size_for_term_id(mrb_state *mrb, mrb_value self)
91 {
92 grn_ctx *ctx = (grn_ctx *)mrb->ud;
93 grn_obj *index_column;
94 mrb_int term_id;
95 unsigned int size;
96
97 index_column = DATA_PTR(self);
98 mrb_get_args(mrb, "i", &term_id);
99
100 size = grn_ii_estimate_size(ctx, (grn_ii *)index_column, (grn_id)term_id);
101 return mrb_int_value(mrb, size);
102 }
103
104 static mrb_value
105 mrb_grn_index_column_estimate_size_for_query(mrb_state *mrb, mrb_value self)
106 {
107 grn_ctx *ctx = (grn_ctx *)mrb->ud;
108 grn_obj *index_column;
109 grn_obj *lexicon;
110 mrb_value mrb_query;
111 void *query;
112 unsigned int query_size;
113 grn_mrb_value_to_raw_data_buffer buffer;
114 mrb_sym keyword_mode = mrb_intern_lit(mrb, "mode");
115 mrb_value mrb_mode;
116 const mrb_kwargs kwargs = {1, 0, &keyword_mode, &mrb_mode, NULL};
117 grn_search_optarg optarg;
118 unsigned int size;
119
120 index_column = DATA_PTR(self);
121 mrb_get_args(mrb, "o:", &mrb_query, &kwargs);
122
123 lexicon = grn_ctx_at(ctx, index_column->header.domain);
124 grn_mrb_value_to_raw_data_buffer_init(mrb, &buffer);
125 grn_mrb_value_to_raw_data(mrb, "query", mrb_query, lexicon->header.domain,
126 &buffer, &query, &query_size);
127 grn_obj_unref(ctx, lexicon);
128
129 memset(&optarg, 0, sizeof(grn_search_optarg));
130 optarg.mode = GRN_OP_EXACT;
131
132 if (!mrb_undef_p(mrb_mode) && !mrb_nil_p(mrb_mode)) {
133 optarg.mode = grn_mrb_value_to_operator(mrb, mrb_mode);
134 }
135
136 size = grn_ii_estimate_size_for_query(ctx, (grn_ii *)index_column,
137 query, query_size, &optarg);
138 grn_mrb_value_to_raw_data_buffer_fin(mrb, &buffer);
139
140 grn_mrb_ctx_check(mrb);
141
142 return mrb_int_value(mrb, size);
143 }
144
145 static mrb_value
146 mrb_grn_index_column_estimate_size_for_lexicon_cursor(mrb_state *mrb,
147 mrb_value self)
148 {
149 grn_ctx *ctx = (grn_ctx *)mrb->ud;
150 grn_obj *index_column;
151 mrb_value mrb_lexicon_cursor;
152 grn_table_cursor *lexicon_cursor;
153 unsigned int size;
154
155 index_column = DATA_PTR(self);
156 mrb_get_args(mrb, "o", &mrb_lexicon_cursor);
157
158 lexicon_cursor = DATA_PTR(mrb_lexicon_cursor);
159 size = grn_ii_estimate_size_for_lexicon_cursor(ctx,
160 (grn_ii *)index_column,
161 lexicon_cursor);
162 return mrb_int_value(mrb, size);
163 }
164
165 void
166 grn_mrb_index_column_init(grn_ctx *ctx)
167 {
168 grn_mrb_data *data = &(ctx->impl->mrb);
169 mrb_state *mrb = data->state;
170 struct RClass *module = data->module;
171 struct RClass *column_class;
172 struct RClass *klass;
173
174 column_class = mrb_class_get_under(mrb, module, "Column");
175 klass = mrb_define_class_under(mrb, module, "IndexColumn", column_class);
176 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
177 mrb_define_method(mrb, klass, "initialize",
178 mrb_grn_index_column_initialize, MRB_ARGS_REQ(1));
179
180 mrb_define_method(mrb, klass, "lexicon",
181 mrb_grn_index_column_get_lexicon,
182 MRB_ARGS_NONE());
183
184 mrb_define_method(mrb, klass, "source_ids",
185 mrb_grn_index_column_get_source_ids,
186 MRB_ARGS_NONE());
187
188 mrb_define_method(mrb, klass, "estimate_size_for_term_id",
189 mrb_grn_index_column_estimate_size_for_term_id,
190 MRB_ARGS_REQ(1));
191 mrb_define_method(mrb, klass, "estimate_size_for_query",
192 mrb_grn_index_column_estimate_size_for_query,
193 MRB_ARGS_ARG(1, 1));
194 mrb_define_method(mrb, klass, "estimate_size_for_lexicon_cursor",
195 mrb_grn_index_column_estimate_size_for_lexicon_cursor,
196 MRB_ARGS_REQ(1));
197 }
198 #endif