mongory 0.9.0-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 +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/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 -164
  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 -124
  69. data/lib/mongory/matchers/and_matcher.rb +0 -72
  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 -79
  79. data/lib/mongory/matchers/in_matcher.rb +0 -84
  80. data/lib/mongory/matchers/literal_matcher.rb +0 -121
  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 -68
  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
@@ -15,6 +15,65 @@
15
15
  #include "matcher_traversable.h"
16
16
  #include "../foundations/utils.h"
17
17
 
18
+ #if defined(_MSC_VER)
19
+ # define MONGORY_THREAD_LOCAL __declspec(thread)
20
+ #else
21
+ # define MONGORY_THREAD_LOCAL __thread
22
+ #endif
23
+
24
+ static MONGORY_THREAD_LOCAL mongory_compile_budget mongory_tls_compile_budget = {0};
25
+
26
+ mongory_compile_budget mongory_compile_budget_begin(void) {
27
+ mongory_compile_budget saved = mongory_tls_compile_budget;
28
+ mongory_tls_compile_budget.active = true;
29
+ mongory_tls_compile_budget.depth = 0;
30
+ mongory_tls_compile_budget.nodes = 0;
31
+ mongory_tls_compile_budget.max_depth = mongory_max_depth_get();
32
+ mongory_tls_compile_budget.max_nodes = mongory_max_nodes_get();
33
+ return saved;
34
+ }
35
+
36
+ void mongory_compile_budget_end(mongory_compile_budget saved) {
37
+ mongory_tls_compile_budget = saved;
38
+ }
39
+
40
+ bool mongory_compile_budget_enter_depth(mongory_memory_pool *pool) {
41
+ if (!mongory_tls_compile_budget.active) {
42
+ return true;
43
+ }
44
+ mongory_tls_compile_budget.depth++;
45
+ if (mongory_tls_compile_budget.max_depth > 0 &&
46
+ mongory_tls_compile_budget.depth > mongory_tls_compile_budget.max_depth) {
47
+ mongory_tls_compile_budget.depth--;
48
+ if (pool && pool->error == NULL) {
49
+ pool->error = &MONGORY_DEPTH_LIMIT_ERROR;
50
+ }
51
+ return false;
52
+ }
53
+ return true;
54
+ }
55
+
56
+ void mongory_compile_budget_leave_depth(void) {
57
+ if (mongory_tls_compile_budget.active && mongory_tls_compile_budget.depth > 0) {
58
+ mongory_tls_compile_budget.depth--;
59
+ }
60
+ }
61
+
62
+ bool mongory_compile_budget_count_node(mongory_memory_pool *pool) {
63
+ if (!mongory_tls_compile_budget.active) {
64
+ return true;
65
+ }
66
+ mongory_tls_compile_budget.nodes++;
67
+ if (mongory_tls_compile_budget.max_nodes > 0 &&
68
+ mongory_tls_compile_budget.nodes > mongory_tls_compile_budget.max_nodes) {
69
+ if (pool && pool->error == NULL) {
70
+ pool->error = &MONGORY_NODE_LIMIT_ERROR;
71
+ }
72
+ return false;
73
+ }
74
+ return true;
75
+ }
76
+
18
77
  /**
19
78
  * @brief Allocates and initializes common fields of a `mongory_matcher`.
20
79
  *
@@ -35,6 +94,9 @@ mongory_matcher *mongory_matcher_base_new(mongory_memory_pool *pool, mongory_val
35
94
  if (!pool || !pool->alloc) {
36
95
  return NULL; // Invalid memory pool.
37
96
  }
97
+ if (!mongory_compile_budget_count_node(pool)) {
98
+ return NULL;
99
+ }
38
100
  mongory_matcher *matcher = MG_ALLOC_PTR(pool, mongory_matcher);
39
101
  if (matcher == NULL) {
40
102
  // Allocation failed, pool->alloc might set pool->error.
@@ -55,6 +117,7 @@ mongory_matcher *mongory_matcher_base_new(mongory_memory_pool *pool, mongory_val
55
117
  matcher->trace_level = 0;
56
118
  matcher->extern_ctx = extern_ctx; // Set the external context.
57
119
  matcher->priority = 1.0; // Set the priority to 1.0.
120
+ matcher->parent = NULL; // Attached later when wrapped by a parent.
58
121
  return matcher;
59
122
  }
60
123
 
@@ -85,6 +148,9 @@ mongory_matcher *mongory_matcher_always_true_new(mongory_memory_pool *pool, mong
85
148
  matcher->match = mongory_matcher_always_true_match;
86
149
  matcher->original_match = mongory_matcher_always_true_match;
87
150
  matcher->name = mongory_string_cpy(pool, "Always True");
151
+ if (!matcher->name) {
152
+ return NULL;
153
+ }
88
154
  matcher->explain = mongory_matcher_base_explain;
89
155
  // Optionally set original_match as well if it's a strict policy
90
156
  // matcher->context.original_match = mongory_matcher_always_true_match;
@@ -118,6 +184,9 @@ mongory_matcher *mongory_matcher_always_false_new(mongory_memory_pool *pool, mon
118
184
  matcher->match = mongory_matcher_always_false_match;
119
185
  matcher->original_match = mongory_matcher_always_false_match;
120
186
  matcher->name = mongory_string_cpy(pool, "Always False");
187
+ if (!matcher->name) {
188
+ return NULL;
189
+ }
121
190
  matcher->explain = mongory_matcher_base_explain;
122
191
  // matcher->context.original_match = mongory_matcher_always_false_match;
123
192
  return matcher;
@@ -55,6 +55,7 @@ struct mongory_matcher {
55
55
  mongory_array *trace_stack; /**< The trace stack for this matcher. */
56
56
  int trace_level; /**< The trace level for this matcher. */
57
57
  double priority; /**< The priority for this matcher. */
58
+ mongory_matcher *parent; /**< Enclosing parent matcher for dynamic priority updates. */
58
59
  void *extern_ctx; /**< External context for the matcher. */
59
60
  };
60
61
 
@@ -99,4 +100,18 @@ mongory_matcher *mongory_matcher_always_false_new(mongory_memory_pool *pool, mon
99
100
 
100
101
  void mongory_matcher_enable_trace_at(mongory_matcher *matcher, mongory_array *trace_stack, int level);
101
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
+
102
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) {
@@ -8,9 +8,10 @@
8
8
  #include "external_matcher.h"
9
9
  #include "../foundations/config_private.h" // For mongory_matcher_build_func_get
10
10
  #include "../foundations/array_private.h" // For mongory_array_sort_by
11
+ #include "../foundations/memory_pool_private.h" // For atomic children load/store
11
12
  #include "../foundations/string_buffer.h" // For mongory_string_buffer_new
12
13
  #include "base_matcher.h" // For mongory_matcher_always_true_new, etc.
13
- #include "literal_matcher.h" // For mongory_matcher_field_new
14
+ #include "value_matcher.h" // For mongory_matcher_field_new
14
15
  #include "mongory-core/foundations/error.h" // For MONGORY_ERROR_INVALID_ARGUMENT
15
16
  #include "mongory-core/foundations/memory_pool.h"
16
17
  #include "mongory-core/foundations/table.h" // For mongory_table operations
@@ -26,6 +27,18 @@
26
27
  double mongory_matcher_calculate_priority(mongory_array *sub_matchers);
27
28
  mongory_array *mongory_matcher_sort_matchers(mongory_array *sub_matchers);
28
29
 
30
+ static inline void mongory_matcher_attach_children_parent(mongory_composite_matcher *parent, mongory_array *children) {
31
+ if (!parent || !children) {
32
+ return;
33
+ }
34
+ for (size_t i = 0; i < children->count; i++) {
35
+ mongory_matcher *child = (mongory_matcher *)children->get(children, i);
36
+ if (child != NULL) {
37
+ child->parent = (mongory_matcher *)parent;
38
+ }
39
+ }
40
+ }
41
+
29
42
  /**
30
43
  * @brief Allocates and initializes a `mongory_composite_matcher` structure.
31
44
  *
@@ -44,6 +57,8 @@ mongory_array *mongory_matcher_sort_matchers(mongory_array *sub_matchers);
44
57
  mongory_composite_matcher *mongory_matcher_composite_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
45
58
  if (!pool || !pool->alloc)
46
59
  return NULL;
60
+ if (!mongory_compile_budget_count_node(pool))
61
+ return NULL;
47
62
 
48
63
  mongory_composite_matcher *composite = MG_ALLOC_PTR(pool, mongory_composite_matcher);
49
64
  if (composite == NULL) {
@@ -61,6 +76,7 @@ mongory_composite_matcher *mongory_matcher_composite_new(mongory_memory_pool *po
61
76
  composite->base.traverse = mongory_matcher_composite_traverse;
62
77
  composite->base.extern_ctx = extern_ctx;
63
78
  composite->base.priority = 2.0;
79
+ composite->base.parent = NULL;
64
80
  return composite;
65
81
  }
66
82
 
@@ -80,7 +96,7 @@ mongory_composite_matcher *mongory_matcher_composite_new(mongory_memory_pool *po
80
96
  // ============================================================================
81
97
  static inline bool mongory_matcher_and_match(mongory_matcher *matcher, mongory_value *value) {
82
98
  mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
83
- mongory_array *children = composite->children;
99
+ mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
84
100
  int total = (int)children->count;
85
101
  for (int i = 0; i < total; i++) {
86
102
  mongory_matcher *child = (mongory_matcher *)children->get(children, i);
@@ -104,7 +120,7 @@ static inline bool mongory_matcher_and_match(mongory_matcher *matcher, mongory_v
104
120
  */
105
121
  bool mongory_matcher_or_match(mongory_matcher *matcher, mongory_value *value) {
106
122
  mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
107
- mongory_array *children = composite->children;
123
+ mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
108
124
  int total = (int)children->count;
109
125
  for (int i = 0; i < total; i++) {
110
126
  mongory_matcher *child = (mongory_matcher *)children->get(children, i);
@@ -128,11 +144,7 @@ typedef struct mongory_matcher_table_build_sub_matcher_context {
128
144
  static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mongory_value *value, mongory_matcher_table_build_sub_matcher_context *ctx);
129
145
 
130
146
  static inline void mongory_matcher_build_error_set(mongory_memory_pool *pool, const char *message) {
131
- pool->error = MG_ALLOC_PTR(pool, mongory_error);
132
- if (pool->error) {
133
- pool->error->type = MONGORY_ERROR_INVALID_ARGUMENT;
134
- pool->error->message = message;
135
- }
147
+ mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, message);
136
148
  }
137
149
 
138
150
  /**
@@ -166,7 +178,9 @@ static inline bool mongory_matcher_table_build_sub_matcher(char *key, mongory_va
166
178
  return false;
167
179
  }
168
180
 
169
- matchers_array->push(matchers_array, (mongory_value *)sub_matcher);
181
+ if (!matchers_array->push(matchers_array, (mongory_value *)sub_matcher)) {
182
+ return false;
183
+ }
170
184
  return true;
171
185
  }
172
186
 
@@ -228,42 +242,64 @@ static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mong
228
242
  * @param condition A `mongory_value` of type `MONGORY_TYPE_TABLE`.
229
243
  * @return A `mongory_matcher` representing the combined logic of the table, or NULL on failure.
230
244
  */
231
- 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) {
232
246
  if (!MONGORY_VALIDATE_TABLE(pool, table_condition)) {
233
247
  return NULL;
234
248
  }
249
+ if (!mongory_compile_budget_enter_depth(pool)) {
250
+ return NULL;
251
+ }
235
252
 
236
253
  mongory_table *table = table_condition->data.t;
237
254
  if (table->count == 0) {
238
255
  // Empty table condition matches everything.
239
- 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;
240
259
  }
241
260
 
242
261
  mongory_array *sub_matchers = mongory_array_new(pool);
243
- if (sub_matchers == NULL)
262
+ if (sub_matchers == NULL) {
263
+ mongory_compile_budget_leave_depth();
244
264
  return NULL; // Failed to create array for sub-matchers.
265
+ }
245
266
 
246
267
  mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, table};
247
268
  // Iterate over the condition table, building sub-matchers.
248
269
  if (!table->each(table, &build_ctx, mongory_matcher_table_build_sub_matcher)) {
249
270
  // Building one of the sub-matchers failed.
271
+ mongory_compile_budget_leave_depth();
250
272
  return NULL;
251
273
  }
252
274
 
253
275
  if (sub_matchers->count == 1) {
254
- 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;
255
279
  }
256
280
 
257
281
  // Combine sub-matchers using AND logic.
258
282
  mongory_composite_matcher *final_matcher = mongory_matcher_composite_new(pool, table_condition, extern_ctx);
259
- if (final_matcher == NULL)
283
+ if (final_matcher == NULL) {
284
+ mongory_compile_budget_leave_depth();
260
285
  return NULL;
286
+ }
261
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
+ }
292
+ mongory_matcher_attach_children_parent(final_matcher, final_matcher->children);
262
293
  final_matcher->base.match = mongory_matcher_and_match;
263
294
  final_matcher->base.original_match = mongory_matcher_and_match;
264
295
  final_matcher->base.sub_count = sub_matchers->count;
265
- 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
+ }
266
301
  final_matcher->base.priority += mongory_matcher_calculate_priority(sub_matchers);
302
+ mongory_compile_budget_leave_depth();
267
303
  return (mongory_matcher *)final_matcher;
268
304
  }
269
305
 
@@ -304,19 +340,26 @@ mongory_matcher *mongory_matcher_and_new(mongory_memory_pool *pool, mongory_valu
304
340
  if (!MONGORY_VALIDATE_ARRAY(pool, and_condition)) {
305
341
  return NULL;
306
342
  }
343
+ if (!mongory_compile_budget_enter_depth(pool)) {
344
+ return NULL;
345
+ }
307
346
 
308
347
  mongory_array *array_of_tables = and_condition->data.a;
309
348
  if (array_of_tables->count == 0) {
310
- 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;
311
352
  }
312
353
  mongory_value *sub_condition_of_and_condition = array_of_tables->get(array_of_tables, 0);
313
354
 
314
355
  if (!MONGORY_VALIDATE_TABLE(pool, sub_condition_of_and_condition)) {
356
+ mongory_compile_budget_leave_depth();
315
357
  return NULL;
316
358
  }
317
359
 
318
360
  mongory_array *sub_matchers = mongory_array_new(pool);
319
361
  if (sub_matchers == NULL) {
362
+ mongory_compile_budget_leave_depth();
320
363
  return NULL;
321
364
  }
322
365
 
@@ -328,27 +371,44 @@ mongory_matcher *mongory_matcher_and_new(mongory_memory_pool *pool, mongory_valu
328
371
  for (int i = 0; i < total; i++) {
329
372
  mongory_value *table = array_of_tables->get(array_of_tables, i);
330
373
  if (!mongory_matcher_build_and_sub_matcher(table, &build_ctx)) {
374
+ mongory_compile_budget_leave_depth();
331
375
  return NULL; // Failure during sub-matcher construction.
332
376
  }
333
377
  }
334
378
 
335
379
  if (sub_matchers->count == 0) {
336
- 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;
337
383
  }
338
384
 
339
385
  if (sub_matchers->count == 1) {
340
- 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;
341
389
  }
342
390
 
343
391
  mongory_composite_matcher *final_matcher = mongory_matcher_composite_new(pool, and_condition, extern_ctx);
344
- if (final_matcher == NULL)
392
+ if (final_matcher == NULL) {
393
+ mongory_compile_budget_leave_depth();
345
394
  return NULL;
395
+ }
346
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
+ }
401
+ mongory_matcher_attach_children_parent(final_matcher, final_matcher->children);
347
402
  final_matcher->base.match = mongory_matcher_and_match;
348
403
  final_matcher->base.original_match = mongory_matcher_and_match;
349
404
  final_matcher->base.sub_count = sub_matchers->count;
350
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
+ }
351
410
  final_matcher->base.priority += mongory_matcher_calculate_priority(sub_matchers);
411
+ mongory_compile_budget_leave_depth();
352
412
  return (mongory_matcher *)final_matcher;
353
413
  }
354
414
 
@@ -359,7 +419,7 @@ mongory_matcher *mongory_matcher_and_new(mongory_memory_pool *pool, mongory_valu
359
419
  * @param condition_table A `mongory_value` (table) from the $or array.
360
420
  * @param acc Pointer to `mongory_matcher_table_build_sub_matcher_context`.
361
421
  * The `matchers` array in context will store the result of
362
- * `mongory_matcher_table_cond_new`.
422
+ * `mongory_matcher_condition_new`.
363
423
  * @return True if successful, false otherwise.
364
424
  */
365
425
  static inline bool mongory_matcher_build_or_sub_matcher(mongory_value *condition_table, void *acc) {
@@ -369,11 +429,13 @@ static inline bool mongory_matcher_build_or_sub_matcher(mongory_value *condition
369
429
 
370
430
  // Each 'condition_table' is a complete query object for one branch of the OR.
371
431
  // So, create a full table_cond_new matcher for it.
372
- 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);
373
433
  if (table_level_matcher == NULL) {
374
434
  return false; // Failed to create a matcher for this OR branch.
375
435
  }
376
- 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
+ }
377
439
  return true;
378
440
  }
379
441
 
@@ -394,17 +456,24 @@ mongory_matcher *mongory_matcher_or_new(mongory_memory_pool *pool, mongory_value
394
456
  if (!MONGORY_VALIDATE_ARRAY(pool, or_condition)) {
395
457
  return NULL;
396
458
  }
459
+ if (!mongory_compile_budget_enter_depth(pool)) {
460
+ return NULL;
461
+ }
397
462
  mongory_array *array_of_tables = or_condition->data.a;
398
463
  if (array_of_tables->count == 0) {
399
- 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;
400
467
  }
401
468
  mongory_value *sub_condition_of_or_condition = array_of_tables->get(array_of_tables, 0);
402
469
  if (!MONGORY_VALIDATE_TABLE(pool, sub_condition_of_or_condition)) {
470
+ mongory_compile_budget_leave_depth();
403
471
  return NULL;
404
472
  }
405
473
 
406
474
  mongory_array *sub_matchers = mongory_array_new(pool);
407
475
  if (sub_matchers == NULL) {
476
+ mongory_compile_budget_leave_depth();
408
477
  return NULL;
409
478
  }
410
479
 
@@ -413,23 +482,38 @@ mongory_matcher *mongory_matcher_or_new(mongory_memory_pool *pool, mongory_value
413
482
  for (int i = 0; i < total; i++) {
414
483
  mongory_value *table = array_of_tables->get(array_of_tables, i);
415
484
  if (!mongory_matcher_build_or_sub_matcher(table, &build_ctx)) {
485
+ mongory_compile_budget_leave_depth();
416
486
  return NULL; // Failure building one of the OR branches
417
487
  }
418
488
  }
419
489
 
420
490
  if (sub_matchers->count == 1) {
421
- 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;
422
494
  }
423
495
 
424
496
  mongory_composite_matcher *final_matcher = mongory_matcher_composite_new(pool, or_condition, extern_ctx);
425
- if (final_matcher == NULL)
497
+ if (final_matcher == NULL) {
498
+ mongory_compile_budget_leave_depth();
426
499
  return NULL;
500
+ }
427
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
+ }
506
+ mongory_matcher_attach_children_parent(final_matcher, final_matcher->children);
428
507
  final_matcher->base.match = mongory_matcher_or_match;
429
508
  final_matcher->base.original_match = mongory_matcher_or_match;
430
509
  final_matcher->base.sub_count = sub_matchers->count;
431
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
+ }
432
515
  final_matcher->base.priority += mongory_matcher_calculate_priority(sub_matchers);
516
+ mongory_compile_budget_leave_depth();
433
517
  return (mongory_matcher *)final_matcher;
434
518
  }
435
519
 
@@ -475,23 +559,42 @@ mongory_matcher *mongory_matcher_elem_match_new(mongory_memory_pool *pool, mongo
475
559
  if (!MONGORY_VALIDATE_TABLE(pool, elem_match_condition)) {
476
560
  return NULL;
477
561
  }
562
+ if (!mongory_compile_budget_enter_depth(pool)) {
563
+ return NULL;
564
+ }
478
565
  mongory_array *sub_matchers = mongory_array_new(pool);
479
- if (sub_matchers == NULL)
566
+ if (sub_matchers == NULL) {
567
+ mongory_compile_budget_leave_depth();
480
568
  return NULL;
569
+ }
481
570
  mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
482
- 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();
483
573
  return NULL;
574
+ }
484
575
 
485
576
  mongory_composite_matcher *composite = mongory_matcher_composite_new(pool, elem_match_condition, extern_ctx);
486
- if (composite == NULL)
577
+ if (composite == NULL) {
578
+ mongory_compile_budget_leave_depth();
487
579
  return NULL;
580
+ }
488
581
 
489
582
  composite->children = mongory_matcher_sort_matchers(sub_matchers);
583
+ if (composite->children == NULL) {
584
+ mongory_compile_budget_leave_depth();
585
+ return NULL;
586
+ }
587
+ mongory_matcher_attach_children_parent(composite, composite->children);
490
588
  composite->base.match = mongory_matcher_elem_match_match;
491
589
  composite->base.original_match = mongory_matcher_elem_match_match;
492
590
  composite->base.sub_count = sub_matchers->count;
493
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
+ }
494
596
  composite->base.priority = 3.0 + mongory_matcher_calculate_priority(sub_matchers);
597
+ mongory_compile_budget_leave_depth();
495
598
  return (mongory_matcher *)composite;
496
599
  }
497
600
 
@@ -534,23 +637,42 @@ mongory_matcher *mongory_matcher_every_new(mongory_memory_pool *pool, mongory_va
534
637
  if (!MONGORY_VALIDATE_TABLE(pool, every_condition)) {
535
638
  return NULL;
536
639
  }
640
+ if (!mongory_compile_budget_enter_depth(pool)) {
641
+ return NULL;
642
+ }
537
643
  mongory_array *sub_matchers = mongory_array_new(pool);
538
- if (sub_matchers == NULL)
644
+ if (sub_matchers == NULL) {
645
+ mongory_compile_budget_leave_depth();
539
646
  return NULL;
647
+ }
540
648
  mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
541
- 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();
542
651
  return NULL;
652
+ }
543
653
 
544
654
  mongory_composite_matcher *composite = mongory_matcher_composite_new(pool, every_condition, extern_ctx);
545
- if (composite == NULL)
655
+ if (composite == NULL) {
656
+ mongory_compile_budget_leave_depth();
546
657
  return NULL;
658
+ }
547
659
 
548
660
  composite->children = mongory_matcher_sort_matchers(sub_matchers);
661
+ if (composite->children == NULL) {
662
+ mongory_compile_budget_leave_depth();
663
+ return NULL;
664
+ }
665
+ mongory_matcher_attach_children_parent(composite, composite->children);
549
666
  composite->base.match = mongory_matcher_every_match;
550
667
  composite->base.original_match = mongory_matcher_every_match;
551
668
  composite->base.sub_count = sub_matchers->count;
552
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
+ }
553
674
  composite->base.priority = 3.0 + mongory_matcher_calculate_priority(sub_matchers);
675
+ mongory_compile_budget_leave_depth();
554
676
  return (mongory_matcher *)composite;
555
677
  }
556
678
 
@@ -592,3 +714,17 @@ mongory_array *mongory_matcher_sort_matchers(mongory_array *sub_matchers) {
592
714
  temp_pool->free(temp_pool);
593
715
  return sorted_matchers;
594
716
  }
717
+
718
+ void mongory_matcher_resort_composite(mongory_composite_matcher *composite) {
719
+ if (!composite) {
720
+ return;
721
+ }
722
+ mongory_array *cur = mongory_atomic_load_ptr_acquire(&composite->children);
723
+ if (!cur || cur->count <= 1) {
724
+ return;
725
+ }
726
+ mongory_array *sorted = mongory_matcher_sort_matchers(cur);
727
+ if (sorted != NULL) {
728
+ mongory_atomic_store_ptr_release(&composite->children, sorted);
729
+ }
730
+ }
@@ -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
  /**
@@ -122,4 +122,10 @@ mongory_composite_matcher *mongory_matcher_composite_new(mongory_memory_pool *po
122
122
  */
123
123
  bool mongory_matcher_or_match(mongory_matcher *matcher, mongory_value *value);
124
124
 
125
+ /**
126
+ * @brief Re-sorts a composite matcher's children by updated priority using
127
+ * copy-on-write atomic publication safe for concurrent readers.
128
+ */
129
+ void mongory_matcher_resort_composite(mongory_composite_matcher *composite);
130
+
125
131
  #endif /* MONGORY_MATCHER_COMPOSITE_H */