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
@@ -2,9 +2,15 @@
2
2
  #define MONGORY_MATCHER_EXPLAINABLE_H
3
3
 
4
4
  #include "mongory-core/matchers/matcher.h"
5
+ #include "../foundations/string_buffer.h"
5
6
  #include "matcher_traversable.h"
6
7
  #include <mongory-core.h>
7
8
 
9
+ typedef struct mongory_explain_acc {
10
+ char *prefix;
11
+ mongory_string_buffer *buf;
12
+ } mongory_explain_acc;
13
+
8
14
 
9
15
  /**
10
16
  * @brief Explains a matcher.
@@ -18,6 +24,6 @@ bool mongory_matcher_composite_explain(struct mongory_matcher *matcher, mongory_
18
24
 
19
25
  bool mongory_matcher_field_explain(struct mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
20
26
 
21
- bool mongory_matcher_literal_explain(struct mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
27
+ bool mongory_matcher_value_explain(struct mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
22
28
 
23
29
  #endif /* MONGORY_MATCHER_EXPLAINABLE_H */
@@ -5,7 +5,7 @@
5
5
  #include "../foundations/memory_pool_private.h"
6
6
  #include "base_matcher.h"
7
7
  #include "composite_matcher.h"
8
- #include "literal_matcher.h"
8
+ #include "value_matcher.h"
9
9
 
10
10
  bool mongory_matcher_leaf_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
11
11
  bool continue_traverse = ctx->callback(matcher, ctx);
@@ -40,12 +40,12 @@ bool mongory_matcher_composite_traverse(mongory_matcher *matcher, mongory_matche
40
40
  return true;
41
41
  }
42
42
 
43
- bool mongory_matcher_literal_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
43
+ bool mongory_matcher_value_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
44
44
  void *prev_acc = ctx->acc;
45
45
  if (!mongory_matcher_leaf_traverse(matcher, ctx))
46
46
  return false;
47
- mongory_literal_matcher *literal = (mongory_literal_matcher *)matcher;
48
- mongory_matcher *next_matcher = literal->array_record_matcher ? literal->array_record_matcher : literal->delegate_matcher;
47
+ mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
48
+ mongory_matcher *next_matcher = literal->implicit_array_matcher ? literal->implicit_array_matcher : literal->delegate_matcher;
49
49
  mongory_matcher_traverse_context child_ctx = {
50
50
  .pool = ctx->pool,
51
51
  .level = ctx->level + 1,
@@ -18,6 +18,6 @@ bool mongory_matcher_leaf_traverse(mongory_matcher *matcher, mongory_matcher_tra
18
18
 
19
19
  bool mongory_matcher_composite_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
20
20
 
21
- bool mongory_matcher_literal_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
21
+ bool mongory_matcher_value_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
22
22
 
23
23
  #endif /* MONGORY_MATCHER_TRAVERSABLE_H */
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @file literal_matcher.c
2
+ * @file value_matcher.c
3
3
  * @brief Implements literal matchers, field matchers, $not, and $size.
4
4
  * This is an internal implementation file for the matcher module.
5
5
  *
@@ -10,16 +10,16 @@
10
10
  * - $not inverts the result of a condition.
11
11
  * - $size checks the number of elements in an array against a condition.
12
12
  */
13
- #include "literal_matcher.h"
13
+ #include "value_matcher.h"
14
14
  #include "../foundations/config_private.h" // For mongory_internal_value_converter
15
15
  #include "../foundations/memory_pool_private.h" // For atomic & lock helpers
16
16
  #include "../foundations/utils.h" // For mongory_try_parse_int, mongory_string_cpy
17
17
  #include "../foundations/string_buffer.h" // For mongory_string_buffer_appendf
18
- #include "array_record_matcher.h" // For handling array-specific matching logic
18
+ #include "implicit_array_matcher.h" // For handling array-specific matching logic
19
19
  #include "base_matcher.h" // For mongory_matcher_base_new
20
20
  #include "compare_matcher.h" // For mongory_matcher_equal_new
21
- #include "composite_matcher.h" // For mongory_matcher_composite_new, mongory_matcher_table_cond_new
22
- #include "existance_matcher.h" // For mongory_matcher_exists_new (used by null_new)
21
+ #include "composite_matcher.h" // For mongory_matcher_composite_new, mongory_matcher_condition_new
22
+ #include "existence_matcher.h" // For mongory_matcher_exists_new (used by null_new)
23
23
  #include "matcher_explainable.h"
24
24
  #include "matcher_traversable.h"
25
25
  #include "mongory-core/foundations/array.h" // For mongory_array access
@@ -34,13 +34,13 @@
34
34
  *
35
35
  * This function is used by field matchers, $not, and $size.
36
36
  * It checks the type of the input `value`.
37
- * - If `value` is an array, it may delegate to an `array_record_matcher`
37
+ * - If `value` is an array, it may delegate to an `implicit_array_matcher`
38
38
  * (via `composite->right`, if initialized). This path seems intended for
39
39
  * more complex array matching scenarios where the condition itself might
40
40
  * describe how array elements should be matched.
41
41
  * - Otherwise (if `value` is not an array, or if array-specific handling is
42
42
  * not triggered), it uses the `composite->left` matcher. `composite->left`
43
- * is typically set up by `mongory_matcher_literal_delegate` to handle the
43
+ * is typically set up by `mongory_matcher_value_delegate` to handle the
44
44
  * specific type of the literal condition (e.g., equality for simple values,
45
45
  * regex matcher for regex conditions, table condition matcher for table
46
46
  * conditions).
@@ -73,21 +73,21 @@ static inline void mongory_propagate_build_error(mongory_memory_pool *build_pool
73
73
  }
74
74
  }
75
75
 
76
- static inline bool mongory_matcher_literal_match(mongory_matcher *matcher, mongory_value *value) {
77
- mongory_literal_matcher *literal = (mongory_literal_matcher *)matcher;
76
+ static inline bool mongory_matcher_value_match(mongory_matcher *matcher, mongory_value *value) {
77
+ mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
78
78
  if (!literal || !literal->base.pool)
79
79
  return false; // Basic safety check
80
80
 
81
81
  if (value != NULL && value->type == MONGORY_TYPE_ARRAY) {
82
82
  // Double-checked locking: lazy build runs only on the first array value seen,
83
83
  // keeping non-array matching and subsequent array matches lock-free.
84
- mongory_matcher *arm = mongory_atomic_load_ptr_acquire(&literal->array_record_matcher);
84
+ mongory_matcher *arm = mongory_atomic_load_ptr_acquire(&literal->implicit_array_matcher);
85
85
  if (arm == NULL) {
86
86
  mongory_memory_pool *build_pool = literal->base.pool;
87
87
  mongory_memory_pool_lock(build_pool);
88
- arm = mongory_atomic_load_ptr_relaxed(&literal->array_record_matcher);
88
+ arm = mongory_atomic_load_ptr_relaxed(&literal->implicit_array_matcher);
89
89
  if (arm == NULL) {
90
- arm = mongory_matcher_array_record_new(build_pool, literal->base.condition, literal->base.extern_ctx);
90
+ arm = mongory_matcher_implicit_array_new(build_pool, literal->base.condition, literal->base.extern_ctx);
91
91
  if (arm && literal->base.trace_stack) {
92
92
  mongory_matcher_enable_trace_at(arm, literal->base.trace_stack, literal->base.trace_level + 1);
93
93
  }
@@ -102,7 +102,7 @@ static inline bool mongory_matcher_literal_match(mongory_matcher *matcher, mongo
102
102
  }
103
103
  }
104
104
  }
105
- mongory_atomic_store_ptr_release(&literal->array_record_matcher, arm);
105
+ mongory_atomic_store_ptr_release(&literal->implicit_array_matcher, arm);
106
106
  } else {
107
107
  mongory_propagate_build_error(build_pool, value);
108
108
  }
@@ -135,6 +135,9 @@ static inline mongory_matcher *mongory_matcher_null_new(mongory_memory_pool *poo
135
135
  MG_TABLE_WRAP(pool, 1, "$eq", mongory_value_wrap_n(pool, NULL)),
136
136
  MG_TABLE_WRAP(pool, 1, "$exists", mongory_value_wrap_b(pool, false))
137
137
  );
138
+ if (!new_or_condition || pool->error != NULL) {
139
+ return NULL;
140
+ }
138
141
  return mongory_matcher_or_new(pool, new_or_condition, extern_ctx);
139
142
  }
140
143
 
@@ -142,7 +145,7 @@ static inline mongory_matcher *mongory_matcher_null_new(mongory_memory_pool *poo
142
145
  * @brief Delegates creation of a simple literal matcher based on the
143
146
  * `condition`'s type.
144
147
  *
145
- * - Table condition: creates a `mongory_matcher_table_cond_new`.
148
+ * - Table condition: creates a `mongory_matcher_condition_new`.
146
149
  * - Regex condition: creates a `mongory_matcher_regex_new`.
147
150
  * - Null condition: creates a specialized `mongory_matcher_null_new`.
148
151
  * - Other types: creates an equality matcher (`mongory_matcher_equal_new`).
@@ -151,13 +154,13 @@ static inline mongory_matcher *mongory_matcher_null_new(mongory_memory_pool *poo
151
154
  * @param condition The literal value or condition table.
152
155
  * @return The appropriate simple matcher for the condition, or NULL on failure.
153
156
  */
154
- static inline mongory_matcher *mongory_matcher_literal_delegate(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
157
+ static inline mongory_matcher *mongory_matcher_value_delegate(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
155
158
  if (!condition)
156
159
  return mongory_matcher_equal_new(pool, NULL, extern_ctx); // Or specific null matcher
157
160
 
158
161
  switch (condition->type) {
159
162
  case MONGORY_TYPE_TABLE:
160
- return mongory_matcher_table_cond_new(pool, condition, extern_ctx);
163
+ return mongory_matcher_condition_new(pool, condition, extern_ctx);
161
164
  case MONGORY_TYPE_REGEX:
162
165
  return mongory_matcher_regex_new(pool, condition, extern_ctx);
163
166
  case MONGORY_TYPE_NULL:
@@ -174,7 +177,7 @@ static inline mongory_matcher *mongory_matcher_literal_delegate(mongory_memory_p
174
177
  *
175
178
  * Extracts the value of `field_matcher->field` from the input `value`
176
179
  * (table or array). Then, applies the literal matching logic
177
- * (`mongory_matcher_literal_match`) using the sub-matcher stored in
180
+ * (`mongory_matcher_value_match`) using the sub-matcher stored in
178
181
  * `composite.left` (which was set up by `literal_delegate` based on the
179
182
  * original condition for this field).
180
183
  *
@@ -200,16 +203,20 @@ static inline bool mongory_matcher_field_match(mongory_matcher *matcher, mongory
200
203
  } else if (value->type == MONGORY_TYPE_ARRAY) {
201
204
  if (value->data.a) {
202
205
  int index;
203
- if (!mongory_try_parse_int(field_key, &index))
204
- return false; // Field key is not a valid integer index for an array.
205
- if (index < 0) { // Handle negative indexing (from end of array)
206
- if ((size_t)(-index) > value->data.a->count)
207
- return false; // Out of bounds
208
- index = value->data.a->count + index;
206
+ if (mongory_try_parse_int(field_key, &index)) {
207
+ if (index < 0) { // Handle negative indexing (from end of array)
208
+ if ((size_t)(-index) <= value->data.a->count) {
209
+ index = (int)value->data.a->count + index;
210
+ if ((size_t)index < value->data.a->count) {
211
+ field_value = value->data.a->get(value->data.a, (size_t)index);
212
+ }
213
+ }
214
+ } else if ((size_t)index < value->data.a->count) {
215
+ field_value = value->data.a->get(value->data.a, (size_t)index);
216
+ }
209
217
  }
210
- if ((size_t)index >= value->data.a->count)
211
- return false; // Out of bounds
212
- field_value = value->data.a->get(value->data.a, (size_t)index);
218
+ // Out-of-bounds or non-integer index leaves field_value = NULL (MISSING)
219
+ // so child matchers like $exists: false or $nin can evaluate MISSING.
213
220
  }
214
221
  } else {
215
222
  return false; // Can only extract fields from tables or arrays.
@@ -231,11 +238,17 @@ static inline bool mongory_matcher_field_match(mongory_matcher *matcher, mongory
231
238
  }
232
239
  // Now, use the literal_match logic (which uses composite.left primarily)
233
240
  // to match the extracted field_value.
234
- return mongory_matcher_literal_match(matcher, field_value);
241
+ return mongory_matcher_value_match(matcher, field_value);
235
242
  }
236
243
 
237
244
  mongory_matcher *mongory_matcher_field_new(mongory_memory_pool *pool, char *field_name,
238
245
  mongory_value *condition_for_field, void *extern_ctx) {
246
+ if (!pool || !pool->alloc) {
247
+ return NULL;
248
+ }
249
+ if (!mongory_compile_budget_count_node(pool)) {
250
+ return NULL;
251
+ }
239
252
  mongory_field_matcher *field_m = MG_ALLOC_PTR(pool, mongory_field_matcher);
240
253
  if (field_m == NULL) {
241
254
  pool->error = &MONGORY_ALLOC_ERROR;
@@ -243,27 +256,26 @@ mongory_matcher *mongory_matcher_field_new(mongory_memory_pool *pool, char *fiel
243
256
  }
244
257
  field_m->field = mongory_string_cpy(pool, field_name);
245
258
  if (field_m->field == NULL) {
246
- // pool->free(field_m) is not standard; pool manages its own memory.
247
- // This indicates an error in string copy, likely pool allocation.
248
259
  return NULL;
249
260
  }
250
261
 
251
262
  // Initialize the base composite matcher part
252
263
  field_m->literal.base.pool = pool;
253
- field_m->literal.base.name = NULL; // Can be set if needed, e.g. to field_name
264
+ field_m->literal.base.name = mongory_string_cpy(pool, "Field");
265
+ if (field_m->literal.base.name == NULL) {
266
+ return NULL;
267
+ }
254
268
  field_m->literal.base.match = mongory_matcher_field_match;
255
269
  field_m->literal.base.original_match = mongory_matcher_field_match;
256
270
  field_m->literal.base.sub_count = 1;
257
271
  field_m->literal.base.condition = condition_for_field; // Original condition for the field
258
- field_m->literal.base.name = mongory_string_cpy(pool, "Field");
259
272
  field_m->literal.base.explain = mongory_matcher_field_explain;
260
- field_m->literal.base.traverse = mongory_matcher_literal_traverse;
273
+ field_m->literal.base.traverse = mongory_matcher_value_traverse;
261
274
  field_m->literal.base.extern_ctx = extern_ctx;
262
275
  // The 'left' child of the composite is the actual matcher for the field's value,
263
276
  // determined by the type of 'condition_for_field'.
264
- field_m->literal.delegate_matcher = mongory_matcher_literal_delegate(pool, condition_for_field, extern_ctx);
265
- field_m->literal.array_record_matcher = NULL; // Not typically used by field_match directly,
266
- // but literal_match might use it for arrays.
277
+ field_m->literal.delegate_matcher = mongory_matcher_value_delegate(pool, condition_for_field, extern_ctx);
278
+ field_m->literal.implicit_array_matcher = NULL;
267
279
 
268
280
  if (field_m->literal.delegate_matcher == NULL) {
269
281
  // Failed to create the delegate matcher for the condition.
@@ -278,42 +290,59 @@ mongory_matcher *mongory_matcher_field_new(mongory_memory_pool *pool, char *fiel
278
290
 
279
291
  /**
280
292
  * @brief Match function for a NOT matcher.
281
- * Negates the result of `mongory_matcher_literal_match`.
293
+ * Negates the result of `mongory_matcher_value_match`.
282
294
  * @param matcher The $not matcher.
283
295
  * @param value The value to evaluate.
284
296
  * @return True if the literal match is false, false if it's true.
285
297
  */
286
298
  static inline bool mongory_matcher_not_match(mongory_matcher *matcher, mongory_value *value) {
287
299
  // literal_match uses composite.left (and sometimes .right for arrays)
288
- return !mongory_matcher_literal_match(matcher, value);
300
+ return !mongory_matcher_value_match(matcher, value);
289
301
  }
290
302
 
291
303
  mongory_matcher *mongory_matcher_not_new(mongory_memory_pool *pool, mongory_value *condition_to_negate, void *extern_ctx) {
292
- mongory_literal_matcher *literal = MG_ALLOC_PTR(pool, mongory_literal_matcher);
293
- if (!literal)
304
+ if (!pool || !pool->alloc) {
305
+ return NULL;
306
+ }
307
+ if (!mongory_compile_budget_enter_depth(pool)) {
308
+ return NULL;
309
+ }
310
+ if (!mongory_compile_budget_count_node(pool)) {
311
+ mongory_compile_budget_leave_depth();
312
+ return NULL;
313
+ }
314
+ mongory_value_matcher *literal = MG_ALLOC_PTR(pool, mongory_value_matcher);
315
+ if (!literal) {
316
+ pool->error = &MONGORY_ALLOC_ERROR;
317
+ mongory_compile_budget_leave_depth();
294
318
  return NULL;
319
+ }
295
320
 
296
321
  // The 'left' child is the matcher for the condition being negated.
297
- literal->delegate_matcher = mongory_matcher_literal_delegate(pool, condition_to_negate, extern_ctx);
322
+ literal->delegate_matcher = mongory_matcher_value_delegate(pool, condition_to_negate, extern_ctx);
298
323
  if (!literal->delegate_matcher) {
324
+ mongory_compile_budget_leave_depth();
299
325
  return NULL; // Failed to create delegate for the condition.
300
326
  }
301
- literal->array_record_matcher = NULL;
302
- // composite->right remains NULL for $not, as literal_match's array path
303
- // via composite->right will use condition_to_negate if right is NULL.
327
+ literal->implicit_array_matcher = NULL;
304
328
 
305
329
  literal->base.pool = pool;
306
330
  literal->base.condition = condition_to_negate;
307
331
  literal->base.match = mongory_matcher_not_match;
308
332
  literal->base.original_match = mongory_matcher_not_match;
309
333
  literal->base.name = mongory_string_cpy(pool, "Not");
310
- literal->base.explain = mongory_matcher_literal_explain;
311
- literal->base.traverse = mongory_matcher_literal_traverse;
334
+ if (!literal->base.name) {
335
+ mongory_compile_budget_leave_depth();
336
+ return NULL;
337
+ }
338
+ literal->base.explain = mongory_matcher_value_explain;
339
+ literal->base.traverse = mongory_matcher_value_traverse;
312
340
  literal->base.sub_count = 1;
313
341
  literal->base.extern_ctx = extern_ctx;
314
342
  literal->base.priority = 1.0 + literal->delegate_matcher->priority;
315
343
  literal->base.parent = NULL;
316
344
  literal->delegate_matcher->parent = (mongory_matcher *)literal;
345
+ mongory_compile_budget_leave_depth();
317
346
  return (mongory_matcher *)literal;
318
347
  }
319
348
 
@@ -330,43 +359,58 @@ static inline bool mongory_matcher_size_match(mongory_matcher *matcher, mongory_
330
359
  return false; // $size only applies to valid arrays.
331
360
  }
332
361
  mongory_array *array = value->data.a;
333
- // Wrap the array's count as a mongory_value (integer) to be matched.
334
- // The pool for this temporary size value should be from the input value or matcher.
335
-
336
- // Use literal_match with the matcher's original condition against the size_value.
337
- // The $size matcher's `composite.left` was set up by literal_delegate
338
- // based on the condition provided to $size (e.g., if {$size: 5}, left is an
339
- // equality matcher for 5).
340
- return mongory_matcher_literal_match(matcher, mongory_value_wrap_i(value->pool, (int)array->count));
362
+ mongory_value *size_val = mongory_value_wrap_i(value->pool, (int64_t)array->count);
363
+ if (!size_val) {
364
+ return false;
365
+ }
366
+ return mongory_matcher_value_match(matcher, size_val);
341
367
  }
342
368
 
343
369
  mongory_matcher *mongory_matcher_size_new(mongory_memory_pool *pool, mongory_value *size_condition, void *extern_ctx) {
344
- mongory_literal_matcher *literal = MG_ALLOC_PTR(pool, mongory_literal_matcher);
345
- if (!literal)
370
+ if (!pool || !pool->alloc) {
371
+ return NULL;
372
+ }
373
+ if (!mongory_compile_budget_enter_depth(pool)) {
374
+ return NULL;
375
+ }
376
+ if (!mongory_compile_budget_count_node(pool)) {
377
+ mongory_compile_budget_leave_depth();
346
378
  return NULL;
379
+ }
380
+ mongory_value_matcher *literal = MG_ALLOC_PTR(pool, mongory_value_matcher);
381
+ if (!literal) {
382
+ pool->error = &MONGORY_ALLOC_ERROR;
383
+ mongory_compile_budget_leave_depth();
384
+ return NULL;
385
+ }
347
386
 
348
387
  // The 'left' child is the matcher for the size_condition itself.
349
388
  // E.g., if {$size: {$gt: 5}}, size_condition is {$gt: 5}, and
350
389
  // composite.left becomes a "greater than 5" matcher.
351
- literal->delegate_matcher = mongory_matcher_literal_delegate(pool, size_condition, extern_ctx);
390
+ literal->delegate_matcher = mongory_matcher_value_delegate(pool, size_condition, extern_ctx);
352
391
  if (!literal->delegate_matcher) {
392
+ mongory_compile_budget_leave_depth();
353
393
  return NULL;
354
394
  }
355
- literal->array_record_matcher = NULL;
356
- // composite->right typically NULL for $size, array path of literal_match not primary.
395
+ literal->implicit_array_matcher = NULL;
357
396
 
358
397
  literal->base.pool = pool;
359
398
  literal->base.condition = size_condition;
360
399
  literal->base.match = mongory_matcher_size_match;
361
400
  literal->base.original_match = mongory_matcher_size_match;
362
401
  literal->base.name = mongory_string_cpy(pool, "Size");
363
- literal->base.explain = mongory_matcher_literal_explain;
364
- literal->base.traverse = mongory_matcher_literal_traverse;
402
+ if (!literal->base.name) {
403
+ mongory_compile_budget_leave_depth();
404
+ return NULL;
405
+ }
406
+ literal->base.explain = mongory_matcher_value_explain;
407
+ literal->base.traverse = mongory_matcher_value_traverse;
365
408
  literal->base.sub_count = 1;
366
409
  literal->base.extern_ctx = extern_ctx;
367
410
  literal->base.priority = 1.0 + literal->delegate_matcher->priority;
368
411
  literal->base.parent = NULL;
369
412
  literal->delegate_matcher->parent = (mongory_matcher *)literal;
413
+ mongory_compile_budget_leave_depth();
370
414
  return (mongory_matcher *)literal;
371
415
  }
372
416
 
@@ -1,8 +1,8 @@
1
- #ifndef MONGORY_MATCHER_LITERAL_H
2
- #define MONGORY_MATCHER_LITERAL_H
1
+ #ifndef MONGORY_MATCHER_VALUE_H
2
+ #define MONGORY_MATCHER_VALUE_H
3
3
 
4
4
  /**
5
- * @file literal_matcher.h
5
+ * @file value_matcher.h
6
6
  * @brief Defines constructors for literal-based matchers, field matchers,
7
7
  * $not, and $size. This is an internal header for the matcher module.
8
8
  *
@@ -18,11 +18,11 @@
18
18
  #include "matcher_explainable.h"
19
19
  #include "composite_matcher.h"
20
20
 
21
- typedef struct mongory_literal_matcher {
21
+ typedef struct mongory_value_matcher {
22
22
  mongory_matcher base;
23
23
  mongory_matcher *delegate_matcher;
24
- mongory_matcher *array_record_matcher;
25
- } mongory_literal_matcher;
24
+ mongory_matcher *implicit_array_matcher;
25
+ } mongory_value_matcher;
26
26
 
27
27
  /**
28
28
  * @struct mongory_field_matcher
@@ -30,7 +30,7 @@ typedef struct mongory_literal_matcher {
30
30
  * Stores the field name/index.
31
31
  */
32
32
  typedef struct mongory_field_matcher {
33
- mongory_literal_matcher literal; /**< Base composite matcher structure. */
33
+ mongory_value_matcher literal; /**< Base composite matcher structure. */
34
34
  char *field; /**< Name/index of the field to match. Copied string. */
35
35
  } mongory_field_matcher;
36
36
  /**
@@ -46,7 +46,7 @@ typedef struct mongory_field_matcher {
46
46
  * representation of an index (if input is an array). A copy of this string is
47
47
  * made.
48
48
  * @param condition The `mongory_value` condition to apply to the field's value.
49
- * This condition is processed by `mongory_matcher_literal_delegate` to determine
49
+ * This condition is processed by `mongory_matcher_value_delegate` to determine
50
50
  * the actual sub-matcher (e.g., equality, regex, nested table condition).
51
51
  * @return A new field matcher, or NULL on failure.
52
52
  */
@@ -57,7 +57,7 @@ mongory_matcher *mongory_matcher_field_new(mongory_memory_pool *pool, char *fiel
57
57
  *
58
58
  * This matcher negates the result of a sub-matcher derived from `condition`.
59
59
  * The sub-matcher is typically determined by
60
- * `mongory_matcher_literal_delegate`.
60
+ * `mongory_matcher_value_delegate`.
61
61
  *
62
62
  * @param pool Memory pool for allocation.
63
63
  * @param condition The `mongory_value` condition whose result will be negated.
@@ -70,7 +70,7 @@ mongory_matcher *mongory_matcher_not_new(mongory_memory_pool *pool, mongory_valu
70
70
  *
71
71
  * This matcher checks if the size (count of elements) of an input array
72
72
  * matches the given `condition`. The `condition` itself is processed by
73
- * `mongory_matcher_literal_delegate` (e.g., it could be a number for exact
73
+ * `mongory_matcher_value_delegate` (e.g., it could be a number for exact
74
74
  * size, or a condition table like {$gt: 5}).
75
75
  *
76
76
  * @param pool Memory pool for allocation.
@@ -83,7 +83,7 @@ mongory_matcher *mongory_matcher_size_new(mongory_memory_pool *pool, mongory_val
83
83
  * @brief Creates a "literal" matcher (deprecated or internal use).
84
84
  *
85
85
  * This function seems to be a more generic entry point that delegates to
86
- * `mongory_matcher_literal_delegate` and potentially `array_record_matcher`.
86
+ * `mongory_matcher_value_delegate` and potentially `implicit_array_matcher`.
87
87
  * Its direct public use might be limited compared to `field_new` or specific
88
88
  * operator matchers. It appears to be part of the internal logic for how field
89
89
  * values are matched.
@@ -92,6 +92,6 @@ mongory_matcher *mongory_matcher_size_new(mongory_memory_pool *pool, mongory_val
92
92
  * @param condition The literal `mongory_value` or condition table.
93
93
  * @return A new literal matcher, or NULL on failure.
94
94
  */
95
- mongory_matcher *mongory_matcher_literal_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
95
+ mongory_matcher *mongory_matcher_value_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
96
96
 
97
- #endif /* MONGORY_MATCHER_LITERAL_H */
97
+ #endif /* MONGORY_MATCHER_VALUE_H */
@@ -60,7 +60,7 @@ void Init_mongory_ext(void) {
60
60
  rb_mongory_e_error = rb_define_class_under(rb_mongory_m_mongory, "Error", rb_eStandardError);
61
61
  rb_mongory_e_type_error = rb_define_class_under(rb_mongory_m_mongory, "TypeError", rb_mongory_e_error);
62
62
 
63
- // Register CMatcher class, methods, and core bridges
64
- rb_mongory_init_cmatcher();
63
+ // Register Matcher class, methods, and core bridges
64
+ rb_mongory_init_matcher();
65
65
  rb_mongory_register_adapters();
66
66
  }
@@ -28,7 +28,6 @@ typedef struct rb_mongory_matcher_t {
28
28
  mongory_value *condition;
29
29
  mongory_memory_pool *pool;
30
30
  rb_mongory_memory_pool_t *scratch_free_list;
31
- mongory_memory_pool *trace_pool;
32
31
  mongory_table *string_map;
33
32
  mongory_table *symbol_map;
34
33
  mongory_array *mark_list;
@@ -57,8 +56,8 @@ typedef struct rb_mongory_array_t {
57
56
  rb_mongory_memory_pool_t *rb_mongory_memory_pool_new(void);
58
57
  bool rb_mongory_error_handling(mongory_memory_pool *pool, const char *error_message);
59
58
 
60
- // CMatcher registration (rb_mongory_cmatcher.c)
61
- void rb_mongory_init_cmatcher(void);
59
+ // Matcher registration (rb_mongory_matcher.c)
60
+ void rb_mongory_init_matcher(void);
62
61
 
63
62
  // Value conversion, lazy wrappers, and key caches (rb_mongory_value.c)
64
63
  mongory_value *rb_to_mongory_value_deep(mongory_memory_pool *pool, VALUE rb_value);