1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2013-2016 Brazil
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 version 2.1 as published by the Free Software Foundation.
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#include <mruby/variable.h>
26#include <mruby/string.h>
27
28#include "../grn_mrb.h"
29#include "mrb_ctx.h"
30#include "mrb_bulk.h"
31#include "mrb_converter.h"
32
33static mrb_value
34ctx_class_instance(mrb_state *mrb, mrb_value klass)
35{
36 grn_ctx *ctx = (grn_ctx *)mrb->ud;
37 mrb_value mrb_ctx;
38 mrb_sym iv_name;
39
40 iv_name = mrb_intern_lit(mrb, "@instance");
41 mrb_ctx = mrb_iv_get(mrb, klass, iv_name);
42 if (mrb_nil_p(mrb_ctx)) {
43 struct RBasic *raw_mrb_ctx;
44 raw_mrb_ctx = mrb_obj_alloc(mrb, MRB_TT_DATA, mrb_class_ptr(klass));
45 mrb_ctx = mrb_obj_value(raw_mrb_ctx);
46 DATA_PTR(mrb_ctx) = ctx;
47 mrb_iv_set(mrb, klass, iv_name, mrb_ctx);
48 }
49
50 return mrb_ctx;
51}
52
53static mrb_value
54ctx_array_reference(mrb_state *mrb, mrb_value self)
55{
56 grn_ctx *ctx = (grn_ctx *)mrb->ud;
57 mrb_value mrb_id_or_name;
58 grn_obj *object;
59
60 mrb_get_args(mrb, "o", &mrb_id_or_name);
61
62 if (mrb_nil_p(mrb_id_or_name)) {
63 return mrb_nil_value();
64 }
65
66 if (mrb_fixnum_p(mrb_id_or_name)) {
67 grn_id id = mrb_fixnum(mrb_id_or_name);
68 object = grn_ctx_at(ctx, id);
69 } else {
70 mrb_value mrb_name;
71 mrb_name = mrb_convert_type(mrb, mrb_id_or_name,
72 MRB_TT_STRING, "String", "to_str");
73 object = grn_ctx_get(ctx,
74 RSTRING_PTR(mrb_name),
75 RSTRING_LEN(mrb_name));
76 }
77
78 return grn_mrb_value_from_grn_obj(mrb, object);
79}
80
81static mrb_value
82ctx_get_rc(mrb_state *mrb, mrb_value self)
83{
84 grn_ctx *ctx = (grn_ctx *)mrb->ud;
85
86 return mrb_fixnum_value(ctx->rc);
87}
88
89static mrb_value
90ctx_set_rc(mrb_state *mrb, mrb_value self)
91{
92 grn_ctx *ctx = (grn_ctx *)mrb->ud;
93 mrb_int rc;
94
95 mrb_get_args(mrb, "i", &rc);
96 ctx->rc = rc;
97
98 return mrb_fixnum_value(ctx->rc);
99}
100
101static mrb_value
102ctx_get_error_level(mrb_state *mrb, mrb_value self)
103{
104 grn_ctx *ctx = (grn_ctx *)mrb->ud;
105
106 return mrb_fixnum_value(ctx->errlvl);
107}
108
109static mrb_value
110ctx_set_error_level(mrb_state *mrb, mrb_value self)
111{
112 grn_ctx *ctx = (grn_ctx *)mrb->ud;
113 mrb_int error_level;
114
115 mrb_get_args(mrb, "i", &error_level);
116 ctx->errlvl = error_level;
117
118 return mrb_fixnum_value(ctx->errlvl);
119}
120
121static mrb_value
122ctx_get_error_file(mrb_state *mrb, mrb_value self)
123{
124 grn_ctx *ctx = (grn_ctx *)mrb->ud;
125
126 return mrb_str_new_cstr(mrb, ctx->errfile);
127}
128
129static mrb_value
130ctx_set_error_file(mrb_state *mrb, mrb_value self)
131{
132 grn_ctx *ctx = (grn_ctx *)mrb->ud;
133 mrb_value error_file;
134
135 mrb_get_args(mrb, "S", &error_file);
136 mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@error_file"), error_file);
137 ctx->errfile = mrb_string_value_cstr(mrb, &error_file);
138
139 return error_file;
140}
141
142static mrb_value
143ctx_get_error_line(mrb_state *mrb, mrb_value self)
144{
145 grn_ctx *ctx = (grn_ctx *)mrb->ud;
146
147 return mrb_fixnum_value(ctx->errline);
148}
149
150static mrb_value
151ctx_set_error_line(mrb_state *mrb, mrb_value self)
152{
153 grn_ctx *ctx = (grn_ctx *)mrb->ud;
154 mrb_int error_line;
155
156 mrb_get_args(mrb, "i", &error_line);
157 ctx->errline = error_line;
158
159 return mrb_fixnum_value(ctx->errline);
160}
161
162static mrb_value
163ctx_get_error_method(mrb_state *mrb, mrb_value self)
164{
165 grn_ctx *ctx = (grn_ctx *)mrb->ud;
166
167 return mrb_str_new_cstr(mrb, ctx->errfunc);
168}
169
170static mrb_value
171ctx_set_error_method(mrb_state *mrb, mrb_value self)
172{
173 grn_ctx *ctx = (grn_ctx *)mrb->ud;
174 mrb_value error_method;
175
176 mrb_get_args(mrb, "S", &error_method);
177 mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@error_method"), error_method);
178 ctx->errfunc = mrb_string_value_cstr(mrb, &error_method);
179
180 return error_method;
181}
182
183static mrb_value
184ctx_get_error_message(mrb_state *mrb, mrb_value self)
185{
186 grn_ctx *ctx = (grn_ctx *)mrb->ud;
187
188 return mrb_str_new_cstr(mrb, ctx->errbuf);
189}
190
191static mrb_value
192ctx_set_error_message(mrb_state *mrb, mrb_value self)
193{
194 grn_ctx *ctx = (grn_ctx *)mrb->ud;
195 mrb_value error_message;
196
197 mrb_get_args(mrb, "S", &error_message);
198 grn_ctx_log(ctx, "%.*s",
199 (int)RSTRING_LEN(error_message),
200 RSTRING_PTR(error_message));
201
202 return error_message;
203}
204
205static mrb_value
206ctx_clear_error(mrb_state *mrb, mrb_value self)
207{
208 grn_ctx *ctx = (grn_ctx *)mrb->ud;
209
210 ERRCLR(ctx);
211
212 return mrb_nil_value();
213}
214
215static mrb_value
216ctx_get_command_version(mrb_state *mrb, mrb_value self)
217{
218 grn_ctx *ctx = (grn_ctx *)mrb->ud;
219
220 return mrb_fixnum_value(grn_ctx_get_command_version(ctx));
221}
222
223static mrb_value
224ctx_set_command_version(mrb_state *mrb, mrb_value self)
225{
226 grn_ctx *ctx = (grn_ctx *)mrb->ud;
227 mrb_int command_version;
228
229 mrb_get_args(mrb, "i", &command_version);
230 grn_ctx_set_command_version(ctx, command_version);
231
232 return mrb_fixnum_value(command_version);
233}
234
235static mrb_value
236ctx_get_output(mrb_state *mrb, mrb_value self)
237{
238 grn_ctx *ctx = (grn_ctx *)mrb->ud;
239
240 return grn_mrb_value_from_bulk(mrb, ctx->impl->output.buf);
241}
242
243static mrb_value
244ctx_set_output(mrb_state *mrb, mrb_value self)
245{
246 grn_ctx *ctx = (grn_ctx *)mrb->ud;
247 mrb_value mrb_value_;
248
249 mrb_get_args(mrb, "S", &mrb_value_);
250 GRN_TEXT_SET(ctx, ctx->impl->output.buf,
251 RSTRING_PTR(mrb_value_),
252 RSTRING_LEN(mrb_value_));
253
254 return mrb_value_;
255}
256
257static mrb_value
258ctx_get_database(mrb_state *mrb, mrb_value self)
259{
260 grn_ctx *ctx = (grn_ctx *)mrb->ud;
261
262 return grn_mrb_value_from_grn_obj(mrb, grn_ctx_db(ctx));
263}
264
265static mrb_value
266ctx_is_opened(mrb_state *mrb, mrb_value self)
267{
268 grn_ctx *ctx = (grn_ctx *)mrb->ud;
269 mrb_int mrb_id;
270
271 mrb_get_args(mrb, "i", &mrb_id);
272
273 return mrb_bool_value(grn_ctx_is_opened(ctx, mrb_id));
274}
275
276void
277grn_mrb_ctx_check(mrb_state *mrb)
278{
279 grn_ctx *ctx = (grn_ctx *)mrb->ud;
280 grn_mrb_data *data = &(ctx->impl->mrb);
281 struct RClass *module = data->module;
282 struct RClass *error_class = NULL;
283#define MESSAGE_SIZE 4096
284 char message[MESSAGE_SIZE];
285
286 switch (ctx->rc) {
287 case GRN_SUCCESS:
288 return;
289 case GRN_END_OF_DATA:
290 error_class = mrb_class_get_under(mrb, module, "EndOfData");
291 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
292 "end of data: <%s>(%d)",
293 ctx->errbuf, ctx->rc);
294 break;
295 case GRN_UNKNOWN_ERROR:
296 error_class = mrb_class_get_under(mrb, module, "UnknownError");
297 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
298 "unknown error: <%s>(%d)",
299 ctx->errbuf, ctx->rc);
300 break;
301 case GRN_OPERATION_NOT_PERMITTED:
302 error_class = mrb_class_get_under(mrb, module, "OperationNotPermitted");
303 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
304 "operation not permitted: <%s>(%d)",
305 ctx->errbuf, ctx->rc);
306 break;
307 case GRN_NO_SUCH_FILE_OR_DIRECTORY:
308 error_class = mrb_class_get_under(mrb, module, "NoSuchFileOrDirectory");
309 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
310 "no such file or directory: <%s>(%d)",
311 ctx->errbuf, ctx->rc);
312 break;
313 case GRN_NO_SUCH_PROCESS:
314 error_class = mrb_class_get_under(mrb, module, "NoSuchProcess");
315 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
316 "no such process: <%s>(%d)",
317 ctx->errbuf, ctx->rc);
318 break;
319 case GRN_INTERRUPTED_FUNCTION_CALL:
320 error_class = mrb_class_get_under(mrb, module, "InterruptedFunctionCall");
321 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
322 "interrupted function call: <%s>(%d)",
323 ctx->errbuf, ctx->rc);
324 break;
325 case GRN_INPUT_OUTPUT_ERROR:
326 error_class = mrb_class_get_under(mrb, module, "InputOutputError");
327 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
328 "input output error: <%s>(%d)",
329 ctx->errbuf, ctx->rc);
330 break;
331 case GRN_NO_SUCH_DEVICE_OR_ADDRESS:
332 error_class = mrb_class_get_under(mrb, module, "NoSuchDeviceOrAddress");
333 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
334 "no such device or address: <%s>(%d)",
335 ctx->errbuf, ctx->rc);
336 break;
337 case GRN_ARG_LIST_TOO_LONG:
338 error_class = mrb_class_get_under(mrb, module, "ArgListTooLong");
339 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
340 "arg list too long: <%s>(%d)",
341 ctx->errbuf, ctx->rc);
342 break;
343 case GRN_EXEC_FORMAT_ERROR:
344 error_class = mrb_class_get_under(mrb, module, "ExecFormatError");
345 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
346 "exec format error: <%s>(%d)",
347 ctx->errbuf, ctx->rc);
348 break;
349 case GRN_BAD_FILE_DESCRIPTOR:
350 error_class = mrb_class_get_under(mrb, module, "BadFileDescriptor");
351 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
352 "bad file descriptor: <%s>(%d)",
353 ctx->errbuf, ctx->rc);
354 break;
355 case GRN_NO_CHILD_PROCESSES:
356 error_class = mrb_class_get_under(mrb, module, "NoChildProcesses");
357 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
358 "no child processes: <%s>(%d)",
359 ctx->errbuf, ctx->rc);
360 break;
361 case GRN_RESOURCE_TEMPORARILY_UNAVAILABLE:
362 error_class = mrb_class_get_under(mrb, module,
363 "ResourceTemporarilyUnavailable");
364 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
365 "resource temporarily unavailable: <%s>(%d)",
366 ctx->errbuf, ctx->rc);
367 break;
368 case GRN_NOT_ENOUGH_SPACE:
369 error_class = mrb_class_get_under(mrb, module, "NotEnoughSpace");
370 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
371 "not enough space: <%s>(%d)",
372 ctx->errbuf, ctx->rc);
373 break;
374 case GRN_PERMISSION_DENIED:
375 error_class = mrb_class_get_under(mrb, module, "PermissionDenied");
376 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
377 "permission denied: <%s>(%d)",
378 ctx->errbuf, ctx->rc);
379 break;
380 case GRN_BAD_ADDRESS:
381 error_class = mrb_class_get_under(mrb, module, "BadAddress");
382 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
383 "bad address: <%s>(%d)",
384 ctx->errbuf, ctx->rc);
385 break;
386 case GRN_RESOURCE_BUSY:
387 error_class = mrb_class_get_under(mrb, module, "ResourceBusy");
388 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
389 "resource busy: <%s>(%d)",
390 ctx->errbuf, ctx->rc);
391 break;
392 case GRN_FILE_EXISTS:
393 error_class = mrb_class_get_under(mrb, module, "FileExists");
394 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
395 "file exists: <%s>(%d)",
396 ctx->errbuf, ctx->rc);
397 break;
398 case GRN_IMPROPER_LINK:
399 error_class = mrb_class_get_under(mrb, module, "ImproperLink");
400 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
401 "improper link: <%s>(%d)",
402 ctx->errbuf, ctx->rc);
403 break;
404 case GRN_NO_SUCH_DEVICE:
405 error_class = mrb_class_get_under(mrb, module, "NoSuchDevice");
406 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
407 "no such device: <%s>(%d)",
408 ctx->errbuf, ctx->rc);
409 break;
410 case GRN_NOT_A_DIRECTORY:
411 error_class = mrb_class_get_under(mrb, module, "NotDirectory");
412 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
413 "not directory: <%s>(%d)",
414 ctx->errbuf, ctx->rc);
415 break;
416 case GRN_IS_A_DIRECTORY:
417 error_class = mrb_class_get_under(mrb, module, "IsDirectory");
418 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
419 "is directory: <%s>(%d)",
420 ctx->errbuf, ctx->rc);
421 break;
422 case GRN_INVALID_ARGUMENT:
423 error_class = mrb_class_get_under(mrb, module, "InvalidArgument");
424 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
425 "invalid argument: <%s>(%d)",
426 ctx->errbuf, ctx->rc);
427 break;
428 case GRN_TOO_MANY_OPEN_FILES_IN_SYSTEM:
429 error_class = mrb_class_get_under(mrb, module, "TooManyOpenFilesInSystem");
430 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
431 "too many open files in system: <%s>(%d)",
432 ctx->errbuf, ctx->rc);
433 break;
434 case GRN_TOO_MANY_OPEN_FILES:
435 error_class = mrb_class_get_under(mrb, module, "TooManyOpenFiles");
436 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
437 "too many open files: <%s>(%d)",
438 ctx->errbuf, ctx->rc);
439 break;
440 case GRN_INAPPROPRIATE_I_O_CONTROL_OPERATION:
441 error_class = mrb_class_get_under(mrb, module,
442 "InappropriateIOControlOperation");
443 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
444 "inappropriate IO control operation: <%s>(%d)",
445 ctx->errbuf, ctx->rc);
446 break;
447 case GRN_FILE_TOO_LARGE:
448 error_class = mrb_class_get_under(mrb, module, "FileTooLarge");
449 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
450 "file too large: <%s>(%d)",
451 ctx->errbuf, ctx->rc);
452 break;
453 case GRN_NO_SPACE_LEFT_ON_DEVICE:
454 error_class = mrb_class_get_under(mrb, module, "NoSpaceLeftOnDevice");
455 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
456 "no space left on device: <%s>(%d)",
457 ctx->errbuf, ctx->rc);
458 break;
459 case GRN_INVALID_SEEK:
460 error_class = mrb_class_get_under(mrb, module, "InvalidSeek");
461 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
462 "invalid seek: <%s>(%d)",
463 ctx->errbuf, ctx->rc);
464 break;
465 case GRN_READ_ONLY_FILE_SYSTEM:
466 error_class = mrb_class_get_under(mrb, module, "ReadOnlyFileSystem");
467 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
468 "read only file system: <%s>(%d)",
469 ctx->errbuf, ctx->rc);
470 break;
471 case GRN_TOO_MANY_LINKS:
472 error_class = mrb_class_get_under(mrb, module, "TooManyLinks");
473 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
474 "too many links: <%s>(%d)",
475 ctx->errbuf, ctx->rc);
476 break;
477 case GRN_BROKEN_PIPE:
478 error_class = mrb_class_get_under(mrb, module, "BrokenPipe");
479 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
480 "broken pipe: <%s>(%d)",
481 ctx->errbuf, ctx->rc);
482 break;
483 case GRN_DOMAIN_ERROR:
484 error_class = mrb_class_get_under(mrb, module, "DomainError");
485 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
486 "domain error: <%s>(%d)",
487 ctx->errbuf, ctx->rc);
488 break;
489 case GRN_RESULT_TOO_LARGE:
490 error_class = mrb_class_get_under(mrb, module, "ResultTooLarge");
491 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
492 "result too large: <%s>(%d)",
493 ctx->errbuf, ctx->rc);
494 break;
495 case GRN_RESOURCE_DEADLOCK_AVOIDED:
496 error_class = mrb_class_get_under(mrb, module, "ResourceDeadlockAvoided");
497 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
498 "resource deadlock avoided: <%s>(%d)",
499 ctx->errbuf, ctx->rc);
500 break;
501 case GRN_NO_MEMORY_AVAILABLE:
502 error_class = mrb_class_get_under(mrb, module, "NoMemoryAvailable");
503 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
504 "no memory available: <%s>(%d)",
505 ctx->errbuf, ctx->rc);
506 break;
507 case GRN_FILENAME_TOO_LONG:
508 error_class = mrb_class_get_under(mrb, module, "FilenameTooLong");
509 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
510 "filename too long: <%s>(%d)",
511 ctx->errbuf, ctx->rc);
512 break;
513 case GRN_NO_LOCKS_AVAILABLE:
514 error_class = mrb_class_get_under(mrb, module, "NoLocksAvailable");
515 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
516 "no locks available: <%s>(%d)",
517 ctx->errbuf, ctx->rc);
518 break;
519 case GRN_FUNCTION_NOT_IMPLEMENTED:
520 error_class = mrb_class_get_under(mrb, module, "FunctionNotImplemented");
521 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
522 "function not implemented: <%s>(%d)",
523 ctx->errbuf, ctx->rc);
524 break;
525 case GRN_DIRECTORY_NOT_EMPTY:
526 error_class = mrb_class_get_under(mrb, module, "DirectoryNotEmpty");
527 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
528 "directory not empty: <%s>(%d)",
529 ctx->errbuf, ctx->rc);
530 break;
531 case GRN_ILLEGAL_BYTE_SEQUENCE:
532 error_class = mrb_class_get_under(mrb, module, "IllegalByteSequence");
533 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
534 "illegal byte sequence: <%s>(%d)",
535 ctx->errbuf, ctx->rc);
536 break;
537 case GRN_SOCKET_NOT_INITIALIZED:
538 error_class = mrb_class_get_under(mrb, module, "SocketNotInitialized");
539 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
540 "socket not initialized: <%s>(%d)",
541 ctx->errbuf, ctx->rc);
542 break;
543 case GRN_OPERATION_WOULD_BLOCK:
544 error_class = mrb_class_get_under(mrb, module, "OperationWouldBlock");
545 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
546 "operation would block: <%s>(%d)",
547 ctx->errbuf, ctx->rc);
548 break;
549 case GRN_ADDRESS_IS_NOT_AVAILABLE:
550 error_class = mrb_class_get_under(mrb, module, "AddressIsNotAvailable");
551 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
552 "address is not available: <%s>(%d)",
553 ctx->errbuf, ctx->rc);
554 break;
555 case GRN_NETWORK_IS_DOWN:
556 error_class = mrb_class_get_under(mrb, module, "NetworkIsDown");
557 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
558 "network is down: <%s>(%d)",
559 ctx->errbuf, ctx->rc);
560 break;
561 case GRN_NO_BUFFER:
562 error_class = mrb_class_get_under(mrb, module, "NoBuffer");
563 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
564 "no buffer: <%s>(%d)",
565 ctx->errbuf, ctx->rc);
566 break;
567 case GRN_SOCKET_IS_ALREADY_CONNECTED:
568 error_class = mrb_class_get_under(mrb, module, "SocketIsAlreadyConnected");
569 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
570 "socket is already connected: <%s>(%d)",
571 ctx->errbuf, ctx->rc);
572 break;
573 case GRN_SOCKET_IS_NOT_CONNECTED:
574 error_class = mrb_class_get_under(mrb, module, "SocketIsNotConnected");
575 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
576 "socket is not connected: <%s>(%d)",
577 ctx->errbuf, ctx->rc);
578 break;
579 case GRN_SOCKET_IS_ALREADY_SHUTDOWNED:
580 error_class = mrb_class_get_under(mrb, module, "SocketIsAlreadyShutdowned");
581 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
582 "socket is already shutdowned: <%s>(%d)",
583 ctx->errbuf, ctx->rc);
584 break;
585 case GRN_OPERATION_TIMEOUT:
586 error_class = mrb_class_get_under(mrb, module, "OperationTimeout");
587 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
588 "operation timeout: <%s>(%d)",
589 ctx->errbuf, ctx->rc);
590 break;
591 case GRN_CONNECTION_REFUSED:
592 error_class = mrb_class_get_under(mrb, module, "ConnectionRefused");
593 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
594 "connection refused: <%s>(%d)",
595 ctx->errbuf, ctx->rc);
596 break;
597 case GRN_RANGE_ERROR:
598 error_class = mrb_class_get_under(mrb, module, "RangeError");
599 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
600 "range error: <%s>(%d)",
601 ctx->errbuf, ctx->rc);
602 break;
603 case GRN_TOKENIZER_ERROR:
604 error_class = mrb_class_get_under(mrb, module, "TokenizerError");
605 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
606 "tokenizer error: <%s>(%d)",
607 ctx->errbuf, ctx->rc);
608 break;
609 case GRN_FILE_CORRUPT:
610 error_class = mrb_class_get_under(mrb, module, "FileCorrupt");
611 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
612 "file corrupt: <%s>(%d)",
613 ctx->errbuf, ctx->rc);
614 break;
615 case GRN_INVALID_FORMAT:
616 error_class = mrb_class_get_under(mrb, module, "InvalidFormat");
617 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
618 "invalid format: <%s>(%d)",
619 ctx->errbuf, ctx->rc);
620 break;
621 case GRN_OBJECT_CORRUPT:
622 error_class = mrb_class_get_under(mrb, module, "ObjectCorrupt");
623 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
624 "object corrupt: <%s>(%d)",
625 ctx->errbuf, ctx->rc);
626 break;
627 case GRN_TOO_MANY_SYMBOLIC_LINKS:
628 error_class = mrb_class_get_under(mrb, module, "TooManySymbolicLinks");
629 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
630 "too many symbolic links: <%s>(%d)",
631 ctx->errbuf, ctx->rc);
632 break;
633 case GRN_NOT_SOCKET:
634 error_class = mrb_class_get_under(mrb, module, "NotSocket");
635 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
636 "not socket: <%s>(%d)",
637 ctx->errbuf, ctx->rc);
638 break;
639 case GRN_OPERATION_NOT_SUPPORTED:
640 error_class = mrb_class_get_under(mrb, module, "OperationNotSupported");
641 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
642 "operation not supported: <%s>(%d)",
643 ctx->errbuf, ctx->rc);
644 break;
645 case GRN_ADDRESS_IS_IN_USE:
646 error_class = mrb_class_get_under(mrb, module, "AddressIsInUse");
647 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
648 "address is in use: <%s>(%d)",
649 ctx->errbuf, ctx->rc);
650 break;
651 case GRN_ZLIB_ERROR:
652 error_class = mrb_class_get_under(mrb, module, "ZlibError");
653 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
654 "zlib error: <%s>(%d)",
655 ctx->errbuf, ctx->rc);
656 break;
657 case GRN_LZ4_ERROR:
658 error_class = mrb_class_get_under(mrb, module, "LZ4Error");
659 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
660 "LZ4 error: <%s>(%d)",
661 ctx->errbuf, ctx->rc);
662 break;
663 case GRN_STACK_OVER_FLOW:
664 error_class = mrb_class_get_under(mrb, module, "StackOverFlow");
665 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
666 "stack over flow: <%s>(%d)",
667 ctx->errbuf, ctx->rc);
668 break;
669 case GRN_SYNTAX_ERROR:
670 error_class = mrb_class_get_under(mrb, module, "SyntaxError");
671 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
672 "syntax error: <%s>(%d)",
673 ctx->errbuf, ctx->rc);
674 break;
675 case GRN_RETRY_MAX:
676 error_class = mrb_class_get_under(mrb, module, "RetryMax");
677 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
678 "retry max: <%s>(%d)",
679 ctx->errbuf, ctx->rc);
680 break;
681 case GRN_INCOMPATIBLE_FILE_FORMAT:
682 error_class = mrb_class_get_under(mrb, module, "IncompatibleFileFormat");
683 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
684 "incompatible file format: <%s>(%d)",
685 ctx->errbuf, ctx->rc);
686 break;
687 case GRN_UPDATE_NOT_ALLOWED:
688 error_class = mrb_class_get_under(mrb, module, "UpdateNotAllowed");
689 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
690 "update not allowed: <%s>(%d)",
691 ctx->errbuf, ctx->rc);
692 break;
693 case GRN_TOO_SMALL_OFFSET:
694 error_class = mrb_class_get_under(mrb, module, "TooSmallOffset");
695 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
696 "too small offset: <%s>(%d)",
697 ctx->errbuf, ctx->rc);
698 break;
699 case GRN_TOO_LARGE_OFFSET:
700 error_class = mrb_class_get_under(mrb, module, "TooLargeOffset");
701 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
702 "too large offset: <%s>(%d)",
703 ctx->errbuf, ctx->rc);
704 break;
705 case GRN_TOO_SMALL_LIMIT:
706 error_class = mrb_class_get_under(mrb, module, "TooSmallLimit");
707 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
708 "too small limit: <%s>(%d)",
709 ctx->errbuf, ctx->rc);
710 break;
711 case GRN_CAS_ERROR:
712 error_class = mrb_class_get_under(mrb, module, "CASError");
713 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
714 "CAS error: <%s>(%d)",
715 ctx->errbuf, ctx->rc);
716 break;
717 case GRN_UNSUPPORTED_COMMAND_VERSION:
718 error_class = mrb_class_get_under(mrb, module, "UnsupportedCommandVersion");
719 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
720 "unsupported command version: <%s>(%d)",
721 ctx->errbuf, ctx->rc);
722 break;
723 case GRN_NORMALIZER_ERROR:
724 error_class = mrb_class_get_under(mrb, module, "NormalizerError");
725 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
726 "normalizer error: <%s>(%d)",
727 ctx->errbuf, ctx->rc);
728 break;
729 case GRN_TOKEN_FILTER_ERROR:
730 error_class = mrb_class_get_under(mrb, module, "TokenFilterError");
731 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
732 "token filter error: <%s>(%d)",
733 ctx->errbuf, ctx->rc);
734 break;
735 case GRN_COMMAND_ERROR:
736 error_class = mrb_class_get_under(mrb, module, "CommandError");
737 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
738 "command error: <%s>(%d)",
739 ctx->errbuf, ctx->rc);
740 break;
741 case GRN_PLUGIN_ERROR:
742 error_class = mrb_class_get_under(mrb, module, "PluginError");
743 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
744 "plugin error: <%s>(%d)",
745 ctx->errbuf, ctx->rc);
746 break;
747 case GRN_SCORER_ERROR:
748 error_class = mrb_class_get_under(mrb, module, "ScorerError");
749 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
750 "scorer error: <%s>(%d)",
751 ctx->errbuf, ctx->rc);
752 break;
753 case GRN_CANCEL:
754 error_class = mrb_class_get_under(mrb, module, "Cancel");
755 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
756 "cancel: <%s>(%d)",
757 ctx->errbuf, ctx->rc);
758 break;
759 case GRN_WINDOW_FUNCTION_ERROR:
760 error_class = mrb_class_get_under(mrb, module, "WindowFunctionError");
761 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
762 "window function error: <%s>(%d)",
763 ctx->errbuf, ctx->rc);
764 break;
765 case GRN_ZSTD_ERROR:
766 error_class = mrb_class_get_under(mrb, module, "ZstdError");
767 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
768 "Zstandard error: <%s>(%d)",
769 ctx->errbuf, ctx->rc);
770 break;
771 }
772
773 if (!error_class) {
774 error_class = mrb_class_get_under(mrb, module, "Error");
775 grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE,
776 "unsupported error: <%s>(%d)",
777 ctx->errbuf, ctx->rc);
778 }
779#undef MESSAGE_SIZE
780
781 mrb_raise(mrb, error_class, message);
782}
783
784void
785grn_mrb_ctx_init(grn_ctx *ctx)
786{
787 grn_mrb_data *data = &(ctx->impl->mrb);
788 mrb_state *mrb = data->state;
789 struct RClass *module = data->module;
790 struct RClass *klass;
791
792 klass = mrb_define_class_under(mrb, module, "Context", mrb->object_class);
793 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
794
795 mrb_define_class_method(mrb, klass, "instance",
796 ctx_class_instance, MRB_ARGS_NONE());
797
798 mrb_define_method(mrb, klass, "[]", ctx_array_reference, MRB_ARGS_REQ(1));
799 mrb_define_method(mrb, klass, "rc", ctx_get_rc, MRB_ARGS_NONE());
800 mrb_define_method(mrb, klass, "rc=", ctx_set_rc, MRB_ARGS_REQ(1));
801 mrb_define_method(mrb, klass, "error_level", ctx_get_error_level,
802 MRB_ARGS_NONE());
803 mrb_define_method(mrb, klass, "error_level=", ctx_set_error_level,
804 MRB_ARGS_REQ(1));
805 mrb_define_method(mrb, klass, "error_file", ctx_get_error_file,
806 MRB_ARGS_NONE());
807 mrb_define_method(mrb, klass, "error_file=", ctx_set_error_file,
808 MRB_ARGS_REQ(1));
809 mrb_define_method(mrb, klass, "error_line", ctx_get_error_line,
810 MRB_ARGS_NONE());
811 mrb_define_method(mrb, klass, "error_line=", ctx_set_error_line,
812 MRB_ARGS_REQ(1));
813 mrb_define_method(mrb, klass, "error_method", ctx_get_error_method,
814 MRB_ARGS_NONE());
815 mrb_define_method(mrb, klass, "error_method=", ctx_set_error_method,
816 MRB_ARGS_REQ(1));
817 mrb_define_method(mrb, klass, "error_message", ctx_get_error_message,
818 MRB_ARGS_NONE());
819 mrb_define_method(mrb, klass, "error_message=", ctx_set_error_message,
820 MRB_ARGS_REQ(1));
821 mrb_define_method(mrb, klass, "clear_error", ctx_clear_error, MRB_ARGS_NONE());
822 mrb_define_method(mrb, klass, "command_version",
823 ctx_get_command_version, MRB_ARGS_NONE());
824 mrb_define_method(mrb, klass, "command_version=",
825 ctx_set_command_version, MRB_ARGS_REQ(1));
826
827 mrb_define_method(mrb, klass, "output",
828 ctx_get_output, MRB_ARGS_NONE());
829 mrb_define_method(mrb, klass, "output=",
830 ctx_set_output, MRB_ARGS_REQ(1));
831
832 mrb_define_method(mrb, klass, "database", ctx_get_database,
833 MRB_ARGS_NONE());
834
835 mrb_define_method(mrb, klass, "opened?", ctx_is_opened,
836 MRB_ARGS_REQ(1));
837}
838#endif
839