mongory 0.8.2 → 0.9.1

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 (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +44 -0
  3. data/README.md +8 -4
  4. data/docs/advanced_usage.md +7 -0
  5. data/docs/clang_bridge.md +6 -0
  6. data/ext/mongory_ext/extconf.rb +6 -1
  7. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +25 -0
  8. data/ext/mongory_ext/mongory-core/src/foundations/config.c +64 -27
  9. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +1 -0
  10. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +49 -0
  11. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +45 -0
  12. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +2 -5
  13. data/ext/mongory_ext/mongory-core/src/foundations/value.c +44 -7
  14. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c +31 -10
  15. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +3 -0
  16. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +1 -0
  17. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +32 -13
  18. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +69 -15
  19. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +6 -0
  20. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +78 -15
  21. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +12 -0
  22. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c +71 -17
  23. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +2 -1
  24. data/ext/mongory_ext/mongory_ext.c +35 -724
  25. data/ext/mongory_ext/mongory_ext_internal.h +75 -0
  26. data/ext/mongory_ext/rb_mongory_adapters.c +140 -0
  27. data/ext/mongory_ext/rb_mongory_cmatcher.c +386 -0
  28. data/ext/mongory_ext/rb_mongory_value.c +260 -0
  29. data/lib/generators/mongory/install/templates/initializer.rb.erb +5 -0
  30. data/lib/mongory/converters/abstract_converter.rb +6 -5
  31. data/lib/mongory/converters/condition_converter.rb +2 -2
  32. data/lib/mongory/matchers/abstract_matcher.rb +29 -1
  33. data/lib/mongory/matchers/abstract_multi_matcher.rb +30 -4
  34. data/lib/mongory/matchers/and_matcher.rb +3 -2
  35. data/lib/mongory/matchers/array_record_matcher.rb +24 -12
  36. data/lib/mongory/matchers/elem_match_matcher.rb +2 -0
  37. data/lib/mongory/matchers/eq_matcher.rb +4 -2
  38. data/lib/mongory/matchers/hash_condition_matcher.rb +25 -7
  39. data/lib/mongory/matchers/in_matcher.rb +17 -1
  40. data/lib/mongory/matchers/literal_matcher.rb +22 -17
  41. data/lib/mongory/matchers/ne_matcher.rb +7 -6
  42. data/lib/mongory/matchers/nin_matcher.rb +2 -6
  43. data/lib/mongory/matchers/or_matcher.rb +3 -2
  44. data/lib/mongory/matchers/regex_matcher.rb +29 -0
  45. data/lib/mongory/matchers.rb +7 -4
  46. data/lib/mongory/version.rb +1 -1
  47. data/lib/mongory.rb +57 -5
  48. metadata +7 -2
@@ -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
 
@@ -49,21 +49,31 @@ static inline mongory_matcher *mongory_matcher_compare_new(mongory_memory_pool *
49
49
  * @brief Match function for equality ($eq).
50
50
  *
51
51
  * Compares the input `value` with the matcher's `condition` using the
52
- * polymorphic `comp` function of the value.
52
+ * polymorphic `comp` function of the value. If `value` is an array, also
53
+ * checks if any element in the array is equal to `condition`.
53
54
  *
54
55
  * @param matcher The equality matcher instance.
55
56
  * @param value The value to check.
56
- * @return True if `value` is equal to `matcher->condition`; false otherwise or
57
- * if the types are not comparable.
57
+ * @return True if `value` (or any of its elements when `value` is an array)
58
+ * is equal to `matcher->condition`; false otherwise.
58
59
  */
59
60
  static inline bool mongory_matcher_equal_match(mongory_matcher *matcher, mongory_value *value) {
60
61
  if (!value || !value->comp || !matcher->condition)
61
62
  return false; // Invalid inputs
62
63
  int result = value->comp(value, matcher->condition);
63
- if (result == mongory_value_compare_fail) {
64
- return false; // Types are not comparable or other comparison error.
64
+ if (result == 0) {
65
+ return true; // 0 indicates equality.
66
+ }
67
+ if (value->type == MONGORY_TYPE_ARRAY && value->data.a) {
68
+ mongory_array *arr = value->data.a;
69
+ for (size_t i = 0; i < arr->count; i++) {
70
+ mongory_value *elem = arr->get(arr, i);
71
+ if (elem && elem->comp && elem->comp(elem, matcher->condition) == 0) {
72
+ return true;
73
+ }
74
+ }
65
75
  }
66
- return result == 0; // 0 indicates equality.
76
+ return false;
67
77
  }
68
78
 
69
79
  mongory_matcher *mongory_matcher_equal_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
@@ -80,22 +90,31 @@ mongory_matcher *mongory_matcher_equal_new(mongory_memory_pool *pool, mongory_va
80
90
  * @brief Match function for inequality ($ne).
81
91
  *
82
92
  * This is the logical inverse of the `equal_match` function.
83
- * If the types are not comparable (comparison fails), it is considered "not
84
- * equal", so this function returns true in that case.
93
+ * If `value` is an array, returns false if either the array itself or any of
94
+ * its elements is equal to `matcher->condition`.
85
95
  *
86
96
  * @param matcher The inequality matcher instance.
87
97
  * @param value The value to check.
88
- * @return True if `value` is not equal to `matcher->condition` or if they
89
- * are not comparable.
98
+ * @return True if neither `value` nor any of its elements (when `value` is an
99
+ * array) is equal to `matcher->condition`.
90
100
  */
91
101
  static inline bool mongory_matcher_not_equal_match(mongory_matcher *matcher, mongory_value *value) {
92
102
  if (!value || !value->comp || !matcher->condition)
93
103
  return true; // Invalid inputs, treat as "not equal"
94
104
  int result = value->comp(value, matcher->condition);
95
- if (result == mongory_value_compare_fail) {
96
- return true; // Incomparable types are considered "not equal".
105
+ if (result == 0) {
106
+ return false;
107
+ }
108
+ if (value->type == MONGORY_TYPE_ARRAY && value->data.a) {
109
+ mongory_array *arr = value->data.a;
110
+ for (size_t i = 0; i < arr->count; i++) {
111
+ mongory_value *elem = arr->get(arr, i);
112
+ if (elem && elem->comp && elem->comp(elem, matcher->condition) == 0) {
113
+ return false;
114
+ }
115
+ }
97
116
  }
98
- return result != 0; // Non-zero indicates inequality.
117
+ return true;
99
118
  }
100
119
 
101
120
  mongory_matcher *mongory_matcher_not_equal_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
@@ -8,6 +8,7 @@
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
14
  #include "literal_matcher.h" // For mongory_matcher_field_new
@@ -20,11 +21,24 @@
20
21
  #include "../foundations/utils.h"
21
22
  #include <mongory-core.h> // General include
22
23
  #include <stdio.h> // For sprintf
24
+ #include <string.h> // For strcmp
23
25
 
24
26
  // Forward declaration.
25
27
  double mongory_matcher_calculate_priority(mongory_array *sub_matchers);
26
28
  mongory_array *mongory_matcher_sort_matchers(mongory_array *sub_matchers);
27
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
+
28
42
  /**
29
43
  * @brief Allocates and initializes a `mongory_composite_matcher` structure.
30
44
  *
@@ -60,6 +74,7 @@ mongory_composite_matcher *mongory_matcher_composite_new(mongory_memory_pool *po
60
74
  composite->base.traverse = mongory_matcher_composite_traverse;
61
75
  composite->base.extern_ctx = extern_ctx;
62
76
  composite->base.priority = 2.0;
77
+ composite->base.parent = NULL;
63
78
  return composite;
64
79
  }
65
80
 
@@ -79,7 +94,7 @@ mongory_composite_matcher *mongory_matcher_composite_new(mongory_memory_pool *po
79
94
  // ============================================================================
80
95
  static inline bool mongory_matcher_and_match(mongory_matcher *matcher, mongory_value *value) {
81
96
  mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
82
- mongory_array *children = composite->children;
97
+ mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
83
98
  int total = (int)children->count;
84
99
  for (int i = 0; i < total; i++) {
85
100
  mongory_matcher *child = (mongory_matcher *)children->get(children, i);
@@ -103,7 +118,7 @@ static inline bool mongory_matcher_and_match(mongory_matcher *matcher, mongory_v
103
118
  */
104
119
  bool mongory_matcher_or_match(mongory_matcher *matcher, mongory_value *value) {
105
120
  mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
106
- mongory_array *children = composite->children;
121
+ mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
107
122
  int total = (int)children->count;
108
123
  for (int i = 0; i < total; i++) {
109
124
  mongory_matcher *child = (mongory_matcher *)children->get(children, i);
@@ -121,10 +136,19 @@ typedef struct mongory_matcher_table_build_sub_matcher_context {
121
136
  mongory_memory_pool *pool; /**< Main pool for allocating created matchers. */
122
137
  mongory_array *matchers; /**< Array to store the created sub-matchers. */
123
138
  void *extern_ctx; /**< External context for the matcher. */
139
+ mongory_table *table; /**< Table being iterated, for sibling lookups ($regex/$options). */
124
140
  } mongory_matcher_table_build_sub_matcher_context;
125
141
 
126
142
  static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mongory_value *value, mongory_matcher_table_build_sub_matcher_context *ctx);
127
143
 
144
+ static inline void mongory_matcher_build_error_set(mongory_memory_pool *pool, const char *message) {
145
+ pool->error = MG_ALLOC_PTR(pool, mongory_error);
146
+ if (pool->error) {
147
+ pool->error->type = MONGORY_ERROR_INVALID_ARGUMENT;
148
+ pool->error->message = message;
149
+ }
150
+ }
151
+
128
152
  /**
129
153
  * @brief Callback for iterating over a condition table's key-value pairs.
130
154
  *
@@ -141,6 +165,14 @@ static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mong
141
165
  static inline bool mongory_matcher_table_build_sub_matcher(char *key, mongory_value *value, void *acc) {
142
166
  mongory_matcher_table_build_sub_matcher_context *ctx = (mongory_matcher_table_build_sub_matcher_context *)acc;
143
167
  mongory_array *matchers_array = ctx->matchers;
168
+ if (strcmp(key, "$options") == 0) {
169
+ // Consumed by the sibling $regex; only its presence is checked here.
170
+ if (ctx->table == NULL || ctx->table->get(ctx->table, "$regex") == NULL) {
171
+ mongory_matcher_build_error_set(ctx->pool, "$options requires a sibling $regex.");
172
+ return false;
173
+ }
174
+ return true;
175
+ }
144
176
  mongory_matcher *sub_matcher = mongory_matcher_build_sub_matcher(key, value, ctx);
145
177
  if (sub_matcher == NULL) {
146
178
  // Failed to create sub-matcher (e.g., allocation error, invalid condition
@@ -156,7 +188,8 @@ static inline bool mongory_matcher_table_build_sub_matcher(char *key, mongory_va
156
188
  * @brief Builds a sub-matcher from a key-value pair.
157
189
  *
158
190
  * This function determines the appropriate sub-matcher type based on the key:
159
- * - If the key starts with '$', it looks up a registered matcher builder.
191
+ * - If the key starts with '$', it looks up a registered matcher builder
192
+ * (`$regex` also takes its sibling `$options`). Unknown operators fail.
160
193
  * - Otherwise, it creates a field matcher.
161
194
  *
162
195
  * @param key The key from the condition table.
@@ -172,12 +205,18 @@ static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mong
172
205
  mongory_matcher_build_func build_func = NULL;
173
206
 
174
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
+ }
175
212
  build_func = mongory_matcher_build_func_get(key);
176
213
  if (build_func != NULL) {
177
214
  return build_func(pool, value, ctx->extern_ctx);
178
215
  } else if (mongory_custom_matcher_adapter.lookup != NULL && mongory_custom_matcher_adapter.lookup(key)) {
179
216
  return mongory_matcher_custom_new(pool, key, value, ctx->extern_ctx);
180
217
  }
218
+ mongory_matcher_build_error_set(pool, mongory_string_cpyf(pool, "Unknown operator: %s", key));
219
+ return NULL;
181
220
  }
182
221
 
183
222
  return mongory_matcher_field_new(pool, key, value, ctx->extern_ctx);
@@ -218,7 +257,7 @@ mongory_matcher *mongory_matcher_table_cond_new(mongory_memory_pool *pool, mongo
218
257
  if (sub_matchers == NULL)
219
258
  return NULL; // Failed to create array for sub-matchers.
220
259
 
221
- mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx};
260
+ mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, table};
222
261
  // Iterate over the condition table, building sub-matchers.
223
262
  if (!table->each(table, &build_ctx, mongory_matcher_table_build_sub_matcher)) {
224
263
  // Building one of the sub-matchers failed.
@@ -234,6 +273,7 @@ mongory_matcher *mongory_matcher_table_cond_new(mongory_memory_pool *pool, mongo
234
273
  if (final_matcher == NULL)
235
274
  return NULL;
236
275
  final_matcher->children = mongory_matcher_sort_matchers(sub_matchers);
276
+ mongory_matcher_attach_children_parent(final_matcher, final_matcher->children);
237
277
  final_matcher->base.match = mongory_matcher_and_match;
238
278
  final_matcher->base.original_match = mongory_matcher_and_match;
239
279
  final_matcher->base.sub_count = sub_matchers->count;
@@ -255,9 +295,11 @@ static inline bool mongory_matcher_build_and_sub_matcher(mongory_value *and_sub_
255
295
  // The 'and_sub_condition' is one of the tables in the $and:[{}, {}, ...] array.
256
296
  // We need to build all matchers from this table and add them to the list.
257
297
  // The list in 'acc' (ctx->matchers) will then be ANDed together.
258
- if (!MONGORY_VALIDATE_TABLE(and_sub_condition->pool, and_sub_condition)) {
298
+ mongory_matcher_table_build_sub_matcher_context *ctx = (mongory_matcher_table_build_sub_matcher_context *)acc;
299
+ if (!MONGORY_VALIDATE_TABLE(ctx->pool, and_sub_condition)) {
259
300
  return false; // Element in $and array is not a table.
260
301
  }
302
+ ctx->table = and_sub_condition->data.t;
261
303
  return and_sub_condition->data.t->each(and_sub_condition->data.t, acc, mongory_matcher_table_build_sub_matcher);
262
304
  }
263
305
 
@@ -294,7 +336,7 @@ mongory_matcher *mongory_matcher_and_new(mongory_memory_pool *pool, mongory_valu
294
336
  }
295
337
 
296
338
  // Context for building matchers from EACH table within the $and array.
297
- mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx};
339
+ mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
298
340
  // Iterate through the array of tables provided in the $and condition.
299
341
  // mongory_matcher_build_and_sub_matcher will then iterate keys of EACH table.
300
342
  int total = (int)array_of_tables->count;
@@ -317,6 +359,7 @@ mongory_matcher *mongory_matcher_and_new(mongory_memory_pool *pool, mongory_valu
317
359
  if (final_matcher == NULL)
318
360
  return NULL;
319
361
  final_matcher->children = mongory_matcher_sort_matchers(sub_matchers);
362
+ mongory_matcher_attach_children_parent(final_matcher, final_matcher->children);
320
363
  final_matcher->base.match = mongory_matcher_and_match;
321
364
  final_matcher->base.original_match = mongory_matcher_and_match;
322
365
  final_matcher->base.sub_count = sub_matchers->count;
@@ -381,7 +424,7 @@ mongory_matcher *mongory_matcher_or_new(mongory_memory_pool *pool, mongory_value
381
424
  return NULL;
382
425
  }
383
426
 
384
- mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx};
427
+ mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
385
428
  int total = (int)array_of_tables->count;
386
429
  for (int i = 0; i < total; i++) {
387
430
  mongory_value *table = array_of_tables->get(array_of_tables, i);
@@ -398,6 +441,7 @@ mongory_matcher *mongory_matcher_or_new(mongory_memory_pool *pool, mongory_value
398
441
  if (final_matcher == NULL)
399
442
  return NULL;
400
443
  final_matcher->children = mongory_matcher_sort_matchers(sub_matchers);
444
+ mongory_matcher_attach_children_parent(final_matcher, final_matcher->children);
401
445
  final_matcher->base.match = mongory_matcher_or_match;
402
446
  final_matcher->base.original_match = mongory_matcher_or_match;
403
447
  final_matcher->base.sub_count = sub_matchers->count;
@@ -451,18 +495,16 @@ mongory_matcher *mongory_matcher_elem_match_new(mongory_memory_pool *pool, mongo
451
495
  mongory_array *sub_matchers = mongory_array_new(pool);
452
496
  if (sub_matchers == NULL)
453
497
  return NULL;
454
- mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx};
498
+ mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
455
499
  if (!mongory_matcher_build_and_sub_matcher(elem_match_condition, &build_ctx))
456
500
  return NULL;
457
501
 
458
- if (sub_matchers->count == 0)
459
- return mongory_matcher_always_false_new(pool, elem_match_condition, extern_ctx);
460
-
461
502
  mongory_composite_matcher *composite = mongory_matcher_composite_new(pool, elem_match_condition, extern_ctx);
462
503
  if (composite == NULL)
463
504
  return NULL;
464
505
 
465
506
  composite->children = mongory_matcher_sort_matchers(sub_matchers);
507
+ mongory_matcher_attach_children_parent(composite, composite->children);
466
508
  composite->base.match = mongory_matcher_elem_match_match;
467
509
  composite->base.original_match = mongory_matcher_elem_match_match;
468
510
  composite->base.sub_count = sub_matchers->count;
@@ -513,18 +555,16 @@ mongory_matcher *mongory_matcher_every_new(mongory_memory_pool *pool, mongory_va
513
555
  mongory_array *sub_matchers = mongory_array_new(pool);
514
556
  if (sub_matchers == NULL)
515
557
  return NULL;
516
- mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx};
558
+ mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
517
559
  if (!mongory_matcher_build_and_sub_matcher(every_condition, &build_ctx))
518
560
  return NULL;
519
561
 
520
- if (sub_matchers->count == 0)
521
- return mongory_matcher_always_true_new(pool, every_condition, extern_ctx);
522
-
523
562
  mongory_composite_matcher *composite = mongory_matcher_composite_new(pool, every_condition, extern_ctx);
524
563
  if (composite == NULL)
525
564
  return NULL;
526
565
 
527
566
  composite->children = mongory_matcher_sort_matchers(sub_matchers);
567
+ mongory_matcher_attach_children_parent(composite, composite->children);
528
568
  composite->base.match = mongory_matcher_every_match;
529
569
  composite->base.original_match = mongory_matcher_every_match;
530
570
  composite->base.sub_count = sub_matchers->count;
@@ -571,3 +611,17 @@ mongory_array *mongory_matcher_sort_matchers(mongory_array *sub_matchers) {
571
611
  temp_pool->free(temp_pool);
572
612
  return sorted_matchers;
573
613
  }
614
+
615
+ void mongory_matcher_resort_composite(mongory_composite_matcher *composite) {
616
+ if (!composite) {
617
+ return;
618
+ }
619
+ mongory_array *cur = mongory_atomic_load_ptr_acquire(&composite->children);
620
+ if (!cur || cur->count <= 1) {
621
+ return;
622
+ }
623
+ mongory_array *sorted = mongory_matcher_sort_matchers(cur);
624
+ if (sorted != NULL) {
625
+ mongory_atomic_store_ptr_release(&composite->children, sorted);
626
+ }
627
+ }
@@ -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 */
@@ -14,13 +14,18 @@
14
14
  #include "matcher_explainable.h"
15
15
  #include "matcher_traversable.h"
16
16
 
17
+ typedef struct mongory_regex_matcher {
18
+ mongory_matcher base;
19
+ char *options; // Validated $options flags; NULL when absent or empty.
20
+ } mongory_regex_matcher;
21
+
17
22
  /**
18
23
  * @brief Match function for the $regex matcher.
19
24
  *
20
25
  * Checks if the input `value` (which must be a string) matches the regex
21
26
  * pattern stored in `matcher->condition`. The actual regex matching is
22
- * performed by the function pointed to by
23
- * `mongory_internal_regex_adapter->match_func`.
27
+ * delegated to `mongory_internal_regex_adapter`: `options_match_func` when
28
+ * `$options` flags are present, `match_func` otherwise.
24
29
  *
25
30
  * @param matcher The $regex matcher instance.
26
31
  * @param value The `mongory_value` to test; must be of type
@@ -32,12 +37,17 @@ static inline bool mongory_matcher_regex_match(mongory_matcher *matcher, mongory
32
37
  if (!value || value->type != MONGORY_TYPE_STRING) {
33
38
  return false; // Regex matching applies only to strings.
34
39
  }
40
+ char *options = ((mongory_regex_matcher *)matcher)->options;
41
+ mongory_memory_pool *match_pool = value->pool ? value->pool : matcher->pool;
42
+ if (options) {
43
+ return mongory_internal_regex_adapter.options_match_func(match_pool, matcher->condition, options, value);
44
+ }
35
45
  if (!mongory_internal_regex_adapter.match_func) {
36
46
  return false; // Regex adapter or function not configured.
37
47
  }
38
48
 
39
49
  // Delegate to the configured regex function.
40
- return mongory_internal_regex_adapter.match_func(matcher->pool, matcher->condition, value);
50
+ return mongory_internal_regex_adapter.match_func(match_pool, matcher->condition, value);
41
51
  }
42
52
 
43
53
  /**
@@ -51,25 +61,77 @@ static inline bool mongory_matcher_regex_condition_validate(mongory_value *condi
51
61
  return condition != NULL && (condition->type == MONGORY_TYPE_STRING || condition->type == MONGORY_TYPE_REGEX);
52
62
  }
53
63
 
54
- mongory_matcher *mongory_matcher_regex_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
55
- if (!mongory_matcher_regex_condition_validate(condition)) {
56
- pool->error = MG_ALLOC_PTR(pool, mongory_error);
57
- if (pool->error) {
58
- pool->error->type = MONGORY_ERROR_INVALID_ARGUMENT;
59
- pool->error->message = "$regex condition must be a string or a regex object.";
64
+ static inline void mongory_matcher_regex_error_set(mongory_memory_pool *pool, mongory_error_type type, const char *message) {
65
+ pool->error = MG_ALLOC_PTR(pool, mongory_error);
66
+ if (pool->error) {
67
+ pool->error->type = type;
68
+ pool->error->message = message;
69
+ }
70
+ }
71
+
72
+ /**
73
+ * @brief Validates `$options`: a string made only of the flags i, m, s.
74
+ * @return True if valid, false (with `pool->error` set) otherwise.
75
+ */
76
+ static inline bool mongory_matcher_regex_options_validate(mongory_memory_pool *pool, mongory_value *options) {
77
+ if (options->type != MONGORY_TYPE_STRING || options->data.s == NULL) {
78
+ mongory_matcher_regex_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$options must be a string.");
79
+ return false;
80
+ }
81
+ for (char *flag = options->data.s; *flag; flag++) {
82
+ if (*flag != 'i' && *flag != 'm' && *flag != 's') {
83
+ mongory_matcher_regex_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT,
84
+ mongory_string_cpyf(pool, "$options has unsupported flag '%c'; allowed flags are i, m, s.", *flag));
85
+ return false;
60
86
  }
87
+ }
88
+ return true;
89
+ }
90
+
91
+ mongory_matcher *mongory_matcher_regex_with_options_new(mongory_memory_pool *pool, mongory_value *condition,
92
+ mongory_value *options, void *extern_ctx) {
93
+ if (!mongory_matcher_regex_condition_validate(condition)) {
94
+ mongory_matcher_regex_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$regex condition must be a string or a regex object.");
95
+ return NULL;
96
+ }
97
+ if (options && !mongory_matcher_regex_options_validate(pool, options)) {
98
+ return NULL;
99
+ }
100
+ bool has_options = options && options->data.s[0] != '\0';
101
+ if (has_options && condition->type == MONGORY_TYPE_REGEX) {
102
+ // Flags belong to the compiled regex; mixing them is ambiguous.
103
+ mongory_matcher_regex_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT,
104
+ "$options cannot modify a compiled regex object.");
105
+ return NULL;
106
+ }
107
+ if (has_options && !mongory_internal_regex_adapter.options_match_func) {
108
+ mongory_matcher_regex_error_set(pool, MONGORY_ERROR_UNSUPPORTED_OPERATION,
109
+ "$options requires a regex adapter set via mongory_regex_options_func_set.");
61
110
  return NULL;
62
111
  }
63
112
 
64
- mongory_matcher *matcher = mongory_matcher_base_new(pool, condition, extern_ctx);
113
+ mongory_regex_matcher *matcher = MG_ALLOC_PTR(pool, mongory_regex_matcher);
65
114
  if (!matcher) {
115
+ pool->error = &MONGORY_ALLOC_ERROR;
66
116
  return NULL;
67
117
  }
68
- matcher->match = mongory_matcher_regex_match;
69
- matcher->original_match = mongory_matcher_regex_match;
70
- matcher->name = mongory_string_cpy(pool, "Regex");
71
- matcher->priority = 20.0;
72
- return matcher;
118
+ matcher->base.pool = pool;
119
+ matcher->base.condition = condition;
120
+ matcher->base.sub_count = 0;
121
+ matcher->base.match = mongory_matcher_regex_match;
122
+ matcher->base.original_match = mongory_matcher_regex_match;
123
+ matcher->base.explain = mongory_matcher_base_explain;
124
+ matcher->base.traverse = mongory_matcher_leaf_traverse;
125
+ matcher->base.extern_ctx = extern_ctx;
126
+ matcher->base.name = mongory_string_cpy(pool, "Regex");
127
+ matcher->base.priority = 20.0;
128
+ matcher->base.parent = NULL;
129
+ matcher->options = has_options ? mongory_string_cpy(pool, options->data.s) : NULL;
130
+ return (mongory_matcher *)matcher;
131
+ }
132
+
133
+ mongory_matcher *mongory_matcher_regex_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
134
+ return mongory_matcher_regex_with_options_new(pool, condition, NULL, extern_ctx);
73
135
  }
74
136
 
75
137
  typedef struct mongory_custom_matcher {
@@ -120,5 +182,6 @@ mongory_matcher *mongory_matcher_custom_new(mongory_memory_pool *pool, char *key
120
182
  matcher->base.extern_ctx = extern_ctx;
121
183
  matcher->external_matcher = context->external_matcher;
122
184
  matcher->base.priority = 20.0;
185
+ matcher->base.parent = NULL;
123
186
  return (mongory_matcher *)matcher;
124
187
  }
@@ -31,6 +31,18 @@
31
31
  */
32
32
  mongory_matcher *mongory_matcher_regex_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
33
33
 
34
+ /**
35
+ * @brief Creates a $regex matcher with the sibling `$options` value.
36
+ *
37
+ * `options` may be NULL (no `$options`). Otherwise it must be a string made
38
+ * only of the flags 'i', 'm', 's'. A non-empty `options` requires a regex
39
+ * adapter set via `mongory_regex_options_func_set`.
40
+ *
41
+ * @return A new $regex matcher, or NULL (with `pool->error` set) on failure.
42
+ */
43
+ mongory_matcher *mongory_matcher_regex_with_options_new(mongory_memory_pool *pool, mongory_value *condition,
44
+ mongory_value *options, void *extern_ctx);
45
+
34
46
  /**
35
47
  * @brief Creates a new custom matcher instance.
36
48
  *
@@ -11,8 +11,9 @@
11
11
  * - $size checks the number of elements in an array against a condition.
12
12
  */
13
13
  #include "literal_matcher.h"
14
- #include "../foundations/config_private.h" // For mongory_internal_value_converter
15
- #include "../foundations/utils.h" // For mongory_try_parse_int, mongory_string_cpy
14
+ #include "../foundations/config_private.h" // For mongory_internal_value_converter
15
+ #include "../foundations/memory_pool_private.h" // For atomic & lock helpers
16
+ #include "../foundations/utils.h" // For mongory_try_parse_int, mongory_string_cpy
16
17
  #include "../foundations/string_buffer.h" // For mongory_string_buffer_appendf
17
18
  #include "array_record_matcher.h" // For handling array-specific matching logic
18
19
  #include "base_matcher.h" // For mongory_matcher_base_new
@@ -49,25 +50,66 @@
49
50
  * @param value The `mongory_value` to evaluate against the matcher's condition.
50
51
  * @return True if the value matches, false otherwise.
51
52
  */
53
+ static inline void mongory_propagate_build_error(mongory_memory_pool *build_pool, mongory_value *value) {
54
+ if (!build_pool || !build_pool->error) {
55
+ return;
56
+ }
57
+ if (value && value->pool && value->pool != build_pool) {
58
+ if (build_pool->error == &MONGORY_ALLOC_ERROR) {
59
+ value->pool->error = &MONGORY_ALLOC_ERROR;
60
+ } else {
61
+ mongory_error *copied = MG_ALLOC_PTR(value->pool, mongory_error);
62
+ if (copied) {
63
+ copied->type = build_pool->error->type;
64
+ copied->message = build_pool->error->message
65
+ ? mongory_string_cpy(value->pool, (char *)build_pool->error->message)
66
+ : NULL;
67
+ value->pool->error = copied;
68
+ } else {
69
+ value->pool->error = &MONGORY_ALLOC_ERROR;
70
+ }
71
+ }
72
+ build_pool->error = NULL;
73
+ }
74
+ }
75
+
52
76
  static inline bool mongory_matcher_literal_match(mongory_matcher *matcher, mongory_value *value) {
53
77
  mongory_literal_matcher *literal = (mongory_literal_matcher *)matcher;
54
78
  if (!literal || !literal->base.pool)
55
79
  return false; // Basic safety check
56
80
 
57
81
  if (value != NULL && value->type == MONGORY_TYPE_ARRAY) {
58
- // If the value being matched is an array, there's special handling.
59
- // The `right` child of the composite matcher is used for array record matching.
60
- // It's created on-demand if not already present.
61
- if (literal->array_record_matcher == NULL) {
62
- // Lazily create the array_record_matcher if needed.
63
- // The condition for array_record_new is the original condition of the literal matcher.
64
- literal->array_record_matcher = mongory_matcher_array_record_new(literal->base.pool, literal->base.condition, literal->base.extern_ctx);
65
- if (literal->array_record_matcher && literal->base.trace_stack) {
66
- mongory_matcher_enable_trace_at(literal->array_record_matcher, literal->base.trace_stack, literal->base.trace_level + 1);
82
+ // Double-checked locking: lazy build runs only on the first array value seen,
83
+ // keeping non-array matching and subsequent array matches lock-free.
84
+ mongory_matcher *arm = mongory_atomic_load_ptr_acquire(&literal->array_record_matcher);
85
+ if (arm == NULL) {
86
+ mongory_memory_pool *build_pool = literal->base.pool;
87
+ mongory_memory_pool_lock(build_pool);
88
+ arm = mongory_atomic_load_ptr_relaxed(&literal->array_record_matcher);
89
+ if (arm == NULL) {
90
+ arm = mongory_matcher_array_record_new(build_pool, literal->base.condition, literal->base.extern_ctx);
91
+ if (arm && literal->base.trace_stack) {
92
+ mongory_matcher_enable_trace_at(arm, literal->base.trace_stack, literal->base.trace_level + 1);
93
+ }
94
+ if (arm != NULL) {
95
+ arm->parent = &literal->base;
96
+ double delta = (1.0 + arm->priority) - literal->base.priority;
97
+ if (delta != 0.0) {
98
+ for (mongory_matcher *cur = &literal->base; cur != NULL; cur = cur->parent) {
99
+ cur->priority += delta;
100
+ if (cur->traverse == mongory_matcher_composite_traverse) {
101
+ mongory_matcher_resort_composite((mongory_composite_matcher *)cur);
102
+ }
103
+ }
104
+ }
105
+ mongory_atomic_store_ptr_release(&literal->array_record_matcher, arm);
106
+ } else {
107
+ mongory_propagate_build_error(build_pool, value);
108
+ }
67
109
  }
110
+ mongory_memory_pool_unlock(build_pool);
68
111
  }
69
- // If right child exists (or was successfully created), use it.
70
- return literal->array_record_matcher ? literal->array_record_matcher->match(literal->array_record_matcher, value) : false;
112
+ return arm ? arm->match(arm, value) : false;
71
113
  } else {
72
114
  // For non-array values, or if array-specific path wasn't taken.
73
115
  // The `left` child handles the general literal condition.
@@ -176,10 +218,16 @@ static inline bool mongory_matcher_field_match(mongory_matcher *matcher, mongory
176
218
  // If the extracted field value is a pointer type that needs conversion
177
219
  // (e.g., from a language binding), convert it.
178
220
  if (field_value && field_value->type == MONGORY_TYPE_POINTER && mongory_internal_value_converter.shallow_convert) {
179
- // The pool for the converted value should ideally be the field_value's pool
180
- // or the matcher's pool.
181
- mongory_memory_pool *conversion_pool = field_value->pool ? field_value->pool : matcher->pool;
182
- field_value = mongory_internal_value_converter.shallow_convert(conversion_pool, field_value->data.ptr);
221
+ // Prefer the caller/record's thread-local pool over the shared matcher->pool.
222
+ mongory_memory_pool *conversion_pool =
223
+ field_value->pool ? field_value->pool : (value->pool ? value->pool : matcher->pool);
224
+ if (conversion_pool == matcher->pool) {
225
+ mongory_memory_pool_lock(matcher->pool);
226
+ field_value = mongory_internal_value_converter.shallow_convert(conversion_pool, field_value->data.ptr);
227
+ mongory_memory_pool_unlock(matcher->pool);
228
+ } else {
229
+ field_value = mongory_internal_value_converter.shallow_convert(conversion_pool, field_value->data.ptr);
230
+ }
183
231
  }
184
232
  // Now, use the literal_match logic (which uses composite.left primarily)
185
233
  // to match the extracted field_value.
@@ -222,6 +270,8 @@ mongory_matcher *mongory_matcher_field_new(mongory_memory_pool *pool, char *fiel
222
270
  return NULL;
223
271
  }
224
272
  field_m->literal.base.priority = 1.0 + field_m->literal.delegate_matcher->priority;
273
+ field_m->literal.base.parent = NULL;
274
+ field_m->literal.delegate_matcher->parent = (mongory_matcher *)field_m;
225
275
 
226
276
  return (mongory_matcher *)field_m;
227
277
  }
@@ -262,6 +312,8 @@ mongory_matcher *mongory_matcher_not_new(mongory_memory_pool *pool, mongory_valu
262
312
  literal->base.sub_count = 1;
263
313
  literal->base.extern_ctx = extern_ctx;
264
314
  literal->base.priority = 1.0 + literal->delegate_matcher->priority;
315
+ literal->base.parent = NULL;
316
+ literal->delegate_matcher->parent = (mongory_matcher *)literal;
265
317
  return (mongory_matcher *)literal;
266
318
  }
267
319
 
@@ -313,6 +365,8 @@ mongory_matcher *mongory_matcher_size_new(mongory_memory_pool *pool, mongory_val
313
365
  literal->base.sub_count = 1;
314
366
  literal->base.extern_ctx = extern_ctx;
315
367
  literal->base.priority = 1.0 + literal->delegate_matcher->priority;
368
+ literal->base.parent = NULL;
369
+ literal->delegate_matcher->parent = (mongory_matcher *)literal;
316
370
  return (mongory_matcher *)literal;
317
371
  }
318
372
 
@@ -2,6 +2,7 @@
2
2
  #define MONGORY_MATCHER_TRAVERSABLE_C
3
3
 
4
4
  #include "matcher_traversable.h"
5
+ #include "../foundations/memory_pool_private.h"
5
6
  #include "base_matcher.h"
6
7
  #include "composite_matcher.h"
7
8
  #include "literal_matcher.h"
@@ -19,7 +20,7 @@ bool mongory_matcher_composite_traverse(mongory_matcher *matcher, mongory_matche
19
20
  if (!mongory_matcher_leaf_traverse(matcher, ctx))
20
21
  return false;
21
22
  mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
22
- mongory_array *children = composite->children;
23
+ mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
23
24
  int total = (int)children->count;
24
25
  mongory_matcher_traverse_context child_ctx = {
25
26
  .pool = ctx->pool,