Debian Codegraph @

lib/mrb/mrb_table_cursor.c

raw symbols SCIP
1 /*
2 Copyright(C) 2014-2015 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/string.h>
27 #include <mruby/hash.h>
28 #include <mruby/variable.h>
29
30 #include "mrb_ctx.h"
31 #include "mrb_bulk.h"
32 #include "mrb_table_cursor.h"
33
34 #include "mrb_converter.h"
35 #include "mrb_options.h"
36
37 static struct mrb_data_type mrb_grn_table_cursor_type = {
38 "Groonga::TableCursor",
39 NULL
40 };
41
42 static mrb_value
43 mrb_grn_table_cursor_class_open_raw(mrb_state *mrb, mrb_value klass)
44 {
45 grn_ctx *ctx = (grn_ctx *)mrb->ud;
46 mrb_value mrb_table;
47 mrb_value mrb_options = mrb_nil_value();
48 grn_table_cursor *table_cursor;
49 grn_obj *table;
50 void *min = NULL;
51 unsigned int min_size = 0;
52 grn_mrb_value_to_raw_data_buffer min_buffer;
53 void *max = NULL;
54 unsigned int max_size = 0;
55 grn_mrb_value_to_raw_data_buffer max_buffer;
56 int offset = 0;
57 int limit = -1;
58 int flags = 0;
59
60 mrb_get_args(mrb, "o|H", &mrb_table, &mrb_options);
61
62 table = DATA_PTR(mrb_table);
63 grn_mrb_value_to_raw_data_buffer_init(mrb, &min_buffer);
64 grn_mrb_value_to_raw_data_buffer_init(mrb, &max_buffer);
65 if (!mrb_nil_p(mrb_options)) {
66 grn_id key_domain_id;
67 mrb_value mrb_min;
68 mrb_value mrb_max;
69 mrb_value mrb_flags;
70
71 if (table->header.type == GRN_DB) {
72 key_domain_id = GRN_DB_SHORT_TEXT;
73 } else {
74 key_domain_id = table->header.domain;
75 }
76
77 mrb_min = grn_mrb_options_get_lit(mrb, mrb_options, "min");
78 grn_mrb_value_to_raw_data(mrb, "min", mrb_min,
79 key_domain_id, &min_buffer, &min, &min_size);
80
81 mrb_max = grn_mrb_options_get_lit(mrb, mrb_options, "max");
82 grn_mrb_value_to_raw_data(mrb, "max", mrb_max,
83 key_domain_id, &max_buffer, &max, &max_size);
84
85 mrb_flags = grn_mrb_options_get_lit(mrb, mrb_options, "flags");
86 if (!mrb_nil_p(mrb_flags)) {
87 flags = (int)mrb_integer(mrb_flags);
88 }
89 }
90 table_cursor = grn_table_cursor_open(ctx, table,
91 min, min_size,
92 max, max_size,
93 offset, limit, flags);
94 grn_mrb_value_to_raw_data_buffer_fin(mrb, &min_buffer);
95 grn_mrb_value_to_raw_data_buffer_fin(mrb, &max_buffer);
96 grn_mrb_ctx_check(mrb);
97
98 {
99 mrb_value mrb_table_cursor;
100 mrb_table_cursor = mrb_funcall(mrb, klass,
101 "new", 1, mrb_cptr_value(mrb, table_cursor));
102 mrb_iv_set(mrb, mrb_table_cursor, mrb_intern_lit(mrb, "@table"), mrb_table);
103 return mrb_table_cursor;
104 }
105 }
106
107 static mrb_value
108 mrb_grn_table_cursor_initialize(mrb_state *mrb, mrb_value self)
109 {
110 mrb_value mrb_table_cursor_ptr;
111
112 mrb_get_args(mrb, "o", &mrb_table_cursor_ptr);
113 DATA_TYPE(self) = &mrb_grn_table_cursor_type;
114 DATA_PTR(self) = mrb_cptr(mrb_table_cursor_ptr);
115
116 return self;
117 }
118
119 static mrb_value
120 mrb_grn_table_cursor_close(mrb_state *mrb, mrb_value self)
121 {
122 grn_ctx *ctx = (grn_ctx *)mrb->ud;
123 grn_table_cursor *table_cursor;
124
125 table_cursor = DATA_PTR(self);
126 if (table_cursor) {
127 DATA_PTR(self) = NULL;
128 grn_table_cursor_close(ctx, table_cursor);
129 grn_mrb_ctx_check(mrb);
130 }
131
132 return mrb_nil_value();
133 }
134
135 static mrb_value
136 mrb_grn_table_cursor_next(mrb_state *mrb, mrb_value self)
137 {
138 grn_ctx *ctx = (grn_ctx *)mrb->ud;
139 grn_id id;
140
141 id = grn_table_cursor_next(ctx, DATA_PTR(self));
142 grn_mrb_ctx_check(mrb);
143
144 return mrb_int_value(mrb, id);
145 }
146
147 static mrb_value
148 mrb_grn_table_cursor_count(mrb_state *mrb, mrb_value self)
149 {
150 grn_ctx *ctx = (grn_ctx *)mrb->ud;
151 int n_records = 0;
152
153 while (grn_table_cursor_next(ctx, DATA_PTR(self)) != GRN_ID_NIL) {
154 n_records++;
155 }
156
157 return mrb_int_value(mrb, n_records);
158 }
159
160 static mrb_value
161 mrb_grn_table_cursor_get_key(mrb_state *mrb, mrb_value self)
162 {
163 grn_ctx *ctx = (grn_ctx *)mrb->ud;
164 mrb_value mrb_table;
165 grn_obj *table;
166 grn_id domain;
167 void *key;
168 int key_size;
169
170 mrb_table = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@table"));
171 table = DATA_PTR(mrb_table);
172 if (table->header.type == GRN_DB) {
173 domain = GRN_DB_SHORT_TEXT;
174 } else {
175 domain = table->header.domain;
176 }
177 key_size = grn_table_cursor_get_key(ctx, DATA_PTR(self), &key);
178
179 return grn_mrb_value_from_raw_data(mrb, domain, key, key_size);
180 }
181
182 void
183 grn_mrb_table_cursor_init(grn_ctx *ctx)
184 {
185 grn_mrb_data *data = &(ctx->impl->mrb);
186 mrb_state *mrb = data->state;
187 struct RClass *module = data->module;
188 struct RClass *klass;
189
190 klass = mrb_define_class_under(mrb, module, "TableCursor", mrb->object_class);
191 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
192
193 mrb_define_class_method(mrb, klass, "open_raw",
194 mrb_grn_table_cursor_class_open_raw,
195 MRB_ARGS_ARG(1, 1));
196
197 mrb_define_method(mrb, klass, "initialize",
198 mrb_grn_table_cursor_initialize, MRB_ARGS_REQ(1));
199 mrb_define_method(mrb, klass, "close",
200 mrb_grn_table_cursor_close, MRB_ARGS_NONE());
201 mrb_define_method(mrb, klass, "next",
202 mrb_grn_table_cursor_next, MRB_ARGS_NONE());
203 mrb_define_method(mrb, klass, "count",
204 mrb_grn_table_cursor_count, MRB_ARGS_NONE());
205
206 mrb_define_method(mrb, klass, "key",
207 mrb_grn_table_cursor_get_key, MRB_ARGS_NONE());
208 }
209 #endif