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.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/README.md +55 -73
  4. data/docs/clang_bridge.md +2 -2
  5. data/docs/performance.md +43 -7
  6. data/lib/core/2.6/mongory_ext.so +0 -0
  7. data/lib/core/2.7/mongory_ext.so +0 -0
  8. data/lib/core/3.0/mongory_ext.so +0 -0
  9. data/lib/core/3.1/mongory_ext.so +0 -0
  10. data/lib/core/3.2/mongory_ext.so +0 -0
  11. data/lib/core/3.3/mongory_ext.so +0 -0
  12. data/lib/core/3.4/mongory_ext.so +0 -0
  13. data/lib/core/4.0/mongory_ext.so +0 -0
  14. data/lib/mongory/converters/abstract_converter.rb +5 -5
  15. data/lib/mongory/converters/converted.rb +0 -4
  16. data/lib/mongory/converters/key_converter.rb +13 -36
  17. data/lib/mongory/converters/value_converter.rb +4 -4
  18. data/lib/mongory/matchers.rb +8 -7
  19. data/lib/mongory/query_builder.rb +9 -0
  20. data/lib/mongory/utils/context.rb +0 -6
  21. data/lib/mongory/utils/singleton_builder.rb +1 -4
  22. data/lib/mongory/version.rb +1 -1
  23. data/lib/mongory.rb +0 -1
  24. data/mongory.gemspec +13 -12
  25. data/script/bench.rb +77 -4
  26. data/script/coverage.rb +84 -0
  27. metadata +9 -54
  28. data/ext/mongory_ext/extconf.rb +0 -115
  29. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +0 -62
  30. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +0 -122
  31. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +0 -217
  32. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +0 -82
  33. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -115
  34. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +0 -127
  35. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +0 -175
  36. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +0 -103
  37. data/ext/mongory_ext/mongory-core/include/mongory-core.h +0 -12
  38. data/ext/mongory_ext/mongory-core/src/foundations/array.c +0 -336
  39. data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +0 -20
  40. data/ext/mongory_ext/mongory-core/src/foundations/config.c +0 -335
  41. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +0 -49
  42. data/ext/mongory_ext/mongory-core/src/foundations/error.c +0 -48
  43. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +0 -406
  44. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +0 -45
  45. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +0 -117
  46. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +0 -43
  47. data/ext/mongory_ext/mongory-core/src/foundations/table.c +0 -502
  48. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +0 -206
  49. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +0 -77
  50. data/ext/mongory_ext/mongory-core/src/foundations/value.c +0 -548
  51. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +0 -193
  52. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +0 -117
  53. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +0 -241
  54. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +0 -83
  55. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +0 -730
  56. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +0 -131
  57. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +0 -158
  58. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.h +0 -48
  59. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +0 -199
  60. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +0 -58
  61. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +0 -204
  62. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.h +0 -47
  63. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +0 -124
  64. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +0 -46
  65. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +0 -529
  66. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
  67. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
  68. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +0 -61
  69. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +0 -23
  70. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +0 -416
  71. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +0 -97
  72. data/ext/mongory_ext/mongory_ext.c +0 -66
  73. data/ext/mongory_ext/mongory_ext_internal.h +0 -74
  74. data/ext/mongory_ext/rb_mongory_adapters.c +0 -140
  75. data/ext/mongory_ext/rb_mongory_matcher.c +0 -440
  76. data/ext/mongory_ext/rb_mongory_value.c +0 -327
@@ -1,131 +0,0 @@
1
- #ifndef MONGORY_MATCHER_COMPOSITE_H
2
- #define MONGORY_MATCHER_COMPOSITE_H
3
-
4
- /**
5
- * @file composite_matcher.h
6
- * @brief Defines structures and constructors for composite matchers.
7
- * This is an internal header for the matcher module.
8
- *
9
- * Composite matchers combine other matchers, such as logical AND/OR,
10
- * or apply a matcher to elements of an array or fields of a table.
11
- */
12
-
13
- #include "base_matcher.h"
14
- #include "mongory-core/foundations/array.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
-
19
- /**
20
- * @struct mongory_composite_matcher
21
- * @brief Represents a matcher composed of other matchers.
22
- *
23
- * Typically has a `left` and/or `right` child matcher. The interpretation
24
- * of these children depends on the specific composite matcher type (e.g., for
25
- * AND/OR, both are used; for $elemMatch, `left` might be the sub-matcher for
26
- * elements).
27
- */
28
- typedef struct mongory_composite_matcher {
29
- mongory_matcher base; /**< Base matcher structure. */
30
- mongory_array *children; /**< Children matchers. */
31
- } mongory_composite_matcher;
32
-
33
- /** @name Composite Matcher Constructors
34
- * Functions to create instances of various composite matchers.
35
- * @{
36
- */
37
-
38
- /**
39
- * @brief Creates an "AND" ($and) matcher.
40
- * Matches if all conditions specified in an array of condition tables are met.
41
- * @param pool Memory pool for allocation.
42
- * @param condition A `mongory_value` of type `MONGORY_TYPE_ARRAY`, where each
43
- * element is a `MONGORY_TYPE_TABLE` representing a sub-condition.
44
- * @return A new `$and` matcher, or NULL on failure or if condition is invalid.
45
- */
46
- mongory_matcher *mongory_matcher_and_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
47
-
48
- /**
49
- * @brief Creates an "OR" ($or) matcher.
50
- * Matches if any condition specified in an array of condition tables is met.
51
- * @param pool Memory pool for allocation.
52
- * @param condition A `mongory_value` of type `MONGORY_TYPE_ARRAY`, where each
53
- * element is a `MONGORY_TYPE_TABLE` representing a sub-condition.
54
- * @return A new `$or` matcher, or NULL on failure or if condition is invalid.
55
- */
56
- mongory_matcher *mongory_matcher_or_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
57
-
58
- /**
59
- * @brief Creates an "element match" ($elemMatch) matcher.
60
- * Matches an array field if at least one element in the array matches the given
61
- * condition table.
62
- * @param pool Memory pool for allocation.
63
- * @param condition A `mongory_value` of type `MONGORY_TYPE_TABLE` representing
64
- * the condition to apply to array elements.
65
- * @return A new `$elemMatch` matcher, or NULL on failure.
66
- */
67
- mongory_matcher *mongory_matcher_elem_match_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
68
-
69
- /**
70
- * @brief Creates an "every element matches" ($every) matcher.
71
- * Matches an array field if ALL elements in the array match the given condition
72
- * table. (Note: This is a common pattern, though MongoDB's $all has more
73
- * complex behavior. This $every is simpler.)
74
- * @param pool Memory pool for allocation.
75
- * @param condition A `mongory_value` of type `MONGORY_TYPE_TABLE` representing
76
- * the condition to apply to array elements.
77
- * @return A new `$every` matcher, or NULL on failure.
78
- */
79
- mongory_matcher *mongory_matcher_every_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
80
-
81
- /**
82
- * @brief Creates a "table condition" matcher.
83
- * This is a core matcher that parses a condition table (similar to a MongoDB
84
- * query document) and builds a tree of sub-matchers based on its keys and
85
- * values. Field names imply field matchers, and `$`-prefixed keys imply
86
- * operator matchers. These are implicitly ANDed together.
87
- * @param pool Memory pool for allocation.
88
- * @param condition A `mongory_value` of type `MONGORY_TYPE_TABLE`.
89
- * @return A new table condition matcher, or NULL on failure/invalid condition.
90
- */
91
- mongory_matcher *mongory_matcher_condition_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
92
- /** @} */
93
-
94
- /**
95
- * @brief Allocates and initializes a base `mongory_composite_matcher`
96
- * structure.
97
- *
98
- * Sets up the base `mongory_matcher` fields within the composite structure.
99
- * Child matchers (`left`, `right`) and the specific `match` function must be
100
- * set by the caller.
101
- *
102
- * @param pool Memory pool for allocation.
103
- * @param condition The condition associated with this composite matcher (can be
104
- * NULL if the condition is implicitly defined by children).
105
- * @return mongory_composite_matcher* Pointer to the new composite matcher
106
- * structure, or NULL on failure.
107
- */
108
- mongory_composite_matcher *mongory_matcher_composite_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
109
-
110
- /**
111
- * @brief The actual match logic for an OR operation on a composite matcher.
112
- *
113
- * This function is typically assigned to the `match` field of an OR composite
114
- * matcher. It checks if either the `left` or `right` child matcher matches the
115
- * given value.
116
- *
117
- * @param matcher A pointer to the `mongory_matcher` (which is expected to be a
118
- * `mongory_composite_matcher` for OR).
119
- * @param value The `mongory_value` to evaluate.
120
- * @return bool True if `left->match()` or `right->match()` returns true, false
121
- * otherwise. Returns false if children are NULL or if their match calls fail.
122
- */
123
- bool mongory_matcher_or_match(mongory_matcher *matcher, mongory_value *value);
124
-
125
- /**
126
- * @brief Re-sorts a composite matcher's children by updated priority using
127
- * copy-on-write atomic publication safe for concurrent readers.
128
- */
129
- void mongory_matcher_resort_composite(mongory_composite_matcher *composite);
130
-
131
- #endif /* MONGORY_MATCHER_COMPOSITE_H */
@@ -1,158 +0,0 @@
1
- /**
2
- * @file existence_matcher.c
3
- * @brief Implements $exists and $present matchers.
4
- * This is an internal implementation file for the matcher module.
5
- */
6
- #include "existence_matcher.h"
7
- #include "base_matcher.h" // For mongory_matcher_base_new
8
- #include "mongory-core/foundations/array.h" // For value->data.a->count
9
- #include "mongory-core/foundations/error.h" // For mongory_error types
10
- #include "mongory-core/foundations/table.h" // For value->data.t->count
11
- #include "mongory-core/foundations/value.h" // For mongory_value types
12
- #include <mongory-core.h> // General include
13
- #include "../foundations/utils.h"
14
-
15
- /**
16
- * @brief Validates that the condition for an existence/presence matcher is a
17
- * boolean.
18
- * @param condition The `mongory_value` condition to validate.
19
- * @return True if `condition` is not NULL and is of type `MONGORY_TYPE_BOOL`,
20
- * false otherwise.
21
- */
22
- static bool mongory_matcher_validate_bool_condition(mongory_value *condition) {
23
- if (condition == NULL) {
24
- return false; // Condition itself must exist.
25
- }
26
- if (condition->type != MONGORY_TYPE_BOOL) {
27
- return false; // Condition must be a boolean value.
28
- }
29
- return true;
30
- }
31
-
32
- /**
33
- * @brief Match function for the $exists matcher.
34
- *
35
- * Compares the existence of the input `value` (i.e., `value != NULL`)
36
- * with the boolean `matcher->condition->data.b`.
37
- * - If `condition` is true, matches if `value` is not NULL.
38
- * - If `condition` is false, matches if `value` is NULL.
39
- *
40
- * @param matcher The $exists matcher instance. Its condition must be boolean.
41
- * @param value The value whose existence is being checked. This is typically
42
- * the result of a field lookup.
43
- * @return True if the existence matches the condition, false otherwise.
44
- */
45
- static inline bool mongory_matcher_exists_match(mongory_matcher *matcher, mongory_value *value) {
46
- // The condition for $exists is a boolean (e.g., field: {$exists: true})
47
- bool condition_expects_existence = matcher->condition->data.b;
48
- bool actual_value_exists = (value != NULL);
49
-
50
- return condition_expects_existence == actual_value_exists;
51
- }
52
-
53
- mongory_matcher *mongory_matcher_exists_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
54
- if (!mongory_matcher_validate_bool_condition(condition)) {
55
- mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$exists condition must be a boolean value.");
56
- return NULL;
57
- }
58
- mongory_matcher *matcher = mongory_matcher_base_new(pool, condition, extern_ctx);
59
- if (!matcher) {
60
- return NULL;
61
- }
62
- matcher->match = mongory_matcher_exists_match;
63
- matcher->original_match = mongory_matcher_exists_match;
64
- matcher->name = mongory_string_cpy(pool, "Exists");
65
- if (!matcher->name) {
66
- return NULL;
67
- }
68
- matcher->priority = 2.0;
69
- return matcher;
70
- }
71
-
72
- /**
73
- * @brief Match function for the $present matcher.
74
- *
75
- * Determines if a `value` is "present" based on its type and content,
76
- * and compares this with the boolean `matcher->condition->data.b`.
77
- * - NULL value: Present if condition is false.
78
- * - Array: Present if non-empty and condition is true.
79
- * - Table: Present if non-empty and condition is true.
80
- * - String: Present if non-NULL, non-empty, and condition is true.
81
- * - MONGORY_TYPE_NULL: Not present (matches if condition is false).
82
- * - Boolean: Present if its value matches the condition.
83
- * - Other non-NULL types: Considered present if condition is true.
84
- *
85
- * @param matcher The $present matcher. Its condition must be boolean.
86
- * @param value The value to check for presence.
87
- * @return True if the presence status matches the condition, false otherwise.
88
- */
89
- static inline bool mongory_matcher_present_match(mongory_matcher *matcher, mongory_value *value) {
90
- bool condition_expects_presence = matcher->condition->data.b;
91
-
92
- if (value == NULL) {
93
- // A NULL value (field does not exist) is "present" if condition_expects_presence is false.
94
- return !condition_expects_presence;
95
- }
96
-
97
- bool actual_value_is_present = false;
98
- switch (value->type) {
99
- case MONGORY_TYPE_ARRAY:
100
- actual_value_is_present = (value->data.a != NULL && value->data.a->count > 0);
101
- break;
102
- case MONGORY_TYPE_TABLE:
103
- actual_value_is_present = (value->data.t != NULL && value->data.t->count > 0);
104
- break;
105
- case MONGORY_TYPE_STRING: {
106
- // If the string is NULL, it is not "present".
107
- // If the strint is " " or more than one space, it is not "present".
108
- char *s = value->data.s;
109
- if (s != NULL && *s != '\0') {
110
- char *p = s;
111
- while (*p != '\0') {
112
- if (*p != ' ') {
113
- actual_value_is_present = true;
114
- break;
115
- }
116
- p++;
117
- }
118
- }
119
- break;
120
- }
121
- case MONGORY_TYPE_NULL:
122
- actual_value_is_present = false; // An explicit BSON-style Null is not "present".
123
- break;
124
- case MONGORY_TYPE_BOOL:
125
- // For a boolean value, "present" means its own value matches the condition.
126
- // e.g. {$present: true} on a true boolean is true.
127
- // {$present: false} on a true boolean is false.
128
- // {$present: true} on a false boolean is false.
129
- // {$present: false} on a false boolean is true.
130
- return value->data.b == condition_expects_presence;
131
- default:
132
- // For other types (int, double, pointer, regex, unsupported), if they are
133
- // not NULL (checked above), they are considered "present".
134
- actual_value_is_present = true;
135
- break;
136
- }
137
-
138
- return condition_expects_presence == actual_value_is_present;
139
- }
140
-
141
- mongory_matcher *mongory_matcher_present_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
142
- if (!mongory_matcher_validate_bool_condition(condition)) {
143
- mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$present condition must be a boolean value.");
144
- return NULL;
145
- }
146
- mongory_matcher *matcher = mongory_matcher_base_new(pool, condition, extern_ctx);
147
- if (!matcher) {
148
- return NULL;
149
- }
150
- matcher->match = mongory_matcher_present_match;
151
- matcher->original_match = mongory_matcher_present_match;
152
- matcher->name = mongory_string_cpy(pool, "Present");
153
- if (!matcher->name) {
154
- return NULL;
155
- }
156
- matcher->priority = 2.0;
157
- return matcher;
158
- }
@@ -1,48 +0,0 @@
1
- #ifndef MONGORY_MATCHER_EXISTENCE_H
2
- #define MONGORY_MATCHER_EXISTENCE_H
3
-
4
- /**
5
- * @file existence_matcher.h
6
- * @brief Defines constructors for existence and presence matchers ($exists,
7
- * $present). This is an internal header for the matcher module.
8
- */
9
-
10
- #include "base_matcher.h"
11
- #include "mongory-core/foundations/memory_pool.h"
12
- #include "mongory-core/foundations/value.h"
13
- #include "mongory-core/matchers/matcher.h" // For mongory_matcher structure
14
-
15
- /**
16
- * @brief Creates an "exists" ($exists) matcher.
17
- *
18
- * This matcher checks for the existence of a field (i.e., if a value is
19
- * non-NULL when retrieved from a table or array). The `condition` for this
20
- * matcher must be a boolean `mongory_value`.
21
- * - If `condition` is true, it matches if the field exists (value is not NULL).
22
- * - If `condition` is false, it matches if the field does not exist (value is
23
- * NULL).
24
- *
25
- * @param pool Memory pool for allocation.
26
- * @param condition A `mongory_value` of type `MONGORY_TYPE_BOOL`.
27
- * @return A new `$exists` matcher, or NULL on failure or invalid condition.
28
- */
29
- mongory_matcher *mongory_matcher_exists_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
30
-
31
- /**
32
- * @brief Creates a "present" ($present) matcher.
33
- *
34
- * This matcher provides a more nuanced check than `$exists`. It considers a
35
- * value "present" based on its type and content (e.g., non-empty array/table,
36
- * non-empty string). The `condition` for this matcher must be a boolean
37
- * `mongory_value`.
38
- * - If `condition` is true, it matches if the value is considered "present".
39
- * - If `condition` is false, it matches if the value is not "present" (e.g.,
40
- * NULL, empty array/table/string).
41
- *
42
- * @param pool Memory pool for allocation.
43
- * @param condition A `mongory_value` of type `MONGORY_TYPE_BOOL`.
44
- * @return A new `$present` matcher, or NULL on failure or invalid condition.
45
- */
46
- mongory_matcher *mongory_matcher_present_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
47
-
48
- #endif /* MONGORY_MATCHER_EXISTENCE_H */
@@ -1,199 +0,0 @@
1
- /**
2
- * @file external_matcher.c
3
- * @brief Implements the $regex matcher.
4
- * This is an internal implementation file for the matcher module.
5
- */
6
- #include "external_matcher.h"
7
- #include "../foundations/config_private.h" // For mongory_internal_regex_adapter
8
- #include "base_matcher.h" // For mongory_matcher_base_new
9
- #include "mongory-core/foundations/config.h"
10
- #include "../foundations/utils.h" // For mongory_string_cpy
11
- #include "mongory-core/foundations/error.h" // For MONGORY_ERROR_INVALID_ARGUMENT
12
- #include "mongory-core/foundations/memory_pool.h"
13
- #include "mongory-core/foundations/value.h"
14
- #include "matcher_explainable.h"
15
- #include "matcher_traversable.h"
16
-
17
- typedef struct mongory_regex_matcher {
18
- mongory_matcher base;
19
- char *options; // Validated $options flags; NULL when absent or empty.
20
- } mongory_regex_matcher;
21
-
22
- /**
23
- * @brief Match function for the $regex matcher.
24
- *
25
- * Checks if the input `value` (which must be a string) matches the regex
26
- * pattern stored in `matcher->condition`. The actual regex matching is
27
- * delegated to `mongory_internal_regex_adapter`: `options_match_func` when
28
- * `$options` flags are present, `match_func` otherwise.
29
- *
30
- * @param matcher The $regex matcher instance.
31
- * @param value The `mongory_value` to test; must be of type
32
- * `MONGORY_TYPE_STRING`.
33
- * @return True if the string value matches the regex, false otherwise or if
34
- * input is not a string.
35
- */
36
- static inline bool mongory_matcher_regex_match(mongory_matcher *matcher, mongory_value *value) {
37
- if (!value || value->type != MONGORY_TYPE_STRING) {
38
- return false; // Regex matching applies only to strings.
39
- }
40
- char *options = ((mongory_regex_matcher *)matcher)->options;
41
- mongory_memory_pool *match_pool = value->pool ? value->pool : matcher->pool;
42
- if (options) {
43
- return mongory_internal_regex_adapter.options_match_func(match_pool, matcher->condition, options, value);
44
- }
45
- if (!mongory_internal_regex_adapter.match_func) {
46
- return false; // Regex adapter or function not configured.
47
- }
48
-
49
- // Delegate to the configured regex function.
50
- return mongory_internal_regex_adapter.match_func(match_pool, matcher->condition, value);
51
- }
52
-
53
- /**
54
- * @brief Validates that the condition for a $regex matcher is valid.
55
- * The condition must be non-NULL and either a `MONGORY_TYPE_STRING` (pattern)
56
- * or `MONGORY_TYPE_REGEX` (pre-compiled regex object).
57
- * @param condition The `mongory_value` condition to validate.
58
- * @return True if the condition is valid for a regex matcher, false otherwise.
59
- */
60
- static inline bool mongory_matcher_regex_condition_validate(mongory_value *condition) {
61
- return condition != NULL && (condition->type == MONGORY_TYPE_STRING || condition->type == MONGORY_TYPE_REGEX);
62
- }
63
-
64
- static inline void mongory_matcher_regex_error_set(mongory_memory_pool *pool, mongory_error_type type, const char *message) {
65
- mongory_error_set(pool, type, message);
66
- }
67
-
68
- /**
69
- * @brief Validates `$options`: a string made only of the flags i, m, s.
70
- * @return True if valid, false (with `pool->error` set) otherwise.
71
- */
72
- static inline bool mongory_matcher_regex_options_validate(mongory_memory_pool *pool, mongory_value *options) {
73
- if (options->type != MONGORY_TYPE_STRING || options->data.s == NULL) {
74
- mongory_matcher_regex_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$options must be a string.");
75
- return false;
76
- }
77
- for (char *flag = options->data.s; *flag; flag++) {
78
- if (*flag != 'i' && *flag != 'm' && *flag != 's') {
79
- mongory_matcher_regex_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT,
80
- mongory_string_cpyf(pool, "$options has unsupported flag '%c'; allowed flags are i, m, s.", *flag));
81
- return false;
82
- }
83
- }
84
- return true;
85
- }
86
-
87
- mongory_matcher *mongory_matcher_regex_with_options_new(mongory_memory_pool *pool, mongory_value *condition,
88
- mongory_value *options, void *extern_ctx) {
89
- if (!mongory_matcher_regex_condition_validate(condition)) {
90
- mongory_matcher_regex_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, "$regex condition must be a string or a regex object.");
91
- return NULL;
92
- }
93
- if (options && !mongory_matcher_regex_options_validate(pool, options)) {
94
- return NULL;
95
- }
96
- bool has_options = options && options->data.s[0] != '\0';
97
- if (has_options && condition->type == MONGORY_TYPE_REGEX) {
98
- // Flags belong to the compiled regex; mixing them is ambiguous.
99
- mongory_matcher_regex_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT,
100
- "$options cannot modify a compiled regex object.");
101
- return NULL;
102
- }
103
- if (has_options && !mongory_internal_regex_adapter.options_match_func) {
104
- mongory_matcher_regex_error_set(pool, MONGORY_ERROR_UNSUPPORTED_OPERATION,
105
- "$options requires a regex adapter set via mongory_regex_options_func_set.");
106
- return NULL;
107
- }
108
-
109
- if (!mongory_compile_budget_count_node(pool)) {
110
- return NULL;
111
- }
112
-
113
- mongory_regex_matcher *matcher = MG_ALLOC_PTR(pool, mongory_regex_matcher);
114
- if (!matcher) {
115
- pool->error = &MONGORY_ALLOC_ERROR;
116
- return NULL;
117
- }
118
- matcher->base.pool = pool;
119
- matcher->base.condition = condition;
120
- matcher->base.sub_count = 0;
121
- matcher->base.match = mongory_matcher_regex_match;
122
- matcher->base.original_match = mongory_matcher_regex_match;
123
- matcher->base.explain = mongory_matcher_base_explain;
124
- matcher->base.traverse = mongory_matcher_leaf_traverse;
125
- matcher->base.extern_ctx = extern_ctx;
126
- matcher->base.name = mongory_string_cpy(pool, "Regex");
127
- if (!matcher->base.name) {
128
- return NULL;
129
- }
130
- matcher->base.priority = 20.0;
131
- matcher->base.parent = NULL;
132
- matcher->options = has_options ? mongory_string_cpy(pool, options->data.s) : NULL;
133
- if (has_options && !matcher->options) {
134
- return NULL;
135
- }
136
- return (mongory_matcher *)matcher;
137
- }
138
-
139
- mongory_matcher *mongory_matcher_regex_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
140
- return mongory_matcher_regex_with_options_new(pool, condition, NULL, extern_ctx);
141
- }
142
-
143
- typedef struct mongory_custom_matcher {
144
- mongory_matcher base;
145
- void *external_matcher;
146
- } mongory_custom_matcher;
147
-
148
- /**
149
- * @brief The match function for a custom matcher.
150
- * @param matcher The matcher to match against.
151
- * @param value The value to match.
152
- * @return True if the value matches the matcher, false otherwise.
153
- */
154
- bool mongory_matcher_custom_match(mongory_matcher *matcher, mongory_value *value) {
155
- if (mongory_custom_matcher_adapter.match == NULL) {
156
- return false; // Custom matcher adapter not initialized.
157
- }
158
- mongory_custom_matcher *custom_matcher = (mongory_custom_matcher *)matcher;
159
- return mongory_custom_matcher_adapter.match(custom_matcher->external_matcher, value);
160
- }
161
-
162
- /**
163
- * @brief Creates a new custom matcher instance.
164
- *
165
- * @param pool The memory pool to use for the matcher's allocations.
166
- * @param key The key for the custom matcher.
167
- * @param condition The `mongory_value` representing the condition for this
168
- * matcher.
169
- * @return mongory_matcher* A pointer to the newly created custom matcher, or
170
- * NULL on failure.
171
- */
172
- mongory_matcher *mongory_matcher_custom_new(mongory_memory_pool *pool, char *key, mongory_value *condition, void *extern_ctx) {
173
- if (mongory_custom_matcher_adapter.build == NULL)
174
- return NULL; // Custom matcher adapter not initialized.
175
- if (!mongory_compile_budget_count_node(pool))
176
- return NULL;
177
- mongory_custom_matcher *matcher = MG_ALLOC_PTR(pool, mongory_custom_matcher);
178
- if (matcher == NULL) {
179
- if (pool) {
180
- pool->error = &MONGORY_ALLOC_ERROR;
181
- }
182
- return NULL;
183
- }
184
- mongory_matcher_custom_context *context = mongory_custom_matcher_adapter.build(key, condition, extern_ctx);
185
- if (context == NULL)
186
- return NULL;
187
- matcher->base.pool = pool;
188
- matcher->base.condition = condition;
189
- matcher->base.name = context->name;
190
- matcher->base.match = mongory_matcher_custom_match;
191
- matcher->base.original_match = mongory_matcher_custom_match;
192
- matcher->base.explain = mongory_matcher_base_explain;
193
- matcher->base.traverse = mongory_matcher_leaf_traverse;
194
- matcher->base.extern_ctx = extern_ctx;
195
- matcher->external_matcher = context->external_matcher;
196
- matcher->base.priority = 20.0;
197
- matcher->base.parent = NULL;
198
- return (mongory_matcher *)matcher;
199
- }
@@ -1,58 +0,0 @@
1
- #ifndef MONGORY_MATCHER_EXTERNAL_H
2
- #define MONGORY_MATCHER_EXTERNAL_H
3
-
4
- /**
5
- * @file external_matcher.h
6
- * @brief Defines the constructor for matchers that are not built-in.
7
- * This is an internal header for the matcher module.
8
- */
9
-
10
- #include "base_matcher.h"
11
- #include "mongory-core/foundations/memory_pool.h"
12
- #include "mongory-core/foundations/value.h"
13
- #include "mongory-core/matchers/matcher.h" // For mongory_matcher structure
14
-
15
- /**
16
- * @brief Creates a regular expression ($regex) matcher.
17
- *
18
- * This matcher tests if an input string `mongory_value` matches a given regular
19
- * expression. The regular expression itself is provided in the `condition`
20
- * value, which can be either a `MONGORY_TYPE_STRING` (the pattern) or a
21
- * `MONGORY_TYPE_REGEX` (a pre-compiled regex object, if the underlying regex
22
- * engine supports it and it's wrapped). The actual regex matching logic is
23
- * delegated to a function provided via `mongory_regex_func_set` (see
24
- * `foundations/config.h`).
25
- *
26
- * @param pool Memory pool for allocation.
27
- * @param condition A `mongory_value` representing the regex pattern. This
28
- * should be of type `MONGORY_TYPE_STRING` or `MONGORY_TYPE_REGEX`.
29
- * @return A new $regex matcher, or NULL on failure or if the condition is
30
- * invalid.
31
- */
32
- mongory_matcher *mongory_matcher_regex_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
33
-
34
- /**
35
- * @brief Creates a $regex matcher with the sibling `$options` value.
36
- *
37
- * `options` may be NULL (no `$options`). Otherwise it must be a string made
38
- * only of the flags 'i', 'm', 's'. A non-empty `options` requires a regex
39
- * adapter set via `mongory_regex_options_func_set`.
40
- *
41
- * @return A new $regex matcher, or NULL (with `pool->error` set) on failure.
42
- */
43
- mongory_matcher *mongory_matcher_regex_with_options_new(mongory_memory_pool *pool, mongory_value *condition,
44
- mongory_value *options, void *extern_ctx);
45
-
46
- /**
47
- * @brief Creates a new custom matcher instance.
48
- *
49
- * @param pool The memory pool to use for the matcher's allocations.
50
- * @param key The key for the custom matcher.
51
- * @param condition The `mongory_value` representing the condition for this
52
- * matcher.
53
- * @return mongory_matcher* A pointer to the newly created custom matcher, or
54
- * NULL on failure.
55
- */
56
- mongory_matcher *mongory_matcher_custom_new(mongory_memory_pool *pool, char *key, mongory_value *condition, void *extern_ctx);
57
-
58
- #endif /* MONGORY_MATCHER_EXTERNAL_H */