mongory 2.1.0-x86_64-linux → 2.2.0-x86_64-linux
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/lib/core/2.6/mongory_ext.so +0 -0
- data/lib/core/2.7/mongory_ext.so +0 -0
- data/lib/core/3.0/mongory_ext.so +0 -0
- data/lib/core/3.1/mongory_ext.so +0 -0
- data/lib/core/3.2/mongory_ext.so +0 -0
- data/lib/core/3.3/mongory_ext.so +0 -0
- data/lib/core/3.4/mongory_ext.so +0 -0
- data/lib/core/4.0/mongory_ext.so +0 -0
- 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/mongory.gemspec +13 -12
- data/script/bench.rb +77 -4
- data/script/coverage.rb +84 -0
- metadata +9 -54
- data/ext/mongory_ext/extconf.rb +0 -115
- data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +0 -62
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +0 -122
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +0 -217
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +0 -82
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -115
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +0 -127
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +0 -175
- data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +0 -103
- data/ext/mongory_ext/mongory-core/include/mongory-core.h +0 -12
- data/ext/mongory_ext/mongory-core/src/foundations/array.c +0 -336
- data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +0 -20
- data/ext/mongory_ext/mongory-core/src/foundations/config.c +0 -335
- data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +0 -49
- data/ext/mongory_ext/mongory-core/src/foundations/error.c +0 -48
- data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +0 -406
- data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +0 -45
- data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +0 -117
- data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +0 -43
- data/ext/mongory_ext/mongory-core/src/foundations/table.c +0 -502
- data/ext/mongory_ext/mongory-core/src/foundations/utils.c +0 -206
- data/ext/mongory_ext/mongory-core/src/foundations/utils.h +0 -77
- data/ext/mongory_ext/mongory-core/src/foundations/value.c +0 -548
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +0 -193
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +0 -117
- data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +0 -241
- data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +0 -83
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +0 -730
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +0 -131
- data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +0 -158
- data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.h +0 -48
- data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +0 -199
- data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +0 -58
- data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +0 -204
- data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.h +0 -47
- data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +0 -124
- data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +0 -46
- data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +0 -529
- 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
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +0 -61
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +0 -23
- data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +0 -416
- data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +0 -97
- data/ext/mongory_ext/mongory_ext.c +0 -66
- data/ext/mongory_ext/mongory_ext_internal.h +0 -74
- data/ext/mongory_ext/rb_mongory_adapters.c +0 -140
- data/ext/mongory_ext/rb_mongory_matcher.c +0 -440
- data/ext/mongory_ext/rb_mongory_value.c +0 -327
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
#ifndef MONGORY_MATCHER_TRAVERSABLE_C
|
|
2
|
-
#define MONGORY_MATCHER_TRAVERSABLE_C
|
|
3
|
-
|
|
4
|
-
#include "matcher_traversable.h"
|
|
5
|
-
#include "../foundations/memory_pool_private.h"
|
|
6
|
-
#include "base_matcher.h"
|
|
7
|
-
#include "composite_matcher.h"
|
|
8
|
-
#include "value_matcher.h"
|
|
9
|
-
|
|
10
|
-
bool mongory_matcher_leaf_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
|
|
11
|
-
bool continue_traverse = ctx->callback(matcher, ctx);
|
|
12
|
-
if (!continue_traverse)
|
|
13
|
-
return false;
|
|
14
|
-
ctx->count++;
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
bool mongory_matcher_composite_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
|
|
19
|
-
void *prev_acc = ctx->acc;
|
|
20
|
-
if (!mongory_matcher_leaf_traverse(matcher, ctx))
|
|
21
|
-
return false;
|
|
22
|
-
mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
|
|
23
|
-
mongory_array *children = mongory_atomic_load_ptr_acquire(&composite->children);
|
|
24
|
-
int total = (int)children->count;
|
|
25
|
-
mongory_matcher_traverse_context child_ctx = {
|
|
26
|
-
.pool = ctx->pool,
|
|
27
|
-
.level = ctx->level + 1,
|
|
28
|
-
.count = 0,
|
|
29
|
-
.total = total,
|
|
30
|
-
.acc = ctx->acc,
|
|
31
|
-
.callback = ctx->callback,
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
for (int i = 0; i < total; i++) {
|
|
35
|
-
mongory_matcher *child = (mongory_matcher *)children->get(children, i);
|
|
36
|
-
if (!child->traverse(child, &child_ctx))
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
ctx->acc = prev_acc;
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
bool mongory_matcher_value_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
|
|
44
|
-
void *prev_acc = ctx->acc;
|
|
45
|
-
if (!mongory_matcher_leaf_traverse(matcher, ctx))
|
|
46
|
-
return false;
|
|
47
|
-
mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
|
|
48
|
-
mongory_matcher *next_matcher = literal->implicit_array_matcher ? literal->implicit_array_matcher : literal->delegate_matcher;
|
|
49
|
-
mongory_matcher_traverse_context child_ctx = {
|
|
50
|
-
.pool = ctx->pool,
|
|
51
|
-
.level = ctx->level + 1,
|
|
52
|
-
.count = 0,
|
|
53
|
-
.total = 1,
|
|
54
|
-
.acc = ctx->acc,
|
|
55
|
-
.callback = ctx->callback,
|
|
56
|
-
};
|
|
57
|
-
bool result = next_matcher->traverse(next_matcher, &child_ctx);
|
|
58
|
-
ctx->acc = prev_acc;
|
|
59
|
-
return result;
|
|
60
|
-
}
|
|
61
|
-
#endif /* MONGORY_MATCHER_TRAVERSABLE_C */
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
#ifndef MONGORY_MATCHER_TRAVERSABLE_H
|
|
2
|
-
#define MONGORY_MATCHER_TRAVERSABLE_H
|
|
3
|
-
|
|
4
|
-
#include "mongory-core/matchers/matcher.h"
|
|
5
|
-
|
|
6
|
-
typedef struct mongory_matcher_traverse_context mongory_matcher_traverse_context;
|
|
7
|
-
typedef bool (*mongory_matcher_traverse_func)(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
|
|
8
|
-
struct mongory_matcher_traverse_context {
|
|
9
|
-
mongory_memory_pool *pool;
|
|
10
|
-
int level;
|
|
11
|
-
int count;
|
|
12
|
-
int total;
|
|
13
|
-
void *acc;
|
|
14
|
-
mongory_matcher_traverse_func callback;
|
|
15
|
-
} ;
|
|
16
|
-
|
|
17
|
-
bool mongory_matcher_leaf_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
|
|
18
|
-
|
|
19
|
-
bool mongory_matcher_composite_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
|
|
20
|
-
|
|
21
|
-
bool mongory_matcher_value_traverse(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
|
|
22
|
-
|
|
23
|
-
#endif /* MONGORY_MATCHER_TRAVERSABLE_H */
|
|
@@ -1,416 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file value_matcher.c
|
|
3
|
-
* @brief Implements literal matchers, field matchers, $not, and $size.
|
|
4
|
-
* This is an internal implementation file for the matcher module.
|
|
5
|
-
*
|
|
6
|
-
* - "Literal" matching often involves comparing a value directly or using a
|
|
7
|
-
* simple condition (like equality).
|
|
8
|
-
* - Field matchers extract a value from a table/array by field name/index and
|
|
9
|
-
* apply a condition to it.
|
|
10
|
-
* - $not inverts the result of a condition.
|
|
11
|
-
* - $size checks the number of elements in an array against a condition.
|
|
12
|
-
*/
|
|
13
|
-
#include "value_matcher.h"
|
|
14
|
-
#include "../foundations/config_private.h" // For mongory_internal_value_converter
|
|
15
|
-
#include "../foundations/memory_pool_private.h" // For atomic & lock helpers
|
|
16
|
-
#include "../foundations/utils.h" // For mongory_try_parse_int, mongory_string_cpy
|
|
17
|
-
#include "../foundations/string_buffer.h" // For mongory_string_buffer_appendf
|
|
18
|
-
#include "implicit_array_matcher.h" // For handling array-specific matching logic
|
|
19
|
-
#include "base_matcher.h" // For mongory_matcher_base_new
|
|
20
|
-
#include "compare_matcher.h" // For mongory_matcher_equal_new
|
|
21
|
-
#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
|
-
#include "matcher_traversable.h"
|
|
25
|
-
#include "mongory-core/foundations/array.h" // For mongory_array access
|
|
26
|
-
#include "mongory-core/foundations/table.h" // For mongory_table access
|
|
27
|
-
#include "mongory-core/foundations/value.h" // For mongory_value types and wrappers
|
|
28
|
-
#include "external_matcher.h" // For mongory_matcher_regex_new
|
|
29
|
-
#include <mongory-core.h> // General include
|
|
30
|
-
#include <stdio.h> // For printf
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @brief Core matching logic for literal-based conditions.
|
|
34
|
-
*
|
|
35
|
-
* This function is used by field matchers, $not, and $size.
|
|
36
|
-
* It checks the type of the input `value`.
|
|
37
|
-
* - If `value` is an array, it may delegate to an `implicit_array_matcher`
|
|
38
|
-
* (via `composite->right`, if initialized). This path seems intended for
|
|
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).
|
|
47
|
-
*
|
|
48
|
-
* @param matcher The `mongory_matcher` (expected to be a
|
|
49
|
-
* `mongory_composite_matcher` where `left` and possibly `right` are set up).
|
|
50
|
-
* @param value The `mongory_value` to evaluate against the matcher's condition.
|
|
51
|
-
* @return True if the value matches, false otherwise.
|
|
52
|
-
*/
|
|
53
|
-
static inline void mongory_propagate_build_error(mongory_memory_pool *build_pool, mongory_value *value) {
|
|
54
|
-
if (!build_pool || !build_pool->error) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
if (value && value->pool && value->pool != build_pool) {
|
|
58
|
-
if (build_pool->error == &MONGORY_ALLOC_ERROR) {
|
|
59
|
-
value->pool->error = &MONGORY_ALLOC_ERROR;
|
|
60
|
-
} else {
|
|
61
|
-
mongory_error *copied = MG_ALLOC_PTR(value->pool, mongory_error);
|
|
62
|
-
if (copied) {
|
|
63
|
-
copied->type = build_pool->error->type;
|
|
64
|
-
copied->message = build_pool->error->message
|
|
65
|
-
? mongory_string_cpy(value->pool, (char *)build_pool->error->message)
|
|
66
|
-
: NULL;
|
|
67
|
-
value->pool->error = copied;
|
|
68
|
-
} else {
|
|
69
|
-
value->pool->error = &MONGORY_ALLOC_ERROR;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
build_pool->error = NULL;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
static inline bool mongory_matcher_value_match(mongory_matcher *matcher, mongory_value *value) {
|
|
77
|
-
mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
|
|
78
|
-
if (!literal || !literal->base.pool)
|
|
79
|
-
return false; // Basic safety check
|
|
80
|
-
|
|
81
|
-
if (value != NULL && value->type == MONGORY_TYPE_ARRAY) {
|
|
82
|
-
// Double-checked locking: lazy build runs only on the first array value seen,
|
|
83
|
-
// keeping non-array matching and subsequent array matches lock-free.
|
|
84
|
-
mongory_matcher *arm = mongory_atomic_load_ptr_acquire(&literal->implicit_array_matcher);
|
|
85
|
-
if (arm == NULL) {
|
|
86
|
-
mongory_memory_pool *build_pool = literal->base.pool;
|
|
87
|
-
mongory_memory_pool_lock(build_pool);
|
|
88
|
-
arm = mongory_atomic_load_ptr_relaxed(&literal->implicit_array_matcher);
|
|
89
|
-
if (arm == NULL) {
|
|
90
|
-
arm = mongory_matcher_implicit_array_new(build_pool, literal->base.condition, literal->base.extern_ctx);
|
|
91
|
-
if (arm && literal->base.trace_stack) {
|
|
92
|
-
mongory_matcher_enable_trace_at(arm, literal->base.trace_stack, literal->base.trace_level + 1);
|
|
93
|
-
}
|
|
94
|
-
if (arm != NULL) {
|
|
95
|
-
arm->parent = &literal->base;
|
|
96
|
-
double delta = (1.0 + arm->priority) - literal->base.priority;
|
|
97
|
-
if (delta != 0.0) {
|
|
98
|
-
for (mongory_matcher *cur = &literal->base; cur != NULL; cur = cur->parent) {
|
|
99
|
-
cur->priority += delta;
|
|
100
|
-
if (cur->traverse == mongory_matcher_composite_traverse) {
|
|
101
|
-
mongory_matcher_resort_composite((mongory_composite_matcher *)cur);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
mongory_atomic_store_ptr_release(&literal->implicit_array_matcher, arm);
|
|
106
|
-
} else {
|
|
107
|
-
mongory_propagate_build_error(build_pool, value);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
mongory_memory_pool_unlock(build_pool);
|
|
111
|
-
}
|
|
112
|
-
return arm ? arm->match(arm, value) : false;
|
|
113
|
-
} else {
|
|
114
|
-
// For non-array values, or if array-specific path wasn't taken.
|
|
115
|
-
// The `left` child handles the general literal condition.
|
|
116
|
-
return literal->delegate_matcher ? literal->delegate_matcher->match(literal->delegate_matcher, value) : false;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
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
|
-
/**
|
|
145
|
-
* @brief Delegates creation of a simple literal matcher based on the
|
|
146
|
-
* `condition`'s type.
|
|
147
|
-
*
|
|
148
|
-
* - Table condition: creates a `mongory_matcher_condition_new`.
|
|
149
|
-
* - Regex condition: creates a `mongory_matcher_regex_new`.
|
|
150
|
-
* - Null condition: creates a specialized `mongory_matcher_null_new`.
|
|
151
|
-
* - Other types: creates an equality matcher (`mongory_matcher_equal_new`).
|
|
152
|
-
*
|
|
153
|
-
* @param pool Memory pool for allocation.
|
|
154
|
-
* @param condition The literal value or condition table.
|
|
155
|
-
* @return The appropriate simple matcher for the condition, or NULL on failure.
|
|
156
|
-
*/
|
|
157
|
-
static inline mongory_matcher *mongory_matcher_value_delegate(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
|
|
158
|
-
if (!condition)
|
|
159
|
-
return mongory_matcher_equal_new(pool, NULL, extern_ctx); // Or specific null matcher
|
|
160
|
-
|
|
161
|
-
switch (condition->type) {
|
|
162
|
-
case MONGORY_TYPE_TABLE:
|
|
163
|
-
return mongory_matcher_condition_new(pool, condition, extern_ctx);
|
|
164
|
-
case MONGORY_TYPE_REGEX:
|
|
165
|
-
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
|
-
default:
|
|
170
|
-
// For boolean, int, double, string, array (equality), pointer, unsupported.
|
|
171
|
-
return mongory_matcher_equal_new(pool, condition, extern_ctx);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* @brief Match function for a field matcher.
|
|
177
|
-
*
|
|
178
|
-
* Extracts the value of `field_matcher->field` from the input `value`
|
|
179
|
-
* (table or array). Then, applies the literal matching logic
|
|
180
|
-
* (`mongory_matcher_value_match`) using the sub-matcher stored in
|
|
181
|
-
* `composite.left` (which was set up by `literal_delegate` based on the
|
|
182
|
-
* original condition for this field).
|
|
183
|
-
*
|
|
184
|
-
* @param matcher Pointer to the `mongory_matcher` (a `mongory_field_matcher`).
|
|
185
|
-
* @param value The input table or array to extract the field from.
|
|
186
|
-
* @return True if the field's value matches the condition, false otherwise.
|
|
187
|
-
*/
|
|
188
|
-
static inline bool mongory_matcher_field_match(mongory_matcher *matcher, mongory_value *value) {
|
|
189
|
-
if (value == NULL) { // Cannot extract field from NULL.
|
|
190
|
-
return false;
|
|
191
|
-
}
|
|
192
|
-
mongory_field_matcher *field_matcher = (mongory_field_matcher *)matcher;
|
|
193
|
-
if (!field_matcher->field)
|
|
194
|
-
return false; // No field specified.
|
|
195
|
-
|
|
196
|
-
mongory_value *field_value = NULL;
|
|
197
|
-
char *field_key = field_matcher->field;
|
|
198
|
-
|
|
199
|
-
if (value->type == MONGORY_TYPE_TABLE) {
|
|
200
|
-
if (value->data.t) {
|
|
201
|
-
field_value = value->data.t->get(value->data.t, field_key);
|
|
202
|
-
}
|
|
203
|
-
} else if (value->type == MONGORY_TYPE_ARRAY) {
|
|
204
|
-
if (value->data.a) {
|
|
205
|
-
int index;
|
|
206
|
-
if (mongory_try_parse_int(field_key, &index)) {
|
|
207
|
-
if (index < 0) { // Handle negative indexing (from end of array)
|
|
208
|
-
if ((size_t)(-index) <= value->data.a->count) {
|
|
209
|
-
index = (int)value->data.a->count + index;
|
|
210
|
-
if ((size_t)index < value->data.a->count) {
|
|
211
|
-
field_value = value->data.a->get(value->data.a, (size_t)index);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
} else if ((size_t)index < value->data.a->count) {
|
|
215
|
-
field_value = value->data.a->get(value->data.a, (size_t)index);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
// Out-of-bounds or non-integer index leaves field_value = NULL (MISSING)
|
|
219
|
-
// so child matchers like $exists: false or $nin can evaluate MISSING.
|
|
220
|
-
}
|
|
221
|
-
} else {
|
|
222
|
-
return false; // Can only extract fields from tables or arrays.
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// If the extracted field value is a pointer type that needs conversion
|
|
226
|
-
// (e.g., from a language binding), convert it.
|
|
227
|
-
if (field_value && field_value->type == MONGORY_TYPE_POINTER && mongory_internal_value_converter.shallow_convert) {
|
|
228
|
-
// Prefer the caller/record's thread-local pool over the shared matcher->pool.
|
|
229
|
-
mongory_memory_pool *conversion_pool =
|
|
230
|
-
field_value->pool ? field_value->pool : (value->pool ? value->pool : matcher->pool);
|
|
231
|
-
if (conversion_pool == matcher->pool) {
|
|
232
|
-
mongory_memory_pool_lock(matcher->pool);
|
|
233
|
-
field_value = mongory_internal_value_converter.shallow_convert(conversion_pool, field_value->data.ptr);
|
|
234
|
-
mongory_memory_pool_unlock(matcher->pool);
|
|
235
|
-
} else {
|
|
236
|
-
field_value = mongory_internal_value_converter.shallow_convert(conversion_pool, field_value->data.ptr);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
// Now, use the literal_match logic (which uses composite.left primarily)
|
|
240
|
-
// to match the extracted field_value.
|
|
241
|
-
return mongory_matcher_value_match(matcher, field_value);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
mongory_matcher *mongory_matcher_field_new(mongory_memory_pool *pool, char *field_name,
|
|
245
|
-
mongory_value *condition_for_field, void *extern_ctx) {
|
|
246
|
-
if (!pool || !pool->alloc) {
|
|
247
|
-
return NULL;
|
|
248
|
-
}
|
|
249
|
-
if (!mongory_compile_budget_count_node(pool)) {
|
|
250
|
-
return NULL;
|
|
251
|
-
}
|
|
252
|
-
mongory_field_matcher *field_m = MG_ALLOC_PTR(pool, mongory_field_matcher);
|
|
253
|
-
if (field_m == NULL) {
|
|
254
|
-
pool->error = &MONGORY_ALLOC_ERROR;
|
|
255
|
-
return NULL;
|
|
256
|
-
}
|
|
257
|
-
field_m->field = mongory_string_cpy(pool, field_name);
|
|
258
|
-
if (field_m->field == NULL) {
|
|
259
|
-
return NULL;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// Initialize the base composite matcher part
|
|
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'.
|
|
277
|
-
field_m->literal.delegate_matcher = mongory_matcher_value_delegate(pool, condition_for_field, extern_ctx);
|
|
278
|
-
field_m->literal.implicit_array_matcher = NULL;
|
|
279
|
-
|
|
280
|
-
if (field_m->literal.delegate_matcher == NULL) {
|
|
281
|
-
// Failed to create the delegate matcher for the condition.
|
|
282
|
-
return NULL;
|
|
283
|
-
}
|
|
284
|
-
field_m->literal.base.priority = 1.0 + field_m->literal.delegate_matcher->priority;
|
|
285
|
-
field_m->literal.base.parent = NULL;
|
|
286
|
-
field_m->literal.delegate_matcher->parent = (mongory_matcher *)field_m;
|
|
287
|
-
|
|
288
|
-
return (mongory_matcher *)field_m;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* @brief Match function for a NOT matcher.
|
|
293
|
-
* Negates the result of `mongory_matcher_value_match`.
|
|
294
|
-
* @param matcher The $not matcher.
|
|
295
|
-
* @param value The value to evaluate.
|
|
296
|
-
* @return True if the literal match is false, false if it's true.
|
|
297
|
-
*/
|
|
298
|
-
static inline bool mongory_matcher_not_match(mongory_matcher *matcher, mongory_value *value) {
|
|
299
|
-
// literal_match uses composite.left (and sometimes .right for arrays)
|
|
300
|
-
return !mongory_matcher_value_match(matcher, value);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
mongory_matcher *mongory_matcher_not_new(mongory_memory_pool *pool, mongory_value *condition_to_negate, void *extern_ctx) {
|
|
304
|
-
if (!pool || !pool->alloc) {
|
|
305
|
-
return NULL;
|
|
306
|
-
}
|
|
307
|
-
if (!mongory_compile_budget_enter_depth(pool)) {
|
|
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();
|
|
318
|
-
return NULL;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// The 'left' child is the matcher for the condition being negated.
|
|
322
|
-
literal->delegate_matcher = mongory_matcher_value_delegate(pool, condition_to_negate, extern_ctx);
|
|
323
|
-
if (!literal->delegate_matcher) {
|
|
324
|
-
mongory_compile_budget_leave_depth();
|
|
325
|
-
return NULL; // Failed to create delegate for the condition.
|
|
326
|
-
}
|
|
327
|
-
literal->implicit_array_matcher = NULL;
|
|
328
|
-
|
|
329
|
-
literal->base.pool = pool;
|
|
330
|
-
literal->base.condition = condition_to_negate;
|
|
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;
|
|
337
|
-
}
|
|
338
|
-
literal->base.explain = mongory_matcher_value_explain;
|
|
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();
|
|
346
|
-
return (mongory_matcher *)literal;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* @brief Match function for a $size matcher.
|
|
351
|
-
* Checks if the input `value` (must be an array) has a size that matches
|
|
352
|
-
* the condition associated with the $size matcher.
|
|
353
|
-
* @param matcher The $size matcher.
|
|
354
|
-
* @param value The value to evaluate (must be an array).
|
|
355
|
-
* @return True if array size matches condition, false otherwise.
|
|
356
|
-
*/
|
|
357
|
-
static inline bool mongory_matcher_size_match(mongory_matcher *matcher, mongory_value *value) {
|
|
358
|
-
if (!value || value->type != MONGORY_TYPE_ARRAY || !value->data.a) {
|
|
359
|
-
return false; // $size only applies to valid arrays.
|
|
360
|
-
}
|
|
361
|
-
mongory_array *array = value->data.a;
|
|
362
|
-
mongory_value *size_val = mongory_value_wrap_i(value->pool, (int64_t)array->count);
|
|
363
|
-
if (!size_val) {
|
|
364
|
-
return false;
|
|
365
|
-
}
|
|
366
|
-
return mongory_matcher_value_match(matcher, size_val);
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
mongory_matcher *mongory_matcher_size_new(mongory_memory_pool *pool, mongory_value *size_condition, void *extern_ctx) {
|
|
370
|
-
if (!pool || !pool->alloc) {
|
|
371
|
-
return NULL;
|
|
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;
|
|
415
|
-
}
|
|
416
|
-
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
#ifndef MONGORY_MATCHER_VALUE_H
|
|
2
|
-
#define MONGORY_MATCHER_VALUE_H
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @file value_matcher.h
|
|
6
|
-
* @brief Defines constructors for literal-based matchers, field matchers,
|
|
7
|
-
* $not, and $size. This is an internal header for the matcher module.
|
|
8
|
-
*
|
|
9
|
-
* "Literal" here often refers to matching a field's value against a specific
|
|
10
|
-
* condition, which might itself be a simple value or a more complex condition
|
|
11
|
-
* table.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
#include "base_matcher.h"
|
|
15
|
-
#include "mongory-core/foundations/memory_pool.h"
|
|
16
|
-
#include "mongory-core/foundations/value.h"
|
|
17
|
-
#include "mongory-core/matchers/matcher.h" // For mongory_matcher structure
|
|
18
|
-
#include "matcher_explainable.h"
|
|
19
|
-
#include "composite_matcher.h"
|
|
20
|
-
|
|
21
|
-
typedef struct mongory_value_matcher {
|
|
22
|
-
mongory_matcher base;
|
|
23
|
-
mongory_matcher *delegate_matcher;
|
|
24
|
-
mongory_matcher *implicit_array_matcher;
|
|
25
|
-
} mongory_value_matcher;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @struct mongory_field_matcher
|
|
29
|
-
* @brief Specialized composite matcher for matching a specific field.
|
|
30
|
-
* Stores the field name/index.
|
|
31
|
-
*/
|
|
32
|
-
typedef struct mongory_field_matcher {
|
|
33
|
-
mongory_value_matcher literal; /**< Base composite matcher structure. */
|
|
34
|
-
char *field; /**< Name/index of the field to match. Copied string. */
|
|
35
|
-
} mongory_field_matcher;
|
|
36
|
-
/**
|
|
37
|
-
* @brief Creates a "field" matcher.
|
|
38
|
-
*
|
|
39
|
-
* This matcher extracts a value from a specified `field` (or array index) of
|
|
40
|
-
* an input `mongory_value` (which is expected to be a table or array). It then
|
|
41
|
-
* applies a sub-matcher (derived from `condition`) to this extracted field
|
|
42
|
-
* value.
|
|
43
|
-
*
|
|
44
|
-
* @param pool Memory pool for allocation.
|
|
45
|
-
* @param field The name of the field (if input is a table) or string
|
|
46
|
-
* representation of an index (if input is an array). A copy of this string is
|
|
47
|
-
* made.
|
|
48
|
-
* @param condition The `mongory_value` condition to apply to the field's value.
|
|
49
|
-
* This condition is processed by `mongory_matcher_value_delegate` to determine
|
|
50
|
-
* the actual sub-matcher (e.g., equality, regex, nested table condition).
|
|
51
|
-
* @return A new field matcher, or NULL on failure.
|
|
52
|
-
*/
|
|
53
|
-
mongory_matcher *mongory_matcher_field_new(mongory_memory_pool *pool, char *field, mongory_value *condition, void *extern_ctx);
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @brief Creates a "NOT" ($not) matcher.
|
|
57
|
-
*
|
|
58
|
-
* This matcher negates the result of a sub-matcher derived from `condition`.
|
|
59
|
-
* The sub-matcher is typically determined by
|
|
60
|
-
* `mongory_matcher_value_delegate`.
|
|
61
|
-
*
|
|
62
|
-
* @param pool Memory pool for allocation.
|
|
63
|
-
* @param condition The `mongory_value` condition whose result will be negated.
|
|
64
|
-
* @return A new $not matcher, or NULL on failure.
|
|
65
|
-
*/
|
|
66
|
-
mongory_matcher *mongory_matcher_not_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* @brief Creates a "size" ($size) matcher.
|
|
70
|
-
*
|
|
71
|
-
* This matcher checks if the size (count of elements) of an input array
|
|
72
|
-
* matches the given `condition`. The `condition` itself is processed by
|
|
73
|
-
* `mongory_matcher_value_delegate` (e.g., it could be a number for exact
|
|
74
|
-
* size, or a condition table like {$gt: 5}).
|
|
75
|
-
*
|
|
76
|
-
* @param pool Memory pool for allocation.
|
|
77
|
-
* @param condition The `mongory_value` condition to apply to the array's size.
|
|
78
|
-
* @return A new $size matcher, or NULL on failure.
|
|
79
|
-
*/
|
|
80
|
-
mongory_matcher *mongory_matcher_size_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
|
|
81
|
-
|
|
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
|
-
#endif /* MONGORY_MATCHER_VALUE_H */
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file mongory_ext.c
|
|
3
|
-
* @brief Extension entry point and shared pool/error helpers for mongory-rb.
|
|
4
|
-
*/
|
|
5
|
-
#include "mongory_ext_internal.h"
|
|
6
|
-
#include <stdlib.h>
|
|
7
|
-
#include <string.h>
|
|
8
|
-
|
|
9
|
-
VALUE rb_mongory_m_mongory;
|
|
10
|
-
VALUE rb_mongory_m_matchers;
|
|
11
|
-
VALUE rb_mongory_c_matcher;
|
|
12
|
-
VALUE rb_mongory_c_matcher_context;
|
|
13
|
-
VALUE rb_mongory_e_error;
|
|
14
|
-
VALUE rb_mongory_e_type_error;
|
|
15
|
-
VALUE rb_mongory_data_converter;
|
|
16
|
-
VALUE rb_mongory_condition_converter;
|
|
17
|
-
|
|
18
|
-
rb_mongory_memory_pool_t *rb_mongory_memory_pool_new(void) {
|
|
19
|
-
rb_mongory_memory_pool_t *pool = malloc(sizeof(rb_mongory_memory_pool_t));
|
|
20
|
-
if (!pool) {
|
|
21
|
-
return NULL;
|
|
22
|
-
}
|
|
23
|
-
mongory_memory_pool *base = mongory_memory_pool_new();
|
|
24
|
-
if (!base) {
|
|
25
|
-
free(pool);
|
|
26
|
-
return NULL;
|
|
27
|
-
}
|
|
28
|
-
memcpy(&pool->base, base, sizeof(mongory_memory_pool));
|
|
29
|
-
pool->owner = NULL;
|
|
30
|
-
pool->next_scratch = NULL;
|
|
31
|
-
free(base);
|
|
32
|
-
|
|
33
|
-
return pool;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
bool rb_mongory_error_handling(mongory_memory_pool *pool, const char *error_message) {
|
|
37
|
-
if (pool->error) {
|
|
38
|
-
const char *msg = pool->error->message;
|
|
39
|
-
pool->error = NULL;
|
|
40
|
-
rb_raise(rb_mongory_e_type_error, "%s: %s", error_message, msg);
|
|
41
|
-
}
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
void Init_mongory_ext(void) {
|
|
46
|
-
// Initialize mongory core
|
|
47
|
-
mongory_init();
|
|
48
|
-
|
|
49
|
-
// Define modules and classes
|
|
50
|
-
rb_mongory_m_mongory = rb_define_module("Mongory");
|
|
51
|
-
rb_mongory_m_matchers = rb_define_module_under(rb_mongory_m_mongory, "Matchers");
|
|
52
|
-
VALUE mMongoryUtils = rb_define_module_under(rb_mongory_m_mongory, "Utils");
|
|
53
|
-
rb_mongory_c_matcher_context = rb_define_class_under(mMongoryUtils, "Context", rb_cObject);
|
|
54
|
-
|
|
55
|
-
// Mongory converters
|
|
56
|
-
rb_mongory_data_converter = rb_funcall(rb_mongory_m_mongory, rb_intern("data_converter"), 0);
|
|
57
|
-
rb_mongory_condition_converter = rb_funcall(rb_mongory_m_mongory, rb_intern("condition_converter"), 0);
|
|
58
|
-
|
|
59
|
-
// Define error classes
|
|
60
|
-
rb_mongory_e_error = rb_define_class_under(rb_mongory_m_mongory, "Error", rb_eStandardError);
|
|
61
|
-
rb_mongory_e_type_error = rb_define_class_under(rb_mongory_m_mongory, "TypeError", rb_mongory_e_error);
|
|
62
|
-
|
|
63
|
-
// Register Matcher class, methods, and core bridges
|
|
64
|
-
rb_mongory_init_matcher();
|
|
65
|
-
rb_mongory_register_adapters();
|
|
66
|
-
}
|