mongory 0.8.1-aarch64-linux → 0.9.0-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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +66 -0
- data/README.md +12 -0
- data/docs/advanced_usage.md +7 -0
- data/docs/clang_bridge.md +6 -0
- data/ext/mongory_ext/extconf.rb +6 -1
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +25 -0
- data/ext/mongory_ext/mongory-core/src/foundations/array.c +22 -15
- data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +1 -0
- data/ext/mongory_ext/mongory-core/src/foundations/config.c +64 -27
- data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +1 -0
- data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +49 -0
- data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +45 -0
- data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +0 -2
- data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +0 -6
- data/ext/mongory_ext/mongory-core/src/foundations/table.c +9 -10
- data/ext/mongory_ext/mongory-core/src/foundations/utils.c +21 -14
- data/ext/mongory_ext/mongory-core/src/foundations/utils.h +2 -2
- data/ext/mongory_ext/mongory-core/src/foundations/value.c +54 -9
- data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c +31 -10
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +2 -0
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +2 -0
- data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +32 -13
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +34 -13
- data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.c +16 -3
- data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +76 -15
- data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +12 -0
- data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c +57 -15
- data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +43 -7
- data/ext/mongory_ext/mongory_ext.c +36 -687
- data/ext/mongory_ext/mongory_ext_internal.h +75 -0
- data/ext/mongory_ext/rb_mongory_adapters.c +140 -0
- data/ext/mongory_ext/rb_mongory_cmatcher.c +386 -0
- data/ext/mongory_ext/rb_mongory_value.c +260 -0
- data/lib/core/2.6/mongory_ext.so +0 -0
- data/lib/core/2.7/mongory_ext.so +0 -0
- data/lib/core/3.0/mongory_ext.so +0 -0
- data/lib/core/3.1/mongory_ext.so +0 -0
- data/lib/core/3.2/mongory_ext.so +0 -0
- data/lib/core/3.3/mongory_ext.so +0 -0
- data/lib/core/3.4/mongory_ext.so +0 -0
- data/lib/core/4.0/mongory_ext.so +0 -0
- data/lib/generators/mongory/install/templates/initializer.rb.erb +5 -0
- data/lib/mongory/converters/abstract_converter.rb +6 -5
- data/lib/mongory/converters/condition_converter.rb +2 -2
- data/lib/mongory/converters/key_converter.rb +1 -1
- data/lib/mongory/matchers/abstract_matcher.rb +1 -1
- data/lib/mongory/matchers/abstract_multi_matcher.rb +2 -2
- data/lib/mongory/matchers/array_record_matcher.rb +23 -11
- data/lib/mongory/matchers/elem_match_matcher.rb +2 -0
- data/lib/mongory/matchers/eq_matcher.rb +4 -2
- data/lib/mongory/matchers/field_matcher.rb +16 -3
- data/lib/mongory/matchers/hash_condition_matcher.rb +23 -6
- data/lib/mongory/matchers/in_matcher.rb +17 -1
- data/lib/mongory/matchers/ne_matcher.rb +7 -6
- data/lib/mongory/matchers/nin_matcher.rb +2 -6
- data/lib/mongory/matchers/regex_matcher.rb +29 -0
- data/lib/mongory/matchers.rb +7 -4
- data/lib/mongory/version.rb +1 -1
- data/lib/mongory.rb +57 -5
- metadata +8 -2
|
@@ -0,0 +1,75 @@
|
|
|
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_memory_pool *trace_pool;
|
|
32
|
+
mongory_table *string_map;
|
|
33
|
+
mongory_table *symbol_map;
|
|
34
|
+
mongory_array *mark_list;
|
|
35
|
+
VALUE ctx;
|
|
36
|
+
} rb_mongory_matcher_t;
|
|
37
|
+
|
|
38
|
+
struct rb_mongory_memory_pool_t {
|
|
39
|
+
mongory_memory_pool base;
|
|
40
|
+
rb_mongory_matcher_t *owner;
|
|
41
|
+
rb_mongory_memory_pool_t *next_scratch;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
typedef struct rb_mongory_table_t {
|
|
45
|
+
mongory_table base;
|
|
46
|
+
VALUE rb_hash;
|
|
47
|
+
rb_mongory_matcher_t *owner;
|
|
48
|
+
} rb_mongory_table_t;
|
|
49
|
+
|
|
50
|
+
typedef struct rb_mongory_array_t {
|
|
51
|
+
mongory_array base;
|
|
52
|
+
VALUE rb_array;
|
|
53
|
+
rb_mongory_matcher_t *owner;
|
|
54
|
+
} rb_mongory_array_t;
|
|
55
|
+
|
|
56
|
+
// Pool & error helpers (mongory_ext.c)
|
|
57
|
+
rb_mongory_memory_pool_t *rb_mongory_memory_pool_new(void);
|
|
58
|
+
bool rb_mongory_error_handling(mongory_memory_pool *pool, const char *error_message);
|
|
59
|
+
|
|
60
|
+
// CMatcher registration (rb_mongory_cmatcher.c)
|
|
61
|
+
void rb_mongory_init_cmatcher(void);
|
|
62
|
+
|
|
63
|
+
// Value conversion, lazy wrappers, and key caches (rb_mongory_value.c)
|
|
64
|
+
mongory_value *rb_to_mongory_value_deep(mongory_memory_pool *pool, VALUE rb_value);
|
|
65
|
+
mongory_value *rb_to_mongory_value_shallow(mongory_memory_pool *pool, VALUE rb_value);
|
|
66
|
+
mongory_value *rb_mongory_table_wrap(mongory_memory_pool *pool, VALUE rb_hash);
|
|
67
|
+
mongory_value *rb_mongory_array_wrap(mongory_memory_pool *pool, VALUE rb_array);
|
|
68
|
+
void *mongory_value_to_ruby(mongory_memory_pool *pool, mongory_value *value);
|
|
69
|
+
VALUE rb_mongory_cache_fetch_string(rb_mongory_matcher_t *owner, const char *key);
|
|
70
|
+
VALUE rb_mongory_cache_fetch_symbol(rb_mongory_matcher_t *owner, const char *key);
|
|
71
|
+
|
|
72
|
+
// Core regex & custom matcher adapters (rb_mongory_adapters.c)
|
|
73
|
+
void rb_mongory_register_adapters(void);
|
|
74
|
+
|
|
75
|
+
#endif // MONGORY_EXT_INTERNAL_H
|
|
@@ -0,0 +1,140 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file rb_mongory_cmatcher.c
|
|
3
|
+
* @brief Mongory::CMatcher 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
|
+
if (wrapper->trace_pool) {
|
|
35
|
+
wrapper->trace_pool->free(wrapper->trace_pool);
|
|
36
|
+
wrapper->trace_pool = NULL;
|
|
37
|
+
}
|
|
38
|
+
wrapper->matcher = NULL;
|
|
39
|
+
wrapper->condition = NULL;
|
|
40
|
+
wrapper->string_map = NULL;
|
|
41
|
+
wrapper->symbol_map = NULL;
|
|
42
|
+
wrapper->mark_list = NULL;
|
|
43
|
+
wrapper->ctx = 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static rb_mongory_matcher_t *get_initialized_matcher(VALUE self) {
|
|
47
|
+
rb_mongory_matcher_t *wrapper;
|
|
48
|
+
TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
|
|
49
|
+
if (!wrapper || !wrapper->matcher || !wrapper->condition || !wrapper->pool) {
|
|
50
|
+
rb_raise(rb_mongory_e_error, "uninitialized %s", rb_obj_classname(self));
|
|
51
|
+
}
|
|
52
|
+
return wrapper;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static void rb_mongory_matcher_free(void *ptr) {
|
|
56
|
+
rb_mongory_matcher_t *wrapper = (rb_mongory_matcher_t *)ptr;
|
|
57
|
+
if (!wrapper) return;
|
|
58
|
+
rb_mongory_matcher_release_pools(wrapper);
|
|
59
|
+
xfree(wrapper);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static bool gc_mark_array_cb(mongory_value *value, void *acc) {
|
|
63
|
+
(void)acc;
|
|
64
|
+
if (value && value->origin) rb_gc_mark((VALUE)value->origin);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static void rb_mongory_matcher_mark(void *ptr) {
|
|
69
|
+
rb_mongory_matcher_t *self = (rb_mongory_matcher_t *)ptr;
|
|
70
|
+
if (!self) return;
|
|
71
|
+
if (self->ctx) rb_gc_mark(self->ctx);
|
|
72
|
+
if (self->mark_list) {
|
|
73
|
+
self->mark_list->each(self->mark_list, NULL, gc_mark_array_cb);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static VALUE rb_mongory_matcher_alloc(VALUE klass) {
|
|
78
|
+
rb_mongory_matcher_t *wrapper;
|
|
79
|
+
VALUE obj = TypedData_Make_Struct(klass, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
|
|
80
|
+
memset(wrapper, 0, sizeof(rb_mongory_matcher_t));
|
|
81
|
+
return obj;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Mongory::CMatcher#initialize(condition, context: ...)
|
|
85
|
+
static VALUE rb_mongory_matcher_initialize(int argc, VALUE *argv, VALUE self) {
|
|
86
|
+
rb_mongory_matcher_t *wrapper;
|
|
87
|
+
TypedData_Get_Struct(self, rb_mongory_matcher_t, &rb_mongory_matcher_type, wrapper);
|
|
88
|
+
rb_mongory_matcher_release_pools(wrapper);
|
|
89
|
+
|
|
90
|
+
rb_mongory_memory_pool_t *matcher_pool = rb_mongory_memory_pool_new();
|
|
91
|
+
rb_mongory_memory_pool_t *scratch_pool = rb_mongory_memory_pool_new();
|
|
92
|
+
if (!matcher_pool || !scratch_pool) {
|
|
93
|
+
if (matcher_pool) matcher_pool->base.free(&matcher_pool->base);
|
|
94
|
+
if (scratch_pool) scratch_pool->base.free(&scratch_pool->base);
|
|
95
|
+
rb_raise(rb_mongory_e_error, "Failed to allocate matcher memory pools");
|
|
96
|
+
}
|
|
97
|
+
mongory_memory_pool *matcher_pool_base = &matcher_pool->base;
|
|
98
|
+
wrapper->pool = matcher_pool_base;
|
|
99
|
+
wrapper->scratch_free_list = scratch_pool;
|
|
100
|
+
wrapper->string_map = mongory_table_new(matcher_pool_base);
|
|
101
|
+
wrapper->symbol_map = mongory_table_new(matcher_pool_base);
|
|
102
|
+
wrapper->mark_list = mongory_array_new(matcher_pool_base);
|
|
103
|
+
matcher_pool->owner = wrapper;
|
|
104
|
+
scratch_pool->owner = wrapper;
|
|
105
|
+
rb_mongory_matcher_parse_argv(wrapper, argc, argv);
|
|
106
|
+
|
|
107
|
+
mongory_matcher *matcher = mongory_matcher_new(matcher_pool_base, wrapper->condition, wrapper->ctx);
|
|
108
|
+
if (matcher_pool_base->error || !matcher) {
|
|
109
|
+
char err_msg[256];
|
|
110
|
+
snprintf(err_msg, sizeof(err_msg), "%s",
|
|
111
|
+
(matcher_pool_base->error && matcher_pool_base->error->message)
|
|
112
|
+
? matcher_pool_base->error->message
|
|
113
|
+
: "Unknown error");
|
|
114
|
+
rb_mongory_matcher_release_pools(wrapper);
|
|
115
|
+
rb_raise(rb_mongory_e_type_error, "Failed to create matcher: %s", err_msg);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
wrapper->matcher = matcher;
|
|
119
|
+
return self;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Disallow dup/clone so C memory pools are never shared across instances
|
|
123
|
+
static VALUE rb_mongory_matcher_initialize_copy(VALUE self, VALUE orig) {
|
|
124
|
+
(void)orig;
|
|
125
|
+
rb_raise(rb_eTypeError, "can't copy %s", rb_obj_classname(self));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static void rb_mongory_matcher_parse_argv(rb_mongory_matcher_t *self, int argc, VALUE *argv) {
|
|
129
|
+
VALUE condition, kw_hash;
|
|
130
|
+
if (argc == 1) {
|
|
131
|
+
// In Ruby 3.0+, a single bare hash like `CMatcher.new(name: 'John')` is marked
|
|
132
|
+
// as keywords, which would fail `rb_scan_args(..., "1:", ...)` with 0 positional args.
|
|
133
|
+
rb_scan_args(argc, argv, "1", &condition);
|
|
134
|
+
kw_hash = Qnil;
|
|
135
|
+
} else {
|
|
136
|
+
rb_scan_args(argc, argv, "1:", &condition, &kw_hash);
|
|
137
|
+
}
|
|
138
|
+
const ID ctx_id[1] = { rb_intern("context") };
|
|
139
|
+
VALUE kw_vals[1] = { Qundef };
|
|
140
|
+
if (kw_hash != Qnil) {
|
|
141
|
+
rb_get_kwargs(kw_hash, ctx_id, 0, 1, kw_vals);
|
|
142
|
+
}
|
|
143
|
+
if (kw_vals[0] != Qundef) {
|
|
144
|
+
self->ctx = kw_vals[0];
|
|
145
|
+
} else {
|
|
146
|
+
self->ctx = rb_funcall(rb_mongory_c_matcher_context, rb_intern("new"), 0);
|
|
147
|
+
}
|
|
148
|
+
VALUE converted_condition = rb_funcall(rb_mongory_condition_converter, rb_intern("convert"), 1, condition);
|
|
149
|
+
self->condition = rb_to_mongory_value_deep(self->pool, converted_condition);
|
|
150
|
+
self->mark_list->push(self->mark_list, self->condition);
|
|
151
|
+
mongory_value *store_ctx = mongory_value_wrap_u(self->pool, (void *)self->ctx);
|
|
152
|
+
store_ctx->origin = (void *)self->ctx;
|
|
153
|
+
self->mark_list->push(self->mark_list, store_ctx);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
typedef struct {
|
|
157
|
+
rb_mongory_matcher_t *wrapper;
|
|
158
|
+
mongory_memory_pool *owner_pool;
|
|
159
|
+
rb_mongory_memory_pool_t *scratch;
|
|
160
|
+
} rb_mongory_scratch_lease_t;
|
|
161
|
+
|
|
162
|
+
typedef struct {
|
|
163
|
+
rb_mongory_matcher_t *wrapper;
|
|
164
|
+
mongory_memory_pool *scratch_pool;
|
|
165
|
+
VALUE data;
|
|
166
|
+
} rb_mongory_match_call_t;
|
|
167
|
+
|
|
168
|
+
typedef struct {
|
|
169
|
+
rb_mongory_matcher_t *wrapper;
|
|
170
|
+
mongory_memory_pool *scratch_pool;
|
|
171
|
+
} rb_mongory_explain_call_t;
|
|
172
|
+
|
|
173
|
+
// Pops an idle scratch pool from wrapper->scratch_free_list, or allocates a new
|
|
174
|
+
// reusable pool when re-entered concurrently; release resets and pushes it back.
|
|
175
|
+
static rb_mongory_scratch_lease_t rb_mongory_scratch_acquire(rb_mongory_matcher_t *wrapper) {
|
|
176
|
+
rb_mongory_memory_pool_t *scratch = wrapper->scratch_free_list;
|
|
177
|
+
if (scratch) {
|
|
178
|
+
wrapper->scratch_free_list = scratch->next_scratch;
|
|
179
|
+
scratch->next_scratch = NULL;
|
|
180
|
+
scratch->base.error = NULL;
|
|
181
|
+
} else {
|
|
182
|
+
scratch = rb_mongory_memory_pool_new();
|
|
183
|
+
if (!scratch) {
|
|
184
|
+
rb_raise(rb_mongory_e_error, "Failed to allocate scratch pool");
|
|
185
|
+
}
|
|
186
|
+
scratch->owner = wrapper;
|
|
187
|
+
}
|
|
188
|
+
rb_mongory_scratch_lease_t lease = {
|
|
189
|
+
.wrapper = wrapper,
|
|
190
|
+
.owner_pool = wrapper->pool,
|
|
191
|
+
.scratch = scratch,
|
|
192
|
+
};
|
|
193
|
+
return lease;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
static VALUE rb_mongory_scratch_release(VALUE arg) {
|
|
197
|
+
rb_mongory_scratch_lease_t *lease = (rb_mongory_scratch_lease_t *)arg;
|
|
198
|
+
rb_mongory_memory_pool_t *scratch = lease->scratch;
|
|
199
|
+
if (!scratch) {
|
|
200
|
+
return Qnil;
|
|
201
|
+
}
|
|
202
|
+
lease->scratch = NULL;
|
|
203
|
+
if (lease->wrapper && lease->wrapper->pool == lease->owner_pool) {
|
|
204
|
+
scratch->base.error = NULL;
|
|
205
|
+
scratch->base.reset(&scratch->base);
|
|
206
|
+
scratch->next_scratch = lease->wrapper->scratch_free_list;
|
|
207
|
+
lease->wrapper->scratch_free_list = scratch;
|
|
208
|
+
} else {
|
|
209
|
+
scratch->base.free(&scratch->base);
|
|
210
|
+
}
|
|
211
|
+
return Qnil;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
static VALUE rb_mongory_matcher_match_body(VALUE arg) {
|
|
215
|
+
rb_mongory_match_call_t *call = (rb_mongory_match_call_t *)arg;
|
|
216
|
+
rb_mongory_matcher_t *self_wrapper = call->wrapper;
|
|
217
|
+
mongory_matcher *matcher = self_wrapper->matcher;
|
|
218
|
+
mongory_memory_pool *scratch_pool = call->scratch_pool;
|
|
219
|
+
mongory_memory_pool *trace_pool = self_wrapper->trace_pool;
|
|
220
|
+
mongory_value *data_value = rb_to_mongory_value_shallow(scratch_pool, call->data);
|
|
221
|
+
|
|
222
|
+
if (rb_mongory_error_handling(scratch_pool, "Match failed")) {
|
|
223
|
+
return Qnil;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
bool result = mongory_matcher_match(matcher, data_value);
|
|
227
|
+
|
|
228
|
+
if (trace_pool) {
|
|
229
|
+
mongory_matcher_print_trace(matcher);
|
|
230
|
+
trace_pool->reset(trace_pool);
|
|
231
|
+
mongory_matcher_enable_trace(matcher, trace_pool);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return result ? Qtrue : Qfalse;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Mongory::CMatcher#match?(data)
|
|
238
|
+
static VALUE rb_mongory_matcher_match(VALUE self, VALUE data) {
|
|
239
|
+
rb_mongory_matcher_t *self_wrapper = get_initialized_matcher(self);
|
|
240
|
+
rb_mongory_scratch_lease_t lease = rb_mongory_scratch_acquire(self_wrapper);
|
|
241
|
+
rb_mongory_match_call_t call = {
|
|
242
|
+
.wrapper = self_wrapper,
|
|
243
|
+
.scratch_pool = &lease.scratch->base,
|
|
244
|
+
.data = data,
|
|
245
|
+
};
|
|
246
|
+
VALUE result = rb_ensure(rb_mongory_matcher_match_body, (VALUE)&call, rb_mongory_scratch_release, (VALUE)&lease);
|
|
247
|
+
RB_GC_GUARD(self);
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
static VALUE rb_mongory_matcher_explain_body(VALUE arg) {
|
|
252
|
+
rb_mongory_explain_call_t *call = (rb_mongory_explain_call_t *)arg;
|
|
253
|
+
mongory_matcher_explain(call->wrapper->matcher, call->scratch_pool);
|
|
254
|
+
rb_mongory_error_handling(call->scratch_pool, "Explain failed");
|
|
255
|
+
return Qnil;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Mongory::CMatcher#explain
|
|
259
|
+
static VALUE rb_mongory_matcher_explain(VALUE self) {
|
|
260
|
+
rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
|
|
261
|
+
rb_mongory_scratch_lease_t lease = rb_mongory_scratch_acquire(wrapper);
|
|
262
|
+
rb_mongory_explain_call_t call = {
|
|
263
|
+
.wrapper = wrapper,
|
|
264
|
+
.scratch_pool = &lease.scratch->base,
|
|
265
|
+
};
|
|
266
|
+
VALUE result = rb_ensure(rb_mongory_matcher_explain_body, (VALUE)&call, rb_mongory_scratch_release, (VALUE)&lease);
|
|
267
|
+
RB_GC_GUARD(self);
|
|
268
|
+
return result;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Mongory::CMatcher#trace(data)
|
|
272
|
+
static VALUE rb_mongory_matcher_trace(VALUE self, VALUE data) {
|
|
273
|
+
rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
|
|
274
|
+
rb_mongory_memory_pool_t *trace_pool = rb_mongory_memory_pool_new();
|
|
275
|
+
trace_pool->owner = wrapper;
|
|
276
|
+
mongory_memory_pool *trace_pool_base = &trace_pool->base;
|
|
277
|
+
mongory_value *data_value = rb_to_mongory_value_shallow(trace_pool_base, data);
|
|
278
|
+
|
|
279
|
+
if (trace_pool_base->error) {
|
|
280
|
+
char err_msg[256];
|
|
281
|
+
snprintf(err_msg, sizeof(err_msg), "%s",
|
|
282
|
+
trace_pool_base->error->message ? trace_pool_base->error->message : "Unknown error");
|
|
283
|
+
trace_pool_base->free(trace_pool_base);
|
|
284
|
+
rb_raise(rb_mongory_e_type_error, "Trace failed: %s", err_msg);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
bool matched = mongory_matcher_trace(wrapper->matcher, data_value);
|
|
288
|
+
trace_pool_base->free(trace_pool_base);
|
|
289
|
+
|
|
290
|
+
return matched ? Qtrue : Qfalse;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Mongory::CMatcher#enable_trace
|
|
294
|
+
static VALUE rb_mongory_matcher_enable_trace(VALUE self) {
|
|
295
|
+
rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
|
|
296
|
+
if (wrapper->trace_pool) {
|
|
297
|
+
wrapper->trace_pool->free(wrapper->trace_pool);
|
|
298
|
+
wrapper->trace_pool = NULL;
|
|
299
|
+
}
|
|
300
|
+
rb_mongory_memory_pool_t *trace_pool = rb_mongory_memory_pool_new();
|
|
301
|
+
trace_pool->owner = wrapper;
|
|
302
|
+
mongory_memory_pool *trace_pool_base = &trace_pool->base;
|
|
303
|
+
mongory_matcher_enable_trace(wrapper->matcher, trace_pool_base);
|
|
304
|
+
|
|
305
|
+
if (trace_pool_base->error) {
|
|
306
|
+
char err_msg[256];
|
|
307
|
+
snprintf(err_msg, sizeof(err_msg), "%s",
|
|
308
|
+
trace_pool_base->error->message ? trace_pool_base->error->message : "Unknown error");
|
|
309
|
+
trace_pool_base->free(trace_pool_base);
|
|
310
|
+
rb_raise(rb_mongory_e_type_error, "Enable trace failed: %s", err_msg);
|
|
311
|
+
}
|
|
312
|
+
wrapper->trace_pool = trace_pool_base;
|
|
313
|
+
|
|
314
|
+
return Qnil;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Mongory::CMatcher#disable_trace
|
|
318
|
+
static VALUE rb_mongory_matcher_disable_trace(VALUE self) {
|
|
319
|
+
rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
|
|
320
|
+
mongory_memory_pool *trace_pool = wrapper->trace_pool;
|
|
321
|
+
mongory_matcher_disable_trace(wrapper->matcher);
|
|
322
|
+
if (trace_pool) {
|
|
323
|
+
wrapper->trace_pool = NULL;
|
|
324
|
+
if (trace_pool->error) {
|
|
325
|
+
char err_msg[256];
|
|
326
|
+
snprintf(err_msg, sizeof(err_msg), "%s",
|
|
327
|
+
trace_pool->error->message ? trace_pool->error->message : "Unknown error");
|
|
328
|
+
trace_pool->free(trace_pool);
|
|
329
|
+
rb_raise(rb_mongory_e_type_error, "Disable trace failed: %s", err_msg);
|
|
330
|
+
}
|
|
331
|
+
trace_pool->free(trace_pool);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return Qnil;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Mongory::CMatcher#print_trace
|
|
338
|
+
static VALUE rb_mongory_matcher_print_trace(VALUE self) {
|
|
339
|
+
rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
|
|
340
|
+
if (!wrapper->trace_pool) {
|
|
341
|
+
return Qnil;
|
|
342
|
+
}
|
|
343
|
+
mongory_matcher_print_trace(wrapper->matcher);
|
|
344
|
+
rb_mongory_error_handling(wrapper->trace_pool, "Print trace failed");
|
|
345
|
+
|
|
346
|
+
return Qnil;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Mongory::CMatcher#condition
|
|
350
|
+
static VALUE rb_mongory_matcher_condition(VALUE self) {
|
|
351
|
+
rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
|
|
352
|
+
|
|
353
|
+
return (VALUE)wrapper->condition->origin;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Mongory::CMatcher#context
|
|
357
|
+
static VALUE rb_mongory_matcher_context(VALUE self) {
|
|
358
|
+
rb_mongory_matcher_t *wrapper = get_initialized_matcher(self);
|
|
359
|
+
|
|
360
|
+
return wrapper->ctx ? wrapper->ctx : Qnil;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Mongory::CMatcher.trace_result_colorful=(colorful)
|
|
364
|
+
static VALUE rb_mongory_matcher_trace_result_colorful(VALUE self, VALUE colorful) {
|
|
365
|
+
(void)self;
|
|
366
|
+
mongory_matcher_trace_result_colorful_set(RTEST(colorful));
|
|
367
|
+
|
|
368
|
+
return Qnil;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
void rb_mongory_init_cmatcher(void) {
|
|
372
|
+
rb_mongory_c_matcher = rb_define_class_under(rb_mongory_m_mongory, "CMatcher", rb_cObject);
|
|
373
|
+
rb_define_alloc_func(rb_mongory_c_matcher, rb_mongory_matcher_alloc);
|
|
374
|
+
|
|
375
|
+
rb_define_method(rb_mongory_c_matcher, "initialize", rb_mongory_matcher_initialize, -1);
|
|
376
|
+
rb_define_method(rb_mongory_c_matcher, "initialize_copy", rb_mongory_matcher_initialize_copy, 1);
|
|
377
|
+
rb_define_singleton_method(rb_mongory_c_matcher, "trace_result_colorful=", rb_mongory_matcher_trace_result_colorful, 1);
|
|
378
|
+
rb_define_method(rb_mongory_c_matcher, "match?", rb_mongory_matcher_match, 1);
|
|
379
|
+
rb_define_method(rb_mongory_c_matcher, "explain", rb_mongory_matcher_explain, 0);
|
|
380
|
+
rb_define_method(rb_mongory_c_matcher, "condition", rb_mongory_matcher_condition, 0);
|
|
381
|
+
rb_define_method(rb_mongory_c_matcher, "context", rb_mongory_matcher_context, 0);
|
|
382
|
+
rb_define_method(rb_mongory_c_matcher, "trace", rb_mongory_matcher_trace, 1);
|
|
383
|
+
rb_define_method(rb_mongory_c_matcher, "enable_trace", rb_mongory_matcher_enable_trace, 0);
|
|
384
|
+
rb_define_method(rb_mongory_c_matcher, "disable_trace", rb_mongory_matcher_disable_trace, 0);
|
|
385
|
+
rb_define_method(rb_mongory_c_matcher, "print_trace", rb_mongory_matcher_print_trace, 0);
|
|
386
|
+
}
|