mongory 0.8.2 → 0.9.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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -0
  3. data/docs/advanced_usage.md +7 -0
  4. data/docs/clang_bridge.md +6 -0
  5. data/ext/mongory_ext/extconf.rb +6 -1
  6. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +25 -0
  7. data/ext/mongory_ext/mongory-core/src/foundations/config.c +64 -27
  8. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +1 -0
  9. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +49 -0
  10. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +45 -0
  11. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +2 -5
  12. data/ext/mongory_ext/mongory-core/src/foundations/value.c +44 -7
  13. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c +31 -10
  14. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +2 -0
  15. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +32 -13
  16. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +34 -13
  17. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +76 -15
  18. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +12 -0
  19. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c +55 -17
  20. data/ext/mongory_ext/mongory_ext.c +35 -724
  21. data/ext/mongory_ext/mongory_ext_internal.h +75 -0
  22. data/ext/mongory_ext/rb_mongory_adapters.c +140 -0
  23. data/ext/mongory_ext/rb_mongory_cmatcher.c +386 -0
  24. data/ext/mongory_ext/rb_mongory_value.c +260 -0
  25. data/lib/generators/mongory/install/templates/initializer.rb.erb +5 -0
  26. data/lib/mongory/converters/abstract_converter.rb +6 -5
  27. data/lib/mongory/converters/condition_converter.rb +2 -2
  28. data/lib/mongory/matchers/abstract_matcher.rb +1 -1
  29. data/lib/mongory/matchers/abstract_multi_matcher.rb +2 -2
  30. data/lib/mongory/matchers/array_record_matcher.rb +23 -11
  31. data/lib/mongory/matchers/elem_match_matcher.rb +2 -0
  32. data/lib/mongory/matchers/eq_matcher.rb +4 -2
  33. data/lib/mongory/matchers/hash_condition_matcher.rb +23 -6
  34. data/lib/mongory/matchers/in_matcher.rb +17 -1
  35. data/lib/mongory/matchers/ne_matcher.rb +7 -6
  36. data/lib/mongory/matchers/nin_matcher.rb +2 -6
  37. data/lib/mongory/matchers/regex_matcher.rb +29 -0
  38. data/lib/mongory/matchers.rb +7 -4
  39. data/lib/mongory/version.rb +1 -1
  40. data/lib/mongory.rb +57 -5
  41. metadata +7 -2
@@ -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) {
@@ -20,6 +20,7 @@
20
20
  #include "../foundations/utils.h"
21
21
  #include <mongory-core.h> // General include
22
22
  #include <stdio.h> // For sprintf
23
+ #include <string.h> // For strcmp
23
24
 
24
25
  // Forward declaration.
25
26
  double mongory_matcher_calculate_priority(mongory_array *sub_matchers);
@@ -121,10 +122,19 @@ typedef struct mongory_matcher_table_build_sub_matcher_context {
121
122
  mongory_memory_pool *pool; /**< Main pool for allocating created matchers. */
122
123
  mongory_array *matchers; /**< Array to store the created sub-matchers. */
123
124
  void *extern_ctx; /**< External context for the matcher. */
125
+ mongory_table *table; /**< Table being iterated, for sibling lookups ($regex/$options). */
124
126
  } mongory_matcher_table_build_sub_matcher_context;
125
127
 
126
128
  static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mongory_value *value, mongory_matcher_table_build_sub_matcher_context *ctx);
127
129
 
130
+ static inline void mongory_matcher_build_error_set(mongory_memory_pool *pool, const char *message) {
131
+ pool->error = MG_ALLOC_PTR(pool, mongory_error);
132
+ if (pool->error) {
133
+ pool->error->type = MONGORY_ERROR_INVALID_ARGUMENT;
134
+ pool->error->message = message;
135
+ }
136
+ }
137
+
128
138
  /**
129
139
  * @brief Callback for iterating over a condition table's key-value pairs.
130
140
  *
@@ -141,6 +151,14 @@ static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mong
141
151
  static inline bool mongory_matcher_table_build_sub_matcher(char *key, mongory_value *value, void *acc) {
142
152
  mongory_matcher_table_build_sub_matcher_context *ctx = (mongory_matcher_table_build_sub_matcher_context *)acc;
143
153
  mongory_array *matchers_array = ctx->matchers;
154
+ if (strcmp(key, "$options") == 0) {
155
+ // Consumed by the sibling $regex; only its presence is checked here.
156
+ if (ctx->table == NULL || ctx->table->get(ctx->table, "$regex") == NULL) {
157
+ mongory_matcher_build_error_set(ctx->pool, "$options requires a sibling $regex.");
158
+ return false;
159
+ }
160
+ return true;
161
+ }
144
162
  mongory_matcher *sub_matcher = mongory_matcher_build_sub_matcher(key, value, ctx);
145
163
  if (sub_matcher == NULL) {
146
164
  // Failed to create sub-matcher (e.g., allocation error, invalid condition
@@ -156,7 +174,8 @@ static inline bool mongory_matcher_table_build_sub_matcher(char *key, mongory_va
156
174
  * @brief Builds a sub-matcher from a key-value pair.
157
175
  *
158
176
  * 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.
177
+ * - If the key starts with '$', it looks up a registered matcher builder
178
+ * (`$regex` also takes its sibling `$options`). Unknown operators fail.
160
179
  * - Otherwise, it creates a field matcher.
161
180
  *
162
181
  * @param key The key from the condition table.
@@ -172,12 +191,18 @@ static inline mongory_matcher *mongory_matcher_build_sub_matcher(char *key, mong
172
191
  mongory_matcher_build_func build_func = NULL;
173
192
 
174
193
  if (key[0] == '$') { // Operator key (e.g., "$eq", "$in")
194
+ if (strcmp(key, "$regex") == 0) {
195
+ mongory_value *options = ctx->table ? ctx->table->get(ctx->table, "$options") : NULL;
196
+ return mongory_matcher_regex_with_options_new(pool, value, options, ctx->extern_ctx);
197
+ }
175
198
  build_func = mongory_matcher_build_func_get(key);
176
199
  if (build_func != NULL) {
177
200
  return build_func(pool, value, ctx->extern_ctx);
178
201
  } else if (mongory_custom_matcher_adapter.lookup != NULL && mongory_custom_matcher_adapter.lookup(key)) {
179
202
  return mongory_matcher_custom_new(pool, key, value, ctx->extern_ctx);
180
203
  }
204
+ mongory_matcher_build_error_set(pool, mongory_string_cpyf(pool, "Unknown operator: %s", key));
205
+ return NULL;
181
206
  }
182
207
 
183
208
  return mongory_matcher_field_new(pool, key, value, ctx->extern_ctx);
@@ -218,7 +243,7 @@ mongory_matcher *mongory_matcher_table_cond_new(mongory_memory_pool *pool, mongo
218
243
  if (sub_matchers == NULL)
219
244
  return NULL; // Failed to create array for sub-matchers.
220
245
 
221
- mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx};
246
+ mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, table};
222
247
  // Iterate over the condition table, building sub-matchers.
223
248
  if (!table->each(table, &build_ctx, mongory_matcher_table_build_sub_matcher)) {
224
249
  // Building one of the sub-matchers failed.
@@ -255,9 +280,11 @@ static inline bool mongory_matcher_build_and_sub_matcher(mongory_value *and_sub_
255
280
  // The 'and_sub_condition' is one of the tables in the $and:[{}, {}, ...] array.
256
281
  // We need to build all matchers from this table and add them to the list.
257
282
  // The list in 'acc' (ctx->matchers) will then be ANDed together.
258
- if (!MONGORY_VALIDATE_TABLE(and_sub_condition->pool, and_sub_condition)) {
283
+ mongory_matcher_table_build_sub_matcher_context *ctx = (mongory_matcher_table_build_sub_matcher_context *)acc;
284
+ if (!MONGORY_VALIDATE_TABLE(ctx->pool, and_sub_condition)) {
259
285
  return false; // Element in $and array is not a table.
260
286
  }
287
+ ctx->table = and_sub_condition->data.t;
261
288
  return and_sub_condition->data.t->each(and_sub_condition->data.t, acc, mongory_matcher_table_build_sub_matcher);
262
289
  }
263
290
 
@@ -294,7 +321,7 @@ mongory_matcher *mongory_matcher_and_new(mongory_memory_pool *pool, mongory_valu
294
321
  }
295
322
 
296
323
  // 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};
324
+ mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
298
325
  // Iterate through the array of tables provided in the $and condition.
299
326
  // mongory_matcher_build_and_sub_matcher will then iterate keys of EACH table.
300
327
  int total = (int)array_of_tables->count;
@@ -381,7 +408,7 @@ mongory_matcher *mongory_matcher_or_new(mongory_memory_pool *pool, mongory_value
381
408
  return NULL;
382
409
  }
383
410
 
384
- mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx};
411
+ mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
385
412
  int total = (int)array_of_tables->count;
386
413
  for (int i = 0; i < total; i++) {
387
414
  mongory_value *table = array_of_tables->get(array_of_tables, i);
@@ -451,13 +478,10 @@ mongory_matcher *mongory_matcher_elem_match_new(mongory_memory_pool *pool, mongo
451
478
  mongory_array *sub_matchers = mongory_array_new(pool);
452
479
  if (sub_matchers == NULL)
453
480
  return NULL;
454
- mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx};
481
+ mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
455
482
  if (!mongory_matcher_build_and_sub_matcher(elem_match_condition, &build_ctx))
456
483
  return NULL;
457
484
 
458
- if (sub_matchers->count == 0)
459
- return mongory_matcher_always_false_new(pool, elem_match_condition, extern_ctx);
460
-
461
485
  mongory_composite_matcher *composite = mongory_matcher_composite_new(pool, elem_match_condition, extern_ctx);
462
486
  if (composite == NULL)
463
487
  return NULL;
@@ -513,13 +537,10 @@ mongory_matcher *mongory_matcher_every_new(mongory_memory_pool *pool, mongory_va
513
537
  mongory_array *sub_matchers = mongory_array_new(pool);
514
538
  if (sub_matchers == NULL)
515
539
  return NULL;
516
- mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx};
540
+ mongory_matcher_table_build_sub_matcher_context build_ctx = {pool, sub_matchers, extern_ctx, NULL};
517
541
  if (!mongory_matcher_build_and_sub_matcher(every_condition, &build_ctx))
518
542
  return NULL;
519
543
 
520
- if (sub_matchers->count == 0)
521
- return mongory_matcher_always_true_new(pool, every_condition, extern_ctx);
522
-
523
544
  mongory_composite_matcher *composite = mongory_matcher_composite_new(pool, every_condition, extern_ctx);
524
545
  if (composite == NULL)
525
546
  return NULL;
@@ -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,76 @@ 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->options = has_options ? mongory_string_cpy(pool, options->data.s) : NULL;
129
+ return (mongory_matcher *)matcher;
130
+ }
131
+
132
+ mongory_matcher *mongory_matcher_regex_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
133
+ return mongory_matcher_regex_with_options_new(pool, condition, NULL, extern_ctx);
73
134
  }
74
135
 
75
136
  typedef struct mongory_custom_matcher {
@@ -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,56 @@
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
+ mongory_atomic_store_ptr_release(&literal->array_record_matcher, arm);
96
+ } else {
97
+ mongory_propagate_build_error(build_pool, value);
98
+ }
67
99
  }
100
+ mongory_memory_pool_unlock(build_pool);
68
101
  }
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;
102
+ return arm ? arm->match(arm, value) : false;
71
103
  } else {
72
104
  // For non-array values, or if array-specific path wasn't taken.
73
105
  // The `left` child handles the general literal condition.
@@ -176,10 +208,16 @@ static inline bool mongory_matcher_field_match(mongory_matcher *matcher, mongory
176
208
  // If the extracted field value is a pointer type that needs conversion
177
209
  // (e.g., from a language binding), convert it.
178
210
  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);
211
+ // Prefer the caller/record's thread-local pool over the shared matcher->pool.
212
+ mongory_memory_pool *conversion_pool =
213
+ field_value->pool ? field_value->pool : (value->pool ? value->pool : matcher->pool);
214
+ if (conversion_pool == matcher->pool) {
215
+ mongory_memory_pool_lock(matcher->pool);
216
+ field_value = mongory_internal_value_converter.shallow_convert(conversion_pool, field_value->data.ptr);
217
+ mongory_memory_pool_unlock(matcher->pool);
218
+ } else {
219
+ field_value = mongory_internal_value_converter.shallow_convert(conversion_pool, field_value->data.ptr);
220
+ }
183
221
  }
184
222
  // Now, use the literal_match logic (which uses composite.left primarily)
185
223
  // to match the extracted field_value.