|
2 | 2 |
|
3 | 3 | #include "simdjson.h"
|
4 | 4 |
|
5 |
| -VALUE rb_mFastJsonparser; |
6 |
| - |
7 |
| -VALUE rb_eFastJsonparserParseError; |
| 5 | +VALUE rb_eFastJsonparserUnknownError, rb_eFastJsonparserParseError; |
8 | 6 |
|
9 | 7 | using namespace simdjson;
|
10 | 8 |
|
@@ -93,36 +91,44 @@ static VALUE rb_fast_jsonparser_load(VALUE self, VALUE arg)
|
93 | 91 | return Qnil;
|
94 | 92 | }
|
95 | 93 |
|
96 |
| -static VALUE rb_fast_jsonparser_load_many(VALUE self, VALUE arg) |
| 94 | +static VALUE rb_fast_jsonparser_load_many(VALUE self, VALUE arg, VALUE batch_size) |
97 | 95 | {
|
98 | 96 | Check_Type(arg, T_STRING);
|
| 97 | + Check_Type(batch_size, T_FIXNUM); |
99 | 98 |
|
100 |
| - dom::parser parser; |
101 |
| - auto [docs, error] = parser.load_many(RSTRING_PTR(arg)); |
102 |
| - if (error == SUCCESS) |
103 |
| - { |
104 |
| - for (dom::element doc : docs) |
| 99 | + try { |
| 100 | + dom::parser parser; |
| 101 | + auto [docs, error] = parser.load_many(RSTRING_PTR(arg), FIX2INT(batch_size)); |
| 102 | + if (error == SUCCESS) |
105 | 103 | {
|
106 |
| - if (rb_block_given_p()) |
| 104 | + for (dom::element doc : docs) |
107 | 105 | {
|
108 | 106 | rb_yield(make_ruby_object(doc));
|
109 | 107 | }
|
| 108 | + return Qnil; |
110 | 109 | }
|
| 110 | + rb_raise(rb_eFastJsonparserParseError, "parse error"); |
111 | 111 | return Qnil;
|
| 112 | + } catch (simdjson::simdjson_error error) { |
| 113 | + rb_raise(rb_eFastJsonparserUnknownError, "%s", error.what()); |
112 | 114 | }
|
113 |
| - rb_raise(rb_eFastJsonparserParseError, "parse error"); |
114 |
| - return Qnil; |
115 | 115 | }
|
116 | 116 |
|
117 | 117 | extern "C"
|
118 | 118 | {
|
119 | 119 |
|
120 | 120 | void Init_fast_jsonparser(void)
|
121 | 121 | {
|
122 |
| - rb_mFastJsonparser = rb_define_module("FastJsonparser"); |
123 |
| - rb_eFastJsonparserParseError = rb_define_class_under(rb_mFastJsonparser, "ParseError", rb_eStandardError); |
| 122 | + VALUE rb_mFastJsonparser = rb_const_get(rb_cObject, rb_intern("FastJsonparser")); |
| 123 | + |
124 | 124 | rb_define_module_function(rb_mFastJsonparser, "parse", reinterpret_cast<VALUE (*)(...)>(rb_fast_jsonparser_parse), 1);
|
125 | 125 | rb_define_module_function(rb_mFastJsonparser, "load", reinterpret_cast<VALUE (*)(...)>(rb_fast_jsonparser_load), 1);
|
126 |
| - rb_define_module_function(rb_mFastJsonparser, "load_many", reinterpret_cast<VALUE (*)(...)>(rb_fast_jsonparser_load_many), 1); |
| 126 | + rb_define_module_function(rb_mFastJsonparser, "_load_many", reinterpret_cast<VALUE (*)(...)>(rb_fast_jsonparser_load_many), 2); |
| 127 | + |
| 128 | + rb_eFastJsonparserParseError = rb_const_get(rb_mFastJsonparser, rb_intern("ParseError")); |
| 129 | + rb_global_variable(&rb_eFastJsonparserParseError); |
| 130 | + rb_eFastJsonparserUnknownError = rb_const_get(rb_mFastJsonparser, rb_intern("UnknownError")); |
| 131 | + rb_global_variable(&rb_eFastJsonparserUnknownError); |
| 132 | + |
127 | 133 | }
|
128 | 134 | }
|
0 commit comments