mongory 2.1.1-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 (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -0
  3. data/README.md +45 -74
  4. data/docs/clang_bridge.md +2 -2
  5. data/docs/performance.md +28 -1
  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/coverage.rb +84 -0
  26. metadata +9 -54
  27. data/ext/mongory_ext/extconf.rb +0 -115
  28. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +0 -62
  29. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +0 -122
  30. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +0 -217
  31. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +0 -82
  32. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -115
  33. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +0 -127
  34. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +0 -175
  35. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +0 -103
  36. data/ext/mongory_ext/mongory-core/include/mongory-core.h +0 -12
  37. data/ext/mongory_ext/mongory-core/src/foundations/array.c +0 -336
  38. data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +0 -20
  39. data/ext/mongory_ext/mongory-core/src/foundations/config.c +0 -335
  40. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +0 -49
  41. data/ext/mongory_ext/mongory-core/src/foundations/error.c +0 -48
  42. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +0 -406
  43. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +0 -45
  44. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +0 -117
  45. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +0 -43
  46. data/ext/mongory_ext/mongory-core/src/foundations/table.c +0 -502
  47. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +0 -206
  48. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +0 -77
  49. data/ext/mongory_ext/mongory-core/src/foundations/value.c +0 -548
  50. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +0 -193
  51. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +0 -117
  52. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +0 -241
  53. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +0 -83
  54. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +0 -730
  55. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +0 -131
  56. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +0 -158
  57. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.h +0 -48
  58. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +0 -199
  59. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +0 -58
  60. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +0 -204
  61. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.h +0 -47
  62. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +0 -483
  63. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +0 -46
  64. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +0 -529
  65. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
  66. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
  67. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +0 -61
  68. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +0 -23
  69. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +0 -416
  70. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +0 -97
  71. data/ext/mongory_ext/mongory_ext.c +0 -66
  72. data/ext/mongory_ext/mongory_ext_internal.h +0 -74
  73. data/ext/mongory_ext/rb_mongory_adapters.c +0 -140
  74. data/ext/mongory_ext/rb_mongory_matcher.c +0 -440
  75. data/ext/mongory_ext/rb_mongory_value.c +0 -327
@@ -1,74 +0,0 @@
1
- #ifndef MONGORY_EXT_INTERNAL_H
2
- #define MONGORY_EXT_INTERNAL_H
3
-
4
- /**
5
- * @file mongory_ext_internal.h
6
- * @brief Shared types, globals, and internal prototypes across mongory_ext C translation units.
7
- */
8
-
9
- #include "mongory-core.h"
10
- #include <ruby.h>
11
- #include <ruby/encoding.h>
12
-
13
- // Shared Ruby module, class, error, and converter references initialized in Init_mongory_ext
14
- extern VALUE rb_mongory_m_mongory;
15
- extern VALUE rb_mongory_m_matchers;
16
- extern VALUE rb_mongory_c_matcher;
17
- extern VALUE rb_mongory_c_matcher_context;
18
- extern VALUE rb_mongory_e_error;
19
- extern VALUE rb_mongory_e_type_error;
20
- extern VALUE rb_mongory_data_converter;
21
- extern VALUE rb_mongory_condition_converter;
22
-
23
- typedef struct rb_mongory_memory_pool_t rb_mongory_memory_pool_t;
24
-
25
- // Matcher wrapper structure
26
- typedef struct rb_mongory_matcher_t {
27
- mongory_matcher *matcher;
28
- mongory_value *condition;
29
- mongory_memory_pool *pool;
30
- rb_mongory_memory_pool_t *scratch_free_list;
31
- mongory_table *string_map;
32
- mongory_table *symbol_map;
33
- mongory_array *mark_list;
34
- VALUE ctx;
35
- } rb_mongory_matcher_t;
36
-
37
- struct rb_mongory_memory_pool_t {
38
- mongory_memory_pool base;
39
- rb_mongory_matcher_t *owner;
40
- rb_mongory_memory_pool_t *next_scratch;
41
- };
42
-
43
- typedef struct rb_mongory_table_t {
44
- mongory_table base;
45
- VALUE rb_hash;
46
- rb_mongory_matcher_t *owner;
47
- } rb_mongory_table_t;
48
-
49
- typedef struct rb_mongory_array_t {
50
- mongory_array base;
51
- VALUE rb_array;
52
- rb_mongory_matcher_t *owner;
53
- } rb_mongory_array_t;
54
-
55
- // Pool & error helpers (mongory_ext.c)
56
- rb_mongory_memory_pool_t *rb_mongory_memory_pool_new(void);
57
- bool rb_mongory_error_handling(mongory_memory_pool *pool, const char *error_message);
58
-
59
- // Matcher registration (rb_mongory_matcher.c)
60
- void rb_mongory_init_matcher(void);
61
-
62
- // Value conversion, lazy wrappers, and key caches (rb_mongory_value.c)
63
- mongory_value *rb_to_mongory_value_deep(mongory_memory_pool *pool, VALUE rb_value);
64
- mongory_value *rb_to_mongory_value_shallow(mongory_memory_pool *pool, VALUE rb_value);
65
- mongory_value *rb_mongory_table_wrap(mongory_memory_pool *pool, VALUE rb_hash);
66
- mongory_value *rb_mongory_array_wrap(mongory_memory_pool *pool, VALUE rb_array);
67
- void *mongory_value_to_ruby(mongory_memory_pool *pool, mongory_value *value);
68
- VALUE rb_mongory_cache_fetch_string(rb_mongory_matcher_t *owner, const char *key);
69
- VALUE rb_mongory_cache_fetch_symbol(rb_mongory_matcher_t *owner, const char *key);
70
-
71
- // Core regex & custom matcher adapters (rb_mongory_adapters.c)
72
- void rb_mongory_register_adapters(void);
73
-
74
- #endif // MONGORY_EXT_INTERNAL_H
@@ -1,140 +0,0 @@
1
- /**
2
- * @file rb_mongory_adapters.c
3
- * @brief Bridges mongory-core regex, value converter, and custom matcher hooks to Ruby.
4
- */
5
- #include "mongory_ext_internal.h"
6
- #include <string.h>
7
-
8
- // Replaces a string pattern with its compiled Regexp so later matches reuse it,
9
- // and keeps the Regexp alive for the GC.
10
- static VALUE rb_mongory_regex_cache(mongory_memory_pool *pool, mongory_value *pattern, VALUE rb_re) {
11
- if (pattern->type == MONGORY_TYPE_REGEX && pattern->data.regex) {
12
- return (VALUE)pattern->data.regex;
13
- }
14
- mongory_memory_pool *pattern_pool = pattern->pool;
15
- mongory_value *temp_value = mongory_value_wrap_regex(pool, (void *)rb_re);
16
- if (temp_value) {
17
- memcpy(pattern, temp_value, sizeof(mongory_value));
18
- pattern->pool = pattern_pool;
19
- pattern->origin = (void *)rb_re;
20
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
21
- if (rb_pool->owner) {
22
- rb_pool->owner->mark_list->push(rb_pool->owner->mark_list, pattern);
23
- }
24
- }
25
- return rb_re;
26
- }
27
-
28
- static bool rb_mongory_regex_match_adapter(mongory_memory_pool *pool, mongory_value *pattern, mongory_value *value) {
29
- if (!pattern || !value || value->type != MONGORY_TYPE_STRING) {
30
- return false;
31
- }
32
-
33
- VALUE rb_str = (VALUE)value->origin;
34
- VALUE rb_re = Qnil;
35
-
36
- if (pattern->type == MONGORY_TYPE_REGEX && pattern->data.regex) {
37
- rb_re = (VALUE)pattern->data.regex;
38
- } else if (pattern->type == MONGORY_TYPE_STRING) {
39
- rb_re = rb_mongory_regex_cache(pool, pattern, rb_funcall(rb_cRegexp, rb_intern("new"), 1, (VALUE)pattern->origin));
40
- } else {
41
- return false;
42
- }
43
-
44
- VALUE matched = rb_funcall(rb_re, rb_intern("match?"), 1, rb_str);
45
- return RTEST(matched);
46
- }
47
-
48
- // Regex adapter for `$options` (validated by core to i, m, s). Ruby's `^`/`$`
49
- // already match at line boundaries, so only i and s need flags; Ruby's
50
- // MULTILINE is MongoDB's `s`. Core rejects `$options` on a compiled regex, so
51
- // pattern is a string until the first match caches it.
52
- static bool rb_mongory_regex_options_match_adapter(mongory_memory_pool *pool, mongory_value *pattern,
53
- const char *options, mongory_value *value) {
54
- if (!pattern || !value || value->type != MONGORY_TYPE_STRING) {
55
- return false;
56
- }
57
- if (pattern->type == MONGORY_TYPE_STRING) {
58
- int flags = 0;
59
- for (const char *flag = options; *flag; flag++) {
60
- if (*flag == 'i') {
61
- flags |= NUM2INT(rb_const_get(rb_cRegexp, rb_intern("IGNORECASE")));
62
- } else if (*flag == 's') {
63
- flags |= NUM2INT(rb_const_get(rb_cRegexp, rb_intern("MULTILINE")));
64
- }
65
- }
66
- rb_mongory_regex_cache(pool, pattern,
67
- rb_funcall(rb_cRegexp, rb_intern("new"), 2, (VALUE)pattern->origin, INT2FIX(flags)));
68
- }
69
- return rb_mongory_regex_match_adapter(pool, pattern, value);
70
- }
71
-
72
- static char *rb_mongory_regex_stringify_adapter(mongory_memory_pool *pool, mongory_value *pattern) {
73
- (void)pool;
74
- if (pattern->type != MONGORY_TYPE_REGEX) {
75
- return NULL;
76
- }
77
- VALUE rb_re = (VALUE)pattern->data.regex;
78
- VALUE rb_str = rb_funcall(rb_re, rb_intern("inspect"), 0);
79
- return StringValueCStr(rb_str);
80
- }
81
-
82
- static mongory_matcher_custom_context *rb_mongory_custom_matcher_build(char *key, mongory_value *condition, void *ctx) {
83
- mongory_memory_pool *pool = condition->pool;
84
- rb_mongory_memory_pool_t *rb_pool = (rb_mongory_memory_pool_t *)pool;
85
- rb_mongory_matcher_t *owner = rb_pool->owner;
86
- VALUE matcher_class = rb_funcall(rb_mongory_m_matchers, rb_intern("lookup"), 1,
87
- rb_mongory_cache_fetch_string(owner, key));
88
- if (matcher_class == Qnil) {
89
- return NULL;
90
- }
91
- VALUE kw_hash = rb_hash_new();
92
- rb_hash_aset(kw_hash, ID2SYM(rb_intern("context")), (VALUE)ctx);
93
-
94
- #ifdef RB_PASS_KEYWORDS
95
- VALUE argv_new[2] = { (VALUE)condition->origin, kw_hash };
96
- VALUE matcher = rb_funcallv_kw(matcher_class, rb_intern("new"), 2, argv_new, RB_PASS_KEYWORDS);
97
- #else
98
- VALUE matcher = rb_funcall(matcher_class, rb_intern("new"), 2, (VALUE)condition->origin, kw_hash);
99
- #endif
100
-
101
- if (matcher == Qnil) {
102
- return NULL;
103
- }
104
- mongory_value *matcher_value = mongory_value_wrap_u(pool, NULL);
105
- matcher_value->origin = (void *)matcher;
106
- owner->mark_list->push(owner->mark_list, matcher_value);
107
- VALUE class_name = rb_funcall(matcher_class, rb_intern("name"), 0);
108
- mongory_matcher_custom_context *return_ctx = MG_ALLOC_PTR(pool, mongory_matcher_custom_context);
109
- if (return_ctx == NULL) {
110
- return NULL;
111
- }
112
- return_ctx->name = StringValueCStr(class_name);
113
- return_ctx->external_matcher = (void *)matcher;
114
- return return_ctx;
115
- }
116
-
117
- static bool rb_mongory_custom_matcher_match(void *ruby_matcher, mongory_value *value) {
118
- VALUE matcher = (VALUE)ruby_matcher;
119
- VALUE match_result = rb_funcall(matcher, rb_intern("match?"), 1, value->origin);
120
- return RTEST(match_result);
121
- }
122
-
123
- static bool rb_mongory_custom_matcher_lookup(char *key) {
124
- VALUE matcher_class = rb_funcall(rb_mongory_m_matchers, rb_intern("lookup"), 1, rb_str_new_cstr(key));
125
- return RTEST(matcher_class);
126
- }
127
-
128
- void rb_mongory_register_adapters(void) {
129
- mongory_regex_func_set(rb_mongory_regex_match_adapter);
130
- mongory_regex_options_func_set(rb_mongory_regex_options_match_adapter);
131
- mongory_regex_stringify_func_set(rb_mongory_regex_stringify_adapter);
132
-
133
- mongory_value_converter_deep_convert_set(rb_to_mongory_value_deep);
134
- mongory_value_converter_shallow_convert_set(rb_to_mongory_value_shallow);
135
- mongory_value_converter_recover_set(mongory_value_to_ruby);
136
-
137
- mongory_custom_matcher_match_func_set(rb_mongory_custom_matcher_match);
138
- mongory_custom_matcher_build_func_set(rb_mongory_custom_matcher_build);
139
- mongory_custom_matcher_lookup_func_set(rb_mongory_custom_matcher_lookup);
140
- }
@@ -1,440 +0,0 @@
1
- /**
2
- * @file rb_mongory_matcher.c
3
- * @brief Mongory::Matcher Ruby class lifecycle, matching, and trace methods.
4
- */
5
- #include "mongory_ext_internal.h"
6
- #include <stdio.h>
7
- #include <string.h>
8
-
9
- static void rb_mongory_matcher_mark(void *ptr);
10
- static void rb_mongory_matcher_free(void *ptr);
11
- static void rb_mongory_matcher_parse_argv(rb_mongory_matcher_t *self, int argc, VALUE *argv);
12
-
13
- static const rb_data_type_t rb_mongory_matcher_type = {
14
- .wrap_struct_name = "mongory_matcher",
15
- .function = {
16
- .dmark = rb_mongory_matcher_mark,
17
- .dfree = rb_mongory_matcher_free,
18
- .dsize = NULL,
19
- },
20
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
21
- };
22
-
23
- static void rb_mongory_matcher_release_pools(rb_mongory_matcher_t *wrapper) {
24
- if (wrapper->pool) {
25
- wrapper->pool->free(wrapper->pool);
26
- wrapper->pool = NULL;
27
- }
28
- while (wrapper->scratch_free_list) {
29
- rb_mongory_memory_pool_t *scratch = wrapper->scratch_free_list;
30
- wrapper->scratch_free_list = scratch->next_scratch;
31
- scratch->next_scratch = NULL;
32
- scratch->base.free(&scratch->base);
33
- }
34
- wrapper->matcher = NULL;
35
- wrapper->condition = NULL;
36
- wrapper->string_map = NULL;
37
- wrapper->symbol_map = NULL;
38
- wrapper->mark_list = NULL;
39
- wrapper->ctx = 0;
40
- }
41
-
42
- static rb_mongory_matcher_t *get_initialized_matcher(VALUE self) {
43
- rb_mongory_matcher_t *wrapper;
44
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
45
- if (!wrapper || !wrapper->matcher || !wrapper->condition || !wrapper->pool) {
46
- rb_raise(rb_mongory_e_error, "uninitialized %s", rb_obj_classname(self));
47
- }
48
- return wrapper;
49
- }
50
-
51
- static void rb_mongory_matcher_free(void *ptr) {
52
- rb_mongory_matcher_t *wrapper = (rb_mongory_matcher_t *)ptr;
53
- if (!wrapper) return;
54
- rb_mongory_matcher_release_pools(wrapper);
55
- xfree(wrapper);
56
- }
57
-
58
- static bool gc_mark_array_cb(mongory_value *value, void *acc) {
59
- (void)acc;
60
- if (value && value->origin) rb_gc_mark((VALUE)value->origin);
61
- return true;
62
- }
63
-
64
- static void rb_mongory_matcher_mark(void *ptr) {
65
- rb_mongory_matcher_t *self = (rb_mongory_matcher_t *)ptr;
66
- if (!self) return;
67
- if (self->ctx) rb_gc_mark(self->ctx);
68
- if (self->mark_list) {
69
- self->mark_list->each(self->mark_list, NULL, gc_mark_array_cb);
70
- }
71
- }
72
-
73
- static VALUE rb_mongory_matcher_alloc(VALUE klass) {
74
- rb_mongory_matcher_t *wrapper;
75
- VALUE obj = TypedData_Make_Struct(klass, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
76
- memset(wrapper, 0, sizeof(rb_mongory_matcher_t));
77
- return obj;
78
- }
79
-
80
- // Mongory::Matcher#initialize(condition, context: ...)
81
- static VALUE rb_mongory_matcher_initialize(int argc, VALUE *argv, VALUE self) {
82
- rb_mongory_matcher_t *wrapper;
83
- TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
84
- rb_mongory_matcher_release_pools(wrapper);
85
-
86
- rb_mongory_memory_pool_t *matcher_pool = rb_mongory_memory_pool_new();
87
- rb_mongory_memory_pool_t *scratch_pool = rb_mongory_memory_pool_new();
88
- if (!matcher_pool || !scratch_pool) {
89
- if (matcher_pool) matcher_pool->base.free(&matcher_pool->base);
90
- if (scratch_pool) scratch_pool->base.free(&scratch_pool->base);
91
- rb_raise(rb_mongory_e_error, "Failed to allocate matcher memory pools");
92
- }
93
- mongory_memory_pool *matcher_pool_base = &matcher_pool->base;
94
- wrapper->pool = matcher_pool_base;
95
- wrapper->scratch_free_list = scratch_pool;
96
- wrapper->string_map = mongory_table_new(matcher_pool_base);
97
- wrapper->symbol_map = mongory_table_new(matcher_pool_base);
98
- wrapper->mark_list = mongory_array_new(matcher_pool_base);
99
- matcher_pool->owner = wrapper;
100
- scratch_pool->owner = wrapper;
101
- rb_mongory_matcher_parse_argv(wrapper, argc, argv);
102
-
103
- mongory_matcher *matcher = mongory_matcher_new(matcher_pool_base, wrapper->condition, wrapper->ctx);
104
- if (matcher_pool_base->error || !matcher) {
105
- char err_msg[256];
106
- snprintf(err_msg, sizeof(err_msg), "%s",
107
- (matcher_pool_base->error && matcher_pool_base->error->message)
108
- ? matcher_pool_base->error->message
109
- : "Unknown error");
110
- rb_mongory_matcher_release_pools(wrapper);
111
- rb_raise(rb_mongory_e_type_error, "Failed to create matcher: %s", err_msg);
112
- }
113
-
114
- wrapper->matcher = matcher;
115
- return self;
116
- }
117
-
118
- // Disallow dup/clone so C memory pools are never shared across instances
119
- static VALUE rb_mongory_matcher_initialize_copy(VALUE self, VALUE orig) {
120
- (void)orig;
121
- rb_raise(rb_eTypeError, "can't copy %s", rb_obj_classname(self));
122
- }
123
-
124
- static void rb_mongory_matcher_parse_argv(rb_mongory_matcher_t *self, int argc, VALUE *argv) {
125
- VALUE condition, kw_hash;
126
- if (argc == 1) {
127
- // In Ruby 3.0+, a single bare hash like `Matcher.new(name: 'John')` is marked
128
- // as keywords, which would fail `rb_scan_args(..., "1:", ...)` with 0 positional args.
129
- rb_scan_args(argc, argv, "1", &condition);
130
- kw_hash = Qnil;
131
- } else {
132
- rb_scan_args(argc, argv, "1:", &condition, &kw_hash);
133
- }
134
- const ID ctx_id[1] = { rb_intern("context") };
135
- VALUE kw_vals[1] = { Qundef };
136
- if (kw_hash != Qnil) {
137
- rb_get_kwargs(kw_hash, ctx_id, 0, 1, kw_vals);
138
- }
139
- if (kw_vals[0] != Qundef) {
140
- self->ctx = kw_vals[0];
141
- } else {
142
- self->ctx = rb_funcall(rb_mongory_c_matcher_context, rb_intern("new"), 0);
143
- }
144
- VALUE converted_condition = rb_funcall(rb_mongory_condition_converter, rb_intern("convert"), 1, condition);
145
- self->condition = rb_to_mongory_value_deep(self->pool, converted_condition);
146
- self->mark_list->push(self->mark_list, self->condition);
147
- mongory_value *store_ctx = mongory_value_wrap_u(self->pool, (void *)self->ctx);
148
- store_ctx->origin = (void *)self->ctx;
149
- self->mark_list->push(self->mark_list, store_ctx);
150
- }
151
-
152
- typedef struct {
153
- rb_mongory_matcher_t *wrapper;
154
- mongory_memory_pool *owner_pool;
155
- rb_mongory_memory_pool_t *scratch;
156
- } rb_mongory_scratch_lease_t;
157
-
158
- typedef struct {
159
- rb_mongory_matcher_t *wrapper;
160
- mongory_memory_pool *scratch_pool;
161
- VALUE data;
162
- } rb_mongory_match_call_t;
163
-
164
- typedef struct {
165
- rb_mongory_matcher_t *wrapper;
166
- mongory_memory_pool *scratch_pool;
167
- } rb_mongory_explain_call_t;
168
-
169
- // Pops an idle scratch pool from wrapper->scratch_free_list, or allocates a new
170
- // reusable pool when re-entered concurrently; release resets and pushes it back.
171
- static rb_mongory_scratch_lease_t rb_mongory_scratch_acquire(rb_mongory_matcher_t *wrapper) {
172
- rb_mongory_memory_pool_t *scratch = wrapper->scratch_free_list;
173
- if (scratch) {
174
- wrapper->scratch_free_list = scratch->next_scratch;
175
- scratch->next_scratch = NULL;
176
- scratch->base.error = NULL;
177
- } else {
178
- scratch = rb_mongory_memory_pool_new();
179
- if (!scratch) {
180
- rb_raise(rb_mongory_e_error, "Failed to allocate scratch pool");
181
- }
182
- scratch->owner = wrapper;
183
- }
184
- rb_mongory_scratch_lease_t lease = {
185
- .wrapper = wrapper,
186
- .owner_pool = wrapper->pool,
187
- .scratch = scratch,
188
- };
189
- return lease;
190
- }
191
-
192
- static VALUE rb_mongory_scratch_release(VALUE arg) {
193
- rb_mongory_scratch_lease_t *lease = (rb_mongory_scratch_lease_t *)arg;
194
- rb_mongory_memory_pool_t *scratch = lease->scratch;
195
- if (!scratch) {
196
- return Qnil;
197
- }
198
- lease->scratch = NULL;
199
- if (lease->wrapper && lease->wrapper->pool == lease->owner_pool) {
200
- scratch->base.error = NULL;
201
- scratch->base.reset(&scratch->base);
202
- scratch->next_scratch = lease->wrapper->scratch_free_list;
203
- lease->wrapper->scratch_free_list = scratch;
204
- } else {
205
- scratch->base.free(&scratch->base);
206
- }
207
- return Qnil;
208
- }
209
-
210
- static VALUE rb_mongory_matcher_match_body(VALUE arg) {
211
- rb_mongory_match_call_t *call = (rb_mongory_match_call_t *)arg;
212
- rb_mongory_matcher_t *self_wrapper = call->wrapper;
213
- mongory_matcher *matcher = self_wrapper->matcher;
214
- mongory_memory_pool *scratch_pool = call->scratch_pool;
215
- mongory_value *data_value = rb_to_mongory_value_shallow(scratch_pool, call->data);
216
-
217
- if (rb_mongory_error_handling(scratch_pool, "Match failed")) {
218
- return Qnil;
219
- }
220
-
221
- bool result = mongory_matcher_match(matcher, data_value);
222
- return result ? Qtrue : Qfalse;
223
- }
224
-
225
- // Mongory::Matcher#match?(data)
226
- static VALUE rb_mongory_matcher_match(VALUE self, VALUE data) {
227
- rb_mongory_matcher_t *self_wrapper = get_initialized_matcher(self);
228
- rb_mongory_scratch_lease_t lease = rb_mongory_scratch_acquire(self_wrapper);
229
- rb_mongory_match_call_t call = {
230
- .wrapper = self_wrapper,
231
- .scratch_pool = &lease.scratch->base,
232
- .data = data,
233
- };
234
- VALUE result = rb_ensure(rb_mongory_matcher_match_body, (VALUE)&call, rb_mongory_scratch_release, (VALUE)&lease);
235
- RB_GC_GUARD(self);
236
- return result;
237
- }
238
-
239
- static VALUE rb_mongory_value_to_rb(mongory_value *val);
240
-
241
- static bool rb_mongory_table_entry_to_rb_hash(char *key, mongory_value *val, void *acc) {
242
- VALUE hash = (VALUE)acc;
243
- VALUE rb_key = rb_utf8_str_new_cstr(key ? key : "");
244
- VALUE rb_val = rb_mongory_value_to_rb(val);
245
- rb_hash_aset(hash, rb_key, rb_val);
246
- return true;
247
- }
248
-
249
- static VALUE rb_mongory_value_to_rb(mongory_value *val) {
250
- if (!val) {
251
- return Qnil;
252
- }
253
- if (val->origin != NULL) {
254
- return (VALUE)val->origin;
255
- }
256
- switch (val->type) {
257
- case MONGORY_TYPE_NULL:
258
- return Qnil;
259
- case MONGORY_TYPE_BOOL:
260
- return val->data.b ? Qtrue : Qfalse;
261
- case MONGORY_TYPE_INT:
262
- return LL2NUM(val->data.i);
263
- case MONGORY_TYPE_DOUBLE:
264
- return DBL2NUM(val->data.d);
265
- case MONGORY_TYPE_STRING:
266
- return rb_utf8_str_new_cstr(val->data.s ? val->data.s : "");
267
- case MONGORY_TYPE_ARRAY: {
268
- mongory_array *arr = val->data.a;
269
- size_t count = arr ? arr->count : 0;
270
- VALUE rb_arr = rb_ary_new_capa((long)count);
271
- for (size_t i = 0; i < count; i++) {
272
- rb_ary_push(rb_arr, rb_mongory_value_to_rb(arr->get(arr, i)));
273
- }
274
- return rb_arr;
275
- }
276
- case MONGORY_TYPE_TABLE: {
277
- VALUE rb_hash = rb_hash_new();
278
- if (val->data.t && val->data.t->each) {
279
- val->data.t->each(val->data.t, (void *)rb_hash, rb_mongory_table_entry_to_rb_hash);
280
- }
281
- return rb_hash;
282
- }
283
- default:
284
- return Qnil;
285
- }
286
- }
287
-
288
- static VALUE rb_mongory_tree_node_to_rb(mongory_value *node_val) {
289
- if (!node_val || node_val->type != MONGORY_TYPE_TABLE || !node_val->data.t) {
290
- return Qnil;
291
- }
292
- mongory_table *node = node_val->data.t;
293
- VALUE hash = rb_hash_new();
294
-
295
- mongory_value *name_val = node->get(node, "name");
296
- if (name_val && name_val->type == MONGORY_TYPE_STRING) {
297
- rb_hash_aset(hash, ID2SYM(rb_intern("name")), rb_utf8_str_new_cstr(name_val->data.s ? name_val->data.s : ""));
298
- }
299
-
300
- mongory_value *cond_val = node->get(node, "condition");
301
- rb_hash_aset(hash, ID2SYM(rb_intern("condition")), rb_mongory_value_to_rb(cond_val));
302
-
303
- mongory_value *priority_val = node->get(node, "priority");
304
- if (priority_val && priority_val->type == MONGORY_TYPE_DOUBLE) {
305
- rb_hash_aset(hash, ID2SYM(rb_intern("priority")), DBL2NUM(priority_val->data.d));
306
- }
307
-
308
- mongory_value *field_val = node->get(node, "field");
309
- if (field_val && field_val->type == MONGORY_TYPE_STRING && field_val->data.s) {
310
- rb_hash_aset(hash, ID2SYM(rb_intern("field")), rb_utf8_str_new_cstr(field_val->data.s));
311
- }
312
-
313
- mongory_value *record_val = node->get(node, "record");
314
- if (record_val) {
315
- rb_hash_aset(hash, ID2SYM(rb_intern("record")), rb_mongory_value_to_rb(record_val));
316
- }
317
-
318
- mongory_value *result_val = node->get(node, "result");
319
- if (result_val && result_val->type == MONGORY_TYPE_STRING && result_val->data.s) {
320
- rb_hash_aset(hash, ID2SYM(rb_intern("result")), ID2SYM(rb_intern(result_val->data.s)));
321
- }
322
-
323
- mongory_value *children_val = node->get(node, "children");
324
- VALUE rb_children = rb_ary_new();
325
- if (children_val && children_val->type == MONGORY_TYPE_ARRAY && children_val->data.a) {
326
- mongory_array *arr = children_val->data.a;
327
- for (size_t i = 0; i < arr->count; i++) {
328
- rb_ary_push(rb_children, rb_mongory_tree_node_to_rb(arr->get(arr, i)));
329
- }
330
- }
331
- rb_hash_aset(hash, ID2SYM(rb_intern("children")), rb_children);
332
-
333
- return hash;
334
- }
335
-
336
- static VALUE rb_mongory_tree_renderer_module(void) {
337
- VALUE m_utils = rb_const_get(rb_mongory_m_mongory, rb_intern("Utils"));
338
- return rb_const_get(m_utils, rb_intern("TreeRenderer"));
339
- }
340
-
341
- static VALUE rb_mongory_matcher_explain_tree_body(VALUE arg) {
342
- rb_mongory_explain_call_t *call = (rb_mongory_explain_call_t *)arg;
343
- mongory_value *tree = mongory_matcher_explain_tree(call->wrapper->matcher, call->scratch_pool);
344
- rb_mongory_error_handling(call->scratch_pool, "Explain failed");
345
- return rb_mongory_tree_node_to_rb(tree);
346
- }
347
-
348
- // Mongory::Matcher#explain_tree
349
- static VALUE rb_mongory_matcher_explain_tree(VALUE self) {
350
- rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
351
- rb_mongory_scratch_lease_t lease = rb_mongory_scratch_acquire(wrapper);
352
- rb_mongory_explain_call_t call = {
353
- .wrapper = wrapper,
354
- .scratch_pool = &lease.scratch->base,
355
- };
356
- VALUE result = rb_ensure(rb_mongory_matcher_explain_tree_body, (VALUE)&call, rb_mongory_scratch_release, (VALUE)&lease);
357
- RB_GC_GUARD(self);
358
- return result;
359
- }
360
-
361
- // Mongory::Matcher#explain
362
- static VALUE rb_mongory_matcher_explain(VALUE self) {
363
- VALUE tree = rb_mongory_matcher_explain_tree(self);
364
- if (!NIL_P(tree)) {
365
- rb_funcall(rb_mongory_tree_renderer_module(), rb_intern("print_explain"), 1, tree);
366
- }
367
- RB_GC_GUARD(tree);
368
- return Qnil;
369
- }
370
-
371
- // Mongory::Matcher#trace_tree(data)
372
- static VALUE rb_mongory_matcher_trace_tree(VALUE self, VALUE data) {
373
- rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
374
- rb_mongory_memory_pool_t *trace_pool = rb_mongory_memory_pool_new();
375
- trace_pool->owner = wrapper;
376
- mongory_memory_pool *trace_pool_base = &trace_pool->base;
377
- mongory_value *data_value = rb_to_mongory_value_shallow(trace_pool_base, data);
378
-
379
- if (trace_pool_base->error) {
380
- char err_msg[256];
381
- snprintf(err_msg, sizeof(err_msg), "%s",
382
- trace_pool_base->error->message ? trace_pool_base->error->message : "Unknown error");
383
- trace_pool_base->free(trace_pool_base);
384
- rb_raise(rb_mongory_e_type_error, "Trace failed: %s", err_msg);
385
- }
386
-
387
- mongory_value *tree = mongory_matcher_trace_tree(wrapper->matcher, data_value);
388
- if (trace_pool_base->error) {
389
- char err_msg[256];
390
- snprintf(err_msg, sizeof(err_msg), "%s",
391
- trace_pool_base->error->message ? trace_pool_base->error->message : "Unknown error");
392
- trace_pool_base->free(trace_pool_base);
393
- rb_raise(rb_mongory_e_type_error, "Trace failed: %s", err_msg);
394
- }
395
- VALUE result = rb_mongory_tree_node_to_rb(tree);
396
- trace_pool_base->free(trace_pool_base);
397
-
398
- return result;
399
- }
400
-
401
- // Mongory::Matcher#trace(data)
402
- static VALUE rb_mongory_matcher_trace(VALUE self, VALUE data) {
403
- VALUE tree = rb_mongory_matcher_trace_tree(self, data);
404
- if (NIL_P(tree)) {
405
- return Qfalse;
406
- }
407
- rb_funcall(rb_mongory_tree_renderer_module(), rb_intern("print_trace"), 1, tree);
408
- VALUE res = rb_hash_aref(tree, ID2SYM(rb_intern("result")));
409
- RB_GC_GUARD(tree);
410
- return res == ID2SYM(rb_intern("pass")) ? Qtrue : Qfalse;
411
- }
412
-
413
- // Mongory::Matcher#condition
414
- static VALUE rb_mongory_matcher_condition(VALUE self) {
415
- rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
416
-
417
- return (VALUE)wrapper->condition->origin;
418
- }
419
-
420
- // Mongory::Matcher#context
421
- static VALUE rb_mongory_matcher_context(VALUE self) {
422
- rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
423
-
424
- return wrapper->ctx ? wrapper->ctx : Qnil;
425
- }
426
-
427
- void rb_mongory_init_matcher(void) {
428
- rb_mongory_c_matcher = rb_define_class_under(rb_mongory_m_mongory, "Matcher", rb_cObject);
429
- rb_define_alloc_func(rb_mongory_c_matcher, rb_mongory_matcher_alloc);
430
-
431
- rb_define_method(rb_mongory_c_matcher, "initialize", rb_mongory_matcher_initialize, -1);
432
- rb_define_method(rb_mongory_c_matcher, "initialize_copy", rb_mongory_matcher_initialize_copy, 1);
433
- rb_define_method(rb_mongory_c_matcher, "match?", rb_mongory_matcher_match, 1);
434
- rb_define_method(rb_mongory_c_matcher, "explain_tree", rb_mongory_matcher_explain_tree, 0);
435
- rb_define_method(rb_mongory_c_matcher, "explain", rb_mongory_matcher_explain, 0);
436
- rb_define_method(rb_mongory_c_matcher, "condition", rb_mongory_matcher_condition, 0);
437
- rb_define_method(rb_mongory_c_matcher, "context", rb_mongory_matcher_context, 0);
438
- rb_define_method(rb_mongory_c_matcher, "trace_tree", rb_mongory_matcher_trace_tree, 1);
439
- rb_define_method(rb_mongory_c_matcher, "trace", rb_mongory_matcher_trace, 1);
440
- }