| 1 | /* |
| 2 | Copyright(C) 2020 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #include "../grn_ctx_impl.h" |
| 20 | |
| 21 | #ifdef GRN_WITH_MRUBY |
| 22 | #include <mruby.h> |
| 23 | #include <mruby/class.h> |
| 24 | #include <mruby/data.h> |
| 25 | |
| 26 | #include "mrb_ctx.h" |
| 27 | #include "mrb_table_with_key.h" |
| 28 | #include "mrb_converter.h" |
| 29 | |
| 30 | static mrb_value |
| 31 | mrb_grn_table_with_key_array_reference(mrb_state *mrb, mrb_value self) |
| 32 | { |
| 33 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 34 | grn_obj *table; |
| 35 | grn_id key_domain_id; |
| 36 | mrb_value mrb_key; |
| 37 | grn_id record_id; |
| 38 | grn_mrb_value_to_raw_data_buffer buffer; |
| 39 | void *key; |
| 40 | unsigned int key_size; |
| 41 | |
| 42 | mrb_get_args(mrb, "o", &mrb_key); |
| 43 | |
| 44 | table = DATA_PTR(self); |
| 45 | if (table->header.type == GRN_DB) { |
| 46 | key_domain_id = GRN_DB_SHORT_TEXT; |
| 47 | } else { |
| 48 | key_domain_id = table->header.domain; |
| 49 | } |
| 50 | |
| 51 | grn_mrb_value_to_raw_data_buffer_init(mrb, &buffer); |
| 52 | grn_mrb_value_to_raw_data(mrb, "key", mrb_key, key_domain_id, |
| 53 | &buffer, &key, &key_size); |
| 54 | record_id = grn_table_get(ctx, table, key, key_size); |
| 55 | grn_mrb_value_to_raw_data_buffer_fin(mrb, &buffer); |
| 56 | |
| 57 | if (record_id == GRN_ID_NIL) { |
| 58 | return mrb_nil_value(); |
| 59 | } else { |
| 60 | return mrb_int_value(mrb, record_id); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static mrb_value |
| 65 | mrb_grn_table_with_key_get_duplicated_keys(mrb_state *mrb, mrb_value self) |
| 66 | { |
| 67 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 68 | grn_obj *table = DATA_PTR(self); |
| 69 | grn_obj *duplicated_keys = NULL; |
| 70 | grn_table_get_duplicated_keys(ctx, table, &duplicated_keys); |
| 71 | grn_mrb_ctx_check(mrb); |
| 72 | return grn_mrb_value_from_grn_obj(mrb, duplicated_keys); |
| 73 | } |
| 74 | |
| 75 | static mrb_value |
| 76 | mrb_grn_table_with_key_have_duplicated_keys(mrb_state *mrb, mrb_value self) |
| 77 | { |
| 78 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 79 | grn_obj *table = DATA_PTR(self); |
| 80 | bool have_duplicated_keys = grn_table_have_duplicated_keys(ctx, table); |
| 81 | grn_mrb_ctx_check(mrb); |
| 82 | return mrb_bool_value(have_duplicated_keys); |
| 83 | } |
| 84 | |
| 85 | static mrb_value |
| 86 | mrb_grn_table_with_key_is_lexicon_without_data_column(mrb_state *mrb, |
| 87 | mrb_value self) |
| 88 | { |
| 89 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 90 | grn_obj *table = DATA_PTR(self); |
| 91 | return mrb_bool_value(grn_obj_is_lexicon_without_data_column(ctx, table)); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | grn_mrb_table_with_key_init(grn_ctx *ctx) |
| 96 | { |
| 97 | grn_mrb_data *data = &(ctx->impl->mrb); |
| 98 | mrb_state *mrb = data->state; |
| 99 | struct RClass *module = data->module; |
| 100 | struct RClass *table_class; |
| 101 | struct RClass *klass; |
| 102 | |
| 103 | table_class = mrb_class_get_under(mrb, module, "Table"); |
| 104 | klass = mrb_define_class_under(mrb, module, "TableWithKey", table_class); |
| 105 | MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); |
| 106 | |
| 107 | mrb_define_method(mrb, klass, "[]", |
| 108 | mrb_grn_table_with_key_array_reference, |
| 109 | MRB_ARGS_REQ(1)); |
| 110 | mrb_define_method(mrb, klass, "duplicated_keys", |
| 111 | mrb_grn_table_with_key_get_duplicated_keys, |
| 112 | MRB_ARGS_NONE()); |
| 113 | mrb_define_method(mrb, klass, "have_duplicated_keys?", |
| 114 | mrb_grn_table_with_key_have_duplicated_keys, |
| 115 | MRB_ARGS_NONE()); |
| 116 | mrb_define_method(mrb, klass, "lexicon_without_data_column?", |
| 117 | mrb_grn_table_with_key_is_lexicon_without_data_column, |
| 118 | MRB_ARGS_NONE()); |
| 119 | } |
| 120 | #endif |