mongory 0.9.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -0
- data/README.md +121 -422
- data/SUBMODULE_INTEGRATION.md +2 -2
- data/docs/clang_bridge.md +28 -59
- data/docs/migration.md +17 -0
- data/docs/performance.md +14 -51
- data/examples/benchmark.rb +10 -10
- data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +62 -0
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +31 -0
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +3 -0
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +20 -0
- data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +31 -4
- data/ext/mongory_ext/mongory-core/src/foundations/array.c +48 -6
- data/ext/mongory_ext/mongory-core/src/foundations/config.c +30 -2
- data/ext/mongory_ext/mongory-core/src/foundations/error.c +10 -0
- data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +78 -19
- data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +73 -19
- data/ext/mongory_ext/mongory-core/src/foundations/table.c +8 -3
- data/ext/mongory_ext/mongory-core/src/foundations/utils.c +44 -55
- data/ext/mongory_ext/mongory-core/src/foundations/utils.h +7 -0
- data/ext/mongory_ext/mongory-core/src/foundations/value.c +3 -0
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +69 -0
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +15 -0
- data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +9 -4
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +167 -31
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +7 -1
- data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.c → existence_matcher.c} +10 -12
- data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.h → existence_matcher.h} +4 -4
- data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +20 -6
- data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.c → implicit_array_matcher.c} +57 -38
- data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.h → implicit_array_matcher.h} +5 -5
- data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +8 -10
- data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +337 -96
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +59 -21
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +7 -1
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +6 -5
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -1
- data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.c → value_matcher.c} +120 -60
- data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.h → value_matcher.h} +13 -13
- data/ext/mongory_ext/mongory_ext.c +2 -2
- data/ext/mongory_ext/mongory_ext_internal.h +2 -2
- data/ext/mongory_ext/{rb_mongory_cmatcher.c → rb_mongory_matcher.c} +157 -23
- data/lib/generators/mongory/install/templates/initializer.rb.erb +0 -5
- data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
- data/lib/mongory/converters/converted.rb +28 -6
- data/lib/mongory/converters/key_converter.rb +2 -0
- data/lib/mongory/matcher.rb +63 -0
- data/lib/mongory/matchers/abstract_matcher.rb +18 -164
- data/lib/mongory/matchers.rb +68 -84
- data/lib/mongory/query_builder.rb +30 -41
- data/lib/mongory/utils/tree_renderer.rb +108 -0
- data/lib/mongory/utils.rb +2 -67
- data/lib/mongory/version.rb +1 -1
- data/lib/mongory.rb +9 -59
- data/script/bench.rb +54 -0
- metadata +13 -37
- data/lib/mongory/c_matcher.rb +0 -57
- data/lib/mongory/c_query_builder.rb +0 -44
- data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -124
- data/lib/mongory/matchers/and_matcher.rb +0 -72
- data/lib/mongory/matchers/array_record_matcher.rb +0 -105
- data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
- data/lib/mongory/matchers/eq_matcher.rb +0 -48
- data/lib/mongory/matchers/every_matcher.rb +0 -56
- data/lib/mongory/matchers/exists_matcher.rb +0 -53
- data/lib/mongory/matchers/field_matcher.rb +0 -160
- data/lib/mongory/matchers/gt_matcher.rb +0 -41
- data/lib/mongory/matchers/gte_matcher.rb +0 -41
- data/lib/mongory/matchers/hash_condition_matcher.rb +0 -79
- data/lib/mongory/matchers/in_matcher.rb +0 -84
- data/lib/mongory/matchers/literal_matcher.rb +0 -121
- data/lib/mongory/matchers/lt_matcher.rb +0 -41
- data/lib/mongory/matchers/lte_matcher.rb +0 -41
- data/lib/mongory/matchers/ne_matcher.rb +0 -39
- data/lib/mongory/matchers/nin_matcher.rb +0 -64
- data/lib/mongory/matchers/not_matcher.rb +0 -40
- data/lib/mongory/matchers/or_matcher.rb +0 -68
- data/lib/mongory/matchers/present_matcher.rb +0 -55
- data/lib/mongory/matchers/regex_matcher.rb +0 -109
- data/lib/mongory/matchers/size_matcher.rb +0 -54
- data/lib/mongory/query_matcher.rb +0 -93
- data/lib/mongory/rails.rb +0 -15
- data/lib/mongory/utils/debugger.rb +0 -125
- data/lib/mongory/utils/rails_patch.rb +0 -22
|
@@ -2,78 +2,41 @@
|
|
|
2
2
|
|
|
3
3
|
module Mongory
|
|
4
4
|
module Matchers
|
|
5
|
-
# AbstractMatcher is the base class for
|
|
5
|
+
# AbstractMatcher is the base class for custom Ruby matchers in Mongory.
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
# Each matcher is responsible for evaluating a specific type of condition against
|
|
11
|
-
# a record value. The base class provides infrastructure for:
|
|
12
|
-
# - Condition validation
|
|
13
|
-
# - Value conversion
|
|
14
|
-
# - Debug output
|
|
15
|
-
# - Proc caching
|
|
7
|
+
# Built-in operators are implemented natively in `mongory-core`. Subclass
|
|
8
|
+
# `AbstractMatcher` and register it with `Mongory::Matchers.register` to extend
|
|
9
|
+
# Mongory with custom operators evaluated via the `mongory-core` custom matcher bridge.
|
|
16
10
|
#
|
|
17
11
|
# @abstract Subclasses must implement {#match} to define their matching logic
|
|
18
12
|
#
|
|
19
|
-
# @example
|
|
20
|
-
# class
|
|
13
|
+
# @example Custom matcher implementation
|
|
14
|
+
# class DivisibleByMatcher < Mongory::Matchers::AbstractMatcher
|
|
21
15
|
# def match(record)
|
|
22
|
-
# record
|
|
16
|
+
# record.is_a?(Numeric) && (record % @condition).zero?
|
|
23
17
|
# end
|
|
24
|
-
# end
|
|
25
18
|
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
#
|
|
19
|
+
# def check_validity!
|
|
20
|
+
# raise Mongory::TypeError, '$divisibleBy needs a non-zero Integer' unless
|
|
21
|
+
# @condition.is_a?(Integer) && @condition != 0
|
|
22
|
+
# end
|
|
23
|
+
# end
|
|
30
24
|
#
|
|
31
|
-
#
|
|
25
|
+
# Mongory::Matchers.register(:divisible_by, '$divisibleBy', DivisibleByMatcher)
|
|
32
26
|
class AbstractMatcher
|
|
33
|
-
include Utils
|
|
34
|
-
|
|
35
27
|
singleton_class.alias_method :build, :new
|
|
36
28
|
|
|
37
|
-
# Sentinel value used to represent missing keys when traversing nested hashes.
|
|
38
|
-
# This is used instead of nil to distinguish between missing keys and nil values.
|
|
39
|
-
#
|
|
40
|
-
# @api private
|
|
41
|
-
KEY_NOT_FOUND = SingletonBuilder.new('KEY_NOT_FOUND').freeze
|
|
42
|
-
|
|
43
|
-
# Defines a lazily-evaluated matcher accessor with instance-level caching.
|
|
44
|
-
# This is used to create cached accessors for submatcher instances.
|
|
45
|
-
#
|
|
46
|
-
# @param name [Symbol] the name of the matcher (e.g., :collection)
|
|
47
|
-
# @yield the block that constructs the matcher instance
|
|
48
|
-
# @return [void]
|
|
49
|
-
# @example
|
|
50
|
-
# define_matcher(:array_matcher) do
|
|
51
|
-
# ArrayMatcher.build(@condition)
|
|
52
|
-
# end
|
|
53
|
-
def self.define_matcher(name, &block)
|
|
54
|
-
define_instance_cache_method(:"#{name}_matcher", &block)
|
|
55
|
-
end
|
|
56
|
-
|
|
57
29
|
# @return [Object] the raw condition this matcher was initialized with
|
|
58
30
|
attr_reader :condition
|
|
59
31
|
|
|
60
|
-
# @return [Context] the query context containing configuration and current record
|
|
32
|
+
# @return [Utils::Context] the query context containing configuration and current record
|
|
61
33
|
attr_reader :context
|
|
62
34
|
|
|
63
|
-
# Returns a unique key representing this matcher instance.
|
|
64
|
-
# Used for deduplication in multi-matchers.
|
|
65
|
-
#
|
|
66
|
-
# @return [String] a unique key for this matcher instance
|
|
67
|
-
# @see AbstractMultiMatcher#matchers
|
|
68
|
-
def uniq_key
|
|
69
|
-
"#{self.class}:condition:#{@condition.class}:#{@condition}"
|
|
70
|
-
end
|
|
71
|
-
|
|
72
35
|
# Initializes the matcher with a raw condition.
|
|
73
36
|
#
|
|
74
37
|
# @param condition [Object] the condition to match against
|
|
75
|
-
# @param context [Context] the query context containing configuration
|
|
76
|
-
def initialize(condition, context: Context.new)
|
|
38
|
+
# @param context [Utils::Context] the query context containing configuration
|
|
39
|
+
def initialize(condition, context: Utils::Context.new)
|
|
77
40
|
@condition = condition
|
|
78
41
|
@context = context
|
|
79
42
|
|
|
@@ -81,60 +44,15 @@ module Mongory
|
|
|
81
44
|
end
|
|
82
45
|
|
|
83
46
|
# Matches the given record against the condition.
|
|
84
|
-
# This method handles error cases and uses the cached proc for performance.
|
|
85
47
|
#
|
|
86
48
|
# @param record [Object] the input record
|
|
87
49
|
# @return [Boolean] whether the record matches the condition
|
|
88
50
|
def match?(record)
|
|
89
|
-
|
|
51
|
+
match(record)
|
|
90
52
|
rescue StandardError
|
|
91
53
|
false
|
|
92
54
|
end
|
|
93
55
|
|
|
94
|
-
# Converts the matcher into a Proc that can be used for matching.
|
|
95
|
-
# The Proc is cached for better performance.
|
|
96
|
-
#
|
|
97
|
-
# @return [Proc] a Proc that can be used to match records
|
|
98
|
-
def cached_proc
|
|
99
|
-
@cached_proc ||= raw_proc
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
alias_method :to_proc, :cached_proc
|
|
103
|
-
|
|
104
|
-
# Creates a debug-enabled version of the matcher proc.
|
|
105
|
-
# This version includes tracing and error handling.
|
|
106
|
-
#
|
|
107
|
-
# @return [Proc] a debug-enabled version of the matcher proc
|
|
108
|
-
def debug_proc
|
|
109
|
-
return @debug_proc if defined?(@debug_proc)
|
|
110
|
-
|
|
111
|
-
raw_proc = raw_proc()
|
|
112
|
-
@debug_proc = Proc.new do |record|
|
|
113
|
-
result = nil
|
|
114
|
-
|
|
115
|
-
Debugger.instance.with_indent do
|
|
116
|
-
result = begin
|
|
117
|
-
raw_proc.call(record)
|
|
118
|
-
rescue StandardError => e
|
|
119
|
-
e
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
debug_display(record, result)
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
result.is_a?(Exception) ? false : result
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
# Creates a raw Proc from the match method.
|
|
130
|
-
# This is used internally by to_proc and can be overridden by subclasses
|
|
131
|
-
# to provide custom matching behavior.
|
|
132
|
-
#
|
|
133
|
-
# @return [Proc] the raw Proc implementation of the match method
|
|
134
|
-
def raw_proc
|
|
135
|
-
method(:match).to_proc
|
|
136
|
-
end
|
|
137
|
-
|
|
138
56
|
# Performs the actual match logic.
|
|
139
57
|
# Subclasses must override this method.
|
|
140
58
|
#
|
|
@@ -147,73 +65,9 @@ module Mongory
|
|
|
147
65
|
# Override in subclasses to raise error if invalid.
|
|
148
66
|
#
|
|
149
67
|
# @abstract
|
|
150
|
-
# @raise [TypeError] if the condition is invalid
|
|
68
|
+
# @raise [Mongory::TypeError] if the condition is invalid
|
|
151
69
|
# @return [void]
|
|
152
70
|
def check_validity!; end
|
|
153
|
-
|
|
154
|
-
# Recursively prints the matcher structure into a formatted tree.
|
|
155
|
-
# Supports indentation and branching layout using prefix symbols.
|
|
156
|
-
#
|
|
157
|
-
# @param prefix [String] tree prefix (indentation + lines)
|
|
158
|
-
# @param is_last [Boolean] whether this node is the last among siblings
|
|
159
|
-
# @return [void]
|
|
160
|
-
def render_tree(prefix = '', is_last: true)
|
|
161
|
-
puts "#{prefix}#{is_last ? '└─ ' : '├─ '}#{tree_title}\n"
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
def priority
|
|
165
|
-
20
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
private
|
|
169
|
-
|
|
170
|
-
# Returns a single-line string representing this matcher in the tree output.
|
|
171
|
-
# Format: `<MatcherType>: <condition.inspect>`
|
|
172
|
-
#
|
|
173
|
-
# @return [String] a formatted title for tree display
|
|
174
|
-
def tree_title
|
|
175
|
-
matcher_name = self.class.name.split('::').last.sub('Matcher', '')
|
|
176
|
-
"#{matcher_name}: #{@condition.inspect}"
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
# Normalizes the record before matching.
|
|
180
|
-
#
|
|
181
|
-
# If the record is the KEY_NOT_FOUND sentinel (representing a missing field),
|
|
182
|
-
# it is converted to `nil` so matchers can interpret it consistently.
|
|
183
|
-
# Other values are returned as-is.
|
|
184
|
-
#
|
|
185
|
-
# @param record [Object] the record value to normalize
|
|
186
|
-
# @return [Object, nil] the normalized record
|
|
187
|
-
# @see Mongory::KEY_NOT_FOUND
|
|
188
|
-
def normalize(record)
|
|
189
|
-
record == KEY_NOT_FOUND ? nil : record
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
# Formats a debug string for match output.
|
|
193
|
-
# Uses ANSI escape codes to highlight matched vs. mismatched records.
|
|
194
|
-
#
|
|
195
|
-
# @param record [Object] the record being tested
|
|
196
|
-
# @param result [Boolean, Exception] whether the match succeeded or an error occurred
|
|
197
|
-
# @return [String] the formatted output string
|
|
198
|
-
def debug_display(record, result)
|
|
199
|
-
"#{self.class.name.split('::').last} #{colored_result(result)}, " \
|
|
200
|
-
"condition: #{@condition.inspect}, " \
|
|
201
|
-
"record: #{record.inspect}"
|
|
202
|
-
end
|
|
203
|
-
|
|
204
|
-
# Formats the match result with ANSI color codes for terminal output.
|
|
205
|
-
#
|
|
206
|
-
# @param result [Boolean, Exception] the match result or error
|
|
207
|
-
# @return [String] the colored result string
|
|
208
|
-
def colored_result(result)
|
|
209
|
-
if result.is_a?(Exception)
|
|
210
|
-
"\e[45;97m#{result}\e[0m"
|
|
211
|
-
elsif result
|
|
212
|
-
"\e[30;42mMatched\e[0m"
|
|
213
|
-
else
|
|
214
|
-
"\e[30;41mDismatch\e[0m"
|
|
215
|
-
end
|
|
216
|
-
end
|
|
217
71
|
end
|
|
218
72
|
end
|
|
219
73
|
end
|
data/lib/mongory/matchers.rb
CHANGED
|
@@ -1,30 +1,66 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Mongory
|
|
4
|
-
# Provides matcher registration and operator
|
|
4
|
+
# Provides custom matcher registration and Symbol operator snippet management.
|
|
5
5
|
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
# - Safely isolating symbol extension behind an explicit opt-in flag
|
|
6
|
+
# Built-in operators are evaluated natively by `mongory-core` and registered in
|
|
7
|
+
# `BUILTIN_OPERATORS` so `enable_symbol_snippets!` can expose Symbol methods
|
|
8
|
+
# (e.g., `:age.gt`, `:tags.in`).
|
|
10
9
|
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
# Symbol snippets are only enabled if `Matchers.enable_symbol_snippets!` is called,
|
|
15
|
-
# preventing namespace pollution unless explicitly requested.
|
|
10
|
+
# Custom Ruby matchers subclassing `Mongory::Matchers::AbstractMatcher` are registered
|
|
11
|
+
# via `Matchers.register(method_sym, operator, klass)` and bridged into `mongory-core`
|
|
12
|
+
# through `Matchers.lookup(operator)`.
|
|
16
13
|
module Matchers
|
|
14
|
+
# Built-in operators implemented natively in `mongory-core`.
|
|
15
|
+
BUILTIN_OPERATORS = {
|
|
16
|
+
eq: '$eq',
|
|
17
|
+
ne: '$ne',
|
|
18
|
+
gt: '$gt',
|
|
19
|
+
gte: '$gte',
|
|
20
|
+
lt: '$lt',
|
|
21
|
+
lte: '$lte',
|
|
22
|
+
in: '$in',
|
|
23
|
+
nin: '$nin',
|
|
24
|
+
exists: '$exists',
|
|
25
|
+
present: '$present',
|
|
26
|
+
regex: '$regex',
|
|
27
|
+
and: '$and',
|
|
28
|
+
or: '$or',
|
|
29
|
+
not: '$not',
|
|
30
|
+
elem_match: '$elemMatch',
|
|
31
|
+
every: '$every',
|
|
32
|
+
size: '$size'
|
|
33
|
+
}.freeze
|
|
34
|
+
|
|
35
|
+
# @private
|
|
36
|
+
#
|
|
37
|
+
# Internal helper representing a registration of an operator and its associated symbol snippet method.
|
|
38
|
+
# Used to delay method definition on Symbol until explicitly enabled.
|
|
39
|
+
Registry = Struct.new(:method_sym, :operator) do
|
|
40
|
+
# Defines a method on Symbol to support operator snippet expansion.
|
|
41
|
+
#
|
|
42
|
+
# @return [void]
|
|
43
|
+
def apply!
|
|
44
|
+
return if Symbol.method_defined?(method_sym)
|
|
45
|
+
|
|
46
|
+
operator = operator()
|
|
47
|
+
Symbol.define_method(method_sym) do
|
|
48
|
+
Mongory::QueryOperator.new(to_s, operator)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
17
53
|
@operator_mapping = {}
|
|
18
|
-
@registries =
|
|
54
|
+
@registries = BUILTIN_OPERATORS.map { |method_sym, operator| Registry.new(method_sym, operator).freeze }
|
|
19
55
|
@enable_symbol_snippets = false
|
|
20
56
|
|
|
21
|
-
# Registers a matcher class for a given operator and method symbol.
|
|
57
|
+
# Registers a custom matcher class for a given operator and method symbol.
|
|
22
58
|
#
|
|
23
|
-
# @param method_sym [Symbol] the method name to be added to Symbol (e.g., :
|
|
24
|
-
# @param operator [String] the Mongo-style operator (e.g., "$
|
|
59
|
+
# @param method_sym [Symbol] the method name to be added to Symbol (e.g., :class_in)
|
|
60
|
+
# @param operator [String] the Mongo-style operator (e.g., "$classIn")
|
|
25
61
|
# @param klass [Class] the matcher class to associate with the operator
|
|
26
62
|
# @return [void]
|
|
27
|
-
# @raise [
|
|
63
|
+
# @raise [Mongory::TypeError] if validations fail
|
|
28
64
|
def self.register(method_sym, operator, klass)
|
|
29
65
|
Validator.validate_method(method_sym)
|
|
30
66
|
Validator.validate_operator(operator, klass)
|
|
@@ -38,7 +74,7 @@ module Mongory
|
|
|
38
74
|
registry.apply!
|
|
39
75
|
end
|
|
40
76
|
|
|
41
|
-
# Enables dynamic symbol snippet generation for registered operators.
|
|
77
|
+
# Enables dynamic symbol snippet generation for built-in and registered operators.
|
|
42
78
|
# This defines methods like `:age.gt => QueryOperator.new(...)`.
|
|
43
79
|
#
|
|
44
80
|
# @return [void]
|
|
@@ -47,19 +83,28 @@ module Mongory
|
|
|
47
83
|
@registries.each(&:apply!)
|
|
48
84
|
end
|
|
49
85
|
|
|
50
|
-
# Retrieves the matcher class associated with a Mongo-style operator.
|
|
86
|
+
# Retrieves the custom matcher class associated with a Mongo-style operator.
|
|
87
|
+
# Built-in operators are handled natively by `mongory-core` and return `nil`.
|
|
51
88
|
#
|
|
52
89
|
# @param operator [String]
|
|
53
|
-
# @return [Class, nil] the registered matcher class or nil if not found
|
|
90
|
+
# @return [Class, nil] the registered custom matcher class or nil if not found
|
|
54
91
|
def self.lookup(operator)
|
|
55
92
|
@operator_mapping[operator]
|
|
56
93
|
end
|
|
57
94
|
|
|
58
|
-
# Returns
|
|
95
|
+
# Returns whether the operator is a built-in `mongory-core` operator.
|
|
96
|
+
#
|
|
97
|
+
# @param operator [String]
|
|
98
|
+
# @return [Boolean]
|
|
99
|
+
def self.builtin?(operator)
|
|
100
|
+
BUILTIN_OPERATORS.value?(operator)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Returns all built-in and custom registered operator keys.
|
|
59
104
|
#
|
|
60
105
|
# @return [Array<String>]
|
|
61
106
|
def self.operators
|
|
62
|
-
@operator_mapping.keys
|
|
107
|
+
(BUILTIN_OPERATORS.values + @operator_mapping.keys).uniq
|
|
63
108
|
end
|
|
64
109
|
|
|
65
110
|
def self.freeze
|
|
@@ -71,16 +116,7 @@ module Mongory
|
|
|
71
116
|
|
|
72
117
|
# @private
|
|
73
118
|
#
|
|
74
|
-
# Internal helper module used by `Matchers.
|
|
75
|
-
#
|
|
76
|
-
# This includes:
|
|
77
|
-
# - Ensuring operators are valid Mongo-style strings (e.g., "$gt")
|
|
78
|
-
# - Verifying matcher class inheritance
|
|
79
|
-
# - Enforcing naming rules for symbol snippets (e.g., :gt, :not_match)
|
|
80
|
-
#
|
|
81
|
-
# These validations protect against incorrect matcher setup and prevent unsafe symbol definitions.
|
|
82
|
-
#
|
|
83
|
-
# @see Matchers.registry
|
|
119
|
+
# Internal helper module used by `Matchers.register` to validate matcher registration parameters.
|
|
84
120
|
module Validator
|
|
85
121
|
# Validates the given operator string.
|
|
86
122
|
# Ensures it matches the Mongo-style format like "$gt".
|
|
@@ -91,9 +127,8 @@ module Mongory
|
|
|
91
127
|
# @return [void]
|
|
92
128
|
# @raise [Mongory::TypeError] if operator format is invalid
|
|
93
129
|
def self.validate_operator(operator, klass = nil)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
end
|
|
130
|
+
existing = Matchers.lookup(operator) || (Matchers.builtin?(operator) ? 'built-in' : nil)
|
|
131
|
+
warn "Duplicate operator registration: #{operator} (#{existing} vs #{klass})" if existing
|
|
97
132
|
|
|
98
133
|
return if operator.is_a?(String) && operator.match?(/^\$[a-z]+([A-Z][a-z]+)*$/)
|
|
99
134
|
|
|
@@ -122,58 +157,7 @@ module Mongory
|
|
|
122
157
|
raise Mongory::TypeError, "Method symbol must match /^([a-z]+_)*[a-z]+$/, but got #{method_sym.inspect}"
|
|
123
158
|
end
|
|
124
159
|
end
|
|
125
|
-
|
|
126
|
-
# @private
|
|
127
|
-
#
|
|
128
|
-
# Internal helper representing a registration of an operator and its associated symbol snippet method.
|
|
129
|
-
# Used to delay method definition on Symbol until explicitly enabled.
|
|
130
|
-
#
|
|
131
|
-
# Each instance holds:
|
|
132
|
-
# - the method symbol (e.g., `:gt`)
|
|
133
|
-
# - the corresponding Mongo-style operator (e.g., `"$gt"`)
|
|
134
|
-
#
|
|
135
|
-
# These instances are collected and replayed upon calling `Matchers.enable_symbol_snippets!`.
|
|
136
|
-
#
|
|
137
|
-
# @!attribute method_sym
|
|
138
|
-
# @return [Symbol] the symbol method name (e.g., :in, :gt, :exists)
|
|
139
|
-
# @!attribute operator
|
|
140
|
-
# @return [String] the Mongo-style operator this snippet maps to (e.g., "$in")
|
|
141
|
-
Registry = Struct.new(:method_sym, :operator) do
|
|
142
|
-
# Defines a method on Symbol to support operator snippet expansion.
|
|
143
|
-
#
|
|
144
|
-
# @return [void]
|
|
145
|
-
def apply!
|
|
146
|
-
return if Symbol.method_defined?(method_sym)
|
|
147
|
-
|
|
148
|
-
operator = operator()
|
|
149
|
-
Symbol.define_method(method_sym) do
|
|
150
|
-
Mongory::QueryOperator.new(to_s, operator)
|
|
151
|
-
end
|
|
152
|
-
end
|
|
153
|
-
end
|
|
154
160
|
end
|
|
155
161
|
end
|
|
156
162
|
|
|
157
163
|
require_relative 'matchers/abstract_matcher'
|
|
158
|
-
require_relative 'matchers/abstract_multi_matcher'
|
|
159
|
-
require_relative 'matchers/literal_matcher'
|
|
160
|
-
require_relative 'matchers/hash_condition_matcher'
|
|
161
|
-
require_relative 'matchers/and_matcher'
|
|
162
|
-
require_relative 'matchers/array_record_matcher'
|
|
163
|
-
require_relative 'matchers/elem_match_matcher'
|
|
164
|
-
require_relative 'matchers/every_matcher'
|
|
165
|
-
require_relative 'matchers/eq_matcher'
|
|
166
|
-
require_relative 'matchers/exists_matcher'
|
|
167
|
-
require_relative 'matchers/gt_matcher'
|
|
168
|
-
require_relative 'matchers/gte_matcher'
|
|
169
|
-
require_relative 'matchers/in_matcher'
|
|
170
|
-
require_relative 'matchers/field_matcher'
|
|
171
|
-
require_relative 'matchers/lt_matcher'
|
|
172
|
-
require_relative 'matchers/lte_matcher'
|
|
173
|
-
require_relative 'matchers/ne_matcher'
|
|
174
|
-
require_relative 'matchers/nin_matcher'
|
|
175
|
-
require_relative 'matchers/not_matcher'
|
|
176
|
-
require_relative 'matchers/or_matcher'
|
|
177
|
-
require_relative 'matchers/present_matcher'
|
|
178
|
-
require_relative 'matchers/regex_matcher'
|
|
179
|
-
require_relative 'matchers/size_matcher'
|
|
@@ -8,7 +8,7 @@ module Mongory
|
|
|
8
8
|
# It supports condition chaining (`where`, `or`, `not`),
|
|
9
9
|
# limiting, and plucking fields.
|
|
10
10
|
#
|
|
11
|
-
# Internally it compiles all conditions and invokes `
|
|
11
|
+
# Internally it compiles all conditions and invokes `Matcher` (backed by `mongory-core`).
|
|
12
12
|
#
|
|
13
13
|
# @example Basic query
|
|
14
14
|
# records.mongory
|
|
@@ -25,7 +25,6 @@ module Mongory
|
|
|
25
25
|
# .pluck(:name, :email)
|
|
26
26
|
class QueryBuilder
|
|
27
27
|
include ::Enumerable
|
|
28
|
-
include Utils
|
|
29
28
|
|
|
30
29
|
# Initializes a new query builder with the given record set.
|
|
31
30
|
#
|
|
@@ -37,7 +36,6 @@ module Mongory
|
|
|
37
36
|
end
|
|
38
37
|
|
|
39
38
|
# Iterates through all records that match the current matcher.
|
|
40
|
-
# Uses the standard matcher implementation.
|
|
41
39
|
#
|
|
42
40
|
# @yieldparam record [Object] each matching record
|
|
43
41
|
# @return [Enumerator] if no block given
|
|
@@ -45,51 +43,44 @@ module Mongory
|
|
|
45
43
|
def each
|
|
46
44
|
return to_enum(:each) unless block_given?
|
|
47
45
|
|
|
48
|
-
@matcher.prepare_query
|
|
49
46
|
@records.each do |record|
|
|
50
47
|
@context.current_record = record
|
|
51
48
|
yield record if @matcher.match?(record)
|
|
52
49
|
end
|
|
53
50
|
end
|
|
54
51
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
#
|
|
58
|
-
#
|
|
59
|
-
# Since C extension has implemented, the fast mode is no longer needed. Use C extension instead.
|
|
60
|
-
#
|
|
61
|
-
# This method is deprecated and will be removed in future versions.
|
|
52
|
+
alias_method :fast, :each
|
|
53
|
+
|
|
54
|
+
# Traces matcher evaluation for all records and returns an Array of structured trace tree Hashes.
|
|
55
|
+
# If a block is given, matched records are also yielded.
|
|
62
56
|
#
|
|
63
57
|
# @yieldparam record [Object] each matching record
|
|
64
|
-
# @return [
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
warn('Deprecated: Since C extension has implemented, the fast mode is no longer needed. Use C extension instead.')
|
|
68
|
-
return to_enum(:fast) unless block_given?
|
|
69
|
-
|
|
70
|
-
@context.need_convert = false
|
|
71
|
-
@matcher.prepare_query
|
|
72
|
-
matcher_block = @matcher.to_proc
|
|
58
|
+
# @return [Array<Hash>] structured trace tree per evaluated record
|
|
59
|
+
def trace_tree
|
|
60
|
+
trees = []
|
|
73
61
|
@records.each do |record|
|
|
74
62
|
@context.current_record = record
|
|
75
|
-
|
|
63
|
+
tree = @matcher.trace_tree(record)
|
|
64
|
+
trees << tree if tree
|
|
65
|
+
yield record if block_given? && tree && tree[:result] == :pass
|
|
76
66
|
end
|
|
67
|
+
trees
|
|
77
68
|
end
|
|
78
69
|
|
|
70
|
+
# Prints each record's trace tree to STDOUT and yields matching records.
|
|
71
|
+
#
|
|
72
|
+
# @yieldparam record [Object] each matching record
|
|
73
|
+
# @return [Enumerator] if no block given
|
|
74
|
+
# @return [void] if block given
|
|
79
75
|
def trace
|
|
80
76
|
return to_enum(:trace) unless block_given?
|
|
81
77
|
|
|
82
|
-
Mongory.debugger.enable
|
|
83
|
-
@matcher.prepare_query
|
|
84
78
|
@records.each do |record|
|
|
85
79
|
@context.current_record = record
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
yield record if
|
|
89
|
-
Mongory.debugger.clear
|
|
80
|
+
tree = @matcher.trace_tree(record)
|
|
81
|
+
Utils::TreeRenderer.print_trace(tree) if tree
|
|
82
|
+
yield record if tree && tree[:result] == :pass
|
|
90
83
|
end
|
|
91
|
-
ensure
|
|
92
|
-
Mongory.debugger.disable
|
|
93
84
|
end
|
|
94
85
|
|
|
95
86
|
# Adds a condition to filter records using the given condition.
|
|
@@ -211,23 +202,21 @@ module Mongory
|
|
|
211
202
|
end
|
|
212
203
|
end
|
|
213
204
|
|
|
205
|
+
# Returns the internal matcher tree structure as a structured Hash.
|
|
206
|
+
#
|
|
207
|
+
# @return [Hash] structured matcher tree
|
|
208
|
+
def explain_tree
|
|
209
|
+
@matcher.match?(@records.first)
|
|
210
|
+
@matcher.explain_tree
|
|
211
|
+
end
|
|
212
|
+
|
|
214
213
|
# Prints the internal matcher tree structure for the current query.
|
|
215
214
|
# Will output a human-readable visual tree of matchers.
|
|
216
215
|
# This is useful for debugging and visualizing complex conditions.
|
|
217
216
|
#
|
|
218
217
|
# @return [void]
|
|
219
218
|
def explain
|
|
220
|
-
|
|
221
|
-
@matcher.render_tree
|
|
222
|
-
nil
|
|
223
|
-
end
|
|
224
|
-
|
|
225
|
-
def c
|
|
226
|
-
return self unless defined?(Mongory::CMatcher)
|
|
227
|
-
|
|
228
|
-
c_builder = CQueryBuilder.new(@records, context: @context)
|
|
229
|
-
c_builder.send(:set_matcher, @matcher.condition)
|
|
230
|
-
c_builder
|
|
219
|
+
Utils::TreeRenderer.print_explain(explain_tree)
|
|
231
220
|
end
|
|
232
221
|
|
|
233
222
|
private
|
|
@@ -249,7 +238,7 @@ module Mongory
|
|
|
249
238
|
# @param condition [Hash] the condition to build the matcher from
|
|
250
239
|
# @return [void]
|
|
251
240
|
def set_matcher(condition = {})
|
|
252
|
-
@matcher =
|
|
241
|
+
@matcher = Matcher.new(condition, context: @context)
|
|
253
242
|
end
|
|
254
243
|
|
|
255
244
|
# @private
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mongory
|
|
4
|
+
module Utils
|
|
5
|
+
# Renders structured explain_tree and trace_tree Hash structures to STDOUT.
|
|
6
|
+
module TreeRenderer
|
|
7
|
+
RESULT_LABELS = {
|
|
8
|
+
pass: 'Matched',
|
|
9
|
+
fail: 'Dismatch',
|
|
10
|
+
skip: 'Skipped'
|
|
11
|
+
}.freeze
|
|
12
|
+
|
|
13
|
+
COLOR_RESULT_LABELS = {
|
|
14
|
+
pass: "\e[30;42mMatched\e[0m",
|
|
15
|
+
fail: "\e[30;41mDismatch\e[0m",
|
|
16
|
+
skip: "\e[30;43mSkipped\e[0m"
|
|
17
|
+
}.freeze
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
# Recursively marks an explain_tree node and all descendants with `result: :skip`.
|
|
22
|
+
#
|
|
23
|
+
# @param node [Hash] structured explain_tree node
|
|
24
|
+
# @return [Hash] structured trace_tree node with `result: :skip`
|
|
25
|
+
def mark_skipped(node)
|
|
26
|
+
skipped = node.merge(result: :skip)
|
|
27
|
+
skipped[:children] = (node[:children] || []).map { |child| mark_skipped(child) }
|
|
28
|
+
skipped
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Prints a structured explain_tree Hash to STDOUT.
|
|
32
|
+
#
|
|
33
|
+
# @param node [Hash] root explain_tree node
|
|
34
|
+
# @param prefix [String] indentation prefix
|
|
35
|
+
# @param is_last [Boolean] whether node is last sibling
|
|
36
|
+
# @param is_root [Boolean] whether node is root
|
|
37
|
+
# @return [void]
|
|
38
|
+
def print_explain(node, prefix = '', is_last: true, is_root: true)
|
|
39
|
+
return unless node
|
|
40
|
+
|
|
41
|
+
connector = explain_connector(is_root: is_root, is_last: is_last)
|
|
42
|
+
puts "#{prefix}#{connector}#{explain_title(node)}"
|
|
43
|
+
|
|
44
|
+
children = node[:children] || []
|
|
45
|
+
next_prefix = explain_next_prefix(prefix, is_root: is_root, is_last: is_last)
|
|
46
|
+
last_index = children.length - 1
|
|
47
|
+
children.each_with_index do |child, index|
|
|
48
|
+
print_explain(child, next_prefix, is_last: index == last_index, is_root: false)
|
|
49
|
+
end
|
|
50
|
+
nil
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def explain_connector(is_root:, is_last:)
|
|
54
|
+
return '' if is_root
|
|
55
|
+
|
|
56
|
+
is_last ? '└─ ' : '├─ '
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def explain_next_prefix(prefix, is_root:, is_last:)
|
|
60
|
+
return prefix if is_root
|
|
61
|
+
|
|
62
|
+
"#{prefix}#{is_last ? ' ' : '│ '}"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Prints a structured trace_tree Hash to STDOUT.
|
|
66
|
+
#
|
|
67
|
+
# @param node [Hash] root trace_tree node
|
|
68
|
+
# @param level [Integer] indentation depth
|
|
69
|
+
# @param colorful [Boolean] whether to colorize result labels
|
|
70
|
+
# @return [void]
|
|
71
|
+
def print_trace(node, level = 0, colorful: true)
|
|
72
|
+
return unless node
|
|
73
|
+
|
|
74
|
+
indent = ' ' * level
|
|
75
|
+
puts "#{indent}#{trace_line(node, colorful: colorful)}"
|
|
76
|
+
(node[:children] || []).each do |child|
|
|
77
|
+
print_trace(child, level + 1, colorful: colorful)
|
|
78
|
+
end
|
|
79
|
+
nil
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def explain_title(node)
|
|
83
|
+
if node.key?(:field)
|
|
84
|
+
"Field: #{node[:field].inspect}, to match: #{node[:condition].inspect}"
|
|
85
|
+
else
|
|
86
|
+
"#{node[:name]}: #{node[:condition].inspect}"
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def trace_line(node, colorful: true)
|
|
91
|
+
result = node[:result]
|
|
92
|
+
labels = colorful ? COLOR_RESULT_LABELS : RESULT_LABELS
|
|
93
|
+
res_label = labels.fetch(result, result.to_s)
|
|
94
|
+
parts = ["#{node[:name]}: #{res_label}"]
|
|
95
|
+
parts << "field: #{node[:field].inspect}" if node.key?(:field)
|
|
96
|
+
parts << "condition: #{node[:condition].inspect}"
|
|
97
|
+
parts << "record: #{format_record(node)}" unless result == :skip
|
|
98
|
+
parts.join(', ')
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def format_record(node)
|
|
102
|
+
return 'Nothing' unless node.key?(:record)
|
|
103
|
+
|
|
104
|
+
node[:record].inspect
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|