Debian Codegraph @

lib/mrb/mrb_table_sort_key.c

raw symbols SCIP
1 /*
2 Copyright(C) 2015 Brazil
3 Copyright(C) 2020-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/hash.h>
27 #include <mruby/array.h>
28 #include <mruby/string.h>
29
30 #include "mrb_ctx.h"
31 #include "mrb_table_sort_key.h"
32
33 static void
34 mrb_grn_table_sort_key_free(mrb_state *mrb, void *data)
35 {
36 grn_ctx *ctx = (grn_ctx *)mrb->ud;
37 grn_table_sort_key *sort_key = data;
38
39 if (!sort_key) {
40 return;
41 }
42
43 if (sort_key->key) {
44 if (sort_key->key->header.type == GRN_ACCESSOR) {
45 grn_obj_unlink(ctx, sort_key->key);
46 } else {
47 grn_obj_unref(ctx, sort_key->key);
48 }
49 }
50 mrb_free(mrb, sort_key);
51 }
52
53 static struct mrb_data_type mrb_grn_table_sort_key_type = {
54 "Groonga::TableSortKey",
55 mrb_grn_table_sort_key_free
56 };
57
58 static mrb_value
59 mrb_grn_table_sort_key_initialize(mrb_state *mrb, mrb_value self)
60 {
61 grn_table_sort_key *result;
62
63 DATA_TYPE(self) = &mrb_grn_table_sort_key_type;
64
65 result = mrb_calloc(mrb, 1, sizeof(grn_table_sort_key));
66 DATA_PTR(self) = result;
67
68 return self;
69 }
70
71
72 static mrb_value
73 mrb_grn_table_sort_key_close(mrb_state *mrb, mrb_value self)
74 {
75 grn_table_sort_key *sort_key;
76
77 sort_key = DATA_PTR(self);
78 if (sort_key) {
79 mrb_grn_table_sort_key_free(mrb, sort_key);
80 DATA_PTR(self) = NULL;
81 }
82
83 return mrb_nil_value();
84 }
85
86 static mrb_value
87 mrb_grn_table_sort_key_set_key(mrb_state *mrb, mrb_value self)
88 {
89 grn_ctx *ctx = (grn_ctx *)mrb->ud;
90 grn_table_sort_key *sort_key;
91 mrb_value mrb_key;
92
93 sort_key = DATA_PTR(self);
94 mrb_get_args(mrb, "o", &mrb_key);
95
96 if (sort_key->key) {
97 grn_obj_unlink(ctx, sort_key->key);
98 }
99
100 if (mrb_nil_p(mrb_key)) {
101 sort_key->key = NULL;
102 } else {
103 sort_key->key = DATA_PTR(mrb_key);
104 }
105
106 return mrb_nil_value();
107 }
108
109 static mrb_value
110 mrb_grn_table_sort_key_set_flags(mrb_state *mrb, mrb_value self)
111 {
112 grn_table_sort_key *sort_key;
113 mrb_int flags;
114
115 sort_key = DATA_PTR(self);
116 mrb_get_args(mrb, "i", &flags);
117
118 sort_key->flags = (grn_table_sort_flags)flags;
119
120 return mrb_nil_value();
121 }
122
123 static mrb_value
124 mrb_grn_table_sort_key_set_offset(mrb_state *mrb, mrb_value self)
125 {
126 grn_table_sort_key *sort_key;
127 mrb_int offset;
128
129 sort_key = DATA_PTR(self);
130 mrb_get_args(mrb, "i", &offset);
131
132 sort_key->offset = (int)offset;
133
134 return mrb_nil_value();
135 }
136
137 void
138 grn_mrb_table_sort_key_init(grn_ctx *ctx)
139 {
140 grn_mrb_data *data = &(ctx->impl->mrb);
141 mrb_state *mrb = data->state;
142 struct RClass *module = data->module;
143 struct RClass *klass;
144
145 klass = mrb_define_class_under(mrb, module, "TableSortKey",
146 mrb->object_class);
147 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
148
149 mrb_define_method(mrb, klass, "initialize",
150 mrb_grn_table_sort_key_initialize, MRB_ARGS_NONE());
151
152 mrb_define_method(mrb, klass, "close",
153 mrb_grn_table_sort_key_close, MRB_ARGS_NONE());
154
155 mrb_define_method(mrb, klass, "key=",
156 mrb_grn_table_sort_key_set_key, MRB_ARGS_REQ(1));
157 mrb_define_method(mrb, klass, "flags=",
158 mrb_grn_table_sort_key_set_flags, MRB_ARGS_REQ(1));
159 mrb_define_method(mrb, klass, "offset=",
160 mrb_grn_table_sort_key_set_offset, MRB_ARGS_REQ(1));
161 }
162 #endif