mongory 0.9.1-aarch64-linux → 2.0.0-aarch64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +115 -420
  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 +68 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +14 -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 +132 -29
  27. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +1 -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 +18 -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 +4 -4
  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} +104 -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/core/2.6/mongory_ext.so +0 -0
  45. data/lib/core/2.7/mongory_ext.so +0 -0
  46. data/lib/core/3.0/mongory_ext.so +0 -0
  47. data/lib/core/3.1/mongory_ext.so +0 -0
  48. data/lib/core/3.2/mongory_ext.so +0 -0
  49. data/lib/core/3.3/mongory_ext.so +0 -0
  50. data/lib/core/3.4/mongory_ext.so +0 -0
  51. data/lib/core/4.0/mongory_ext.so +0 -0
  52. data/lib/generators/mongory/install/templates/initializer.rb.erb +0 -5
  53. data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
  54. data/lib/mongory/converters/converted.rb +28 -6
  55. data/lib/mongory/converters/key_converter.rb +2 -0
  56. data/lib/mongory/matcher.rb +63 -0
  57. data/lib/mongory/matchers/abstract_matcher.rb +18 -192
  58. data/lib/mongory/matchers.rb +68 -84
  59. data/lib/mongory/query_builder.rb +30 -41
  60. data/lib/mongory/utils/tree_renderer.rb +108 -0
  61. data/lib/mongory/utils.rb +2 -67
  62. data/lib/mongory/version.rb +1 -1
  63. data/lib/mongory.rb +9 -59
  64. data/script/bench.rb +54 -0
  65. metadata +13 -37
  66. data/lib/mongory/c_matcher.rb +0 -57
  67. data/lib/mongory/c_query_builder.rb +0 -44
  68. data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -150
  69. data/lib/mongory/matchers/and_matcher.rb +0 -73
  70. data/lib/mongory/matchers/array_record_matcher.rb +0 -105
  71. data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
  72. data/lib/mongory/matchers/eq_matcher.rb +0 -48
  73. data/lib/mongory/matchers/every_matcher.rb +0 -56
  74. data/lib/mongory/matchers/exists_matcher.rb +0 -53
  75. data/lib/mongory/matchers/field_matcher.rb +0 -160
  76. data/lib/mongory/matchers/gt_matcher.rb +0 -41
  77. data/lib/mongory/matchers/gte_matcher.rb +0 -41
  78. data/lib/mongory/matchers/hash_condition_matcher.rb +0 -80
  79. data/lib/mongory/matchers/in_matcher.rb +0 -84
  80. data/lib/mongory/matchers/literal_matcher.rb +0 -126
  81. data/lib/mongory/matchers/lt_matcher.rb +0 -41
  82. data/lib/mongory/matchers/lte_matcher.rb +0 -41
  83. data/lib/mongory/matchers/ne_matcher.rb +0 -39
  84. data/lib/mongory/matchers/nin_matcher.rb +0 -64
  85. data/lib/mongory/matchers/not_matcher.rb +0 -40
  86. data/lib/mongory/matchers/or_matcher.rb +0 -69
  87. data/lib/mongory/matchers/present_matcher.rb +0 -55
  88. data/lib/mongory/matchers/regex_matcher.rb +0 -109
  89. data/lib/mongory/matchers/size_matcher.rb +0 -54
  90. data/lib/mongory/query_matcher.rb +0 -93
  91. data/lib/mongory/rails.rb +0 -15
  92. data/lib/mongory/utils/debugger.rb +0 -125
  93. data/lib/mongory/utils/rails_patch.rb +0 -22
@@ -100,4 +100,18 @@ mongory_matcher *mongory_matcher_always_false_new(mongory_memory_pool *pool, mon
100
100
 
101
101
  void mongory_matcher_enable_trace_at(mongory_matcher *matcher, mongory_array *trace_stack, int level);
102
102
 
103
+ typedef struct mongory_compile_budget {
104
+ bool active;
105
+ size_t depth;
106
+ size_t nodes;
107
+ size_t max_depth;
108
+ size_t max_nodes;
109
+ } mongory_compile_budget;
110
+
111
+ mongory_compile_budget mongory_compile_budget_begin(void);
112
+ void mongory_compile_budget_end(mongory_compile_budget saved);
113
+ bool mongory_compile_budget_enter_depth(mongory_memory_pool *pool);
114
+ void mongory_compile_budget_leave_depth(void);
115
+ bool mongory_compile_budget_count_node(mongory_memory_pool *pool);
116
+
103
117
  #endif /* MONGORY_MATCHER_BASE_H */
@@ -127,6 +127,11 @@ mongory_matcher *mongory_matcher_not_equal_new(mongory_memory_pool *pool, mongor
127
127
  return matcher;
128
128
  }
129
129
 
130
+ static inline bool mongory_value_is_orderable(const mongory_value *value) {
131
+ return value != NULL &&
132
+ (value->type == MONGORY_TYPE_INT || value->type == MONGORY_TYPE_DOUBLE || value->type == MONGORY_TYPE_STRING);
133
+ }
134
+
130
135
  /**
131
136
  * @brief Match function for "greater than" ($gt).
132
137
  * @param matcher The $gt matcher instance.
@@ -135,7 +140,7 @@ mongory_matcher *mongory_matcher_not_equal_new(mongory_memory_pool *pool, mongor
135
140
  * otherwise or on comparison failure.
136
141
  */
137
142
  static inline bool mongory_matcher_greater_than_match(mongory_matcher *matcher, mongory_value *value) {
138
- if (!value || !value->comp || !matcher->condition)
143
+ if (!mongory_value_is_orderable(value) || !mongory_value_is_orderable(matcher->condition) || !value->comp)
139
144
  return false;
140
145
  int result = value->comp(value, matcher->condition);
141
146
  if (result == mongory_value_compare_fail) {
@@ -162,7 +167,7 @@ mongory_matcher *mongory_matcher_greater_than_new(mongory_memory_pool *pool, mon
162
167
  * on comparison failure.
163
168
  */
164
169
  static inline bool mongory_matcher_less_than_match(mongory_matcher *matcher, mongory_value *value) {
165
- if (!value || !value->comp || !matcher->condition)
170
+ if (!mongory_value_is_orderable(value) || !mongory_value_is_orderable(matcher->condition) || !value->comp)
166
171
  return false;
167
172
  int result = value->comp(value, matcher->condition);
168
173
  if (result == mongory_value_compare_fail) {
@@ -189,7 +194,7 @@ mongory_matcher *mongory_matcher_less_than_new(mongory_memory_pool *pool, mongor
189
194
  * comparison failure.
190
195
  */
191
196
  static inline bool mongory_matcher_greater_than_or_equal_match(mongory_matcher *matcher, mongory_value *value) {
192
- if (!value || !value->comp || !matcher->condition)
197
+ if (!mongory_value_is_orderable(value) || !mongory_value_is_orderable(matcher->condition) || !value->comp)
193
198
  return false;
194
199
  int result = value->comp(value, matcher->condition);
195
200
  if (result == mongory_value_compare_fail) {
@@ -216,7 +221,7 @@ mongory_matcher *mongory_matcher_greater_than_or_equal_new(mongory_memory_pool *
216
221
  * comparison failure.
217
222
  */
218
223
  static inline bool mongory_matcher_less_than_or_equal_match(mongory_matcher *matcher, mongory_value *value) {
219
- if (!value || !value->comp || !matcher->condition)
224
+ if (!mongory_value_is_orderable(value) || !mongory_value_is_orderable(matcher->condition) || !value->comp)
220
225
  return false;
221
226
  int result = value->comp(value, matcher->condition);
222
227
  if (result == mongory_value_compare_fail) {
@@ -11,7 +11,7 @@
11
11
  #include "../foundations/memory_pool_private.h" // For atomic children load/store
12
12
  #include "../foundations/string_buffer.h" // For mongory_string_buffer_new
13
13
  #include "base_matcher.h" // For mongory_matcher_always_true_new, etc.
14
- #include "literal_matcher.h" // For mongory_matcher_field_new
14
+ #include "value_matcher.h" // For mongory_matcher_field_new
15
15
  #include "mongory-core/foundations/error.h" // For MONGORY_ERROR_INVALID_ARGUMENT
16
16
  #include "mongory-core/foundations/memory_pool.h"
17
17
  #include "mongory-core/foundations/table.h" // For mongory_table operations
@@ -57,6 +57,8 @@ static inline void mongory_matcher_attach_children_parent(mongory_composite_matc
57
57
  mongory_composite_matcher *mongory_matcher_composite_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
58
58
  if (!pool || !pool->alloc)
59
59
  return NULL;
60
+ if (!mongory_compile_budget_count_node(pool))
61
+ return NULL;
60
62
 
61
63
  mongory_composite_matcher *composite = MG_ALLOC_PTR(pool, mongory_composite_matcher);
62
64
  if (composite == NULL) {
@@ -142,11 +144,7 @@ typedef struct mongory_matcher_table_build_sub_matcher_context {
142
144
  static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mongory_value *value, mongory_matcher_table_build_sub_matcher_context *ctx);
143
145
 
144
146
  static inline void mongory_matcher_build_error_set(mongory_memory_pool *pool, const char *message) {
145
- pool->error = MG_ALLOC_PTR(pool, mongory_error);
146
- if (pool->error) {
147
- pool->error->type = MONGORY_ERROR_INVALID_ARGUMENT;
148
- pool->error->message = message;
149
- }
147
+ mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, message);
150
148
  }
151
149
 
152
150
  /**
@@ -180,7 +178,9 @@ static inline bool mongory_matcher_table_build_sub_matcher(char *key, mongory_va
180
178
  return false;
181
179
  }
182
180
 
183
- matchers_array->push(matchers_array, (mongory_value *)sub_matcher);
181
+ if (!matchers_array->push(matchers_array, (mongory_value *)sub_matcher)) {
182
+ return false;
183
+ }
184
184
  return true;
185
185
  }
186
186
 
@@ -242,43 +242,64 @@ static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mong
242
242
  * @param condition A `mongory_value` of type `MONGORY_TYPE_TABLE`.
243
243
  * @return A `mongory_matcher` representing the combined logic of the table, or NULL on failure.
244
244
  */
245
- mongory_matcher *mongory_matcher_table_cond_new(mongory_memory_pool *pool, mongory_value *table_condition, void *extern_ctx) {
245
+ mongory_matcher *mongory_matcher_condition_new(mongory_memory_pool *pool, mongory_value *table_condition, void *extern_ctx) {
246
246
  if (!MONGORY_VALIDATE_TABLE(pool, table_condition)) {
247
247
  return NULL;
248
248
  }
249
+ if (!mongory_compile_budget_enter_depth(pool)) {
250
+ return NULL;
251
+ }
249
252
 
250
253
  mongory_table *table = table_condition->data.t;
251
254
  if (table->count == 0) {
252
255
  // Empty table condition matches everything.
253
- return mongory_matcher_always_true_new(pool, table_condition, extern_ctx);
256
+ mongory_matcher *res = mongory_matcher_always_true_new(pool, table_condition, extern_ctx);
257
+ mongory_compile_budget_leave_depth();
258
+ return res;
254
259
  }
255
260
 
256
261
  mongory_array *sub_matchers = mongory_array_new(pool);
257
- if (sub_matchers == NULL)
262
+ if (sub_matchers == NULL) {
263
+ mongory_compile_budget_leave_depth();
258
264
  return NULL; // Failed to create array for sub-matchers.
265
+ }
259
266
 
260
267
  mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, table};
261
268
  // Iterate over the condition table, building sub-matchers.
262
269
  if (!table->each(table, &build_ctx, mongory_matcher_table_build_sub_matcher)) {
263
270
  // Building one of the sub-matchers failed.
271
+ mongory_compile_budget_leave_depth();
264
272
  return NULL;
265
273
  }
266
274
 
267
275
  if (sub_matchers->count == 1) {
268
- return (mongory_matcher *)sub_matchers->get(sub_matchers, 0);
276
+ mongory_matcher *res = (mongory_matcher *)sub_matchers->get(sub_matchers, 0);
277
+ mongory_compile_budget_leave_depth();
278
+ return res;
269
279
  }
270
280
 
271
281
  // Combine sub-matchers using AND logic.
272
282
  mongory_composite_matcher *final_matcher = mongory_matcher_composite_new(pool, table_condition, extern_ctx);
273
- if (final_matcher == NULL)
283
+ if (final_matcher == NULL) {
284
+ mongory_compile_budget_leave_depth();
274
285
  return NULL;
286
+ }
275
287
  final_matcher->children = mongory_matcher_sort_matchers(sub_matchers);
288
+ if (final_matcher->children == NULL) {
289
+ mongory_compile_budget_leave_depth();
290
+ return NULL;
291
+ }
276
292
  mongory_matcher_attach_children_parent(final_matcher, final_matcher->children);
277
293
  final_matcher->base.match = mongory_matcher_and_match;
278
294
  final_matcher->base.original_match = mongory_matcher_and_match;
279
295
  final_matcher->base.sub_count = sub_matchers->count;
280
- final_matcher->base.name = mongory_string_cpy(pool, "Condition");
296
+ final_matcher->base.name = mongory_string_cpy(pool, "And");
297
+ if (final_matcher->base.name == NULL) {
298
+ mongory_compile_budget_leave_depth();
299
+ return NULL;
300
+ }
281
301
  final_matcher->base.priority += mongory_matcher_calculate_priority(sub_matchers);
302
+ mongory_compile_budget_leave_depth();
282
303
  return (mongory_matcher *)final_matcher;
283
304
  }
284
305
 
@@ -319,19 +340,26 @@ mongory_matcher *mongory_matcher_and_new(mongory_memory_pool *pool, mongory_valu
319
340
  if (!MONGORY_VALIDATE_ARRAY(pool, and_condition)) {
320
341
  return NULL;
321
342
  }
343
+ if (!mongory_compile_budget_enter_depth(pool)) {
344
+ return NULL;
345
+ }
322
346
 
323
347
  mongory_array *array_of_tables = and_condition->data.a;
324
348
  if (array_of_tables->count == 0) {
325
- return mongory_matcher_always_true_new(pool, and_condition, extern_ctx); // $and:[] is true
349
+ mongory_matcher *res = mongory_matcher_always_true_new(pool, and_condition, extern_ctx); // $and:[] is true
350
+ mongory_compile_budget_leave_depth();
351
+ return res;
326
352
  }
327
353
  mongory_value *sub_condition_of_and_condition = array_of_tables->get(array_of_tables, 0);
328
354
 
329
355
  if (!MONGORY_VALIDATE_TABLE(pool, sub_condition_of_and_condition)) {
356
+ mongory_compile_budget_leave_depth();
330
357
  return NULL;
331
358
  }
332
359
 
333
360
  mongory_array *sub_matchers = mongory_array_new(pool);
334
361
  if (sub_matchers == NULL) {
362
+ mongory_compile_budget_leave_depth();
335
363
  return NULL;
336
364
  }
337
365
 
@@ -343,28 +371,44 @@ mongory_matcher *mongory_matcher_and_new(mongory_memory_pool *pool, mongory_valu
343
371
  for (int i = 0; i < total; i++) {
344
372
  mongory_value *table = array_of_tables->get(array_of_tables, i);
345
373
  if (!mongory_matcher_build_and_sub_matcher(table, &build_ctx)) {
374
+ mongory_compile_budget_leave_depth();
346
375
  return NULL; // Failure during sub-matcher construction.
347
376
  }
348
377
  }
349
378
 
350
379
  if (sub_matchers->count == 0) {
351
- return mongory_matcher_always_true_new(pool, and_condition, extern_ctx);
380
+ mongory_matcher *res = mongory_matcher_always_true_new(pool, and_condition, extern_ctx);
381
+ mongory_compile_budget_leave_depth();
382
+ return res;
352
383
  }
353
384
 
354
385
  if (sub_matchers->count == 1) {
355
- return (mongory_matcher *)sub_matchers->get(sub_matchers, 0);
386
+ mongory_matcher *res = (mongory_matcher *)sub_matchers->get(sub_matchers, 0);
387
+ mongory_compile_budget_leave_depth();
388
+ return res;
356
389
  }
357
390
 
358
391
  mongory_composite_matcher *final_matcher = mongory_matcher_composite_new(pool, and_condition, extern_ctx);
359
- if (final_matcher == NULL)
392
+ if (final_matcher == NULL) {
393
+ mongory_compile_budget_leave_depth();
360
394
  return NULL;
395
+ }
361
396
  final_matcher->children = mongory_matcher_sort_matchers(sub_matchers);
397
+ if (final_matcher->children == NULL) {
398
+ mongory_compile_budget_leave_depth();
399
+ return NULL;
400
+ }
362
401
  mongory_matcher_attach_children_parent(final_matcher, final_matcher->children);
363
402
  final_matcher->base.match = mongory_matcher_and_match;
364
403
  final_matcher->base.original_match = mongory_matcher_and_match;
365
404
  final_matcher->base.sub_count = sub_matchers->count;
366
405
  final_matcher->base.name = mongory_string_cpy(pool, "And");
406
+ if (final_matcher->base.name == NULL) {
407
+ mongory_compile_budget_leave_depth();
408
+ return NULL;
409
+ }
367
410
  final_matcher->base.priority += mongory_matcher_calculate_priority(sub_matchers);
411
+ mongory_compile_budget_leave_depth();
368
412
  return (mongory_matcher *)final_matcher;
369
413
  }
370
414
 
@@ -375,7 +419,7 @@ mongory_matcher *mongory_matcher_and_new(mongory_memory_pool *pool, mongory_valu
375
419
  * @param condition_table A `mongory_value` (table) from the $or array.
376
420
  * @param acc Pointer to `mongory_matcher_table_build_sub_matcher_context`.
377
421
  * The `matchers` array in context will store the result of
378
- * `mongory_matcher_table_cond_new`.
422
+ * `mongory_matcher_condition_new`.
379
423
  * @return True if successful, false otherwise.
380
424
  */
381
425
  static inline bool mongory_matcher_build_or_sub_matcher(mongory_value *condition_table, void *acc) {
@@ -385,11 +429,13 @@ static inline bool mongory_matcher_build_or_sub_matcher(mongory_value *condition
385
429
 
386
430
  // Each 'condition_table' is a complete query object for one branch of the OR.
387
431
  // So, create a full table_cond_new matcher for it.
388
- mongory_matcher *table_level_matcher = mongory_matcher_table_cond_new(pool_for_new_matchers, condition_table, ctx->extern_ctx);
432
+ mongory_matcher *table_level_matcher = mongory_matcher_condition_new(pool_for_new_matchers, condition_table, ctx->extern_ctx);
389
433
  if (table_level_matcher == NULL) {
390
434
  return false; // Failed to create a matcher for this OR branch.
391
435
  }
392
- array_to_store_table_matchers->push(array_to_store_table_matchers, (mongory_value *)table_level_matcher);
436
+ if (!array_to_store_table_matchers->push(array_to_store_table_matchers, (mongory_value *)table_level_matcher)) {
437
+ return false;
438
+ }
393
439
  return true;
394
440
  }
395
441
 
@@ -410,17 +456,24 @@ mongory_matcher *mongory_matcher_or_new(mongory_memory_pool *pool, mongory_value
410
456
  if (!MONGORY_VALIDATE_ARRAY(pool, or_condition)) {
411
457
  return NULL;
412
458
  }
459
+ if (!mongory_compile_budget_enter_depth(pool)) {
460
+ return NULL;
461
+ }
413
462
  mongory_array *array_of_tables = or_condition->data.a;
414
463
  if (array_of_tables->count == 0) {
415
- return mongory_matcher_always_false_new(pool, or_condition, extern_ctx); // $or:[] is false
464
+ mongory_matcher *res = mongory_matcher_always_false_new(pool, or_condition, extern_ctx); // $or:[] is false
465
+ mongory_compile_budget_leave_depth();
466
+ return res;
416
467
  }
417
468
  mongory_value *sub_condition_of_or_condition = array_of_tables->get(array_of_tables, 0);
418
469
  if (!MONGORY_VALIDATE_TABLE(pool, sub_condition_of_or_condition)) {
470
+ mongory_compile_budget_leave_depth();
419
471
  return NULL;
420
472
  }
421
473
 
422
474
  mongory_array *sub_matchers = mongory_array_new(pool);
423
475
  if (sub_matchers == NULL) {
476
+ mongory_compile_budget_leave_depth();
424
477
  return NULL;
425
478
  }
426
479
 
@@ -429,24 +482,38 @@ mongory_matcher *mongory_matcher_or_new(mongory_memory_pool *pool, mongory_value
429
482
  for (int i = 0; i < total; i++) {
430
483
  mongory_value *table = array_of_tables->get(array_of_tables, i);
431
484
  if (!mongory_matcher_build_or_sub_matcher(table, &build_ctx)) {
485
+ mongory_compile_budget_leave_depth();
432
486
  return NULL; // Failure building one of the OR branches
433
487
  }
434
488
  }
435
489
 
436
490
  if (sub_matchers->count == 1) {
437
- return (mongory_matcher *)sub_matchers->get(sub_matchers, 0);
491
+ mongory_matcher *res = (mongory_matcher *)sub_matchers->get(sub_matchers, 0);
492
+ mongory_compile_budget_leave_depth();
493
+ return res;
438
494
  }
439
495
 
440
496
  mongory_composite_matcher *final_matcher = mongory_matcher_composite_new(pool, or_condition, extern_ctx);
441
- if (final_matcher == NULL)
497
+ if (final_matcher == NULL) {
498
+ mongory_compile_budget_leave_depth();
442
499
  return NULL;
500
+ }
443
501
  final_matcher->children = mongory_matcher_sort_matchers(sub_matchers);
502
+ if (final_matcher->children == NULL) {
503
+ mongory_compile_budget_leave_depth();
504
+ return NULL;
505
+ }
444
506
  mongory_matcher_attach_children_parent(final_matcher, final_matcher->children);
445
507
  final_matcher->base.match = mongory_matcher_or_match;
446
508
  final_matcher->base.original_match = mongory_matcher_or_match;
447
509
  final_matcher->base.sub_count = sub_matchers->count;
448
510
  final_matcher->base.name = mongory_string_cpy(pool, "Or");
511
+ if (final_matcher->base.name == NULL) {
512
+ mongory_compile_budget_leave_depth();
513
+ return NULL;
514
+ }
449
515
  final_matcher->base.priority += mongory_matcher_calculate_priority(sub_matchers);
516
+ mongory_compile_budget_leave_depth();
450
517
  return (mongory_matcher *)final_matcher;
451
518
  }
452
519
 
@@ -492,24 +559,42 @@ mongory_matcher *mongory_matcher_elem_match_new(mongory_memory_pool *pool, mongo
492
559
  if (!MONGORY_VALIDATE_TABLE(pool, elem_match_condition)) {
493
560
  return NULL;
494
561
  }
562
+ if (!mongory_compile_budget_enter_depth(pool)) {
563
+ return NULL;
564
+ }
495
565
  mongory_array *sub_matchers = mongory_array_new(pool);
496
- if (sub_matchers == NULL)
566
+ if (sub_matchers == NULL) {
567
+ mongory_compile_budget_leave_depth();
497
568
  return NULL;
569
+ }
498
570
  mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
499
- if (!mongory_matcher_build_and_sub_matcher(elem_match_condition, &build_ctx))
571
+ if (!mongory_matcher_build_and_sub_matcher(elem_match_condition, &build_ctx)) {
572
+ mongory_compile_budget_leave_depth();
500
573
  return NULL;
574
+ }
501
575
 
502
576
  mongory_composite_matcher *composite = mongory_matcher_composite_new(pool, elem_match_condition, extern_ctx);
503
- if (composite == NULL)
577
+ if (composite == NULL) {
578
+ mongory_compile_budget_leave_depth();
504
579
  return NULL;
580
+ }
505
581
 
506
582
  composite->children = mongory_matcher_sort_matchers(sub_matchers);
583
+ if (composite->children == NULL) {
584
+ mongory_compile_budget_leave_depth();
585
+ return NULL;
586
+ }
507
587
  mongory_matcher_attach_children_parent(composite, composite->children);
508
588
  composite->base.match = mongory_matcher_elem_match_match;
509
589
  composite->base.original_match = mongory_matcher_elem_match_match;
510
590
  composite->base.sub_count = sub_matchers->count;
511
591
  composite->base.name = mongory_string_cpy(pool, "ElemMatch");
592
+ if (composite->base.name == NULL) {
593
+ mongory_compile_budget_leave_depth();
594
+ return NULL;
595
+ }
512
596
  composite->base.priority = 3.0 + mongory_matcher_calculate_priority(sub_matchers);
597
+ mongory_compile_budget_leave_depth();
513
598
  return (mongory_matcher *)composite;
514
599
  }
515
600
 
@@ -552,24 +637,42 @@ mongory_matcher *mongory_matcher_every_new(mongory_memory_pool *pool, mongory_va
552
637
  if (!MONGORY_VALIDATE_TABLE(pool, every_condition)) {
553
638
  return NULL;
554
639
  }
640
+ if (!mongory_compile_budget_enter_depth(pool)) {
641
+ return NULL;
642
+ }
555
643
  mongory_array *sub_matchers = mongory_array_new(pool);
556
- if (sub_matchers == NULL)
644
+ if (sub_matchers == NULL) {
645
+ mongory_compile_budget_leave_depth();
557
646
  return NULL;
647
+ }
558
648
  mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
559
- if (!mongory_matcher_build_and_sub_matcher(every_condition, &build_ctx))
649
+ if (!mongory_matcher_build_and_sub_matcher(every_condition, &build_ctx)) {
650
+ mongory_compile_budget_leave_depth();
560
651
  return NULL;
652
+ }
561
653
 
562
654
  mongory_composite_matcher *composite = mongory_matcher_composite_new(pool, every_condition, extern_ctx);
563
- if (composite == NULL)
655
+ if (composite == NULL) {
656
+ mongory_compile_budget_leave_depth();
564
657
  return NULL;
658
+ }
565
659
 
566
660
  composite->children = mongory_matcher_sort_matchers(sub_matchers);
661
+ if (composite->children == NULL) {
662
+ mongory_compile_budget_leave_depth();
663
+ return NULL;
664
+ }
567
665
  mongory_matcher_attach_children_parent(composite, composite->children);
568
666
  composite->base.match = mongory_matcher_every_match;
569
667
  composite->base.original_match = mongory_matcher_every_match;
570
668
  composite->base.sub_count = sub_matchers->count;
571
669
  composite->base.name = mongory_string_cpy(pool, "Every");
670
+ if (composite->base.name == NULL) {
671
+ mongory_compile_budget_leave_depth();
672
+ return NULL;
673
+ }
572
674
  composite->base.priority = 3.0 + mongory_matcher_calculate_priority(sub_matchers);
675
+ mongory_compile_budget_leave_depth();
573
676
  return (mongory_matcher *)composite;
574
677
  }
575
678
 
@@ -88,7 +88,7 @@ mongory_matcher *mongory_matcher_every_new(mongory_memory_pool *pool, mongory_va
88
88
  * @param condition A `mongory_value` of type `MONGORY_TYPE_TABLE`.
89
89
  * @return A new table condition matcher, or NULL on failure/invalid condition.
90
90
  */
91
- mongory_matcher *mongory_matcher_table_cond_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
91
+ mongory_matcher *mongory_matcher_condition_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
92
92
  /** @} */
93
93
 
94
94
  /**
@@ -1,9 +1,9 @@
1
1
  /**
2
- * @file existance_matcher.c
2
+ * @file existence_matcher.c
3
3
  * @brief Implements $exists and $present matchers.
4
4
  * This is an internal implementation file for the matcher module.
5
5
  */
6
- #include "existance_matcher.h"
6
+ #include "existence_matcher.h"
7
7
  #include "base_matcher.h" // For mongory_matcher_base_new
8
8
  #include "mongory-core/foundations/array.h" // For value->data.a->count
9
9
  #include "mongory-core/foundations/error.h" // For mongory_error types
@@ -52,11 +52,7 @@ static inline bool mongory_matcher_exists_match(mongory_matcher *matcher, mongor
52
52
 
53
53
  mongory_matcher *mongory_matcher_exists_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
54
54
  if (!mongory_matcher_validate_bool_condition(condition)) {
55
- pool->error = MG_ALLOC_PTR(pool, mongory_error);
56
- if (pool->error) {
57
- pool->error->type = MONGORY_ERROR_INVALID_ARGUMENT;
58
- pool->error->message = "$exists condition must be a boolean value.";
59
- }
55
+ mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$exists condition must be a boolean value.");
60
56
  return NULL;
61
57
  }
62
58
  mongory_matcher *matcher = mongory_matcher_base_new(pool, condition, extern_ctx);
@@ -66,6 +62,9 @@ mongory_matcher *mongory_matcher_exists_new(mongory_memory_pool *pool, mongory_v
66
62
  matcher->match = mongory_matcher_exists_match;
67
63
  matcher->original_match = mongory_matcher_exists_match;
68
64
  matcher->name = mongory_string_cpy(pool, "Exists");
65
+ if (!matcher->name) {
66
+ return NULL;
67
+ }
69
68
  matcher->priority = 2.0;
70
69
  return matcher;
71
70
  }
@@ -141,11 +140,7 @@ static inline bool mongory_matcher_present_match(mongory_matcher *matcher, mongo
141
140
 
142
141
  mongory_matcher *mongory_matcher_present_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
143
142
  if (!mongory_matcher_validate_bool_condition(condition)) {
144
- pool->error = MG_ALLOC_PTR(pool, mongory_error);
145
- if (pool->error) {
146
- pool->error->type = MONGORY_ERROR_INVALID_ARGUMENT;
147
- pool->error->message = "$present condition must be a boolean value.";
148
- }
143
+ mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$present condition must be a boolean value.");
149
144
  return NULL;
150
145
  }
151
146
  mongory_matcher *matcher = mongory_matcher_base_new(pool, condition, extern_ctx);
@@ -155,6 +150,9 @@ mongory_matcher *mongory_matcher_present_new(mongory_memory_pool *pool, mongory_
155
150
  matcher->match = mongory_matcher_present_match;
156
151
  matcher->original_match = mongory_matcher_present_match;
157
152
  matcher->name = mongory_string_cpy(pool, "Present");
153
+ if (!matcher->name) {
154
+ return NULL;
155
+ }
158
156
  matcher->priority = 2.0;
159
157
  return matcher;
160
158
  }
@@ -1,8 +1,8 @@
1
- #ifndef MONGORY_MATCHER_EXISTANCE_H
2
- #define MONGORY_MATCHER_EXISTANCE_H
1
+ #ifndef MONGORY_MATCHER_EXISTENCE_H
2
+ #define MONGORY_MATCHER_EXISTENCE_H
3
3
 
4
4
  /**
5
- * @file existance_matcher.h
5
+ * @file existence_matcher.h
6
6
  * @brief Defines constructors for existence and presence matchers ($exists,
7
7
  * $present). This is an internal header for the matcher module.
8
8
  */
@@ -45,4 +45,4 @@ mongory_matcher *mongory_matcher_exists_new(mongory_memory_pool *pool, mongory_v
45
45
  */
46
46
  mongory_matcher *mongory_matcher_present_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
47
47
 
48
- #endif /* MONGORY_MATCHER_EXISTANCE_H */
48
+ #endif /* MONGORY_MATCHER_EXISTENCE_H */
@@ -62,11 +62,7 @@ static inline bool mongory_matcher_regex_condition_validate(mongory_value *condi
62
62
  }
63
63
 
64
64
  static inline void mongory_matcher_regex_error_set(mongory_memory_pool *pool, mongory_error_type type, const char *message) {
65
- pool->error = MG_ALLOC_PTR(pool, mongory_error);
66
- if (pool->error) {
67
- pool->error->type = type;
68
- pool->error->message = message;
69
- }
65
+ mongory_error_set(pool, type, message);
70
66
  }
71
67
 
72
68
  /**
@@ -110,6 +106,10 @@ mongory_matcher *mongory_matcher_regex_with_options_new(mongory_memory_pool *poo
110
106
  return NULL;
111
107
  }
112
108
 
109
+ if (!mongory_compile_budget_count_node(pool)) {
110
+ return NULL;
111
+ }
112
+
113
113
  mongory_regex_matcher *matcher = MG_ALLOC_PTR(pool, mongory_regex_matcher);
114
114
  if (!matcher) {
115
115
  pool->error = &MONGORY_ALLOC_ERROR;
@@ -124,9 +124,15 @@ mongory_matcher *mongory_matcher_regex_with_options_new(mongory_memory_pool *poo
124
124
  matcher->base.traverse = mongory_matcher_leaf_traverse;
125
125
  matcher->base.extern_ctx = extern_ctx;
126
126
  matcher->base.name = mongory_string_cpy(pool, "Regex");
127
+ if (!matcher->base.name) {
128
+ return NULL;
129
+ }
127
130
  matcher->base.priority = 20.0;
128
131
  matcher->base.parent = NULL;
129
132
  matcher->options = has_options ? mongory_string_cpy(pool, options->data.s) : NULL;
133
+ if (has_options && !matcher->options) {
134
+ return NULL;
135
+ }
130
136
  return (mongory_matcher *)matcher;
131
137
  }
132
138
 
@@ -166,9 +172,15 @@ bool mongory_matcher_custom_match(mongory_matcher *matcher, mongory_value *value
166
172
  mongory_matcher *mongory_matcher_custom_new(mongory_memory_pool *pool, char *key, mongory_value *condition, void *extern_ctx) {
167
173
  if (mongory_custom_matcher_adapter.build == NULL)
168
174
  return NULL; // Custom matcher adapter not initialized.
175
+ if (!mongory_compile_budget_count_node(pool))
176
+ return NULL;
169
177
  mongory_custom_matcher *matcher = MG_ALLOC_PTR(pool, mongory_custom_matcher);
170
- if (matcher == NULL)
178
+ if (matcher == NULL) {
179
+ if (pool) {
180
+ pool->error = &MONGORY_ALLOC_ERROR;
181
+ }
171
182
  return NULL;
183
+ }
172
184
  mongory_matcher_custom_context *context = mongory_custom_matcher_adapter.build(key, condition, extern_ctx);
173
185
  if (context == NULL)
174
186
  return NULL;