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
@@ -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 */
@@ -2,9 +2,10 @@
2
2
  #define MONGORY_MATCHER_TRAVERSABLE_C
3
3
 
4
4
  #include "matcher_traversable.h"
5
+ #include "../foundations/memory_pool_private.h"
5
6
  #include "base_matcher.h"
6
7
  #include "composite_matcher.h"
7
- #include "literal_matcher.h"
8
+ #include "value_matcher.h"
8
9
 
9
10
  bool mongory_matcher_leaf_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
10
11
  bool continue_traverse = ctx->callback(matcher, ctx);
@@ -19,7 +20,7 @@ bool mongory_matcher_composite_traverse(mongory_matcher *matcher, mongory_matche
19
20
  if (!mongory_matcher_leaf_traverse(matcher, ctx))
20
21
  return false;
21
22
  mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
22
- mongory_array *children = composite->children;
23
+ mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
23
24
  int total = (int)children->count;
24
25
  mongory_matcher_traverse_context child_ctx = {
25
26
  .pool = ctx->pool,
@@ -39,12 +40,12 @@ bool mongory_matcher_composite_traverse(mongory_matcher *matcher, mongory_matche
39
40
  return true;
40
41
  }
41
42
 
42
- 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) {
43
44
  void *prev_acc = ctx->acc;
44
45
  if (!mongory_matcher_leaf_traverse(matcher, ctx))
45
46
  return false;
46
- mongory_literal_matcher *literal = (mongory_literal_matcher *)matcher;
47
- 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;
48
49
  mongory_matcher_traverse_context child_ctx = {
49
50
  .pool = ctx->pool,
50
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,26 +73,36 @@ 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
  }
94
94
  if (arm != NULL) {
95
- mongory_atomic_store_ptr_release(&literal->array_record_matcher, arm);
95
+ arm->parent = &literal->base;
96
+ double delta = (1.0 + arm->priority) - literal->base.priority;
97
+ if (delta != 0.0) {
98
+ for (mongory_matcher *cur = &literal->base; cur != NULL; cur = cur->parent) {
99
+ cur->priority += delta;
100
+ if (cur->traverse == mongory_matcher_composite_traverse) {
101
+ mongory_matcher_resort_composite((mongory_composite_matcher *)cur);
102
+ }
103
+ }
104
+ }
105
+ mongory_atomic_store_ptr_release(&literal->implicit_array_matcher, arm);
96
106
  } else {
97
107
  mongory_propagate_build_error(build_pool, value);
98
108
  }
@@ -125,6 +135,9 @@ static inline mongory_matcher *mongory_matcher_null_new(mongory_memory_pool *poo
125
135
  MG_TABLE_WRAP(pool, 1, "$eq", mongory_value_wrap_n(pool, NULL)),
126
136
  MG_TABLE_WRAP(pool, 1, "$exists", mongory_value_wrap_b(pool, false))
127
137
  );
138
+ if (!new_or_condition || pool->error != NULL) {
139
+ return NULL;
140
+ }
128
141
  return mongory_matcher_or_new(pool, new_or_condition, extern_ctx);
129
142
  }
130
143
 
@@ -132,7 +145,7 @@ static inline mongory_matcher *mongory_matcher_null_new(mongory_memory_pool *poo
132
145
  * @brief Delegates creation of a simple literal matcher based on the
133
146
  * `condition`'s type.
134
147
  *
135
- * - Table condition: creates a `mongory_matcher_table_cond_new`.
148
+ * - Table condition: creates a `mongory_matcher_condition_new`.
136
149
  * - Regex condition: creates a `mongory_matcher_regex_new`.
137
150
  * - Null condition: creates a specialized `mongory_matcher_null_new`.
138
151
  * - Other types: creates an equality matcher (`mongory_matcher_equal_new`).
@@ -141,13 +154,13 @@ static inline mongory_matcher *mongory_matcher_null_new(mongory_memory_pool *poo
141
154
  * @param condition The literal value or condition table.
142
155
  * @return The appropriate simple matcher for the condition, or NULL on failure.
143
156
  */
144
- 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) {
145
158
  if (!condition)
146
159
  return mongory_matcher_equal_new(pool, NULL, extern_ctx); // Or specific null matcher
147
160
 
148
161
  switch (condition->type) {
149
162
  case MONGORY_TYPE_TABLE:
150
- return mongory_matcher_table_cond_new(pool, condition, extern_ctx);
163
+ return mongory_matcher_condition_new(pool, condition, extern_ctx);
151
164
  case MONGORY_TYPE_REGEX:
152
165
  return mongory_matcher_regex_new(pool, condition, extern_ctx);
153
166
  case MONGORY_TYPE_NULL:
@@ -164,7 +177,7 @@ static inline mongory_matcher *mongory_matcher_literal_delegate(mongory_memory_p
164
177
  *
165
178
  * Extracts the value of `field_matcher->field` from the input `value`
166
179
  * (table or array). Then, applies the literal matching logic
167
- * (`mongory_matcher_literal_match`) using the sub-matcher stored in
180
+ * (`mongory_matcher_value_match`) using the sub-matcher stored in
168
181
  * `composite.left` (which was set up by `literal_delegate` based on the
169
182
  * original condition for this field).
170
183
  *
@@ -190,16 +203,20 @@ static inline bool mongory_matcher_field_match(mongory_matcher *matcher, mongory
190
203
  } else if (value->type == MONGORY_TYPE_ARRAY) {
191
204
  if (value->data.a) {
192
205
  int index;
193
- if (!mongory_try_parse_int(field_key, &index))
194
- return false; // Field key is not a valid integer index for an array.
195
- if (index < 0) { // Handle negative indexing (from end of array)
196
- if ((size_t)(-index) > value->data.a->count)
197
- return false; // Out of bounds
198
- 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
+ }
199
217
  }
200
- if ((size_t)index >= value->data.a->count)
201
- return false; // Out of bounds
202
- 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.
203
220
  }
204
221
  } else {
205
222
  return false; // Can only extract fields from tables or arrays.
@@ -221,11 +238,17 @@ static inline bool mongory_matcher_field_match(mongory_matcher *matcher, mongory
221
238
  }
222
239
  // Now, use the literal_match logic (which uses composite.left primarily)
223
240
  // to match the extracted field_value.
224
- return mongory_matcher_literal_match(matcher, field_value);
241
+ return mongory_matcher_value_match(matcher, field_value);
225
242
  }
226
243
 
227
244
  mongory_matcher *mongory_matcher_field_new(mongory_memory_pool *pool, char *field_name,
228
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
+ }
229
252
  mongory_field_matcher *field_m = MG_ALLOC_PTR(pool, mongory_field_matcher);
230
253
  if (field_m == NULL) {
231
254
  pool->error = &MONGORY_ALLOC_ERROR;
@@ -233,73 +256,93 @@ mongory_matcher *mongory_matcher_field_new(mongory_memory_pool *pool, char *fiel
233
256
  }
234
257
  field_m->field = mongory_string_cpy(pool, field_name);
235
258
  if (field_m->field == NULL) {
236
- // pool->free(field_m) is not standard; pool manages its own memory.
237
- // This indicates an error in string copy, likely pool allocation.
238
259
  return NULL;
239
260
  }
240
261
 
241
262
  // Initialize the base composite matcher part
242
263
  field_m->literal.base.pool = pool;
243
- 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
+ }
244
268
  field_m->literal.base.match = mongory_matcher_field_match;
245
269
  field_m->literal.base.original_match = mongory_matcher_field_match;
246
270
  field_m->literal.base.sub_count = 1;
247
271
  field_m->literal.base.condition = condition_for_field; // Original condition for the field
248
- field_m->literal.base.name = mongory_string_cpy(pool, "Field");
249
272
  field_m->literal.base.explain = mongory_matcher_field_explain;
250
- field_m->literal.base.traverse = mongory_matcher_literal_traverse;
273
+ field_m->literal.base.traverse = mongory_matcher_value_traverse;
251
274
  field_m->literal.base.extern_ctx = extern_ctx;
252
275
  // The 'left' child of the composite is the actual matcher for the field's value,
253
276
  // determined by the type of 'condition_for_field'.
254
- field_m->literal.delegate_matcher = mongory_matcher_literal_delegate(pool, condition_for_field, extern_ctx);
255
- field_m->literal.array_record_matcher = NULL; // Not typically used by field_match directly,
256
- // 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;
257
279
 
258
280
  if (field_m->literal.delegate_matcher == NULL) {
259
281
  // Failed to create the delegate matcher for the condition.
260
282
  return NULL;
261
283
  }
262
284
  field_m->literal.base.priority = 1.0 + field_m->literal.delegate_matcher->priority;
285
+ field_m->literal.base.parent = NULL;
286
+ field_m->literal.delegate_matcher->parent = (mongory_matcher *)field_m;
263
287
 
264
288
  return (mongory_matcher *)field_m;
265
289
  }
266
290
 
267
291
  /**
268
292
  * @brief Match function for a NOT matcher.
269
- * Negates the result of `mongory_matcher_literal_match`.
293
+ * Negates the result of `mongory_matcher_value_match`.
270
294
  * @param matcher The $not matcher.
271
295
  * @param value The value to evaluate.
272
296
  * @return True if the literal match is false, false if it's true.
273
297
  */
274
298
  static inline bool mongory_matcher_not_match(mongory_matcher *matcher, mongory_value *value) {
275
299
  // literal_match uses composite.left (and sometimes .right for arrays)
276
- return !mongory_matcher_literal_match(matcher, value);
300
+ return !mongory_matcher_value_match(matcher, value);
277
301
  }
278
302
 
279
303
  mongory_matcher *mongory_matcher_not_new(mongory_memory_pool *pool, mongory_value *condition_to_negate, void *extern_ctx) {
280
- mongory_literal_matcher *literal = MG_ALLOC_PTR(pool, mongory_literal_matcher);
281
- 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();
282
318
  return NULL;
319
+ }
283
320
 
284
321
  // The 'left' child is the matcher for the condition being negated.
285
- 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);
286
323
  if (!literal->delegate_matcher) {
324
+ mongory_compile_budget_leave_depth();
287
325
  return NULL; // Failed to create delegate for the condition.
288
326
  }
289
- literal->array_record_matcher = NULL;
290
- // composite->right remains NULL for $not, as literal_match's array path
291
- // via composite->right will use condition_to_negate if right is NULL.
327
+ literal->implicit_array_matcher = NULL;
292
328
 
293
329
  literal->base.pool = pool;
294
330
  literal->base.condition = condition_to_negate;
295
331
  literal->base.match = mongory_matcher_not_match;
296
332
  literal->base.original_match = mongory_matcher_not_match;
297
333
  literal->base.name = mongory_string_cpy(pool, "Not");
298
- literal->base.explain = mongory_matcher_literal_explain;
299
- 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;
300
340
  literal->base.sub_count = 1;
301
341
  literal->base.extern_ctx = extern_ctx;
302
342
  literal->base.priority = 1.0 + literal->delegate_matcher->priority;
343
+ literal->base.parent = NULL;
344
+ literal->delegate_matcher->parent = (mongory_matcher *)literal;
345
+ mongory_compile_budget_leave_depth();
303
346
  return (mongory_matcher *)literal;
304
347
  }
305
348
 
@@ -316,41 +359,58 @@ static inline bool mongory_matcher_size_match(mongory_matcher *matcher, mongory_
316
359
  return false; // $size only applies to valid arrays.
317
360
  }
318
361
  mongory_array *array = value->data.a;
319
- // Wrap the array's count as a mongory_value (integer) to be matched.
320
- // The pool for this temporary size value should be from the input value or matcher.
321
-
322
- // Use literal_match with the matcher's original condition against the size_value.
323
- // The $size matcher's `composite.left` was set up by literal_delegate
324
- // based on the condition provided to $size (e.g., if {$size: 5}, left is an
325
- // equality matcher for 5).
326
- 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);
327
367
  }
328
368
 
329
369
  mongory_matcher *mongory_matcher_size_new(mongory_memory_pool *pool, mongory_value *size_condition, void *extern_ctx) {
330
- mongory_literal_matcher *literal = MG_ALLOC_PTR(pool, mongory_literal_matcher);
331
- if (!literal)
370
+ if (!pool || !pool->alloc) {
371
+ return NULL;
372
+ }
373
+ if (!mongory_compile_budget_enter_depth(pool)) {
332
374
  return NULL;
375
+ }
376
+ if (!mongory_compile_budget_count_node(pool)) {
377
+ mongory_compile_budget_leave_depth();
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
+ }
333
386
 
334
387
  // The 'left' child is the matcher for the size_condition itself.
335
388
  // E.g., if {$size: {$gt: 5}}, size_condition is {$gt: 5}, and
336
389
  // composite.left becomes a "greater than 5" matcher.
337
- 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);
338
391
  if (!literal->delegate_matcher) {
392
+ mongory_compile_budget_leave_depth();
339
393
  return NULL;
340
394
  }
341
- literal->array_record_matcher = NULL;
342
- // composite->right typically NULL for $size, array path of literal_match not primary.
395
+ literal->implicit_array_matcher = NULL;
343
396
 
344
397
  literal->base.pool = pool;
345
398
  literal->base.condition = size_condition;
346
399
  literal->base.match = mongory_matcher_size_match;
347
400
  literal->base.original_match = mongory_matcher_size_match;
348
401
  literal->base.name = mongory_string_cpy(pool, "Size");
349
- literal->base.explain = mongory_matcher_literal_explain;
350
- 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;
351
408
  literal->base.sub_count = 1;
352
409
  literal->base.extern_ctx = extern_ctx;
353
410
  literal->base.priority = 1.0 + literal->delegate_matcher->priority;
411
+ literal->base.parent = NULL;
412
+ literal->delegate_matcher->parent = (mongory_matcher *)literal;
413
+ mongory_compile_budget_leave_depth();
354
414
  return (mongory_matcher *)literal;
355
415
  }
356
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
  }
@@ -57,8 +57,8 @@ typedef struct rb_mongory_array_t {
57
57
  rb_mongory_memory_pool_t *rb_mongory_memory_pool_new(void);
58
58
  bool rb_mongory_error_handling(mongory_memory_pool *pool, const char *error_message);
59
59
 
60
- // CMatcher registration (rb_mongory_cmatcher.c)
61
- void rb_mongory_init_cmatcher(void);
60
+ // Matcher registration (rb_mongory_matcher.c)
61
+ void rb_mongory_init_matcher(void);
62
62
 
63
63
  // Value conversion, lazy wrappers, and key caches (rb_mongory_value.c)
64
64
  mongory_value *rb_to_mongory_value_deep(mongory_memory_pool *pool, VALUE rb_value);