mongory 2.1.1-x86_64-linux → 2.2.0-x86_64-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 (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -0
  3. data/README.md +45 -74
  4. data/docs/clang_bridge.md +2 -2
  5. data/docs/performance.md +28 -1
  6. data/lib/core/2.6/mongory_ext.so +0 -0
  7. data/lib/core/2.7/mongory_ext.so +0 -0
  8. data/lib/core/3.0/mongory_ext.so +0 -0
  9. data/lib/core/3.1/mongory_ext.so +0 -0
  10. data/lib/core/3.2/mongory_ext.so +0 -0
  11. data/lib/core/3.3/mongory_ext.so +0 -0
  12. data/lib/core/3.4/mongory_ext.so +0 -0
  13. data/lib/core/4.0/mongory_ext.so +0 -0
  14. data/lib/mongory/converters/abstract_converter.rb +5 -5
  15. data/lib/mongory/converters/converted.rb +0 -4
  16. data/lib/mongory/converters/key_converter.rb +13 -36
  17. data/lib/mongory/converters/value_converter.rb +4 -4
  18. data/lib/mongory/matchers.rb +8 -7
  19. data/lib/mongory/query_builder.rb +9 -0
  20. data/lib/mongory/utils/context.rb +0 -6
  21. data/lib/mongory/utils/singleton_builder.rb +1 -4
  22. data/lib/mongory/version.rb +1 -1
  23. data/lib/mongory.rb +0 -1
  24. data/mongory.gemspec +13 -12
  25. data/script/coverage.rb +84 -0
  26. metadata +9 -54
  27. data/ext/mongory_ext/extconf.rb +0 -115
  28. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +0 -62
  29. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +0 -122
  30. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +0 -217
  31. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +0 -82
  32. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -115
  33. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +0 -127
  34. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +0 -175
  35. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +0 -103
  36. data/ext/mongory_ext/mongory-core/include/mongory-core.h +0 -12
  37. data/ext/mongory_ext/mongory-core/src/foundations/array.c +0 -336
  38. data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +0 -20
  39. data/ext/mongory_ext/mongory-core/src/foundations/config.c +0 -335
  40. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +0 -49
  41. data/ext/mongory_ext/mongory-core/src/foundations/error.c +0 -48
  42. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +0 -406
  43. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +0 -45
  44. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +0 -117
  45. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +0 -43
  46. data/ext/mongory_ext/mongory-core/src/foundations/table.c +0 -502
  47. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +0 -206
  48. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +0 -77
  49. data/ext/mongory_ext/mongory-core/src/foundations/value.c +0 -548
  50. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +0 -193
  51. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +0 -117
  52. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +0 -241
  53. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +0 -83
  54. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +0 -730
  55. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +0 -131
  56. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +0 -158
  57. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.h +0 -48
  58. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +0 -199
  59. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +0 -58
  60. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +0 -204
  61. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.h +0 -47
  62. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +0 -483
  63. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +0 -46
  64. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +0 -529
  65. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
  66. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
  67. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +0 -61
  68. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +0 -23
  69. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +0 -416
  70. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +0 -97
  71. data/ext/mongory_ext/mongory_ext.c +0 -66
  72. data/ext/mongory_ext/mongory_ext_internal.h +0 -74
  73. data/ext/mongory_ext/rb_mongory_adapters.c +0 -140
  74. data/ext/mongory_ext/rb_mongory_matcher.c +0 -440
  75. data/ext/mongory_ext/rb_mongory_value.c +0 -327
@@ -1,730 +0,0 @@
1
- /**
2
- * @file composite_matcher.c
3
- * @brief Implements composite matchers like AND, OR, $elemMatch, and the
4
- * core table condition parser. This is an internal implementation file for the
5
- * matcher module.
6
- */
7
- #include "composite_matcher.h"
8
- #include "external_matcher.h"
9
- #include "../foundations/config_private.h" // For mongory_matcher_build_func_get
10
- #include "../foundations/array_private.h" // For mongory_array_sort_by
11
- #include "../foundations/memory_pool_private.h" // For atomic children load/store
12
- #include "../foundations/string_buffer.h" // For mongory_string_buffer_new
13
- #include "base_matcher.h" // For mongory_matcher_always_true_new, etc.
14
- #include "value_matcher.h" // For mongory_matcher_field_new
15
- #include "mongory-core/foundations/error.h" // For MONGORY_ERROR_INVALID_ARGUMENT
16
- #include "mongory-core/foundations/memory_pool.h"
17
- #include "mongory-core/foundations/table.h" // For mongory_table operations
18
- #include "mongory-core/foundations/value.h"
19
- #include "matcher_explainable.h"
20
- #include "matcher_traversable.h"
21
- #include "../foundations/utils.h"
22
- #include <mongory-core.h> // General include
23
- #include <stdio.h> // For sprintf
24
- #include <string.h> // For strcmp
25
-
26
- // Forward declaration.
27
- double mongory_matcher_calculate_priority(mongory_array *sub_matchers);
28
- mongory_array *mongory_matcher_sort_matchers(mongory_array *sub_matchers);
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
-
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
- // ============================================================================
55
- // Core Composite Matcher Functions
56
- // ============================================================================
57
- 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;
81
- }
82
-
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
- // ============================================================================
95
- // Logical Operator Match Functions (AND, OR)
96
- // ============================================================================
97
- static inline bool mongory_matcher_and_match(mongory_matcher *matcher, mongory_value *value) {
98
- mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
99
- mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
100
- int total = (int)children->count;
101
- for (int i = 0; i < total; i++) {
102
- mongory_matcher *child = (mongory_matcher *)children->get(children, i);
103
- if (!child->match(child, value)) {
104
- return false;
105
- }
106
- }
107
- return true; // Both matched (or didn't exist, which is fine for AND).
108
- }
109
-
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) {
122
- mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
123
- mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
124
- int total = (int)children->count;
125
- for (int i = 0; i < total; i++) {
126
- mongory_matcher *child = (mongory_matcher *)children->get(children, i);
127
- if (child->match(child, value)) {
128
- return true;
129
- }
130
- }
131
- return false; // Neither matched (or children didn't exist).
132
- }
133
-
134
- /**
135
- * @brief Context structure for building sub-matchers from a table.
136
- */
137
- typedef struct mongory_matcher_table_build_sub_matcher_context {
138
- mongory_memory_pool *pool; /**< Main pool for allocating created matchers. */
139
- mongory_array *matchers; /**< Array to store the created sub-matchers. */
140
- void *extern_ctx; /**< External context for the matcher. */
141
- mongory_table *table; /**< Table being iterated, for sibling lookups ($regex/$options). */
142
- } mongory_matcher_table_build_sub_matcher_context;
143
-
144
- static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mongory_value *value, mongory_matcher_table_build_sub_matcher_context *ctx);
145
-
146
- static inline void mongory_matcher_build_error_set(mongory_memory_pool *pool, const char *message) {
147
- mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, message);
148
- }
149
-
150
- /**
151
- * @brief Callback for iterating over a condition table's key-value pairs.
152
- *
153
- * For each pair, it creates an appropriate sub-matcher:
154
- * - If key starts with '$', it looks up a registered matcher builder.
155
- * - Otherwise, it creates a field matcher (`mongory_matcher_field_new`).
156
- * The created sub-matcher is added to the `matchers` array in the context.
157
- *
158
- * @param key The key from the condition table.
159
- * @param value The value associated with the key.
160
- * @param acc Pointer to `mongory_matcher_table_build_sub_matcher_context`.
161
- * @return True to continue iteration, false if a sub-matcher creation fails.
162
- */
163
- static inline bool mongory_matcher_table_build_sub_matcher(char *key, mongory_value *value, void *acc) {
164
- mongory_matcher_table_build_sub_matcher_context *ctx = (mongory_matcher_table_build_sub_matcher_context *)acc;
165
- mongory_array *matchers_array = ctx->matchers;
166
- if (strcmp(key, "$options") == 0) {
167
- // Consumed by the sibling $regex; only its presence is checked here.
168
- if (ctx->table == NULL || ctx->table->get(ctx->table, "$regex") == NULL) {
169
- mongory_matcher_build_error_set(ctx->pool, "$options requires a sibling $regex.");
170
- return false;
171
- }
172
- return true;
173
- }
174
- mongory_matcher *sub_matcher = mongory_matcher_build_sub_matcher(key, value, ctx);
175
- if (sub_matcher == NULL) {
176
- // Failed to create sub-matcher (e.g., allocation error, invalid condition
177
- // for sub-matcher)
178
- return false;
179
- }
180
-
181
- if (!matchers_array->push(matchers_array, (mongory_value *)sub_matcher)) {
182
- return false;
183
- }
184
- return true;
185
- }
186
-
187
- /**
188
- * @brief Builds a sub-matcher from a key-value pair.
189
- *
190
- * This function determines the appropriate sub-matcher type based on the key:
191
- * - If the key starts with '$', it looks up a registered matcher builder
192
- * (`$regex` also takes its sibling `$options`). Unknown operators fail.
193
- * - Otherwise, it creates a field matcher.
194
- *
195
- * @param key The key from the condition table.
196
- * @param value The value associated with the key.
197
- * @param ctx Pointer to `mongory_matcher_table_build_sub_matcher_context`.
198
- * @return A new sub-matcher, or NULL on failure.
199
- */
200
- // ============================================================================
201
- // Matcher Construction from Conditions
202
- // ============================================================================
203
- static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mongory_value *value, mongory_matcher_table_build_sub_matcher_context *ctx) {
204
- mongory_memory_pool *pool = ctx->pool;
205
- mongory_matcher_build_func build_func = NULL;
206
-
207
- if (key[0] == '$') { // Operator key (e.g., "$eq", "$in")
208
- if (strcmp(key, "$regex") == 0) {
209
- mongory_value *options = ctx->table ? ctx->table->get(ctx->table, "$options") : NULL;
210
- return mongory_matcher_regex_with_options_new(pool, value, options, ctx->extern_ctx);
211
- }
212
- build_func = mongory_matcher_build_func_get(key);
213
- if (build_func != NULL) {
214
- return build_func(pool, value, ctx->extern_ctx);
215
- } else if (mongory_custom_matcher_adapter.lookup != NULL && mongory_custom_matcher_adapter.lookup(key)) {
216
- return mongory_matcher_custom_new(pool, key, value, ctx->extern_ctx);
217
- }
218
- mongory_matcher_build_error_set(pool, mongory_string_cpyf(pool, "Unknown operator: %s", key));
219
- return NULL;
220
- }
221
-
222
- return mongory_matcher_field_new(pool, key, value, ctx->extern_ctx);
223
- }
224
-
225
- /**
226
- * @brief Creates a matcher from a table-based condition.
227
- *
228
- * Parses the `condition` table, creating sub-matchers for each key-value pair.
229
- *
230
- * This is a core function of the query engine. It takes a query document (a
231
- * table) and builds a tree of matchers that represents the logic of that query.
232
- *
233
- * The process is as follows:
234
- * 1. Iterate through each key-value pair in the `condition` table.
235
- * 2. For each pair, create a specific sub-matcher (e.g., a `field_matcher` for
236
- * a field name, or a `$gt` matcher for a `"$gt"` operator).
237
- * 3. Store all these sub-matchers in a temporary array.
238
- * 4. Use `mongory_matcher_binary_construct` to combine all the sub-matchers
239
- * into a single matcher tree using AND logic.
240
- *
241
- * @param pool Memory pool for allocations.
242
- * @param condition A `mongory_value` of type `MONGORY_TYPE_TABLE`.
243
- * @return A `mongory_matcher` representing the combined logic of the table, or NULL on failure.
244
- */
245
- mongory_matcher *mongory_matcher_condition_new(mongory_memory_pool *pool, mongory_value *table_condition, void *extern_ctx) {
246
- if (!MONGORY_VALIDATE_TABLE(pool, table_condition)) {
247
- return NULL;
248
- }
249
- if (!mongory_compile_budget_enter_depth(pool)) {
250
- return NULL;
251
- }
252
-
253
- mongory_table *table = table_condition->data.t;
254
- if (table->count == 0) {
255
- // Empty table condition matches everything.
256
- mongory_matcher *res = mongory_matcher_always_true_new(pool, table_condition, extern_ctx);
257
- mongory_compile_budget_leave_depth();
258
- return res;
259
- }
260
-
261
- mongory_array *sub_matchers = mongory_array_new(pool);
262
- if (sub_matchers == NULL) {
263
- mongory_compile_budget_leave_depth();
264
- return NULL; // Failed to create array for sub-matchers.
265
- }
266
-
267
- mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, table};
268
- // Iterate over the condition table, building sub-matchers.
269
- if (!table->each(table, &build_ctx, mongory_matcher_table_build_sub_matcher)) {
270
- // Building one of the sub-matchers failed.
271
- mongory_compile_budget_leave_depth();
272
- return NULL;
273
- }
274
-
275
- if (sub_matchers->count == 1) {
276
- mongory_matcher *res = (mongory_matcher *)sub_matchers->get(sub_matchers, 0);
277
- mongory_compile_budget_leave_depth();
278
- return res;
279
- }
280
-
281
- // 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;
304
- }
305
-
306
- /**
307
- * @brief Callback for $and constructor to build sub-matchers from each table
308
- * in the condition array. This is a bit complex: each element of the $and array
309
- * is a table, and each key-value in THAT table becomes a sub-matcher. These
310
- * are then ANDed together.
311
- * @param condition_table A `mongory_value` (table) from the $and array.
312
- * @param acc Pointer to `mongory_matcher_table_build_sub_matcher_context`.
313
- * @return Result of iterating through `condition_table`.
314
- */
315
- static inline bool mongory_matcher_build_and_sub_matcher(mongory_value *and_sub_condition, void *acc) {
316
- // The 'and_sub_condition' is one of the tables in the $and:[{}, {}, ...] array.
317
- // We need to build all matchers from this table and add them to the list.
318
- // The list in 'acc' (ctx->matchers) will then be ANDed together.
319
- mongory_matcher_table_build_sub_matcher_context *ctx = (mongory_matcher_table_build_sub_matcher_context *)acc;
320
- if (!MONGORY_VALIDATE_TABLE(ctx->pool, and_sub_condition)) {
321
- return false; // Element in $and array is not a table.
322
- }
323
- ctx->table = and_sub_condition->data.t;
324
- return and_sub_condition->data.t->each(and_sub_condition->data.t, acc, mongory_matcher_table_build_sub_matcher);
325
- }
326
-
327
- /**
328
- * @brief Creates an "AND" ($and) matcher from an array of condition tables.
329
- * @param pool Memory pool for allocations.
330
- *
331
- * The `$and` operator takes an array of query documents. This function builds
332
- * a single, flat list of all the sub-matchers from all the query documents,
333
- * and then combines them into one large AND-connected matcher tree.
334
- *
335
- * @param pool Memory pool for allocations.
336
- * @param condition A `mongory_value` array of table conditions.
337
- * @return A new $and matcher, or NULL on failure.
338
- */
339
- 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)) {
344
- return NULL;
345
- }
346
-
347
- mongory_array *array_of_tables = and_condition->data.a;
348
- 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;
358
- }
359
-
360
- mongory_array *sub_matchers = mongory_array_new(pool);
361
- if (sub_matchers == NULL) {
362
- mongory_compile_budget_leave_depth();
363
- return NULL;
364
- }
365
-
366
- // Context for building matchers from EACH table within the $and array.
367
- 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.
376
- }
377
- }
378
-
379
- 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;
383
- }
384
-
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;
440
- }
441
-
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)) {
460
- return NULL;
461
- }
462
- mongory_array *array_of_tables = or_condition->data.a;
463
- 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;
472
- }
473
-
474
- mongory_array *sub_matchers = mongory_array_new(pool);
475
- if (sub_matchers == NULL) {
476
- mongory_compile_budget_leave_depth();
477
- return NULL;
478
- }
479
-
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
487
- }
488
- }
489
-
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;
494
- }
495
-
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;
518
- }
519
-
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
- // ============================================================================
530
- // Array-based Match Functions ($elemMatch, $every)
531
- // ============================================================================
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;
545
- }
546
- }
547
- return false;
548
- }
549
-
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;
599
- }
600
-
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
- 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;
626
- }
627
-
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)) {
641
- return NULL;
642
- }
643
- mongory_array *sub_matchers = mongory_array_new(pool);
644
- if (sub_matchers == NULL) {
645
- mongory_compile_budget_leave_depth();
646
- return NULL;
647
- }
648
- 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;
652
- }
653
-
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
- }
659
-
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;
677
- }
678
-
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;
694
- }
695
-
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);
700
- }
701
-
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;
711
- return NULL;
712
- }
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;
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
- }