mongory 0.8.1-aarch64-linux → 0.9.0-aarch64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +66 -0
  3. data/README.md +12 -0
  4. data/docs/advanced_usage.md +7 -0
  5. data/docs/clang_bridge.md +6 -0
  6. data/ext/mongory_ext/extconf.rb +6 -1
  7. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +25 -0
  8. data/ext/mongory_ext/mongory-core/src/foundations/array.c +22 -15
  9. data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +1 -0
  10. data/ext/mongory_ext/mongory-core/src/foundations/config.c +64 -27
  11. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +1 -0
  12. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +49 -0
  13. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +45 -0
  14. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +0 -2
  15. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +0 -6
  16. data/ext/mongory_ext/mongory-core/src/foundations/table.c +9 -10
  17. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +21 -14
  18. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +2 -2
  19. data/ext/mongory_ext/mongory-core/src/foundations/value.c +54 -9
  20. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c +31 -10
  21. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +2 -0
  22. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +2 -0
  23. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +32 -13
  24. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +34 -13
  25. data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.c +16 -3
  26. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +76 -15
  27. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +12 -0
  28. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c +57 -15
  29. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +43 -7
  30. data/ext/mongory_ext/mongory_ext.c +36 -687
  31. data/ext/mongory_ext/mongory_ext_internal.h +75 -0
  32. data/ext/mongory_ext/rb_mongory_adapters.c +140 -0
  33. data/ext/mongory_ext/rb_mongory_cmatcher.c +386 -0
  34. data/ext/mongory_ext/rb_mongory_value.c +260 -0
  35. data/lib/core/2.6/mongory_ext.so +0 -0
  36. data/lib/core/2.7/mongory_ext.so +0 -0
  37. data/lib/core/3.0/mongory_ext.so +0 -0
  38. data/lib/core/3.1/mongory_ext.so +0 -0
  39. data/lib/core/3.2/mongory_ext.so +0 -0
  40. data/lib/core/3.3/mongory_ext.so +0 -0
  41. data/lib/core/3.4/mongory_ext.so +0 -0
  42. data/lib/core/4.0/mongory_ext.so +0 -0
  43. data/lib/generators/mongory/install/templates/initializer.rb.erb +5 -0
  44. data/lib/mongory/converters/abstract_converter.rb +6 -5
  45. data/lib/mongory/converters/condition_converter.rb +2 -2
  46. data/lib/mongory/converters/key_converter.rb +1 -1
  47. data/lib/mongory/matchers/abstract_matcher.rb +1 -1
  48. data/lib/mongory/matchers/abstract_multi_matcher.rb +2 -2
  49. data/lib/mongory/matchers/array_record_matcher.rb +23 -11
  50. data/lib/mongory/matchers/elem_match_matcher.rb +2 -0
  51. data/lib/mongory/matchers/eq_matcher.rb +4 -2
  52. data/lib/mongory/matchers/field_matcher.rb +16 -3
  53. data/lib/mongory/matchers/hash_condition_matcher.rb +23 -6
  54. data/lib/mongory/matchers/in_matcher.rb +17 -1
  55. data/lib/mongory/matchers/ne_matcher.rb +7 -6
  56. data/lib/mongory/matchers/nin_matcher.rb +2 -6
  57. data/lib/mongory/matchers/regex_matcher.rb +29 -0
  58. data/lib/mongory/matchers.rb +7 -4
  59. data/lib/mongory/version.rb +1 -1
  60. data/lib/mongory.rb +57 -5
  61. metadata +8 -2
@@ -1,717 +1,66 @@
1
1
  /**
2
2
  * @file mongory_ext.c
3
- * @brief Ruby C extension wrapper for mongory-core
4
- *
5
- * This file provides Ruby bindings for the mongory-core C library,
6
- * allowing Ruby applications to use high-performance C-based matching
7
- * operations while maintaining the elegant Ruby DSL.
3
+ * @brief Extension entry point and shared pool/error helpers for mongory-rb.
8
4
  */
9
- #include "mongory-core.h"
10
- #include <ruby.h>
11
- #include <ruby/encoding.h>
5
+ #include "mongory_ext_internal.h"
6
+ #include <stdlib.h>
12
7
  #include <string.h>
13
8
 
14
- // Ruby module and class definitions
15
- static VALUE mMongory;
16
- static VALUE cMongoryMatcher;
17
- static VALUE cMongoryMatcherContext;
18
- static VALUE mMongoryMatchers;
9
+ VALUE rb_mongory_m_mongory;
10
+ VALUE rb_mongory_m_matchers;
11
+ VALUE rb_mongory_c_matcher;
12
+ VALUE rb_mongory_c_matcher_context;
13
+ VALUE rb_mongory_e_error;
14
+ VALUE rb_mongory_e_type_error;
15
+ VALUE rb_mongory_data_converter;
16
+ VALUE rb_mongory_condition_converter;
19
17
 
20
- // Error classes
21
- static VALUE eMongoryError;
22
- static VALUE eMongoryTypeError;
23
-
24
- // Converter instance
25
- static VALUE inMongoryDataConverter;
26
- static VALUE inMongoryConditionConverter;
27
-
28
- // Matcher wrapper structure
29
- typedef struct rb_mongory_matcher_t {
30
- mongory_matcher *matcher;
31
- mongory_value *condition;
32
- mongory_memory_pool *pool;
33
- mongory_memory_pool *scratch_pool;
34
- mongory_memory_pool *trace_pool;
35
- mongory_table *string_map;
36
- mongory_table *symbol_map;
37
- mongory_array *mark_list;
38
- VALUE ctx;
39
- } rb_mongory_matcher_t;
40
-
41
- typedef struct rb_mongory_memory_pool_t {
42
- mongory_memory_pool base;
43
- rb_mongory_matcher_t *owner;
44
- } rb_mongory_memory_pool_t;
45
-
46
- typedef struct rb_mongory_table_t {
47
- mongory_table base;
48
- VALUE rb_hash;
49
- rb_mongory_matcher_t *owner;
50
- } rb_mongory_table_t;
51
-
52
- typedef struct rb_mongory_array_t {
53
- mongory_array base;
54
- VALUE rb_array;
55
- rb_mongory_matcher_t *owner;
56
- } rb_mongory_array_t;
57
-
58
- typedef struct {
59
- mongory_table *table;
60
- mongory_memory_pool *pool;
61
- } hash_conv_ctx;
62
-
63
- // Forward declarations
64
- static void rb_mongory_matcher_mark(void *ptr);
65
- static void rb_mongory_matcher_free(void *ptr);
66
- mongory_value *rb_to_mongory_value_deep(mongory_memory_pool *pool, VALUE rb_value);
67
- mongory_value *rb_to_mongory_value_shallow(mongory_memory_pool *pool, VALUE rb_value);
68
- mongory_value *rb_mongory_table_wrap(mongory_memory_pool *pool, VALUE rb_hash);
69
- mongory_value *rb_mongory_array_wrap(mongory_memory_pool *pool, VALUE rb_array);
70
- static VALUE cache_fetch_string(rb_mongory_matcher_t *owner, const char *key);
71
- static VALUE cache_fetch_symbol(rb_mongory_matcher_t *owner, const char *key);
72
- static rb_mongory_memory_pool_t *rb_mongory_memory_pool_new();
73
- static void rb_mongory_matcher_parse_argv(rb_mongory_matcher_t *wrapper, int argc, VALUE *argv);
74
- static bool rb_mongory_error_handling(mongory_memory_pool *pool, char *error_message);
75
-
76
- static const rb_data_type_t rb_mongory_matcher_type = {
77
- .wrap_struct_name = "mongory_matcher",
78
- .function = {
79
- .dmark = rb_mongory_matcher_mark,
80
- .dfree = rb_mongory_matcher_free,
81
- .dsize = NULL,
82
- },
83
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
84
- };
85
-
86
- /**
87
- * Ruby method implementations
88
- */
89
-
90
- // Mongory::CMatcher.new(condition)
91
- static VALUE rb_mongory_matcher_new(int argc, VALUE *argv, VALUE class) {
92
- rb_mongory_memory_pool_t *matcher_pool = rb_mongory_memory_pool_new();
93
- mongory_memory_pool *matcher_pool_base = &matcher_pool->base;
94
- rb_mongory_memory_pool_t *scratch_pool = rb_mongory_memory_pool_new();
95
- mongory_memory_pool *scratch_pool_base = &scratch_pool->base;
96
- rb_mongory_matcher_t *wrapper = ALLOC(rb_mongory_matcher_t);
97
- wrapper->pool = matcher_pool_base;
98
- wrapper->scratch_pool = scratch_pool_base;
99
- wrapper->trace_pool = NULL;
100
- wrapper->ctx = NULL;
101
- wrapper->string_map = mongory_table_new(matcher_pool_base);
102
- wrapper->symbol_map = mongory_table_new(matcher_pool_base);
103
- wrapper->mark_list = mongory_array_new(matcher_pool_base);
104
- wrapper->condition = NULL;
105
- wrapper->matcher = NULL;
106
- matcher_pool->owner = wrapper;
107
- scratch_pool->owner = wrapper;
108
- rb_mongory_matcher_parse_argv(wrapper, argc, argv);
109
-
110
- mongory_matcher *matcher = mongory_matcher_new(matcher_pool_base, wrapper->condition, wrapper->ctx);
111
- if (rb_mongory_error_handling(matcher_pool_base, "Failed to create matcher")) {
112
- matcher_pool_base->free(matcher_pool_base);
113
- scratch_pool_base->free(scratch_pool_base);
114
- xfree(wrapper);
115
- return Qnil;
116
- }
117
-
118
- wrapper->matcher = matcher;
119
- return TypedData_Wrap_Struct(class, &rb_mongory_matcher_type, wrapper);
120
- }
121
-
122
- static void rb_mongory_matcher_parse_argv(rb_mongory_matcher_t *self, int argc, VALUE *argv) {
123
- VALUE condition, kw_hash;
124
- rb_scan_args(argc, argv, "1:", &condition, &kw_hash);
125
- const ID ctx_id[1] = { rb_intern("context") };
126
- VALUE kw_vals[1] = { Qundef };
127
- if (kw_hash != Qnil) {
128
- rb_get_kwargs(kw_hash, ctx_id, 1, 0, kw_vals);
129
- }
130
- if (kw_vals[0] != Qundef) {
131
- self->ctx = kw_vals[0];
132
- } else {
133
- self->ctx = rb_funcall(cMongoryMatcherContext, rb_intern("new"), 0);
134
- }
135
- VALUE converted_condition = rb_funcall(inMongoryConditionConverter, rb_intern("convert"), 1, condition);
136
- self->condition = rb_to_mongory_value_deep(self->pool, converted_condition);
137
- self->mark_list->push(self->mark_list, self->condition);
138
- mongory_value *store_ctx = mongory_value_wrap_u(self->pool, (void *)self->ctx);
139
- store_ctx->origin = (void *)self->ctx;
140
- self->mark_list->push(self->mark_list, store_ctx);
141
- }
142
-
143
- // Mongory::CMatcher#match(data)
144
- static VALUE rb_mongory_matcher_match(VALUE self, VALUE data) {
145
- rb_mongory_matcher_t *self_wrapper;
146
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, self_wrapper);
147
- mongory_matcher *matcher = self_wrapper->matcher;
148
- mongory_memory_pool *scratch_pool = self_wrapper->scratch_pool;
149
- mongory_memory_pool *trace_pool = self_wrapper->trace_pool;
150
- mongory_value *data_value = rb_to_mongory_value_shallow(scratch_pool, data);
151
-
152
- if (rb_mongory_error_handling(scratch_pool, "Match failed")) {
153
- return Qnil;
154
- }
155
-
156
- bool result = mongory_matcher_match(matcher, data_value);
157
-
158
- if (trace_pool) {
159
- mongory_matcher_print_trace(matcher);
160
- trace_pool->reset(trace_pool);
161
- mongory_matcher_enable_trace(matcher, trace_pool);
162
- }
163
-
164
- scratch_pool->reset(scratch_pool);
165
-
166
- return result ? Qtrue : Qfalse;
167
- }
168
-
169
- // Mongory::CMatcher#explain
170
- static VALUE rb_mongory_matcher_explain(VALUE self) {
171
- rb_mongory_matcher_t *wrapper;
172
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
173
- mongory_matcher_explain(wrapper->matcher, wrapper->scratch_pool);
174
- if (rb_mongory_error_handling(wrapper->scratch_pool, "Explain failed")) {
175
- return Qnil;
176
- }
177
- wrapper->scratch_pool->reset(wrapper->scratch_pool);
178
- return Qnil;
179
- }
180
-
181
- // Mongory::CMatcher#trace(data)
182
- static VALUE rb_mongory_matcher_trace(VALUE self, VALUE data) {
183
- rb_mongory_matcher_t *wrapper;
184
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
185
- mongory_memory_pool *trace_pool = mongory_memory_pool_new();
186
- mongory_value *data_value = rb_to_mongory_value_shallow(trace_pool, data);
187
-
188
- if (rb_mongory_error_handling(trace_pool, "Trace failed")) {
189
- trace_pool->free(trace_pool);
190
- return Qnil;
191
- }
192
-
193
- bool matched = mongory_matcher_trace(wrapper->matcher, data_value);
194
- trace_pool->free(trace_pool);
195
-
196
- return matched ? Qtrue : Qfalse;
197
- }
198
-
199
- // Mongory::CMatcher#enable_trace
200
- static VALUE rb_mongory_matcher_enable_trace(VALUE self) {
201
- rb_mongory_matcher_t *wrapper;
202
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
203
- mongory_memory_pool *trace_pool = rb_mongory_memory_pool_new();
204
- mongory_matcher_enable_trace(wrapper->matcher, trace_pool);
205
-
206
- if (rb_mongory_error_handling(trace_pool, "Enable trace failed")) {
207
- trace_pool->free(trace_pool);
208
- return Qnil;
209
- }
210
- wrapper->trace_pool = trace_pool;
211
-
212
- return Qnil;
213
- }
214
-
215
- // Mongory::CMatcher#disable_trace
216
- static VALUE rb_mongory_matcher_disable_trace(VALUE self) {
217
- rb_mongory_matcher_t *wrapper;
218
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
219
- mongory_memory_pool *trace_pool = wrapper->trace_pool;
220
- mongory_matcher_disable_trace(wrapper->matcher);
221
- rb_mongory_error_handling(trace_pool, "Disable trace failed");
222
- trace_pool->free(trace_pool);
223
- wrapper->trace_pool = NULL;
224
-
225
- return Qnil;
226
- }
227
-
228
- // Mongory::CMatcher#print_trace
229
- static VALUE rb_mongory_matcher_print_trace(VALUE self) {
230
- rb_mongory_matcher_t *wrapper;
231
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
232
- mongory_matcher_print_trace(wrapper->matcher);
233
- rb_mongory_error_handling(wrapper->trace_pool, "Print trace failed");
234
-
235
- return Qnil;
236
- }
237
-
238
- // Mongory::CMatcher#condition
239
- static VALUE rb_mongory_matcher_condition(VALUE self) {
240
- rb_mongory_matcher_t *wrapper;
241
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
242
-
243
- return (VALUE)wrapper->condition->origin;
244
- }
245
-
246
- // Mongory::CMatcher#context
247
- static VALUE rb_mongory_matcher_context(VALUE self) {
248
- rb_mongory_matcher_t *wrapper;
249
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
250
-
251
- return wrapper->ctx ? wrapper->ctx : Qnil;
252
- }
253
-
254
- // Mongory::CMatcher.trace_result_colorful=(colorful)
255
- static VALUE rb_mongory_matcher_trace_result_colorful(VALUE self, VALUE colorful) {
256
- (void)self;
257
- mongory_matcher_trace_result_colorful_set(RTEST(colorful));
258
-
259
- return Qnil;
260
- }
261
-
262
- /**
263
- * Create a new memory pool
264
- */
265
- static rb_mongory_memory_pool_t *rb_mongory_memory_pool_new() {
18
+ rb_mongory_memory_pool_t *rb_mongory_memory_pool_new(void) {
266
19
  rb_mongory_memory_pool_t *pool = malloc(sizeof(rb_mongory_memory_pool_t));
267
- mongory_memory_pool *base = mongory_memory_pool_new();
268
- memcpy(&pool->base, base, sizeof(mongory_memory_pool));
269
- free(base);
270
-
271
- return pool;
272
- }
273
-
274
- /**
275
- * Ruby GC management functions
276
- */
277
- static void rb_mongory_matcher_free(void *ptr) {
278
- rb_mongory_matcher_t *wrapper = (rb_mongory_matcher_t *)ptr;
279
- mongory_memory_pool *pool = wrapper->pool;
280
- mongory_memory_pool *scratch_pool = wrapper->scratch_pool;
281
- mongory_memory_pool *trace_pool = wrapper->trace_pool;
282
- pool->free(pool);
283
- scratch_pool->free(scratch_pool);
284
- if (trace_pool) {
285
- trace_pool->free(trace_pool);
286
- }
287
- xfree(wrapper);
288
- }
289
-
290
- /**
291
- * GC marking callback for mongory_array
292
- */
293
- static bool gc_mark_array_cb(mongory_value *value, void *acc) {
294
- (void)acc;
295
- if (value && value->origin) rb_gc_mark((VALUE)value->origin);
296
- return true;
297
- }
298
-
299
- /**
300
- * GC marking callback for mongory_matcher
301
- */
302
- static void rb_mongory_matcher_mark(void *ptr) {
303
- rb_mongory_matcher_t *self = (rb_mongory_matcher_t *)ptr;
304
- if (!self) return;
305
- self->mark_list->each(self->mark_list, NULL, gc_mark_array_cb);
306
- }
307
-
308
- /**
309
- * Helper functions for Ruby/C conversion
310
- */
311
-
312
- // Helper function to convert Ruby value to C string
313
- static char *rb_mongory_value_to_cstr(mongory_value *value, mongory_memory_pool *pool) {
314
- (void)pool;
315
- VALUE rb_value = (VALUE)value->origin;
316
- VALUE rb_str = rb_funcall(rb_value, rb_intern("inspect"), 0);
317
- return StringValueCStr(rb_str);
318
- }
319
-
320
- // Helper function for primitive conversion of Ruby value to mongory_value
321
- static mongory_value *rb_to_mongory_value_primitive(mongory_memory_pool *pool, VALUE rb_value) {
322
- mongory_value *mg_value = NULL;
323
- switch (TYPE(rb_value)) {
324
- case T_NIL:
325
- mg_value = mongory_value_wrap_n(pool, NULL);
326
- break;
327
-
328
- case T_TRUE:
329
- mg_value = mongory_value_wrap_b(pool, true);
330
- break;
331
-
332
- case T_FALSE:
333
- mg_value = mongory_value_wrap_b(pool, false);
334
- break;
335
-
336
- case T_FIXNUM:
337
- mg_value = mongory_value_wrap_i(pool, rb_num2long_inline(rb_value));
338
- break;
339
-
340
- case T_BIGNUM:
341
- mg_value = mongory_value_wrap_i(pool, rb_num2ll_inline(rb_value));
342
- break;
343
-
344
- case T_FLOAT:
345
- mg_value = mongory_value_wrap_d(pool, rb_num2dbl(rb_value));
346
- break;
347
-
348
- case T_STRING:
349
- mg_value = mongory_value_wrap_s(pool, StringValueCStr(rb_value));
350
- break;
351
-
352
- case T_SYMBOL:
353
- mg_value = mongory_value_wrap_s(pool, (char *)rb_id2name(rb_sym2id(rb_value)));
354
- break;
355
-
356
- case T_REGEXP:
357
- mg_value = mongory_value_wrap_regex(pool, (void *)rb_value);
358
- break;
359
- }
360
- return mg_value;
361
- }
362
- // Shallow conversion: Convert Ruby value to mongory_value (fully materialize arrays/tables)
363
- static mongory_value *rb_to_mongory_value_shallow_rec(mongory_memory_pool *pool, VALUE rb_value, bool converted) {
364
- mongory_value *mg_value = rb_to_mongory_value_primitive(pool, rb_value);
365
- if (mg_value) {
366
- mg_value->origin = rb_value;
367
- return mg_value;
368
- }
369
-
370
- switch (TYPE(rb_value)) {
371
- case T_ARRAY: {
372
- mg_value = rb_mongory_array_wrap(pool, rb_value);
373
- break;
374
- }
375
-
376
- case T_HASH: {
377
- mg_value = rb_mongory_table_wrap(pool, rb_value);
378
- break;
379
- }
380
-
381
- default:
382
- if (converted) {
383
- mg_value = mongory_value_wrap_u(pool, (void *)rb_value);
384
- break;
385
- } else {
386
- VALUE converted_value = rb_funcall(inMongoryDataConverter, rb_intern("convert"), 1, rb_value);
387
- return rb_to_mongory_value_shallow_rec(pool, converted_value, true);
388
- }
389
- }
390
- mg_value->origin = rb_value;
391
- return mg_value;
392
- }
393
-
394
- // Shallow conversion: Convert Ruby value to mongory_value (fully materialize arrays/tables)
395
- mongory_value *rb_to_mongory_value_shallow(mongory_memory_pool *pool, VALUE rb_value) {
396
- return rb_to_mongory_value_shallow_rec(pool, rb_value, false);
397
- }
398
-
399
- // Helper function for deep conversion of hash values
400
- static int hash_foreach_deep_convert_cb(VALUE key, VALUE val, VALUE ptr) {
401
- hash_conv_ctx *ctx = (hash_conv_ctx *)ptr;
402
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)ctx->pool;
403
- rb_mongory_matcher_t *owner = rb_pool->owner;
404
- mongory_table *store_map;
405
- char *key_str;
406
- if (SYMBOL_P(key)) {
407
- key_str = (char *)rb_id2name(SYM2ID(key));
408
- store_map = owner->symbol_map;
409
- } else {
410
- key_str = StringValueCStr(key);
411
- store_map = owner->string_map;
412
- }
413
- mongory_value *store = mongory_value_wrap_u(ctx->pool, NULL);
414
- store->origin = (void *)key;
415
- store_map->set(store_map, key_str, store);
416
- mongory_value *cval = rb_to_mongory_value_deep(ctx->pool, val);
417
- ctx->table->set(ctx->table, key_str, cval);
418
- return ST_CONTINUE;
419
- }
420
-
421
- // Deep conversion: Convert Ruby value to mongory_value (fully materialize arrays/tables)
422
- static mongory_value *rb_to_mongory_value_deep_rec(mongory_memory_pool *pool, VALUE rb_value, bool converted) {
423
- mongory_value *mg_value = rb_to_mongory_value_primitive(pool, rb_value);
424
- if (mg_value) {
425
- mg_value->origin = rb_value;
426
- return mg_value;
427
- }
428
- switch (TYPE(rb_value)) {
429
- case T_ARRAY: {
430
- mongory_array *array = mongory_array_new(pool);
431
-
432
- for (long i = 0; i < RARRAY_LEN(rb_value); i++) {
433
- array->push(array, rb_to_mongory_value_deep(pool, RARRAY_AREF(rb_value, i)));
434
- }
435
- mg_value = mongory_value_wrap_a(pool, array);
436
- break;
437
- }
438
-
439
- case T_HASH: {
440
- mongory_table *table = mongory_table_new(pool);
441
-
442
- hash_conv_ctx ctx = {table, pool};
443
- rb_hash_foreach(rb_value, hash_foreach_deep_convert_cb, (VALUE)&ctx);
444
-
445
- mg_value = mongory_value_wrap_t(pool, table);
446
- break;
447
- }
448
-
449
- default:
450
- if (converted) {
451
- mg_value = mongory_value_wrap_u(pool, (void *)rb_value);
452
- break;
453
- } else {
454
- VALUE converted_value = rb_funcall(inMongoryDataConverter, rb_intern("convert"), 1, rb_value);
455
- mg_value = rb_to_mongory_value_deep_rec(pool, converted_value, true);
456
- break;
457
- }
458
- }
459
- mg_value->origin = rb_value;
460
- return mg_value;
461
- }
462
-
463
- // Deep conversion: Convert Ruby value to mongory_value (fully materialize arrays/tables)
464
- mongory_value *rb_to_mongory_value_deep(mongory_memory_pool *pool, VALUE rb_value) {
465
- return rb_to_mongory_value_deep_rec(pool, rb_value, false);
466
- }
467
-
468
- // Get value from Ruby hash via mongory_table
469
- mongory_value *rb_mongory_table_get(mongory_table *self, char *key) {
470
- rb_mongory_table_t *table = (rb_mongory_table_t *)self;
471
- VALUE rb_hash = table->rb_hash;
472
- VALUE rb_value = Qundef;
473
-
474
- // Use cached Ruby String key if possible
475
- VALUE key_str = cache_fetch_string(table->owner, key);
476
- rb_value = rb_hash_lookup2(rb_hash, key_str, Qundef);
477
-
478
- if (rb_value == Qundef) {
479
- // Fallback to Symbol key, using cache
480
- VALUE key_sym = cache_fetch_symbol(table->owner, key);
481
- rb_value = rb_hash_lookup2(rb_hash, key_sym, Qundef);
482
- }
483
-
484
- if (rb_value == Qundef) {
20
+ if (!pool) {
485
21
  return NULL;
486
22
  }
487
- return rb_to_mongory_value_shallow(table->base.pool, rb_value);
488
- }
489
-
490
- // Shallow conversion: Wrap Ruby hash as mongory_table
491
- mongory_value *rb_mongory_table_wrap(mongory_memory_pool *pool, VALUE rb_hash) {
492
- rb_mongory_table_t *table = MG_ALLOC_PTR(pool, rb_mongory_table_t);
493
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
494
- table->base.pool = pool;
495
- table->base.get = rb_mongory_table_get;
496
- table->rb_hash = rb_hash;
497
- table->base.count = RHASH_SIZE(rb_hash);
498
- table->owner = rb_pool->owner;
499
- mongory_value *mg_value = mongory_value_wrap_t(pool, &table->base);
500
- mg_value->origin = (void *)rb_hash;
501
- mg_value->to_str = rb_mongory_value_to_cstr;
502
- return mg_value;
503
- }
504
-
505
- // Get value from Ruby array via mongory_array
506
- static mongory_value *rb_mongory_array_get(mongory_array *self, size_t index) {
507
- rb_mongory_array_t *array = (rb_mongory_array_t *)self;
508
- VALUE rb_array = array->rb_array;
509
- if (index >= (size_t)RARRAY_LEN(rb_array)) {
510
- return NULL;
511
- }
512
- VALUE rb_value = rb_ary_entry(rb_array, index);
513
- return rb_to_mongory_value_shallow(self->pool, rb_value);
514
- }
515
-
516
- // Shallow conversion: Wrap Ruby array as mongory_array
517
- mongory_value *rb_mongory_array_wrap(mongory_memory_pool *pool, VALUE rb_array) {
518
- rb_mongory_array_t *array = MG_ALLOC_PTR(pool, rb_mongory_array_t);
519
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
520
- array->base.pool = pool;
521
- array->base.get = rb_mongory_array_get;
522
- array->rb_array = rb_array;
523
- array->base.count = RARRAY_LEN(rb_array);
524
- array->owner = rb_pool->owner;
525
- mongory_value *mg_value = mongory_value_wrap_a(pool, &array->base);
526
- mg_value->origin = (void *)rb_array;
527
- mg_value->to_str = rb_mongory_value_to_cstr;
528
- return mg_value;
529
- }
530
-
531
- // Convert mongory_value to Ruby value (returns the original Ruby value where binding in below conversion is needed)
532
- void *mongory_value_to_ruby(mongory_memory_pool *pool, mongory_value *value) {
533
- (void)pool;
534
- if (!value)
535
- return NULL;
536
- return value->origin;
537
- }
538
-
539
- // ===== Cache helper implementations =====
540
- static VALUE cache_fetch_string(rb_mongory_matcher_t *owner, const char *key) {
541
- if (!owner || !owner->string_map) return rb_utf8_str_new_cstr(key);
542
- mongory_value *v = owner->string_map->get(owner->string_map, (char *)key);
543
- if (v && v->origin) return (VALUE)v->origin;
544
- VALUE s = rb_utf8_str_new_cstr(key);
545
- mongory_value *store = mongory_value_wrap_u(owner->pool, NULL);
546
- store->origin = (void *)s;
547
- owner->string_map->set(owner->string_map, (char *)key, store);
548
- owner->mark_list->push(owner->mark_list, store);
549
- return s;
550
- }
551
-
552
- // Helper function to convert char* key to Ruby symbol
553
- static inline VALUE char_key_to_symbol(const char *key, rb_encoding *enc) {
554
- ID id = rb_check_id_cstr(key, (long)strlen(key), enc);
555
- if (!id) id = rb_intern3(key, (long)strlen(key), enc);
556
- return ID2SYM(id);
557
- }
558
-
559
- // Cache helper for Ruby symbol keys
560
- static VALUE cache_fetch_symbol(rb_mongory_matcher_t *owner, const char *key) {
561
- rb_encoding *enc = rb_utf8_encoding();
562
- if (!owner || !owner->symbol_map) {
563
- return char_key_to_symbol(key, enc);
564
- }
565
- mongory_value *v = owner->symbol_map->get(owner->symbol_map, (char *)key);
566
- if (v && v->origin) return (VALUE)v->origin;
567
- VALUE sym = char_key_to_symbol(key, enc);
568
- mongory_value *store = mongory_value_wrap_u(owner->pool, NULL);
569
- store->origin = (void *)sym;
570
- owner->symbol_map->set(owner->symbol_map, (char *)key, store);
571
- owner->mark_list->push(owner->mark_list, store);
572
- return sym;
573
- }
574
-
575
- // Regex adapter bridging to Ruby's Regexp
576
- static bool rb_mongory_regex_match_adapter(mongory_memory_pool *pool, mongory_value *pattern, mongory_value *value) {
577
- if (!pattern || !value) {
578
- return false;
579
- }
580
- if (value->type != MONGORY_TYPE_STRING) {
581
- return false;
582
- }
583
-
584
- VALUE rb_str = (VALUE)value->origin;
585
- VALUE rb_re = Qnil;
586
-
587
- if (pattern->type == MONGORY_TYPE_REGEX && pattern->data.regex) {
588
- rb_re = (VALUE)pattern->data.regex;
589
- } else if (pattern->type == MONGORY_TYPE_STRING) {
590
- rb_re = rb_funcall(rb_cRegexp, rb_intern("new"), 1, (VALUE)pattern->origin);
591
- mongory_value *temp_value = mongory_value_wrap_regex(pool, (void *)rb_re);
592
- memcpy(pattern, temp_value, sizeof(mongory_value));
593
- pattern->origin = (void *)rb_re;
594
- } else {
595
- return false;
596
- }
597
-
598
- VALUE matched = rb_funcall(rb_re, rb_intern("match?"), 1, rb_str);
599
- return RTEST(matched);
600
- }
601
-
602
- // Regex adapter bridging to Ruby's Regexp
603
- static char *rb_mongory_regex_stringify_adapter(mongory_memory_pool *pool, mongory_value *pattern) {
604
- (void)pool;
605
- if (pattern->type != MONGORY_TYPE_REGEX) {
606
- return NULL;
607
- }
608
- VALUE rb_re = (VALUE)pattern->data.regex;
609
- VALUE rb_str = rb_funcall(rb_re, rb_intern("inspect"), 0);
610
- return StringValueCStr(rb_str);
611
- }
612
-
613
- // Custom matcher adapter bridging to Ruby's custom matcher
614
- static mongory_matcher_custom_context *rb_mongory_custom_matcher_build(char *key, mongory_value *condition, void *ctx) {
615
- mongory_memory_pool *pool = condition->pool;
616
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
617
- rb_mongory_matcher_t *owner = rb_pool->owner;
618
- VALUE matcher_class = rb_funcall(mMongoryMatchers, rb_intern("lookup"), 1, cache_fetch_string(owner, key));
619
- if (matcher_class == Qnil) {
620
- return NULL;
621
- }
622
- VALUE kw_hash = rb_hash_new();
623
- rb_hash_aset(kw_hash, ID2SYM(rb_intern("context")), (VALUE)ctx);
624
-
625
- #ifdef RB_PASS_KEYWORDS
626
- VALUE argv_new[2] = { (VALUE)condition->origin, kw_hash };
627
- VALUE matcher = rb_funcallv_kw(matcher_class, rb_intern("new"), 2, argv_new, RB_PASS_KEYWORDS);
628
- #else
629
- VALUE matcher = rb_funcall(matcher_class, rb_intern("new"), 2, (VALUE)condition->origin, kw_hash);
630
- #endif
631
-
632
- if (matcher == Qnil) {
633
- return NULL;
634
- }
635
- mongory_value *matcher_value = mongory_value_wrap_u(pool, NULL);
636
- matcher_value->origin = (void *)matcher;
637
- owner->mark_list->push(owner->mark_list, matcher_value);
638
- VALUE class_name = rb_funcall(matcher_class, rb_intern("name"), 0);
639
- mongory_matcher_custom_context *return_ctx = MG_ALLOC_PTR(pool, mongory_matcher_custom_context);
640
- if (return_ctx == NULL) {
23
+ mongory_memory_pool *base = mongory_memory_pool_new();
24
+ if (!base) {
25
+ free(pool);
641
26
  return NULL;
642
27
  }
643
- return_ctx->name = StringValueCStr(class_name);
644
- return_ctx->external_matcher = (void *)matcher;
645
- return return_ctx;
646
- }
647
-
648
- // Custom matcher adapter bridging to Ruby's custom matcher
649
- static bool rb_mongory_custom_matcher_match(void *ruby_matcher, mongory_value *value) {
650
- VALUE matcher = (VALUE)ruby_matcher;
651
- VALUE match_result = rb_funcall(matcher, rb_intern("match?"), 1, value->origin);
652
- return RTEST(match_result);
653
- }
28
+ memcpy(&pool->base, base, sizeof(mongory_memory_pool));
29
+ pool->owner = NULL;
30
+ pool->next_scratch = NULL;
31
+ free(base);
654
32
 
655
- // Custom matcher adapter bridging to Ruby's custom matcher
656
- static bool rb_mongory_custom_matcher_lookup(char *key) {
657
- VALUE matcher_class = rb_funcall(mMongoryMatchers, rb_intern("lookup"), 1, rb_str_new_cstr(key));
658
- return RTEST(matcher_class);
33
+ return pool;
659
34
  }
660
35
 
661
- // Error handling for mongory_memory_pool
662
- static bool rb_mongory_error_handling(mongory_memory_pool *pool, char *error_message) {
36
+ bool rb_mongory_error_handling(mongory_memory_pool *pool, const char *error_message) {
663
37
  if (pool->error) {
664
- rb_raise(eMongoryTypeError, "%s: %s", error_message, pool->error->message);
38
+ const char *msg = pool->error->message;
665
39
  pool->error = NULL;
666
- return true;
40
+ rb_raise(rb_mongory_e_type_error, "%s: %s", error_message, msg);
667
41
  }
668
42
  return false;
669
43
  }
670
44
 
671
- /**
672
- * Extension initialization
673
- */
674
45
  void Init_mongory_ext(void) {
675
46
  // Initialize mongory core
676
47
  mongory_init();
677
48
 
678
49
  // Define modules and classes
679
- mMongory = rb_define_module("Mongory");
680
- cMongoryMatcher = rb_define_class_under(mMongory, "CMatcher", rb_cObject);
681
- mMongoryMatchers = rb_define_module_under(mMongory, "Matchers");
682
- VALUE mMongoryUtils = rb_define_module_under(mMongory, "Utils");
683
- cMongoryMatcherContext = rb_define_class_under(mMongoryUtils, "Context", rb_cObject);
50
+ rb_mongory_m_mongory = rb_define_module("Mongory");
51
+ rb_mongory_m_matchers = rb_define_module_under(rb_mongory_m_mongory, "Matchers");
52
+ VALUE mMongoryUtils = rb_define_module_under(rb_mongory_m_mongory, "Utils");
53
+ rb_mongory_c_matcher_context = rb_define_class_under(mMongoryUtils, "Context", rb_cObject);
54
+
684
55
  // Mongory converters
685
- inMongoryDataConverter = rb_funcall(mMongory, rb_intern("data_converter"), 0);
686
- inMongoryConditionConverter = rb_funcall(mMongory, rb_intern("condition_converter"), 0);
56
+ rb_mongory_data_converter = rb_funcall(rb_mongory_m_mongory, rb_intern("data_converter"), 0);
57
+ rb_mongory_condition_converter = rb_funcall(rb_mongory_m_mongory, rb_intern("condition_converter"), 0);
687
58
 
688
59
  // Define error classes
689
- eMongoryError = rb_define_class_under(mMongory, "Error", rb_eStandardError);
690
- eMongoryTypeError = rb_define_class_under(mMongory, "TypeError", eMongoryError);
691
-
692
- // Define Matcher methods
693
- rb_define_singleton_method(cMongoryMatcher, "new", rb_mongory_matcher_new, -1);
694
- rb_define_singleton_method(cMongoryMatcher, "trace_result_colorful=", rb_mongory_matcher_trace_result_colorful, 1);
695
- rb_define_method(cMongoryMatcher, "match?", rb_mongory_matcher_match, 1);
696
- rb_define_method(cMongoryMatcher, "explain", rb_mongory_matcher_explain, 0);
697
- rb_define_method(cMongoryMatcher, "condition", rb_mongory_matcher_condition, 0);
698
- rb_define_method(cMongoryMatcher, "context", rb_mongory_matcher_context, 0);
699
- rb_define_method(cMongoryMatcher, "trace", rb_mongory_matcher_trace, 1);
700
- rb_define_method(cMongoryMatcher, "enable_trace", rb_mongory_matcher_enable_trace, 0);
701
- rb_define_method(cMongoryMatcher, "disable_trace", rb_mongory_matcher_disable_trace, 0);
702
- rb_define_method(cMongoryMatcher, "print_trace", rb_mongory_matcher_print_trace, 0);
703
-
704
- // Set regex adapter to use Ruby's Regexp
705
- mongory_regex_func_set(rb_mongory_regex_match_adapter);
706
- mongory_regex_stringify_func_set(rb_mongory_regex_stringify_adapter);
707
-
708
- // Set value converter functions
709
- mongory_value_converter_deep_convert_set(rb_to_mongory_value_deep);
710
- mongory_value_converter_shallow_convert_set(rb_to_mongory_value_shallow);
711
- mongory_value_converter_recover_set(mongory_value_to_ruby);
60
+ rb_mongory_e_error = rb_define_class_under(rb_mongory_m_mongory, "Error", rb_eStandardError);
61
+ rb_mongory_e_type_error = rb_define_class_under(rb_mongory_m_mongory, "TypeError", rb_mongory_e_error);
712
62
 
713
- // Set custom matcher adapter
714
- mongory_custom_matcher_match_func_set(rb_mongory_custom_matcher_match);
715
- mongory_custom_matcher_build_func_set(rb_mongory_custom_matcher_build);
716
- mongory_custom_matcher_lookup_func_set(rb_mongory_custom_matcher_lookup);
63
+ // Register CMatcher class, methods, and core bridges
64
+ rb_mongory_init_cmatcher();
65
+ rb_mongory_register_adapters();
717
66
  }