mongory 0.9.1 → 2.0.1
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 +25 -0
- data/README.md +115 -420
- data/SUBMODULE_INTEGRATION.md +7 -20
- data/docs/clang_bridge.md +28 -63
- data/docs/migration.md +17 -0
- data/docs/performance.md +14 -51
- data/examples/benchmark.rb +10 -26
- data/ext/mongory_ext/extconf.rb +1 -1
- 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 +68 -0
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +14 -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 +132 -29
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +1 -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 +18 -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 +4 -4
- 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} +104 -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 -3
- data/ext/mongory_ext/{rb_mongory_cmatcher.c → rb_mongory_matcher.c} +145 -91
- data/ext/mongory_ext/rb_mongory_value.c +67 -0
- 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/condition_converter.rb +1 -1
- data/lib/mongory/converters/converted.rb +28 -6
- data/lib/mongory/converters/key_converter.rb +2 -0
- data/lib/mongory/matcher.rb +50 -0
- data/lib/mongory/matchers/abstract_matcher.rb +18 -192
- data/lib/mongory/matchers.rb +68 -84
- data/lib/mongory/query_builder.rb +29 -42
- data/lib/mongory/utils/context.rb +10 -15
- data/lib/mongory/utils/tree_renderer.rb +98 -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
- data/scripts/build_with_core.sh +0 -1
- 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 -150
- data/lib/mongory/matchers/and_matcher.rb +0 -73
- 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 -80
- data/lib/mongory/matchers/in_matcher.rb +0 -84
- data/lib/mongory/matchers/literal_matcher.rb +0 -126
- 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 -69
- 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,81 +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
|
-
# @return [AbstractMatcher, nil] the enclosing matcher notified when priority changes
|
|
64
|
-
attr_accessor :parent
|
|
65
|
-
|
|
66
|
-
# Returns a unique key representing this matcher instance.
|
|
67
|
-
# Used for deduplication in multi-matchers.
|
|
68
|
-
#
|
|
69
|
-
# @return [String] a unique key for this matcher instance
|
|
70
|
-
# @see AbstractMultiMatcher#matchers
|
|
71
|
-
def uniq_key
|
|
72
|
-
"#{self.class}:condition:#{@condition.class}:#{@condition}"
|
|
73
|
-
end
|
|
74
|
-
|
|
75
35
|
# Initializes the matcher with a raw condition.
|
|
76
36
|
#
|
|
77
37
|
# @param condition [Object] the condition to match against
|
|
78
|
-
# @param context [Context] the query context containing configuration
|
|
79
|
-
def initialize(condition, context: Context.new)
|
|
38
|
+
# @param context [Utils::Context] the query context containing configuration
|
|
39
|
+
def initialize(condition, context: Utils::Context.new)
|
|
80
40
|
@condition = condition
|
|
81
41
|
@context = context
|
|
82
42
|
|
|
@@ -84,60 +44,15 @@ module Mongory
|
|
|
84
44
|
end
|
|
85
45
|
|
|
86
46
|
# Matches the given record against the condition.
|
|
87
|
-
# This method handles error cases and uses the cached proc for performance.
|
|
88
47
|
#
|
|
89
48
|
# @param record [Object] the input record
|
|
90
49
|
# @return [Boolean] whether the record matches the condition
|
|
91
50
|
def match?(record)
|
|
92
|
-
|
|
51
|
+
match(record)
|
|
93
52
|
rescue StandardError
|
|
94
53
|
false
|
|
95
54
|
end
|
|
96
55
|
|
|
97
|
-
# Converts the matcher into a Proc that can be used for matching.
|
|
98
|
-
# The Proc is cached for better performance.
|
|
99
|
-
#
|
|
100
|
-
# @return [Proc] a Proc that can be used to match records
|
|
101
|
-
def cached_proc
|
|
102
|
-
@cached_proc ||= raw_proc
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
alias_method :to_proc, :cached_proc
|
|
106
|
-
|
|
107
|
-
# Creates a debug-enabled version of the matcher proc.
|
|
108
|
-
# This version includes tracing and error handling.
|
|
109
|
-
#
|
|
110
|
-
# @return [Proc] a debug-enabled version of the matcher proc
|
|
111
|
-
def debug_proc
|
|
112
|
-
return @debug_proc if defined?(@debug_proc)
|
|
113
|
-
|
|
114
|
-
raw_proc = raw_proc()
|
|
115
|
-
@debug_proc = Proc.new do |record|
|
|
116
|
-
result = nil
|
|
117
|
-
|
|
118
|
-
Debugger.instance.with_indent do
|
|
119
|
-
result = begin
|
|
120
|
-
raw_proc.call(record)
|
|
121
|
-
rescue StandardError => e
|
|
122
|
-
e
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
debug_display(record, result)
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
result.is_a?(Exception) ? false : result
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
# Creates a raw Proc from the match method.
|
|
133
|
-
# This is used internally by to_proc and can be overridden by subclasses
|
|
134
|
-
# to provide custom matching behavior.
|
|
135
|
-
#
|
|
136
|
-
# @return [Proc] the raw Proc implementation of the match method
|
|
137
|
-
def raw_proc
|
|
138
|
-
method(:match).to_proc
|
|
139
|
-
end
|
|
140
|
-
|
|
141
56
|
# Performs the actual match logic.
|
|
142
57
|
# Subclasses must override this method.
|
|
143
58
|
#
|
|
@@ -150,98 +65,9 @@ module Mongory
|
|
|
150
65
|
# Override in subclasses to raise error if invalid.
|
|
151
66
|
#
|
|
152
67
|
# @abstract
|
|
153
|
-
# @raise [TypeError] if the condition is invalid
|
|
68
|
+
# @raise [Mongory::TypeError] if the condition is invalid
|
|
154
69
|
# @return [void]
|
|
155
70
|
def check_validity!; end
|
|
156
|
-
|
|
157
|
-
# Recursively prints the matcher structure into a formatted tree.
|
|
158
|
-
# Supports indentation and branching layout using prefix symbols.
|
|
159
|
-
#
|
|
160
|
-
# @param prefix [String] tree prefix (indentation + lines)
|
|
161
|
-
# @param is_last [Boolean] whether this node is the last among siblings
|
|
162
|
-
# @return [void]
|
|
163
|
-
def render_tree(prefix = '', is_last: true)
|
|
164
|
-
puts "#{prefix}#{is_last ? '└─ ' : '├─ '}#{tree_title}\n"
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
def priority
|
|
168
|
-
20
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
# Clears memoized match procs after a descendant changes priority or order.
|
|
172
|
-
#
|
|
173
|
-
# @return [void]
|
|
174
|
-
def invalidate_proc_cache!
|
|
175
|
-
@cached_proc = nil
|
|
176
|
-
remove_instance_variable(:@debug_proc) if instance_variable_defined?(:@debug_proc)
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
# Called by a child matcher after its priority increases dynamically.
|
|
180
|
-
#
|
|
181
|
-
# @return [void]
|
|
182
|
-
def on_child_priority_changed
|
|
183
|
-
notify_priority_changed
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
private
|
|
187
|
-
|
|
188
|
-
def notify_priority_changed
|
|
189
|
-
invalidate_proc_cache!
|
|
190
|
-
@parent&.on_child_priority_changed
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
def attach_child(child)
|
|
194
|
-
child.parent = self if child.respond_to?(:parent=)
|
|
195
|
-
child
|
|
196
|
-
end
|
|
197
|
-
|
|
198
|
-
# Returns a single-line string representing this matcher in the tree output.
|
|
199
|
-
# Format: `<MatcherType>: <condition.inspect>`
|
|
200
|
-
#
|
|
201
|
-
# @return [String] a formatted title for tree display
|
|
202
|
-
def tree_title
|
|
203
|
-
matcher_name = self.class.name.split('::').last.sub('Matcher', '')
|
|
204
|
-
"#{matcher_name}: #{@condition.inspect}"
|
|
205
|
-
end
|
|
206
|
-
|
|
207
|
-
# Normalizes the record before matching.
|
|
208
|
-
#
|
|
209
|
-
# If the record is the KEY_NOT_FOUND sentinel (representing a missing field),
|
|
210
|
-
# it is converted to `nil` so matchers can interpret it consistently.
|
|
211
|
-
# Other values are returned as-is.
|
|
212
|
-
#
|
|
213
|
-
# @param record [Object] the record value to normalize
|
|
214
|
-
# @return [Object, nil] the normalized record
|
|
215
|
-
# @see Mongory::KEY_NOT_FOUND
|
|
216
|
-
def normalize(record)
|
|
217
|
-
record == KEY_NOT_FOUND ? nil : record
|
|
218
|
-
end
|
|
219
|
-
|
|
220
|
-
# Formats a debug string for match output.
|
|
221
|
-
# Uses ANSI escape codes to highlight matched vs. mismatched records.
|
|
222
|
-
#
|
|
223
|
-
# @param record [Object] the record being tested
|
|
224
|
-
# @param result [Boolean, Exception] whether the match succeeded or an error occurred
|
|
225
|
-
# @return [String] the formatted output string
|
|
226
|
-
def debug_display(record, result)
|
|
227
|
-
"#{self.class.name.split('::').last} #{colored_result(result)}, " \
|
|
228
|
-
"condition: #{@condition.inspect}, " \
|
|
229
|
-
"record: #{record.inspect}"
|
|
230
|
-
end
|
|
231
|
-
|
|
232
|
-
# Formats the match result with ANSI color codes for terminal output.
|
|
233
|
-
#
|
|
234
|
-
# @param result [Boolean, Exception] the match result or error
|
|
235
|
-
# @return [String] the colored result string
|
|
236
|
-
def colored_result(result)
|
|
237
|
-
if result.is_a?(Exception)
|
|
238
|
-
"\e[45;97m#{result}\e[0m"
|
|
239
|
-
elsif result
|
|
240
|
-
"\e[30;42mMatched\e[0m"
|
|
241
|
-
else
|
|
242
|
-
"\e[30;41mDismatch\e[0m"
|
|
243
|
-
end
|
|
244
|
-
end
|
|
245
71
|
end
|
|
246
72
|
end
|
|
247
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,42 @@ 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
|
-
# @deprecated
|
|
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
|
+
# Traces matcher evaluation for all records and returns an Array of structured trace tree Hashes.
|
|
53
|
+
# If a block is given, matched records are also yielded.
|
|
62
54
|
#
|
|
63
55
|
# @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
|
|
56
|
+
# @return [Array<Hash>] structured trace tree per evaluated record
|
|
57
|
+
def trace_tree
|
|
58
|
+
trees = []
|
|
73
59
|
@records.each do |record|
|
|
74
60
|
@context.current_record = record
|
|
75
|
-
|
|
61
|
+
tree = @matcher.trace_tree(record)
|
|
62
|
+
trees << tree if tree
|
|
63
|
+
yield record if block_given? && tree && tree[:result] == :pass
|
|
76
64
|
end
|
|
65
|
+
trees
|
|
77
66
|
end
|
|
78
67
|
|
|
68
|
+
# Prints each record's trace tree to STDOUT and yields matching records.
|
|
69
|
+
#
|
|
70
|
+
# @yieldparam record [Object] each matching record
|
|
71
|
+
# @return [Enumerator] if no block given
|
|
72
|
+
# @return [void] if block given
|
|
79
73
|
def trace
|
|
80
74
|
return to_enum(:trace) unless block_given?
|
|
81
75
|
|
|
82
|
-
Mongory.debugger.enable
|
|
83
|
-
@matcher.prepare_query
|
|
84
76
|
@records.each do |record|
|
|
85
77
|
@context.current_record = record
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
yield record if
|
|
89
|
-
Mongory.debugger.clear
|
|
78
|
+
tree = @matcher.trace_tree(record)
|
|
79
|
+
Utils::TreeRenderer.print_trace(tree) if tree
|
|
80
|
+
yield record if tree && tree[:result] == :pass
|
|
90
81
|
end
|
|
91
|
-
ensure
|
|
92
|
-
Mongory.debugger.disable
|
|
93
82
|
end
|
|
94
83
|
|
|
95
84
|
# Adds a condition to filter records using the given condition.
|
|
@@ -202,7 +191,7 @@ module Mongory
|
|
|
202
191
|
# @return [QueryBuilder] A new query builder instance with merged context
|
|
203
192
|
# @note Creates a new query builder with the current matcher's condition and merged context
|
|
204
193
|
# @example
|
|
205
|
-
# query.with_context(
|
|
194
|
+
# query.with_context(tenant: current_tenant) #=> Returns a new query builder with merged context
|
|
206
195
|
def with_context(addon_context = {})
|
|
207
196
|
dup_instance_exec do
|
|
208
197
|
@context = @context.dup
|
|
@@ -211,23 +200,21 @@ module Mongory
|
|
|
211
200
|
end
|
|
212
201
|
end
|
|
213
202
|
|
|
203
|
+
# Returns the internal matcher tree structure as a structured Hash.
|
|
204
|
+
#
|
|
205
|
+
# @return [Hash] structured matcher tree
|
|
206
|
+
def explain_tree
|
|
207
|
+
@matcher.match?(@records.first)
|
|
208
|
+
@matcher.explain_tree
|
|
209
|
+
end
|
|
210
|
+
|
|
214
211
|
# Prints the internal matcher tree structure for the current query.
|
|
215
212
|
# Will output a human-readable visual tree of matchers.
|
|
216
213
|
# This is useful for debugging and visualizing complex conditions.
|
|
217
214
|
#
|
|
218
215
|
# @return [void]
|
|
219
216
|
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
|
|
217
|
+
Utils::TreeRenderer.print_explain(explain_tree)
|
|
231
218
|
end
|
|
232
219
|
|
|
233
220
|
private
|
|
@@ -249,7 +236,7 @@ module Mongory
|
|
|
249
236
|
# @param condition [Hash] the condition to build the matcher from
|
|
250
237
|
# @return [void]
|
|
251
238
|
def set_matcher(condition = {})
|
|
252
|
-
@matcher =
|
|
239
|
+
@matcher = Matcher.new(condition, context: @context)
|
|
253
240
|
end
|
|
254
241
|
|
|
255
242
|
# @private
|
|
@@ -2,35 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
module Mongory
|
|
4
4
|
module Utils
|
|
5
|
-
# Context is a utility class that provides a stable
|
|
6
|
-
#
|
|
7
|
-
# and the current record being
|
|
5
|
+
# Context is a utility class that provides a stable, mutable shared context
|
|
6
|
+
# for the Mongory query builder and custom matchers. It holds user-defined
|
|
7
|
+
# configuration and the current record being processed.
|
|
8
8
|
#
|
|
9
9
|
# @example
|
|
10
|
-
# context = Mongory::Utils::Context.new(
|
|
10
|
+
# context = Mongory::Utils::Context.new(tenant: current_tenant)
|
|
11
11
|
# context.current_record = record
|
|
12
|
-
# context.config = new_config
|
|
13
12
|
#
|
|
14
|
-
# @attr [
|
|
15
|
-
# @attr [
|
|
16
|
-
# @attr [Boolean] need_convert Whether the record needs to be converted before matching
|
|
13
|
+
# @attr [Hash] config The configuration hash for the context
|
|
14
|
+
# @attr [Object] current_record The current record being processed
|
|
17
15
|
class Context
|
|
18
|
-
attr_accessor :config, :current_record
|
|
16
|
+
attr_accessor :config, :current_record
|
|
19
17
|
|
|
20
18
|
# Initializes a new Context instance with the given configuration.
|
|
21
19
|
#
|
|
22
|
-
# @param config [
|
|
20
|
+
# @param config [Hash] The configuration hash for the context.
|
|
23
21
|
# @return [Context] A new Context instance.
|
|
24
22
|
def initialize(config = {})
|
|
25
23
|
@config = config
|
|
26
24
|
@current_record = nil
|
|
27
|
-
@need_convert = true
|
|
28
25
|
end
|
|
29
26
|
|
|
30
|
-
# Creates a duplicate of the context with its own configuration.
|
|
27
|
+
# Creates a duplicate of the context with its own configuration hash.
|
|
31
28
|
#
|
|
32
29
|
# @return [Context] A new context instance with duplicated configuration
|
|
33
|
-
# @note The new context shares the same configuration object but has its own state
|
|
34
30
|
def dup
|
|
35
31
|
new_context = super
|
|
36
32
|
new_context.config = @config.dup
|
|
@@ -39,8 +35,7 @@ module Mongory
|
|
|
39
35
|
|
|
40
36
|
def to_hash
|
|
41
37
|
{
|
|
42
|
-
config: @config
|
|
43
|
-
need_convert: @need_convert
|
|
38
|
+
config: @config
|
|
44
39
|
}
|
|
45
40
|
end
|
|
46
41
|
end
|