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
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mongory
|
|
4
|
-
module Matchers
|
|
5
|
-
# FieldMatcher handles field-level matching by extracting and comparing field values.
|
|
6
|
-
#
|
|
7
|
-
# This matcher is responsible for:
|
|
8
|
-
# 1. Extracting field values from records using dot notation
|
|
9
|
-
# 2. Converting extracted values if needed
|
|
10
|
-
# 3. Delegating the actual comparison to a submatcher
|
|
11
|
-
#
|
|
12
|
-
# It supports:
|
|
13
|
-
# - Hash records with string/symbol keys
|
|
14
|
-
# - Array records with numeric indices
|
|
15
|
-
# - Objects that respond to `[]`
|
|
16
|
-
#
|
|
17
|
-
# @example Basic field matching
|
|
18
|
-
# matcher = FieldMatcher.build('age', 30)
|
|
19
|
-
# matcher.match?({ 'age' => 30 }) #=> true
|
|
20
|
-
# matcher.match?({ age: 30 }) #=> true
|
|
21
|
-
#
|
|
22
|
-
# @see LiteralMatcher
|
|
23
|
-
class FieldMatcher < LiteralMatcher
|
|
24
|
-
# A list of classes that should never be used for value digging.
|
|
25
|
-
# These typically respond to `#[]` but are semantically invalid for this context.
|
|
26
|
-
CLASSES_NOT_ALLOW_TO_DIG = [
|
|
27
|
-
::String,
|
|
28
|
-
::Integer,
|
|
29
|
-
::Proc,
|
|
30
|
-
::Method,
|
|
31
|
-
::MatchData,
|
|
32
|
-
::Thread,
|
|
33
|
-
::Symbol
|
|
34
|
-
].freeze
|
|
35
|
-
|
|
36
|
-
INTEGER_INDEX_REGEX = /\A(?:0|-?[1-9]\d*)\z/.freeze
|
|
37
|
-
|
|
38
|
-
# Initializes a new field matcher.
|
|
39
|
-
#
|
|
40
|
-
# @param field [String, Symbol] the field to match against
|
|
41
|
-
# @param condition [Object] the condition to match with
|
|
42
|
-
# @param context [Context] the query context
|
|
43
|
-
def initialize(field, condition, context: Context.new)
|
|
44
|
-
@field = field
|
|
45
|
-
super(condition, context: context)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
# Creates a raw Proc that performs the field matching operation.
|
|
49
|
-
# The Proc extracts the field value and delegates to the submatcher.
|
|
50
|
-
#
|
|
51
|
-
# This method first ensures the record is structurally eligible for field extraction—
|
|
52
|
-
# it must be a Hash, Array, or respond to `[]`. If the structure does not allow for
|
|
53
|
-
# field access (e.g., nil, primitive values, or unsupported types), the match returns false.
|
|
54
|
-
#
|
|
55
|
-
# The field value is then extracted using the following rules:
|
|
56
|
-
# - If the record is a Hash, it attempts to fetch using the field key,
|
|
57
|
-
# falling back to symbolized key if needed.
|
|
58
|
-
# - If the record is an Array, it fetches by index.
|
|
59
|
-
# - If the record does not support `[]` or is disallowed for dig operations,
|
|
60
|
-
# the match returns false immediately.
|
|
61
|
-
#
|
|
62
|
-
# Once the value is extracted, it is passed through the data converter
|
|
63
|
-
# and matched against the condition via the superclass.
|
|
64
|
-
#
|
|
65
|
-
# @example Matching a Hash with a nil field value
|
|
66
|
-
# matcher = Mongory::QueryMatcher.new(a: nil)
|
|
67
|
-
# matcher.match?({ a: nil }) # => true
|
|
68
|
-
#
|
|
69
|
-
# @example Record is nil (structure not diggable)
|
|
70
|
-
# matcher = Mongory::QueryMatcher.new(a: nil)
|
|
71
|
-
# matcher.match?(nil) # => false
|
|
72
|
-
#
|
|
73
|
-
# @example Matching against an Array by index
|
|
74
|
-
# matcher = Mongory::QueryMatcher.new(0 => /abc/)
|
|
75
|
-
# matcher.match?(['abcdef']) # => true
|
|
76
|
-
#
|
|
77
|
-
# @example Hash with symbol key, matcher uses string key
|
|
78
|
-
# matcher = Mongory::QueryMatcher.new('a' => 123)
|
|
79
|
-
# matcher.match?({ a: 123 }) # => true
|
|
80
|
-
|
|
81
|
-
# Creates a raw Proc that performs the field-based matching operation.
|
|
82
|
-
# The Proc extracts the field value and delegates matching to the superclass.
|
|
83
|
-
#
|
|
84
|
-
# @return [Proc] A proc that performs field-based matching with context awareness
|
|
85
|
-
# @note The proc handles field extraction and delegates matching to the superclass
|
|
86
|
-
def raw_proc
|
|
87
|
-
super_proc = super
|
|
88
|
-
field = @field
|
|
89
|
-
str_field, sym_field, int_field = parse_field_keys(field)
|
|
90
|
-
need_convert = @context.need_convert
|
|
91
|
-
data_converter = Mongory.data_converter
|
|
92
|
-
|
|
93
|
-
Proc.new do |record|
|
|
94
|
-
sub_record =
|
|
95
|
-
case record
|
|
96
|
-
when Hash
|
|
97
|
-
record.fetch(str_field) do
|
|
98
|
-
record.fetch(sym_field, KEY_NOT_FOUND)
|
|
99
|
-
end
|
|
100
|
-
when Array
|
|
101
|
-
int_field ? record.fetch(int_field, KEY_NOT_FOUND) : KEY_NOT_FOUND
|
|
102
|
-
when KEY_NOT_FOUND, *CLASSES_NOT_ALLOW_TO_DIG
|
|
103
|
-
next false
|
|
104
|
-
else
|
|
105
|
-
next false unless record.respond_to?(:[])
|
|
106
|
-
|
|
107
|
-
record[field]
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
sub_record = data_converter.convert(sub_record) if need_convert
|
|
111
|
-
super_proc.call(sub_record)
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
def priority
|
|
116
|
-
1 + super
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
# Returns a unique key for this matcher, including the field name.
|
|
120
|
-
# Used for deduplication in multi-matchers.
|
|
121
|
-
#
|
|
122
|
-
# @return [String] a unique key for this matcher
|
|
123
|
-
# @see AbstractMultiMatcher#matchers
|
|
124
|
-
def uniq_key
|
|
125
|
-
super + "field:#{@field}"
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
private
|
|
129
|
-
|
|
130
|
-
def parse_field_keys(field)
|
|
131
|
-
str_field = field.to_s
|
|
132
|
-
int_field = if field.is_a?(Integer)
|
|
133
|
-
field
|
|
134
|
-
elsif field.is_a?(String) && INTEGER_INDEX_REGEX.match?(field)
|
|
135
|
-
field.to_i
|
|
136
|
-
end
|
|
137
|
-
[str_field, str_field.to_sym, int_field]
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
# Returns a single-line summary of the field matcher including the field and condition.
|
|
141
|
-
#
|
|
142
|
-
# @return [String] a formatted title for tree display
|
|
143
|
-
def tree_title
|
|
144
|
-
"Field: #{@field.inspect} to match: #{@condition.inspect}"
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
# Custom display logic for debugging, including colored field highlighting.
|
|
148
|
-
#
|
|
149
|
-
# @param record [Object] the input record
|
|
150
|
-
# @param result [Boolean] match result
|
|
151
|
-
# @return [String] formatted debug string with highlighted field
|
|
152
|
-
def debug_display(record, result)
|
|
153
|
-
"#{self.class.name.split('::').last} #{colored_result(result)}, " \
|
|
154
|
-
"condition: #{@condition.inspect}, " \
|
|
155
|
-
"\e[30;47mfield: #{@field.inspect}\e[0m, " \
|
|
156
|
-
"record: #{record.inspect.gsub(@field.inspect, "\e[30;47m#{@field.inspect}\e[0m")}"
|
|
157
|
-
end
|
|
158
|
-
end
|
|
159
|
-
end
|
|
160
|
-
end
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mongory
|
|
4
|
-
module Matchers
|
|
5
|
-
# GtMatcher implements the `$gt` (greater than) operator.
|
|
6
|
-
#
|
|
7
|
-
# It returns true if the record is strictly greater than the condition.
|
|
8
|
-
#
|
|
9
|
-
# Inherits core logic from AbstractMatcher, including
|
|
10
|
-
# error handling and optional preprocessing.
|
|
11
|
-
#
|
|
12
|
-
# @example
|
|
13
|
-
# matcher = GtMatcher.build(10)
|
|
14
|
-
# matcher.match?(15) #=> true
|
|
15
|
-
# matcher.match?(10) #=> false
|
|
16
|
-
#
|
|
17
|
-
# @see AbstractMatcher
|
|
18
|
-
class GtMatcher < AbstractMatcher
|
|
19
|
-
# Creates a raw Proc that performs the greater-than comparison.
|
|
20
|
-
# The Proc uses the `>` operator to compare values.
|
|
21
|
-
#
|
|
22
|
-
# @return [Proc] A proc that performs greater-than comparison with error handling
|
|
23
|
-
# @note The proc includes error handling for invalid comparisons
|
|
24
|
-
def raw_proc
|
|
25
|
-
condition = @condition
|
|
26
|
-
|
|
27
|
-
Proc.new do |record|
|
|
28
|
-
record > condition
|
|
29
|
-
rescue StandardError
|
|
30
|
-
false
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def priority
|
|
35
|
-
3
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
register(:gt, '$gt', GtMatcher)
|
|
40
|
-
end
|
|
41
|
-
end
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mongory
|
|
4
|
-
module Matchers
|
|
5
|
-
# GteMatcher implements the `$gte` (greater than or equal) operator.
|
|
6
|
-
#
|
|
7
|
-
# It returns true if the record is greater than or equal to the condition value.
|
|
8
|
-
#
|
|
9
|
-
# Inherits comparison logic and error safety from AbstractMatcher.
|
|
10
|
-
#
|
|
11
|
-
# @example
|
|
12
|
-
# matcher = GteMatcher.build(10)
|
|
13
|
-
# matcher.match?(10) #=> true
|
|
14
|
-
# matcher.match?(11) #=> true
|
|
15
|
-
# matcher.match?(9) #=> false
|
|
16
|
-
#
|
|
17
|
-
# @see AbstractMatcher
|
|
18
|
-
class GteMatcher < AbstractMatcher
|
|
19
|
-
# Creates a raw Proc that performs the greater-than-or-equal comparison.
|
|
20
|
-
# The Proc uses the `>=` operator to compare values.
|
|
21
|
-
#
|
|
22
|
-
# @return [Proc] A proc that performs greater-than-or-equal comparison with error handling
|
|
23
|
-
# @note The proc includes error handling for invalid comparisons
|
|
24
|
-
def raw_proc
|
|
25
|
-
condition = @condition
|
|
26
|
-
|
|
27
|
-
Proc.new do |record|
|
|
28
|
-
record >= condition
|
|
29
|
-
rescue StandardError
|
|
30
|
-
false
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def priority
|
|
35
|
-
3
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
register(:gte, '$gte', GteMatcher)
|
|
40
|
-
end
|
|
41
|
-
end
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mongory
|
|
4
|
-
module Matchers
|
|
5
|
-
# HashConditionMatcher is responsible for handling field-level query conditions.
|
|
6
|
-
#
|
|
7
|
-
# It receives a Hash of key-value pairs and delegates each one to an appropriate matcher
|
|
8
|
-
# based on whether the key is a recognized operator or a data field path.
|
|
9
|
-
#
|
|
10
|
-
# Each subcondition is matched independently using the `:all?` strategy, meaning
|
|
11
|
-
# all subconditions must match for the entire HashConditionMatcher to succeed.
|
|
12
|
-
# For empty conditions, it returns true (using TRUE_PROC).
|
|
13
|
-
#
|
|
14
|
-
# This matcher plays a central role in dispatching symbolic query conditions
|
|
15
|
-
# to the appropriate field or operator matcher.
|
|
16
|
-
#
|
|
17
|
-
# @example Basic field matching
|
|
18
|
-
# matcher = HashConditionMatcher.build({ age: { :$gt => 30 }, active: true })
|
|
19
|
-
# matcher.match?(record) #=> true only if all subconditions match
|
|
20
|
-
#
|
|
21
|
-
# @example Empty conditions
|
|
22
|
-
# matcher = HashConditionMatcher.build({})
|
|
23
|
-
# matcher.match?(record) #=> true (uses TRUE_PROC)
|
|
24
|
-
#
|
|
25
|
-
# @see AbstractMultiMatcher
|
|
26
|
-
class HashConditionMatcher < AbstractMultiMatcher
|
|
27
|
-
enable_unwrap!
|
|
28
|
-
|
|
29
|
-
# Creates a raw Proc that performs the hash condition matching operation.
|
|
30
|
-
# The Proc combines all submatcher Procs and returns true only if all match.
|
|
31
|
-
# For empty conditions, returns TRUE_PROC.
|
|
32
|
-
#
|
|
33
|
-
# @return [Proc] a Proc that performs the hash condition matching operation
|
|
34
|
-
def raw_proc
|
|
35
|
-
combine_procs_with_and(*matchers.map(&:to_proc))
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# Returns the list of matchers for each key-value pair in the condition.
|
|
39
|
-
#
|
|
40
|
-
# For each pair:
|
|
41
|
-
# - `$regex` with a sibling `$options` builds a RegexMatcher with those flags
|
|
42
|
-
# - If the key is a registered operator, uses the corresponding matcher
|
|
43
|
-
# - An unknown `$` key raises, as MongoDB rejects unknown operators
|
|
44
|
-
# - Otherwise, wraps the value in a FieldMatcher for field path matching
|
|
45
|
-
#
|
|
46
|
-
# @return [Array<AbstractMatcher>] List of matchers for each condition
|
|
47
|
-
# @raise [TypeError] for an unknown operator or a `$options` without `$regex`
|
|
48
|
-
define_instance_cache_method(:matchers) do
|
|
49
|
-
children = @condition.map do |key, value|
|
|
50
|
-
next if key == '$options' && @condition.key?('$regex')
|
|
51
|
-
|
|
52
|
-
build_matcher(key, value)
|
|
53
|
-
end.compact
|
|
54
|
-
sort_and_adopt(children)
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def check_validity!
|
|
58
|
-
return super if @condition.is_a?(Hash)
|
|
59
|
-
|
|
60
|
-
raise TypeError, 'condition needs a Hash.'
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
private
|
|
64
|
-
|
|
65
|
-
def build_matcher(key, value)
|
|
66
|
-
if key == '$regex' && @condition.key?('$options')
|
|
67
|
-
RegexMatcher.build(RegexMatcher.apply_options(value, @condition['$options']), context: @context)
|
|
68
|
-
elsif (matcher_class = Matchers.lookup(key))
|
|
69
|
-
matcher_class.build(value, context: @context)
|
|
70
|
-
elsif key == '$options'
|
|
71
|
-
raise TypeError, '$options needs a sibling $regex'
|
|
72
|
-
elsif key.to_s.start_with?('$')
|
|
73
|
-
raise TypeError, "unknown operator #{key}"
|
|
74
|
-
else
|
|
75
|
-
FieldMatcher.build(key, value, context: @context)
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
end
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mongory
|
|
4
|
-
module Matchers
|
|
5
|
-
# InMatcher implements the `$in` operator.
|
|
6
|
-
#
|
|
7
|
-
# It checks whether the record matches any value in the condition array.
|
|
8
|
-
# If the record is an array, it succeeds if any item overlaps with the condition.
|
|
9
|
-
# If the record is a single value (including `nil`), it matches if it is included in the condition.
|
|
10
|
-
#
|
|
11
|
-
# @example Match single value
|
|
12
|
-
# matcher = InMatcher.build([1, 2, 3])
|
|
13
|
-
# matcher.match?(2) #=> true
|
|
14
|
-
# matcher.match?(5) #=> false
|
|
15
|
-
#
|
|
16
|
-
# @example Match nil
|
|
17
|
-
# matcher = InMatcher.build([nil])
|
|
18
|
-
# matcher.match?(nil) #=> true
|
|
19
|
-
#
|
|
20
|
-
# @example Match with array
|
|
21
|
-
# matcher = InMatcher.build([2, 4])
|
|
22
|
-
# matcher.match?([1, 2, 3]) #=> true
|
|
23
|
-
# matcher.match?([5, 6]) #=> false
|
|
24
|
-
#
|
|
25
|
-
# @see AbstractMatcher
|
|
26
|
-
class InMatcher < AbstractMatcher
|
|
27
|
-
def self.build(condition, context: Context.new)
|
|
28
|
-
return super unless condition.is_a?(Range)
|
|
29
|
-
|
|
30
|
-
end_op = condition.exclude_end? ? '$lt' : '$lte'
|
|
31
|
-
head, tail = [condition.first, condition.last].sort
|
|
32
|
-
AndMatcher.build([{ '$gte' => head }, { end_op => tail }], context: context)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
# Creates a raw Proc that performs the in-matching operation.
|
|
36
|
-
# The Proc checks if any element of the record is in the condition array.
|
|
37
|
-
#
|
|
38
|
-
# @return [Proc] a Proc that performs the in-matching operation
|
|
39
|
-
def raw_proc
|
|
40
|
-
condition = build_condition_set
|
|
41
|
-
|
|
42
|
-
Proc.new do |record|
|
|
43
|
-
if record.is_a?(Array)
|
|
44
|
-
is_present?(condition & record)
|
|
45
|
-
else
|
|
46
|
-
condition.include?(record)
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def priority
|
|
52
|
-
1 + Math.log(@condition.size + 1, 1.5)
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
# Ensures the condition is an array or range.
|
|
56
|
-
#
|
|
57
|
-
# @raise [TypeError] if condition is not an array nor a range
|
|
58
|
-
# @return [void]
|
|
59
|
-
def check_validity!
|
|
60
|
-
return if @condition.is_a?(Array)
|
|
61
|
-
|
|
62
|
-
raise TypeError, '$in needs an array or range'
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
private
|
|
66
|
-
|
|
67
|
-
def build_condition_set
|
|
68
|
-
Set.new(@condition).tap do |set|
|
|
69
|
-
@condition.each do |item|
|
|
70
|
-
if item.is_a?(Integer)
|
|
71
|
-
float_val = item.to_f
|
|
72
|
-
set.add(float_val) if float_val == item
|
|
73
|
-
elsif item.is_a?(Float) && item.finite?
|
|
74
|
-
int_val = item.to_i
|
|
75
|
-
set.add(int_val) if int_val == item
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
register(:in, '$in', InMatcher)
|
|
83
|
-
end
|
|
84
|
-
end
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mongory
|
|
4
|
-
module Matchers
|
|
5
|
-
# LiteralMatcher handles direct value comparison with special array handling.
|
|
6
|
-
#
|
|
7
|
-
# This matcher is used when a condition is a literal value (not an operator).
|
|
8
|
-
# It handles both direct equality comparison and array-record scenarios.
|
|
9
|
-
#
|
|
10
|
-
# For array records:
|
|
11
|
-
# - Uses ArrayRecordMatcher to check if any element matches
|
|
12
|
-
# For non-array records:
|
|
13
|
-
# - Uses appropriate matcher based on condition type (Hash, Regexp, nil, etc.)
|
|
14
|
-
#
|
|
15
|
-
# @example Basic equality matching
|
|
16
|
-
# matcher = LiteralMatcher.build(42)
|
|
17
|
-
# matcher.match?(42) #=> true
|
|
18
|
-
# matcher.match?([42, 43]) #=> true (array contains 42)
|
|
19
|
-
#
|
|
20
|
-
# @example Regexp matching
|
|
21
|
-
# matcher = LiteralMatcher.build(/foo/)
|
|
22
|
-
# matcher.match?("foo") #=> true
|
|
23
|
-
# matcher.match?(["foobar"]) #=> true
|
|
24
|
-
#
|
|
25
|
-
# @example Hash condition matching
|
|
26
|
-
# matcher = LiteralMatcher.build({ '$gt' => 10 })
|
|
27
|
-
# matcher.match?(15) #=> true
|
|
28
|
-
# matcher.match?([5, 15]) #=> true
|
|
29
|
-
#
|
|
30
|
-
# @see AbstractMatcher
|
|
31
|
-
# @see ArrayRecordMatcher
|
|
32
|
-
class LiteralMatcher < AbstractMatcher
|
|
33
|
-
# Creates a raw Proc that performs the literal matching operation.
|
|
34
|
-
# The Proc handles both array and non-array records appropriately.
|
|
35
|
-
#
|
|
36
|
-
# @return [Proc] a Proc that performs the literal matching operation
|
|
37
|
-
def raw_proc
|
|
38
|
-
array_record_proc = @array_record_matcher&.to_proc
|
|
39
|
-
dispatched_proc = dispatched_matcher.to_proc
|
|
40
|
-
|
|
41
|
-
Proc.new do |record|
|
|
42
|
-
if record.is_a?(Array)
|
|
43
|
-
unless array_record_proc
|
|
44
|
-
built = !instance_variable_defined?(:@array_record_matcher)
|
|
45
|
-
array_record_proc = array_record_matcher.to_proc
|
|
46
|
-
notify_priority_changed if built
|
|
47
|
-
end
|
|
48
|
-
array_record_proc.call(record)
|
|
49
|
-
else
|
|
50
|
-
dispatched_proc.call(record)
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def priority
|
|
56
|
-
1 + (@array_record_matcher || dispatched_matcher).priority
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
# Selects and returns the appropriate matcher instance for a given literal condition.
|
|
60
|
-
#
|
|
61
|
-
# This method analyzes the type of the raw condition (e.g., Hash, Regexp, nil)
|
|
62
|
-
# and returns a dedicated matcher instance accordingly:
|
|
63
|
-
#
|
|
64
|
-
# - Hash → dispatches to `HashConditionMatcher`
|
|
65
|
-
# - Regexp → dispatches to `RegexMatcher`
|
|
66
|
-
# - nil → dispatches to an `OrMatcher` that emulates MongoDB's `{ field: nil }` behavior
|
|
67
|
-
#
|
|
68
|
-
# For all other literal types, this method returns `EqMatcher`, and fallback equality matching will be used.
|
|
69
|
-
#
|
|
70
|
-
# This matcher is cached after the first invocation using `define_instance_cache_method`
|
|
71
|
-
# to avoid unnecessary re-instantiation.
|
|
72
|
-
#
|
|
73
|
-
# @see Mongory::Matchers::HashConditionMatcher
|
|
74
|
-
# @see Mongory::Matchers::RegexMatcher
|
|
75
|
-
# @see Mongory::Matchers::OrMatcher
|
|
76
|
-
# @see Mongory::Matchers::EqMatcher
|
|
77
|
-
# @return [AbstractMatcher] the matcher used for non-array literal values
|
|
78
|
-
# @!method dispatched_matcher
|
|
79
|
-
define_matcher(:dispatched) do
|
|
80
|
-
matcher = case @condition
|
|
81
|
-
when Hash
|
|
82
|
-
HashConditionMatcher.build(@condition, context: @context)
|
|
83
|
-
when Regexp
|
|
84
|
-
RegexMatcher.build(@condition, context: @context)
|
|
85
|
-
when nil
|
|
86
|
-
OrMatcher.build([
|
|
87
|
-
{ '$exists' => false },
|
|
88
|
-
{ '$eq' => nil }
|
|
89
|
-
], context: @context)
|
|
90
|
-
else
|
|
91
|
-
EqMatcher.build(@condition, context: @context)
|
|
92
|
-
end
|
|
93
|
-
attach_child(matcher)
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
# Lazily defines the collection matcher for array records.
|
|
97
|
-
#
|
|
98
|
-
# @see ArrayRecordMatcher
|
|
99
|
-
# @return [ArrayRecordMatcher] the matcher used to match array-type records
|
|
100
|
-
# @!method array_record_matcher
|
|
101
|
-
define_matcher(:array_record) do
|
|
102
|
-
attach_child(ArrayRecordMatcher.build(@condition, context: @context))
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
# Validates the nested condition matcher, if applicable.
|
|
106
|
-
#
|
|
107
|
-
# @return [void]
|
|
108
|
-
def check_validity!
|
|
109
|
-
dispatched_matcher.check_validity!
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
# Outputs the matcher tree by selecting either collection or condition matcher.
|
|
113
|
-
# Delegates `render_tree` to whichever submatcher was active.
|
|
114
|
-
#
|
|
115
|
-
# @param prefix [String] the prefix string for tree rendering
|
|
116
|
-
# @param is_last [Boolean] whether this is the last node in the tree
|
|
117
|
-
# @return [void]
|
|
118
|
-
def render_tree(prefix = '', is_last: true)
|
|
119
|
-
super
|
|
120
|
-
|
|
121
|
-
target_matcher = @array_record_matcher || dispatched_matcher
|
|
122
|
-
target_matcher.render_tree("#{prefix}#{is_last ? ' ' : '│ '}", is_last: true)
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
end
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mongory
|
|
4
|
-
module Matchers
|
|
5
|
-
# LtMatcher implements the `$lt` (less than) operator.
|
|
6
|
-
#
|
|
7
|
-
# It returns true if the record is strictly less than the condition value.
|
|
8
|
-
#
|
|
9
|
-
# This matcher inherits from AbstractMatcher and uses the `<` operator.
|
|
10
|
-
#
|
|
11
|
-
# @example
|
|
12
|
-
# matcher = LtMatcher.build(10)
|
|
13
|
-
# matcher.match?(9) #=> true
|
|
14
|
-
# matcher.match?(10) #=> false
|
|
15
|
-
# matcher.match?(11) #=> false
|
|
16
|
-
#
|
|
17
|
-
# @see AbstractMatcher
|
|
18
|
-
class LtMatcher < AbstractMatcher
|
|
19
|
-
# Creates a raw Proc that performs the less-than comparison.
|
|
20
|
-
# The Proc uses the `<` operator to compare values.
|
|
21
|
-
#
|
|
22
|
-
# @return [Proc] A proc that performs less-than comparison with error handling
|
|
23
|
-
# @note The proc includes error handling for invalid comparisons
|
|
24
|
-
def raw_proc
|
|
25
|
-
condition = @condition
|
|
26
|
-
|
|
27
|
-
Proc.new do |record|
|
|
28
|
-
record < condition
|
|
29
|
-
rescue StandardError
|
|
30
|
-
false
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def priority
|
|
35
|
-
3
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
register(:lt, '$lt', LtMatcher)
|
|
40
|
-
end
|
|
41
|
-
end
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mongory
|
|
4
|
-
module Matchers
|
|
5
|
-
# LteMatcher implements the `$lte` (less than or equal to) operator.
|
|
6
|
-
#
|
|
7
|
-
# It returns true if the record is less than or equal to the condition value.
|
|
8
|
-
#
|
|
9
|
-
# This matcher inherits from AbstractMatcher and uses the `<=` operator.
|
|
10
|
-
#
|
|
11
|
-
# @example
|
|
12
|
-
# matcher = LteMatcher.build(10)
|
|
13
|
-
# matcher.match?(9) #=> true
|
|
14
|
-
# matcher.match?(10) #=> true
|
|
15
|
-
# matcher.match?(11) #=> false
|
|
16
|
-
#
|
|
17
|
-
# @see AbstractMatcher
|
|
18
|
-
class LteMatcher < AbstractMatcher
|
|
19
|
-
# Creates a raw Proc that performs the less-than-or-equal comparison.
|
|
20
|
-
# The Proc uses the `<=` operator to compare values.
|
|
21
|
-
#
|
|
22
|
-
# @return [Proc] A proc that performs less-than-or-equal comparison with error handling
|
|
23
|
-
# @note The proc includes error handling for invalid comparisons
|
|
24
|
-
def raw_proc
|
|
25
|
-
condition = @condition
|
|
26
|
-
|
|
27
|
-
Proc.new do |record|
|
|
28
|
-
record <= condition
|
|
29
|
-
rescue StandardError
|
|
30
|
-
false
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def priority
|
|
35
|
-
3
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
register(:lte, '$lte', LteMatcher)
|
|
40
|
-
end
|
|
41
|
-
end
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mongory
|
|
4
|
-
module Matchers
|
|
5
|
-
# NeMatcher implements the `$ne` (not equal) operator.
|
|
6
|
-
#
|
|
7
|
-
# It returns true if the record is *not equal* to the condition.
|
|
8
|
-
# If the record is an `Array`, it returns false if either the array itself
|
|
9
|
-
# equals the condition or any element in the array equals the condition.
|
|
10
|
-
#
|
|
11
|
-
# @example
|
|
12
|
-
# matcher = NeMatcher.build(42)
|
|
13
|
-
# matcher.match?(41) #=> true
|
|
14
|
-
# matcher.match?(42) #=> false
|
|
15
|
-
# matcher.match?([10, 42]) #=> false
|
|
16
|
-
# matcher.match?([10, 20]) #=> true
|
|
17
|
-
#
|
|
18
|
-
# @see AbstractMatcher
|
|
19
|
-
class NeMatcher < AbstractMatcher
|
|
20
|
-
# Creates a raw Proc that performs the not-equal comparison.
|
|
21
|
-
# The Proc uses the `!=` operator to compare values.
|
|
22
|
-
#
|
|
23
|
-
# @return [Proc] A proc that performs not-equal comparison
|
|
24
|
-
def raw_proc
|
|
25
|
-
condition = @condition
|
|
26
|
-
|
|
27
|
-
Proc.new do |record|
|
|
28
|
-
record != condition && !(record.is_a?(Array) && record.include?(condition))
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def priority
|
|
33
|
-
1
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
register(:ne, '$ne', NeMatcher)
|
|
38
|
-
end
|
|
39
|
-
end
|