mongory 0.8.2 → 0.9.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 +44 -0
- data/README.md +8 -4
- data/docs/advanced_usage.md +7 -0
- data/docs/clang_bridge.md +6 -0
- data/ext/mongory_ext/extconf.rb +6 -1
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +25 -0
- data/ext/mongory_ext/mongory-core/src/foundations/config.c +64 -27
- data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +1 -0
- data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +49 -0
- data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +45 -0
- data/ext/mongory_ext/mongory-core/src/foundations/utils.c +2 -5
- data/ext/mongory_ext/mongory-core/src/foundations/value.c +44 -7
- data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c +31 -10
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +3 -0
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +1 -0
- data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +32 -13
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +69 -15
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +6 -0
- data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +78 -15
- data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +12 -0
- data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c +71 -17
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +2 -1
- data/ext/mongory_ext/mongory_ext.c +35 -724
- data/ext/mongory_ext/mongory_ext_internal.h +75 -0
- data/ext/mongory_ext/rb_mongory_adapters.c +140 -0
- data/ext/mongory_ext/rb_mongory_cmatcher.c +386 -0
- data/ext/mongory_ext/rb_mongory_value.c +260 -0
- data/lib/generators/mongory/install/templates/initializer.rb.erb +5 -0
- data/lib/mongory/converters/abstract_converter.rb +6 -5
- data/lib/mongory/converters/condition_converter.rb +2 -2
- data/lib/mongory/matchers/abstract_matcher.rb +29 -1
- data/lib/mongory/matchers/abstract_multi_matcher.rb +30 -4
- data/lib/mongory/matchers/and_matcher.rb +3 -2
- data/lib/mongory/matchers/array_record_matcher.rb +24 -12
- data/lib/mongory/matchers/elem_match_matcher.rb +2 -0
- data/lib/mongory/matchers/eq_matcher.rb +4 -2
- data/lib/mongory/matchers/hash_condition_matcher.rb +25 -7
- data/lib/mongory/matchers/in_matcher.rb +17 -1
- data/lib/mongory/matchers/literal_matcher.rb +22 -17
- data/lib/mongory/matchers/ne_matcher.rb +7 -6
- data/lib/mongory/matchers/nin_matcher.rb +2 -6
- data/lib/mongory/matchers/or_matcher.rb +3 -2
- data/lib/mongory/matchers/regex_matcher.rb +29 -0
- data/lib/mongory/matchers.rb +7 -4
- data/lib/mongory/version.rb +1 -1
- data/lib/mongory.rb +57 -5
- metadata +7 -2
|
@@ -35,12 +35,16 @@ module Mongory
|
|
|
35
35
|
#
|
|
36
36
|
# @return [Proc] a Proc that performs the literal matching operation
|
|
37
37
|
def raw_proc
|
|
38
|
-
array_record_proc =
|
|
38
|
+
array_record_proc = @array_record_matcher&.to_proc
|
|
39
39
|
dispatched_proc = dispatched_matcher.to_proc
|
|
40
40
|
|
|
41
41
|
Proc.new do |record|
|
|
42
42
|
if record.is_a?(Array)
|
|
43
|
-
array_record_proc
|
|
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
|
|
44
48
|
array_record_proc.call(record)
|
|
45
49
|
else
|
|
46
50
|
dispatched_proc.call(record)
|
|
@@ -49,7 +53,7 @@ module Mongory
|
|
|
49
53
|
end
|
|
50
54
|
|
|
51
55
|
def priority
|
|
52
|
-
1 + dispatched_matcher.priority
|
|
56
|
+
1 + (@array_record_matcher || dispatched_matcher).priority
|
|
53
57
|
end
|
|
54
58
|
|
|
55
59
|
# Selects and returns the appropriate matcher instance for a given literal condition.
|
|
@@ -73,19 +77,20 @@ module Mongory
|
|
|
73
77
|
# @return [AbstractMatcher] the matcher used for non-array literal values
|
|
74
78
|
# @!method dispatched_matcher
|
|
75
79
|
define_matcher(:dispatched) do
|
|
76
|
-
case @condition
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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)
|
|
89
94
|
end
|
|
90
95
|
|
|
91
96
|
# Lazily defines the collection matcher for array records.
|
|
@@ -94,7 +99,7 @@ module Mongory
|
|
|
94
99
|
# @return [ArrayRecordMatcher] the matcher used to match array-type records
|
|
95
100
|
# @!method array_record_matcher
|
|
96
101
|
define_matcher(:array_record) do
|
|
97
|
-
ArrayRecordMatcher.build(@condition, context: @context)
|
|
102
|
+
attach_child(ArrayRecordMatcher.build(@condition, context: @context))
|
|
98
103
|
end
|
|
99
104
|
|
|
100
105
|
# Validates the nested condition matcher, if applicable.
|
|
@@ -5,14 +5,15 @@ module Mongory
|
|
|
5
5
|
# NeMatcher implements the `$ne` (not equal) operator.
|
|
6
6
|
#
|
|
7
7
|
# It returns true if the record is *not equal* to the condition.
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
# and uses Ruby's `!=` operator for comparison.
|
|
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.
|
|
11
10
|
#
|
|
12
11
|
# @example
|
|
13
12
|
# matcher = NeMatcher.build(42)
|
|
14
|
-
# matcher.match?(41)
|
|
15
|
-
# matcher.match?(42)
|
|
13
|
+
# matcher.match?(41) #=> true
|
|
14
|
+
# matcher.match?(42) #=> false
|
|
15
|
+
# matcher.match?([10, 42]) #=> false
|
|
16
|
+
# matcher.match?([10, 20]) #=> true
|
|
16
17
|
#
|
|
17
18
|
# @see AbstractMatcher
|
|
18
19
|
class NeMatcher < AbstractMatcher
|
|
@@ -24,7 +25,7 @@ module Mongory
|
|
|
24
25
|
condition = @condition
|
|
25
26
|
|
|
26
27
|
Proc.new do |record|
|
|
27
|
-
record != condition
|
|
28
|
+
record != condition && !(record.is_a?(Array) && record.include?(condition))
|
|
28
29
|
end
|
|
29
30
|
end
|
|
30
31
|
|
|
@@ -23,7 +23,7 @@ module Mongory
|
|
|
23
23
|
# matcher.match?([4, 5]) #=> false
|
|
24
24
|
#
|
|
25
25
|
# @see AbstractMatcher
|
|
26
|
-
class NinMatcher <
|
|
26
|
+
class NinMatcher < InMatcher
|
|
27
27
|
def self.build(condition, context: Context.new)
|
|
28
28
|
return super unless condition.is_a?(Range)
|
|
29
29
|
|
|
@@ -37,7 +37,7 @@ module Mongory
|
|
|
37
37
|
#
|
|
38
38
|
# @return [Proc] A proc that performs not-in matching
|
|
39
39
|
def raw_proc
|
|
40
|
-
condition =
|
|
40
|
+
condition = build_condition_set
|
|
41
41
|
|
|
42
42
|
Proc.new do |record|
|
|
43
43
|
if record.is_a?(Array)
|
|
@@ -48,10 +48,6 @@ module Mongory
|
|
|
48
48
|
end
|
|
49
49
|
end
|
|
50
50
|
|
|
51
|
-
def priority
|
|
52
|
-
1 + Math.log(@condition.size + 1, 1.5)
|
|
53
|
-
end
|
|
54
|
-
|
|
55
51
|
# Ensures the condition is a valid array or range.
|
|
56
52
|
#
|
|
57
53
|
# @raise [TypeError] if the condition is not an array nor a range
|
|
@@ -43,9 +43,10 @@ module Mongory
|
|
|
43
43
|
#
|
|
44
44
|
# @return [Array<AbstractMatcher>] array of submatchers
|
|
45
45
|
define_instance_cache_method(:matchers) do
|
|
46
|
-
@condition.map do |condition|
|
|
46
|
+
children = @condition.map do |condition|
|
|
47
47
|
HashConditionMatcher.build(condition, context: @context)
|
|
48
|
-
end
|
|
48
|
+
end
|
|
49
|
+
sort_and_adopt(children)
|
|
49
50
|
end
|
|
50
51
|
|
|
51
52
|
# Ensures the condition is an array of hashes.
|
|
@@ -31,6 +31,35 @@ module Mongory
|
|
|
31
31
|
#
|
|
32
32
|
# @see AbstractMatcher
|
|
33
33
|
class RegexMatcher < AbstractMatcher
|
|
34
|
+
# MongoDB `$options` flags mapped to Ruby. Ruby's `^`/`$` always match at
|
|
35
|
+
# line boundaries, so `m` needs no flag; Ruby's MULTILINE is MongoDB's `s`.
|
|
36
|
+
OPTION_FLAGS = {
|
|
37
|
+
'i' => Regexp::IGNORECASE,
|
|
38
|
+
'm' => 0,
|
|
39
|
+
's' => Regexp::MULTILINE
|
|
40
|
+
}.freeze
|
|
41
|
+
|
|
42
|
+
# Compiles a `$regex` pattern string with MongoDB `$options` flags.
|
|
43
|
+
#
|
|
44
|
+
# @param pattern [String, Regexp, Object] the `$regex` value
|
|
45
|
+
# @param options [Object] the `$options` value
|
|
46
|
+
# @return [Regexp, Object] the compiled Regexp, or a non-string pattern
|
|
47
|
+
# unchanged so that check_validity! reports it
|
|
48
|
+
# @raise [TypeError] if options is invalid or pattern is already a Regexp
|
|
49
|
+
def self.apply_options(pattern, options)
|
|
50
|
+
raise TypeError, '$options needs a string' unless options.is_a?(String)
|
|
51
|
+
|
|
52
|
+
flags = options.each_char.reduce(0) do |result, char|
|
|
53
|
+
raise TypeError, "unsupported $options flag #{char.inspect}" unless OPTION_FLAGS.key?(char)
|
|
54
|
+
|
|
55
|
+
result | OPTION_FLAGS[char]
|
|
56
|
+
end
|
|
57
|
+
raise TypeError, '$options cannot modify a Regexp' if pattern.is_a?(Regexp)
|
|
58
|
+
return pattern unless pattern.is_a?(String)
|
|
59
|
+
|
|
60
|
+
Regexp.new(pattern, flags)
|
|
61
|
+
end
|
|
62
|
+
|
|
34
63
|
# Initializes the matcher with a regex pattern.
|
|
35
64
|
# Converts string patterns to Regexp objects.
|
|
36
65
|
#
|
data/lib/mongory/matchers.rb
CHANGED
|
@@ -16,6 +16,7 @@ module Mongory
|
|
|
16
16
|
module Matchers
|
|
17
17
|
@operator_mapping = {}
|
|
18
18
|
@registries = []
|
|
19
|
+
@enable_symbol_snippets = false
|
|
19
20
|
|
|
20
21
|
# Registers a matcher class for a given operator and method symbol.
|
|
21
22
|
#
|
|
@@ -26,11 +27,11 @@ module Mongory
|
|
|
26
27
|
# @raise [ArgumentError] if validations fail
|
|
27
28
|
def self.register(method_sym, operator, klass)
|
|
28
29
|
Validator.validate_method(method_sym)
|
|
29
|
-
Validator.validate_operator(operator)
|
|
30
|
+
Validator.validate_operator(operator, klass)
|
|
30
31
|
Validator.validate_class(klass)
|
|
31
32
|
|
|
32
33
|
@operator_mapping[operator] = klass
|
|
33
|
-
registry = Registry.new(method_sym, operator)
|
|
34
|
+
registry = Registry.new(method_sym, operator).freeze
|
|
34
35
|
@registries << registry
|
|
35
36
|
return unless @enable_symbol_snippets
|
|
36
37
|
|
|
@@ -62,9 +63,10 @@ module Mongory
|
|
|
62
63
|
end
|
|
63
64
|
|
|
64
65
|
def self.freeze
|
|
65
|
-
|
|
66
|
+
@registries.each(&:freeze)
|
|
66
67
|
@operator_mapping.freeze
|
|
67
68
|
@registries.freeze
|
|
69
|
+
super
|
|
68
70
|
end
|
|
69
71
|
|
|
70
72
|
# @private
|
|
@@ -85,9 +87,10 @@ module Mongory
|
|
|
85
87
|
# Warns on duplicate registration.
|
|
86
88
|
#
|
|
87
89
|
# @param operator [String]
|
|
90
|
+
# @param klass [Class, nil]
|
|
88
91
|
# @return [void]
|
|
89
92
|
# @raise [Mongory::TypeError] if operator format is invalid
|
|
90
|
-
def self.validate_operator(operator)
|
|
93
|
+
def self.validate_operator(operator, klass = nil)
|
|
91
94
|
if Matchers.lookup(operator)
|
|
92
95
|
warn "Duplicate operator registration: #{operator} (#{Matchers.lookup(operator)} vs #{klass})"
|
|
93
96
|
end
|
data/lib/mongory/version.rb
CHANGED
data/lib/mongory.rb
CHANGED
|
@@ -29,6 +29,8 @@ module Mongory
|
|
|
29
29
|
class Error < StandardError; end
|
|
30
30
|
class TypeError < Error; end
|
|
31
31
|
|
|
32
|
+
@c_extension_by_default = false
|
|
33
|
+
|
|
32
34
|
# Yields Mongory for configuration and freezes key components.
|
|
33
35
|
#
|
|
34
36
|
# @example Configure converters
|
|
@@ -47,12 +49,21 @@ module Mongory
|
|
|
47
49
|
# end
|
|
48
50
|
#
|
|
49
51
|
# @yieldparam self [Mongory]
|
|
50
|
-
# @return [
|
|
52
|
+
# @return [self]
|
|
51
53
|
def self.configure
|
|
52
54
|
yield self
|
|
55
|
+
freeze
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Freezes Mongory configuration, converters, and matchers.
|
|
59
|
+
#
|
|
60
|
+
# @return [self]
|
|
61
|
+
def self.freeze
|
|
53
62
|
data_converter.freeze
|
|
54
63
|
condition_converter.freeze
|
|
64
|
+
@debugger = Utils::Debugger.instance
|
|
55
65
|
Matchers.freeze
|
|
66
|
+
super
|
|
56
67
|
end
|
|
57
68
|
|
|
58
69
|
# Returns the data converter instance.
|
|
@@ -77,11 +88,25 @@ module Mongory
|
|
|
77
88
|
end
|
|
78
89
|
|
|
79
90
|
# Builds a new query over the given record set.
|
|
91
|
+
# Uses `CQueryBuilder` when `enable_c_extension_by_default!` is enabled and the C extension is available;
|
|
92
|
+
# otherwise uses `QueryBuilder`.
|
|
80
93
|
#
|
|
81
94
|
# @param records [Enumerable] any enumerable object (Array, AR::Relation, etc.)
|
|
82
|
-
# @return [QueryBuilder] a new query builder
|
|
95
|
+
# @return [QueryBuilder, CQueryBuilder] a new query builder
|
|
83
96
|
def self.build_query(records)
|
|
84
|
-
QueryBuilder
|
|
97
|
+
builder_class = c_extension_by_default? ? CQueryBuilder : QueryBuilder
|
|
98
|
+
builder_class.new(records)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Builds a new matcher for the given condition.
|
|
102
|
+
# Uses `CMatcher` when `enable_c_extension_by_default!` is enabled and the C extension is available;
|
|
103
|
+
# otherwise uses `QueryMatcher`.
|
|
104
|
+
#
|
|
105
|
+
# @param condition [Hash] the query condition
|
|
106
|
+
# @return [QueryMatcher, CMatcher] a new matcher
|
|
107
|
+
def self.build_matcher(condition)
|
|
108
|
+
matcher_class = c_extension_by_default? ? CMatcher : QueryMatcher
|
|
109
|
+
matcher_class.new(condition)
|
|
85
110
|
end
|
|
86
111
|
|
|
87
112
|
# Registers a class to support `.mongory` query DSL.
|
|
@@ -103,15 +128,42 @@ module Mongory
|
|
|
103
128
|
Matchers.enable_symbol_snippets!
|
|
104
129
|
end
|
|
105
130
|
|
|
131
|
+
# Enables using the C extension (`CQueryBuilder`) by default when building queries
|
|
132
|
+
# via `Mongory.build_query` or `.mongory`.
|
|
133
|
+
#
|
|
134
|
+
# Automatically falls back to the pure Ruby `QueryBuilder` if the C extension is unavailable.
|
|
135
|
+
#
|
|
136
|
+
# @return [Boolean] true if C extension is available and enabled by default
|
|
137
|
+
def self.enable_c_extension_by_default!
|
|
138
|
+
@c_extension_by_default = !defined?(Mongory::CMatcher).nil?
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
singleton_class.alias_method :use_c_extension_by_default!, :enable_c_extension_by_default!
|
|
142
|
+
|
|
143
|
+
# Disables using the C extension by default, reverting `.mongory` and `Mongory.build_query`
|
|
144
|
+
# to the pure Ruby `QueryBuilder`.
|
|
145
|
+
#
|
|
146
|
+
# @return [void]
|
|
147
|
+
def self.disable_c_extension_by_default!
|
|
148
|
+
@c_extension_by_default = false
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Returns whether the C extension is used by default for queries.
|
|
152
|
+
#
|
|
153
|
+
# @return [Boolean]
|
|
154
|
+
def self.c_extension_by_default?
|
|
155
|
+
@c_extension_by_default == true && !defined?(Mongory::CMatcher).nil?
|
|
156
|
+
end
|
|
157
|
+
|
|
106
158
|
# Adds a `#mongory` method to the target class via inclusion.
|
|
107
159
|
#
|
|
108
160
|
# Typically used internally by `Mongory.register(...)`.
|
|
109
161
|
module ClassExtention
|
|
110
162
|
# Returns a query builder scoped to `self`.
|
|
111
163
|
#
|
|
112
|
-
# @return [QueryBuilder]
|
|
164
|
+
# @return [QueryBuilder, CQueryBuilder]
|
|
113
165
|
def mongory
|
|
114
|
-
Mongory
|
|
166
|
+
Mongory.build_query(self)
|
|
115
167
|
end
|
|
116
168
|
end
|
|
117
169
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mongory
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.9.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- koten0224
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-18 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A Mongo-like in-memory query DSL for Ruby
|
|
14
14
|
email:
|
|
@@ -50,6 +50,7 @@ files:
|
|
|
50
50
|
- ext/mongory_ext/mongory-core/src/foundations/config_private.h
|
|
51
51
|
- ext/mongory_ext/mongory-core/src/foundations/error.c
|
|
52
52
|
- ext/mongory_ext/mongory-core/src/foundations/memory_pool.c
|
|
53
|
+
- ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h
|
|
53
54
|
- ext/mongory_ext/mongory-core/src/foundations/string_buffer.c
|
|
54
55
|
- ext/mongory_ext/mongory-core/src/foundations/string_buffer.h
|
|
55
56
|
- ext/mongory_ext/mongory-core/src/foundations/table.c
|
|
@@ -78,6 +79,10 @@ files:
|
|
|
78
79
|
- ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c
|
|
79
80
|
- ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h
|
|
80
81
|
- ext/mongory_ext/mongory_ext.c
|
|
82
|
+
- ext/mongory_ext/mongory_ext_internal.h
|
|
83
|
+
- ext/mongory_ext/rb_mongory_adapters.c
|
|
84
|
+
- ext/mongory_ext/rb_mongory_cmatcher.c
|
|
85
|
+
- ext/mongory_ext/rb_mongory_value.c
|
|
81
86
|
- lib/generators/mongory/install/install_generator.rb
|
|
82
87
|
- lib/generators/mongory/install/templates/initializer.rb.erb
|
|
83
88
|
- lib/generators/mongory/matcher/matcher_generator.rb
|