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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +30 -0
- data/README.md +55 -73
- data/docs/clang_bridge.md +2 -2
- data/docs/performance.md +43 -7
- data/ext/mongory_ext/extconf.rb +15 -0
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +28 -43
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -1
- data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +3 -4
- data/ext/mongory_ext/mongory-core/src/foundations/array.c +28 -5
- data/ext/mongory_ext/mongory-core/src/foundations/config.c +24 -22
- data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +3 -3
- data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +22 -13
- data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +4 -3
- data/ext/mongory_ext/mongory-core/src/foundations/table.c +60 -224
- data/ext/mongory_ext/mongory-core/src/foundations/table_private.h +36 -0
- data/ext/mongory_ext/mongory-core/src/foundations/utils.c +30 -47
- data/ext/mongory_ext/mongory-core/src/foundations/utils.h +2 -5
- data/ext/mongory_ext/mongory-core/src/foundations/value.c +18 -32
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +89 -104
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +18 -11
- data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +35 -150
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +244 -418
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +30 -0
- data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +15 -56
- data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -39
- data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +14 -15
- data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +382 -74
- data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +373 -123
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +20 -12
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -4
- data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +214 -171
- data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +8 -16
- data/ext/mongory_ext/mongory_ext.c +26 -4
- data/ext/mongory_ext/mongory_ext_internal.h +15 -3
- data/ext/mongory_ext/rb_mongory_adapters.c +52 -19
- data/ext/mongory_ext/rb_mongory_matcher.c +115 -36
- data/ext/mongory_ext/rb_mongory_value.c +124 -51
- data/lib/mongory/converters/abstract_converter.rb +5 -5
- data/lib/mongory/converters/converted.rb +0 -4
- data/lib/mongory/converters/key_converter.rb +13 -36
- data/lib/mongory/converters/value_converter.rb +4 -4
- data/lib/mongory/matchers.rb +8 -7
- data/lib/mongory/query_builder.rb +9 -0
- data/lib/mongory/utils/context.rb +0 -6
- data/lib/mongory/utils/singleton_builder.rb +1 -4
- data/lib/mongory/version.rb +1 -1
- data/lib/mongory.rb +0 -1
- data/script/bench.rb +77 -4
- data/script/coverage.rb +84 -0
- metadata +7 -7
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
|
@@ -8,32 +8,35 @@
|
|
|
8
8
|
#include "value_matcher.h"
|
|
9
9
|
|
|
10
10
|
bool mongory_matcher_leaf_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
|
|
11
|
-
|
|
12
|
-
if (!continue_traverse)
|
|
11
|
+
if (!matcher || !ctx || !ctx->callback) {
|
|
13
12
|
return false;
|
|
14
|
-
|
|
15
|
-
return
|
|
13
|
+
}
|
|
14
|
+
return ctx->callback(matcher, ctx);
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
bool mongory_matcher_composite_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
|
|
18
|
+
if (!matcher || !ctx) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
19
21
|
void *prev_acc = ctx->acc;
|
|
20
22
|
if (!mongory_matcher_leaf_traverse(matcher, ctx))
|
|
21
23
|
return false;
|
|
22
24
|
mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
|
|
23
25
|
mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
|
|
26
|
+
if (!children) {
|
|
27
|
+
ctx->acc = prev_acc;
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
24
30
|
int total = (int)children->count;
|
|
25
31
|
mongory_matcher_traverse_context child_ctx = {
|
|
26
|
-
.pool = ctx->pool,
|
|
27
32
|
.level = ctx->level + 1,
|
|
28
|
-
.count = 0,
|
|
29
|
-
.total = total,
|
|
30
33
|
.acc = ctx->acc,
|
|
31
34
|
.callback = ctx->callback,
|
|
32
35
|
};
|
|
33
36
|
|
|
34
37
|
for (int i = 0; i < total; i++) {
|
|
35
38
|
mongory_matcher *child = (mongory_matcher *)children->get(children, i);
|
|
36
|
-
if (!child->traverse(child, &child_ctx))
|
|
39
|
+
if (!child || !child->traverse || !child->traverse(child, &child_ctx))
|
|
37
40
|
return false;
|
|
38
41
|
}
|
|
39
42
|
ctx->acc = prev_acc;
|
|
@@ -41,16 +44,21 @@ bool mongory_matcher_composite_traverse(mongory_matcher *matcher, mongory_matche
|
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
bool mongory_matcher_value_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
|
|
47
|
+
if (!matcher || !ctx) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
44
50
|
void *prev_acc = ctx->acc;
|
|
45
51
|
if (!mongory_matcher_leaf_traverse(matcher, ctx))
|
|
46
52
|
return false;
|
|
47
53
|
mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
|
|
48
|
-
mongory_matcher *
|
|
54
|
+
mongory_matcher *arm = mongory_literal_load_implicit(literal);
|
|
55
|
+
mongory_matcher *next_matcher = arm ? arm : literal->delegate_matcher;
|
|
56
|
+
if (!next_matcher || !next_matcher->traverse) {
|
|
57
|
+
ctx->acc = prev_acc;
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
49
60
|
mongory_matcher_traverse_context child_ctx = {
|
|
50
|
-
.pool = ctx->pool,
|
|
51
61
|
.level = ctx->level + 1,
|
|
52
|
-
.count = 0,
|
|
53
|
-
.total = 1,
|
|
54
62
|
.acc = ctx->acc,
|
|
55
63
|
.callback = ctx->callback,
|
|
56
64
|
};
|
|
@@ -6,13 +6,10 @@
|
|
|
6
6
|
typedef struct mongory_matcher_traverse_context mongory_matcher_traverse_context;
|
|
7
7
|
typedef bool (*mongory_matcher_traverse_func)(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
|
|
8
8
|
struct mongory_matcher_traverse_context {
|
|
9
|
-
mongory_memory_pool *pool;
|
|
10
9
|
int level;
|
|
11
|
-
int count;
|
|
12
|
-
int total;
|
|
13
10
|
void *acc;
|
|
14
11
|
mongory_matcher_traverse_func callback;
|
|
15
|
-
}
|
|
12
|
+
};
|
|
16
13
|
|
|
17
14
|
bool mongory_matcher_leaf_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
|
|
18
15
|
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* - $size checks the number of elements in an array against a condition.
|
|
12
12
|
*/
|
|
13
13
|
#include "value_matcher.h"
|
|
14
|
+
#include <string.h>
|
|
14
15
|
#include "../foundations/config_private.h" // For mongory_internal_value_converter
|
|
15
16
|
#include "../foundations/memory_pool_private.h" // For atomic & lock helpers
|
|
16
17
|
#include "../foundations/utils.h" // For mongory_try_parse_int, mongory_string_cpy
|
|
@@ -19,8 +20,6 @@
|
|
|
19
20
|
#include "base_matcher.h" // For mongory_matcher_base_new
|
|
20
21
|
#include "compare_matcher.h" // For mongory_matcher_equal_new
|
|
21
22
|
#include "composite_matcher.h" // For mongory_matcher_composite_new, mongory_matcher_condition_new
|
|
22
|
-
#include "existence_matcher.h" // For mongory_matcher_exists_new (used by null_new)
|
|
23
|
-
#include "matcher_explainable.h"
|
|
24
23
|
#include "matcher_traversable.h"
|
|
25
24
|
#include "mongory-core/foundations/array.h" // For mongory_array access
|
|
26
25
|
#include "mongory-core/foundations/table.h" // For mongory_table access
|
|
@@ -34,19 +33,10 @@
|
|
|
34
33
|
*
|
|
35
34
|
* This function is used by field matchers, $not, and $size.
|
|
36
35
|
* It checks the type of the input `value`.
|
|
37
|
-
* - If `value` is an array, it may delegate to
|
|
38
|
-
*
|
|
39
|
-
* more complex array matching scenarios where the condition itself might
|
|
40
|
-
* describe how array elements should be matched.
|
|
41
|
-
* - Otherwise (if `value` is not an array, or if array-specific handling is
|
|
42
|
-
* not triggered), it uses the `composite->left` matcher. `composite->left`
|
|
43
|
-
* is typically set up by `mongory_matcher_value_delegate` to handle the
|
|
44
|
-
* specific type of the literal condition (e.g., equality for simple values,
|
|
45
|
-
* regex matcher for regex conditions, table condition matcher for table
|
|
46
|
-
* conditions).
|
|
36
|
+
* - If `value` is an array, it may delegate to a lazily constructed `implicit_array_matcher`.
|
|
37
|
+
* - Otherwise, it delegates to `literal->delegate_matcher`.
|
|
47
38
|
*
|
|
48
|
-
* @param matcher The `
|
|
49
|
-
* `mongory_composite_matcher` where `left` and possibly `right` are set up).
|
|
39
|
+
* @param matcher The `mongory_value_matcher` instance.
|
|
50
40
|
* @param value The `mongory_value` to evaluate against the matcher's condition.
|
|
51
41
|
* @return True if the value matches, false otherwise.
|
|
52
42
|
*/
|
|
@@ -81,7 +71,7 @@ static inline bool mongory_matcher_value_match(mongory_matcher *matcher, mongory
|
|
|
81
71
|
if (value != NULL && value->type == MONGORY_TYPE_ARRAY) {
|
|
82
72
|
// Double-checked locking: lazy build runs only on the first array value seen,
|
|
83
73
|
// keeping non-array matching and subsequent array matches lock-free.
|
|
84
|
-
mongory_matcher *arm =
|
|
74
|
+
mongory_matcher *arm = mongory_literal_load_implicit(literal);
|
|
85
75
|
if (arm == NULL) {
|
|
86
76
|
mongory_memory_pool *build_pool = literal->base.pool;
|
|
87
77
|
mongory_memory_pool_lock(build_pool);
|
|
@@ -93,7 +83,8 @@ static inline bool mongory_matcher_value_match(mongory_matcher *matcher, mongory
|
|
|
93
83
|
}
|
|
94
84
|
if (arm != NULL) {
|
|
95
85
|
arm->parent = &literal->base;
|
|
96
|
-
double
|
|
86
|
+
double base_offset = (literal->base.name && strcmp(literal->base.name, "Not") == 0) ? 2.0 : 1.0;
|
|
87
|
+
double delta = (base_offset + arm->priority) - literal->base.priority;
|
|
97
88
|
if (delta != 0.0) {
|
|
98
89
|
for (mongory_matcher *cur = &literal->base; cur != NULL; cur = cur->parent) {
|
|
99
90
|
cur->priority += delta;
|
|
@@ -117,38 +108,15 @@ static inline bool mongory_matcher_value_match(mongory_matcher *matcher, mongory
|
|
|
117
108
|
}
|
|
118
109
|
}
|
|
119
110
|
|
|
120
|
-
/**
|
|
121
|
-
* @brief Internal helper to create a specialized matcher for a `MONGORY_TYPE_NULL`
|
|
122
|
-
* condition.
|
|
123
|
-
*
|
|
124
|
-
* This creates an OR condition:
|
|
125
|
-
* (`value` IS MONGORY_TYPE_NULL) OR (`value` DOES NOT EXIST (evaluates to
|
|
126
|
-
* false for $exists:true)). This means it matches actual BSON nulls or missing
|
|
127
|
-
* fields.
|
|
128
|
-
*
|
|
129
|
-
* @param pool Memory pool for allocation.
|
|
130
|
-
* @param condition The `mongory_value` which is of `MONGORY_TYPE_NULL`.
|
|
131
|
-
* @return A composite matcher for the NULL condition, or NULL on failure.
|
|
132
|
-
*/
|
|
133
|
-
static inline mongory_matcher *mongory_matcher_null_new(mongory_memory_pool *pool, void *extern_ctx) {
|
|
134
|
-
mongory_value *new_or_condition = MG_ARRAY_WRAP(pool, 2,
|
|
135
|
-
MG_TABLE_WRAP(pool, 1, "$eq", mongory_value_wrap_n(pool, NULL)),
|
|
136
|
-
MG_TABLE_WRAP(pool, 1, "$exists", mongory_value_wrap_b(pool, false))
|
|
137
|
-
);
|
|
138
|
-
if (!new_or_condition || pool->error != NULL) {
|
|
139
|
-
return NULL;
|
|
140
|
-
}
|
|
141
|
-
return mongory_matcher_or_new(pool, new_or_condition, extern_ctx);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
111
|
/**
|
|
145
112
|
* @brief Delegates creation of a simple literal matcher based on the
|
|
146
113
|
* `condition`'s type.
|
|
147
114
|
*
|
|
148
115
|
* - Table condition: creates a `mongory_matcher_condition_new`.
|
|
149
116
|
* - Regex condition: creates a `mongory_matcher_regex_new`.
|
|
150
|
-
* -
|
|
151
|
-
*
|
|
117
|
+
* - Other types (including `MONGORY_TYPE_NULL`, since `$eq: null` matches both
|
|
118
|
+
* explicit `null` and missing): creates an equality matcher
|
|
119
|
+
* (`mongory_matcher_equal_new`).
|
|
152
120
|
*
|
|
153
121
|
* @param pool Memory pool for allocation.
|
|
154
122
|
* @param condition The literal value or condition table.
|
|
@@ -156,18 +124,15 @@ static inline mongory_matcher *mongory_matcher_null_new(mongory_memory_pool *poo
|
|
|
156
124
|
*/
|
|
157
125
|
static inline mongory_matcher *mongory_matcher_value_delegate(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
|
|
158
126
|
if (!condition)
|
|
159
|
-
return mongory_matcher_equal_new(pool, NULL, extern_ctx);
|
|
127
|
+
return mongory_matcher_equal_new(pool, NULL, extern_ctx);
|
|
160
128
|
|
|
161
129
|
switch (condition->type) {
|
|
162
130
|
case MONGORY_TYPE_TABLE:
|
|
163
131
|
return mongory_matcher_condition_new(pool, condition, extern_ctx);
|
|
164
132
|
case MONGORY_TYPE_REGEX:
|
|
165
133
|
return mongory_matcher_regex_new(pool, condition, extern_ctx);
|
|
166
|
-
case MONGORY_TYPE_NULL:
|
|
167
|
-
// When the condition is explicitly `null`, e.g. `{ field: null }`
|
|
168
|
-
return mongory_matcher_null_new(pool, extern_ctx);
|
|
169
134
|
default:
|
|
170
|
-
// For boolean, int, double, string, array (equality), pointer, unsupported.
|
|
135
|
+
// For null, boolean, int, double, string, array (equality), pointer, unsupported.
|
|
171
136
|
return mongory_matcher_equal_new(pool, condition, extern_ctx);
|
|
172
137
|
}
|
|
173
138
|
}
|
|
@@ -176,23 +141,26 @@ static inline mongory_matcher *mongory_matcher_value_delegate(mongory_memory_poo
|
|
|
176
141
|
* @brief Match function for a field matcher.
|
|
177
142
|
*
|
|
178
143
|
* Extracts the value of `field_matcher->field` from the input `value`
|
|
179
|
-
* (table or array)
|
|
180
|
-
*
|
|
181
|
-
* `composite.left` (which was set up by `literal_delegate` based on the
|
|
182
|
-
* original condition for this field).
|
|
144
|
+
* (table or array), then applies `mongory_matcher_value_match` using
|
|
145
|
+
* `literal.delegate_matcher`.
|
|
183
146
|
*
|
|
184
147
|
* @param matcher Pointer to the `mongory_matcher` (a `mongory_field_matcher`).
|
|
185
148
|
* @param value The input table or array to extract the field from.
|
|
186
149
|
* @return True if the field's value matches the condition, false otherwise.
|
|
187
150
|
*/
|
|
188
151
|
static inline bool mongory_matcher_field_match(mongory_matcher *matcher, mongory_value *value) {
|
|
189
|
-
if (
|
|
152
|
+
if (!matcher) {
|
|
190
153
|
return false;
|
|
191
154
|
}
|
|
192
155
|
mongory_field_matcher *field_matcher = (mongory_field_matcher *)matcher;
|
|
193
156
|
if (!field_matcher->field)
|
|
194
157
|
return false; // No field specified.
|
|
195
158
|
|
|
159
|
+
if (value == NULL) { // Cannot extract field from NULL.
|
|
160
|
+
// A nested field under a missing parent evaluates its condition against MISSING.
|
|
161
|
+
return field_matcher->is_nested ? mongory_matcher_value_match(matcher, NULL) : false;
|
|
162
|
+
}
|
|
163
|
+
|
|
196
164
|
mongory_value *field_value = NULL;
|
|
197
165
|
char *field_key = field_matcher->field;
|
|
198
166
|
|
|
@@ -219,7 +187,8 @@ static inline bool mongory_matcher_field_match(mongory_matcher *matcher, mongory
|
|
|
219
187
|
// so child matchers like $exists: false or $nin can evaluate MISSING.
|
|
220
188
|
}
|
|
221
189
|
} else {
|
|
222
|
-
|
|
190
|
+
// Can only extract fields from tables or arrays; a nested field sees MISSING.
|
|
191
|
+
return field_matcher->is_nested ? mongory_matcher_value_match(matcher, NULL) : false;
|
|
223
192
|
}
|
|
224
193
|
|
|
225
194
|
// If the extracted field value is a pointer type that needs conversion
|
|
@@ -236,58 +205,190 @@ static inline bool mongory_matcher_field_match(mongory_matcher *matcher, mongory
|
|
|
236
205
|
field_value = mongory_internal_value_converter.shallow_convert(conversion_pool, field_value->data.ptr);
|
|
237
206
|
}
|
|
238
207
|
}
|
|
239
|
-
// Now, use the literal_match logic (which uses composite.left primarily)
|
|
240
|
-
// to match the extracted field_value.
|
|
241
208
|
return mongory_matcher_value_match(matcher, field_value);
|
|
242
209
|
}
|
|
243
210
|
|
|
244
|
-
mongory_matcher *
|
|
245
|
-
|
|
246
|
-
if (!pool
|
|
211
|
+
static mongory_matcher *mongory_matcher_path_field_new(mongory_memory_pool *pool, char *field_name,
|
|
212
|
+
mongory_value *condition_for_field, void *extern_ctx) {
|
|
213
|
+
if (!MONGORY_VALIDATE_PTR(pool, field_name)) {
|
|
247
214
|
return NULL;
|
|
248
215
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
mongory_field_matcher *field_m = MG_ALLOC_PTR(pool, mongory_field_matcher);
|
|
216
|
+
mongory_field_matcher *field_m = (mongory_field_matcher *)mongory_matcher_alloc_init(
|
|
217
|
+
pool, sizeof(mongory_field_matcher), condition_for_field, extern_ctx, mongory_matcher_field_match,
|
|
218
|
+
mongory_matcher_value_traverse, "Field", 1.0);
|
|
253
219
|
if (field_m == NULL) {
|
|
254
|
-
pool->error = &MONGORY_ALLOC_ERROR;
|
|
255
220
|
return NULL;
|
|
256
221
|
}
|
|
257
222
|
field_m->field = mongory_string_cpy(pool, field_name);
|
|
258
223
|
if (field_m->field == NULL) {
|
|
259
224
|
return NULL;
|
|
260
225
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
field_m->literal.base.pool = pool;
|
|
264
|
-
field_m->literal.base.name = mongory_string_cpy(pool, "Field");
|
|
265
|
-
if (field_m->literal.base.name == NULL) {
|
|
266
|
-
return NULL;
|
|
267
|
-
}
|
|
268
|
-
field_m->literal.base.match = mongory_matcher_field_match;
|
|
269
|
-
field_m->literal.base.original_match = mongory_matcher_field_match;
|
|
270
|
-
field_m->literal.base.sub_count = 1;
|
|
271
|
-
field_m->literal.base.condition = condition_for_field; // Original condition for the field
|
|
272
|
-
field_m->literal.base.explain = mongory_matcher_field_explain;
|
|
273
|
-
field_m->literal.base.traverse = mongory_matcher_value_traverse;
|
|
274
|
-
field_m->literal.base.extern_ctx = extern_ctx;
|
|
275
|
-
// The 'left' child of the composite is the actual matcher for the field's value,
|
|
276
|
-
// determined by the type of 'condition_for_field'.
|
|
226
|
+
field_m->is_nested = mongory_compile_ctx_in_field();
|
|
227
|
+
mongory_compile_ctx_enter_field();
|
|
277
228
|
field_m->literal.delegate_matcher = mongory_matcher_value_delegate(pool, condition_for_field, extern_ctx);
|
|
278
|
-
|
|
279
|
-
|
|
229
|
+
mongory_compile_ctx_leave_field();
|
|
280
230
|
if (field_m->literal.delegate_matcher == NULL) {
|
|
281
|
-
// Failed to create the delegate matcher for the condition.
|
|
282
231
|
return NULL;
|
|
283
232
|
}
|
|
284
233
|
field_m->literal.base.priority = 1.0 + field_m->literal.delegate_matcher->priority;
|
|
285
|
-
field_m->literal.base.parent = NULL;
|
|
286
234
|
field_m->literal.delegate_matcher->parent = (mongory_matcher *)field_m;
|
|
287
235
|
|
|
288
236
|
return (mongory_matcher *)field_m;
|
|
289
237
|
}
|
|
290
238
|
|
|
239
|
+
// ============================================================================
|
|
240
|
+
// Null Negation Lifting
|
|
241
|
+
//
|
|
242
|
+
// $ne: null and $nin with a null item on a field path (also when nested under
|
|
243
|
+
// sub-field objects) compile to NOT of their positive form ($eq: null, $in)
|
|
244
|
+
// over the whole path, so missing parents and arrays along the path behave as
|
|
245
|
+
// in MongoDB. Other negations and operator arguments ($elemMatch, $and, ...)
|
|
246
|
+
// are left as is.
|
|
247
|
+
// ============================================================================
|
|
248
|
+
|
|
249
|
+
static inline bool mongory_value_is_null(mongory_value *value) {
|
|
250
|
+
return value != NULL && value->type == MONGORY_TYPE_NULL;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
static inline bool mongory_is_negation_operator(char *key, mongory_value *value) {
|
|
254
|
+
if (strcmp(key, "$ne") == 0) {
|
|
255
|
+
return mongory_value_is_null(value);
|
|
256
|
+
}
|
|
257
|
+
if (strcmp(key, "$nin") != 0 || value == NULL || value->type != MONGORY_TYPE_ARRAY || value->data.a == NULL) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
mongory_array *items = value->data.a;
|
|
261
|
+
for (size_t i = 0; i < items->count; i++) {
|
|
262
|
+
if (mongory_value_is_null(items->get(items, i))) {
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
static bool mongory_condition_has_negation(mongory_value *condition, size_t depth);
|
|
270
|
+
|
|
271
|
+
typedef struct mongory_negation_scan_context {
|
|
272
|
+
size_t depth;
|
|
273
|
+
bool found;
|
|
274
|
+
} mongory_negation_scan_context;
|
|
275
|
+
|
|
276
|
+
static bool mongory_negation_scan_pair(char *key, mongory_value *value, void *acc) {
|
|
277
|
+
mongory_negation_scan_context *ctx = (mongory_negation_scan_context *)acc;
|
|
278
|
+
ctx->found = key[0] == '$' ? mongory_is_negation_operator(key, value)
|
|
279
|
+
: mongory_condition_has_negation(value, ctx->depth + 1);
|
|
280
|
+
return !ctx->found;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Read-only scan, so conditions without negations compile with no extra allocation.
|
|
284
|
+
static bool mongory_condition_has_negation(mongory_value *condition, size_t depth) {
|
|
285
|
+
if (condition == NULL || condition->type != MONGORY_TYPE_TABLE || condition->data.t == NULL) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
size_t max_depth = mongory_max_depth_get();
|
|
289
|
+
if (max_depth > 0 && depth > max_depth) {
|
|
290
|
+
return false; // Too deep to compile anyway; the builder reports the depth error.
|
|
291
|
+
}
|
|
292
|
+
mongory_negation_scan_context ctx = {depth, false};
|
|
293
|
+
condition->data.t->each(condition->data.t, &ctx, mongory_negation_scan_pair);
|
|
294
|
+
return ctx.found;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
static bool mongory_condition_split_negations(mongory_memory_pool *pool, mongory_value *condition, mongory_value **pos,
|
|
298
|
+
mongory_array *negs);
|
|
299
|
+
|
|
300
|
+
typedef struct mongory_negation_split_context {
|
|
301
|
+
mongory_memory_pool *pool;
|
|
302
|
+
mongory_table *rest;
|
|
303
|
+
mongory_array *negs;
|
|
304
|
+
} mongory_negation_split_context;
|
|
305
|
+
|
|
306
|
+
static bool mongory_negation_split_pair(char *key, mongory_value *value, void *acc) {
|
|
307
|
+
mongory_negation_split_context *ctx = (mongory_negation_split_context *)acc;
|
|
308
|
+
mongory_memory_pool *pool = ctx->pool;
|
|
309
|
+
if (key[0] == '$') {
|
|
310
|
+
if (!mongory_is_negation_operator(key, value)) {
|
|
311
|
+
return ctx->rest->set(ctx->rest, key, value);
|
|
312
|
+
}
|
|
313
|
+
mongory_value *neg = MG_TABLE_WRAP(pool, 1, strcmp(key, "$ne") == 0 ? "$eq" : "$in", value);
|
|
314
|
+
return neg != NULL && pool->error == NULL && ctx->negs->push(ctx->negs, neg);
|
|
315
|
+
}
|
|
316
|
+
if (!mongory_condition_has_negation(value, 0)) {
|
|
317
|
+
return ctx->rest->set(ctx->rest, key, value);
|
|
318
|
+
}
|
|
319
|
+
// Sub-field negations are re-rooted at this field: {b: {$ne: null}} -> NOT {b: {$eq: null}}.
|
|
320
|
+
mongory_value *sub_pos = NULL;
|
|
321
|
+
mongory_array *sub_negs = mongory_array_new(pool);
|
|
322
|
+
if (sub_negs == NULL || !mongory_condition_split_negations(pool, value, &sub_pos, sub_negs)) {
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
for (size_t i = 0; i < sub_negs->count; i++) {
|
|
326
|
+
mongory_value *neg = MG_TABLE_WRAP(pool, 1, key, sub_negs->get(sub_negs, i));
|
|
327
|
+
if (neg == NULL || pool->error != NULL || !ctx->negs->push(ctx->negs, neg)) {
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return sub_pos == NULL || ctx->rest->set(ctx->rest, key, sub_pos);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Splits a table condition into its positive part (*pos, NULL when empty) and
|
|
335
|
+
// the positive forms of its negations (appended to negs).
|
|
336
|
+
static bool mongory_condition_split_negations(mongory_memory_pool *pool, mongory_value *condition, mongory_value **pos,
|
|
337
|
+
mongory_array *negs) {
|
|
338
|
+
if (!mongory_compile_ctx_enter_depth(pool)) {
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
mongory_table *rest = mongory_table_new(pool);
|
|
342
|
+
mongory_negation_split_context ctx = {pool, rest, negs};
|
|
343
|
+
bool ok = rest != NULL && condition->data.t->each(condition->data.t, &ctx, mongory_negation_split_pair);
|
|
344
|
+
mongory_compile_ctx_leave_depth();
|
|
345
|
+
if (!ok || pool->error != NULL) {
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
*pos = rest->count > 0 ? mongory_value_wrap_t(pool, rest) : NULL;
|
|
349
|
+
return rest->count == 0 || *pos != NULL;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
static mongory_matcher *mongory_matcher_field_lift_negations(mongory_memory_pool *pool, char *field_name,
|
|
353
|
+
mongory_value *condition, void *extern_ctx) {
|
|
354
|
+
// Outside a field (document root or $elemMatch element), the negation only
|
|
355
|
+
// matches records that can hold fields.
|
|
356
|
+
bool document_only = !mongory_compile_ctx_in_field();
|
|
357
|
+
mongory_value *pos = NULL;
|
|
358
|
+
mongory_array *negs = mongory_array_new(pool);
|
|
359
|
+
mongory_array *children = mongory_array_new(pool);
|
|
360
|
+
if (negs == NULL || children == NULL || !mongory_condition_split_negations(pool, condition, &pos, negs)) {
|
|
361
|
+
return NULL;
|
|
362
|
+
}
|
|
363
|
+
if (pos != NULL) {
|
|
364
|
+
mongory_matcher *positive = mongory_matcher_path_field_new(pool, field_name, pos, extern_ctx);
|
|
365
|
+
if (positive == NULL || !children->push(children, (mongory_value *)positive)) {
|
|
366
|
+
return NULL;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
for (size_t i = 0; i < negs->count; i++) {
|
|
370
|
+
mongory_value *neg = negs->get(negs, i);
|
|
371
|
+
mongory_matcher *inner = mongory_matcher_path_field_new(pool, field_name, neg, extern_ctx);
|
|
372
|
+
mongory_matcher *not_matcher =
|
|
373
|
+
inner ? mongory_matcher_negation_new(pool, inner, neg, document_only, extern_ctx) : NULL;
|
|
374
|
+
if (not_matcher == NULL || !children->push(children, (mongory_value *)not_matcher)) {
|
|
375
|
+
return NULL;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (children->count == 1) {
|
|
379
|
+
return (mongory_matcher *)children->get(children, 0);
|
|
380
|
+
}
|
|
381
|
+
return mongory_matcher_and_of_new(pool, children, condition, extern_ctx);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
mongory_matcher *mongory_matcher_field_new(mongory_memory_pool *pool, char *field_name, mongory_value *condition_for_field,
|
|
385
|
+
void *extern_ctx) {
|
|
386
|
+
if (mongory_condition_has_negation(condition_for_field, 0)) {
|
|
387
|
+
return mongory_matcher_field_lift_negations(pool, field_name, condition_for_field, extern_ctx);
|
|
388
|
+
}
|
|
389
|
+
return mongory_matcher_path_field_new(pool, field_name, condition_for_field, extern_ctx);
|
|
390
|
+
}
|
|
391
|
+
|
|
291
392
|
/**
|
|
292
393
|
* @brief Match function for a NOT matcher.
|
|
293
394
|
* Negates the result of `mongory_matcher_value_match`.
|
|
@@ -296,56 +397,40 @@ mongory_matcher *mongory_matcher_field_new(mongory_memory_pool *pool, char *fiel
|
|
|
296
397
|
* @return True if the literal match is false, false if it's true.
|
|
297
398
|
*/
|
|
298
399
|
static inline bool mongory_matcher_not_match(mongory_matcher *matcher, mongory_value *value) {
|
|
299
|
-
|
|
400
|
+
if (!matcher) {
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
300
403
|
return !mongory_matcher_value_match(matcher, value);
|
|
301
404
|
}
|
|
302
405
|
|
|
303
|
-
mongory_matcher *
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
if (!
|
|
308
|
-
return NULL;
|
|
309
|
-
}
|
|
310
|
-
if (!mongory_compile_budget_count_node(pool)) {
|
|
311
|
-
mongory_compile_budget_leave_depth();
|
|
312
|
-
return NULL;
|
|
313
|
-
}
|
|
314
|
-
mongory_value_matcher *literal = MG_ALLOC_PTR(pool, mongory_value_matcher);
|
|
315
|
-
if (!literal) {
|
|
316
|
-
pool->error = &MONGORY_ALLOC_ERROR;
|
|
317
|
-
mongory_compile_budget_leave_depth();
|
|
406
|
+
static mongory_matcher *mongory_matcher_value_wrapper_new(mongory_memory_pool *pool, mongory_value *condition,
|
|
407
|
+
void *extern_ctx, const char *name,
|
|
408
|
+
mongory_matcher_match_func match_fn, double base_prio,
|
|
409
|
+
double prio_scale) {
|
|
410
|
+
if (!mongory_validate_pool(pool) || !mongory_compile_ctx_enter_depth(pool)) {
|
|
318
411
|
return NULL;
|
|
319
412
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
if (
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
literal->base.match = mongory_matcher_not_match;
|
|
332
|
-
literal->base.original_match = mongory_matcher_not_match;
|
|
333
|
-
literal->base.name = mongory_string_cpy(pool, "Not");
|
|
334
|
-
if (!literal->base.name) {
|
|
335
|
-
mongory_compile_budget_leave_depth();
|
|
336
|
-
return NULL;
|
|
413
|
+
mongory_value_matcher *literal = (mongory_value_matcher *)mongory_matcher_alloc_init(
|
|
414
|
+
pool, sizeof(mongory_value_matcher), condition, extern_ctx, match_fn, mongory_matcher_value_traverse, name,
|
|
415
|
+
base_prio);
|
|
416
|
+
if (literal) {
|
|
417
|
+
literal->delegate_matcher = mongory_matcher_value_delegate(pool, condition, extern_ctx);
|
|
418
|
+
if (literal->delegate_matcher) {
|
|
419
|
+
literal->base.priority = base_prio + literal->delegate_matcher->priority * prio_scale;
|
|
420
|
+
literal->delegate_matcher->parent = (mongory_matcher *)literal;
|
|
421
|
+
} else {
|
|
422
|
+
literal = NULL;
|
|
423
|
+
}
|
|
337
424
|
}
|
|
338
|
-
|
|
339
|
-
literal->base.traverse = mongory_matcher_value_traverse;
|
|
340
|
-
literal->base.sub_count = 1;
|
|
341
|
-
literal->base.extern_ctx = extern_ctx;
|
|
342
|
-
literal->base.priority = 1.0 + literal->delegate_matcher->priority;
|
|
343
|
-
literal->base.parent = NULL;
|
|
344
|
-
literal->delegate_matcher->parent = (mongory_matcher *)literal;
|
|
345
|
-
mongory_compile_budget_leave_depth();
|
|
425
|
+
mongory_compile_ctx_leave_depth();
|
|
346
426
|
return (mongory_matcher *)literal;
|
|
347
427
|
}
|
|
348
428
|
|
|
429
|
+
mongory_matcher *mongory_matcher_not_new(mongory_memory_pool *pool, mongory_value *condition_to_negate, void *extern_ctx) {
|
|
430
|
+
return mongory_matcher_value_wrapper_new(pool, condition_to_negate, extern_ctx, "Not", mongory_matcher_not_match,
|
|
431
|
+
2.0, 1.0);
|
|
432
|
+
}
|
|
433
|
+
|
|
349
434
|
/**
|
|
350
435
|
* @brief Match function for a $size matcher.
|
|
351
436
|
* Checks if the input `value` (must be an array) has a size that matches
|
|
@@ -355,11 +440,12 @@ mongory_matcher *mongory_matcher_not_new(mongory_memory_pool *pool, mongory_valu
|
|
|
355
440
|
* @return True if array size matches condition, false otherwise.
|
|
356
441
|
*/
|
|
357
442
|
static inline bool mongory_matcher_size_match(mongory_matcher *matcher, mongory_value *value) {
|
|
358
|
-
if (!value || value->type != MONGORY_TYPE_ARRAY || !value->data.a) {
|
|
443
|
+
if (!matcher || !value || value->type != MONGORY_TYPE_ARRAY || !value->data.a) {
|
|
359
444
|
return false; // $size only applies to valid arrays.
|
|
360
445
|
}
|
|
361
446
|
mongory_array *array = value->data.a;
|
|
362
|
-
|
|
447
|
+
mongory_memory_pool *size_pool = value->pool ? value->pool : matcher->pool;
|
|
448
|
+
mongory_value *size_val = mongory_value_wrap_i(size_pool, (int64_t)array->count);
|
|
363
449
|
if (!size_val) {
|
|
364
450
|
return false;
|
|
365
451
|
}
|
|
@@ -367,50 +453,7 @@ static inline bool mongory_matcher_size_match(mongory_matcher *matcher, mongory_
|
|
|
367
453
|
}
|
|
368
454
|
|
|
369
455
|
mongory_matcher *mongory_matcher_size_new(mongory_memory_pool *pool, mongory_value *size_condition, void *extern_ctx) {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
}
|
|
373
|
-
if (!mongory_compile_budget_enter_depth(pool)) {
|
|
374
|
-
return NULL;
|
|
375
|
-
}
|
|
376
|
-
if (!mongory_compile_budget_count_node(pool)) {
|
|
377
|
-
mongory_compile_budget_leave_depth();
|
|
378
|
-
return NULL;
|
|
379
|
-
}
|
|
380
|
-
mongory_value_matcher *literal = MG_ALLOC_PTR(pool, mongory_value_matcher);
|
|
381
|
-
if (!literal) {
|
|
382
|
-
pool->error = &MONGORY_ALLOC_ERROR;
|
|
383
|
-
mongory_compile_budget_leave_depth();
|
|
384
|
-
return NULL;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
// The 'left' child is the matcher for the size_condition itself.
|
|
388
|
-
// E.g., if {$size: {$gt: 5}}, size_condition is {$gt: 5}, and
|
|
389
|
-
// composite.left becomes a "greater than 5" matcher.
|
|
390
|
-
literal->delegate_matcher = mongory_matcher_value_delegate(pool, size_condition, extern_ctx);
|
|
391
|
-
if (!literal->delegate_matcher) {
|
|
392
|
-
mongory_compile_budget_leave_depth();
|
|
393
|
-
return NULL;
|
|
394
|
-
}
|
|
395
|
-
literal->implicit_array_matcher = NULL;
|
|
396
|
-
|
|
397
|
-
literal->base.pool = pool;
|
|
398
|
-
literal->base.condition = size_condition;
|
|
399
|
-
literal->base.match = mongory_matcher_size_match;
|
|
400
|
-
literal->base.original_match = mongory_matcher_size_match;
|
|
401
|
-
literal->base.name = mongory_string_cpy(pool, "Size");
|
|
402
|
-
if (!literal->base.name) {
|
|
403
|
-
mongory_compile_budget_leave_depth();
|
|
404
|
-
return NULL;
|
|
405
|
-
}
|
|
406
|
-
literal->base.explain = mongory_matcher_value_explain;
|
|
407
|
-
literal->base.traverse = mongory_matcher_value_traverse;
|
|
408
|
-
literal->base.sub_count = 1;
|
|
409
|
-
literal->base.extern_ctx = extern_ctx;
|
|
410
|
-
literal->base.priority = 1.0 + literal->delegate_matcher->priority;
|
|
411
|
-
literal->base.parent = NULL;
|
|
412
|
-
literal->delegate_matcher->parent = (mongory_matcher *)literal;
|
|
413
|
-
mongory_compile_budget_leave_depth();
|
|
414
|
-
return (mongory_matcher *)literal;
|
|
456
|
+
return mongory_matcher_value_wrapper_new(pool, size_condition, extern_ctx, "Size", mongory_matcher_size_match,
|
|
457
|
+
1.0, 0.5);
|
|
415
458
|
}
|
|
416
459
|
|
|
@@ -11,11 +11,11 @@
|
|
|
11
11
|
* table.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
#include "../foundations/memory_pool_private.h"
|
|
14
15
|
#include "base_matcher.h"
|
|
15
16
|
#include "mongory-core/foundations/memory_pool.h"
|
|
16
17
|
#include "mongory-core/foundations/value.h"
|
|
17
18
|
#include "mongory-core/matchers/matcher.h" // For mongory_matcher structure
|
|
18
|
-
#include "matcher_explainable.h"
|
|
19
19
|
#include "composite_matcher.h"
|
|
20
20
|
|
|
21
21
|
typedef struct mongory_value_matcher {
|
|
@@ -24,6 +24,10 @@ typedef struct mongory_value_matcher {
|
|
|
24
24
|
mongory_matcher *implicit_array_matcher;
|
|
25
25
|
} mongory_value_matcher;
|
|
26
26
|
|
|
27
|
+
static inline mongory_matcher *mongory_literal_load_implicit(mongory_value_matcher *literal) {
|
|
28
|
+
return mongory_atomic_load_ptr_acquire(&literal->implicit_array_matcher);
|
|
29
|
+
}
|
|
30
|
+
|
|
27
31
|
/**
|
|
28
32
|
* @struct mongory_field_matcher
|
|
29
33
|
* @brief Specialized composite matcher for matching a specific field.
|
|
@@ -32,6 +36,7 @@ typedef struct mongory_value_matcher {
|
|
|
32
36
|
typedef struct mongory_field_matcher {
|
|
33
37
|
mongory_value_matcher literal; /**< Base composite matcher structure. */
|
|
34
38
|
char *field; /**< Name/index of the field to match. Copied string. */
|
|
39
|
+
bool is_nested; /**< True when enclosed by another field matcher (non-root). */
|
|
35
40
|
} mongory_field_matcher;
|
|
36
41
|
/**
|
|
37
42
|
* @brief Creates a "field" matcher.
|
|
@@ -48,6 +53,8 @@ typedef struct mongory_field_matcher {
|
|
|
48
53
|
* @param condition The `mongory_value` condition to apply to the field's value.
|
|
49
54
|
* This condition is processed by `mongory_matcher_value_delegate` to determine
|
|
50
55
|
* the actual sub-matcher (e.g., equality, regex, nested table condition).
|
|
56
|
+
* Null negations ($ne: null, $nin with a null item), also under sub-fields,
|
|
57
|
+
* are lifted into "Not" nodes wrapping the field of their positive form.
|
|
51
58
|
* @return A new field matcher, or NULL on failure.
|
|
52
59
|
*/
|
|
53
60
|
mongory_matcher *mongory_matcher_field_new(mongory_memory_pool *pool, char *field, mongory_value *condition, void *extern_ctx);
|
|
@@ -79,19 +86,4 @@ mongory_matcher *mongory_matcher_not_new(mongory_memory_pool *pool, mongory_valu
|
|
|
79
86
|
*/
|
|
80
87
|
mongory_matcher *mongory_matcher_size_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
|
|
81
88
|
|
|
82
|
-
/**
|
|
83
|
-
* @brief Creates a "literal" matcher (deprecated or internal use).
|
|
84
|
-
*
|
|
85
|
-
* This function seems to be a more generic entry point that delegates to
|
|
86
|
-
* `mongory_matcher_value_delegate` and potentially `implicit_array_matcher`.
|
|
87
|
-
* Its direct public use might be limited compared to `field_new` or specific
|
|
88
|
-
* operator matchers. It appears to be part of the internal logic for how field
|
|
89
|
-
* values are matched.
|
|
90
|
-
*
|
|
91
|
-
* @param pool Memory pool for allocation.
|
|
92
|
-
* @param condition The literal `mongory_value` or condition table.
|
|
93
|
-
* @return A new literal matcher, or NULL on failure.
|
|
94
|
-
*/
|
|
95
|
-
mongory_matcher *mongory_matcher_value_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
|
|
96
|
-
|
|
97
89
|
#endif /* MONGORY_MATCHER_VALUE_H */
|