Debian Codegraph @

plugins/functions/index_column.c

raw symbols SCIP
1 /*
2 Copyright (C) 2017 Brazil
3 Copyright (C) 2019-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 #ifdef GRN_EMBEDDED
21 # define GRN_PLUGIN_FUNCTION_TAG functions_time
22 #endif
23
24 #include <groonga/plugin.h>
25
26 static grn_rc
27 selector_index_column_df_ratio_between(grn_ctx *ctx,
28 grn_obj *table,
29 grn_obj *index,
30 int n_args,
31 grn_obj **args,
32 grn_obj *res,
33 grn_operator op)
34 {
35 grn_rc rc = GRN_SUCCESS;
36 grn_obj *index_column;
37 grn_ii *ii;
38 double min;
39 double max;
40 grn_obj *source_table;
41 unsigned int n_documents;
42 grn_posting posting;
43
44 if ((n_args - 1) != 3) {
45 GRN_PLUGIN_ERROR(ctx,
46 GRN_INVALID_ARGUMENT,
47 "index_column_df_ratio_between(): "
48 "wrong number of arguments (%d for 3)", n_args - 1);
49 rc = ctx->rc;
50 goto exit;
51 }
52
53 index_column = args[1];
54 ii = (grn_ii *)index_column;
55 min = GRN_FLOAT_VALUE(args[2]);
56 max = GRN_FLOAT_VALUE(args[3]);
57
58 source_table = grn_ctx_at(ctx, grn_obj_get_range(ctx, index_column));
59 n_documents = grn_table_size(ctx, source_table);
60 memset(&posting, 0, sizeof(grn_posting));
61 posting.sid = 1;
62
63 if (op == GRN_OP_AND) {
64 GRN_TABLE_EACH_BEGIN(ctx, res, cursor, record_id) {
65 void *key;
66 grn_id term_id;
67 uint32_t n_match_documents;
68 double df_ratio;
69
70 grn_table_cursor_get_key(ctx, cursor, &key);
71 term_id = *(grn_id *)key;
72 n_match_documents = grn_ii_estimate_size(ctx, ii, term_id);
73 if (n_match_documents > n_documents) {
74 n_match_documents = n_documents;
75 }
76 df_ratio = (double)n_match_documents / (double)n_documents;
77 if (min <= df_ratio && df_ratio <= max) {
78 posting.rid = term_id;
79 grn_ii_posting_add(ctx, &posting, (grn_hash *)res, op);
80 }
81 } GRN_TABLE_EACH_END(ctx, cursor);
82 grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op);
83 } else {
84 GRN_TABLE_EACH_BEGIN(ctx, table, cursor, term_id) {
85 uint32_t n_match_documents;
86 double df_ratio;
87
88 n_match_documents = grn_ii_estimate_size(ctx, ii, term_id);
89 if (n_match_documents > n_documents) {
90 n_match_documents = n_documents;
91 }
92 df_ratio = (double)n_match_documents / (double)n_documents;
93 {
94 void *key;
95 int key_size;
96 key_size = grn_table_cursor_get_key(ctx, cursor, &key);
97 }
98 if (min <= df_ratio && df_ratio <= max) {
99 posting.rid = term_id;
100 grn_ii_posting_add(ctx, &posting, (grn_hash *)res, op);
101 }
102 } GRN_TABLE_EACH_END(ctx, cursor);
103 }
104
105 exit :
106 return rc;
107 }
108
109 typedef struct {
110 grn_id term_id;
111 grn_obj *term_table;
112 grn_obj *index_column;
113 } caller_index_info;
114
115 static void
116 caller_index_info_fin(grn_ctx *ctx,
117 caller_index_info *caller_index_info)
118 {
119 if (caller_index_info->index_column) {
120 grn_obj_unref(ctx, caller_index_info->index_column);
121 }
122 if (caller_index_info->term_table) {
123 grn_obj_unref(ctx, caller_index_info->term_table);
124 }
125 }
126
127 static grn_rc
128 caller_index_info_init(grn_ctx *ctx,
129 caller_index_info *caller_index_info,
130 grn_obj *index_column_name,
131 grn_user_data *user_data,
132 const char *error_message_label)
133 {
134 caller_index_info->term_id = GRN_ID_NIL;
135 caller_index_info->term_table = NULL;
136 caller_index_info->index_column = NULL;
137
138 {
139 grn_obj *expr;
140 grn_obj *variable;
141
142 expr = grn_plugin_proc_get_caller(ctx, user_data);
143 if (!expr) {
144 GRN_PLUGIN_ERROR(ctx,
145 GRN_INVALID_ARGUMENT,
146 "%s: "
147 "called directly", error_message_label);
148 return ctx->rc;
149 }
150
151 variable = grn_expr_get_var_by_offset(ctx, expr, 0);
152 if (!variable) {
153 GRN_PLUGIN_ERROR(ctx,
154 GRN_INVALID_ARGUMENT,
155 "%s: "
156 "caller expression must have target record information",
157 error_message_label);
158 return ctx->rc;
159 }
160
161 caller_index_info->term_table = grn_ctx_at(ctx, variable->header.domain);
162 caller_index_info->term_id = GRN_RECORD_VALUE(variable);
163 while (true) {
164 grn_obj *key_type;
165
166 key_type = grn_ctx_at(ctx, caller_index_info->term_table->header.domain);
167 if (!grn_obj_is_table(ctx, key_type)) {
168 grn_obj_unref(ctx, key_type);
169 break;
170 }
171
172 grn_table_get_key(ctx,
173 caller_index_info->term_table,
174 caller_index_info->term_id,
175 &(caller_index_info->term_id),
176 sizeof(grn_id));
177 grn_obj_unref(ctx, caller_index_info->term_table);
178 caller_index_info->term_table = key_type;
179 }
180 }
181
182 if (!grn_obj_is_text_family_bulk(ctx, index_column_name)) {
183 grn_obj inspected;
184 GRN_TEXT_INIT(&inspected, 0);
185 grn_inspect(ctx, &inspected, index_column_name);
186 GRN_PLUGIN_ERROR(ctx,
187 GRN_INVALID_ARGUMENT,
188 "%s: "
189 "the first argument must be index column name: %.*s",
190 error_message_label,
191 (int)GRN_TEXT_LEN(&inspected),
192 GRN_TEXT_VALUE(&inspected));
193 GRN_OBJ_FIN(ctx, &inspected);
194 caller_index_info_fin(ctx, caller_index_info);
195 return ctx->rc;
196 }
197
198 caller_index_info->index_column =
199 grn_obj_column(ctx,
200 caller_index_info->term_table,
201 GRN_TEXT_VALUE(index_column_name),
202 (uint32_t)GRN_TEXT_LEN(index_column_name));
203 if (!caller_index_info->index_column) {
204 GRN_PLUGIN_ERROR(ctx,
205 GRN_INVALID_ARGUMENT,
206 "%s: "
207 "nonexistent object: <%.*s>",
208 error_message_label,
209 (int)GRN_TEXT_LEN(index_column_name),
210 GRN_TEXT_VALUE(index_column_name));
211 caller_index_info_fin(ctx, caller_index_info);
212 return ctx->rc;
213 }
214
215 return GRN_SUCCESS;
216 }
217
218 static grn_obj *
219 func_index_column_df_ratio(grn_ctx *ctx,
220 int n_args,
221 grn_obj **args,
222 grn_user_data *user_data)
223 {
224 grn_ii *ii;
225 caller_index_info caller_index_info;
226
227 if (n_args != 1) {
228 GRN_PLUGIN_ERROR(ctx,
229 GRN_INVALID_ARGUMENT,
230 "index_column_df_ratio(): "
231 "wrong number of arguments (%d for 1)", n_args - 1);
232 return NULL;
233 }
234
235 if (caller_index_info_init(ctx,
236 &caller_index_info,
237 args[0],
238 user_data,
239 "index_column_df_ratio()") != GRN_SUCCESS) {
240 return NULL;
241 }
242
243 ii = (grn_ii *)caller_index_info.index_column;
244
245 {
246 grn_obj *source_table;
247 unsigned int n_documents;
248 uint32_t n_match_documents;
249 double df_ratio;
250 grn_obj *df_ratio_value;
251
252 source_table = grn_ctx_at(ctx, grn_obj_get_range(ctx, caller_index_info.index_column));
253 n_documents = grn_table_size(ctx, source_table);
254 n_match_documents = grn_ii_estimate_size(ctx, ii, caller_index_info.term_id);
255 if (n_match_documents > n_documents) {
256 n_match_documents = n_documents;
257 }
258 df_ratio = (double)n_match_documents / (double)n_documents;
259 grn_obj_unref(ctx, source_table);
260
261 df_ratio_value = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_FLOAT, 0);
262 if (df_ratio_value) {
263 GRN_FLOAT_SET(ctx, df_ratio_value, df_ratio);
264 }
265 caller_index_info_fin(ctx, &caller_index_info);
266 return df_ratio_value;
267 }
268 }
269
270 static grn_obj *
271 func_index_column_source_records(grn_ctx *ctx,
272 int n_args,
273 grn_obj **args,
274 grn_user_data *user_data)
275 {
276 grn_ii *ii;
277 caller_index_info caller_index_info;
278 int64_t limit = -1;
279
280 if (n_args < 1 || n_args > 2) {
281 GRN_PLUGIN_ERROR(ctx,
282 GRN_INVALID_ARGUMENT,
283 "index_column_source_records(): "
284 "wrong number of arguments (%d for 1..2)", n_args - 1);
285 return NULL;
286 }
287
288 if (caller_index_info_init(ctx,
289 &caller_index_info,
290 args[0],
291 user_data,
292 "index_column_source_records()") != GRN_SUCCESS) {
293 return NULL;
294 }
295
296 if (n_args == 2) {
297 grn_obj *options = args[1];
298
299 switch (options->header.type) {
300 case GRN_TABLE_HASH_KEY :
301 {
302 grn_hash_cursor *cursor;
303 void *key;
304 int key_size;
305 cursor = grn_hash_cursor_open(ctx, (grn_hash *)options,
306 NULL, 0, NULL, 0,
307 0, -1, 0);
308 if (!cursor) {
309 GRN_PLUGIN_ERROR(ctx, GRN_NO_MEMORY_AVAILABLE,
310 "index_column_source_records(): failed to open cursor for options");
311 caller_index_info_fin(ctx, &caller_index_info);
312 return NULL;
313 }
314 while (grn_hash_cursor_next(ctx, cursor) != GRN_ID_NIL) {
315 void *value;
316 grn_obj *proc_value;
317 grn_hash_cursor_get_key_value(ctx, cursor, &key, &key_size, &value);
318 proc_value = value;
319
320 #define KEY_EQUAL(name) \
321 (key_size == strlen(name) && memcmp(key, name, strlen(name)) == 0)
322
323 if (KEY_EQUAL("limit")) {
324 limit = grn_plugin_proc_get_value_int64(ctx,
325 proc_value,
326 limit,
327 "index_column_source_records()");
328 if (ctx->rc != GRN_SUCCESS) {
329 grn_hash_cursor_close(ctx, cursor);
330 caller_index_info_fin(ctx, &caller_index_info);
331 return NULL;
332 }
333 } else {
334 GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
335 "index_column_source_records(): unknown option name: <%.*s>",
336 key_size, (char *)key);
337 grn_hash_cursor_close(ctx, cursor);
338 caller_index_info_fin(ctx, &caller_index_info);
339 return NULL;
340 }
341 #undef KEY_EQUAL
342 }
343 grn_hash_cursor_close(ctx, cursor);
344 }
345 break;
346 default :
347 {
348 grn_obj inspected;
349 GRN_TEXT_INIT(&inspected, 0);
350 grn_inspect(ctx, &inspected, options);
351 GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
352 "index_column_source_records(): "
353 "2nd argument must be object literal: <%.*s>",
354 (int)GRN_TEXT_LEN(&inspected),
355 GRN_TEXT_VALUE(&inspected));
356 GRN_OBJ_FIN(ctx, &inspected);
357 caller_index_info_fin(ctx, &caller_index_info);
358 return NULL;
359 }
360 }
361 }
362
363 ii = (grn_ii *)caller_index_info.index_column;
364
365 {
366 grn_obj *records;
367
368 records = grn_plugin_proc_alloc(ctx, user_data,
369 grn_obj_get_range(ctx, caller_index_info.index_column),
370 GRN_OBJ_VECTOR);
371 if (!records) {
372 caller_index_info_fin(ctx, &caller_index_info);
373 return NULL;
374 }
375
376 {
377 grn_ii_cursor *ii_cursor;
378 grn_posting *posting;
379 int64_t n_records = 0;
380 ii_cursor = grn_ii_cursor_open(ctx,
381 ii,
382 caller_index_info.term_id,
383 GRN_ID_NIL,
384 GRN_ID_MAX,
385 (int)grn_ii_get_n_elements(ctx, ii),
386 0);
387 if (ii_cursor) {
388 while ((posting = grn_ii_cursor_next(ctx, ii_cursor))) {
389 if (limit > 0 && n_records >= limit) {
390 break;
391 }
392 GRN_RECORD_PUT(ctx, records, posting->rid);
393 n_records++;
394 }
395 grn_ii_cursor_close(ctx, ii_cursor);
396 }
397 }
398 caller_index_info_fin(ctx, &caller_index_info);
399 return records;
400 }
401 }
402
403 static grn_obj *
404 func_index_column_have_source_record(grn_ctx *ctx,
405 int n_args,
406 grn_obj **args,
407 grn_user_data *user_data)
408 {
409 grn_ii *ii;
410 caller_index_info caller_index_info;
411
412 if (n_args != 1) {
413 GRN_PLUGIN_ERROR(ctx,
414 GRN_INVALID_ARGUMENT,
415 "index_column_have_source_record(): "
416 "wrong number of arguments (%d for 1)", n_args - 1);
417 return NULL;
418 }
419
420 grn_rc rc = caller_index_info_init(ctx,
421 &caller_index_info,
422 args[0],
423 user_data,
424 "index_column_have_source_record()");
425 if (rc != GRN_SUCCESS) {
426 return NULL;
427 }
428
429 ii = (grn_ii *)caller_index_info.index_column;
430 grn_obj *have =
431 grn_plugin_proc_alloc(ctx, user_data, GRN_DB_BOOL, 0);
432 if (!have) {
433 caller_index_info_fin(ctx, &caller_index_info);
434 return NULL;
435 }
436
437 GRN_BOOL_SET(ctx, have, false);
438 {
439 grn_ii_cursor *ii_cursor;
440 ii_cursor = grn_ii_cursor_open(ctx,
441 ii,
442 caller_index_info.term_id,
443 GRN_ID_NIL,
444 GRN_ID_MAX,
445 (int)grn_ii_get_n_elements(ctx, ii),
446 0);
447 if (ii_cursor) {
448 grn_posting *posting;
449 while ((posting = grn_ii_cursor_next(ctx, ii_cursor))) {
450 GRN_BOOL_SET(ctx, have, true);
451 true;
452 }
453 grn_ii_cursor_close(ctx, ii_cursor);
454 }
455 }
456 caller_index_info_fin(ctx, &caller_index_info);
457 return have;
458 }
459
460 grn_rc
461 GRN_PLUGIN_INIT(grn_ctx *ctx)
462 {
463 return ctx->rc;
464 }
465
466 grn_rc
467 GRN_PLUGIN_REGISTER(grn_ctx *ctx)
468 {
469 grn_obj *selector_proc;
470
471 selector_proc = grn_proc_create(ctx, "index_column_df_ratio_between", -1,
472 GRN_PROC_FUNCTION,
473 NULL, NULL, NULL, 0, NULL);
474 grn_proc_set_selector(ctx, selector_proc,
475 selector_index_column_df_ratio_between);
476 grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_NOP);
477
478 grn_proc_create(ctx, "index_column_df_ratio", -1,
479 GRN_PROC_FUNCTION,
480 func_index_column_df_ratio, NULL, NULL, 0, NULL);
481
482 grn_proc_create(ctx, "index_column_source_records", -1,
483 GRN_PROC_FUNCTION,
484 func_index_column_source_records, NULL, NULL, 0, NULL);
485
486 grn_proc_create(ctx, "index_column_have_source_record", -1,
487 GRN_PROC_FUNCTION,
488 func_index_column_have_source_record, NULL, NULL, 0, NULL);
489
490 return ctx->rc;
491 }
492
493 grn_rc
494 GRN_PLUGIN_FIN(grn_ctx *ctx)
495 {
496 return GRN_SUCCESS;
497 }