| 1 | /* |
| 2 | Copyright(C) 2017 Brazil |
| 3 | Copyright(C) 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 | |
| 22 | #ifdef GRN_WITH_MRUBY |
| 23 | #include <mruby.h> |
| 24 | #include <mruby/class.h> |
| 25 | #include <mruby/data.h> |
| 26 | #include <mruby/array.h> |
| 27 | |
| 28 | #include "mrb_ctx.h" |
| 29 | #include "mrb_window_definition.h" |
| 30 | |
| 31 | static void |
| 32 | mrb_grn_window_definition_free(mrb_state *mrb, void *data) |
| 33 | { |
| 34 | grn_window_definition *definition = data; |
| 35 | |
| 36 | if (!definition) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | if (definition->sort_keys) { |
| 41 | mrb_free(mrb, definition->sort_keys); |
| 42 | } |
| 43 | if (definition->group_keys) { |
| 44 | mrb_free(mrb, definition->group_keys); |
| 45 | } |
| 46 | mrb_free(mrb, definition); |
| 47 | } |
| 48 | |
| 49 | static struct mrb_data_type mrb_grn_window_definition_type = { |
| 50 | "Groonga::WindowDefinition", |
| 51 | mrb_grn_window_definition_free |
| 52 | }; |
| 53 | |
| 54 | static mrb_value |
| 55 | mrb_grn_window_definition_initialize(mrb_state *mrb, mrb_value self) |
| 56 | { |
| 57 | grn_window_definition *result; |
| 58 | |
| 59 | DATA_TYPE(self) = &mrb_grn_window_definition_type; |
| 60 | |
| 61 | result = mrb_calloc(mrb, 1, sizeof(grn_window_definition)); |
| 62 | DATA_PTR(self) = result; |
| 63 | |
| 64 | return self; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | static mrb_value |
| 69 | mrb_grn_window_definition_close(mrb_state *mrb, mrb_value self) |
| 70 | { |
| 71 | grn_window_definition *definition; |
| 72 | |
| 73 | definition = DATA_PTR(self); |
| 74 | if (definition) { |
| 75 | mrb_grn_window_definition_free(mrb, definition); |
| 76 | DATA_PTR(self) = NULL; |
| 77 | } |
| 78 | |
| 79 | return mrb_nil_value(); |
| 80 | } |
| 81 | |
| 82 | static mrb_value |
| 83 | mrb_grn_window_definition_set_sort_keys(mrb_state *mrb, mrb_value self) |
| 84 | { |
| 85 | grn_window_definition *definition; |
| 86 | mrb_value mrb_keys; |
| 87 | |
| 88 | definition = DATA_PTR(self); |
| 89 | mrb_get_args(mrb, "A!", &mrb_keys); |
| 90 | |
| 91 | if (definition->sort_keys) { |
| 92 | mrb_free(mrb, definition->sort_keys); |
| 93 | } |
| 94 | |
| 95 | if (mrb_nil_p(mrb_keys)) { |
| 96 | definition->sort_keys = NULL; |
| 97 | definition->n_sort_keys = 0; |
| 98 | } else { |
| 99 | size_t n = (size_t)RARRAY_LEN(mrb_keys); |
| 100 | definition->sort_keys = mrb_calloc(mrb, n, sizeof(grn_table_sort_key)); |
| 101 | size_t i; |
| 102 | for (i = 0; i < n; i++) { |
| 103 | grn_table_sort_key *sort_key = DATA_PTR(RARRAY_PTR(mrb_keys)[i]); |
| 104 | definition->sort_keys[i] = *sort_key; |
| 105 | } |
| 106 | definition->n_sort_keys = n; |
| 107 | } |
| 108 | |
| 109 | return mrb_nil_value(); |
| 110 | } |
| 111 | |
| 112 | static mrb_value |
| 113 | mrb_grn_window_definition_set_group_keys(mrb_state *mrb, mrb_value self) |
| 114 | { |
| 115 | grn_window_definition *definition; |
| 116 | mrb_value mrb_keys; |
| 117 | |
| 118 | definition = DATA_PTR(self); |
| 119 | mrb_get_args(mrb, "A!", &mrb_keys); |
| 120 | |
| 121 | if (definition->group_keys) { |
| 122 | mrb_free(mrb, definition->group_keys); |
| 123 | } |
| 124 | |
| 125 | if (mrb_nil_p(mrb_keys)) { |
| 126 | definition->group_keys = NULL; |
| 127 | definition->n_group_keys = 0; |
| 128 | } else { |
| 129 | size_t n = (size_t)RARRAY_LEN(mrb_keys); |
| 130 | definition->group_keys = mrb_calloc(mrb, n, sizeof(grn_table_sort_key)); |
| 131 | size_t i; |
| 132 | for (i = 0; i < n; i++) { |
| 133 | grn_table_sort_key *group_key = DATA_PTR(RARRAY_PTR(mrb_keys)[i]); |
| 134 | definition->group_keys[i] = *group_key; |
| 135 | } |
| 136 | definition->n_group_keys = n; |
| 137 | } |
| 138 | |
| 139 | return mrb_nil_value(); |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | grn_mrb_window_definition_init(grn_ctx *ctx) |
| 144 | { |
| 145 | grn_mrb_data *data = &(ctx->impl->mrb); |
| 146 | mrb_state *mrb = data->state; |
| 147 | struct RClass *module = data->module; |
| 148 | struct RClass *klass; |
| 149 | |
| 150 | klass = mrb_define_class_under(mrb, module, "WindowDefinition", |
| 151 | mrb->object_class); |
| 152 | MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); |
| 153 | |
| 154 | mrb_define_method(mrb, klass, "initialize", |
| 155 | mrb_grn_window_definition_initialize, MRB_ARGS_NONE()); |
| 156 | |
| 157 | mrb_define_method(mrb, klass, "close", |
| 158 | mrb_grn_window_definition_close, MRB_ARGS_NONE()); |
| 159 | |
| 160 | mrb_define_method(mrb, klass, "sort_keys=", |
| 161 | mrb_grn_window_definition_set_sort_keys, MRB_ARGS_REQ(1)); |
| 162 | mrb_define_method(mrb, klass, "group_keys=", |
| 163 | mrb_grn_window_definition_set_group_keys, MRB_ARGS_REQ(1)); |
| 164 | } |
| 165 | #endif |