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
|
@@ -22,34 +22,302 @@
|
|
|
22
22
|
#include <mongory-core.h> // General include, might not be strictly necessary here
|
|
23
23
|
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
*
|
|
30
|
-
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
*
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// Dotted-Key Condition Expansion
|
|
27
|
+
// ============================================================================
|
|
28
|
+
|
|
29
|
+
static mongory_value *mongory_expand_table(mongory_memory_pool *pool, mongory_value *value, size_t depth);
|
|
30
|
+
|
|
31
|
+
static bool mongory_expand_deep_merge_set(mongory_memory_pool *pool, mongory_table *table, char *key,
|
|
32
|
+
mongory_value *value);
|
|
33
|
+
|
|
34
|
+
typedef struct mongory_expand_merge_ctx {
|
|
35
|
+
mongory_memory_pool *pool;
|
|
36
|
+
mongory_table *target;
|
|
37
|
+
} mongory_expand_merge_ctx;
|
|
38
|
+
|
|
39
|
+
static bool mongory_expand_merge_each(char *key, mongory_value *value, void *acc) {
|
|
40
|
+
mongory_expand_merge_ctx *ctx = (mongory_expand_merge_ctx *)acc;
|
|
41
|
+
return mongory_expand_deep_merge_set(ctx->pool, ctx->target, key, value);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static bool mongory_expand_deep_merge_set(mongory_memory_pool *pool, mongory_table *table, char *key,
|
|
45
|
+
mongory_value *value) {
|
|
46
|
+
mongory_value *existing = table->get(table, key);
|
|
47
|
+
if (!existing) {
|
|
48
|
+
return table->set(table, key, value);
|
|
49
|
+
}
|
|
50
|
+
if (existing->type == MONGORY_TYPE_TABLE && existing->data.t &&
|
|
51
|
+
value && value->type == MONGORY_TYPE_TABLE && value->data.t &&
|
|
52
|
+
key[0] != '$') {
|
|
53
|
+
mongory_expand_merge_ctx ctx = {pool, existing->data.t};
|
|
54
|
+
return value->data.t->each(value->data.t, &ctx, mongory_expand_merge_each);
|
|
55
|
+
}
|
|
56
|
+
// Leaf collision: preserve both conditions in `$and`.
|
|
57
|
+
table->del(table, key);
|
|
58
|
+
mongory_table *left_branch = mongory_table_new(pool);
|
|
59
|
+
mongory_table *right_branch = mongory_table_new(pool);
|
|
60
|
+
if (!left_branch || !right_branch) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if (!left_branch->set(left_branch, key, existing) || !right_branch->set(right_branch, key, value)) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
mongory_value *left_val = mongory_value_wrap_t(pool, left_branch);
|
|
67
|
+
mongory_value *right_val = mongory_value_wrap_t(pool, right_branch);
|
|
68
|
+
if (!left_val || !right_val) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
mongory_value *and_val = table->get(table, "$and");
|
|
73
|
+
mongory_array *and_arr = NULL;
|
|
74
|
+
if (and_val && and_val->type == MONGORY_TYPE_ARRAY && and_val->data.a) {
|
|
75
|
+
and_arr = and_val->data.a;
|
|
76
|
+
} else {
|
|
77
|
+
and_arr = mongory_array_new(pool);
|
|
78
|
+
if (!and_arr) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
if (and_val) {
|
|
82
|
+
mongory_table *prev_and = mongory_table_new(pool);
|
|
83
|
+
if (!prev_and || !prev_and->set(prev_and, "$and", and_val)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
mongory_value *prev_and_val = mongory_value_wrap_t(pool, prev_and);
|
|
87
|
+
if (!prev_and_val || !and_arr->push(and_arr, prev_and_val)) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
mongory_value *and_arr_val = mongory_value_wrap_a(pool, and_arr);
|
|
92
|
+
if (!and_arr_val || !table->set(table, "$and", and_arr_val)) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return and_arr->push(and_arr, left_val) && and_arr->push(and_arr, right_val);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
static char **mongory_split_dotted_key(mongory_memory_pool *pool, const char *key, size_t base_depth,
|
|
100
|
+
size_t *count_out) {
|
|
101
|
+
size_t len = strlen(key);
|
|
102
|
+
char *buf = (char *)MG_ALLOC(pool, len + 1);
|
|
103
|
+
if (!buf) {
|
|
104
|
+
pool->error = &MONGORY_ALLOC_ERROR;
|
|
105
|
+
return NULL;
|
|
106
|
+
}
|
|
107
|
+
size_t out = 0;
|
|
108
|
+
size_t segments = 1;
|
|
109
|
+
for (size_t i = 0; i < len; i++) {
|
|
110
|
+
if (key[i] == '\\' && key[i + 1] == '.') {
|
|
111
|
+
buf[out++] = '.';
|
|
112
|
+
i++;
|
|
113
|
+
} else if (key[i] == '.') {
|
|
114
|
+
buf[out++] = '\0';
|
|
115
|
+
segments++;
|
|
116
|
+
} else {
|
|
117
|
+
buf[out++] = key[i];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
buf[out] = '\0';
|
|
121
|
+
while (segments > 1 && out > 0 && buf[out - 1] == '\0') {
|
|
122
|
+
out--;
|
|
123
|
+
segments--;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
size_t max_depth = mongory_max_depth_get();
|
|
127
|
+
if (max_depth > 0 && base_depth + (segments - 1) > max_depth) {
|
|
128
|
+
pool->error = &MONGORY_DEPTH_LIMIT_ERROR;
|
|
50
129
|
return NULL;
|
|
51
130
|
}
|
|
52
131
|
|
|
132
|
+
char **parts = MG_ALLOC_ARY(pool, char *, segments);
|
|
133
|
+
if (!parts) {
|
|
134
|
+
pool->error = &MONGORY_ALLOC_ERROR;
|
|
135
|
+
return NULL;
|
|
136
|
+
}
|
|
137
|
+
char *cursor = buf;
|
|
138
|
+
for (size_t i = 0; i < segments; i++) {
|
|
139
|
+
parts[i] = cursor;
|
|
140
|
+
cursor += strlen(cursor) + 1;
|
|
141
|
+
}
|
|
142
|
+
*count_out = segments;
|
|
143
|
+
return parts;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
static inline bool mongory_is_logical_list_op(const char *key) {
|
|
147
|
+
return strcmp(key, "$and") == 0 || strcmp(key, "$or") == 0 || strcmp(key, "$nor") == 0;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
static inline bool mongory_is_nested_table_op(const char *key) {
|
|
151
|
+
return strcmp(key, "$elemMatch") == 0 || strcmp(key, "$every") == 0 ||
|
|
152
|
+
strcmp(key, "$not") == 0 || strcmp(key, "$size") == 0;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static mongory_value *mongory_expand_value_for_key(mongory_memory_pool *pool, const char *key, mongory_value *value,
|
|
156
|
+
size_t depth) {
|
|
157
|
+
if (!value) {
|
|
158
|
+
return value;
|
|
159
|
+
}
|
|
160
|
+
if (key[0] == '$') {
|
|
161
|
+
if (mongory_is_logical_list_op(key)) {
|
|
162
|
+
if (value->type != MONGORY_TYPE_ARRAY || !value->data.a) {
|
|
163
|
+
return value;
|
|
164
|
+
}
|
|
165
|
+
mongory_array *src = value->data.a;
|
|
166
|
+
mongory_array *dst = mongory_array_new(pool);
|
|
167
|
+
if (!dst) {
|
|
168
|
+
return NULL;
|
|
169
|
+
}
|
|
170
|
+
for (size_t i = 0; i < src->count; i++) {
|
|
171
|
+
mongory_value *elem = src->get(src, i);
|
|
172
|
+
mongory_value *norm = (elem && elem->type == MONGORY_TYPE_TABLE && elem->data.t)
|
|
173
|
+
? mongory_expand_table(pool, elem, depth + 1)
|
|
174
|
+
: elem;
|
|
175
|
+
if (pool->error != NULL || !dst->push(dst, norm)) {
|
|
176
|
+
return NULL;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
mongory_value *wrapped = mongory_value_wrap_a(pool, dst);
|
|
180
|
+
if (wrapped) {
|
|
181
|
+
wrapped->origin = value->origin;
|
|
182
|
+
}
|
|
183
|
+
return wrapped;
|
|
184
|
+
}
|
|
185
|
+
if (mongory_is_nested_table_op(key)) {
|
|
186
|
+
if (value->type == MONGORY_TYPE_TABLE && value->data.t) {
|
|
187
|
+
return mongory_expand_table(pool, value, depth + 1);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// Literal comparison/inclusion/existence/regex and custom operators keep raw operands verbatim.
|
|
191
|
+
return value;
|
|
192
|
+
}
|
|
193
|
+
if (value->type == MONGORY_TYPE_TABLE && value->data.t) {
|
|
194
|
+
return mongory_expand_table(pool, value, depth + 1);
|
|
195
|
+
}
|
|
196
|
+
return value;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
typedef struct mongory_expand_table_ctx {
|
|
200
|
+
mongory_memory_pool *pool;
|
|
201
|
+
mongory_table *result;
|
|
202
|
+
size_t depth;
|
|
203
|
+
} mongory_expand_table_ctx;
|
|
204
|
+
|
|
205
|
+
static bool mongory_expand_table_pair(char *key, mongory_value *value, void *acc) {
|
|
206
|
+
mongory_expand_table_ctx *ctx = (mongory_expand_table_ctx *)acc;
|
|
207
|
+
mongory_memory_pool *pool = ctx->pool;
|
|
208
|
+
size_t count = 0;
|
|
209
|
+
char **parts = mongory_split_dotted_key(pool, key, ctx->depth, &count);
|
|
210
|
+
if (!parts) {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
mongory_value *nested = mongory_expand_value_for_key(pool, parts[count - 1], value, ctx->depth + (count - 1));
|
|
215
|
+
if (pool->error != NULL || (value != NULL && nested == NULL)) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
for (size_t i = count - 1; i >= 1; i--) {
|
|
219
|
+
mongory_table *wrapper = mongory_table_new(pool);
|
|
220
|
+
if (!wrapper || !wrapper->set(wrapper, parts[i], nested)) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
nested = mongory_value_wrap_t(pool, wrapper);
|
|
224
|
+
if (!nested) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return mongory_expand_deep_merge_set(pool, ctx->result, parts[0], nested);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
static mongory_value *mongory_expand_table(mongory_memory_pool *pool, mongory_value *value, size_t depth) {
|
|
232
|
+
size_t max_depth = mongory_max_depth_get();
|
|
233
|
+
if (max_depth > 0 && depth > max_depth) {
|
|
234
|
+
pool->error = &MONGORY_DEPTH_LIMIT_ERROR;
|
|
235
|
+
return NULL;
|
|
236
|
+
}
|
|
237
|
+
mongory_table *result = mongory_table_new(pool);
|
|
238
|
+
if (!result) {
|
|
239
|
+
return NULL;
|
|
240
|
+
}
|
|
241
|
+
mongory_expand_table_ctx ctx = {pool, result, depth};
|
|
242
|
+
if (!value->data.t->each(value->data.t, &ctx, mongory_expand_table_pair) || pool->error != NULL) {
|
|
243
|
+
return NULL;
|
|
244
|
+
}
|
|
245
|
+
mongory_value *wrapped = mongory_value_wrap_t(pool, result);
|
|
246
|
+
if (wrapped) {
|
|
247
|
+
wrapped->origin = value->origin;
|
|
248
|
+
}
|
|
249
|
+
return wrapped;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
typedef struct mongory_dot_scan_ctx {
|
|
253
|
+
size_t depth;
|
|
254
|
+
bool found;
|
|
255
|
+
} mongory_dot_scan_ctx;
|
|
256
|
+
|
|
257
|
+
static bool mongory_table_needs_dot_expansion(mongory_value *value, size_t depth);
|
|
258
|
+
|
|
259
|
+
static bool mongory_dot_scan_pair(char *key, mongory_value *value, void *acc) {
|
|
260
|
+
mongory_dot_scan_ctx *ctx = (mongory_dot_scan_ctx *)acc;
|
|
261
|
+
if (strchr(key, '.') != NULL) {
|
|
262
|
+
ctx->found = true;
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
if (!value) {
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
if (key[0] == '$') {
|
|
269
|
+
if (mongory_is_logical_list_op(key)) {
|
|
270
|
+
if (value->type == MONGORY_TYPE_ARRAY && value->data.a) {
|
|
271
|
+
mongory_array *arr = value->data.a;
|
|
272
|
+
for (size_t i = 0; i < arr->count; i++) {
|
|
273
|
+
mongory_value *elem = arr->get(arr, i);
|
|
274
|
+
if (elem && elem->type == MONGORY_TYPE_TABLE && elem->data.t &&
|
|
275
|
+
mongory_table_needs_dot_expansion(elem, ctx->depth + 1)) {
|
|
276
|
+
ctx->found = true;
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
if (mongory_is_nested_table_op(key) &&
|
|
284
|
+
value->type == MONGORY_TYPE_TABLE && value->data.t &&
|
|
285
|
+
mongory_table_needs_dot_expansion(value, ctx->depth + 1)) {
|
|
286
|
+
ctx->found = true;
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
if (value->type == MONGORY_TYPE_TABLE && value->data.t &&
|
|
292
|
+
mongory_table_needs_dot_expansion(value, ctx->depth + 1)) {
|
|
293
|
+
ctx->found = true;
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
static bool mongory_table_needs_dot_expansion(mongory_value *value, size_t depth) {
|
|
300
|
+
size_t max_depth = mongory_max_depth_get();
|
|
301
|
+
if (max_depth > 0 && depth > max_depth) {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
mongory_dot_scan_ctx ctx = {depth, false};
|
|
305
|
+
value->data.t->each(value->data.t, &ctx, mongory_dot_scan_pair);
|
|
306
|
+
return ctx.found;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
mongory_matcher *mongory_matcher_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
|
|
310
|
+
if (!MONGORY_VALIDATE_TABLE(pool, condition)) {
|
|
311
|
+
return NULL;
|
|
312
|
+
}
|
|
313
|
+
mongory_compile_ctx saved = mongory_compile_ctx_begin();
|
|
314
|
+
mongory_value *expanded = mongory_table_needs_dot_expansion(condition, 1)
|
|
315
|
+
? mongory_expand_table(pool, condition, 1)
|
|
316
|
+
: condition;
|
|
317
|
+
mongory_matcher *matcher = (expanded && pool->error == NULL)
|
|
318
|
+
? mongory_matcher_condition_new(pool, expanded, extern_ctx)
|
|
319
|
+
: NULL;
|
|
320
|
+
mongory_compile_ctx_end(saved);
|
|
53
321
|
return matcher;
|
|
54
322
|
}
|
|
55
323
|
|
|
@@ -65,20 +333,17 @@ mongory_matcher *mongory_matcher_new(mongory_memory_pool *pool, mongory_value *c
|
|
|
65
333
|
* @param value The value to check against the matcher's condition.
|
|
66
334
|
* @return True if the value satisfies the matcher's condition, false otherwise.
|
|
67
335
|
*/
|
|
68
|
-
bool mongory_matcher_match(mongory_matcher *matcher, mongory_value *value) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
mongory_matcher *matcher,
|
|
72
|
-
mongory_memory_pool *pool,
|
|
73
|
-
bool as_skip
|
|
74
|
-
) {
|
|
75
|
-
MONGORY_VALIDATE_PTR(pool, matcher);
|
|
76
|
-
if (pool->error != NULL) {
|
|
77
|
-
return NULL;
|
|
336
|
+
bool mongory_matcher_match(mongory_matcher *matcher, mongory_value *value) {
|
|
337
|
+
if (!matcher || !matcher->match) {
|
|
338
|
+
return false;
|
|
78
339
|
}
|
|
340
|
+
return matcher->match(matcher, value);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
static mongory_table *mongory_matcher_init_tree_node(mongory_memory_pool *pool, mongory_matcher *matcher,
|
|
344
|
+
mongory_array *children) {
|
|
79
345
|
mongory_table *node = mongory_table_new(pool);
|
|
80
|
-
|
|
81
|
-
if (!node || !children || pool->error != NULL) {
|
|
346
|
+
if (!node || pool->error != NULL) {
|
|
82
347
|
return NULL;
|
|
83
348
|
}
|
|
84
349
|
const char *name = matcher->name ? matcher->name : "";
|
|
@@ -91,15 +356,28 @@ static mongory_value *mongory_matcher_build_explain_node(
|
|
|
91
356
|
node->set(node, "field", mongory_value_wrap_s(pool, field_matcher->field));
|
|
92
357
|
}
|
|
93
358
|
}
|
|
94
|
-
|
|
95
|
-
|
|
359
|
+
node->set(node, "children", mongory_value_wrap_a(pool, children));
|
|
360
|
+
return pool->error == NULL ? node : NULL;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
static mongory_value *mongory_matcher_build_explain_node(
|
|
364
|
+
mongory_matcher *matcher,
|
|
365
|
+
mongory_memory_pool *pool,
|
|
366
|
+
bool as_skip
|
|
367
|
+
) {
|
|
368
|
+
if (!MONGORY_VALIDATE_PTR(pool, matcher)) {
|
|
369
|
+
return NULL;
|
|
370
|
+
}
|
|
371
|
+
mongory_array *children = mongory_array_new(pool);
|
|
372
|
+
if (!children || pool->error != NULL) {
|
|
373
|
+
return NULL;
|
|
96
374
|
}
|
|
97
375
|
|
|
98
376
|
if (matcher->traverse == mongory_matcher_composite_traverse) {
|
|
99
377
|
mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
|
|
100
378
|
mongory_array *src_children = mongory_atomic_load_ptr_acquire(&composite->children);
|
|
101
|
-
|
|
102
|
-
for (
|
|
379
|
+
size_t total = src_children ? src_children->count : 0;
|
|
380
|
+
for (size_t i = 0; i < total; i++) {
|
|
103
381
|
mongory_matcher *child = (mongory_matcher *)src_children->get(src_children, i);
|
|
104
382
|
mongory_value *child_node = mongory_matcher_build_explain_node(child, pool, as_skip);
|
|
105
383
|
if (!child_node || pool->error != NULL) {
|
|
@@ -109,7 +387,8 @@ static mongory_value *mongory_matcher_build_explain_node(
|
|
|
109
387
|
}
|
|
110
388
|
} else if (matcher->traverse == mongory_matcher_value_traverse) {
|
|
111
389
|
mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
|
|
112
|
-
mongory_matcher *
|
|
390
|
+
mongory_matcher *arm = mongory_literal_load_implicit(literal);
|
|
391
|
+
mongory_matcher *next = arm ? arm : literal->delegate_matcher;
|
|
113
392
|
if (next) {
|
|
114
393
|
mongory_value *child_node = mongory_matcher_build_explain_node(next, pool, as_skip);
|
|
115
394
|
if (!child_node || pool->error != NULL) {
|
|
@@ -119,7 +398,13 @@ static mongory_value *mongory_matcher_build_explain_node(
|
|
|
119
398
|
}
|
|
120
399
|
}
|
|
121
400
|
|
|
122
|
-
node
|
|
401
|
+
mongory_table *node = mongory_matcher_init_tree_node(pool, matcher, children);
|
|
402
|
+
if (!node) {
|
|
403
|
+
return NULL;
|
|
404
|
+
}
|
|
405
|
+
if (as_skip) {
|
|
406
|
+
node->set(node, "result", mongory_value_wrap_s(pool, "skip"));
|
|
407
|
+
}
|
|
123
408
|
return mongory_value_wrap_t(pool, node);
|
|
124
409
|
}
|
|
125
410
|
|
|
@@ -173,11 +458,13 @@ static void mongory_explain_tree_print_node(
|
|
|
173
458
|
}
|
|
174
459
|
|
|
175
460
|
mongory_value *mongory_matcher_explain_tree(mongory_matcher *matcher, mongory_memory_pool *temp_pool) {
|
|
176
|
-
MONGORY_VALIDATE_PTR(temp_pool, matcher)
|
|
177
|
-
if (temp_pool->error != NULL) {
|
|
461
|
+
if (!MONGORY_VALIDATE_PTR(temp_pool, matcher)) {
|
|
178
462
|
return NULL;
|
|
179
463
|
}
|
|
180
|
-
|
|
464
|
+
mongory_memory_pool_lock(matcher->pool);
|
|
465
|
+
mongory_value *tree = mongory_matcher_build_explain_node(matcher, temp_pool, false);
|
|
466
|
+
mongory_memory_pool_unlock(matcher->pool);
|
|
467
|
+
return tree;
|
|
181
468
|
}
|
|
182
469
|
|
|
183
470
|
void mongory_matcher_explain(mongory_matcher *matcher, mongory_memory_pool *temp_pool) {
|
|
@@ -195,13 +482,15 @@ typedef struct mongory_matcher_traced_match_context {
|
|
|
195
482
|
} mongory_matcher_traced_match_context;
|
|
196
483
|
|
|
197
484
|
static bool mongory_matcher_traced_match(mongory_matcher *matcher, mongory_value *value) {
|
|
485
|
+
if (!matcher || !matcher->original_match) {
|
|
486
|
+
return false;
|
|
487
|
+
}
|
|
198
488
|
bool matched = matcher->original_match(matcher, value);
|
|
199
489
|
mongory_array *trace_stack = matcher->trace_stack;
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (pool->error != NULL) {
|
|
203
|
-
return false;
|
|
490
|
+
if (!trace_stack || !mongory_validate_pool(trace_stack->pool)) {
|
|
491
|
+
return matched;
|
|
204
492
|
}
|
|
493
|
+
mongory_memory_pool *pool = trace_stack->pool;
|
|
205
494
|
|
|
206
495
|
mongory_matcher_traced_match_context *trace_result = MG_ALLOC_PTR(pool, mongory_matcher_traced_match_context);
|
|
207
496
|
if (!trace_result) {
|
|
@@ -211,7 +500,10 @@ static bool mongory_matcher_traced_match(mongory_matcher *matcher, mongory_value
|
|
|
211
500
|
trace_result->record = value;
|
|
212
501
|
trace_result->matched = matched;
|
|
213
502
|
trace_result->level = matcher->trace_level;
|
|
214
|
-
|
|
503
|
+
mongory_value *wrapped = mongory_value_wrap_ptr(pool, (void *)trace_result);
|
|
504
|
+
if (wrapped) {
|
|
505
|
+
trace_stack->push(trace_stack, wrapped);
|
|
506
|
+
}
|
|
215
507
|
|
|
216
508
|
return matched;
|
|
217
509
|
}
|
|
@@ -222,12 +514,9 @@ static bool mongory_matcher_enable_trace_cb(mongory_matcher *matcher, mongory_ma
|
|
|
222
514
|
matcher->match = mongory_matcher_traced_match;
|
|
223
515
|
if (matcher->traverse == mongory_matcher_value_traverse) {
|
|
224
516
|
mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
|
|
225
|
-
if (literal
|
|
517
|
+
if (mongory_literal_load_implicit(literal) && literal->delegate_matcher) {
|
|
226
518
|
mongory_matcher_traverse_context delegate_ctx = {
|
|
227
|
-
.pool = ctx->pool,
|
|
228
519
|
.level = ctx->level + 1,
|
|
229
|
-
.count = 0,
|
|
230
|
-
.total = 1,
|
|
231
520
|
.acc = ctx->acc,
|
|
232
521
|
.callback = ctx->callback,
|
|
233
522
|
};
|
|
@@ -242,11 +531,9 @@ static bool mongory_matcher_disable_trace_cb(mongory_matcher *matcher, mongory_m
|
|
|
242
531
|
matcher->trace_stack = NULL;
|
|
243
532
|
if (matcher->traverse == mongory_matcher_value_traverse) {
|
|
244
533
|
mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
|
|
245
|
-
if (literal
|
|
534
|
+
if (mongory_literal_load_implicit(literal) && literal->delegate_matcher) {
|
|
246
535
|
mongory_matcher_traverse_context delegate_ctx = {
|
|
247
536
|
.level = ctx->level + 1,
|
|
248
|
-
.count = 0,
|
|
249
|
-
.total = 1,
|
|
250
537
|
.callback = ctx->callback,
|
|
251
538
|
};
|
|
252
539
|
literal->delegate_matcher->traverse(literal->delegate_matcher, &delegate_ctx);
|
|
@@ -272,8 +559,7 @@ static mongory_array *mongory_matcher_build_trace_nodes(
|
|
|
272
559
|
mongory_memory_pool *pool,
|
|
273
560
|
mongory_array *executed_matchers_out
|
|
274
561
|
) {
|
|
275
|
-
MONGORY_VALIDATE_PTR(pool, traces)
|
|
276
|
-
if (pool->error != NULL) {
|
|
562
|
+
if (!MONGORY_VALIDATE_PTR(pool, traces) || !MONGORY_VALIDATE_PTR(pool, traces->get)) {
|
|
277
563
|
return NULL;
|
|
278
564
|
}
|
|
279
565
|
mongory_array *nodes = mongory_array_new(pool);
|
|
@@ -284,8 +570,7 @@ static mongory_array *mongory_matcher_build_trace_nodes(
|
|
|
284
570
|
int total = (int)traces->count;
|
|
285
571
|
for (int i = 0; i < total; i++) {
|
|
286
572
|
mongory_value *item = traces->get(traces, i);
|
|
287
|
-
MONGORY_VALIDATE_PTR(pool, item)
|
|
288
|
-
if (pool->error != NULL) {
|
|
573
|
+
if (!MONGORY_VALIDATE_PTR(pool, item) || !MONGORY_VALIDATE_PTR(pool, item->data.ptr)) {
|
|
289
574
|
return NULL;
|
|
290
575
|
}
|
|
291
576
|
mongory_matcher_traced_match_context *entry = (mongory_matcher_traced_match_context *)item->data.ptr;
|
|
@@ -322,7 +607,8 @@ static mongory_array *mongory_matcher_build_trace_nodes(
|
|
|
322
607
|
}
|
|
323
608
|
} else if (m->traverse == mongory_matcher_value_traverse) {
|
|
324
609
|
mongory_value_matcher *lit = (mongory_value_matcher *)m;
|
|
325
|
-
mongory_matcher *
|
|
610
|
+
mongory_matcher *arm = mongory_literal_load_implicit(lit);
|
|
611
|
+
mongory_matcher *next = arm ? arm : lit->delegate_matcher;
|
|
326
612
|
if (next && children->count == 0) {
|
|
327
613
|
mongory_value *skip_node = mongory_matcher_build_explain_node(next, pool, true);
|
|
328
614
|
if (!skip_node || pool->error != NULL) {
|
|
@@ -332,25 +618,14 @@ static mongory_array *mongory_matcher_build_trace_nodes(
|
|
|
332
618
|
}
|
|
333
619
|
}
|
|
334
620
|
|
|
335
|
-
mongory_table *node =
|
|
336
|
-
if (!node
|
|
621
|
+
mongory_table *node = mongory_matcher_init_tree_node(pool, m, children);
|
|
622
|
+
if (!node) {
|
|
337
623
|
return NULL;
|
|
338
624
|
}
|
|
339
|
-
const char *name = m->name ? m->name : "";
|
|
340
|
-
node->set(node, "name", mongory_value_wrap_s(pool, (char *)name));
|
|
341
|
-
node->set(node, "condition", m->condition ? m->condition : mongory_value_wrap_n(pool, NULL));
|
|
342
|
-
node->set(node, "priority", mongory_value_wrap_d(pool, m->priority));
|
|
343
|
-
if (strcmp(name, "Field") == 0) {
|
|
344
|
-
mongory_field_matcher *field_matcher = (mongory_field_matcher *)m;
|
|
345
|
-
if (field_matcher->field) {
|
|
346
|
-
node->set(node, "field", mongory_value_wrap_s(pool, field_matcher->field));
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
625
|
if (entry->record != NULL) {
|
|
350
626
|
node->set(node, "record", entry->record);
|
|
351
627
|
}
|
|
352
628
|
node->set(node, "result", mongory_value_wrap_s(pool, entry->matched ? "pass" : "fail"));
|
|
353
|
-
node->set(node, "children", mongory_value_wrap_a(pool, children));
|
|
354
629
|
|
|
355
630
|
mongory_value *wrapped_node = mongory_value_wrap_t(pool, node);
|
|
356
631
|
if (!wrapped_node || !nodes->push(nodes, wrapped_node) || pool->error != NULL) {
|
|
@@ -370,18 +645,14 @@ static mongory_array *mongory_matcher_build_trace_nodes(
|
|
|
370
645
|
}
|
|
371
646
|
|
|
372
647
|
void mongory_matcher_enable_trace_at(mongory_matcher *matcher, mongory_array *trace_stack, int level) {
|
|
373
|
-
if (!matcher || !trace_stack) {
|
|
648
|
+
if (!matcher || !trace_stack || !trace_stack->pool) {
|
|
374
649
|
return;
|
|
375
650
|
}
|
|
376
|
-
MONGORY_VALIDATE_PTR(trace_stack->pool, matcher->traverse)
|
|
377
|
-
if (trace_stack->pool->error != NULL) {
|
|
651
|
+
if (!MONGORY_VALIDATE_PTR(trace_stack->pool, matcher->traverse)) {
|
|
378
652
|
return;
|
|
379
653
|
}
|
|
380
654
|
mongory_matcher_traverse_context ctx = {
|
|
381
|
-
.pool = trace_stack->pool,
|
|
382
655
|
.level = level,
|
|
383
|
-
.count = 0,
|
|
384
|
-
.total = 0,
|
|
385
656
|
.acc = (void *)trace_stack,
|
|
386
657
|
.callback = mongory_matcher_enable_trace_cb,
|
|
387
658
|
};
|
|
@@ -389,8 +660,7 @@ void mongory_matcher_enable_trace_at(mongory_matcher *matcher, mongory_array *tr
|
|
|
389
660
|
}
|
|
390
661
|
|
|
391
662
|
void mongory_matcher_enable_trace(mongory_matcher *matcher, mongory_memory_pool *temp_pool) {
|
|
392
|
-
MONGORY_VALIDATE_PTR(temp_pool, matcher)
|
|
393
|
-
if (temp_pool->error != NULL) {
|
|
663
|
+
if (!MONGORY_VALIDATE_PTR(temp_pool, matcher) || !MONGORY_VALIDATE_PTR(temp_pool, matcher->traverse)) {
|
|
394
664
|
return;
|
|
395
665
|
}
|
|
396
666
|
mongory_array *trace_stack = mongory_array_new(temp_pool);
|
|
@@ -401,14 +671,14 @@ void mongory_matcher_enable_trace(mongory_matcher *matcher, mongory_memory_pool
|
|
|
401
671
|
}
|
|
402
672
|
|
|
403
673
|
void mongory_matcher_disable_trace(mongory_matcher *matcher) {
|
|
404
|
-
|
|
405
|
-
|
|
674
|
+
if (!matcher || !matcher->pool) {
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
if (!MONGORY_VALIDATE_PTR(matcher->pool, matcher->traverse)) {
|
|
406
678
|
return;
|
|
407
679
|
}
|
|
408
680
|
mongory_matcher_traverse_context ctx = {
|
|
409
681
|
.level = 0,
|
|
410
|
-
.count = 0,
|
|
411
|
-
.total = 0,
|
|
412
682
|
.callback = mongory_matcher_disable_trace_cb,
|
|
413
683
|
};
|
|
414
684
|
matcher->traverse(matcher, &ctx);
|
|
@@ -428,26 +698,11 @@ static void mongory_matcher_print_trace_node(mongory_value *node_val, mongory_me
|
|
|
428
698
|
|
|
429
699
|
const char *name = (name_val && name_val->type == MONGORY_TYPE_STRING && name_val->data.s) ? name_val->data.s : "";
|
|
430
700
|
const char *res_raw = (result_val && result_val->type == MONGORY_TYPE_STRING && result_val->data.s) ? result_val->data.s : "fail";
|
|
431
|
-
const char *res_label = NULL;
|
|
432
701
|
bool is_skip = strcmp(res_raw, "skip") == 0;
|
|
433
702
|
bool is_pass = strcmp(res_raw, "pass") == 0;
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
} else if (is_pass) {
|
|
438
|
-
res_label = "\e[30;42mMatched\e[0m";
|
|
439
|
-
} else {
|
|
440
|
-
res_label = "\e[30;41mDismatch\e[0m";
|
|
441
|
-
}
|
|
442
|
-
} else {
|
|
443
|
-
if (is_skip) {
|
|
444
|
-
res_label = "Skipped";
|
|
445
|
-
} else if (is_pass) {
|
|
446
|
-
res_label = "Matched";
|
|
447
|
-
} else {
|
|
448
|
-
res_label = "Dismatch";
|
|
449
|
-
}
|
|
450
|
-
}
|
|
703
|
+
const char *res_label = is_skip ? (mongory_matcher_trace_result_colorful ? "\e[30;43mSkipped\e[0m" : "Skipped")
|
|
704
|
+
: is_pass ? (mongory_matcher_trace_result_colorful ? "\e[30;42mMatched\e[0m" : "Matched")
|
|
705
|
+
: (mongory_matcher_trace_result_colorful ? "\e[30;41mDismatch\e[0m" : "Dismatch");
|
|
451
706
|
|
|
452
707
|
char *cdtn = (cond_val && cond_val->to_str) ? cond_val->to_str(cond_val, pool) : "null";
|
|
453
708
|
char *rcd = (record_val && record_val->to_str) ? record_val->to_str(record_val, pool) : "Nothing";
|
|
@@ -456,17 +711,14 @@ static void mongory_matcher_print_trace_node(mongory_value *node_val, mongory_me
|
|
|
456
711
|
putchar(' ');
|
|
457
712
|
}
|
|
458
713
|
if (field_val && field_val->type == MONGORY_TYPE_STRING && field_val->data.s) {
|
|
459
|
-
|
|
460
|
-
printf("%s: %s, field: \"%s\", condition: %s\n", name, res_label, field_val->data.s, cdtn);
|
|
461
|
-
} else {
|
|
462
|
-
printf("%s: %s, field: \"%s\", condition: %s, record: %s\n", name, res_label, field_val->data.s, cdtn, rcd);
|
|
463
|
-
}
|
|
714
|
+
printf("%s: %s, field: \"%s\", condition: %s", name, res_label, field_val->data.s, cdtn);
|
|
464
715
|
} else {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
716
|
+
printf("%s: %s, condition: %s", name, res_label, cdtn);
|
|
717
|
+
}
|
|
718
|
+
if (is_skip) {
|
|
719
|
+
putchar('\n');
|
|
720
|
+
} else {
|
|
721
|
+
printf(", record: %s\n", rcd);
|
|
470
722
|
}
|
|
471
723
|
|
|
472
724
|
if (children_val && children_val->type == MONGORY_TYPE_ARRAY && children_val->data.a) {
|
|
@@ -479,7 +731,7 @@ static void mongory_matcher_print_trace_node(mongory_value *node_val, mongory_me
|
|
|
479
731
|
}
|
|
480
732
|
|
|
481
733
|
void mongory_matcher_print_trace(mongory_matcher *matcher) {
|
|
482
|
-
if (!matcher || matcher->trace_stack == NULL) {
|
|
734
|
+
if (!matcher || matcher->trace_stack == NULL || matcher->trace_stack->pool == NULL) {
|
|
483
735
|
return;
|
|
484
736
|
}
|
|
485
737
|
mongory_memory_pool *pool = matcher->trace_stack->pool;
|
|
@@ -494,9 +746,8 @@ void mongory_matcher_print_trace(mongory_matcher *matcher) {
|
|
|
494
746
|
}
|
|
495
747
|
|
|
496
748
|
mongory_value *mongory_matcher_trace_tree(mongory_matcher *matcher, mongory_value *value) {
|
|
497
|
-
mongory_memory_pool *pool = value ? value->pool : (matcher ? matcher->pool : NULL);
|
|
498
|
-
MONGORY_VALIDATE_PTR(pool, matcher)
|
|
499
|
-
if (!pool || pool->error != NULL) {
|
|
749
|
+
mongory_memory_pool *pool = (value && value->pool) ? value->pool : (matcher ? matcher->pool : NULL);
|
|
750
|
+
if (!MONGORY_VALIDATE_PTR(pool, matcher) || !MONGORY_VALIDATE_PTR(pool, matcher->match)) {
|
|
500
751
|
return NULL;
|
|
501
752
|
}
|
|
502
753
|
mongory_matcher_enable_trace(matcher, pool);
|
|
@@ -514,9 +765,8 @@ mongory_value *mongory_matcher_trace_tree(mongory_matcher *matcher, mongory_valu
|
|
|
514
765
|
}
|
|
515
766
|
|
|
516
767
|
bool mongory_matcher_trace(mongory_matcher *matcher, mongory_value *value) {
|
|
517
|
-
mongory_memory_pool *pool = value ? value->pool : (matcher ? matcher->pool : NULL);
|
|
518
|
-
MONGORY_VALIDATE_PTR(pool, matcher)
|
|
519
|
-
if (!pool || pool->error != NULL) {
|
|
768
|
+
mongory_memory_pool *pool = (value && value->pool) ? value->pool : (matcher ? matcher->pool : NULL);
|
|
769
|
+
if (!MONGORY_VALIDATE_PTR(pool, matcher) || !MONGORY_VALIDATE_PTR(pool, matcher->match)) {
|
|
520
770
|
return false;
|
|
521
771
|
}
|
|
522
772
|
mongory_value *tree = mongory_matcher_trace_tree(matcher, value);
|