mongory 2.1.0 → 2.2.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 (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/README.md +55 -73
  4. data/docs/clang_bridge.md +2 -2
  5. data/docs/performance.md +43 -7
  6. data/ext/mongory_ext/extconf.rb +15 -0
  7. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +28 -43
  8. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -1
  9. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +3 -4
  10. data/ext/mongory_ext/mongory-core/src/foundations/array.c +28 -5
  11. data/ext/mongory_ext/mongory-core/src/foundations/config.c +24 -22
  12. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +3 -3
  13. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +22 -13
  14. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +4 -3
  15. data/ext/mongory_ext/mongory-core/src/foundations/table.c +60 -224
  16. data/ext/mongory_ext/mongory-core/src/foundations/table_private.h +36 -0
  17. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +30 -47
  18. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +2 -5
  19. data/ext/mongory_ext/mongory-core/src/foundations/value.c +18 -32
  20. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +89 -104
  21. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +18 -11
  22. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +35 -150
  23. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +244 -418
  24. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +30 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +15 -56
  26. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -39
  27. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +14 -15
  28. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +382 -74
  29. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +373 -123
  30. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +20 -12
  31. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -4
  32. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +214 -171
  33. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +8 -16
  34. data/ext/mongory_ext/mongory_ext.c +26 -4
  35. data/ext/mongory_ext/mongory_ext_internal.h +15 -3
  36. data/ext/mongory_ext/rb_mongory_adapters.c +52 -19
  37. data/ext/mongory_ext/rb_mongory_matcher.c +115 -36
  38. data/ext/mongory_ext/rb_mongory_value.c +124 -51
  39. data/lib/mongory/converters/abstract_converter.rb +5 -5
  40. data/lib/mongory/converters/converted.rb +0 -4
  41. data/lib/mongory/converters/key_converter.rb +13 -36
  42. data/lib/mongory/converters/value_converter.rb +4 -4
  43. data/lib/mongory/matchers.rb +8 -7
  44. data/lib/mongory/query_builder.rb +9 -0
  45. data/lib/mongory/utils/context.rb +0 -6
  46. data/lib/mongory/utils/singleton_builder.rb +1 -4
  47. data/lib/mongory/version.rb +1 -1
  48. data/lib/mongory.rb +0 -1
  49. data/script/bench.rb +77 -4
  50. data/script/coverage.rb +84 -0
  51. metadata +7 -7
  52. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
  53. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
@@ -7,7 +7,6 @@
7
7
  #include "composite_matcher.h"
8
8
  #include "external_matcher.h"
9
9
  #include "../foundations/config_private.h" // For mongory_matcher_build_func_get
10
- #include "../foundations/array_private.h" // For mongory_array_sort_by
11
10
  #include "../foundations/memory_pool_private.h" // For atomic children load/store
12
11
  #include "../foundations/string_buffer.h" // For mongory_string_buffer_new
13
12
  #include "base_matcher.h" // For mongory_matcher_always_true_new, etc.
@@ -16,119 +15,159 @@
16
15
  #include "mongory-core/foundations/memory_pool.h"
17
16
  #include "mongory-core/foundations/table.h" // For mongory_table operations
18
17
  #include "mongory-core/foundations/value.h"
19
- #include "matcher_explainable.h"
20
18
  #include "matcher_traversable.h"
21
19
  #include "../foundations/utils.h"
22
20
  #include <mongory-core.h> // General include
23
21
  #include <stdio.h> // For sprintf
24
22
  #include <string.h> // For strcmp
25
23
 
26
- // Forward declaration.
27
- double mongory_matcher_calculate_priority(mongory_array *sub_matchers);
28
- mongory_array *mongory_matcher_sort_matchers(mongory_array *sub_matchers);
24
+ static mongory_array *mongory_matcher_sort_matchers(mongory_array *sub_matchers);
29
25
 
30
- static inline void mongory_matcher_attach_children_parent(mongory_composite_matcher *parent, mongory_array *children) {
31
- if (!parent || !children) {
32
- return;
26
+ static double mongory_matcher_calculate_priority(mongory_array *sub_matchers) {
27
+ if (!sub_matchers) {
28
+ return 0.0;
33
29
  }
34
- for (size_t i = 0; i < children->count; i++) {
35
- mongory_matcher *child = (mongory_matcher *)children->get(children, i);
30
+ double priority = 0.0;
31
+ for (size_t i = 0; i < sub_matchers->count; i++) {
32
+ priority += ((mongory_matcher *)sub_matchers->get(sub_matchers, i))->priority;
33
+ }
34
+ return priority;
35
+ }
36
+
37
+ static mongory_matcher *mongory_matcher_composite_finalize(mongory_memory_pool *pool, mongory_array *sub_matchers,
38
+ mongory_value *condition, void *extern_ctx, const char *name,
39
+ mongory_matcher_match_func match_fn, double base_priority,
40
+ double priority_scale) {
41
+ mongory_composite_matcher *composite = mongory_matcher_composite_new(pool, condition, extern_ctx);
42
+ if (composite == NULL) {
43
+ return NULL;
44
+ }
45
+ composite->children = mongory_matcher_sort_matchers(sub_matchers);
46
+ if (composite->children == NULL) {
47
+ return NULL;
48
+ }
49
+ for (size_t i = 0; i < composite->children->count; i++) {
50
+ mongory_matcher *child = (mongory_matcher *)composite->children->get(composite->children, i);
36
51
  if (child != NULL) {
37
- child->parent = (mongory_matcher *)parent;
52
+ child->parent = (mongory_matcher *)composite;
38
53
  }
39
54
  }
55
+ composite->base.match = match_fn;
56
+ composite->base.original_match = match_fn;
57
+ composite->base.name = mongory_string_cpy(pool, (char *)name);
58
+ if (composite->base.name == NULL) {
59
+ return NULL;
60
+ }
61
+ composite->base.priority = base_priority + priority_scale * mongory_matcher_calculate_priority(sub_matchers);
62
+ return (mongory_matcher *)composite;
40
63
  }
41
64
 
42
- /**
43
- * @brief Allocates and initializes a `mongory_composite_matcher` structure.
44
- *
45
- * Initializes the base matcher part and sets child pointers (`left`, `right`)
46
- * to NULL. The specific `match` function and child matchers must be set by the
47
- * derived composite matcher's constructor.
48
- *
49
- * @param pool The memory pool for allocation.
50
- * @param condition The condition value for this composite matcher.
51
- * @return mongory_composite_matcher* Pointer to the new matcher, or NULL on
52
- * failure.
53
- */
54
65
  // ============================================================================
55
66
  // Core Composite Matcher Functions
56
67
  // ============================================================================
57
68
  mongory_composite_matcher *mongory_matcher_composite_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
58
- if (!pool || !pool->alloc)
59
- return NULL;
60
- if (!mongory_compile_budget_count_node(pool))
61
- return NULL;
62
-
63
- mongory_composite_matcher *composite = MG_ALLOC_PTR(pool, mongory_composite_matcher);
64
- if (composite == NULL) {
65
- pool->error = &MONGORY_ALLOC_ERROR;
66
- return NULL; // Allocation failed.
67
- }
68
- // Initialize base matcher fields
69
- composite->base.pool = pool;
70
- composite->base.name = NULL; // Specific name to be set by derived type if any
71
- composite->base.match = NULL; // Specific match fn to be set by derived type
72
- composite->base.explain = mongory_matcher_composite_explain; // Specific explain fn to be set by derived type
73
- composite->base.original_match = NULL;
74
- composite->base.sub_count = 0;
75
- composite->base.condition = condition;
76
- composite->base.traverse = mongory_matcher_composite_traverse;
77
- composite->base.extern_ctx = extern_ctx;
78
- composite->base.priority = 2.0;
79
- composite->base.parent = NULL;
80
- return composite;
69
+ return (mongory_composite_matcher *)mongory_matcher_alloc_init(
70
+ pool, sizeof(mongory_composite_matcher), condition, extern_ctx, NULL, mongory_matcher_composite_traverse,
71
+ NULL, 2.0);
81
72
  }
82
73
 
83
- /**
84
- * @brief Match function for an AND logical operation.
85
- *
86
- * Evaluates to true if both `left` and `right` child matchers (if they exist)
87
- * evaluate to true. If a child does not exist, it's considered true for this
88
- * operation.
89
- *
90
- * @param matcher Pointer to the composite AND matcher.
91
- * @param value The value to evaluate.
92
- * @return True if all child conditions are met, false otherwise.
93
- */
94
74
  // ============================================================================
95
- // Logical Operator Match Functions (AND, OR)
75
+ // Logical Operator Match Functions (AND, OR, NOR)
96
76
  // ============================================================================
97
77
  static inline bool mongory_matcher_and_match(mongory_matcher *matcher, mongory_value *value) {
78
+ if (!matcher) {
79
+ return false;
80
+ }
98
81
  mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
99
82
  mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
100
- int total = (int)children->count;
101
- for (int i = 0; i < total; i++) {
83
+ if (!children) {
84
+ return false;
85
+ }
86
+ for (size_t i = 0; i < children->count; i++) {
102
87
  mongory_matcher *child = (mongory_matcher *)children->get(children, i);
103
- if (!child->match(child, value)) {
88
+ if (!child || !child->match || !child->match(child, value)) {
104
89
  return false;
105
90
  }
106
91
  }
107
- return true; // Both matched (or didn't exist, which is fine for AND).
92
+ return true;
108
93
  }
109
94
 
110
- /**
111
- * @brief Match function for an OR logical operation.
112
- *
113
- * Evaluates to true if either the `left` or `right` child matcher (if they
114
- * exist) evaluates to true. If a child does not exist, it's considered false
115
- * for this operation.
116
- *
117
- * @param matcher Pointer to the composite OR matcher.
118
- * @param value The value to evaluate.
119
- * @return True if any child condition is met, false otherwise.
120
- */
121
- bool mongory_matcher_or_match(mongory_matcher *matcher, mongory_value *value) {
95
+ static inline bool mongory_matcher_disjunction_match(mongory_matcher *matcher, mongory_value *value, bool invert) {
96
+ if (!matcher) {
97
+ return false;
98
+ }
122
99
  mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
123
100
  mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
124
- int total = (int)children->count;
125
- for (int i = 0; i < total; i++) {
101
+ if (!children) {
102
+ return false;
103
+ }
104
+ for (size_t i = 0; i < children->count; i++) {
126
105
  mongory_matcher *child = (mongory_matcher *)children->get(children, i);
127
- if (child->match(child, value)) {
128
- return true;
106
+ if (child && child->match && child->match(child, value)) {
107
+ return !invert;
129
108
  }
130
109
  }
131
- return false; // Neither matched (or children didn't exist).
110
+ return invert;
111
+ }
112
+
113
+ bool mongory_matcher_or_match(mongory_matcher *matcher, mongory_value *value) {
114
+ return mongory_matcher_disjunction_match(matcher, value, false);
115
+ }
116
+
117
+ static inline bool mongory_matcher_nor_match(mongory_matcher *matcher, mongory_value *value) {
118
+ return mongory_matcher_disjunction_match(matcher, value, true);
119
+ }
120
+
121
+ mongory_matcher *mongory_matcher_and_of_new(mongory_memory_pool *pool, mongory_array *sub_matchers, mongory_value *condition,
122
+ void *extern_ctx) {
123
+ return mongory_matcher_composite_finalize(pool, sub_matchers, condition, extern_ctx, "And", mongory_matcher_and_match,
124
+ 2.0, 1.0);
125
+ }
126
+
127
+ // A record that cannot hold fields: a field path never resolves on it.
128
+ static inline bool mongory_value_is_field_container(mongory_value *value) {
129
+ return value != NULL && (value->type == MONGORY_TYPE_TABLE || value->type == MONGORY_TYPE_ARRAY);
130
+ }
131
+
132
+ static inline bool mongory_matcher_negation_match(mongory_matcher *matcher, mongory_value *value) {
133
+ if (!matcher) {
134
+ return false;
135
+ }
136
+ mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
137
+ mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
138
+ mongory_matcher *inner = children ? (mongory_matcher *)children->get(children, 0) : NULL;
139
+ return inner != NULL && !inner->match(inner, value);
140
+ }
141
+
142
+ static inline bool mongory_matcher_document_negation_match(mongory_matcher *matcher, mongory_value *value) {
143
+ return mongory_value_is_field_container(value) && mongory_matcher_negation_match(matcher, value);
144
+ }
145
+
146
+ mongory_matcher *mongory_matcher_negation_new(mongory_memory_pool *pool, mongory_matcher *inner, mongory_value *condition,
147
+ bool document_only, void *extern_ctx) {
148
+ if (!MONGORY_VALIDATE_PTR(pool, inner)) {
149
+ return NULL;
150
+ }
151
+ mongory_composite_matcher *composite = mongory_matcher_composite_new(pool, condition, extern_ctx);
152
+ if (composite == NULL) {
153
+ return NULL;
154
+ }
155
+ mongory_array *children = mongory_array_new(pool);
156
+ if (children == NULL || !children->push(children, (mongory_value *)inner)) {
157
+ return NULL;
158
+ }
159
+ composite->children = children;
160
+ inner->parent = (mongory_matcher *)composite;
161
+ mongory_matcher_match_func match_fn =
162
+ document_only ? mongory_matcher_document_negation_match : mongory_matcher_negation_match;
163
+ composite->base.match = match_fn;
164
+ composite->base.original_match = match_fn;
165
+ composite->base.name = mongory_string_cpy(pool, "Not");
166
+ if (composite->base.name == NULL) {
167
+ return NULL;
168
+ }
169
+ composite->base.priority += inner->priority;
170
+ return (mongory_matcher *)composite;
132
171
  }
133
172
 
134
173
  /**
@@ -212,7 +251,7 @@ static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mong
212
251
  build_func = mongory_matcher_build_func_get(key);
213
252
  if (build_func != NULL) {
214
253
  return build_func(pool, value, ctx->extern_ctx);
215
- } else if (mongory_custom_matcher_adapter.lookup != NULL && mongory_custom_matcher_adapter.lookup(key)) {
254
+ } else if (mongory_custom_matcher_adapter.lookup != NULL && mongory_custom_matcher_adapter.lookup(key, ctx->extern_ctx)) {
216
255
  return mongory_matcher_custom_new(pool, key, value, ctx->extern_ctx);
217
256
  }
218
257
  mongory_matcher_build_error_set(pool, mongory_string_cpyf(pool, "Unknown operator: %s", key));
@@ -246,7 +285,7 @@ mongory_matcher *mongory_matcher_condition_new(mongory_memory_pool *pool, mongor
246
285
  if (!MONGORY_VALIDATE_TABLE(pool, table_condition)) {
247
286
  return NULL;
248
287
  }
249
- if (!mongory_compile_budget_enter_depth(pool)) {
288
+ if (!mongory_compile_ctx_enter_depth(pool)) {
250
289
  return NULL;
251
290
  }
252
291
 
@@ -254,13 +293,13 @@ mongory_matcher *mongory_matcher_condition_new(mongory_memory_pool *pool, mongor
254
293
  if (table->count == 0) {
255
294
  // Empty table condition matches everything.
256
295
  mongory_matcher *res = mongory_matcher_always_true_new(pool, table_condition, extern_ctx);
257
- mongory_compile_budget_leave_depth();
296
+ mongory_compile_ctx_leave_depth();
258
297
  return res;
259
298
  }
260
299
 
261
300
  mongory_array *sub_matchers = mongory_array_new(pool);
262
301
  if (sub_matchers == NULL) {
263
- mongory_compile_budget_leave_depth();
302
+ mongory_compile_ctx_leave_depth();
264
303
  return NULL; // Failed to create array for sub-matchers.
265
304
  }
266
305
 
@@ -268,39 +307,20 @@ mongory_matcher *mongory_matcher_condition_new(mongory_memory_pool *pool, mongor
268
307
  // Iterate over the condition table, building sub-matchers.
269
308
  if (!table->each(table, &build_ctx, mongory_matcher_table_build_sub_matcher)) {
270
309
  // Building one of the sub-matchers failed.
271
- mongory_compile_budget_leave_depth();
310
+ mongory_compile_ctx_leave_depth();
272
311
  return NULL;
273
312
  }
274
313
 
275
314
  if (sub_matchers->count == 1) {
276
315
  mongory_matcher *res = (mongory_matcher *)sub_matchers->get(sub_matchers, 0);
277
- mongory_compile_budget_leave_depth();
316
+ mongory_compile_ctx_leave_depth();
278
317
  return res;
279
318
  }
280
319
 
281
320
  // Combine sub-matchers using AND logic.
282
- mongory_composite_matcher *final_matcher = mongory_matcher_composite_new(pool, table_condition, extern_ctx);
283
- if (final_matcher == NULL) {
284
- mongory_compile_budget_leave_depth();
285
- return NULL;
286
- }
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);
293
- final_matcher->base.match = mongory_matcher_and_match;
294
- final_matcher->base.original_match = mongory_matcher_and_match;
295
- final_matcher->base.sub_count = sub_matchers->count;
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
- }
301
- final_matcher->base.priority += mongory_matcher_calculate_priority(sub_matchers);
302
- mongory_compile_budget_leave_depth();
303
- return (mongory_matcher *)final_matcher;
321
+ mongory_matcher *res = mongory_matcher_and_of_new(pool, sub_matchers, table_condition, extern_ctx);
322
+ mongory_compile_ctx_leave_depth();
323
+ return res;
304
324
  }
305
325
 
306
326
  /**
@@ -337,382 +357,188 @@ static inline bool mongory_matcher_build_and_sub_matcher(mongory_value *and_sub_
337
357
  * @return A new $and matcher, or NULL on failure.
338
358
  */
339
359
  mongory_matcher *mongory_matcher_and_new(mongory_memory_pool *pool, mongory_value *and_condition, void *extern_ctx) {
340
- if (!MONGORY_VALIDATE_ARRAY(pool, and_condition)) {
341
- return NULL;
342
- }
343
- if (!mongory_compile_budget_enter_depth(pool)) {
360
+ if (!MONGORY_VALIDATE_ARRAY(pool, and_condition) || !mongory_compile_ctx_enter_depth(pool)) {
344
361
  return NULL;
345
362
  }
346
363
 
364
+ mongory_matcher *res = NULL;
347
365
  mongory_array *array_of_tables = and_condition->data.a;
348
366
  if (array_of_tables->count == 0) {
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;
352
- }
353
- mongory_value *sub_condition_of_and_condition = array_of_tables->get(array_of_tables, 0);
354
-
355
- if (!MONGORY_VALIDATE_TABLE(pool, sub_condition_of_and_condition)) {
356
- mongory_compile_budget_leave_depth();
357
- return NULL;
367
+ res = mongory_matcher_always_true_new(pool, and_condition, extern_ctx);
368
+ goto done;
358
369
  }
359
370
 
360
371
  mongory_array *sub_matchers = mongory_array_new(pool);
361
372
  if (sub_matchers == NULL) {
362
- mongory_compile_budget_leave_depth();
363
- return NULL;
373
+ goto done;
364
374
  }
365
375
 
366
- // Context for building matchers from EACH table within the $and array.
367
376
  mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
368
- // Iterate through the array of tables provided in the $and condition.
369
- // mongory_matcher_build_and_sub_matcher will then iterate keys of EACH table.
370
- int total = (int)array_of_tables->count;
371
- for (int i = 0; i < total; i++) {
372
- mongory_value *table = array_of_tables->get(array_of_tables, i);
373
- if (!mongory_matcher_build_and_sub_matcher(table, &build_ctx)) {
374
- mongory_compile_budget_leave_depth();
375
- return NULL; // Failure during sub-matcher construction.
377
+ for (size_t i = 0; i < array_of_tables->count; i++) {
378
+ if (!mongory_matcher_build_and_sub_matcher(array_of_tables->get(array_of_tables, i), &build_ctx)) {
379
+ goto done;
376
380
  }
377
381
  }
378
382
 
379
383
  if (sub_matchers->count == 0) {
380
- mongory_matcher *res = mongory_matcher_always_true_new(pool, and_condition, extern_ctx);
381
- mongory_compile_budget_leave_depth();
382
- return res;
384
+ res = mongory_matcher_always_true_new(pool, and_condition, extern_ctx);
385
+ } else if (sub_matchers->count == 1) {
386
+ res = (mongory_matcher *)sub_matchers->get(sub_matchers, 0);
387
+ } else {
388
+ res = mongory_matcher_and_of_new(pool, sub_matchers, and_condition, extern_ctx);
383
389
  }
384
390
 
385
- if (sub_matchers->count == 1) {
386
- mongory_matcher *res = (mongory_matcher *)sub_matchers->get(sub_matchers, 0);
387
- mongory_compile_budget_leave_depth();
388
- return res;
389
- }
390
-
391
- mongory_composite_matcher *final_matcher = mongory_matcher_composite_new(pool, and_condition, extern_ctx);
392
- if (final_matcher == NULL) {
393
- mongory_compile_budget_leave_depth();
394
- return NULL;
395
- }
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);
402
- final_matcher->base.match = mongory_matcher_and_match;
403
- final_matcher->base.original_match = mongory_matcher_and_match;
404
- final_matcher->base.sub_count = sub_matchers->count;
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
- }
410
- final_matcher->base.priority += mongory_matcher_calculate_priority(sub_matchers);
411
- mongory_compile_budget_leave_depth();
412
- return (mongory_matcher *)final_matcher;
413
- }
414
-
415
- /**
416
- * @brief Callback for $or constructor. Each element in the $or array is a
417
- * complete table condition, which is turned into a single matcher. These
418
- * "table condition matchers" are then ORed.
419
- * @param condition_table A `mongory_value` (table) from the $or array.
420
- * @param acc Pointer to `mongory_matcher_table_build_sub_matcher_context`.
421
- * The `matchers` array in context will store the result of
422
- * `mongory_matcher_condition_new`.
423
- * @return True if successful, false otherwise.
424
- */
425
- static inline bool mongory_matcher_build_or_sub_matcher(mongory_value *condition_table, void *acc) {
426
- mongory_matcher_table_build_sub_matcher_context *ctx = (mongory_matcher_table_build_sub_matcher_context *)acc;
427
- mongory_memory_pool *pool_for_new_matchers = ctx->pool;
428
- mongory_array *array_to_store_table_matchers = ctx->matchers;
429
-
430
- // Each 'condition_table' is a complete query object for one branch of the OR.
431
- // So, create a full table_cond_new matcher for it.
432
- mongory_matcher *table_level_matcher = mongory_matcher_condition_new(pool_for_new_matchers, condition_table, ctx->extern_ctx);
433
- if (table_level_matcher == NULL) {
434
- return false; // Failed to create a matcher for this OR branch.
435
- }
436
- if (!array_to_store_table_matchers->push(array_to_store_table_matchers, (mongory_value *)table_level_matcher)) {
437
- return false;
438
- }
439
- return true;
391
+ done:
392
+ mongory_compile_ctx_leave_depth();
393
+ return res;
440
394
  }
441
395
 
442
- /**
443
- * @brief Creates an "OR" ($or) matcher from an array of condition tables.
444
- * @param pool Memory pool for allocations.
445
- *
446
- * The `$or` operator takes an array of query documents. For each document in
447
- * the array, this function creates a complete sub-matcher (using
448
- * `table_cond_new`). It then combines these top-level sub-matchers into an
449
- * OR-connected tree. This is different from `$and`, which flattens the list.
450
- *
451
- * @param pool Memory pool for allocations.
452
- * @param condition A `mongory_value` array of table conditions.
453
- * @return A new $or matcher, or NULL on failure.
454
- */
455
- mongory_matcher *mongory_matcher_or_new(mongory_memory_pool *pool, mongory_value *or_condition, void *extern_ctx) {
456
- if (!MONGORY_VALIDATE_ARRAY(pool, or_condition)) {
457
- return NULL;
458
- }
459
- if (!mongory_compile_budget_enter_depth(pool)) {
396
+ static mongory_matcher *mongory_matcher_branch_list_new(mongory_memory_pool *pool, mongory_value *condition,
397
+ void *extern_ctx, const char *name,
398
+ mongory_matcher_match_func match_fn, bool empty_is_true,
399
+ bool unwrap_single) {
400
+ if (!MONGORY_VALIDATE_ARRAY(pool, condition) || !mongory_compile_ctx_enter_depth(pool)) {
460
401
  return NULL;
461
402
  }
462
- mongory_array *array_of_tables = or_condition->data.a;
403
+
404
+ mongory_matcher *res = NULL;
405
+ mongory_array *array_of_tables = condition->data.a;
463
406
  if (array_of_tables->count == 0) {
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;
467
- }
468
- mongory_value *sub_condition_of_or_condition = array_of_tables->get(array_of_tables, 0);
469
- if (!MONGORY_VALIDATE_TABLE(pool, sub_condition_of_or_condition)) {
470
- mongory_compile_budget_leave_depth();
471
- return NULL;
407
+ res = empty_is_true ? mongory_matcher_always_true_new(pool, condition, extern_ctx)
408
+ : mongory_matcher_always_false_new(pool, condition, extern_ctx);
409
+ goto done;
472
410
  }
473
411
 
474
412
  mongory_array *sub_matchers = mongory_array_new(pool);
475
413
  if (sub_matchers == NULL) {
476
- mongory_compile_budget_leave_depth();
477
- return NULL;
414
+ goto done;
478
415
  }
479
416
 
480
- mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
481
- int total = (int)array_of_tables->count;
482
- for (int i = 0; i < total; i++) {
483
- mongory_value *table = array_of_tables->get(array_of_tables, i);
484
- if (!mongory_matcher_build_or_sub_matcher(table, &build_ctx)) {
485
- mongory_compile_budget_leave_depth();
486
- return NULL; // Failure building one of the OR branches
417
+ for (size_t i = 0; i < array_of_tables->count; i++) {
418
+ mongory_matcher *branch =
419
+ mongory_matcher_condition_new(pool, array_of_tables->get(array_of_tables, i), extern_ctx);
420
+ if (branch == NULL || !sub_matchers->push(sub_matchers, (mongory_value *)branch)) {
421
+ goto done;
487
422
  }
488
423
  }
489
424
 
490
- if (sub_matchers->count == 1) {
491
- mongory_matcher *res = (mongory_matcher *)sub_matchers->get(sub_matchers, 0);
492
- mongory_compile_budget_leave_depth();
493
- return res;
425
+ if (unwrap_single && sub_matchers->count == 1) {
426
+ res = (mongory_matcher *)sub_matchers->get(sub_matchers, 0);
427
+ } else {
428
+ res = mongory_matcher_composite_finalize(pool, sub_matchers, condition, extern_ctx, name, match_fn, 2.0, 1.0);
494
429
  }
495
430
 
496
- mongory_composite_matcher *final_matcher = mongory_matcher_composite_new(pool, or_condition, extern_ctx);
497
- if (final_matcher == NULL) {
498
- mongory_compile_budget_leave_depth();
499
- return NULL;
500
- }
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);
507
- final_matcher->base.match = mongory_matcher_or_match;
508
- final_matcher->base.original_match = mongory_matcher_or_match;
509
- final_matcher->base.sub_count = sub_matchers->count;
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
- }
515
- final_matcher->base.priority += mongory_matcher_calculate_priority(sub_matchers);
516
- mongory_compile_budget_leave_depth();
517
- return (mongory_matcher *)final_matcher;
431
+ done:
432
+ mongory_compile_ctx_leave_depth();
433
+ return res;
434
+ }
435
+
436
+ mongory_matcher *mongory_matcher_or_new(mongory_memory_pool *pool, mongory_value *or_condition, void *extern_ctx) {
437
+ return mongory_matcher_branch_list_new(pool, or_condition, extern_ctx, "Or", mongory_matcher_or_match, false, true);
438
+ }
439
+
440
+ mongory_matcher *mongory_matcher_nor_new(mongory_memory_pool *pool, mongory_value *nor_condition, void *extern_ctx) {
441
+ return mongory_matcher_branch_list_new(pool, nor_condition, extern_ctx, "Nor", mongory_matcher_nor_match, true, false);
518
442
  }
519
443
 
520
- /**
521
- * @brief Match function for $elemMatch.
522
- * Checks if any element in the input array `value` matches the condition
523
- * stored in `composite->children`.
524
- * @param matcher The $elemMatch composite matcher.
525
- * @param value_to_check The input value, expected to be an array.
526
- * @return True if `value_to_check` is an array and at least one of its elements
527
- * matches.
528
- */
529
444
  // ============================================================================
530
445
  // Array-based Match Functions ($elemMatch, $every)
531
446
  // ============================================================================
532
- static inline bool mongory_matcher_elem_match_match(mongory_matcher *matcher, mongory_value *elem_match_target) {
533
- if (elem_match_target == NULL || elem_match_target->type != MONGORY_TYPE_ARRAY) {
534
- return false; // $elemMatch applies to arrays.
535
- }
536
- mongory_array *target_array = elem_match_target->data.a;
537
- if (target_array->count == 0)
538
- return false; // Empty array cannot have a matching element.
539
-
540
- int total = (int)target_array->count;
541
- for (int i = 0; i < total; i++) {
542
- mongory_value *value = target_array->get(target_array, i);
543
- if (mongory_matcher_and_match(matcher, value)) {
544
- return true;
447
+ static inline bool mongory_matcher_array_elem_match(mongory_matcher *matcher, mongory_value *target, bool stop_on) {
448
+ if (!matcher || target == NULL || target->type != MONGORY_TYPE_ARRAY || target->data.a == NULL ||
449
+ target->data.a->count == 0) {
450
+ return false;
451
+ }
452
+ mongory_array *target_array = target->data.a;
453
+ for (size_t i = 0; i < target_array->count; i++) {
454
+ if (mongory_matcher_and_match(matcher, target_array->get(target_array, i)) == stop_on) {
455
+ return stop_on;
545
456
  }
546
457
  }
547
- return false;
458
+ return !stop_on;
548
459
  }
549
460
 
550
- /**
551
- * @brief Creates an $elemMatch matcher.
552
- * The `condition` (a table) is used to create a sub-matcher
553
- * (`composite->left`) which is then applied to each element of an input array.
554
- * @param pool Memory pool for allocations.
555
- * @param condition The table condition for matching array elements.
556
- * @return A new $elemMatch matcher, or NULL on failure.
557
- */
558
- mongory_matcher *mongory_matcher_elem_match_new(mongory_memory_pool *pool, mongory_value *elem_match_condition, void *extern_ctx) {
559
- if (!MONGORY_VALIDATE_TABLE(pool, elem_match_condition)) {
560
- return NULL;
561
- }
562
- if (!mongory_compile_budget_enter_depth(pool)) {
563
- return NULL;
564
- }
565
- mongory_array *sub_matchers = mongory_array_new(pool);
566
- if (sub_matchers == NULL) {
567
- mongory_compile_budget_leave_depth();
568
- return NULL;
569
- }
570
- mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
571
- if (!mongory_matcher_build_and_sub_matcher(elem_match_condition, &build_ctx)) {
572
- mongory_compile_budget_leave_depth();
573
- return NULL;
574
- }
575
-
576
- mongory_composite_matcher *composite = mongory_matcher_composite_new(pool, elem_match_condition, extern_ctx);
577
- if (composite == NULL) {
578
- mongory_compile_budget_leave_depth();
579
- return NULL;
580
- }
581
-
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);
588
- composite->base.match = mongory_matcher_elem_match_match;
589
- composite->base.original_match = mongory_matcher_elem_match_match;
590
- composite->base.sub_count = sub_matchers->count;
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
- }
596
- composite->base.priority = 3.0 + mongory_matcher_calculate_priority(sub_matchers);
597
- mongory_compile_budget_leave_depth();
598
- return (mongory_matcher *)composite;
461
+ static inline bool mongory_matcher_elem_match_match(mongory_matcher *matcher, mongory_value *elem_match_target) {
462
+ return mongory_matcher_array_elem_match(matcher, elem_match_target, true);
599
463
  }
600
464
 
601
- /**
602
- * @brief Match function for $every.
603
- * Checks if all elements in the input array `value` match the condition
604
- * stored in `composite->children`.
605
- * @param matcher The $every composite matcher.
606
- * @param value_to_check The input value, expected to be an array.
607
- * @return True if `value_to_check` is an array and all its elements match (or
608
- * if array is empty).
609
- */
610
465
  static inline bool mongory_matcher_every_match(mongory_matcher *matcher, mongory_value *every_match_target) {
611
- if (every_match_target == NULL || every_match_target->type != MONGORY_TYPE_ARRAY) {
612
- return false;
613
- }
614
- mongory_array *target_array = every_match_target->data.a;
615
- if (target_array->count == 0)
616
- return false; // Non-empty array must have at least one element.
617
-
618
- int total = (int)target_array->count;
619
- for (int i = 0; i < total; i++) {
620
- mongory_value *value = target_array->get(target_array, i);
621
- if (!mongory_matcher_and_match(matcher, value)) {
622
- return false;
623
- }
624
- }
625
- return true;
466
+ return mongory_matcher_array_elem_match(matcher, every_match_target, false);
626
467
  }
627
468
 
628
- /**
629
- * @brief Creates an $every matcher.
630
- * The `condition` (a table) is used to create a sub-matcher
631
- * (`composite->left`) which is then applied to each element of an input array.
632
- * @param pool Memory pool for allocations.
633
- * @param condition The table condition for matching array elements.
634
- * @return A new $every matcher, or NULL on failure.
635
- */
636
- mongory_matcher *mongory_matcher_every_new(mongory_memory_pool *pool, mongory_value *every_condition, void *extern_ctx) {
637
- if (!MONGORY_VALIDATE_TABLE(pool, every_condition)) {
638
- return NULL;
639
- }
640
- if (!mongory_compile_budget_enter_depth(pool)) {
469
+ static mongory_matcher *mongory_matcher_array_elem_new(mongory_memory_pool *pool, mongory_value *condition,
470
+ void *extern_ctx, const char *name,
471
+ mongory_matcher_match_func match_fn) {
472
+ if (!MONGORY_VALIDATE_TABLE(pool, condition) || !mongory_compile_ctx_enter_depth(pool)) {
641
473
  return NULL;
642
474
  }
475
+
476
+ mongory_matcher *res = NULL;
643
477
  mongory_array *sub_matchers = mongory_array_new(pool);
644
478
  if (sub_matchers == NULL) {
645
- mongory_compile_budget_leave_depth();
646
- return NULL;
479
+ goto done;
647
480
  }
481
+
648
482
  mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
649
- if (!mongory_matcher_build_and_sub_matcher(every_condition, &build_ctx)) {
650
- mongory_compile_budget_leave_depth();
651
- return NULL;
483
+ size_t saved_field_depth = mongory_compile_ctx_reset_field();
484
+ bool built = mongory_matcher_build_and_sub_matcher(condition, &build_ctx);
485
+ mongory_compile_ctx_restore_field(saved_field_depth);
486
+ if (!built) {
487
+ goto done;
652
488
  }
653
489
 
654
- mongory_composite_matcher *composite = mongory_matcher_composite_new(pool, every_condition, extern_ctx);
655
- if (composite == NULL) {
656
- mongory_compile_budget_leave_depth();
657
- return NULL;
658
- }
490
+ res = mongory_matcher_composite_finalize(pool, sub_matchers, condition, extern_ctx, name, match_fn, 8.0, 2.0);
659
491
 
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);
666
- composite->base.match = mongory_matcher_every_match;
667
- composite->base.original_match = mongory_matcher_every_match;
668
- composite->base.sub_count = sub_matchers->count;
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
- }
674
- composite->base.priority = 3.0 + mongory_matcher_calculate_priority(sub_matchers);
675
- mongory_compile_budget_leave_depth();
676
- return (mongory_matcher *)composite;
492
+ done:
493
+ mongory_compile_ctx_leave_depth();
494
+ return res;
677
495
  }
678
496
 
679
- // ============================================================================
680
- // Helper Functions
681
- // ============================================================================
682
-
683
- /**
684
- * @brief Calculates the priority of a composite matcher.
685
- * @param sub_matchers The array of sub-matchers.
686
- * @return The priority of the composite matcher.
687
- */
688
- double mongory_matcher_calculate_priority(mongory_array *sub_matchers) {
689
- double priority = 0.0;
690
- for (size_t i = 0; i < sub_matchers->count; i++) {
691
- priority += ((mongory_matcher *)sub_matchers->get(sub_matchers, i))->priority;
692
- }
693
- return priority;
497
+ mongory_matcher *mongory_matcher_elem_match_new(mongory_memory_pool *pool, mongory_value *elem_match_condition,
498
+ void *extern_ctx) {
499
+ return mongory_matcher_array_elem_new(pool, elem_match_condition, extern_ctx, "ElemMatch",
500
+ mongory_matcher_elem_match_match);
694
501
  }
695
502
 
696
- static inline size_t mongory_matcher_calc_priority(mongory_value *matcher_value, void *ctx) {
697
- (void)ctx;
698
- mongory_matcher *matcher = (mongory_matcher *)matcher_value;
699
- return (size_t)(matcher->priority * 10000);
503
+ mongory_matcher *mongory_matcher_every_new(mongory_memory_pool *pool, mongory_value *every_condition, void *extern_ctx) {
504
+ return mongory_matcher_array_elem_new(pool, every_condition, extern_ctx, "Every", mongory_matcher_every_match);
700
505
  }
701
506
 
702
- /**
703
- * @brief Sorts the sub-matchers by priority.
704
- * @param sub_matchers The array of sub-matchers.
705
- * @return The sorted array of sub-matchers.
706
- */
707
- mongory_array *mongory_matcher_sort_matchers(mongory_array *sub_matchers) {
708
- mongory_memory_pool *temp_pool = mongory_memory_pool_new();
709
- if (temp_pool == NULL) {
710
- sub_matchers->pool->error = &MONGORY_ALLOC_ERROR;
507
+ // ============================================================================
508
+ // Helper Functions
509
+ // ============================================================================
510
+ static mongory_array *mongory_matcher_sort_matchers(mongory_array *sub_matchers) {
511
+ if (!sub_matchers || !sub_matchers->pool) {
512
+ return NULL;
513
+ }
514
+ if (sub_matchers->count <= 1) {
515
+ return sub_matchers;
516
+ }
517
+ mongory_array *sorted = mongory_array_new(sub_matchers->pool);
518
+ if (sorted == NULL) {
711
519
  return NULL;
712
520
  }
713
- mongory_array *sorted_matchers = mongory_array_sort_by(sub_matchers, temp_pool, NULL, mongory_matcher_calc_priority);
714
- temp_pool->free(temp_pool);
715
- return sorted_matchers;
521
+ for (size_t i = 0; i < sub_matchers->count; i++) {
522
+ mongory_value *item = sub_matchers->get(sub_matchers, i);
523
+ if (!sorted->push(sorted, item)) {
524
+ return NULL;
525
+ }
526
+ size_t key_prio = (size_t)(((mongory_matcher *)item)->priority * 10000);
527
+ size_t j = i;
528
+ while (j > 0) {
529
+ mongory_value *prev = sorted->get(sorted, j - 1);
530
+ size_t prev_prio = (size_t)(((mongory_matcher *)prev)->priority * 10000);
531
+ if (prev_prio <= key_prio) {
532
+ break;
533
+ }
534
+ sorted->set(sorted, j, prev);
535
+ j--;
536
+ }
537
+ if (j != i) {
538
+ sorted->set(sorted, j, item);
539
+ }
540
+ }
541
+ return sorted;
716
542
  }
717
543
 
718
544
  void mongory_matcher_resort_composite(mongory_composite_matcher *composite) {