Debian Codegraph @

lib/mrb/mrb_table.c

raw symbols SCIP
1 /*
2 Copyright (C) 2014-2018 Brazil
3 Copyright (C) 2018-2024 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 <string.h>
22
23 #ifdef GRN_WITH_MRUBY
24 #include <mruby.h>
25 #include <mruby/class.h>
26 #include <mruby/data.h>
27 #include <mruby/hash.h>
28 #include <mruby/array.h>
29 #include <mruby/string.h>
30
31 #include "mrb_ctx.h"
32 #include "mrb_table.h"
33 #include "mrb_converter.h"
34 #include "mrb_options.h"
35
36 static mrb_value
37 mrb_grn_table_is_id(mrb_state *mrb, mrb_value self)
38 {
39 grn_ctx *ctx = (grn_ctx *)mrb->ud;
40 grn_obj *table;
41 mrb_int mrb_record_id;
42 grn_id record_id;
43 grn_id real_record_id;
44
45 mrb_get_args(mrb, "i", &mrb_record_id);
46
47 table = DATA_PTR(self);
48 record_id = (grn_id)mrb_record_id;
49 real_record_id = grn_table_at(ctx, table, record_id);
50 if (real_record_id == record_id) {
51 return mrb_true_value();
52 } else {
53 return mrb_false_value();
54 }
55 }
56
57 static mrb_value
58 mrb_grn_table_have_tokenizer(mrb_state *mrb, mrb_value self)
59 {
60 grn_ctx *ctx = (grn_ctx *)(mrb->ud);
61 grn_obj *table;
62
63 table = DATA_PTR(self);
64 return mrb_bool_value(grn_table_have_tokenizer(ctx, table));
65 }
66
67 static mrb_value
68 mrb_grn_table_find_column(mrb_state *mrb, mrb_value self)
69 {
70 grn_ctx *ctx = (grn_ctx *)mrb->ud;
71 grn_obj *table;
72 mrb_value mrb_column_name;
73 grn_obj *column;
74
75 mrb_get_args(mrb, "o", &mrb_column_name);
76
77 table = DATA_PTR(self);
78 switch (mrb_type(mrb_column_name)) {
79 case MRB_TT_SYMBOL :
80 {
81 const char *name;
82 mrb_int name_length;
83
84 name = mrb_sym2name_len(mrb, mrb_symbol(mrb_column_name), &name_length);
85 column = grn_obj_column(ctx, table, name, (uint32_t)name_length);
86 }
87 break;
88 case MRB_TT_STRING :
89 column = grn_obj_column(ctx, table,
90 RSTRING_PTR(mrb_column_name),
91 RSTRING_LEN(mrb_column_name));
92 break;
93 default :
94 mrb_raisef(mrb, E_ARGUMENT_ERROR,
95 "column name must be Symbol or String: %S",
96 mrb_column_name);
97 break;
98 }
99 grn_mrb_ctx_check(mrb);
100
101 return grn_mrb_value_from_grn_obj(mrb, column);
102 }
103
104 static mrb_value
105 mrb_grn_table_get_column_ids(mrb_state *mrb, mrb_value self)
106 {
107 grn_ctx *ctx = (grn_ctx *)mrb->ud;
108 grn_obj *table;
109 grn_hash *columns;
110 int n_columns;
111 mrb_value mrb_column_ids;
112
113 table = DATA_PTR(self);
114 columns = grn_hash_create(ctx, NULL, sizeof(grn_id), 0,
115 GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY);
116 if (!columns) {
117 grn_mrb_ctx_check(mrb);
118 return mrb_ary_new(mrb);
119 }
120
121 n_columns = grn_table_columns(ctx, table, "", 0, (grn_obj *)columns);
122 mrb_column_ids = mrb_ary_new_capa(mrb, n_columns);
123 {
124 grn_id *key;
125 GRN_HASH_EACH(ctx, columns, id, &key, NULL, NULL, {
126 mrb_ary_push(mrb, mrb_column_ids, mrb_int_value(mrb, *key));
127 });
128 }
129 grn_hash_close(ctx, columns);
130
131 grn_mrb_ctx_check(mrb);
132
133 return mrb_column_ids;
134 }
135
136 static mrb_value
137 mrb_grn_table_create_column(mrb_state *mrb, mrb_value self)
138 {
139 grn_ctx *ctx = (grn_ctx *)mrb->ud;
140 grn_obj *table;
141 mrb_value mrb_name;
142 mrb_int flags;
143 mrb_value mrb_type;
144 grn_obj *type;
145 grn_obj *column;
146
147 mrb_get_args(mrb, "oio", &mrb_name, &flags, &mrb_type);
148
149 table = DATA_PTR(self);
150 type = DATA_PTR(mrb_type);
151 column = grn_column_create(ctx, table,
152 RSTRING_PTR(mrb_name),
153 RSTRING_LEN(mrb_name),
154 NULL,
155 (grn_column_flags)flags,
156 type);
157 grn_mrb_ctx_check(mrb);
158
159 return grn_mrb_value_from_grn_obj(mrb, column);
160 }
161
162 static mrb_value
163 mrb_grn_table_is_locked(mrb_state *mrb, mrb_value self)
164 {
165 grn_ctx *ctx = (grn_ctx *)mrb->ud;
166 unsigned int is_locked;
167
168 is_locked = grn_obj_is_locked(ctx, DATA_PTR(self));
169 grn_mrb_ctx_check(mrb);
170
171 return mrb_bool_value(is_locked != 0);
172 }
173
174 static mrb_value
175 mrb_grn_table_clear_lock(mrb_state *mrb, mrb_value self)
176 {
177 grn_ctx *ctx = (grn_ctx *)mrb->ud;
178 grn_obj_clear_lock(ctx, DATA_PTR(self));
179 grn_mrb_ctx_check(mrb);
180
181 return mrb_nil_value();
182 }
183
184 static mrb_value
185 mrb_grn_table_get_size(mrb_state *mrb, mrb_value self)
186 {
187 grn_ctx *ctx = (grn_ctx *)mrb->ud;
188 unsigned int size;
189
190 size = grn_table_size(ctx, DATA_PTR(self));
191 grn_mrb_ctx_check(mrb);
192
193 return mrb_int_value(mrb, size);
194 }
195
196 static mrb_value
197 mrb_grn_table_is_empty(mrb_state *mrb, mrb_value self)
198 {
199 grn_ctx *ctx = (grn_ctx *)mrb->ud;
200 unsigned int size;
201
202 size = grn_table_size(ctx, DATA_PTR(self));
203 grn_mrb_ctx_check(mrb);
204
205 return mrb_bool_value(size == 0);
206 }
207
208 static mrb_value
209 mrb_grn_table_select(mrb_state *mrb, mrb_value self)
210 {
211 grn_ctx *ctx = (grn_ctx *)mrb->ud;
212 grn_obj *table;
213 grn_obj *expr;
214 grn_obj *result = NULL;
215 grn_operator operator = GRN_OP_OR;
216 mrb_value mrb_expr;
217 mrb_value mrb_options = mrb_nil_value();
218
219 table = DATA_PTR(self);
220 mrb_get_args(mrb, "o|H", &mrb_expr, &mrb_options);
221
222 expr = DATA_PTR(mrb_expr);
223
224 if (!mrb_nil_p(mrb_options)) {
225 mrb_value mrb_result;
226 mrb_value mrb_operator;
227
228 mrb_result = grn_mrb_options_get_lit(mrb, mrb_options, "result");
229 if (!mrb_nil_p(mrb_result)) {
230 result = DATA_PTR(mrb_result);
231 }
232
233 mrb_operator = grn_mrb_options_get_lit(mrb, mrb_options, "operator");
234 if (!mrb_nil_p(mrb_operator)) {
235 operator = mrb_integer(mrb_operator);
236 }
237 }
238
239 result = grn_table_select(ctx, table, expr, result, operator);
240 if (ctx->rc != GRN_SUCCESS) {
241 grn_mrb_ctx_check(mrb);
242 }
243
244 return grn_mrb_value_from_grn_obj(mrb, result);
245 }
246
247 static mrb_value
248 mrb_grn_table_sort_raw(mrb_state *mrb, mrb_value self)
249 {
250 grn_ctx *ctx = (grn_ctx *)mrb->ud;
251 grn_obj *table;
252 mrb_value mrb_keys;
253 grn_table_sort_key *keys;
254 int i, n_keys;
255 mrb_int offset;
256 mrb_int limit;
257 mrb_value mrb_result;
258 grn_obj *result;
259
260 table = DATA_PTR(self);
261 mrb_get_args(mrb, "oiio", &mrb_keys, &offset, &limit, &mrb_result);
262
263 mrb_keys = mrb_convert_type(mrb, mrb_keys,
264 MRB_TT_ARRAY, "Array", "to_ary");
265
266 n_keys = RARRAY_LEN(mrb_keys);
267 keys = GRN_MALLOCN(grn_table_sort_key, n_keys);
268 for (i = 0; i < n_keys; i++) {
269 grn_memcpy(&(keys[i]),
270 DATA_PTR(RARRAY_PTR(mrb_keys)[i]),
271 sizeof(grn_table_sort_key));
272 }
273 result = DATA_PTR(mrb_result);
274 grn_table_sort(ctx, table, (int)offset, (int)limit, result, keys, n_keys);
275 GRN_FREE(keys);
276 grn_mrb_ctx_check(mrb);
277
278 return mrb_result;
279 }
280
281 static mrb_value
282 mrb_grn_table_group_raw(mrb_state *mrb, mrb_value self)
283 {
284 grn_ctx *ctx = (grn_ctx *)mrb->ud;
285 grn_obj *table;
286 mrb_value mrb_keys;
287 grn_table_sort_key *keys;
288 int i, n_keys;
289 mrb_value mrb_result;
290 grn_table_group_result *result;
291
292 table = DATA_PTR(self);
293 mrb_get_args(mrb, "oo", &mrb_keys, &mrb_result);
294
295 mrb_keys = mrb_convert_type(mrb, mrb_keys,
296 MRB_TT_ARRAY, "Array", "to_ary");
297
298 n_keys = RARRAY_LEN(mrb_keys);
299 keys = GRN_MALLOCN(grn_table_sort_key, n_keys);
300 for (i = 0; i < n_keys; i++) {
301 grn_memcpy(&(keys[i]),
302 DATA_PTR(RARRAY_PTR(mrb_keys)[i]),
303 sizeof(grn_table_sort_key));
304 }
305 result = DATA_PTR(mrb_result);
306 grn_table_group(ctx, table, keys, n_keys, result, 1);
307 GRN_FREE(keys);
308 grn_mrb_ctx_check(mrb);
309
310 return mrb_result;
311 }
312
313 static mrb_value
314 mrb_grn_table_delete(mrb_state *mrb, mrb_value self)
315 {
316 grn_ctx *ctx = (grn_ctx *)mrb->ud;
317 grn_obj *table;
318 mrb_value mrb_options;
319 mrb_value mrb_id;
320 mrb_value mrb_key;
321 mrb_value mrb_expression;
322
323 table = DATA_PTR(self);
324 mrb_get_args(mrb, "H", &mrb_options);
325
326 mrb_id = grn_mrb_options_get_lit(mrb, mrb_options, "id");
327 if (!mrb_nil_p(mrb_id)) {
328 grn_table_delete_by_id(ctx, table, (grn_id)mrb_integer(mrb_id));
329 grn_mrb_ctx_check(mrb);
330 return mrb_nil_value();
331 }
332
333 mrb_key = grn_mrb_options_get_lit(mrb, mrb_options, "key");
334 if (!mrb_nil_p(mrb_key)) {
335 grn_id key_domain_id;
336 void *key;
337 unsigned int key_size;
338 grn_mrb_value_to_raw_data_buffer buffer;
339
340 key_domain_id = table->header.domain;
341 grn_mrb_value_to_raw_data_buffer_init(mrb, &buffer);
342 grn_mrb_value_to_raw_data(mrb, "key", mrb_key, key_domain_id,
343 &buffer, &key, &key_size);
344 grn_table_delete(ctx, table, key, key_size);
345 grn_mrb_value_to_raw_data_buffer_fin(mrb, &buffer);
346 grn_mrb_ctx_check(mrb);
347 return mrb_nil_value();
348 }
349
350 mrb_expression = grn_mrb_options_get_lit(mrb, mrb_options, "expression");
351 if (!mrb_nil_p(mrb_expression)) {
352 grn_obj *expression;
353 grn_obj *selected_records;
354 grn_table_cursor *cursor;
355
356 expression = DATA_PTR(mrb_expression);
357 selected_records = grn_table_select(ctx, table, expression, NULL, GRN_OP_OR);
358 grn_mrb_ctx_check(mrb);
359 cursor = grn_table_cursor_open(ctx, selected_records,
360 NULL, 0,
361 NULL, 0,
362 0, -1, 0);
363 if (cursor) {
364 while (grn_table_cursor_next(ctx, cursor) != GRN_ID_NIL) {
365 grn_id *id;
366 grn_table_cursor_get_key(ctx, cursor, (void **)&id);
367 grn_table_delete_by_id(ctx, table, *id);
368 }
369 grn_table_cursor_close(ctx, cursor);
370 }
371 grn_obj_unlink(ctx, selected_records);
372 grn_mrb_ctx_check(mrb);
373
374 return mrb_nil_value();
375 }
376
377 mrb_raisef(mrb, E_ARGUMENT_ERROR,
378 "must have :id, :key or :expression: %S",
379 mrb_options);
380
381 return mrb_nil_value();
382 }
383
384 static mrb_value
385 mrb_grn_table_truncate(mrb_state *mrb, mrb_value self)
386 {
387 grn_ctx *ctx = (grn_ctx *)mrb->ud;
388 grn_obj *table;
389
390 table = DATA_PTR(self);
391 grn_table_truncate(ctx, table);
392 grn_mrb_ctx_check(mrb);
393 return mrb_nil_value();
394 }
395
396 static mrb_value
397 mrb_grn_table_apply_expression(mrb_state *mrb, mrb_value self)
398 {
399 grn_ctx *ctx = (grn_ctx *)mrb->ud;
400 mrb_value mrb_output_column;
401 mrb_value mrb_expression;
402 grn_obj *table;
403 grn_obj *output_column = NULL;
404 grn_obj *expression = NULL;
405
406 mrb_get_args(mrb, "oo", &mrb_output_column, &mrb_expression);
407
408 table = DATA_PTR(self);
409 output_column = GRN_MRB_DATA_PTR(mrb_output_column);
410 expression = GRN_MRB_DATA_PTR(mrb_expression);
411 grn_table_apply_expr(ctx, table, output_column, expression);
412 grn_mrb_ctx_check(mrb);
413
414 return mrb_nil_value();
415 }
416
417 static mrb_value
418 mrb_grn_table_apply_window_function_raw(mrb_state *mrb, mrb_value self)
419 {
420 grn_ctx *ctx = (grn_ctx *)mrb->ud;
421 mrb_value mrb_output_column;
422 mrb_value mrb_window_definition;
423 mrb_value mrb_window_function_call;
424 grn_obj *table;
425 grn_obj *output_column = NULL;
426 grn_window_definition *window_definition = NULL;
427 grn_obj *window_function_call = NULL;
428
429 mrb_get_args(mrb, "ooo",
430 &mrb_output_column,
431 &mrb_window_definition,
432 &mrb_window_function_call);
433
434 table = DATA_PTR(self);
435 output_column = GRN_MRB_DATA_PTR(mrb_output_column);
436 window_definition = GRN_MRB_DATA_PTR(mrb_window_definition);
437 window_function_call = GRN_MRB_DATA_PTR(mrb_window_function_call);
438 grn_table_apply_window_function(ctx,
439 table,
440 output_column,
441 window_definition,
442 window_function_call);
443 grn_mrb_ctx_check(mrb);
444
445 return mrb_nil_value();
446 }
447
448 static mrb_value
449 mrb_grn_table_parse_output_columns(mrb_state *mrb, mrb_value self)
450 {
451 grn_ctx *ctx = (grn_ctx *)mrb->ud;
452 grn_mrb_data *data = &(ctx->impl->mrb);
453 mrb_value mrb_raw_output_columns;
454 grn_obj *table;
455 grn_obj *output_columns;
456 struct RClass *klass;
457 mrb_value mrb_new_arguments[1];
458
459 mrb_get_args(mrb, "S", &mrb_raw_output_columns);
460
461 table = DATA_PTR(self);
462 output_columns = grn_output_columns_parse(ctx,
463 table,
464 RSTRING_PTR(mrb_raw_output_columns),
465 RSTRING_LEN(mrb_raw_output_columns));
466 grn_mrb_ctx_check(mrb);
467
468 klass = mrb_class_get_under(mrb, data->module, "OutputColumns");
469 mrb_new_arguments[0] = mrb_cptr_value(mrb, output_columns);
470 return mrb_obj_new(mrb, klass, 1, mrb_new_arguments);
471 }
472
473 void
474 grn_mrb_table_init(grn_ctx *ctx)
475 {
476 grn_mrb_data *data = &(ctx->impl->mrb);
477 mrb_state *mrb = data->state;
478 struct RClass *module = data->module;
479 struct RClass *object_class = data->object_class;
480 struct RClass *klass;
481
482 klass = mrb_define_class_under(mrb, module, "Table", object_class);
483 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
484
485 mrb_define_method(mrb, klass, "id?",
486 mrb_grn_table_is_id, MRB_ARGS_REQ(1));
487 mrb_define_method(mrb, klass, "have_tokenizer?",
488 mrb_grn_table_have_tokenizer, MRB_ARGS_NONE());
489
490 mrb_define_method(mrb, klass, "find_column",
491 mrb_grn_table_find_column, MRB_ARGS_REQ(1));
492 mrb_define_method(mrb, klass, "column_ids",
493 mrb_grn_table_get_column_ids, MRB_ARGS_NONE());
494
495 mrb_define_method(mrb, klass, "create_column",
496 mrb_grn_table_create_column, MRB_ARGS_REQ(3));
497
498 mrb_define_method(mrb, klass, "locked?",
499 mrb_grn_table_is_locked, MRB_ARGS_NONE());
500 mrb_define_method(mrb, klass, "clear_lock",
501 mrb_grn_table_clear_lock, MRB_ARGS_NONE());
502
503 mrb_define_method(mrb, klass, "size",
504 mrb_grn_table_get_size, MRB_ARGS_NONE());
505 mrb_define_method(mrb, klass, "empty?",
506 mrb_grn_table_is_empty, MRB_ARGS_NONE());
507
508 mrb_define_method(mrb, klass, "select",
509 mrb_grn_table_select, MRB_ARGS_ARG(1, 1));
510 mrb_define_method(mrb, klass, "sort_raw",
511 mrb_grn_table_sort_raw, MRB_ARGS_REQ(4));
512 mrb_define_method(mrb, klass, "group_raw",
513 mrb_grn_table_group_raw, MRB_ARGS_REQ(2));
514
515 mrb_define_method(mrb, klass, "delete",
516 mrb_grn_table_delete, MRB_ARGS_REQ(1));
517
518 mrb_define_method(mrb, klass, "truncate",
519 mrb_grn_table_truncate, MRB_ARGS_NONE());
520
521 mrb_define_method(mrb, klass, "apply_expression",
522 mrb_grn_table_apply_expression, MRB_ARGS_REQ(2));
523 mrb_define_method(mrb, klass, "apply_window_function_raw",
524 mrb_grn_table_apply_window_function_raw, MRB_ARGS_REQ(4));
525
526 mrb_define_method(mrb, klass, "parse_output_columns",
527 mrb_grn_table_parse_output_columns, MRB_ARGS_REQ(1));
528 }
529 #endif