mongory 2.1.1 → 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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -0
  3. data/README.md +45 -74
  4. data/docs/clang_bridge.md +2 -2
  5. data/docs/performance.md +28 -1
  6. data/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 +67 -118
  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/coverage.rb +84 -0
  50. metadata +7 -7
  51. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
  52. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
@@ -55,6 +55,16 @@ mongory_matcher *mongory_matcher_and_new(mongory_memory_pool *pool, mongory_valu
55
55
  */
56
56
  mongory_matcher *mongory_matcher_or_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
57
57
 
58
+ /**
59
+ * @brief Creates a "NOR" ($nor) matcher.
60
+ * Matches if none of the conditions specified in an array of condition tables are met.
61
+ * @param pool Memory pool for allocation.
62
+ * @param condition A `mongory_value` of type `MONGORY_TYPE_ARRAY`, where each
63
+ * element is a `MONGORY_TYPE_TABLE` representing a sub-condition.
64
+ * @return A new `$nor` matcher, or NULL on failure or if condition is invalid.
65
+ */
66
+ mongory_matcher *mongory_matcher_nor_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
67
+
58
68
  /**
59
69
  * @brief Creates an "element match" ($elemMatch) matcher.
60
70
  * Matches an array field if at least one element in the array matches the given
@@ -122,6 +132,26 @@ mongory_composite_matcher *mongory_matcher_composite_new(mongory_memory_pool *po
122
132
  */
123
133
  bool mongory_matcher_or_match(mongory_matcher *matcher, mongory_value *value);
124
134
 
135
+ /**
136
+ * @brief Combines already-built sub-matchers with AND logic ("And" node).
137
+ * @return The And matcher, or NULL on failure.
138
+ */
139
+ mongory_matcher *mongory_matcher_and_of_new(mongory_memory_pool *pool, mongory_array *sub_matchers, mongory_value *condition,
140
+ void *extern_ctx);
141
+
142
+ /**
143
+ * @brief Creates a "Not" node that negates an already-built matcher.
144
+ *
145
+ * Used for null negations lifted to the field path. With `document_only`,
146
+ * a record that cannot hold fields (not a table or array) never matches.
147
+ *
148
+ * @param inner The matcher to negate (non-NULL).
149
+ * @param condition The positive condition shown by explain/trace.
150
+ * @return The Not matcher, or NULL on failure.
151
+ */
152
+ mongory_matcher *mongory_matcher_negation_new(mongory_memory_pool *pool, mongory_matcher *inner, mongory_value *condition,
153
+ bool document_only, void *extern_ctx);
154
+
125
155
  /**
126
156
  * @brief Re-sorts a composite matcher's children by updated priority using
127
157
  * copy-on-write atomic publication safe for concurrent readers.
@@ -19,54 +19,27 @@
19
19
  * @return True if `condition` is not NULL and is of type `MONGORY_TYPE_BOOL`,
20
20
  * false otherwise.
21
21
  */
22
- static bool mongory_matcher_validate_bool_condition(mongory_value *condition) {
23
- if (condition == NULL) {
24
- return false; // Condition itself must exist.
22
+ static mongory_matcher *mongory_matcher_bool_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx,
23
+ mongory_matcher_match_func match_fn, const char *name,
24
+ const char *err_msg, double priority) {
25
+ if (!mongory_validate_pool(pool)) {
26
+ return NULL;
25
27
  }
26
- if (condition->type != MONGORY_TYPE_BOOL) {
27
- return false; // Condition must be a boolean value.
28
+ if (condition == NULL || condition->type != MONGORY_TYPE_BOOL) {
29
+ mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, err_msg);
30
+ return NULL;
28
31
  }
29
- return true;
32
+ return (mongory_matcher *)mongory_matcher_alloc_init(
33
+ pool, sizeof(mongory_matcher), condition, extern_ctx, match_fn, mongory_matcher_leaf_traverse, name, priority);
30
34
  }
31
35
 
32
- /**
33
- * @brief Match function for the $exists matcher.
34
- *
35
- * Compares the existence of the input `value` (i.e., `value != NULL`)
36
- * with the boolean `matcher->condition->data.b`.
37
- * - If `condition` is true, matches if `value` is not NULL.
38
- * - If `condition` is false, matches if `value` is NULL.
39
- *
40
- * @param matcher The $exists matcher instance. Its condition must be boolean.
41
- * @param value The value whose existence is being checked. This is typically
42
- * the result of a field lookup.
43
- * @return True if the existence matches the condition, false otherwise.
44
- */
45
36
  static inline bool mongory_matcher_exists_match(mongory_matcher *matcher, mongory_value *value) {
46
- // The condition for $exists is a boolean (e.g., field: {$exists: true})
47
- bool condition_expects_existence = matcher->condition->data.b;
48
- bool actual_value_exists = (value != NULL);
49
-
50
- return condition_expects_existence == actual_value_exists;
37
+ return matcher->condition->data.b == (value != NULL);
51
38
  }
52
39
 
53
40
  mongory_matcher *mongory_matcher_exists_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
54
- if (!mongory_matcher_validate_bool_condition(condition)) {
55
- mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$exists condition must be a boolean value.");
56
- return NULL;
57
- }
58
- mongory_matcher *matcher = mongory_matcher_base_new(pool, condition, extern_ctx);
59
- if (!matcher) {
60
- return NULL;
61
- }
62
- matcher->match = mongory_matcher_exists_match;
63
- matcher->original_match = mongory_matcher_exists_match;
64
- matcher->name = mongory_string_cpy(pool, "Exists");
65
- if (!matcher->name) {
66
- return NULL;
67
- }
68
- matcher->priority = 2.0;
69
- return matcher;
41
+ return mongory_matcher_bool_new(pool, condition, extern_ctx, mongory_matcher_exists_match, "Exists",
42
+ "$exists condition must be a boolean value.", 2.0);
70
43
  }
71
44
 
72
45
  /**
@@ -139,20 +112,6 @@ static inline bool mongory_matcher_present_match(mongory_matcher *matcher, mongo
139
112
  }
140
113
 
141
114
  mongory_matcher *mongory_matcher_present_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
142
- if (!mongory_matcher_validate_bool_condition(condition)) {
143
- mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$present condition must be a boolean value.");
144
- return NULL;
145
- }
146
- mongory_matcher *matcher = mongory_matcher_base_new(pool, condition, extern_ctx);
147
- if (!matcher) {
148
- return NULL;
149
- }
150
- matcher->match = mongory_matcher_present_match;
151
- matcher->original_match = mongory_matcher_present_match;
152
- matcher->name = mongory_string_cpy(pool, "Present");
153
- if (!matcher->name) {
154
- return NULL;
155
- }
156
- matcher->priority = 2.0;
157
- return matcher;
115
+ return mongory_matcher_bool_new(pool, condition, extern_ctx, mongory_matcher_present_match, "Present",
116
+ "$present condition must be a boolean value.", 2.0);
158
117
  }
@@ -11,7 +11,6 @@
11
11
  #include "mongory-core/foundations/error.h" // For MONGORY_ERROR_INVALID_ARGUMENT
12
12
  #include "mongory-core/foundations/memory_pool.h"
13
13
  #include "mongory-core/foundations/value.h"
14
- #include "matcher_explainable.h"
15
14
  #include "matcher_traversable.h"
16
15
 
17
16
  typedef struct mongory_regex_matcher {
@@ -34,12 +33,15 @@ typedef struct mongory_regex_matcher {
34
33
  * input is not a string.
35
34
  */
36
35
  static inline bool mongory_matcher_regex_match(mongory_matcher *matcher, mongory_value *value) {
37
- if (!value || value->type != MONGORY_TYPE_STRING) {
36
+ if (!matcher || !value || value->type != MONGORY_TYPE_STRING) {
38
37
  return false; // Regex matching applies only to strings.
39
38
  }
40
39
  char *options = ((mongory_regex_matcher *)matcher)->options;
41
40
  mongory_memory_pool *match_pool = value->pool ? value->pool : matcher->pool;
42
41
  if (options) {
42
+ if (!mongory_internal_regex_adapter.options_match_func) {
43
+ return false;
44
+ }
43
45
  return mongory_internal_regex_adapter.options_match_func(match_pool, matcher->condition, options, value);
44
46
  }
45
47
  if (!mongory_internal_regex_adapter.match_func) {
@@ -86,6 +88,9 @@ static inline bool mongory_matcher_regex_options_validate(mongory_memory_pool *p
86
88
 
87
89
  mongory_matcher *mongory_matcher_regex_with_options_new(mongory_memory_pool *pool, mongory_value *condition,
88
90
  mongory_value *options, void *extern_ctx) {
91
+ if (!mongory_validate_pool(pool)) {
92
+ return NULL;
93
+ }
89
94
  if (!mongory_matcher_regex_condition_validate(condition)) {
90
95
  mongory_matcher_regex_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$regex condition must be a string or a regex object.");
91
96
  return NULL;
@@ -106,29 +111,12 @@ mongory_matcher *mongory_matcher_regex_with_options_new(mongory_memory_pool *poo
106
111
  return NULL;
107
112
  }
108
113
 
109
- if (!mongory_compile_budget_count_node(pool)) {
110
- return NULL;
111
- }
112
-
113
- mongory_regex_matcher *matcher = MG_ALLOC_PTR(pool, mongory_regex_matcher);
114
+ mongory_regex_matcher *matcher = (mongory_regex_matcher *)mongory_matcher_alloc_init(
115
+ pool, sizeof(mongory_regex_matcher), condition, extern_ctx, mongory_matcher_regex_match,
116
+ mongory_matcher_leaf_traverse, "Regex", 20.0);
114
117
  if (!matcher) {
115
- pool->error = &MONGORY_ALLOC_ERROR;
116
118
  return NULL;
117
119
  }
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
- if (!matcher->base.name) {
128
- return NULL;
129
- }
130
- matcher->base.priority = 20.0;
131
- matcher->base.parent = NULL;
132
120
  matcher->options = has_options ? mongory_string_cpy(pool, options->data.s) : NULL;
133
121
  if (has_options && !matcher->options) {
134
122
  return NULL;
@@ -152,7 +140,7 @@ typedef struct mongory_custom_matcher {
152
140
  * @return True if the value matches the matcher, false otherwise.
153
141
  */
154
142
  bool mongory_matcher_custom_match(mongory_matcher *matcher, mongory_value *value) {
155
- if (mongory_custom_matcher_adapter.match == NULL) {
143
+ if (!matcher || mongory_custom_matcher_adapter.match == NULL) {
156
144
  return false; // Custom matcher adapter not initialized.
157
145
  }
158
146
  mongory_custom_matcher *custom_matcher = (mongory_custom_matcher *)matcher;
@@ -170,30 +158,21 @@ bool mongory_matcher_custom_match(mongory_matcher *matcher, mongory_value *value
170
158
  * NULL on failure.
171
159
  */
172
160
  mongory_matcher *mongory_matcher_custom_new(mongory_memory_pool *pool, char *key, mongory_value *condition, void *extern_ctx) {
161
+ if (!MONGORY_VALIDATE_PTR(pool, key)) {
162
+ return NULL;
163
+ }
173
164
  if (mongory_custom_matcher_adapter.build == NULL)
174
165
  return NULL; // Custom matcher adapter not initialized.
175
- if (!mongory_compile_budget_count_node(pool))
176
- return NULL;
177
- mongory_custom_matcher *matcher = MG_ALLOC_PTR(pool, mongory_custom_matcher);
178
- if (matcher == NULL) {
179
- if (pool) {
180
- pool->error = &MONGORY_ALLOC_ERROR;
181
- }
166
+ mongory_custom_matcher *matcher = (mongory_custom_matcher *)mongory_matcher_alloc_init(
167
+ pool, sizeof(mongory_custom_matcher), condition, extern_ctx, mongory_matcher_custom_match,
168
+ mongory_matcher_leaf_traverse, NULL, 20.0);
169
+ if (!matcher) {
182
170
  return NULL;
183
171
  }
184
172
  mongory_matcher_custom_context *context = mongory_custom_matcher_adapter.build(key, condition, extern_ctx);
185
173
  if (context == NULL)
186
174
  return NULL;
187
- matcher->base.pool = pool;
188
- matcher->base.condition = condition;
189
175
  matcher->base.name = context->name;
190
- matcher->base.match = mongory_matcher_custom_match;
191
- matcher->base.original_match = mongory_matcher_custom_match;
192
- matcher->base.explain = mongory_matcher_base_explain;
193
- matcher->base.traverse = mongory_matcher_leaf_traverse;
194
- matcher->base.extern_ctx = extern_ctx;
195
176
  matcher->external_matcher = context->external_matcher;
196
- matcher->base.priority = 20.0;
197
- matcher->base.parent = NULL;
198
177
  return (mongory_matcher *)matcher;
199
178
  }
@@ -37,6 +37,7 @@
37
37
  typedef struct mongory_matcher_implicit_array_parse_table_context {
38
38
  mongory_table *parsed_table; /**< Stores operator or indexed conditions. */
39
39
  mongory_table *elem_match_table; /**< Stores conditions for element matching. */
40
+ bool has_explicit_elem_match; /**< An explicit $elemMatch was present, even if empty. */
40
41
  } mongory_matcher_implicit_array_parse_table_context;
41
42
 
42
43
  /**
@@ -76,8 +77,9 @@ static inline bool mongory_matcher_implicit_array_parse_table_foreach(char *key,
76
77
  mongory_table *parsed_table_for_operators = context->parsed_table;
77
78
  mongory_table *table_for_elem_match_conditions = context->elem_match_table;
78
79
 
79
- if (strcmp(key, "$elemMatch") == 0 && value->type == MONGORY_TYPE_TABLE && value->data.t != NULL) {
80
+ if (strcmp(key, "$elemMatch") == 0 && value != NULL && value->type == MONGORY_TYPE_TABLE && value->data.t != NULL) {
80
81
  // Explicit $elemMatch: iterate its sub-table and add to elem_match_table
82
+ context->has_explicit_elem_match = true;
81
83
  mongory_table_merge(table_for_elem_match_conditions, value->data.t);
82
84
  } else if (mongory_is_elem_match_operator(key, value)) {
83
85
  // Element-level comparison operators apply to array elements via $elemMatch
@@ -106,11 +108,7 @@ static inline bool mongory_matcher_implicit_array_parse_table_foreach(char *key,
106
108
  * condition. Returns NULL if input is not a table or on allocation failure.
107
109
  */
108
110
  static inline mongory_value *mongory_matcher_implicit_array_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_set(pool, MONGORY_ERROR_INVALID_TYPE, "Expected condition to be a table, got a non-table value");
111
+ if (!MONGORY_VALIDATE_TABLE(pool, condition)) {
114
112
  return NULL; // Invalid input
115
113
  }
116
114
  mongory_table *parsed_table = mongory_table_new(pool);
@@ -120,7 +118,7 @@ static inline mongory_value *mongory_matcher_implicit_array_parse_table(mongory_
120
118
  return NULL;
121
119
  }
122
120
 
123
- mongory_matcher_implicit_array_parse_table_context parse_ctx = {parsed_table, elem_match_sub_table};
121
+ mongory_matcher_implicit_array_parse_table_context parse_ctx = {parsed_table, elem_match_sub_table, false};
124
122
  mongory_table *original_condition_table = condition->data.t;
125
123
 
126
124
  original_condition_table->each(original_condition_table, &parse_ctx,
@@ -129,16 +127,16 @@ static inline mongory_value *mongory_matcher_implicit_array_parse_table(mongory_
129
127
  return NULL;
130
128
  }
131
129
 
132
- if (elem_match_sub_table->count > 0) {
133
- // If there were conditions for element matching, add them as an $elemMatch
134
- // clause to the main parsed_table.
130
+ if (elem_match_sub_table->count > 0 || parse_ctx.has_explicit_elem_match) {
131
+ // If there were conditions for element matching (or an explicit, possibly
132
+ // empty $elemMatch, which still needs an element), add them as an
133
+ // $elemMatch clause to the main parsed_table.
135
134
  mongory_value *wrapped_elem = mongory_value_wrap_t(pool, elem_match_sub_table);
136
135
  if (!wrapped_elem || !parsed_table->set(parsed_table, "$elemMatch", wrapped_elem)) {
137
136
  return NULL;
138
137
  }
139
138
  }
140
- // If elem_match_sub_table is empty, it's not added. The original parsed_table
141
- // (which might contain $size etc.) is returned.
139
+ // Otherwise the parsed_table (which might contain $size etc.) is returned as is.
142
140
  return mongory_value_wrap_t(pool, parsed_table);
143
141
  }
144
142
 
@@ -160,9 +158,10 @@ static inline mongory_value *mongory_matcher_implicit_array_parse_table(mongory_
160
158
  * @return The constructed implicit_array_matcher, or NULL on failure.
161
159
  */
162
160
  mongory_matcher *mongory_matcher_implicit_array_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
163
- if (!condition || !pool)
161
+ if (!MONGORY_VALIDATE_PTR(pool, condition)) {
164
162
  return NULL;
165
- mongory_compile_budget saved = mongory_compile_budget_begin();
163
+ }
164
+ mongory_compile_ctx saved = mongory_compile_ctx_begin();
166
165
  mongory_matcher *result = NULL;
167
166
  switch (condition->type) {
168
167
  case MONGORY_TYPE_TABLE: {
@@ -199,6 +198,6 @@ mongory_matcher *mongory_matcher_implicit_array_new(mongory_memory_pool *pool, m
199
198
  break;
200
199
  }
201
200
  }
202
- mongory_compile_budget_end(saved);
201
+ mongory_compile_ctx_end(saved);
203
202
  return result;
204
203
  }
@@ -35,7 +35,6 @@ typedef enum mongory_in_num_kind {
35
35
  typedef struct mongory_in_num_slot {
36
36
  mongory_in_num_kind kind;
37
37
  uint64_t key_bits; // int64_t bits (for INT) or IEEE-754 double bits (for DOUBLE)
38
- uint64_t hash;
39
38
  mongory_value *val;
40
39
  } mongory_in_num_slot;
41
40
 
@@ -57,6 +56,7 @@ typedef struct mongory_in_set {
57
56
  typedef struct mongory_inclusion_matcher {
58
57
  mongory_matcher base;
59
58
  mongory_in_set *set;
59
+ bool has_null; // A null item also matches a missing value.
60
60
  } mongory_inclusion_matcher;
61
61
 
62
62
  static inline uint64_t mongory_fnv1a_64(const char *s, size_t len) {
@@ -130,6 +130,21 @@ static inline mongory_in_num_kind mongory_in_classify_num(const mongory_value *v
130
130
  return MONGORY_IN_NUM_EMPTY;
131
131
  }
132
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;
143
+ }
144
+ *found = false;
145
+ return idx;
146
+ }
147
+
133
148
  static mongory_in_set *mongory_in_set_build(mongory_memory_pool *pool, mongory_array *arr) {
134
149
  size_t count = arr->count;
135
150
  size_t str_count = 0;
@@ -227,15 +242,9 @@ static mongory_in_set *mongory_in_set_build(mongory_memory_pool *pool, mongory_a
227
242
  }
228
243
  size_t len = strlen(s);
229
244
  uint64_t h = mongory_fnv1a_64(s, len);
230
- size_t idx = (size_t)(h & set->str_mask);
231
- while (set->str_slots[idx].str != NULL) {
232
- if (set->str_slots[idx].hash == h && set->str_slots[idx].len == len &&
233
- memcmp(set->str_slots[idx].str, s, len) == 0) {
234
- break;
235
- }
236
- idx = (idx + 1) & set->str_mask;
237
- }
238
- if (set->str_slots[idx].str == NULL) {
245
+ bool found = false;
246
+ size_t idx = mongory_in_str_probe(set, s, len, h, &found);
247
+ if (!found) {
239
248
  set->str_slots[idx].str = s;
240
249
  set->str_slots[idx].len = len;
241
250
  set->str_slots[idx].hash = h;
@@ -259,7 +268,6 @@ static mongory_in_set *mongory_in_set_build(mongory_memory_pool *pool, mongory_a
259
268
  if (set->num_slots[idx].kind == MONGORY_IN_NUM_EMPTY) {
260
269
  set->num_slots[idx].kind = kind;
261
270
  set->num_slots[idx].key_bits = bits;
262
- set->num_slots[idx].hash = h;
263
271
  set->num_slots[idx].val = elem;
264
272
  }
265
273
  } else if (!is_nan) {
@@ -289,18 +297,10 @@ static inline bool mongory_in_set_contains_scalar(const mongory_in_set *set, mon
289
297
  if (!set->str_slots || !v->data.s) {
290
298
  return false;
291
299
  }
292
- const char *s = v->data.s;
293
- size_t len = strlen(s);
294
- uint64_t h = mongory_fnv1a_64(s, len);
295
- size_t idx = (size_t)(h & set->str_mask);
296
- while (set->str_slots[idx].str != NULL) {
297
- if (set->str_slots[idx].hash == h && set->str_slots[idx].len == len &&
298
- memcmp(set->str_slots[idx].str, s, len) == 0) {
299
- return true;
300
- }
301
- idx = (idx + 1) & set->str_mask;
302
- }
303
- return false;
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
304
  }
305
305
  case MONGORY_TYPE_INT:
306
306
  case MONGORY_TYPE_DOUBLE: {
@@ -325,15 +325,9 @@ static inline bool mongory_in_set_contains_scalar(const mongory_in_set *set, mon
325
325
  }
326
326
  return false;
327
327
  }
328
- // Large integer/double outside exact 2^53 range -> check fallback
329
- for (size_t i = 0; i < set->fallback_count; i++) {
330
- mongory_value *cand = set->fallback[i];
331
- if (cand->comp(cand, v) == 0) {
332
- return true;
333
- }
334
- }
335
- return false;
328
+ // Large integer/double outside exact 2^53 range falls through to fallback scan.
336
329
  }
330
+ /* fall through */
337
331
  default:
338
332
  for (size_t i = 0; i < set->fallback_count; i++) {
339
333
  mongory_value *cand = set->fallback[i];
@@ -345,139 +339,94 @@ static inline bool mongory_in_set_contains_scalar(const mongory_in_set *set, mon
345
339
  }
346
340
  }
347
341
 
348
- /**
349
- * @brief Validates that the condition for an inclusion matcher is a valid
350
- * array.
351
- */
352
- static inline bool mongory_matcher_validate_array_condition(mongory_value *condition) {
353
- if (condition == NULL) {
354
- return false;
355
- }
356
- if (condition->type != MONGORY_TYPE_ARRAY) {
357
- return false;
358
- }
359
- if (condition->data.a == NULL) {
360
- return false;
361
- }
362
- return true;
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);
363
345
  }
364
346
 
365
347
  static inline bool mongory_matcher_in_match(mongory_matcher *matcher, mongory_value *value_to_check) {
366
- if (!value_to_check || !matcher->condition || !matcher->condition->data.a) {
367
- return false;
368
- }
369
-
370
348
  mongory_inclusion_matcher *inc = (mongory_inclusion_matcher *)matcher;
371
- mongory_in_set *set = inc->set;
372
-
373
- if (set != NULL) {
374
- if (value_to_check->type != MONGORY_TYPE_ARRAY) {
375
- return mongory_in_set_contains_scalar(set, value_to_check);
376
- }
377
- mongory_array *input_array = value_to_check->data.a;
378
- if (!input_array) {
379
- return false;
380
- }
381
- for (size_t i = 0; i < input_array->count; i++) {
382
- mongory_value *input_item = input_array->get(input_array, i);
383
- if (mongory_in_set_contains_scalar(set, input_item)) {
384
- return true;
385
- }
386
- }
349
+ if (!value_to_check) {
350
+ return inc->has_null;
351
+ }
352
+ if (!matcher->condition || !matcher->condition->data.a) {
387
353
  return false;
388
354
  }
389
-
390
- mongory_array *condition_array = matcher->condition->data.a;
391
-
392
355
  if (value_to_check->type != MONGORY_TYPE_ARRAY) {
393
- return mongory_array_includes(condition_array, value_to_check);
356
+ return mongory_inclusion_contains_scalar(inc, value_to_check);
394
357
  }
395
-
396
358
  mongory_array *input_array = value_to_check->data.a;
397
359
  if (!input_array) {
398
360
  return false;
399
361
  }
400
-
401
362
  for (size_t i = 0; i < input_array->count; i++) {
402
- mongory_value *input_item = input_array->get(input_array, i);
403
- if (mongory_array_includes(condition_array, input_item)) {
363
+ if (mongory_inclusion_contains_scalar(inc, input_array->get(input_array, i))) {
404
364
  return true;
405
365
  }
406
366
  }
407
-
408
367
  return false;
409
368
  }
410
369
 
411
370
  static mongory_inclusion_matcher *mongory_inclusion_matcher_alloc(mongory_memory_pool *pool, mongory_value *condition,
412
371
  void *extern_ctx, const char *name,
413
372
  mongory_matcher_match_func match_fn) {
414
- if (!pool || !pool->alloc) {
415
- return NULL;
416
- }
417
- if (!mongory_compile_budget_count_node(pool)) {
418
- return NULL;
419
- }
420
- mongory_inclusion_matcher *inc = MG_ALLOC_PTR(pool, mongory_inclusion_matcher);
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);
421
376
  if (!inc) {
422
- pool->error = &MONGORY_ALLOC_ERROR;
423
- return NULL;
424
- }
425
-
426
- inc->base.original_match = match_fn;
427
- inc->base.sub_count = 0;
428
- inc->base.pool = pool;
429
- inc->base.condition = condition;
430
- inc->base.name = mongory_string_cpy(pool, (char *)name);
431
- if (!inc->base.name) {
432
377
  return NULL;
433
378
  }
434
- inc->base.match = match_fn;
435
- inc->base.explain = mongory_matcher_base_explain;
436
- inc->base.traverse = mongory_matcher_leaf_traverse;
437
- inc->base.trace_stack = NULL;
438
- inc->base.trace_level = 0;
439
- inc->base.extern_ctx = extern_ctx;
440
- inc->base.parent = NULL;
441
- inc->set = NULL;
442
379
 
443
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
+ }
444
389
  if (cond_arr->count >= MONGORY_IN_HASH_THRESHOLD) {
445
390
  inc->set = mongory_in_set_build(pool, cond_arr);
446
391
  if (!inc->set) {
447
392
  return NULL;
448
393
  }
449
- if (inc->set->fallback_count == 0) {
450
- inc->base.priority = 2.5;
451
- } else {
452
- inc->base.priority = 2.5 + mongory_log((double)inc->set->fallback_count + 1.0, 1.5);
453
- }
394
+ inc->base.priority = 3.0 + (double)(inc->set->fallback_count / 8);
454
395
  } else {
455
- inc->base.priority = 1.0 + mongory_log((double)cond_arr->count + 1.0, 1.5);
396
+ inc->base.priority = 2.0 + (double)(cond_arr->count / 8);
456
397
  }
457
398
 
458
399
  return inc;
459
400
  }
460
401
 
461
- mongory_matcher *mongory_matcher_in_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
462
- if (!mongory_matcher_validate_array_condition(condition)) {
463
- mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$in condition must be a valid array.");
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)) {
407
+ return NULL;
408
+ }
409
+ if (condition == NULL || condition->type != MONGORY_TYPE_ARRAY || condition->data.a == NULL) {
410
+ mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, err_msg);
464
411
  return NULL;
465
412
  }
466
- mongory_inclusion_matcher *inc =
467
- mongory_inclusion_matcher_alloc(pool, condition, extern_ctx, "In", mongory_matcher_in_match);
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
+ }
468
417
  return inc ? &inc->base : NULL;
469
418
  }
470
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);
423
+ }
424
+
471
425
  static inline bool mongory_matcher_not_in_match(mongory_matcher *matcher, mongory_value *value) {
472
426
  return !mongory_matcher_in_match(matcher, value);
473
427
  }
474
428
 
475
429
  mongory_matcher *mongory_matcher_not_in_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
476
- if (!mongory_matcher_validate_array_condition(condition)) {
477
- mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$nin condition must be a valid array.");
478
- return NULL;
479
- }
480
- mongory_inclusion_matcher *inc =
481
- mongory_inclusion_matcher_alloc(pool, condition, extern_ctx, "Nin", mongory_matcher_not_in_match);
482
- return inc ? &inc->base : NULL;
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);
483
432
  }