mongory 0.9.1-aarch64-linux → 2.0.1-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 (98) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +115 -420
  4. data/SUBMODULE_INTEGRATION.md +7 -20
  5. data/docs/clang_bridge.md +28 -63
  6. data/docs/migration.md +17 -0
  7. data/docs/performance.md +14 -51
  8. data/examples/benchmark.rb +10 -26
  9. data/ext/mongory_ext/extconf.rb +1 -1
  10. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +62 -0
  11. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +31 -0
  12. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +3 -0
  13. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +20 -0
  14. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +31 -4
  15. data/ext/mongory_ext/mongory-core/src/foundations/array.c +48 -6
  16. data/ext/mongory_ext/mongory-core/src/foundations/config.c +30 -2
  17. data/ext/mongory_ext/mongory-core/src/foundations/error.c +10 -0
  18. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +78 -19
  19. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +73 -19
  20. data/ext/mongory_ext/mongory-core/src/foundations/table.c +8 -3
  21. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +44 -55
  22. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +7 -0
  23. data/ext/mongory_ext/mongory-core/src/foundations/value.c +3 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +68 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +14 -0
  26. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +9 -4
  27. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +132 -29
  28. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +1 -1
  29. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.c → existence_matcher.c} +10 -12
  30. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.h → existence_matcher.h} +4 -4
  31. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -6
  32. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.c → implicit_array_matcher.c} +57 -38
  33. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.h → implicit_array_matcher.h} +5 -5
  34. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +8 -10
  35. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +337 -96
  36. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +59 -21
  37. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +7 -1
  38. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +4 -4
  39. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -1
  40. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.c → value_matcher.c} +104 -60
  41. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.h → value_matcher.h} +13 -13
  42. data/ext/mongory_ext/mongory_ext.c +2 -2
  43. data/ext/mongory_ext/mongory_ext_internal.h +2 -3
  44. data/ext/mongory_ext/{rb_mongory_cmatcher.c → rb_mongory_matcher.c} +145 -91
  45. data/ext/mongory_ext/rb_mongory_value.c +67 -0
  46. data/lib/core/2.6/mongory_ext.so +0 -0
  47. data/lib/core/2.7/mongory_ext.so +0 -0
  48. data/lib/core/3.0/mongory_ext.so +0 -0
  49. data/lib/core/3.1/mongory_ext.so +0 -0
  50. data/lib/core/3.2/mongory_ext.so +0 -0
  51. data/lib/core/3.3/mongory_ext.so +0 -0
  52. data/lib/core/3.4/mongory_ext.so +0 -0
  53. data/lib/core/4.0/mongory_ext.so +0 -0
  54. data/lib/generators/mongory/install/templates/initializer.rb.erb +0 -5
  55. data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
  56. data/lib/mongory/converters/condition_converter.rb +1 -1
  57. data/lib/mongory/converters/converted.rb +28 -6
  58. data/lib/mongory/converters/key_converter.rb +2 -0
  59. data/lib/mongory/matcher.rb +50 -0
  60. data/lib/mongory/matchers/abstract_matcher.rb +18 -192
  61. data/lib/mongory/matchers.rb +68 -84
  62. data/lib/mongory/query_builder.rb +29 -42
  63. data/lib/mongory/utils/context.rb +10 -15
  64. data/lib/mongory/utils/tree_renderer.rb +98 -0
  65. data/lib/mongory/utils.rb +2 -67
  66. data/lib/mongory/version.rb +1 -1
  67. data/lib/mongory.rb +9 -59
  68. data/script/bench.rb +54 -0
  69. data/scripts/build_with_core.sh +0 -1
  70. metadata +13 -37
  71. data/lib/mongory/c_matcher.rb +0 -57
  72. data/lib/mongory/c_query_builder.rb +0 -44
  73. data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -150
  74. data/lib/mongory/matchers/and_matcher.rb +0 -73
  75. data/lib/mongory/matchers/array_record_matcher.rb +0 -105
  76. data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
  77. data/lib/mongory/matchers/eq_matcher.rb +0 -48
  78. data/lib/mongory/matchers/every_matcher.rb +0 -56
  79. data/lib/mongory/matchers/exists_matcher.rb +0 -53
  80. data/lib/mongory/matchers/field_matcher.rb +0 -160
  81. data/lib/mongory/matchers/gt_matcher.rb +0 -41
  82. data/lib/mongory/matchers/gte_matcher.rb +0 -41
  83. data/lib/mongory/matchers/hash_condition_matcher.rb +0 -80
  84. data/lib/mongory/matchers/in_matcher.rb +0 -84
  85. data/lib/mongory/matchers/literal_matcher.rb +0 -126
  86. data/lib/mongory/matchers/lt_matcher.rb +0 -41
  87. data/lib/mongory/matchers/lte_matcher.rb +0 -41
  88. data/lib/mongory/matchers/ne_matcher.rb +0 -39
  89. data/lib/mongory/matchers/nin_matcher.rb +0 -64
  90. data/lib/mongory/matchers/not_matcher.rb +0 -40
  91. data/lib/mongory/matchers/or_matcher.rb +0 -69
  92. data/lib/mongory/matchers/present_matcher.rb +0 -55
  93. data/lib/mongory/matchers/regex_matcher.rb +0 -109
  94. data/lib/mongory/matchers/size_matcher.rb +0 -54
  95. data/lib/mongory/query_matcher.rb +0 -93
  96. data/lib/mongory/rails.rb +0 -15
  97. data/lib/mongory/utils/debugger.rb +0 -125
  98. data/lib/mongory/utils/rails_patch.rb +0 -22
@@ -1,6 +1,6 @@
1
1
  /**
2
- * @file rb_mongory_cmatcher.c
3
- * @brief Mongory::CMatcher Ruby class lifecycle, matching, and trace methods.
2
+ * @file rb_mongory_matcher.c
3
+ * @brief Mongory::Matcher Ruby class lifecycle, matching, and trace methods.
4
4
  */
5
5
  #include "mongory_ext_internal.h"
6
6
  #include <stdio.h>
@@ -31,10 +31,6 @@ static void rb_mongory_matcher_release_pools(rb_mongory_matcher_t *wrapper) {
31
31
  scratch->next_scratch = NULL;
32
32
  scratch->base.free(&scratch->base);
33
33
  }
34
- if (wrapper->trace_pool) {
35
- wrapper->trace_pool->free(wrapper->trace_pool);
36
- wrapper->trace_pool = NULL;
37
- }
38
34
  wrapper->matcher = NULL;
39
35
  wrapper->condition = NULL;
40
36
  wrapper->string_map = NULL;
@@ -81,7 +77,7 @@ static VALUE rb_mongory_matcher_alloc(VALUE klass) {
81
77
  return obj;
82
78
  }
83
79
 
84
- // Mongory::CMatcher#initialize(condition, context: ...)
80
+ // Mongory::Matcher#initialize(condition, context: ...)
85
81
  static VALUE rb_mongory_matcher_initialize(int argc, VALUE *argv, VALUE self) {
86
82
  rb_mongory_matcher_t *wrapper;
87
83
  TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
@@ -128,7 +124,7 @@ static VALUE rb_mongory_matcher_initialize_copy(VALUE self, VALUE orig) {
128
124
  static void rb_mongory_matcher_parse_argv(rb_mongory_matcher_t *self, int argc, VALUE *argv) {
129
125
  VALUE condition, kw_hash;
130
126
  if (argc == 1) {
131
- // In Ruby 3.0+, a single bare hash like `CMatcher.new(name: 'John')` is marked
127
+ // In Ruby 3.0+, a single bare hash like `Matcher.new(name: 'John')` is marked
132
128
  // as keywords, which would fail `rb_scan_args(..., "1:", ...)` with 0 positional args.
133
129
  rb_scan_args(argc, argv, "1", &condition);
134
130
  kw_hash = Qnil;
@@ -216,7 +212,6 @@ static VALUE rb_mongory_matcher_match_body(VALUE arg) {
216
212
  rb_mongory_matcher_t *self_wrapper = call->wrapper;
217
213
  mongory_matcher *matcher = self_wrapper->matcher;
218
214
  mongory_memory_pool *scratch_pool = call->scratch_pool;
219
- mongory_memory_pool *trace_pool = self_wrapper->trace_pool;
220
215
  mongory_value *data_value = rb_to_mongory_value_shallow(scratch_pool, call->data);
221
216
 
222
217
  if (rb_mongory_error_handling(scratch_pool, "Match failed")) {
@@ -224,17 +219,10 @@ static VALUE rb_mongory_matcher_match_body(VALUE arg) {
224
219
  }
225
220
 
226
221
  bool result = mongory_matcher_match(matcher, data_value);
227
-
228
- if (trace_pool) {
229
- mongory_matcher_print_trace(matcher);
230
- trace_pool->reset(trace_pool);
231
- mongory_matcher_enable_trace(matcher, trace_pool);
232
- }
233
-
234
222
  return result ? Qtrue : Qfalse;
235
223
  }
236
224
 
237
- // Mongory::CMatcher#match?(data)
225
+ // Mongory::Matcher#match?(data)
238
226
  static VALUE rb_mongory_matcher_match(VALUE self, VALUE data) {
239
227
  rb_mongory_matcher_t *self_wrapper = get_initialized_matcher(self);
240
228
  rb_mongory_scratch_lease_t lease = rb_mongory_scratch_acquire(self_wrapper);
@@ -248,28 +236,140 @@ static VALUE rb_mongory_matcher_match(VALUE self, VALUE data) {
248
236
  return result;
249
237
  }
250
238
 
251
- static VALUE rb_mongory_matcher_explain_body(VALUE arg) {
239
+ static VALUE rb_mongory_value_to_rb(mongory_value *val);
240
+
241
+ static bool rb_mongory_table_entry_to_rb_hash(char *key, mongory_value *val, void *acc) {
242
+ VALUE hash = (VALUE)acc;
243
+ VALUE rb_key = rb_utf8_str_new_cstr(key ? key : "");
244
+ VALUE rb_val = rb_mongory_value_to_rb(val);
245
+ rb_hash_aset(hash, rb_key, rb_val);
246
+ return true;
247
+ }
248
+
249
+ static VALUE rb_mongory_value_to_rb(mongory_value *val) {
250
+ if (!val) {
251
+ return Qnil;
252
+ }
253
+ if (val->origin != NULL) {
254
+ return (VALUE)val->origin;
255
+ }
256
+ switch (val->type) {
257
+ case MONGORY_TYPE_NULL:
258
+ return Qnil;
259
+ case MONGORY_TYPE_BOOL:
260
+ return val->data.b ? Qtrue : Qfalse;
261
+ case MONGORY_TYPE_INT:
262
+ return LL2NUM(val->data.i);
263
+ case MONGORY_TYPE_DOUBLE:
264
+ return DBL2NUM(val->data.d);
265
+ case MONGORY_TYPE_STRING:
266
+ return rb_utf8_str_new_cstr(val->data.s ? val->data.s : "");
267
+ case MONGORY_TYPE_ARRAY: {
268
+ mongory_array *arr = val->data.a;
269
+ size_t count = arr ? arr->count : 0;
270
+ VALUE rb_arr = rb_ary_new_capa((long)count);
271
+ for (size_t i = 0; i < count; i++) {
272
+ rb_ary_push(rb_arr, rb_mongory_value_to_rb(arr->get(arr, i)));
273
+ }
274
+ return rb_arr;
275
+ }
276
+ case MONGORY_TYPE_TABLE: {
277
+ VALUE rb_hash = rb_hash_new();
278
+ if (val->data.t && val->data.t->each) {
279
+ val->data.t->each(val->data.t, (void *)rb_hash, rb_mongory_table_entry_to_rb_hash);
280
+ }
281
+ return rb_hash;
282
+ }
283
+ default:
284
+ return Qnil;
285
+ }
286
+ }
287
+
288
+ static VALUE rb_mongory_tree_node_to_rb(mongory_value *node_val) {
289
+ if (!node_val || node_val->type != MONGORY_TYPE_TABLE || !node_val->data.t) {
290
+ return Qnil;
291
+ }
292
+ mongory_table *node = node_val->data.t;
293
+ VALUE hash = rb_hash_new();
294
+
295
+ mongory_value *name_val = node->get(node, "name");
296
+ if (name_val && name_val->type == MONGORY_TYPE_STRING) {
297
+ rb_hash_aset(hash, ID2SYM(rb_intern("name")), rb_utf8_str_new_cstr(name_val->data.s ? name_val->data.s : ""));
298
+ }
299
+
300
+ mongory_value *cond_val = node->get(node, "condition");
301
+ rb_hash_aset(hash, ID2SYM(rb_intern("condition")), rb_mongory_value_to_rb(cond_val));
302
+
303
+ mongory_value *priority_val = node->get(node, "priority");
304
+ if (priority_val && priority_val->type == MONGORY_TYPE_DOUBLE) {
305
+ rb_hash_aset(hash, ID2SYM(rb_intern("priority")), DBL2NUM(priority_val->data.d));
306
+ }
307
+
308
+ mongory_value *field_val = node->get(node, "field");
309
+ if (field_val && field_val->type == MONGORY_TYPE_STRING && field_val->data.s) {
310
+ rb_hash_aset(hash, ID2SYM(rb_intern("field")), rb_utf8_str_new_cstr(field_val->data.s));
311
+ }
312
+
313
+ mongory_value *record_val = node->get(node, "record");
314
+ if (record_val) {
315
+ rb_hash_aset(hash, ID2SYM(rb_intern("record")), rb_mongory_value_to_rb(record_val));
316
+ }
317
+
318
+ mongory_value *result_val = node->get(node, "result");
319
+ if (result_val && result_val->type == MONGORY_TYPE_STRING && result_val->data.s) {
320
+ rb_hash_aset(hash, ID2SYM(rb_intern("result")), ID2SYM(rb_intern(result_val->data.s)));
321
+ }
322
+
323
+ mongory_value *children_val = node->get(node, "children");
324
+ VALUE rb_children = rb_ary_new();
325
+ if (children_val && children_val->type == MONGORY_TYPE_ARRAY && children_val->data.a) {
326
+ mongory_array *arr = children_val->data.a;
327
+ for (size_t i = 0; i < arr->count; i++) {
328
+ rb_ary_push(rb_children, rb_mongory_tree_node_to_rb(arr->get(arr, i)));
329
+ }
330
+ }
331
+ rb_hash_aset(hash, ID2SYM(rb_intern("children")), rb_children);
332
+
333
+ return hash;
334
+ }
335
+
336
+ static VALUE rb_mongory_tree_renderer_module(void) {
337
+ VALUE m_utils = rb_const_get(rb_mongory_m_mongory, rb_intern("Utils"));
338
+ return rb_const_get(m_utils, rb_intern("TreeRenderer"));
339
+ }
340
+
341
+ static VALUE rb_mongory_matcher_explain_tree_body(VALUE arg) {
252
342
  rb_mongory_explain_call_t *call = (rb_mongory_explain_call_t *)arg;
253
- mongory_matcher_explain(call->wrapper->matcher, call->scratch_pool);
343
+ mongory_value *tree = mongory_matcher_explain_tree(call->wrapper->matcher, call->scratch_pool);
254
344
  rb_mongory_error_handling(call->scratch_pool, "Explain failed");
255
- return Qnil;
345
+ return rb_mongory_tree_node_to_rb(tree);
256
346
  }
257
347
 
258
- // Mongory::CMatcher#explain
259
- static VALUE rb_mongory_matcher_explain(VALUE self) {
348
+ // Mongory::Matcher#explain_tree
349
+ static VALUE rb_mongory_matcher_explain_tree(VALUE self) {
260
350
  rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
261
351
  rb_mongory_scratch_lease_t lease = rb_mongory_scratch_acquire(wrapper);
262
352
  rb_mongory_explain_call_t call = {
263
353
  .wrapper = wrapper,
264
354
  .scratch_pool = &lease.scratch->base,
265
355
  };
266
- VALUE result = rb_ensure(rb_mongory_matcher_explain_body, (VALUE)&call, rb_mongory_scratch_release, (VALUE)&lease);
356
+ VALUE result = rb_ensure(rb_mongory_matcher_explain_tree_body, (VALUE)&call, rb_mongory_scratch_release, (VALUE)&lease);
267
357
  RB_GC_GUARD(self);
268
358
  return result;
269
359
  }
270
360
 
271
- // Mongory::CMatcher#trace(data)
272
- static VALUE rb_mongory_matcher_trace(VALUE self, VALUE data) {
361
+ // Mongory::Matcher#explain
362
+ static VALUE rb_mongory_matcher_explain(VALUE self) {
363
+ VALUE tree = rb_mongory_matcher_explain_tree(self);
364
+ if (!NIL_P(tree)) {
365
+ rb_funcall(rb_mongory_tree_renderer_module(), rb_intern("print_explain"), 1, tree);
366
+ }
367
+ RB_GC_GUARD(tree);
368
+ return Qnil;
369
+ }
370
+
371
+ // Mongory::Matcher#trace_tree(data)
372
+ static VALUE rb_mongory_matcher_trace_tree(VALUE self, VALUE data) {
273
373
  rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
274
374
  rb_mongory_memory_pool_t *trace_pool = rb_mongory_memory_pool_new();
275
375
  trace_pool->owner = wrapper;
@@ -284,103 +384,57 @@ static VALUE rb_mongory_matcher_trace(VALUE self, VALUE data) {
284
384
  rb_raise(rb_mongory_e_type_error, "Trace failed: %s", err_msg);
285
385
  }
286
386
 
287
- bool matched = mongory_matcher_trace(wrapper->matcher, data_value);
288
- trace_pool_base->free(trace_pool_base);
289
-
290
- return matched ? Qtrue : Qfalse;
291
- }
292
-
293
- // Mongory::CMatcher#enable_trace
294
- static VALUE rb_mongory_matcher_enable_trace(VALUE self) {
295
- rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
296
- if (wrapper->trace_pool) {
297
- wrapper->trace_pool->free(wrapper->trace_pool);
298
- wrapper->trace_pool = NULL;
299
- }
300
- rb_mongory_memory_pool_t *trace_pool = rb_mongory_memory_pool_new();
301
- trace_pool->owner = wrapper;
302
- mongory_memory_pool *trace_pool_base = &trace_pool->base;
303
- mongory_matcher_enable_trace(wrapper->matcher, trace_pool_base);
304
-
387
+ mongory_value *tree = mongory_matcher_trace_tree(wrapper->matcher, data_value);
305
388
  if (trace_pool_base->error) {
306
389
  char err_msg[256];
307
390
  snprintf(err_msg, sizeof(err_msg), "%s",
308
391
  trace_pool_base->error->message ? trace_pool_base->error->message : "Unknown error");
309
392
  trace_pool_base->free(trace_pool_base);
310
- rb_raise(rb_mongory_e_type_error, "Enable trace failed: %s", err_msg);
311
- }
312
- wrapper->trace_pool = trace_pool_base;
313
-
314
- return Qnil;
315
- }
316
-
317
- // Mongory::CMatcher#disable_trace
318
- static VALUE rb_mongory_matcher_disable_trace(VALUE self) {
319
- rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
320
- mongory_memory_pool *trace_pool = wrapper->trace_pool;
321
- mongory_matcher_disable_trace(wrapper->matcher);
322
- if (trace_pool) {
323
- wrapper->trace_pool = NULL;
324
- if (trace_pool->error) {
325
- char err_msg[256];
326
- snprintf(err_msg, sizeof(err_msg), "%s",
327
- trace_pool->error->message ? trace_pool->error->message : "Unknown error");
328
- trace_pool->free(trace_pool);
329
- rb_raise(rb_mongory_e_type_error, "Disable trace failed: %s", err_msg);
330
- }
331
- trace_pool->free(trace_pool);
393
+ rb_raise(rb_mongory_e_type_error, "Trace failed: %s", err_msg);
332
394
  }
395
+ VALUE result = rb_mongory_tree_node_to_rb(tree);
396
+ trace_pool_base->free(trace_pool_base);
333
397
 
334
- return Qnil;
398
+ return result;
335
399
  }
336
400
 
337
- // Mongory::CMatcher#print_trace
338
- static VALUE rb_mongory_matcher_print_trace(VALUE self) {
339
- rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
340
- if (!wrapper->trace_pool) {
341
- return Qnil;
401
+ // Mongory::Matcher#trace(data)
402
+ static VALUE rb_mongory_matcher_trace(VALUE self, VALUE data) {
403
+ VALUE tree = rb_mongory_matcher_trace_tree(self, data);
404
+ if (NIL_P(tree)) {
405
+ return Qfalse;
342
406
  }
343
- mongory_matcher_print_trace(wrapper->matcher);
344
- rb_mongory_error_handling(wrapper->trace_pool, "Print trace failed");
345
-
346
- return Qnil;
407
+ rb_funcall(rb_mongory_tree_renderer_module(), rb_intern("print_trace"), 1, tree);
408
+ VALUE res = rb_hash_aref(tree, ID2SYM(rb_intern("result")));
409
+ RB_GC_GUARD(tree);
410
+ return res == ID2SYM(rb_intern("pass")) ? Qtrue : Qfalse;
347
411
  }
348
412
 
349
- // Mongory::CMatcher#condition
413
+ // Mongory::Matcher#condition
350
414
  static VALUE rb_mongory_matcher_condition(VALUE self) {
351
415
  rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
352
416
 
353
417
  return (VALUE)wrapper->condition->origin;
354
418
  }
355
419
 
356
- // Mongory::CMatcher#context
420
+ // Mongory::Matcher#context
357
421
  static VALUE rb_mongory_matcher_context(VALUE self) {
358
422
  rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
359
423
 
360
424
  return wrapper->ctx ? wrapper->ctx : Qnil;
361
425
  }
362
426
 
363
- // Mongory::CMatcher.trace_result_colorful=(colorful)
364
- static VALUE rb_mongory_matcher_trace_result_colorful(VALUE self, VALUE colorful) {
365
- (void)self;
366
- mongory_matcher_trace_result_colorful_set(RTEST(colorful));
367
-
368
- return Qnil;
369
- }
370
-
371
- void rb_mongory_init_cmatcher(void) {
372
- rb_mongory_c_matcher = rb_define_class_under(rb_mongory_m_mongory, "CMatcher", rb_cObject);
427
+ void rb_mongory_init_matcher(void) {
428
+ rb_mongory_c_matcher = rb_define_class_under(rb_mongory_m_mongory, "Matcher", rb_cObject);
373
429
  rb_define_alloc_func(rb_mongory_c_matcher, rb_mongory_matcher_alloc);
374
430
 
375
431
  rb_define_method(rb_mongory_c_matcher, "initialize", rb_mongory_matcher_initialize, -1);
376
432
  rb_define_method(rb_mongory_c_matcher, "initialize_copy", rb_mongory_matcher_initialize_copy, 1);
377
- rb_define_singleton_method(rb_mongory_c_matcher, "trace_result_colorful=", rb_mongory_matcher_trace_result_colorful, 1);
378
433
  rb_define_method(rb_mongory_c_matcher, "match?", rb_mongory_matcher_match, 1);
434
+ rb_define_method(rb_mongory_c_matcher, "explain_tree", rb_mongory_matcher_explain_tree, 0);
379
435
  rb_define_method(rb_mongory_c_matcher, "explain", rb_mongory_matcher_explain, 0);
380
436
  rb_define_method(rb_mongory_c_matcher, "condition", rb_mongory_matcher_condition, 0);
381
437
  rb_define_method(rb_mongory_c_matcher, "context", rb_mongory_matcher_context, 0);
438
+ rb_define_method(rb_mongory_c_matcher, "trace_tree", rb_mongory_matcher_trace_tree, 1);
382
439
  rb_define_method(rb_mongory_c_matcher, "trace", rb_mongory_matcher_trace, 1);
383
- rb_define_method(rb_mongory_c_matcher, "enable_trace", rb_mongory_matcher_enable_trace, 0);
384
- rb_define_method(rb_mongory_c_matcher, "disable_trace", rb_mongory_matcher_disable_trace, 0);
385
- rb_define_method(rb_mongory_c_matcher, "print_trace", rb_mongory_matcher_print_trace, 0);
386
440
  }
@@ -181,15 +181,61 @@ static mongory_value *rb_mongory_table_get(mongory_table *self, char *key) {
181
181
  return rb_to_mongory_value_shallow(table->base.pool, rb_value);
182
182
  }
183
183
 
184
+ typedef struct {
185
+ mongory_memory_pool *pool;
186
+ void *acc;
187
+ mongory_table_each_pair_callback_func callback;
188
+ bool completed;
189
+ } rb_table_each_ctx_t;
190
+
191
+ static int rb_mongory_table_each_cb(VALUE key, VALUE val, VALUE arg) {
192
+ rb_table_each_ctx_t *ctx = (rb_table_each_ctx_t *)arg;
193
+ const char *key_cstr;
194
+ if (RB_TYPE_P(key, T_SYMBOL)) {
195
+ key_cstr = rb_id2name(rb_sym2id(key));
196
+ } else if (RB_TYPE_P(key, T_STRING)) {
197
+ key_cstr = StringValueCStr(key);
198
+ } else {
199
+ VALUE key_str = rb_funcall(key, rb_intern("to_s"), 0);
200
+ key_cstr = StringValueCStr(key_str);
201
+ }
202
+ mongory_value *mg_val = rb_to_mongory_value_shallow(ctx->pool, val);
203
+ if (!ctx->callback((char *)key_cstr, mg_val, ctx->acc)) {
204
+ ctx->completed = false;
205
+ return ST_STOP;
206
+ }
207
+ return ST_CONTINUE;
208
+ }
209
+
210
+ static bool rb_mongory_table_each(mongory_table *self, void *acc, mongory_table_each_pair_callback_func callback) {
211
+ rb_mongory_table_t *table = (rb_mongory_table_t *)self;
212
+ rb_table_each_ctx_t ctx = {
213
+ .pool = self->pool,
214
+ .acc = acc,
215
+ .callback = callback,
216
+ .completed = true,
217
+ };
218
+ rb_hash_foreach(table->rb_hash, rb_mongory_table_each_cb, (VALUE)&ctx);
219
+ return ctx.completed;
220
+ }
221
+
184
222
  mongory_value *rb_mongory_table_wrap(mongory_memory_pool *pool, VALUE rb_hash) {
185
223
  rb_mongory_table_t *table = MG_ALLOC_PTR(pool, rb_mongory_table_t);
224
+ if (!table) {
225
+ return NULL;
226
+ }
227
+ memset(table, 0, sizeof(rb_mongory_table_t));
186
228
  rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
187
229
  table->base.pool = pool;
188
230
  table->base.get = rb_mongory_table_get;
231
+ table->base.each = rb_mongory_table_each;
189
232
  table->rb_hash = rb_hash;
190
233
  table->base.count = RHASH_SIZE(rb_hash);
191
234
  table->owner = rb_pool->owner;
192
235
  mongory_value *mg_value = mongory_value_wrap_t(pool, &table->base);
236
+ if (!mg_value) {
237
+ return NULL;
238
+ }
193
239
  mg_value->origin = (void *)rb_hash;
194
240
  mg_value->to_str = rb_mongory_value_to_cstr;
195
241
  return mg_value;
@@ -205,15 +251,36 @@ static mongory_value *rb_mongory_array_get(mongory_array *self, size_t index) {
205
251
  return rb_to_mongory_value_shallow(self->pool, rb_value);
206
252
  }
207
253
 
254
+ static bool rb_mongory_array_each(mongory_array *self, void *acc, mongory_array_callback_func callback) {
255
+ rb_mongory_array_t *array = (rb_mongory_array_t *)self;
256
+ VALUE rb_array = array->rb_array;
257
+ long len = RARRAY_LEN(rb_array);
258
+ for (long i = 0; i < len; i++) {
259
+ mongory_value *mg_val = rb_to_mongory_value_shallow(self->pool, rb_ary_entry(rb_array, i));
260
+ if (!callback(mg_val, acc)) {
261
+ return false;
262
+ }
263
+ }
264
+ return true;
265
+ }
266
+
208
267
  mongory_value *rb_mongory_array_wrap(mongory_memory_pool *pool, VALUE rb_array) {
209
268
  rb_mongory_array_t *array = MG_ALLOC_PTR(pool, rb_mongory_array_t);
269
+ if (!array) {
270
+ return NULL;
271
+ }
272
+ memset(array, 0, sizeof(rb_mongory_array_t));
210
273
  rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
211
274
  array->base.pool = pool;
212
275
  array->base.get = rb_mongory_array_get;
276
+ array->base.each = rb_mongory_array_each;
213
277
  array->rb_array = rb_array;
214
278
  array->base.count = RARRAY_LEN(rb_array);
215
279
  array->owner = rb_pool->owner;
216
280
  mongory_value *mg_value = mongory_value_wrap_a(pool, &array->base);
281
+ if (!mg_value) {
282
+ return NULL;
283
+ }
217
284
  mg_value->origin = (void *)rb_array;
218
285
  mg_value->to_str = rb_mongory_value_to_cstr;
219
286
  return mg_value;
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -31,11 +31,6 @@ Mongory.configure do |mc|
31
31
  # It only defines missing ones, and is safe to use with Mongoid or other libraries.
32
32
  mc.enable_symbol_snippets!
33
33
 
34
- # Here to use the C extension (`Mongory::CQueryBuilder`) by default for `.mongory` and `Mongory.build_query`.
35
- # If the C extension is not available on the current platform, it automatically falls back to pure Ruby.
36
- # Comment this out if you prefer using the pure Ruby implementation by default (you can still opt in per query via `.mongory.c`).
37
- mc.enable_c_extension_by_default!
38
-
39
34
  # Here to let you use `some_collection.mongory.where(...)` to build mongory query filter.
40
35
  # Or disable it, and use `Mongory.build_query(some_collection).where(...)` instead.
41
36
  mc.register(Array)
@@ -20,8 +20,7 @@
20
20
  # When this matcher is created as part of a matcher tree:
21
21
  # 1. It receives its @condition during initialization
22
22
  # 2. The @condition is validated via check_validity!
23
- # 3. The match method is called with normalized values
24
- # 4. The matcher can use normalize(record) to handle KEY_NOT_FOUND values
23
+ # 3. The match method is called with the record value from mongory-core
25
24
  #
26
25
  # This matcher typically serves as a leaf node in the matcher tree,
27
26
  # responsible for evaluating a single condition against a value.
@@ -45,9 +44,8 @@
45
44
  #
46
45
  # Implementation Notes:
47
46
  # 1. The match method should implement the specific operator logic
48
- # 2. Use normalize(subject) to handle KEY_NOT_FOUND values consistently
49
- # 3. The condition is available via @condition
50
- # 4. check_validity! should validate the format of @condition
47
+ # 2. The condition is available via @condition
48
+ # 3. check_validity! should validate the format of @condition
51
49
  #
52
50
  # Interface Design:
53
51
  # This matcher provides two levels of matching interface:
@@ -55,24 +53,15 @@
55
53
  # 1. Public Interface (match?):
56
54
  # - Provides error handling
57
55
  # - Ensures safe matching process
58
- # - Suitable for external calls
59
- # - Can be tracked by Mongory.debugger
60
- # - Used for internal calls and debugging
56
+ # - Invoked by the mongory-core custom matcher bridge
61
57
  #
62
58
  # 2. Internal Interface (match):
63
59
  # - Implements the actual matching logic
64
60
  #
65
- # Debugging Support:
66
- # The match method can be tracked by Mongory.debugger to:
67
- # - Visualize the matching process
68
- # - Diagnose matching issues
69
- # - Provide detailed debugging information
70
- #
71
61
  # @see Mongory::Matchers::AbstractMatcher
72
62
  class <%= @matcher_name %> < Mongory::Matchers::AbstractMatcher
73
63
  # Matches the subject against the condition.
74
64
  # This is the internal interface that implements the actual matching logic.
75
- # It can be tracked by Mongory.debugger for debugging purposes.
76
65
  #
77
66
  # @param subject [Object] the value to be tested
78
67
  # @return [Boolean] whether the value matches
@@ -5,7 +5,7 @@ module Mongory
5
5
  # ConditionConverter transforms a flat condition hash into nested hash form.
6
6
  # This is used internally by ValueConverter to normalize condition structures
7
7
  # like { "foo.bar" => 1, "foo.baz" => 2 } into nested Mongo-style conditions.
8
- # Used by QueryMatcher to normalize condition input for internal matching.
8
+ # Used by Matcher to normalize condition input for internal matching.
9
9
  #
10
10
  # Combines key transformation (via KeyConverter) and
11
11
  # value normalization (via ValueConverter), and merges overlapping keys.
@@ -50,13 +50,35 @@ module Mongory
50
50
  private
51
51
 
52
52
  def _deep_merge!(left, right)
53
- left.merge!(right) do |_, a, b|
54
- if a.is_a?(::Hash) && b.is_a?(::Hash)
55
- _deep_merge!(a.dup, b)
56
- else
57
- b
58
- end
53
+ and_clauses = []
54
+ right.each_pair do |key, val|
55
+ _merge_entry!(left, key, val, and_clauses)
59
56
  end
57
+ _append_and_clauses!(left, and_clauses) unless and_clauses.empty?
58
+ left
59
+ end
60
+
61
+ def _merge_entry!(left, key, val, and_clauses)
62
+ if !left.key?(key)
63
+ left[key] = val
64
+ elsif left[key].is_a?(::Hash) && val.is_a?(::Hash)
65
+ left[key] = _deep_merge!(left[key].dup, val)
66
+ else
67
+ and_clauses << Converted::Hash.new(key => left.delete(key))
68
+ and_clauses << Converted::Hash.new(key => val)
69
+ end
70
+ end
71
+
72
+ def _append_and_clauses!(left, and_clauses)
73
+ existing = left['$and']
74
+ merged = if existing.is_a?(::Array)
75
+ existing + and_clauses
76
+ elsif left.key?('$and')
77
+ [Converted::Hash.new('$and' => left.delete('$and')), *and_clauses]
78
+ else
79
+ and_clauses
80
+ end
81
+ left['$and'] = Converted::Array.new(merged)
60
82
  end
61
83
  end
62
84
 
@@ -64,6 +64,8 @@ module Mongory
64
64
  # @param value [Object] the value to assign at the deepest level
65
65
  # @return [Hash] nested hash structure
66
66
  def convert_string_key(key, value)
67
+ return { '' => value } if key.empty?
68
+
67
69
  ret = {}
68
70
  *sub_keys, last_key = key.split(/(?<!\\)\./)
69
71
  last_hash = sub_keys.reduce(ret) do |res, sub_key|
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mongory
4
+ # Core query matcher backed by the `mongory-core` C99 native extension.
5
+ #
6
+ # @example
7
+ # matcher = Mongory::Matcher.new(age: { :$gte => 18 })
8
+ # matcher.match?(age: 20) #=> true
9
+ # # or
10
+ # collection.mongory.where(age: { :$gte => 18 }).to_a
11
+ class Matcher
12
+ # @!method self.new(condition, context: Utils::Context.new)
13
+ # @param condition [Hash] the condition
14
+ # @param context [Utils::Context] optional query context
15
+ # @return [Mongory::Matcher] a new matcher
16
+ # @note This method is implemented in the C extension
17
+ #
18
+ # @!method match?(record)
19
+ # @param record [Object] the record to match against
20
+ # @return [Boolean] true if the record matches the condition, false otherwise
21
+ # @note This method is implemented in the C extension
22
+ # @!method explain_tree
23
+ # @return [Hash] structured explain tree
24
+ # @note This method is implemented in the C extension
25
+ # @!method explain
26
+ # @return [void]
27
+ # @note Prints the matcher tree structure to STDOUT
28
+ # @note This method is implemented in the C extension
29
+ # @!method trace_tree(record)
30
+ # @param record [Object] the record to trace against
31
+ # @return [Hash] structured trace tree
32
+ # @note This method is implemented in the C extension
33
+ # @!method trace(record)
34
+ # @param record [Object] the record to trace against
35
+ # @return [Boolean] true if the record matches the condition, false otherwise
36
+ # @note Prints the matching process to STDOUT
37
+ # @note This method is implemented in the C extension
38
+ # @!method condition
39
+ # @return [Hash] the normalized condition
40
+ # @note This method is implemented in the C extension
41
+ # @!method context
42
+ # @return [Utils::Context] the context
43
+ # @note This method is implemented in the C extension
44
+
45
+ # @return [Proc] a Proc that performs the matching operation
46
+ def to_proc
47
+ Proc.new { |record| match?(record) }
48
+ end
49
+ end
50
+ end