mongory 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/README.md +55 -73
  4. data/docs/clang_bridge.md +2 -2
  5. data/docs/performance.md +43 -7
  6. data/ext/mongory_ext/extconf.rb +15 -0
  7. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +28 -43
  8. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -1
  9. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +3 -4
  10. data/ext/mongory_ext/mongory-core/src/foundations/array.c +28 -5
  11. data/ext/mongory_ext/mongory-core/src/foundations/config.c +24 -22
  12. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +3 -3
  13. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +22 -13
  14. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +4 -3
  15. data/ext/mongory_ext/mongory-core/src/foundations/table.c +60 -224
  16. data/ext/mongory_ext/mongory-core/src/foundations/table_private.h +36 -0
  17. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +30 -47
  18. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +2 -5
  19. data/ext/mongory_ext/mongory-core/src/foundations/value.c +18 -32
  20. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +89 -104
  21. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +18 -11
  22. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +35 -150
  23. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +244 -418
  24. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +30 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +15 -56
  26. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -39
  27. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +14 -15
  28. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +382 -74
  29. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +373 -123
  30. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +20 -12
  31. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -4
  32. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +214 -171
  33. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +8 -16
  34. data/ext/mongory_ext/mongory_ext.c +26 -4
  35. data/ext/mongory_ext/mongory_ext_internal.h +15 -3
  36. data/ext/mongory_ext/rb_mongory_adapters.c +52 -19
  37. data/ext/mongory_ext/rb_mongory_matcher.c +115 -36
  38. data/ext/mongory_ext/rb_mongory_value.c +124 -51
  39. data/lib/mongory/converters/abstract_converter.rb +5 -5
  40. data/lib/mongory/converters/converted.rb +0 -4
  41. data/lib/mongory/converters/key_converter.rb +13 -36
  42. data/lib/mongory/converters/value_converter.rb +4 -4
  43. data/lib/mongory/matchers.rb +8 -7
  44. data/lib/mongory/query_builder.rb +9 -0
  45. data/lib/mongory/utils/context.rb +0 -6
  46. data/lib/mongory/utils/singleton_builder.rb +1 -4
  47. data/lib/mongory/version.rb +1 -1
  48. data/lib/mongory.rb +0 -1
  49. data/script/bench.rb +77 -4
  50. data/script/coverage.rb +84 -0
  51. metadata +7 -7
  52. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
  53. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
@@ -1,7 +1,9 @@
1
1
  /**
2
2
  * @file inclusion_matcher.c
3
3
  * @brief Implements $in and $nin matchers for checking value inclusion in an
4
- * array. This is an internal implementation file for the matcher module.
4
+ * array. Uses an immutable, arena-allocated, type-bucketed O(1) hash set for
5
+ * condition arrays with >= 16 elements while falling back to linear scan for
6
+ * small arrays.
5
7
  */
6
8
  #include "inclusion_matcher.h"
7
9
  #include "base_matcher.h" // For mongory_matcher_base_new
@@ -9,116 +11,422 @@
9
11
  #include "mongory-core/foundations/array.h" // For mongory_array operations
10
12
  #include "mongory-core/foundations/error.h" // For mongory_error
11
13
  #include "mongory-core/foundations/value.h" // For mongory_value
14
+ #include <math.h> // For floor, isnan, isfinite, fabs
12
15
  #include <mongory-core.h> // General include
13
- #include "../foundations/utils.h" // For mongory_string_cpy
16
+ #include <stdint.h> // For uint64_t, int64_t, INT64_C
17
+ #include <string.h> // For strlen, memcmp, memset, memcpy
18
+ #include "../foundations/utils.h" // For mongory_string_cpy, mongory_log
19
+
20
+ #define MONGORY_IN_HASH_THRESHOLD 16
21
+ #define MONGORY_IN_SAFE_INT_MAX (INT64_C(1) << 53)
22
+
23
+ typedef struct mongory_in_str_slot {
24
+ const char *str;
25
+ size_t len;
26
+ uint64_t hash;
27
+ } mongory_in_str_slot;
28
+
29
+ typedef enum mongory_in_num_kind {
30
+ MONGORY_IN_NUM_EMPTY = 0,
31
+ MONGORY_IN_NUM_INT = 1,
32
+ MONGORY_IN_NUM_DOUBLE = 2
33
+ } mongory_in_num_kind;
34
+
35
+ typedef struct mongory_in_num_slot {
36
+ mongory_in_num_kind kind;
37
+ uint64_t key_bits; // int64_t bits (for INT) or IEEE-754 double bits (for DOUBLE)
38
+ mongory_value *val;
39
+ } mongory_in_num_slot;
40
+
41
+ typedef struct mongory_in_set {
42
+ bool has_null;
43
+ bool has_true;
44
+ bool has_false;
45
+
46
+ mongory_in_str_slot *str_slots;
47
+ size_t str_mask;
48
+
49
+ mongory_in_num_slot *num_slots;
50
+ size_t num_mask;
51
+
52
+ mongory_value **fallback;
53
+ size_t fallback_count;
54
+ } mongory_in_set;
55
+
56
+ typedef struct mongory_inclusion_matcher {
57
+ mongory_matcher base;
58
+ mongory_in_set *set;
59
+ bool has_null; // A null item also matches a missing value.
60
+ } mongory_inclusion_matcher;
61
+
62
+ static inline uint64_t mongory_fnv1a_64(const char *s, size_t len) {
63
+ uint64_t h = UINT64_C(14695981039346656037);
64
+ for (size_t i = 0; i < len; i++) {
65
+ h ^= (uint64_t)(unsigned char)s[i];
66
+ h *= UINT64_C(1099511628211);
67
+ }
68
+ return h;
69
+ }
70
+
71
+ static inline uint64_t mongory_splitmix64(uint64_t x) {
72
+ x += UINT64_C(0x9e3779b97f4a7c15);
73
+ x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
74
+ x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
75
+ return x ^ (x >> 31);
76
+ }
77
+
78
+ static inline size_t mongory_next_pow2(size_t n) {
79
+ size_t cap = 32;
80
+ while (cap < n && cap < ((size_t)1 << (sizeof(size_t) * 8 - 1))) {
81
+ cap <<= 1;
82
+ }
83
+ return cap;
84
+ }
14
85
 
15
86
  /**
16
- * @brief Validates that the condition for an inclusion matcher is a valid
17
- * array.
18
- * @param condition The `mongory_value` condition to validate.
19
- * @return True if `condition` is not NULL, is of type `MONGORY_TYPE_ARRAY`,
20
- * and its internal array data `condition->data.a` is not NULL. False otherwise.
87
+ * Classifies a numeric mongory_value into a canonical hashable key:
88
+ * - Returns MONGORY_IN_NUM_INT for exact integers within [-2^53, 2^53] (including
89
+ * integral doubles like 1.0 and -0.0), so 1 == 1.0 and -0.0 == 0 share the same key.
90
+ * - Returns MONGORY_IN_NUM_DOUBLE for finite non-integer doubles within [-2^53, 2^53]
91
+ * and +/-Infinity.
92
+ * - Returns MONGORY_IN_NUM_EMPTY if the value is NaN (never matches) or exceeds
93
+ * safe exact-integer range (caller routes large numbers to fallback).
21
94
  */
22
- static inline bool mongory_matcher_validate_array_condition(mongory_value *condition) {
23
- if (condition == NULL) {
24
- return false; // Condition must exist.
95
+ static inline mongory_in_num_kind mongory_in_classify_num(const mongory_value *v, uint64_t *out_bits, bool *out_is_nan) {
96
+ *out_is_nan = false;
97
+ if (v->type == MONGORY_TYPE_INT) {
98
+ int64_t i = v->data.i;
99
+ if (i >= -MONGORY_IN_SAFE_INT_MAX && i <= MONGORY_IN_SAFE_INT_MAX) {
100
+ *out_bits = (uint64_t)i;
101
+ return MONGORY_IN_NUM_INT;
102
+ }
103
+ return MONGORY_IN_NUM_EMPTY;
25
104
  }
26
- if (condition->type != MONGORY_TYPE_ARRAY) {
27
- return false; // Condition must be an array.
105
+ if (v->type == MONGORY_TYPE_DOUBLE) {
106
+ double d = v->data.d;
107
+ if (isnan(d)) {
108
+ *out_is_nan = true;
109
+ return MONGORY_IN_NUM_EMPTY;
110
+ }
111
+ if (d == 0.0) {
112
+ *out_bits = 0;
113
+ return MONGORY_IN_NUM_INT;
114
+ }
115
+ if (isfinite(d)) {
116
+ if (fabs(d) > (double)MONGORY_IN_SAFE_INT_MAX) {
117
+ return MONGORY_IN_NUM_EMPTY;
118
+ }
119
+ if (floor(d) == d) {
120
+ int64_t i = (int64_t)d;
121
+ *out_bits = (uint64_t)i;
122
+ return MONGORY_IN_NUM_INT;
123
+ }
124
+ }
125
+ uint64_t bits = 0;
126
+ memcpy(&bits, &d, sizeof(bits));
127
+ *out_bits = bits;
128
+ return MONGORY_IN_NUM_DOUBLE;
28
129
  }
29
- if (condition->data.a == NULL) {
30
- return false; // The array data within the condition must be valid.
130
+ return MONGORY_IN_NUM_EMPTY;
131
+ }
132
+
133
+ static inline size_t mongory_in_str_probe(const mongory_in_set *set, const char *s, size_t len, uint64_t h,
134
+ bool *found) {
135
+ size_t idx = (size_t)(h & set->str_mask);
136
+ while (set->str_slots[idx].str != NULL) {
137
+ if (set->str_slots[idx].hash == h && set->str_slots[idx].len == len &&
138
+ memcmp(set->str_slots[idx].str, s, len) == 0) {
139
+ *found = true;
140
+ return idx;
141
+ }
142
+ idx = (idx + 1) & set->str_mask;
31
143
  }
32
- return true;
144
+ *found = false;
145
+ return idx;
33
146
  }
34
147
 
35
- /**
36
- * @brief Match function for the $in matcher.
37
- *
38
- * If `value_to_check` is a scalar, it checks if `value_to_check` is present in
39
- * `matcher->condition` (which must be an array).
40
- * If `value_to_check` is an array, it checks if any element of `value_to_check`
41
- * is present in `matcher->condition` (array intersection).
42
- *
43
- * @param matcher The $in matcher instance.
44
- * @param value_to_check The value to check for inclusion.
45
- * @return True if `value_to_check` (or one of its elements) is found in the
46
- * condition array, false otherwise.
47
- */
48
- static inline bool mongory_matcher_in_match(mongory_matcher *matcher, mongory_value *value_to_check) {
49
- if (!value_to_check || !matcher->condition || !matcher->condition->data.a) {
50
- // Invalid inputs or condition is not a proper array.
148
+ static mongory_in_set *mongory_in_set_build(mongory_memory_pool *pool, mongory_array *arr) {
149
+ size_t count = arr->count;
150
+ size_t str_count = 0;
151
+ size_t num_count = 0;
152
+ size_t fb_count = 0;
153
+
154
+ for (size_t i = 0; i < count; i++) {
155
+ mongory_value *elem = arr->get(arr, i);
156
+ if (!elem) {
157
+ continue;
158
+ }
159
+ switch (elem->type) {
160
+ case MONGORY_TYPE_NULL:
161
+ case MONGORY_TYPE_BOOL:
162
+ break;
163
+ case MONGORY_TYPE_STRING:
164
+ if (elem->data.s != NULL) {
165
+ str_count++;
166
+ }
167
+ break;
168
+ case MONGORY_TYPE_INT:
169
+ case MONGORY_TYPE_DOUBLE: {
170
+ uint64_t bits = 0;
171
+ bool is_nan = false;
172
+ if (mongory_in_classify_num(elem, &bits, &is_nan) != MONGORY_IN_NUM_EMPTY) {
173
+ num_count++;
174
+ } else if (!is_nan) {
175
+ fb_count++;
176
+ }
177
+ break;
178
+ }
179
+ default:
180
+ fb_count++;
181
+ break;
182
+ }
183
+ }
184
+
185
+ mongory_in_set *set = MG_ALLOC_PTR(pool, mongory_in_set);
186
+ if (!set) {
187
+ pool->error = &MONGORY_ALLOC_ERROR;
188
+ return NULL;
189
+ }
190
+ memset(set, 0, sizeof(*set));
191
+
192
+ if (str_count > 0) {
193
+ size_t cap = mongory_next_pow2(str_count * 2);
194
+ set->str_slots = (mongory_in_str_slot *)pool->alloc(pool, cap * sizeof(mongory_in_str_slot));
195
+ if (!set->str_slots) {
196
+ pool->error = &MONGORY_ALLOC_ERROR;
197
+ return NULL;
198
+ }
199
+ memset(set->str_slots, 0, cap * sizeof(mongory_in_str_slot));
200
+ set->str_mask = cap - 1;
201
+ }
202
+
203
+ if (num_count > 0) {
204
+ size_t cap = mongory_next_pow2(num_count * 2);
205
+ set->num_slots = (mongory_in_num_slot *)pool->alloc(pool, cap * sizeof(mongory_in_num_slot));
206
+ if (!set->num_slots) {
207
+ pool->error = &MONGORY_ALLOC_ERROR;
208
+ return NULL;
209
+ }
210
+ memset(set->num_slots, 0, cap * sizeof(mongory_in_num_slot));
211
+ set->num_mask = cap - 1;
212
+ }
213
+
214
+ if (fb_count > 0) {
215
+ set->fallback = (mongory_value **)pool->alloc(pool, fb_count * sizeof(mongory_value *));
216
+ if (!set->fallback) {
217
+ pool->error = &MONGORY_ALLOC_ERROR;
218
+ return NULL;
219
+ }
220
+ }
221
+
222
+ for (size_t i = 0; i < count; i++) {
223
+ mongory_value *elem = arr->get(arr, i);
224
+ if (!elem) {
225
+ continue;
226
+ }
227
+ switch (elem->type) {
228
+ case MONGORY_TYPE_NULL:
229
+ set->has_null = true;
230
+ break;
231
+ case MONGORY_TYPE_BOOL:
232
+ if (elem->data.b) {
233
+ set->has_true = true;
234
+ } else {
235
+ set->has_false = true;
236
+ }
237
+ break;
238
+ case MONGORY_TYPE_STRING: {
239
+ const char *s = elem->data.s;
240
+ if (!s) {
241
+ break;
242
+ }
243
+ size_t len = strlen(s);
244
+ uint64_t h = mongory_fnv1a_64(s, len);
245
+ bool found = false;
246
+ size_t idx = mongory_in_str_probe(set, s, len, h, &found);
247
+ if (!found) {
248
+ set->str_slots[idx].str = s;
249
+ set->str_slots[idx].len = len;
250
+ set->str_slots[idx].hash = h;
251
+ }
252
+ break;
253
+ }
254
+ case MONGORY_TYPE_INT:
255
+ case MONGORY_TYPE_DOUBLE: {
256
+ uint64_t bits = 0;
257
+ bool is_nan = false;
258
+ mongory_in_num_kind kind = mongory_in_classify_num(elem, &bits, &is_nan);
259
+ if (kind != MONGORY_IN_NUM_EMPTY) {
260
+ uint64_t h = mongory_splitmix64(bits ^ ((uint64_t)kind << 60));
261
+ size_t idx = (size_t)(h & set->num_mask);
262
+ while (set->num_slots[idx].kind != MONGORY_IN_NUM_EMPTY) {
263
+ if (set->num_slots[idx].kind == kind && set->num_slots[idx].key_bits == bits) {
264
+ break;
265
+ }
266
+ idx = (idx + 1) & set->num_mask;
267
+ }
268
+ if (set->num_slots[idx].kind == MONGORY_IN_NUM_EMPTY) {
269
+ set->num_slots[idx].kind = kind;
270
+ set->num_slots[idx].key_bits = bits;
271
+ set->num_slots[idx].val = elem;
272
+ }
273
+ } else if (!is_nan) {
274
+ set->fallback[set->fallback_count++] = elem;
275
+ }
276
+ break;
277
+ }
278
+ default:
279
+ set->fallback[set->fallback_count++] = elem;
280
+ break;
281
+ }
282
+ }
283
+
284
+ return set;
285
+ }
286
+
287
+ static inline bool mongory_in_set_contains_scalar(const mongory_in_set *set, mongory_value *v) {
288
+ if (!v) {
51
289
  return false;
52
290
  }
291
+ switch (v->type) {
292
+ case MONGORY_TYPE_NULL:
293
+ return set->has_null;
294
+ case MONGORY_TYPE_BOOL:
295
+ return v->data.b ? set->has_true : set->has_false;
296
+ case MONGORY_TYPE_STRING: {
297
+ if (!set->str_slots || !v->data.s) {
298
+ return false;
299
+ }
300
+ size_t len = strlen(v->data.s);
301
+ bool found = false;
302
+ mongory_in_str_probe(set, v->data.s, len, mongory_fnv1a_64(v->data.s, len), &found);
303
+ return found;
304
+ }
305
+ case MONGORY_TYPE_INT:
306
+ case MONGORY_TYPE_DOUBLE: {
307
+ uint64_t bits = 0;
308
+ bool is_nan = false;
309
+ mongory_in_num_kind kind = mongory_in_classify_num(v, &bits, &is_nan);
310
+ if (is_nan) {
311
+ return false;
312
+ }
313
+ if (kind != MONGORY_IN_NUM_EMPTY) {
314
+ if (!set->num_slots) {
315
+ return false;
316
+ }
317
+ uint64_t h = mongory_splitmix64(bits ^ ((uint64_t)kind << 60));
318
+ size_t idx = (size_t)(h & set->num_mask);
319
+ while (set->num_slots[idx].kind != MONGORY_IN_NUM_EMPTY) {
320
+ if (set->num_slots[idx].kind == kind && set->num_slots[idx].key_bits == bits &&
321
+ set->num_slots[idx].val->comp(set->num_slots[idx].val, v) == 0) {
322
+ return true;
323
+ }
324
+ idx = (idx + 1) & set->num_mask;
325
+ }
326
+ return false;
327
+ }
328
+ // Large integer/double outside exact 2^53 range falls through to fallback scan.
329
+ }
330
+ /* fall through */
331
+ default:
332
+ for (size_t i = 0; i < set->fallback_count; i++) {
333
+ mongory_value *cand = set->fallback[i];
334
+ if (cand->comp(cand, v) == 0) {
335
+ return true;
336
+ }
337
+ }
338
+ return false;
339
+ }
340
+ }
53
341
 
54
- mongory_array *condition_array = matcher->condition->data.a;
342
+ static inline bool mongory_inclusion_contains_scalar(const mongory_inclusion_matcher *inc, mongory_value *item) {
343
+ return inc->set ? mongory_in_set_contains_scalar(inc->set, item)
344
+ : mongory_array_includes(inc->base.condition->data.a, item);
345
+ }
55
346
 
347
+ static inline bool mongory_matcher_in_match(mongory_matcher *matcher, mongory_value *value_to_check) {
348
+ mongory_inclusion_matcher *inc = (mongory_inclusion_matcher *)matcher;
349
+ if (!value_to_check) {
350
+ return inc->has_null;
351
+ }
352
+ if (!matcher->condition || !matcher->condition->data.a) {
353
+ return false;
354
+ }
56
355
  if (value_to_check->type != MONGORY_TYPE_ARRAY) {
57
- return mongory_array_includes(condition_array, value_to_check);
356
+ return mongory_inclusion_contains_scalar(inc, value_to_check);
58
357
  }
59
-
60
358
  mongory_array *input_array = value_to_check->data.a;
61
359
  if (!input_array) {
62
360
  return false;
63
361
  }
64
-
65
362
  for (size_t i = 0; i < input_array->count; i++) {
66
- mongory_value *input_item = input_array->get(input_array, i);
67
- if (mongory_array_includes(condition_array, input_item)) {
363
+ if (mongory_inclusion_contains_scalar(inc, input_array->get(input_array, i))) {
68
364
  return true;
69
365
  }
70
366
  }
71
-
72
367
  return false;
73
368
  }
74
369
 
75
- mongory_matcher *mongory_matcher_in_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
76
- if (!mongory_matcher_validate_array_condition(condition)) {
77
- mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$in condition must be a valid array.");
370
+ static mongory_inclusion_matcher *mongory_inclusion_matcher_alloc(mongory_memory_pool *pool, mongory_value *condition,
371
+ void *extern_ctx, const char *name,
372
+ mongory_matcher_match_func match_fn) {
373
+ mongory_inclusion_matcher *inc = (mongory_inclusion_matcher *)mongory_matcher_alloc_init(
374
+ pool, sizeof(mongory_inclusion_matcher), condition, extern_ctx, match_fn, mongory_matcher_leaf_traverse,
375
+ name, 2.0);
376
+ if (!inc) {
78
377
  return NULL;
79
378
  }
80
- mongory_matcher *matcher = mongory_matcher_base_new(pool, condition, extern_ctx);
81
- if (!matcher) {
379
+
380
+ mongory_array *cond_arr = condition->data.a;
381
+ inc->has_null = false;
382
+ for (size_t i = 0; i < cond_arr->count; i++) {
383
+ mongory_value *item = cond_arr->get(cond_arr, i);
384
+ if (item != NULL && item->type == MONGORY_TYPE_NULL) {
385
+ inc->has_null = true;
386
+ break;
387
+ }
388
+ }
389
+ if (cond_arr->count >= MONGORY_IN_HASH_THRESHOLD) {
390
+ inc->set = mongory_in_set_build(pool, cond_arr);
391
+ if (!inc->set) {
392
+ return NULL;
393
+ }
394
+ inc->base.priority = 3.0 + (double)(inc->set->fallback_count / 8);
395
+ } else {
396
+ inc->base.priority = 2.0 + (double)(cond_arr->count / 8);
397
+ }
398
+
399
+ return inc;
400
+ }
401
+
402
+ static mongory_matcher *mongory_inclusion_matcher_new(mongory_memory_pool *pool, mongory_value *condition,
403
+ void *extern_ctx, const char *name,
404
+ mongory_matcher_match_func match_fn, const char *err_msg,
405
+ double priority_offset) {
406
+ if (!mongory_validate_pool(pool)) {
82
407
  return NULL;
83
408
  }
84
- matcher->match = mongory_matcher_in_match;
85
- matcher->original_match = mongory_matcher_in_match;
86
- matcher->name = mongory_string_cpy(pool, "In");
87
- if (!matcher->name) {
409
+ if (condition == NULL || condition->type != MONGORY_TYPE_ARRAY || condition->data.a == NULL) {
410
+ mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, err_msg);
88
411
  return NULL;
89
412
  }
90
- matcher->priority = 1.0 + mongory_log((double)condition->data.a->count + 1.0, 1.5);
91
- return matcher;
413
+ mongory_inclusion_matcher *inc = mongory_inclusion_matcher_alloc(pool, condition, extern_ctx, name, match_fn);
414
+ if (inc) {
415
+ inc->base.priority += priority_offset;
416
+ }
417
+ return inc ? &inc->base : NULL;
418
+ }
419
+
420
+ mongory_matcher *mongory_matcher_in_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
421
+ return mongory_inclusion_matcher_new(pool, condition, extern_ctx, "In", mongory_matcher_in_match,
422
+ "$in condition must be a valid array.", 0.0);
92
423
  }
93
424
 
94
- /**
95
- * @brief Match function for the $nin (not in) matcher.
96
- * Simply negates the result of the $in logic.
97
- * @param matcher The $nin matcher instance.
98
- * @param value The value to check.
99
- * @return True if the value is NOT found according to $in logic, false
100
- * otherwise.
101
- */
102
425
  static inline bool mongory_matcher_not_in_match(mongory_matcher *matcher, mongory_value *value) {
103
- // $nin is true if $in is false.
104
426
  return !mongory_matcher_in_match(matcher, value);
105
427
  }
106
428
 
107
429
  mongory_matcher *mongory_matcher_not_in_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
108
- if (!mongory_matcher_validate_array_condition(condition)) {
109
- mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$nin condition must be a valid array.");
110
- return NULL;
111
- }
112
- mongory_matcher *matcher = mongory_matcher_base_new(pool, condition, extern_ctx);
113
- if (!matcher) {
114
- return NULL;
115
- }
116
- matcher->match = mongory_matcher_not_in_match;
117
- matcher->original_match = mongory_matcher_not_in_match;
118
- matcher->name = mongory_string_cpy(pool, "Nin");
119
- if (!matcher->name) {
120
- return NULL;
121
- }
122
- matcher->priority = 1.0 + mongory_log((double)condition->data.a->count + 1.0, 1.5);
123
- return matcher;
430
+ return mongory_inclusion_matcher_new(pool, condition, extern_ctx, "Nin", mongory_matcher_not_in_match,
431
+ "$nin condition must be a valid array.", 1.0);
124
432
  }