1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2013-2015 Brazil
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../grn_ctx_impl.h"
#include "../grn_db.h"
#ifdef GRN_WITH_MRUBY
#include <mruby.h>
#include <mruby/class.h>
#include <mruby/data.h>
#include <mruby/string.h>
#include "mrb_converter.h"
#include "mrb_bulk.h"
void
grn_mrb_value_to_raw_data_buffer_init(mrb_state *mrb,
grn_mrb_value_to_raw_data_buffer *buffer)
{
GRN_VOID_INIT(&(buffer->from));
GRN_VOID_INIT(&(buffer->to));
}
void
grn_mrb_value_to_raw_data_buffer_fin(mrb_state *mrb,
grn_mrb_value_to_raw_data_buffer *buffer)
{
grn_ctx *ctx = (grn_ctx *)mrb->ud;
GRN_OBJ_FIN(ctx, &(buffer->from));
GRN_OBJ_FIN(ctx, &(buffer->to));
}
void
grn_mrb_value_to_raw_data(mrb_state *mrb,
const char *context,
mrb_value mrb_value_,
grn_id domain_id,
grn_mrb_value_to_raw_data_buffer *buffer,
void **raw_value,
unsigned int *raw_value_size)
{
grn_ctx *ctx = (grn_ctx *)mrb->ud;
enum mrb_vtype mrb_value_type;
grn_bool try_cast = GRN_FALSE;
grn_obj *from_bulk = NULL;
if (mrb_nil_p(mrb_value_)) {
*raw_value = NULL;
*raw_value_size = 0;
return;
}
mrb_value_type = mrb_type(mrb_value_);
switch (mrb_value_type) {
case MRB_TT_STRING :
switch (domain_id) {
case GRN_DB_SHORT_TEXT :
case GRN_DB_TEXT :
case GRN_DB_LONG_TEXT :
*raw_value = RSTRING_PTR(mrb_value_);
*raw_value_size = RSTRING_LEN(mrb_value_);
break;
default :
try_cast = GRN_TRUE;
break;
}
break;
default :
{
struct RClass *klass;
grn_mrb_data *data = &(ctx->impl->mrb);
klass = mrb_class(mrb, mrb_value_);
if (domain_id == GRN_DB_TIME &&
klass == data->builtin.time_class) {
mrb_value mrb_sec;
mrb_value mrb_usec;
mrb_sec = mrb_funcall(mrb, mrb_value_, "to_i", 0);
mrb_usec = mrb_funcall(mrb, mrb_value_, "usec", 0);
buffer->value.time_value = GRN_TIME_PACK(mrb_fixnum(mrb_sec),
mrb_fixnum(mrb_usec));
*raw_value = &(buffer->value.time_value);
*raw_value_size = sizeof(buffer->value.time_value);
} else {
try_cast = GRN_TRUE;
if (mrb_value_type == MRB_TT_DATA &&
klass == mrb_class_get_under(mrb, data->module, "Bulk")) {
from_bulk = DATA_PTR(mrb_value_);
}
}
}
break;
}
if (!try_cast) {
return;
}
if (!from_bulk) {
from_bulk = &(buffer->from);
grn_mrb_value_to_bulk(mrb, mrb_value_, from_bulk);
}
if (!grn_mrb_bulk_cast(mrb, from_bulk, &(buffer->to), domain_id)) {
grn_obj *domain;
char domain_name[GRN_TABLE_MAX_KEY_SIZE];
int domain_name_size;
domain = grn_ctx_at(ctx, domain_id);
domain_name_size = grn_obj_name(ctx, domain, domain_name,
GRN_TABLE_MAX_KEY_SIZE);
mrb_raisef(mrb, E_ARGUMENT_ERROR,
"%S: failed to convert to %S: %S",
mrb_str_new_static(mrb, context, strlen(context)),
mrb_str_new_static(mrb, domain_name, domain_name_size),
mrb_funcall(mrb, mrb_value_, "inspect", 0));
}
*raw_value = GRN_BULK_HEAD(&(buffer->to));
*raw_value_size = GRN_BULK_VSIZE(&(buffer->to));
}
struct RClass *
grn_mrb_class_from_grn_obj(mrb_state *mrb, grn_obj *object)
{
grn_ctx *ctx = (grn_ctx *)mrb->ud;
grn_mrb_data *data;
struct RClass *klass = NULL;
data = &(ctx->impl->mrb);
switch (object->header.type) {
case GRN_BULK :
klass = mrb_class_get_under(mrb, data->module, "Bulk");
break;
case GRN_ACCESSOR :
klass = mrb_class_get_under(mrb, data->module, "Accessor");
break;
case GRN_COLUMN_FIX_SIZE :
klass = mrb_class_get_under(mrb, data->module, "FixedSizeColumn");
break;
case GRN_COLUMN_VAR_SIZE :
klass = mrb_class_get_under(mrb, data->module, "VariableSizeColumn");
break;
case GRN_COLUMN_INDEX :
klass = mrb_class_get_under(mrb, data->module, "IndexColumn");
break;
case GRN_TYPE :
klass = mrb_class_get_under(mrb, data->module, "Type");
break;
case GRN_PROC :
klass = mrb_class_get_under(mrb, data->module, "Procedure");
break;
case GRN_EXPR :
klass = mrb_class_get_under(mrb, data->module, "Expression");
break;
case GRN_TABLE_NO_KEY :
klass = mrb_class_get_under(mrb, data->module, "Array");
break;
case GRN_TABLE_HASH_KEY :
klass = mrb_class_get_under(mrb, data->module, "HashTable");
break;
case GRN_TABLE_PAT_KEY :
klass = mrb_class_get_under(mrb, data->module, "PatriciaTrie");
break;
case GRN_TABLE_DAT_KEY :
klass = mrb_class_get_under(mrb, data->module, "DoubleArrayTrie");
break;
case GRN_DB :
klass = mrb_class_get_under(mrb, data->module, "Database");
break;
case GRN_VOID :
klass = mrb_class_get_under(mrb, data->module, "Void");
break;
default :
break;
}
if (!klass) {
#define BUFFER_SIZE 1024
char buffer[BUFFER_SIZE];
grn_snprintf(buffer, BUFFER_SIZE, BUFFER_SIZE,
"can't find class for object type: %#x", object->header.type);
mrb_raise(mrb, E_ARGUMENT_ERROR, buffer);
#undef BUFFER_SIZE
}
return klass;
}
mrb_value
grn_mrb_value_from_grn_obj(mrb_state *mrb, grn_obj *object)
{
struct RClass *mrb_class;
mrb_value mrb_new_arguments[1];
mrb_value mrb_object;
if (!object) {
return mrb_nil_value();
}
mrb_class = grn_mrb_class_from_grn_obj(mrb, object);
mrb_new_arguments[0] = mrb_cptr_value(mrb, object);
mrb_object = mrb_obj_new(mrb, mrb_class, 1, mrb_new_arguments);
return mrb_object;
}
grn_id
grn_mrb_class_to_type(mrb_state *mrb, struct RClass *klass)
{
grn_ctx *ctx = (grn_ctx *)mrb->ud;
grn_id type = GRN_DB_VOID;
if (klass == mrb->nil_class) {
type = GRN_DB_VOID;
} else if (klass == mrb->true_class ||
klass == mrb->false_class) {
type = GRN_DB_BOOL;
} else if (klass == mrb->symbol_class) {
type = GRN_DB_TEXT;
} else if (klass == mrb->fixnum_class) {
type = GRN_DB_INT64;
} else if (klass == mrb->float_class) {
type = GRN_DB_FLOAT;
} else if (klass == mrb->string_class) {
type = GRN_DB_TEXT;
} else if (klass == ctx->impl->mrb.builtin.time_class) {
type = GRN_DB_TIME;
} else {
mrb_raisef(mrb, E_ARGUMENT_ERROR,
"unsupported class: %S", mrb_obj_value(klass));
}
return type;
}
static mrb_value
mrb_grn_converter_singleton_convert(mrb_state *mrb, mrb_value klass)
{
grn_ctx *ctx = (grn_ctx *)mrb->ud;
grn_obj *from = &(ctx->impl->mrb.buffer.from);
grn_obj *to = &(ctx->impl->mrb.buffer.to);
mrb_value mrb_from;
mrb_value mrb_to_class;
grn_id to_type;
mrb_get_args(mrb, "oC", &mrb_from, &mrb_to_class);
grn_mrb_value_to_bulk(mrb, mrb_from, from);
to_type = grn_mrb_class_to_type(mrb, mrb_class_ptr(mrb_to_class));
if (!grn_mrb_bulk_cast(mrb, from, to, to_type)) {
mrb_raisef(mrb, E_ARGUMENT_ERROR,
"failed to convert to %S: %S",
mrb_to_class,
mrb_from);
}
return grn_mrb_value_from_bulk(mrb, to);
}
void
grn_mrb_converter_init(grn_ctx *ctx)
{
grn_mrb_data *data = &(ctx->impl->mrb);
mrb_state *mrb = data->state;
struct RClass *module;
module = mrb_define_module_under(mrb, data->module, "Converter");
mrb_define_singleton_method(mrb, (struct RObject *)module, "convert",
mrb_grn_converter_singleton_convert,
MRB_ARGS_REQ(2));
}
#endif
|