mongory 0.8.1 → 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 (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +66 -0
  3. data/README.md +12 -0
  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/array.c +22 -15
  9. data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +1 -0
  10. data/ext/mongory_ext/mongory-core/src/foundations/config.c +64 -27
  11. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +1 -0
  12. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +49 -0
  13. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +45 -0
  14. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +0 -2
  15. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +0 -6
  16. data/ext/mongory_ext/mongory-core/src/foundations/table.c +9 -10
  17. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +21 -14
  18. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +2 -2
  19. data/ext/mongory_ext/mongory-core/src/foundations/value.c +54 -9
  20. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c +31 -10
  21. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +2 -0
  22. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +2 -0
  23. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +32 -13
  24. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +34 -13
  25. data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.c +16 -3
  26. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +76 -15
  27. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +12 -0
  28. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c +57 -15
  29. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +43 -7
  30. data/ext/mongory_ext/mongory_ext.c +36 -687
  31. data/ext/mongory_ext/mongory_ext_internal.h +75 -0
  32. data/ext/mongory_ext/rb_mongory_adapters.c +140 -0
  33. data/ext/mongory_ext/rb_mongory_cmatcher.c +386 -0
  34. data/ext/mongory_ext/rb_mongory_value.c +260 -0
  35. data/lib/generators/mongory/install/templates/initializer.rb.erb +5 -0
  36. data/lib/mongory/converters/abstract_converter.rb +6 -5
  37. data/lib/mongory/converters/condition_converter.rb +2 -2
  38. data/lib/mongory/converters/key_converter.rb +1 -1
  39. data/lib/mongory/matchers/abstract_matcher.rb +1 -1
  40. data/lib/mongory/matchers/abstract_multi_matcher.rb +2 -2
  41. data/lib/mongory/matchers/array_record_matcher.rb +23 -11
  42. data/lib/mongory/matchers/elem_match_matcher.rb +2 -0
  43. data/lib/mongory/matchers/eq_matcher.rb +4 -2
  44. data/lib/mongory/matchers/field_matcher.rb +16 -3
  45. data/lib/mongory/matchers/hash_condition_matcher.rb +23 -6
  46. data/lib/mongory/matchers/in_matcher.rb +17 -1
  47. data/lib/mongory/matchers/ne_matcher.rb +7 -6
  48. data/lib/mongory/matchers/nin_matcher.rb +2 -6
  49. data/lib/mongory/matchers/regex_matcher.rb +29 -0
  50. data/lib/mongory/matchers.rb +7 -4
  51. data/lib/mongory/version.rb +1 -1
  52. data/lib/mongory.rb +57 -5
  53. metadata +7 -2
@@ -25,30 +25,37 @@
25
25
  * `false` otherwise. `errno` may be set by `strtol`.
26
26
  */
27
27
  bool mongory_try_parse_int(const char *key, int *out) {
28
- if (key == NULL || *key == '\0') {
28
+ if (key == NULL) {
29
29
  return false; // Invalid input string.
30
30
  }
31
- if (out == NULL) {
32
- return false; // Output pointer must be valid.
31
+
32
+ // Strictly match "0" or "-?[1-9][0-9]*" (no leading whitespace, '+', "-0", or leading zeros like "01").
33
+ if (key[0] == '0') {
34
+ if (key[1] != '\0') {
35
+ return false; // Disallow leading zeros like "00" or "01".
36
+ }
37
+ if (out != NULL) {
38
+ *out = 0;
39
+ }
40
+ return true;
41
+ }
42
+
43
+ const char *p = (key[0] == '-') ? key + 1 : key;
44
+ if (*p < '1') {
45
+ return false; // Disallow "", "-", "-0", "-01", leading whitespace, or '+'.
33
46
  }
34
47
 
35
48
  char *endptr = NULL;
36
49
  errno = 0; // Clear errno before calling strtol.
37
50
  long val = strtol(key, &endptr, 10); // Base 10 conversion.
38
51
 
39
- // Check for parsing errors reported by strtol.
40
- if (endptr == key) {
41
- return false; // No digits were found.
42
- }
43
- if (*endptr != '\0') {
44
- return false; // Additional characters after the number.
45
- }
46
- if (errno == ERANGE || val < INT_MIN || val > INT_MAX) {
47
- // Value out of range for int. ERANGE is set by strtol for overflow/underflow.
48
- return false;
52
+ if (*endptr != '\0' || errno == ERANGE || val < INT_MIN || val > INT_MAX) {
53
+ return false; // Additional characters or value out of range for int.
49
54
  }
50
55
 
51
- *out = (int)val; // Successfully parsed and within int range.
56
+ if (out != NULL) {
57
+ *out = (int)val; // Successfully parsed and within int range.
58
+ }
52
59
  return true;
53
60
  }
54
61
 
@@ -13,8 +13,8 @@
13
13
  * range (`INT_MIN`, `INT_MAX`).
14
14
  *
15
15
  * @param key The null-terminated string to parse. Must not be NULL or empty.
16
- * @param out A pointer to an integer where the parsed value will be stored if
17
- * successful. Must not be NULL.
16
+ * @param out Optional pointer to an integer where the parsed value will be
17
+ * stored if successful. May be NULL if only validation is needed.
18
18
  * @return bool True if the string was successfully parsed as an integer and is
19
19
  * within `int` range, false otherwise. `errno` might be set by `strtol` on
20
20
  * failure (e.g. `ERANGE`).
@@ -154,6 +154,9 @@ static inline int mongory_value_int_compare(mongory_value *a, mongory_value *b)
154
154
  // Compare int (a) as double with double (b).
155
155
  double a_as_double = (double)a->data.i;
156
156
  double b_value = b->data.d;
157
+ if (b_value != b_value) {
158
+ return mongory_value_compare_fail; // NaN is not comparable.
159
+ }
157
160
  return (a_as_double > b_value) - (a_as_double < b_value);
158
161
  }
159
162
  if (b->type == MONGORY_TYPE_INT) {
@@ -179,14 +182,19 @@ mongory_value *mongory_value_wrap_i(mongory_memory_pool *pool, int64_t i_val) {
179
182
 
180
183
  /** Compares MONGORY_TYPE_DOUBLE with DOUBLE or INT. */
181
184
  static inline int mongory_value_double_compare(mongory_value *a, mongory_value *b) {
185
+ double a_value = a->data.d;
186
+ if (a_value != a_value) {
187
+ return mongory_value_compare_fail; // NaN is not comparable.
188
+ }
182
189
  if (b->type == MONGORY_TYPE_DOUBLE) {
183
- double a_value = a->data.d;
184
190
  double b_value = b->data.d;
191
+ if (b_value != b_value) {
192
+ return mongory_value_compare_fail; // NaN is not comparable.
193
+ }
185
194
  return (a_value > b_value) - (a_value < b_value);
186
195
  }
187
196
  if (b->type == MONGORY_TYPE_INT) {
188
197
  // Compare double (a) with int (b) as double.
189
- double a_value = a->data.d;
190
198
  double b_as_double = (double)b->data.i;
191
199
  return (a_value > b_as_double) - (a_value < b_as_double);
192
200
  }
@@ -293,14 +301,51 @@ mongory_value *mongory_value_wrap_a(mongory_memory_pool *pool, struct mongory_ar
293
301
  return value;
294
302
  }
295
303
 
296
- /** Compares two MONGORY_TYPE_TABLE values. Currently unsupported (always fails). */
304
+ /** Context for comparing two tables key by key. */
305
+ typedef struct mongory_table_compare_context {
306
+ struct mongory_table *other_table;
307
+ bool equal;
308
+ } mongory_table_compare_context;
309
+
310
+ static inline bool mongory_value_table_compare_foreach(char *key, mongory_value *val_a, void *acc) {
311
+ mongory_table_compare_context *ctx = (mongory_table_compare_context *)acc;
312
+ mongory_value *val_b = ctx->other_table->get(ctx->other_table, key);
313
+ if (val_b == NULL) {
314
+ ctx->equal = false;
315
+ return false;
316
+ }
317
+ bool a_is_null = (val_a == NULL || val_a->type == MONGORY_TYPE_NULL);
318
+ bool b_is_null = (val_b->type == MONGORY_TYPE_NULL);
319
+ if (a_is_null && b_is_null) {
320
+ return true;
321
+ }
322
+ if (a_is_null || b_is_null || !val_a->comp || val_a->comp(val_a, val_b) != 0) {
323
+ ctx->equal = false;
324
+ return false;
325
+ }
326
+ return true;
327
+ }
328
+
329
+ /** Compares two MONGORY_TYPE_TABLE values for equality. */
297
330
  static inline int mongory_value_table_compare(mongory_value *a, mongory_value *b) {
298
- (void)a; // Unused.
299
- (void)b; // Unused.
300
- // True table comparison is complex because the order of keys is not
301
- // significant. It would require iterating over keys of one table and checking
302
- // for their presence and value equality in the other. This is not
303
- // implemented. Therefore, tables are considered incomparable.
331
+ if (b->type != MONGORY_TYPE_TABLE || a->data.t == NULL || b->data.t == NULL) {
332
+ return mongory_value_compare_fail;
333
+ }
334
+ struct mongory_table *table_a = a->data.t;
335
+ struct mongory_table *table_b = b->data.t;
336
+ if (table_a->count != table_b->count) {
337
+ return mongory_value_compare_fail;
338
+ }
339
+ if (table_b->each && table_a->get) {
340
+ mongory_table_compare_context ctx = {table_a, true};
341
+ table_b->each(table_b, &ctx, mongory_value_table_compare_foreach);
342
+ return ctx.equal ? 0 : mongory_value_compare_fail;
343
+ }
344
+ if (table_a->each && table_b->get) {
345
+ mongory_table_compare_context ctx = {table_b, true};
346
+ table_a->each(table_a, &ctx, mongory_value_table_compare_foreach);
347
+ return ctx.equal ? 0 : mongory_value_compare_fail;
348
+ }
304
349
  return mongory_value_compare_fail;
305
350
  }
306
351
 
@@ -45,8 +45,12 @@ typedef struct mongory_matcher_array_record_parse_table_context {
45
45
  * Iterates through the main condition table:
46
46
  * - If `key` is "$elemMatch" and `value` is a table, its contents are added to
47
47
  * `context->elem_match_table`.
48
- * - If `key` starts with '$' (another operator) or is numeric (array index),
49
- * it's added to `context->parsed_table`.
48
+ * - If `key` is an element-level comparison operator (`$gt`, `$gte`, `$lt`,
49
+ * `$lte`, `$regex` with its `$options`, or non-array `$eq`), it's added to
50
+ * `context->elem_match_table`.
51
+ * - If `key` starts with '$' (container operator like `$size`, `$in`, `$nin`,
52
+ * `$every`, `$ne`, or array `$eq`) or is a valid integer index, it's added to
53
+ * `context->parsed_table`.
50
54
  * - Otherwise (plain field name), it's considered part of an implicit element
51
55
  * match and added to `context->elem_match_table`.
52
56
  *
@@ -55,6 +59,18 @@ typedef struct mongory_matcher_array_record_parse_table_context {
55
59
  * @param acc Pointer to `mongory_matcher_array_record_parse_table_context`.
56
60
  * @return Always true to continue.
57
61
  */
62
+ static inline bool mongory_is_elem_match_operator(char *key, mongory_value *value) {
63
+ if (strcmp(key, "$gt") == 0 || strcmp(key, "$gte") == 0 ||
64
+ strcmp(key, "$lt") == 0 || strcmp(key, "$lte") == 0 ||
65
+ strcmp(key, "$regex") == 0 || strcmp(key, "$options") == 0) {
66
+ return true;
67
+ }
68
+ if (strcmp(key, "$eq") == 0) {
69
+ return value == NULL || value->type != MONGORY_TYPE_ARRAY;
70
+ }
71
+ return false;
72
+ }
73
+
58
74
  static inline bool mongory_matcher_array_record_parse_table_foreach(char *key, mongory_value *value, void *acc) {
59
75
  mongory_matcher_array_record_parse_table_context *context = (mongory_matcher_array_record_parse_table_context *)acc;
60
76
  mongory_table *parsed_table_for_operators = context->parsed_table;
@@ -63,8 +79,11 @@ static inline bool mongory_matcher_array_record_parse_table_foreach(char *key, m
63
79
  if (strcmp(key, "$elemMatch") == 0 && value->type == MONGORY_TYPE_TABLE && value->data.t != NULL) {
64
80
  // Explicit $elemMatch: iterate its sub-table and add to elem_match_table
65
81
  mongory_table_merge(table_for_elem_match_conditions, value->data.t);
82
+ } else if (mongory_is_elem_match_operator(key, value)) {
83
+ // Element-level comparison operators apply to array elements via $elemMatch
84
+ table_for_elem_match_conditions->set(table_for_elem_match_conditions, key, value);
66
85
  } else if (*key == '$' || mongory_try_parse_int(key, NULL)) {
67
- // Operator (like $size) or numeric index: goes to parsed_table
86
+ // Array-level operator (like $size, $in, $nin, $every, $ne) or numeric index: goes to parsed_table
68
87
  parsed_table_for_operators->set(parsed_table_for_operators, key, value);
69
88
  } else {
70
89
  // Regular field name: implies a condition on elements, goes to
@@ -86,19 +105,21 @@ static inline bool mongory_matcher_array_record_parse_table_foreach(char *key, m
86
105
  * @return A new `mongory_value` (table) containing the parsed and restructured
87
106
  * condition. Returns NULL if input is not a table or on allocation failure.
88
107
  */
89
- static inline mongory_value *mongory_matcher_array_record_parse_table(mongory_value *condition) {
90
- if (!condition->data.t || !condition->pool) {
91
- mongory_error *error = MG_ALLOC_PTR(condition->pool, mongory_error);
108
+ static inline mongory_value *mongory_matcher_array_record_parse_table(mongory_memory_pool *pool, mongory_value *condition) {
109
+ if (!pool) {
110
+ return NULL;
111
+ }
112
+ if (!condition || !condition->data.t) {
113
+ mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
92
114
  if (!error) {
93
- condition->pool->error = &MONGORY_ALLOC_ERROR;
115
+ pool->error = &MONGORY_ALLOC_ERROR;
94
116
  return NULL;
95
117
  }
96
118
  error->type = MONGORY_ERROR_INVALID_TYPE;
97
119
  error->message = "Expected condition to be a table, got a non-table value";
98
- condition->pool->error = error; // TODO: This is a hack, we should use a better error handling mechanism
120
+ pool->error = error; // TODO: This is a hack, we should use a better error handling mechanism
99
121
  return NULL; // Invalid input
100
122
  }
101
- mongory_memory_pool *pool = condition->pool;
102
123
  mongory_table *parsed_table = mongory_table_new(pool);
103
124
  mongory_table *elem_match_sub_table = mongory_table_new(pool);
104
125
 
@@ -146,7 +167,7 @@ mongory_matcher *mongory_matcher_array_record_new(mongory_memory_pool *pool, mon
146
167
  switch (condition->type) {
147
168
  case MONGORY_TYPE_TABLE:
148
169
  return mongory_matcher_table_cond_new(pool,
149
- mongory_matcher_array_record_parse_table(condition),
170
+ mongory_matcher_array_record_parse_table(pool, condition),
150
171
  extern_ctx
151
172
  );
152
173
  case MONGORY_TYPE_ARRAY:
@@ -51,6 +51,8 @@ mongory_matcher *mongory_matcher_base_new(mongory_memory_pool *pool, mongory_val
51
51
  matcher->match = NULL; // Specific match function must be set by derived type.
52
52
  matcher->explain = mongory_matcher_base_explain; // Specific explain function must be set by derived type.
53
53
  matcher->traverse = mongory_matcher_leaf_traverse;
54
+ matcher->trace_stack = NULL;
55
+ matcher->trace_level = 0;
54
56
  matcher->extern_ctx = extern_ctx; // Set the external context.
55
57
  matcher->priority = 1.0; // Set the priority to 1.0.
56
58
  return matcher;
@@ -97,4 +97,6 @@ mongory_matcher *mongory_matcher_always_true_new(mongory_memory_pool *pool, mong
97
97
  */
98
98
  mongory_matcher *mongory_matcher_always_false_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
99
99
 
100
+ void mongory_matcher_enable_trace_at(mongory_matcher *matcher, mongory_array *trace_stack, int level);
101
+
100
102
  #endif /* MONGORY_MATCHER_BASE_H */
@@ -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;
@@ -95,7 +95,7 @@ static inline bool mongory_matcher_present_match(mongory_matcher *matcher, mongo
95
95
  return !condition_expects_presence;
96
96
  }
97
97
 
98
- bool actual_value_is_present;
98
+ bool actual_value_is_present = false;
99
99
  switch (value->type) {
100
100
  case MONGORY_TYPE_ARRAY:
101
101
  actual_value_is_present = (value->data.a != NULL && value->data.a->count > 0);
@@ -103,9 +103,22 @@ static inline bool mongory_matcher_present_match(mongory_matcher *matcher, mongo
103
103
  case MONGORY_TYPE_TABLE:
104
104
  actual_value_is_present = (value->data.t != NULL && value->data.t->count > 0);
105
105
  break;
106
- case MONGORY_TYPE_STRING:
107
- actual_value_is_present = (value->data.s != NULL && *(value->data.s) != '\0');
106
+ case MONGORY_TYPE_STRING: {
107
+ // If the string is NULL, it is not "present".
108
+ // If the strint is " " or more than one space, it is not "present".
109
+ char *s = value->data.s;
110
+ if (s != NULL && *s != '\0') {
111
+ char *p = s;
112
+ while (*p != '\0') {
113
+ if (*p != ' ') {
114
+ actual_value_is_present = true;
115
+ break;
116
+ }
117
+ p++;
118
+ }
119
+ }
108
120
  break;
121
+ }
109
122
  case MONGORY_TYPE_NULL:
110
123
  actual_value_is_present = false; // An explicit BSON-style Null is not "present".
111
124
  break;
@@ -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
  *