mongory 0.6.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -0
  3. data/CHANGELOG.md +42 -0
  4. data/README.md +82 -176
  5. data/Rakefile +77 -0
  6. data/SUBMODULE_INTEGRATION.md +325 -0
  7. data/docs/advanced_usage.md +40 -0
  8. data/docs/clang_bridge.md +69 -0
  9. data/docs/field_names.md +30 -0
  10. data/docs/migration.md +30 -0
  11. data/docs/performance.md +61 -0
  12. data/examples/benchmark.rb +98 -19
  13. data/ext/mongory_ext/extconf.rb +91 -0
  14. data/ext/mongory_ext/mongory-core/LICENSE +3 -0
  15. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +105 -0
  16. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +206 -0
  17. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +82 -0
  18. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +95 -0
  19. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +108 -0
  20. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +175 -0
  21. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +76 -0
  22. data/ext/mongory_ext/mongory-core/include/mongory-core.h +12 -0
  23. data/ext/mongory_ext/mongory-core/src/foundations/array.c +246 -0
  24. data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +18 -0
  25. data/ext/mongory_ext/mongory-core/src/foundations/config.c +406 -0
  26. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +30 -0
  27. data/ext/mongory_ext/mongory-core/src/foundations/error.c +30 -0
  28. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +298 -0
  29. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +65 -0
  30. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +49 -0
  31. data/ext/mongory_ext/mongory-core/src/foundations/table.c +458 -0
  32. data/ext/mongory_ext/mongory-core/src/foundations/value.c +459 -0
  33. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c +309 -0
  34. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.h +47 -0
  35. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +161 -0
  36. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +115 -0
  37. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +210 -0
  38. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +83 -0
  39. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +539 -0
  40. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +125 -0
  41. data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.c +144 -0
  42. data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.h +48 -0
  43. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +121 -0
  44. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +46 -0
  45. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +199 -0
  46. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +46 -0
  47. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c +334 -0
  48. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.h +97 -0
  49. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +196 -0
  50. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +107 -0
  51. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +50 -0
  52. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +55 -0
  53. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +23 -0
  54. data/ext/mongory_ext/mongory_ext.c +635 -0
  55. data/lib/mongory/c_query_builder.rb +44 -0
  56. data/lib/mongory/converters/converted.rb +2 -2
  57. data/lib/mongory/matchers/abstract_matcher.rb +4 -0
  58. data/lib/mongory/matchers/abstract_multi_matcher.rb +44 -0
  59. data/lib/mongory/matchers/and_matcher.rb +2 -23
  60. data/lib/mongory/matchers/array_record_matcher.rb +2 -23
  61. data/lib/mongory/matchers/elem_match_matcher.rb +4 -0
  62. data/lib/mongory/matchers/eq_matcher.rb +4 -0
  63. data/lib/mongory/matchers/every_matcher.rb +4 -0
  64. data/lib/mongory/matchers/exists_matcher.rb +4 -0
  65. data/lib/mongory/matchers/field_matcher.rb +4 -0
  66. data/lib/mongory/matchers/gt_matcher.rb +4 -0
  67. data/lib/mongory/matchers/gte_matcher.rb +4 -0
  68. data/lib/mongory/matchers/hash_condition_matcher.rb +2 -23
  69. data/lib/mongory/matchers/in_matcher.rb +13 -4
  70. data/lib/mongory/matchers/literal_matcher.rb +4 -0
  71. data/lib/mongory/matchers/lt_matcher.rb +4 -0
  72. data/lib/mongory/matchers/lte_matcher.rb +4 -0
  73. data/lib/mongory/matchers/ne_matcher.rb +4 -0
  74. data/lib/mongory/matchers/nin_matcher.rb +14 -5
  75. data/lib/mongory/matchers/not_matcher.rb +4 -0
  76. data/lib/mongory/matchers/or_matcher.rb +2 -23
  77. data/lib/mongory/matchers/present_matcher.rb +4 -0
  78. data/lib/mongory/matchers/regex_matcher.rb +4 -0
  79. data/lib/mongory/matchers/size_matcher.rb +4 -0
  80. data/lib/mongory/query_builder.rb +8 -0
  81. data/lib/mongory/utils/context.rb +7 -0
  82. data/lib/mongory/utils.rb +1 -1
  83. data/lib/mongory/version.rb +1 -1
  84. data/lib/mongory.rb +7 -0
  85. data/mongory.gemspec +10 -4
  86. data/scripts/build_with_core.sh +292 -0
  87. metadata +70 -5
@@ -0,0 +1,210 @@
1
+ /**
2
+ * @file compare_matcher.c
3
+ * @brief Implements comparison matchers like `$eq`, `$gt`, `$lt`, etc.
4
+ *
5
+ * This file follows a factory pattern. A generic private constructor,
6
+ * `mongory_matcher_compare_new`, is used to create a base matcher.
7
+ * Each specific comparison operator (e.g., `$eq`, `$gt`) has its own public
8
+ * constructor (e.g., `mongory_matcher_equal_new`) that provides a specialized
9
+ * matching function to the generic constructor.
10
+ */
11
+ #include "compare_matcher.h"
12
+ #include "base_matcher.h" // For mongory_matcher_base_new
13
+ #include <mongory-core.h> // For mongory_value, mongory_matcher types
14
+
15
+ /**
16
+ * @brief Generic constructor for comparison matchers.
17
+ *
18
+ * Initializes a base matcher and sets its `match` function and
19
+ * `original_match` context field to the provided `match_func`.
20
+ *
21
+ * @param pool The memory pool for allocation.
22
+ * @param condition The `mongory_value` to be stored as the comparison target.
23
+ * @param match_func The specific comparison logic function (e.g., for equality,
24
+ * greater than).
25
+ * @return mongory_matcher* A pointer to the newly created comparison matcher,
26
+ * or NULL on failure.
27
+ */
28
+ static inline mongory_matcher *mongory_matcher_compare_new(mongory_memory_pool *pool, mongory_value *condition,
29
+ mongory_matcher_match_func match_func, void *extern_ctx) {
30
+ mongory_matcher *matcher = mongory_matcher_base_new(pool, condition, extern_ctx);
31
+ if (matcher == NULL) {
32
+ return NULL; // Base matcher allocation failed.
33
+ }
34
+ matcher->match = match_func;
35
+ matcher->original_match = match_func; // Store original match function
36
+ return matcher;
37
+ }
38
+
39
+ // ============================================================================
40
+ // Static Match Functions
41
+ //
42
+ // These functions contain the actual logic for each comparison operator.
43
+ // They all follow the `mongory_matcher_match_func` signature and are passed
44
+ // to the generic constructor.
45
+ // ============================================================================
46
+
47
+ /**
48
+ * @brief Match function for equality ($eq).
49
+ *
50
+ * Compares the input `value` with the matcher's `condition` using the
51
+ * polymorphic `comp` function of the value.
52
+ *
53
+ * @param matcher The equality matcher instance.
54
+ * @param value The value to check.
55
+ * @return True if `value` is equal to `matcher->condition`; false otherwise or
56
+ * if the types are not comparable.
57
+ */
58
+ static inline bool mongory_matcher_equal_match(mongory_matcher *matcher, mongory_value *value) {
59
+ if (!value || !value->comp || !matcher->condition)
60
+ return false; // Invalid inputs
61
+ int result = value->comp(value, matcher->condition);
62
+ if (result == mongory_value_compare_fail) {
63
+ return false; // Types are not comparable or other comparison error.
64
+ }
65
+ return result == 0; // 0 indicates equality.
66
+ }
67
+
68
+ mongory_matcher *mongory_matcher_equal_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
69
+ mongory_matcher *matcher = mongory_matcher_compare_new(pool, condition, mongory_matcher_equal_match, extern_ctx);
70
+ if (!matcher) {
71
+ return NULL;
72
+ }
73
+ matcher->name = mongory_string_cpy(pool, "Eq");
74
+ return matcher;
75
+ }
76
+
77
+ /**
78
+ * @brief Match function for inequality ($ne).
79
+ *
80
+ * This is the logical inverse of the `equal_match` function.
81
+ * If the types are not comparable (comparison fails), it is considered "not
82
+ * equal", so this function returns true in that case.
83
+ *
84
+ * @param matcher The inequality matcher instance.
85
+ * @param value The value to check.
86
+ * @return True if `value` is not equal to `matcher->condition` or if they
87
+ * are not comparable.
88
+ */
89
+ static inline bool mongory_matcher_not_equal_match(mongory_matcher *matcher, mongory_value *value) {
90
+ if (!value || !value->comp || !matcher->condition)
91
+ return true; // Invalid inputs, treat as "not equal"
92
+ int result = value->comp(value, matcher->condition);
93
+ if (result == mongory_value_compare_fail) {
94
+ return true; // Incomparable types are considered "not equal".
95
+ }
96
+ return result != 0; // Non-zero indicates inequality.
97
+ }
98
+
99
+ mongory_matcher *mongory_matcher_not_equal_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
100
+ mongory_matcher *matcher = mongory_matcher_compare_new(pool, condition, mongory_matcher_not_equal_match, extern_ctx);
101
+ if (!matcher) {
102
+ return NULL;
103
+ }
104
+ matcher->name = mongory_string_cpy(pool, "Ne");
105
+ return matcher;
106
+ }
107
+
108
+ /**
109
+ * @brief Match function for "greater than" ($gt).
110
+ * @param matcher The $gt matcher instance.
111
+ * @param value The value to check.
112
+ * @return True if `value` is greater than `matcher->condition`, false
113
+ * otherwise or on comparison failure.
114
+ */
115
+ static inline bool mongory_matcher_greater_than_match(mongory_matcher *matcher, mongory_value *value) {
116
+ if (!value || !value->comp || !matcher->condition)
117
+ return false;
118
+ int result = value->comp(value, matcher->condition);
119
+ if (result == mongory_value_compare_fail) {
120
+ return false;
121
+ }
122
+ return result == 1; // 1 indicates value > condition.
123
+ }
124
+
125
+ mongory_matcher *mongory_matcher_greater_than_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
126
+ mongory_matcher *matcher = mongory_matcher_compare_new(pool, condition, mongory_matcher_greater_than_match, extern_ctx);
127
+ if (!matcher) {
128
+ return NULL;
129
+ }
130
+ matcher->name = mongory_string_cpy(pool, "Gt");
131
+ return matcher;
132
+ }
133
+
134
+ /**
135
+ * @brief Match function for "less than" ($lt).
136
+ * @param matcher The $lt matcher instance.
137
+ * @param value The value to check.
138
+ * @return True if `value` is less than `matcher->condition`, false otherwise or
139
+ * on comparison failure.
140
+ */
141
+ static inline bool mongory_matcher_less_than_match(mongory_matcher *matcher, mongory_value *value) {
142
+ if (!value || !value->comp || !matcher->condition)
143
+ return false;
144
+ int result = value->comp(value, matcher->condition);
145
+ if (result == mongory_value_compare_fail) {
146
+ return false;
147
+ }
148
+ return result == -1; // -1 indicates value < condition.
149
+ }
150
+
151
+ mongory_matcher *mongory_matcher_less_than_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
152
+ mongory_matcher *matcher = mongory_matcher_compare_new(pool, condition, mongory_matcher_less_than_match, extern_ctx);
153
+ if (!matcher) {
154
+ return NULL;
155
+ }
156
+ matcher->name = mongory_string_cpy(pool, "Lt");
157
+ return matcher;
158
+ }
159
+
160
+ /**
161
+ * @brief Match function for "greater than or equal" ($gte).
162
+ * @param matcher The $gte matcher instance.
163
+ * @param value The value to check.
164
+ * @return True if `value` is >= `matcher->condition`, false otherwise or on
165
+ * comparison failure.
166
+ */
167
+ static inline bool mongory_matcher_greater_than_or_equal_match(mongory_matcher *matcher, mongory_value *value) {
168
+ if (!value || !value->comp || !matcher->condition)
169
+ return false;
170
+ int result = value->comp(value, matcher->condition);
171
+ if (result == mongory_value_compare_fail) {
172
+ return false;
173
+ }
174
+ return result >= 0; // 0 or 1 indicates value >= condition.
175
+ }
176
+
177
+ mongory_matcher *mongory_matcher_greater_than_or_equal_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
178
+ mongory_matcher *matcher = mongory_matcher_compare_new(pool, condition, mongory_matcher_greater_than_or_equal_match, extern_ctx);
179
+ if (!matcher) {
180
+ return NULL;
181
+ }
182
+ matcher->name = mongory_string_cpy(pool, "Gte");
183
+ return matcher;
184
+ }
185
+
186
+ /**
187
+ * @brief Match function for "less than or equal" ($lte).
188
+ * @param matcher The $lte matcher instance.
189
+ * @param value The value to check.
190
+ * @return True if `value` is <= `matcher->condition`, false otherwise or on
191
+ * comparison failure.
192
+ */
193
+ static inline bool mongory_matcher_less_than_or_equal_match(mongory_matcher *matcher, mongory_value *value) {
194
+ if (!value || !value->comp || !matcher->condition)
195
+ return false;
196
+ int result = value->comp(value, matcher->condition);
197
+ if (result == mongory_value_compare_fail) {
198
+ return false;
199
+ }
200
+ return result <= 0; // 0 or -1 indicates value <= condition.
201
+ }
202
+
203
+ mongory_matcher *mongory_matcher_less_than_or_equal_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
204
+ mongory_matcher *matcher = mongory_matcher_compare_new(pool, condition, mongory_matcher_less_than_or_equal_match, extern_ctx);
205
+ if (!matcher) {
206
+ return NULL;
207
+ }
208
+ matcher->name = mongory_string_cpy(pool, "Lte");
209
+ return matcher;
210
+ }
@@ -0,0 +1,83 @@
1
+ #ifndef MONGORY_MATCHER_COMPARE_H
2
+ #define MONGORY_MATCHER_COMPARE_H
3
+
4
+ /**
5
+ * @file compare_matcher.h
6
+ * @brief Defines constructors for comparison matchers (e.g., equality, greater
7
+ * than). This is an internal header for the matcher module.
8
+ *
9
+ * These matchers compare an input `mongory_value` against a `condition` value
10
+ * stored within the matcher, using the `comp` function of the input value.
11
+ */
12
+
13
+ #include "base_matcher.h"
14
+ #include "mongory-core/foundations/memory_pool.h"
15
+ #include "mongory-core/foundations/value.h"
16
+ #include "mongory-core/matchers/matcher.h" // For mongory_matcher structure
17
+ #include <stdbool.h>
18
+
19
+ /** @name Comparison Matcher Constructors
20
+ * Functions to create instances of various comparison matchers.
21
+ * Each takes a memory pool and a condition value.
22
+ * @{
23
+ */
24
+
25
+ /**
26
+ * @brief Creates an "equal" ($eq) matcher.
27
+ * Matches if the input value is equal to the matcher's condition value.
28
+ * @param pool Memory pool for allocation.
29
+ * @param condition The `mongory_value` to compare against.
30
+ * @return A new `$eq` matcher, or NULL on failure.
31
+ */
32
+ mongory_matcher *mongory_matcher_equal_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
33
+
34
+ /**
35
+ * @brief Creates a "not equal" ($ne) matcher.
36
+ * Matches if the input value is not equal to the matcher's condition value.
37
+ * Also matches if the types are incompatible for comparison.
38
+ * @param pool Memory pool for allocation.
39
+ * @param condition The `mongory_value` to compare against.
40
+ * @return A new `$ne` matcher, or NULL on failure.
41
+ */
42
+ mongory_matcher *mongory_matcher_not_equal_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
43
+
44
+ /**
45
+ * @brief Creates a "greater than" ($gt) matcher.
46
+ * Matches if the input value is greater than the matcher's condition value.
47
+ * @param pool Memory pool for allocation.
48
+ * @param condition The `mongory_value` to compare against.
49
+ * @return A new `$gt` matcher, or NULL on failure.
50
+ */
51
+ mongory_matcher *mongory_matcher_greater_than_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
52
+
53
+ /**
54
+ * @brief Creates a "less than" ($lt) matcher.
55
+ * Matches if the input value is less than the matcher's condition value.
56
+ * @param pool Memory pool for allocation.
57
+ * @param condition The `mongory_value` to compare against.
58
+ * @return A new `$lt` matcher, or NULL on failure.
59
+ */
60
+ mongory_matcher *mongory_matcher_less_than_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
61
+
62
+ /**
63
+ * @brief Creates a "greater than or equal" ($gte) matcher.
64
+ * Matches if the input value is greater than or equal to the matcher's
65
+ * condition value.
66
+ * @param pool Memory pool for allocation.
67
+ * @param condition The `mongory_value` to compare against.
68
+ * @return A new `$gte` matcher, or NULL on failure.
69
+ */
70
+ mongory_matcher *mongory_matcher_greater_than_or_equal_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
71
+
72
+ /**
73
+ * @brief Creates a "less than or equal" ($lte) matcher.
74
+ * Matches if the input value is less than or equal to the matcher's condition
75
+ * value.
76
+ * @param pool Memory pool for allocation.
77
+ * @param condition The `mongory_value` to compare against.
78
+ * @return A new `$lte` matcher, or NULL on failure.
79
+ */
80
+ mongory_matcher *mongory_matcher_less_than_or_equal_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
81
+ /** @} */
82
+
83
+ #endif /* MONGORY_MATCHER_COMPARE_H */