mongory 0.9.1-aarch64-linux → 2.0.1-aarch64-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 (98) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +115 -420
  4. data/SUBMODULE_INTEGRATION.md +7 -20
  5. data/docs/clang_bridge.md +28 -63
  6. data/docs/migration.md +17 -0
  7. data/docs/performance.md +14 -51
  8. data/examples/benchmark.rb +10 -26
  9. data/ext/mongory_ext/extconf.rb +1 -1
  10. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +62 -0
  11. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +31 -0
  12. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +3 -0
  13. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +20 -0
  14. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +31 -4
  15. data/ext/mongory_ext/mongory-core/src/foundations/array.c +48 -6
  16. data/ext/mongory_ext/mongory-core/src/foundations/config.c +30 -2
  17. data/ext/mongory_ext/mongory-core/src/foundations/error.c +10 -0
  18. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +78 -19
  19. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +73 -19
  20. data/ext/mongory_ext/mongory-core/src/foundations/table.c +8 -3
  21. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +44 -55
  22. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +7 -0
  23. data/ext/mongory_ext/mongory-core/src/foundations/value.c +3 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +68 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +14 -0
  26. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +9 -4
  27. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +132 -29
  28. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +1 -1
  29. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.c → existence_matcher.c} +10 -12
  30. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.h → existence_matcher.h} +4 -4
  31. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -6
  32. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.c → implicit_array_matcher.c} +57 -38
  33. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.h → implicit_array_matcher.h} +5 -5
  34. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +8 -10
  35. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +337 -96
  36. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +59 -21
  37. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +7 -1
  38. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +4 -4
  39. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -1
  40. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.c → value_matcher.c} +104 -60
  41. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.h → value_matcher.h} +13 -13
  42. data/ext/mongory_ext/mongory_ext.c +2 -2
  43. data/ext/mongory_ext/mongory_ext_internal.h +2 -3
  44. data/ext/mongory_ext/{rb_mongory_cmatcher.c → rb_mongory_matcher.c} +145 -91
  45. data/ext/mongory_ext/rb_mongory_value.c +67 -0
  46. data/lib/core/2.6/mongory_ext.so +0 -0
  47. data/lib/core/2.7/mongory_ext.so +0 -0
  48. data/lib/core/3.0/mongory_ext.so +0 -0
  49. data/lib/core/3.1/mongory_ext.so +0 -0
  50. data/lib/core/3.2/mongory_ext.so +0 -0
  51. data/lib/core/3.3/mongory_ext.so +0 -0
  52. data/lib/core/3.4/mongory_ext.so +0 -0
  53. data/lib/core/4.0/mongory_ext.so +0 -0
  54. data/lib/generators/mongory/install/templates/initializer.rb.erb +0 -5
  55. data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
  56. data/lib/mongory/converters/condition_converter.rb +1 -1
  57. data/lib/mongory/converters/converted.rb +28 -6
  58. data/lib/mongory/converters/key_converter.rb +2 -0
  59. data/lib/mongory/matcher.rb +50 -0
  60. data/lib/mongory/matchers/abstract_matcher.rb +18 -192
  61. data/lib/mongory/matchers.rb +68 -84
  62. data/lib/mongory/query_builder.rb +29 -42
  63. data/lib/mongory/utils/context.rb +10 -15
  64. data/lib/mongory/utils/tree_renderer.rb +98 -0
  65. data/lib/mongory/utils.rb +2 -67
  66. data/lib/mongory/version.rb +1 -1
  67. data/lib/mongory.rb +9 -59
  68. data/script/bench.rb +54 -0
  69. data/scripts/build_with_core.sh +0 -1
  70. metadata +13 -37
  71. data/lib/mongory/c_matcher.rb +0 -57
  72. data/lib/mongory/c_query_builder.rb +0 -44
  73. data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -150
  74. data/lib/mongory/matchers/and_matcher.rb +0 -73
  75. data/lib/mongory/matchers/array_record_matcher.rb +0 -105
  76. data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
  77. data/lib/mongory/matchers/eq_matcher.rb +0 -48
  78. data/lib/mongory/matchers/every_matcher.rb +0 -56
  79. data/lib/mongory/matchers/exists_matcher.rb +0 -53
  80. data/lib/mongory/matchers/field_matcher.rb +0 -160
  81. data/lib/mongory/matchers/gt_matcher.rb +0 -41
  82. data/lib/mongory/matchers/gte_matcher.rb +0 -41
  83. data/lib/mongory/matchers/hash_condition_matcher.rb +0 -80
  84. data/lib/mongory/matchers/in_matcher.rb +0 -84
  85. data/lib/mongory/matchers/literal_matcher.rb +0 -126
  86. data/lib/mongory/matchers/lt_matcher.rb +0 -41
  87. data/lib/mongory/matchers/lte_matcher.rb +0 -41
  88. data/lib/mongory/matchers/ne_matcher.rb +0 -39
  89. data/lib/mongory/matchers/nin_matcher.rb +0 -64
  90. data/lib/mongory/matchers/not_matcher.rb +0 -40
  91. data/lib/mongory/matchers/or_matcher.rb +0 -69
  92. data/lib/mongory/matchers/present_matcher.rb +0 -55
  93. data/lib/mongory/matchers/regex_matcher.rb +0 -109
  94. data/lib/mongory/matchers/size_matcher.rb +0 -54
  95. data/lib/mongory/query_matcher.rb +0 -93
  96. data/lib/mongory/rails.rb +0 -15
  97. data/lib/mongory/utils/debugger.rb +0 -125
  98. data/lib/mongory/utils/rails_patch.rb +0 -22
@@ -11,9 +11,10 @@
11
11
  #include "../foundations/utils.h" // For mongory_string_cpyf
12
12
  // Required internal headers for delegation
13
13
  #include "../foundations/config_private.h" // Potentially for global settings
14
+ #include "../foundations/memory_pool_private.h"
14
15
  #include "base_matcher.h" // For mongory_matcher_base_new if used directly
15
- #include "composite_matcher.h" // For mongory_matcher_table_cond_new
16
- #include "literal_matcher.h" // Potentially for other default constructions
16
+ #include "composite_matcher.h" // For mongory_matcher_condition_new
17
+ #include "value_matcher.h" // Potentially for other default constructions
17
18
  #include "mongory-core/foundations/array.h"
18
19
  #include "../foundations/string_buffer.h"
19
20
  #include "mongory-core/foundations/memory_pool.h"
@@ -28,7 +29,7 @@
28
29
  * uses a factory pattern where this function determines the appropriate
29
30
  * specific matcher to create based on the structure of the `condition` value.
30
31
  *
31
- * Currently, it always delegates to `mongory_matcher_table_cond_new`,
32
+ * Currently, it always delegates to `mongory_matcher_condition_new`,
32
33
  * which handles query documents (tables). This is the most common use case,
33
34
  * where the condition is a table like `{ "field": { "$op": "value" } }`.
34
35
  *
@@ -42,7 +43,9 @@ mongory_matcher *mongory_matcher_new(mongory_memory_pool *pool, mongory_value *c
42
43
  // The core logic is delegated to a more specific constructor.
43
44
  // This design allows for easy extension; for example, a different constructor
44
45
  // could be chosen here based on the `condition->type`.
45
- mongory_matcher *matcher = mongory_matcher_table_cond_new(pool, condition, extern_ctx);
46
+ mongory_compile_budget saved = mongory_compile_budget_begin();
47
+ mongory_matcher *matcher = mongory_matcher_condition_new(pool, condition, extern_ctx);
48
+ mongory_compile_budget_end(saved);
46
49
  if (matcher == NULL) {
47
50
  return NULL;
48
51
  }
@@ -64,41 +67,130 @@ mongory_matcher *mongory_matcher_new(mongory_memory_pool *pool, mongory_value *c
64
67
  */
65
68
  bool mongory_matcher_match(mongory_matcher *matcher, mongory_value *value) { return matcher->match(matcher, value); }
66
69
 
67
- static bool mongory_matcher_explain_cb(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
68
- MONGORY_VALIDATE_PTR(ctx->pool, matcher) && MONGORY_VALIDATE_PTR(ctx->pool, matcher->explain);
69
- if (ctx->pool->error != NULL) {
70
- return false;
70
+ static mongory_value *mongory_matcher_build_explain_node(
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;
71
78
  }
72
- matcher->explain(matcher, ctx);
73
- return true;
79
+ mongory_table *node = mongory_table_new(pool);
80
+ mongory_array *children = mongory_array_new(pool);
81
+ if (!node || !children || pool->error != NULL) {
82
+ return NULL;
83
+ }
84
+ const char *name = matcher->name ? matcher->name : "";
85
+ node->set(node, "name", mongory_value_wrap_s(pool, (char *)name));
86
+ node->set(node, "condition", matcher->condition ? matcher->condition : mongory_value_wrap_n(pool, NULL));
87
+ node->set(node, "priority", mongory_value_wrap_d(pool, matcher->priority));
88
+ if (strcmp(name, "Field") == 0) {
89
+ mongory_field_matcher *field_matcher = (mongory_field_matcher *)matcher;
90
+ if (field_matcher->field) {
91
+ node->set(node, "field", mongory_value_wrap_s(pool, field_matcher->field));
92
+ }
93
+ }
94
+ if (as_skip) {
95
+ node->set(node, "result", mongory_value_wrap_s(pool, "skip"));
96
+ }
97
+
98
+ if (matcher->traverse == mongory_matcher_composite_traverse) {
99
+ mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
100
+ mongory_array *src_children = mongory_atomic_load_ptr_acquire(&composite->children);
101
+ int total = src_children ? (int)src_children->count : 0;
102
+ for (int i = 0; i < total; i++) {
103
+ mongory_matcher *child = (mongory_matcher *)src_children->get(src_children, i);
104
+ mongory_value *child_node = mongory_matcher_build_explain_node(child, pool, as_skip);
105
+ if (!child_node || pool->error != NULL) {
106
+ return NULL;
107
+ }
108
+ children->push(children, child_node);
109
+ }
110
+ } else if (matcher->traverse == mongory_matcher_value_traverse) {
111
+ mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
112
+ mongory_matcher *next = literal->implicit_array_matcher ? literal->implicit_array_matcher : literal->delegate_matcher;
113
+ if (next) {
114
+ mongory_value *child_node = mongory_matcher_build_explain_node(next, pool, as_skip);
115
+ if (!child_node || pool->error != NULL) {
116
+ return NULL;
117
+ }
118
+ children->push(children, child_node);
119
+ }
120
+ }
121
+
122
+ node->set(node, "children", mongory_value_wrap_a(pool, children));
123
+ return mongory_value_wrap_t(pool, node);
74
124
  }
75
125
 
76
- /**
77
- * @brief Generates a human-readable explanation of the matcher's criteria.
78
- *
79
- * This function is a polymorphic wrapper around the `explain` function pointer,
80
- * allowing different matcher types to provide their own specific explanations.
81
- *
82
- * @param matcher The matcher to explain.
83
- * @param temp_pool A temporary memory pool for allocating the explanation string(s).
84
- */
85
- void mongory_matcher_explain(mongory_matcher *matcher, mongory_memory_pool *temp_pool) {
86
- MONGORY_VALIDATE_PTR(temp_pool, matcher) && MONGORY_VALIDATE_PTR(temp_pool, matcher->traverse);
87
- if (temp_pool->error != NULL) {
126
+ static void mongory_explain_tree_print_node(
127
+ mongory_value *node_val,
128
+ mongory_memory_pool *pool,
129
+ const char *prefix,
130
+ int count,
131
+ int total
132
+ ) {
133
+ if (!node_val || node_val->type != MONGORY_TYPE_TABLE || !node_val->data.t) {
88
134
  return;
89
135
  }
90
- mongory_matcher_traverse_context ctx = {
91
- .pool = temp_pool,
92
- .count = 0,
93
- .total = 0,
94
- .acc = "",
95
- .callback = mongory_matcher_explain_cb,
96
- };
97
- matcher->traverse(matcher, &ctx);
136
+ mongory_table *node = node_val->data.t;
137
+ mongory_value *name_val = node->get(node, "name");
138
+ mongory_value *cond_val = node->get(node, "condition");
139
+ mongory_value *field_val = node->get(node, "field");
140
+ mongory_value *children_val = node->get(node, "children");
141
+
142
+ const char *connection = "";
143
+ const char *indent = "";
144
+ if (total > 0) {
145
+ if (total - count == 1) {
146
+ connection = "└─ ";
147
+ indent = " ";
148
+ } else {
149
+ connection = "├─ ";
150
+ indent = "│ ";
151
+ }
152
+ }
153
+
154
+ const char *name = (name_val && name_val->type == MONGORY_TYPE_STRING && name_val->data.s) ? name_val->data.s : "";
155
+ char *cond_str = (cond_val && cond_val->to_str) ? cond_val->to_str(cond_val, pool) : "null";
156
+ if (field_val && field_val->type == MONGORY_TYPE_STRING && field_val->data.s) {
157
+ printf("%s%sField: \"%s\", to match: %s\n", prefix, connection, field_val->data.s, cond_str);
158
+ } else {
159
+ printf("%s%s%s: %s\n", prefix, connection, name, cond_str);
160
+ }
161
+
162
+ if (children_val && children_val->type == MONGORY_TYPE_ARRAY && children_val->data.a) {
163
+ mongory_array *children = children_val->data.a;
164
+ int child_total = (int)children->count;
165
+ char *next_prefix = mongory_string_cpyf(pool, "%s%s", prefix, indent);
166
+ if (!next_prefix) {
167
+ return;
168
+ }
169
+ for (int i = 0; i < child_total; i++) {
170
+ mongory_explain_tree_print_node(children->get(children, i), pool, next_prefix, i, child_total);
171
+ }
172
+ }
173
+ }
174
+
175
+ 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) {
178
+ return NULL;
179
+ }
180
+ return mongory_matcher_build_explain_node(matcher, temp_pool, false);
181
+ }
182
+
183
+ void mongory_matcher_explain(mongory_matcher *matcher, mongory_memory_pool *temp_pool) {
184
+ mongory_value *tree = mongory_matcher_explain_tree(matcher, temp_pool);
185
+ if (tree != NULL) {
186
+ mongory_explain_tree_print_node(tree, temp_pool, "", 0, 0);
187
+ }
98
188
  }
99
189
 
100
190
  typedef struct mongory_matcher_traced_match_context {
101
- char *message;
191
+ mongory_matcher *matcher;
192
+ mongory_value *record;
193
+ bool matched;
102
194
  int level;
103
195
  } mongory_matcher_traced_match_context;
104
196
 
@@ -106,37 +198,18 @@ static bool mongory_matcher_traced_match(mongory_matcher *matcher, mongory_value
106
198
  bool matched = matcher->original_match(matcher, value);
107
199
  mongory_array *trace_stack = matcher->trace_stack;
108
200
  mongory_memory_pool *pool = trace_stack->pool;
109
- mongory_value *condition = matcher->condition;
110
- MONGORY_VALIDATE_PTR(pool, condition) && MONGORY_VALIDATE_PTR(pool, condition->to_str);
111
201
  MONGORY_VALIDATE_PTR(pool, matcher->name);
112
- value && MONGORY_VALIDATE_PTR(pool, value->to_str);
113
202
  if (pool->error != NULL) {
114
203
  return false;
115
204
  }
116
- char *res = NULL;
117
- if (mongory_matcher_trace_result_colorful) {
118
- res = matched ? "\e[30;42mMatched\e[0m" : "\e[30;41mDismatch\e[0m"; // Green for matched, red for mismatch.
119
- } else {
120
- res = matched ? "Matched" : "Dismatch";
121
- }
122
- char *name = matcher->name;
123
- char *cdtn = condition->to_str(condition, pool);
124
- char *rcd = value == NULL ? "Nothing" : value->to_str(value, pool);
125
- char *message;
126
-
127
- if (strcmp(name, "Field") == 0) {
128
- mongory_field_matcher *field_matcher = (mongory_field_matcher *)matcher;
129
- if (!MONGORY_VALIDATE_PTR(pool, field_matcher->field)) {
130
- return false;
131
- }
132
- char *fd = field_matcher->field;
133
- message = mongory_string_cpyf(pool, "%s: %s, field: \"%s\", condition: %s, record: %s\n", name, res, fd, cdtn, rcd);
134
- } else {
135
- message = mongory_string_cpyf(pool, "%s: %s, condition: %s, record: %s\n", name, res, cdtn, rcd);
136
- }
137
205
 
138
206
  mongory_matcher_traced_match_context *trace_result = MG_ALLOC_PTR(pool, mongory_matcher_traced_match_context);
139
- trace_result->message = message;
207
+ if (!trace_result) {
208
+ return matched;
209
+ }
210
+ trace_result->matcher = matcher;
211
+ trace_result->record = value;
212
+ trace_result->matched = matched;
140
213
  trace_result->level = matcher->trace_level;
141
214
  trace_stack->push(trace_stack, mongory_value_wrap_ptr(pool, (void *)trace_result));
142
215
 
@@ -147,9 +220,9 @@ static bool mongory_matcher_enable_trace_cb(mongory_matcher *matcher, mongory_ma
147
220
  matcher->trace_stack = (mongory_array *)ctx->acc;
148
221
  matcher->trace_level = ctx->level;
149
222
  matcher->match = mongory_matcher_traced_match;
150
- if (matcher->traverse == mongory_matcher_literal_traverse) {
151
- mongory_literal_matcher *literal = (mongory_literal_matcher *)matcher;
152
- if (literal->array_record_matcher && literal->delegate_matcher) {
223
+ if (matcher->traverse == mongory_matcher_value_traverse) {
224
+ mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
225
+ if (literal->implicit_array_matcher && literal->delegate_matcher) {
153
226
  mongory_matcher_traverse_context delegate_ctx = {
154
227
  .pool = ctx->pool,
155
228
  .level = ctx->level + 1,
@@ -167,9 +240,9 @@ static bool mongory_matcher_enable_trace_cb(mongory_matcher *matcher, mongory_ma
167
240
  static bool mongory_matcher_disable_trace_cb(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
168
241
  matcher->match = matcher->original_match;
169
242
  matcher->trace_stack = NULL;
170
- if (matcher->traverse == mongory_matcher_literal_traverse) {
171
- mongory_literal_matcher *literal = (mongory_literal_matcher *)matcher;
172
- if (literal->array_record_matcher && literal->delegate_matcher) {
243
+ if (matcher->traverse == mongory_matcher_value_traverse) {
244
+ mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
245
+ if (literal->implicit_array_matcher && literal->delegate_matcher) {
173
246
  mongory_matcher_traverse_context delegate_ctx = {
174
247
  .level = ctx->level + 1,
175
248
  .count = 0,
@@ -182,39 +255,118 @@ static bool mongory_matcher_disable_trace_cb(mongory_matcher *matcher, mongory_m
182
255
  return true;
183
256
  }
184
257
 
185
- static mongory_array *mongory_matcher_traces_sort(mongory_array *self, int level) {
186
- mongory_memory_pool *pool = self->pool;
187
- MONGORY_VALIDATE_PTR(pool, self) && MONGORY_VALIDATE_PTR(pool, self->get);
258
+ static bool mongory_matcher_ptr_in_array(mongory_array *arr, mongory_matcher *target) {
259
+ int total = arr ? (int)arr->count : 0;
260
+ for (int i = 0; i < total; i++) {
261
+ mongory_value *v = arr->get(arr, i);
262
+ if (v && v->data.ptr == (void *)target) {
263
+ return true;
264
+ }
265
+ }
266
+ return false;
267
+ }
268
+
269
+ static mongory_array *mongory_matcher_build_trace_nodes(
270
+ mongory_array *traces,
271
+ int level,
272
+ mongory_memory_pool *pool,
273
+ mongory_array *executed_matchers_out
274
+ ) {
275
+ MONGORY_VALIDATE_PTR(pool, traces) && MONGORY_VALIDATE_PTR(pool, traces->get);
188
276
  if (pool->error != NULL) {
189
277
  return NULL;
190
278
  }
191
- mongory_array *sorted_array = mongory_array_new(pool);
279
+ mongory_array *nodes = mongory_array_new(pool);
192
280
  mongory_array *group = mongory_array_new(pool);
193
- int total = (int)self->count;
281
+ if (!nodes || !group || pool->error != NULL) {
282
+ return NULL;
283
+ }
284
+ int total = (int)traces->count;
194
285
  for (int i = 0; i < total; i++) {
195
- mongory_value *item = self->get(self, i);
286
+ mongory_value *item = traces->get(traces, i);
196
287
  MONGORY_VALIDATE_PTR(pool, item) && MONGORY_VALIDATE_PTR(pool, item->data.ptr);
197
288
  if (pool->error != NULL) {
198
289
  return NULL;
199
290
  }
200
- mongory_matcher_traced_match_context *trace = (mongory_matcher_traced_match_context *)item->data.ptr;
201
- if (trace->level == level) {
202
- sorted_array->push(sorted_array, item);
203
- mongory_array *sorted_group = mongory_matcher_traces_sort(group, level + 1);
204
- MONGORY_VALIDATE_PTR(pool, sorted_group) && MONGORY_VALIDATE_PTR(pool, sorted_group->get);
205
- if (pool->error != NULL) {
291
+ mongory_matcher_traced_match_context *entry = (mongory_matcher_traced_match_context *)item->data.ptr;
292
+ if (entry->level == level) {
293
+ if (executed_matchers_out) {
294
+ mongory_value *ptr_val = mongory_value_wrap_ptr(pool, (void *)entry->matcher);
295
+ if (!ptr_val || !executed_matchers_out->push(executed_matchers_out, ptr_val)) {
296
+ return NULL;
297
+ }
298
+ }
299
+ mongory_array *executed_children = mongory_array_new(pool);
300
+ if (!executed_children || pool->error != NULL) {
301
+ return NULL;
302
+ }
303
+ mongory_array *children = mongory_matcher_build_trace_nodes(group, level + 1, pool, executed_children);
304
+ if (!children || pool->error != NULL) {
206
305
  return NULL;
207
306
  }
208
- int sorted_group_total = (int)sorted_group->count;
209
- for (int j = 0; j < sorted_group_total; j++) {
210
- sorted_array->push(sorted_array, sorted_group->get(sorted_group, j));
307
+
308
+ mongory_matcher *m = entry->matcher;
309
+ if (m->traverse == mongory_matcher_composite_traverse) {
310
+ mongory_composite_matcher *comp = (mongory_composite_matcher *)m;
311
+ mongory_array *src_children = mongory_atomic_load_ptr_acquire(&comp->children);
312
+ int child_total = src_children ? (int)src_children->count : 0;
313
+ for (int j = 0; j < child_total; j++) {
314
+ mongory_matcher *child = (mongory_matcher *)src_children->get(src_children, j);
315
+ if (!mongory_matcher_ptr_in_array(executed_children, child)) {
316
+ mongory_value *skip_node = mongory_matcher_build_explain_node(child, pool, true);
317
+ if (!skip_node || pool->error != NULL) {
318
+ return NULL;
319
+ }
320
+ children->push(children, skip_node);
321
+ }
322
+ }
323
+ } else if (m->traverse == mongory_matcher_value_traverse) {
324
+ mongory_value_matcher *lit = (mongory_value_matcher *)m;
325
+ mongory_matcher *next = lit->implicit_array_matcher ? lit->implicit_array_matcher : lit->delegate_matcher;
326
+ if (next && children->count == 0) {
327
+ mongory_value *skip_node = mongory_matcher_build_explain_node(next, pool, true);
328
+ if (!skip_node || pool->error != NULL) {
329
+ return NULL;
330
+ }
331
+ children->push(children, skip_node);
332
+ }
333
+ }
334
+
335
+ mongory_table *node = mongory_table_new(pool);
336
+ if (!node || pool->error != NULL) {
337
+ return NULL;
338
+ }
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
+ if (entry->record != NULL) {
350
+ node->set(node, "record", entry->record);
351
+ }
352
+ 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
+
355
+ mongory_value *wrapped_node = mongory_value_wrap_t(pool, node);
356
+ if (!wrapped_node || !nodes->push(nodes, wrapped_node) || pool->error != NULL) {
357
+ return NULL;
211
358
  }
212
359
  group = mongory_array_new(pool);
360
+ if (!group) {
361
+ return NULL;
362
+ }
213
363
  } else {
214
- group->push(group, item);
364
+ if (!group->push(group, item)) {
365
+ return NULL;
366
+ }
215
367
  }
216
368
  }
217
- return sorted_array;
369
+ return nodes;
218
370
  }
219
371
 
220
372
  void mongory_matcher_enable_trace_at(mongory_matcher *matcher, mongory_array *trace_stack, int level) {
@@ -242,6 +394,9 @@ void mongory_matcher_enable_trace(mongory_matcher *matcher, mongory_memory_pool
242
394
  return;
243
395
  }
244
396
  mongory_array *trace_stack = mongory_array_new(temp_pool);
397
+ if (!trace_stack) {
398
+ return;
399
+ }
245
400
  mongory_matcher_enable_trace_at(matcher, trace_stack, 0);
246
401
  }
247
402
 
@@ -259,30 +414,116 @@ void mongory_matcher_disable_trace(mongory_matcher *matcher) {
259
414
  matcher->traverse(matcher, &ctx);
260
415
  }
261
416
 
417
+ static void mongory_matcher_print_trace_node(mongory_value *node_val, mongory_memory_pool *pool, int level) {
418
+ if (!node_val || node_val->type != MONGORY_TYPE_TABLE || !node_val->data.t) {
419
+ return;
420
+ }
421
+ mongory_table *node = node_val->data.t;
422
+ mongory_value *name_val = node->get(node, "name");
423
+ mongory_value *cond_val = node->get(node, "condition");
424
+ mongory_value *field_val = node->get(node, "field");
425
+ mongory_value *record_val = node->get(node, "record");
426
+ mongory_value *result_val = node->get(node, "result");
427
+ mongory_value *children_val = node->get(node, "children");
428
+
429
+ const char *name = (name_val && name_val->type == MONGORY_TYPE_STRING && name_val->data.s) ? name_val->data.s : "";
430
+ 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
+ bool is_skip = strcmp(res_raw, "skip") == 0;
433
+ bool is_pass = strcmp(res_raw, "pass") == 0;
434
+ if (mongory_matcher_trace_result_colorful) {
435
+ if (is_skip) {
436
+ res_label = "\e[30;43mSkipped\e[0m";
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
+ }
451
+
452
+ char *cdtn = (cond_val && cond_val->to_str) ? cond_val->to_str(cond_val, pool) : "null";
453
+ char *rcd = (record_val && record_val->to_str) ? record_val->to_str(record_val, pool) : "Nothing";
454
+
455
+ for (int i = 0; i < level * 2; i++) {
456
+ putchar(' ');
457
+ }
458
+ if (field_val && field_val->type == MONGORY_TYPE_STRING && field_val->data.s) {
459
+ if (is_skip) {
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
+ }
464
+ } else {
465
+ if (is_skip) {
466
+ printf("%s: %s, condition: %s\n", name, res_label, cdtn);
467
+ } else {
468
+ printf("%s: %s, condition: %s, record: %s\n", name, res_label, cdtn, rcd);
469
+ }
470
+ }
471
+
472
+ if (children_val && children_val->type == MONGORY_TYPE_ARRAY && children_val->data.a) {
473
+ mongory_array *children = children_val->data.a;
474
+ int child_total = (int)children->count;
475
+ for (int i = 0; i < child_total; i++) {
476
+ mongory_matcher_print_trace_node(children->get(children, i), pool, level + 1);
477
+ }
478
+ }
479
+ }
480
+
262
481
  void mongory_matcher_print_trace(mongory_matcher *matcher) {
263
- if (matcher->trace_stack == NULL)
482
+ if (!matcher || matcher->trace_stack == NULL) {
483
+ return;
484
+ }
485
+ mongory_memory_pool *pool = matcher->trace_stack->pool;
486
+ mongory_array *roots = mongory_matcher_build_trace_nodes(matcher->trace_stack, 0, pool, NULL);
487
+ if (!roots || pool->error != NULL) {
264
488
  return;
265
- mongory_array *sorted_trace_stack = mongory_matcher_traces_sort(matcher->trace_stack, 0);
266
- int total = (int)sorted_trace_stack->count;
489
+ }
490
+ int total = (int)roots->count;
267
491
  for (int i = 0; i < total; i++) {
268
- mongory_value *item = sorted_trace_stack->get(sorted_trace_stack, i);
269
- mongory_matcher_traced_match_context *trace = (mongory_matcher_traced_match_context *)item->data.ptr;
270
- int indent_size = trace->level * 2;
271
- char *indent = MG_ALLOC(sorted_trace_stack->pool, indent_size + 1);
272
- memset(indent, ' ', indent_size);
273
- indent[indent_size] = '\0';
274
- printf("%s%s", indent, trace->message);
492
+ mongory_matcher_print_trace_node(roots->get(roots, i), pool, 0);
493
+ }
494
+ }
495
+
496
+ 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) && MONGORY_VALIDATE_PTR(pool, matcher->match);
499
+ if (!pool || pool->error != NULL) {
500
+ return NULL;
501
+ }
502
+ mongory_matcher_enable_trace(matcher, pool);
503
+ if (pool->error != NULL) {
504
+ mongory_matcher_disable_trace(matcher);
505
+ return NULL;
506
+ }
507
+ matcher->match(matcher, value);
508
+ mongory_array *roots = mongory_matcher_build_trace_nodes(matcher->trace_stack, 0, pool, NULL);
509
+ mongory_matcher_disable_trace(matcher);
510
+ if (!roots || roots->count == 0 || pool->error != NULL) {
511
+ return NULL;
275
512
  }
513
+ return roots->get(roots, 0);
276
514
  }
277
515
 
278
516
  bool mongory_matcher_trace(mongory_matcher *matcher, mongory_value *value) {
279
- MONGORY_VALIDATE_PTR(value->pool, matcher) && MONGORY_VALIDATE_PTR(value->pool, matcher->match);
280
- if (value->pool->error != NULL) {
517
+ mongory_memory_pool *pool = value ? value->pool : (matcher ? matcher->pool : NULL);
518
+ MONGORY_VALIDATE_PTR(pool, matcher) && MONGORY_VALIDATE_PTR(pool, matcher->match);
519
+ if (!pool || pool->error != NULL) {
281
520
  return false;
282
521
  }
283
- mongory_matcher_enable_trace(matcher, value->pool);
284
- bool matched = matcher->match(matcher, value);
285
- mongory_matcher_print_trace(matcher);
286
- mongory_matcher_disable_trace(matcher);
287
- return matched;
522
+ mongory_value *tree = mongory_matcher_trace_tree(matcher, value);
523
+ if (!tree || tree->type != MONGORY_TYPE_TABLE || !tree->data.t) {
524
+ return false;
525
+ }
526
+ mongory_matcher_print_trace_node(tree, pool, 0);
527
+ mongory_value *result_val = tree->data.t->get(tree->data.t, "result");
528
+ return result_val && result_val->type == MONGORY_TYPE_STRING && result_val->data.s && strcmp(result_val->data.s, "pass") == 0;
288
529
  }
@@ -5,24 +5,32 @@
5
5
  #include "../foundations/string_buffer.h"
6
6
  #include "base_matcher.h"
7
7
  #include "composite_matcher.h"
8
- #include "literal_matcher.h"
8
+ #include "value_matcher.h"
9
9
  #include "../foundations/utils.h"
10
10
  #include <stdio.h>
11
11
 
12
12
  char *mongory_matcher_title(mongory_matcher *matcher, mongory_memory_pool *pool) {
13
13
  mongory_value *condition = matcher->condition;
14
+ char *cond_str = (condition && condition->to_str) ? condition->to_str(condition, pool) : NULL;
15
+ if (!cond_str) {
16
+ return NULL;
17
+ }
14
18
  return mongory_string_cpyf(pool, "%s: %s",
15
- matcher->name,
16
- condition->to_str(condition, pool)
19
+ matcher->name ? matcher->name : "",
20
+ cond_str
17
21
  );
18
22
  }
19
23
 
20
24
  char *mongory_matcher_title_with_field(mongory_matcher *matcher, mongory_memory_pool *pool) {
21
25
  mongory_field_matcher *field_matcher = (mongory_field_matcher *)matcher;
22
26
  mongory_value *condition = matcher->condition;
27
+ char *cond_str = (condition && condition->to_str) ? condition->to_str(condition, pool) : NULL;
28
+ if (!cond_str) {
29
+ return NULL;
30
+ }
23
31
  return mongory_string_cpyf(pool, "Field: \"%s\", to match: %s",
24
- field_matcher->field,
25
- condition->to_str(condition, pool)
32
+ field_matcher->field ? field_matcher->field : "",
33
+ cond_str
26
34
  );
27
35
  }
28
36
 
@@ -46,34 +54,64 @@ static inline char *mongory_matcher_indent_connection(int count, int total) {
46
54
  return "│ ";
47
55
  }
48
56
 
49
- bool mongory_matcher_base_explain(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
57
+ static mongory_explain_acc *mongory_explain_acc_child(
58
+ mongory_memory_pool *pool,
59
+ mongory_explain_acc *parent_acc,
60
+ int count,
61
+ int total
62
+ ) {
63
+ char *indent = mongory_matcher_indent_connection(count, total);
64
+ mongory_explain_acc *next = MG_ALLOC_PTR(pool, mongory_explain_acc);
65
+ if (!next) {
66
+ return NULL;
67
+ }
68
+ next->prefix = mongory_string_cpyf(pool, "%s%s", parent_acc->prefix, indent);
69
+ if (!next->prefix) {
70
+ return NULL;
71
+ }
72
+ next->buf = parent_acc->buf;
73
+ return next;
74
+ }
75
+
76
+ static bool mongory_matcher_append_explain_line(
77
+ mongory_matcher_traverse_context *ctx,
78
+ char *title
79
+ ) {
80
+ mongory_explain_acc *acc = (mongory_explain_acc *)ctx->acc;
50
81
  char *connection = mongory_matcher_tail_connection(ctx->count, ctx->total);
82
+ if (!acc || !acc->buf || !title) {
83
+ return false;
84
+ }
85
+ mongory_string_buffer_appendf(acc->buf, "%s%s%s\n", acc->prefix, connection, title);
86
+ return ctx->pool->error == NULL;
87
+ }
88
+
89
+ bool mongory_matcher_base_explain(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
51
90
  char *title = mongory_matcher_title(matcher, ctx->pool);
52
- char *prefix = (char *)ctx->acc;
53
- printf("%s%s%s\n", prefix, connection, title);
54
- return true;
91
+ return mongory_matcher_append_explain_line(ctx, title);
55
92
  }
56
93
 
57
94
  bool mongory_matcher_composite_explain(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
58
- mongory_matcher_base_explain(matcher, ctx);
59
- char *prefix = (char *)ctx->acc;
60
- char *indent = mongory_matcher_indent_connection(ctx->count, ctx->total);
61
- ctx->acc = mongory_string_cpyf(ctx->pool, "%s%s", prefix, indent);
62
- return true;
95
+ if (!mongory_matcher_base_explain(matcher, ctx)) {
96
+ return false;
97
+ }
98
+ mongory_explain_acc *acc = (mongory_explain_acc *)ctx->acc;
99
+ ctx->acc = mongory_explain_acc_child(ctx->pool, acc, ctx->count, ctx->total);
100
+ return ctx->acc != NULL;
63
101
  }
64
102
 
65
- bool mongory_matcher_literal_explain(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
103
+ bool mongory_matcher_value_explain(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
66
104
  return mongory_matcher_composite_explain(matcher, ctx);
67
105
  }
68
106
 
69
107
  bool mongory_matcher_field_explain(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
70
- char *tail_connection = mongory_matcher_tail_connection(ctx->count, ctx->total);
71
108
  char *title = mongory_matcher_title_with_field(matcher, ctx->pool);
72
- char *prefix = (char *)ctx->acc;
73
- printf("%s%s%s\n", prefix, tail_connection, title);
74
- char *indent = mongory_matcher_indent_connection(ctx->count, ctx->total);
75
- ctx->acc = mongory_string_cpyf(ctx->pool, "%s%s", prefix, indent);
76
- return true;
109
+ if (!mongory_matcher_append_explain_line(ctx, title)) {
110
+ return false;
111
+ }
112
+ mongory_explain_acc *acc = (mongory_explain_acc *)ctx->acc;
113
+ ctx->acc = mongory_explain_acc_child(ctx->pool, acc, ctx->count, ctx->total);
114
+ return ctx->acc != NULL;
77
115
  }
78
116
 
79
117
  #endif /* MONGORY_MATCHER_EXPLAINABLE_C */