mongory 0.9.0 → 2.0.0

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 (85) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/README.md +121 -422
  4. data/SUBMODULE_INTEGRATION.md +2 -2
  5. data/docs/clang_bridge.md +28 -59
  6. data/docs/migration.md +17 -0
  7. data/docs/performance.md +14 -51
  8. data/examples/benchmark.rb +10 -10
  9. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +62 -0
  10. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +31 -0
  11. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +3 -0
  12. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +20 -0
  13. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +31 -4
  14. data/ext/mongory_ext/mongory-core/src/foundations/array.c +48 -6
  15. data/ext/mongory_ext/mongory-core/src/foundations/config.c +30 -2
  16. data/ext/mongory_ext/mongory-core/src/foundations/error.c +10 -0
  17. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +78 -19
  18. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +73 -19
  19. data/ext/mongory_ext/mongory-core/src/foundations/table.c +8 -3
  20. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +44 -55
  21. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +7 -0
  22. data/ext/mongory_ext/mongory-core/src/foundations/value.c +3 -0
  23. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +69 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +15 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +9 -4
  26. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +167 -31
  27. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +7 -1
  28. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.c → existence_matcher.c} +10 -12
  29. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.h → existence_matcher.h} +4 -4
  30. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +20 -6
  31. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.c → implicit_array_matcher.c} +57 -38
  32. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.h → implicit_array_matcher.h} +5 -5
  33. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +8 -10
  34. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +337 -96
  35. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +59 -21
  36. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +7 -1
  37. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +6 -5
  38. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -1
  39. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.c → value_matcher.c} +120 -60
  40. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.h → value_matcher.h} +13 -13
  41. data/ext/mongory_ext/mongory_ext.c +2 -2
  42. data/ext/mongory_ext/mongory_ext_internal.h +2 -2
  43. data/ext/mongory_ext/{rb_mongory_cmatcher.c → rb_mongory_matcher.c} +157 -23
  44. data/lib/generators/mongory/install/templates/initializer.rb.erb +0 -5
  45. data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
  46. data/lib/mongory/converters/converted.rb +28 -6
  47. data/lib/mongory/converters/key_converter.rb +2 -0
  48. data/lib/mongory/matcher.rb +63 -0
  49. data/lib/mongory/matchers/abstract_matcher.rb +18 -164
  50. data/lib/mongory/matchers.rb +68 -84
  51. data/lib/mongory/query_builder.rb +30 -41
  52. data/lib/mongory/utils/tree_renderer.rb +108 -0
  53. data/lib/mongory/utils.rb +2 -67
  54. data/lib/mongory/version.rb +1 -1
  55. data/lib/mongory.rb +9 -59
  56. data/script/bench.rb +54 -0
  57. metadata +13 -37
  58. data/lib/mongory/c_matcher.rb +0 -57
  59. data/lib/mongory/c_query_builder.rb +0 -44
  60. data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -124
  61. data/lib/mongory/matchers/and_matcher.rb +0 -72
  62. data/lib/mongory/matchers/array_record_matcher.rb +0 -105
  63. data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
  64. data/lib/mongory/matchers/eq_matcher.rb +0 -48
  65. data/lib/mongory/matchers/every_matcher.rb +0 -56
  66. data/lib/mongory/matchers/exists_matcher.rb +0 -53
  67. data/lib/mongory/matchers/field_matcher.rb +0 -160
  68. data/lib/mongory/matchers/gt_matcher.rb +0 -41
  69. data/lib/mongory/matchers/gte_matcher.rb +0 -41
  70. data/lib/mongory/matchers/hash_condition_matcher.rb +0 -79
  71. data/lib/mongory/matchers/in_matcher.rb +0 -84
  72. data/lib/mongory/matchers/literal_matcher.rb +0 -121
  73. data/lib/mongory/matchers/lt_matcher.rb +0 -41
  74. data/lib/mongory/matchers/lte_matcher.rb +0 -41
  75. data/lib/mongory/matchers/ne_matcher.rb +0 -39
  76. data/lib/mongory/matchers/nin_matcher.rb +0 -64
  77. data/lib/mongory/matchers/not_matcher.rb +0 -40
  78. data/lib/mongory/matchers/or_matcher.rb +0 -68
  79. data/lib/mongory/matchers/present_matcher.rb +0 -55
  80. data/lib/mongory/matchers/regex_matcher.rb +0 -109
  81. data/lib/mongory/matchers/size_matcher.rb +0 -54
  82. data/lib/mongory/query_matcher.rb +0 -93
  83. data/lib/mongory/rails.rb +0 -15
  84. data/lib/mongory/utils/debugger.rb +0 -125
  85. 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>
@@ -81,7 +81,7 @@ static VALUE rb_mongory_matcher_alloc(VALUE klass) {
81
81
  return obj;
82
82
  }
83
83
 
84
- // Mongory::CMatcher#initialize(condition, context: ...)
84
+ // Mongory::Matcher#initialize(condition, context: ...)
85
85
  static VALUE rb_mongory_matcher_initialize(int argc, VALUE *argv, VALUE self) {
86
86
  rb_mongory_matcher_t *wrapper;
87
87
  TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
@@ -128,7 +128,7 @@ static VALUE rb_mongory_matcher_initialize_copy(VALUE self, VALUE orig) {
128
128
  static void rb_mongory_matcher_parse_argv(rb_mongory_matcher_t *self, int argc, VALUE *argv) {
129
129
  VALUE condition, kw_hash;
130
130
  if (argc == 1) {
131
- // In Ruby 3.0+, a single bare hash like `CMatcher.new(name: 'John')` is marked
131
+ // In Ruby 3.0+, a single bare hash like `Matcher.new(name: 'John')` is marked
132
132
  // as keywords, which would fail `rb_scan_args(..., "1:", ...)` with 0 positional args.
133
133
  rb_scan_args(argc, argv, "1", &condition);
134
134
  kw_hash = Qnil;
@@ -234,7 +234,7 @@ static VALUE rb_mongory_matcher_match_body(VALUE arg) {
234
234
  return result ? Qtrue : Qfalse;
235
235
  }
236
236
 
237
- // Mongory::CMatcher#match?(data)
237
+ // Mongory::Matcher#match?(data)
238
238
  static VALUE rb_mongory_matcher_match(VALUE self, VALUE data) {
239
239
  rb_mongory_matcher_t *self_wrapper = get_initialized_matcher(self);
240
240
  rb_mongory_scratch_lease_t lease = rb_mongory_scratch_acquire(self_wrapper);
@@ -248,28 +248,140 @@ static VALUE rb_mongory_matcher_match(VALUE self, VALUE data) {
248
248
  return result;
249
249
  }
250
250
 
251
- static VALUE rb_mongory_matcher_explain_body(VALUE arg) {
251
+ static VALUE rb_mongory_value_to_rb(mongory_value *val);
252
+
253
+ static bool rb_mongory_table_entry_to_rb_hash(char *key, mongory_value *val, void *acc) {
254
+ VALUE hash = (VALUE)acc;
255
+ VALUE rb_key = rb_utf8_str_new_cstr(key ? key : "");
256
+ VALUE rb_val = rb_mongory_value_to_rb(val);
257
+ rb_hash_aset(hash, rb_key, rb_val);
258
+ return true;
259
+ }
260
+
261
+ static VALUE rb_mongory_value_to_rb(mongory_value *val) {
262
+ if (!val) {
263
+ return Qnil;
264
+ }
265
+ if (val->origin != NULL) {
266
+ return (VALUE)val->origin;
267
+ }
268
+ switch (val->type) {
269
+ case MONGORY_TYPE_NULL:
270
+ return Qnil;
271
+ case MONGORY_TYPE_BOOL:
272
+ return val->data.b ? Qtrue : Qfalse;
273
+ case MONGORY_TYPE_INT:
274
+ return LL2NUM(val->data.i);
275
+ case MONGORY_TYPE_DOUBLE:
276
+ return DBL2NUM(val->data.d);
277
+ case MONGORY_TYPE_STRING:
278
+ return rb_utf8_str_new_cstr(val->data.s ? val->data.s : "");
279
+ case MONGORY_TYPE_ARRAY: {
280
+ mongory_array *arr = val->data.a;
281
+ size_t count = arr ? arr->count : 0;
282
+ VALUE rb_arr = rb_ary_new_capa((long)count);
283
+ for (size_t i = 0; i < count; i++) {
284
+ rb_ary_push(rb_arr, rb_mongory_value_to_rb(arr->get(arr, i)));
285
+ }
286
+ return rb_arr;
287
+ }
288
+ case MONGORY_TYPE_TABLE: {
289
+ VALUE rb_hash = rb_hash_new();
290
+ if (val->data.t && val->data.t->each) {
291
+ val->data.t->each(val->data.t, (void *)rb_hash, rb_mongory_table_entry_to_rb_hash);
292
+ }
293
+ return rb_hash;
294
+ }
295
+ default:
296
+ return Qnil;
297
+ }
298
+ }
299
+
300
+ static VALUE rb_mongory_tree_node_to_rb(mongory_value *node_val) {
301
+ if (!node_val || node_val->type != MONGORY_TYPE_TABLE || !node_val->data.t) {
302
+ return Qnil;
303
+ }
304
+ mongory_table *node = node_val->data.t;
305
+ VALUE hash = rb_hash_new();
306
+
307
+ mongory_value *name_val = node->get(node, "name");
308
+ if (name_val && name_val->type == MONGORY_TYPE_STRING) {
309
+ rb_hash_aset(hash, ID2SYM(rb_intern("name")), rb_utf8_str_new_cstr(name_val->data.s ? name_val->data.s : ""));
310
+ }
311
+
312
+ mongory_value *cond_val = node->get(node, "condition");
313
+ rb_hash_aset(hash, ID2SYM(rb_intern("condition")), rb_mongory_value_to_rb(cond_val));
314
+
315
+ mongory_value *priority_val = node->get(node, "priority");
316
+ if (priority_val && priority_val->type == MONGORY_TYPE_DOUBLE) {
317
+ rb_hash_aset(hash, ID2SYM(rb_intern("priority")), DBL2NUM(priority_val->data.d));
318
+ }
319
+
320
+ mongory_value *field_val = node->get(node, "field");
321
+ if (field_val && field_val->type == MONGORY_TYPE_STRING && field_val->data.s) {
322
+ rb_hash_aset(hash, ID2SYM(rb_intern("field")), rb_utf8_str_new_cstr(field_val->data.s));
323
+ }
324
+
325
+ mongory_value *record_val = node->get(node, "record");
326
+ if (record_val) {
327
+ rb_hash_aset(hash, ID2SYM(rb_intern("record")), rb_mongory_value_to_rb(record_val));
328
+ }
329
+
330
+ mongory_value *result_val = node->get(node, "result");
331
+ if (result_val && result_val->type == MONGORY_TYPE_STRING && result_val->data.s) {
332
+ rb_hash_aset(hash, ID2SYM(rb_intern("result")), ID2SYM(rb_intern(result_val->data.s)));
333
+ }
334
+
335
+ mongory_value *children_val = node->get(node, "children");
336
+ VALUE rb_children = rb_ary_new();
337
+ if (children_val && children_val->type == MONGORY_TYPE_ARRAY && children_val->data.a) {
338
+ mongory_array *arr = children_val->data.a;
339
+ for (size_t i = 0; i < arr->count; i++) {
340
+ rb_ary_push(rb_children, rb_mongory_tree_node_to_rb(arr->get(arr, i)));
341
+ }
342
+ }
343
+ rb_hash_aset(hash, ID2SYM(rb_intern("children")), rb_children);
344
+
345
+ return hash;
346
+ }
347
+
348
+ static VALUE rb_mongory_tree_renderer_module(void) {
349
+ VALUE m_utils = rb_const_get(rb_mongory_m_mongory, rb_intern("Utils"));
350
+ return rb_const_get(m_utils, rb_intern("TreeRenderer"));
351
+ }
352
+
353
+ static VALUE rb_mongory_matcher_explain_tree_body(VALUE arg) {
252
354
  rb_mongory_explain_call_t *call = (rb_mongory_explain_call_t *)arg;
253
- mongory_matcher_explain(call->wrapper->matcher, call->scratch_pool);
355
+ mongory_value *tree = mongory_matcher_explain_tree(call->wrapper->matcher, call->scratch_pool);
254
356
  rb_mongory_error_handling(call->scratch_pool, "Explain failed");
255
- return Qnil;
357
+ return rb_mongory_tree_node_to_rb(tree);
256
358
  }
257
359
 
258
- // Mongory::CMatcher#explain
259
- static VALUE rb_mongory_matcher_explain(VALUE self) {
360
+ // Mongory::Matcher#explain_tree
361
+ static VALUE rb_mongory_matcher_explain_tree(VALUE self) {
260
362
  rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
261
363
  rb_mongory_scratch_lease_t lease = rb_mongory_scratch_acquire(wrapper);
262
364
  rb_mongory_explain_call_t call = {
263
365
  .wrapper = wrapper,
264
366
  .scratch_pool = &lease.scratch->base,
265
367
  };
266
- VALUE result = rb_ensure(rb_mongory_matcher_explain_body, (VALUE)&call, rb_mongory_scratch_release, (VALUE)&lease);
368
+ VALUE result = rb_ensure(rb_mongory_matcher_explain_tree_body, (VALUE)&call, rb_mongory_scratch_release, (VALUE)&lease);
267
369
  RB_GC_GUARD(self);
268
370
  return result;
269
371
  }
270
372
 
271
- // Mongory::CMatcher#trace(data)
272
- static VALUE rb_mongory_matcher_trace(VALUE self, VALUE data) {
373
+ // Mongory::Matcher#explain
374
+ static VALUE rb_mongory_matcher_explain(VALUE self) {
375
+ VALUE tree = rb_mongory_matcher_explain_tree(self);
376
+ if (!NIL_P(tree)) {
377
+ rb_funcall(rb_mongory_tree_renderer_module(), rb_intern("print_explain"), 1, tree);
378
+ }
379
+ RB_GC_GUARD(tree);
380
+ return Qnil;
381
+ }
382
+
383
+ // Mongory::Matcher#trace_tree(data)
384
+ static VALUE rb_mongory_matcher_trace_tree(VALUE self, VALUE data) {
273
385
  rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
274
386
  rb_mongory_memory_pool_t *trace_pool = rb_mongory_memory_pool_new();
275
387
  trace_pool->owner = wrapper;
@@ -284,13 +396,33 @@ static VALUE rb_mongory_matcher_trace(VALUE self, VALUE data) {
284
396
  rb_raise(rb_mongory_e_type_error, "Trace failed: %s", err_msg);
285
397
  }
286
398
 
287
- bool matched = mongory_matcher_trace(wrapper->matcher, data_value);
399
+ mongory_value *tree = mongory_matcher_trace_tree(wrapper->matcher, data_value);
400
+ if (trace_pool_base->error) {
401
+ char err_msg[256];
402
+ snprintf(err_msg, sizeof(err_msg), "%s",
403
+ trace_pool_base->error->message ? trace_pool_base->error->message : "Unknown error");
404
+ trace_pool_base->free(trace_pool_base);
405
+ rb_raise(rb_mongory_e_type_error, "Trace failed: %s", err_msg);
406
+ }
407
+ VALUE result = rb_mongory_tree_node_to_rb(tree);
288
408
  trace_pool_base->free(trace_pool_base);
289
409
 
290
- return matched ? Qtrue : Qfalse;
410
+ return result;
411
+ }
412
+
413
+ // Mongory::Matcher#trace(data)
414
+ static VALUE rb_mongory_matcher_trace(VALUE self, VALUE data) {
415
+ VALUE tree = rb_mongory_matcher_trace_tree(self, data);
416
+ if (NIL_P(tree)) {
417
+ return Qfalse;
418
+ }
419
+ rb_funcall(rb_mongory_tree_renderer_module(), rb_intern("print_trace"), 1, tree);
420
+ VALUE res = rb_hash_aref(tree, ID2SYM(rb_intern("result")));
421
+ RB_GC_GUARD(tree);
422
+ return res == ID2SYM(rb_intern("pass")) ? Qtrue : Qfalse;
291
423
  }
292
424
 
293
- // Mongory::CMatcher#enable_trace
425
+ // Mongory::Matcher#enable_trace
294
426
  static VALUE rb_mongory_matcher_enable_trace(VALUE self) {
295
427
  rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
296
428
  if (wrapper->trace_pool) {
@@ -314,7 +446,7 @@ static VALUE rb_mongory_matcher_enable_trace(VALUE self) {
314
446
  return Qnil;
315
447
  }
316
448
 
317
- // Mongory::CMatcher#disable_trace
449
+ // Mongory::Matcher#disable_trace
318
450
  static VALUE rb_mongory_matcher_disable_trace(VALUE self) {
319
451
  rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
320
452
  mongory_memory_pool *trace_pool = wrapper->trace_pool;
@@ -334,7 +466,7 @@ static VALUE rb_mongory_matcher_disable_trace(VALUE self) {
334
466
  return Qnil;
335
467
  }
336
468
 
337
- // Mongory::CMatcher#print_trace
469
+ // Mongory::Matcher#print_trace
338
470
  static VALUE rb_mongory_matcher_print_trace(VALUE self) {
339
471
  rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
340
472
  if (!wrapper->trace_pool) {
@@ -346,21 +478,21 @@ static VALUE rb_mongory_matcher_print_trace(VALUE self) {
346
478
  return Qnil;
347
479
  }
348
480
 
349
- // Mongory::CMatcher#condition
481
+ // Mongory::Matcher#condition
350
482
  static VALUE rb_mongory_matcher_condition(VALUE self) {
351
483
  rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
352
484
 
353
485
  return (VALUE)wrapper->condition->origin;
354
486
  }
355
487
 
356
- // Mongory::CMatcher#context
488
+ // Mongory::Matcher#context
357
489
  static VALUE rb_mongory_matcher_context(VALUE self) {
358
490
  rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
359
491
 
360
492
  return wrapper->ctx ? wrapper->ctx : Qnil;
361
493
  }
362
494
 
363
- // Mongory::CMatcher.trace_result_colorful=(colorful)
495
+ // Mongory::Matcher.trace_result_colorful=(colorful)
364
496
  static VALUE rb_mongory_matcher_trace_result_colorful(VALUE self, VALUE colorful) {
365
497
  (void)self;
366
498
  mongory_matcher_trace_result_colorful_set(RTEST(colorful));
@@ -368,17 +500,19 @@ static VALUE rb_mongory_matcher_trace_result_colorful(VALUE self, VALUE colorful
368
500
  return Qnil;
369
501
  }
370
502
 
371
- void rb_mongory_init_cmatcher(void) {
372
- rb_mongory_c_matcher = rb_define_class_under(rb_mongory_m_mongory, "CMatcher", rb_cObject);
503
+ void rb_mongory_init_matcher(void) {
504
+ rb_mongory_c_matcher = rb_define_class_under(rb_mongory_m_mongory, "Matcher", rb_cObject);
373
505
  rb_define_alloc_func(rb_mongory_c_matcher, rb_mongory_matcher_alloc);
374
506
 
375
507
  rb_define_method(rb_mongory_c_matcher, "initialize", rb_mongory_matcher_initialize, -1);
376
508
  rb_define_method(rb_mongory_c_matcher, "initialize_copy", rb_mongory_matcher_initialize_copy, 1);
377
509
  rb_define_singleton_method(rb_mongory_c_matcher, "trace_result_colorful=", rb_mongory_matcher_trace_result_colorful, 1);
378
510
  rb_define_method(rb_mongory_c_matcher, "match?", rb_mongory_matcher_match, 1);
511
+ rb_define_method(rb_mongory_c_matcher, "explain_tree", rb_mongory_matcher_explain_tree, 0);
379
512
  rb_define_method(rb_mongory_c_matcher, "explain", rb_mongory_matcher_explain, 0);
380
513
  rb_define_method(rb_mongory_c_matcher, "condition", rb_mongory_matcher_condition, 0);
381
514
  rb_define_method(rb_mongory_c_matcher, "context", rb_mongory_matcher_context, 0);
515
+ rb_define_method(rb_mongory_c_matcher, "trace_tree", rb_mongory_matcher_trace_tree, 1);
382
516
  rb_define_method(rb_mongory_c_matcher, "trace", rb_mongory_matcher_trace, 1);
383
517
  rb_define_method(rb_mongory_c_matcher, "enable_trace", rb_mongory_matcher_enable_trace, 0);
384
518
  rb_define_method(rb_mongory_c_matcher, "disable_trace", rb_mongory_matcher_disable_trace, 0);
@@ -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
@@ -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,63 @@
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
+ # @!method self.trace_result_colorful=(colorful)
18
+ # @param colorful [Boolean] whether to enable colorful trace result
19
+ # @return [void]
20
+ # @note This method is implemented in the C extension
21
+ #
22
+ # @!method match?(record)
23
+ # @param record [Object] the record to match against
24
+ # @return [Boolean] true if the record matches the condition, false otherwise
25
+ # @note This method is implemented in the C extension
26
+ # @!method explain_tree
27
+ # @return [Hash] structured explain tree
28
+ # @note This method is implemented in the C extension
29
+ # @!method explain
30
+ # @return [void]
31
+ # @note Prints the matcher tree structure to STDOUT
32
+ # @note This method is implemented in the C extension
33
+ # @!method trace_tree(record)
34
+ # @param record [Object] the record to trace against
35
+ # @return [Hash] structured trace tree
36
+ # @note This method is implemented in the C extension
37
+ # @!method trace(record)
38
+ # @param record [Object] the record to trace against
39
+ # @return [Boolean] true if the record matches the condition, false otherwise
40
+ # @note Prints the matching process to STDOUT
41
+ # @note This method is implemented in the C extension
42
+ # @!method enable_trace
43
+ # @return [void]
44
+ # @note This method is implemented in the C extension
45
+ # @!method disable_trace
46
+ # @return [void]
47
+ # @note This method is implemented in the C extension
48
+ # @!method print_trace
49
+ # @return [void]
50
+ # @note This method is implemented in the C extension
51
+ # @!method condition
52
+ # @return [Hash] the normalized condition
53
+ # @note This method is implemented in the C extension
54
+ # @!method context
55
+ # @return [Utils::Context] the context
56
+ # @note This method is implemented in the C extension
57
+
58
+ # @return [Proc] a Proc that performs the matching operation
59
+ def to_proc
60
+ Proc.new { |record| match?(record) }
61
+ end
62
+ end
63
+ end