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
data/lib/mongory/utils.rb
CHANGED
|
@@ -1,76 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'date'
|
|
4
3
|
require_relative 'utils/singleton_builder'
|
|
5
|
-
require_relative 'utils/
|
|
4
|
+
require_relative 'utils/tree_renderer'
|
|
6
5
|
require_relative 'utils/context'
|
|
7
6
|
|
|
8
7
|
module Mongory
|
|
9
|
-
# Utility
|
|
10
|
-
#
|
|
11
|
-
# Includes blank checking, present checking,
|
|
12
|
-
# and class-level instance method caching.
|
|
8
|
+
# Utility namespace for shared Mongory internals (`Context`, `TreeRenderer`, `SingletonBuilder`).
|
|
13
9
|
module Utils
|
|
14
|
-
# When included, also extends the including class with ClassMethods.
|
|
15
|
-
# And record which class include this module.
|
|
16
|
-
#
|
|
17
|
-
# @param base [Class, Module]
|
|
18
|
-
def self.included(base)
|
|
19
|
-
base.extend(ClassMethods)
|
|
20
|
-
super
|
|
21
|
-
included_classes << base
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# Where to record classes that include Utils.
|
|
25
|
-
#
|
|
26
|
-
# @return [Array]
|
|
27
|
-
def self.included_classes
|
|
28
|
-
@included_classes ||= []
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# Checks if an object is "present".
|
|
32
|
-
# Inverse of {#is_blank?}.
|
|
33
|
-
#
|
|
34
|
-
# @param obj [Object]
|
|
35
|
-
# @return [Boolean]
|
|
36
|
-
def is_present?(obj)
|
|
37
|
-
!is_blank?(obj)
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# Determines whether an object is considered "blank".
|
|
41
|
-
# Nil, false, empty string/array/hash are blank.
|
|
42
|
-
#
|
|
43
|
-
# @param obj [Object]
|
|
44
|
-
# @return [Boolean]
|
|
45
|
-
def is_blank?(obj)
|
|
46
|
-
case obj
|
|
47
|
-
when false, nil
|
|
48
|
-
true
|
|
49
|
-
when Hash, Array, String, Set
|
|
50
|
-
obj.empty?
|
|
51
|
-
else
|
|
52
|
-
false
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
# Class-level methods injected via Utils.
|
|
57
|
-
module ClassMethods
|
|
58
|
-
# Defines a lazily-evaluated, memoized instance method.
|
|
59
|
-
#
|
|
60
|
-
# @param name [Symbol] the method name
|
|
61
|
-
# @yield block to compute the value
|
|
62
|
-
# @return [void]
|
|
63
|
-
#
|
|
64
|
-
# @example
|
|
65
|
-
# define_instance_cache_method(:expensive_thing) { compute_something }
|
|
66
|
-
def define_instance_cache_method(name, &block)
|
|
67
|
-
instance_key = :"@#{name}"
|
|
68
|
-
define_method(name) do
|
|
69
|
-
return instance_variable_get(instance_key) if instance_variable_defined?(instance_key)
|
|
70
|
-
|
|
71
|
-
instance_variable_set(instance_key, instance_exec(&block))
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
10
|
end
|
|
76
11
|
end
|
data/lib/mongory/version.rb
CHANGED
data/lib/mongory.rb
CHANGED
|
@@ -7,11 +7,8 @@ require 'singleton'
|
|
|
7
7
|
require_relative 'mongory/version'
|
|
8
8
|
require_relative 'mongory/utils'
|
|
9
9
|
require_relative 'mongory/matchers'
|
|
10
|
-
require_relative 'mongory/query_matcher'
|
|
11
|
-
require_relative 'mongory/query_builder'
|
|
12
10
|
require_relative 'mongory/query_operator'
|
|
13
11
|
require_relative 'mongory/converters'
|
|
14
|
-
require_relative 'mongory/rails' if defined?(Rails::Railtie)
|
|
15
12
|
require_relative 'mongory/mongoid' if defined?(Mongoid)
|
|
16
13
|
|
|
17
14
|
# Main namespace for Mongory DSL and configuration.
|
|
@@ -29,8 +26,6 @@ module Mongory
|
|
|
29
26
|
class Error < StandardError; end
|
|
30
27
|
class TypeError < Error; end
|
|
31
28
|
|
|
32
|
-
@c_extension_by_default = false
|
|
33
|
-
|
|
34
29
|
# Yields Mongory for configuration and freezes key components.
|
|
35
30
|
#
|
|
36
31
|
# @example Configure converters
|
|
@@ -61,7 +56,6 @@ module Mongory
|
|
|
61
56
|
def self.freeze
|
|
62
57
|
data_converter.freeze
|
|
63
58
|
condition_converter.freeze
|
|
64
|
-
@debugger = Utils::Debugger.instance
|
|
65
59
|
Matchers.freeze
|
|
66
60
|
super
|
|
67
61
|
end
|
|
@@ -80,33 +74,20 @@ module Mongory
|
|
|
80
74
|
@condition_converter ||= Converters::ConditionConverter.instance
|
|
81
75
|
end
|
|
82
76
|
|
|
83
|
-
# Returns the debugger instance.
|
|
84
|
-
#
|
|
85
|
-
# @return [Utils::Debugger]
|
|
86
|
-
def self.debugger
|
|
87
|
-
@debugger ||= Utils::Debugger.instance
|
|
88
|
-
end
|
|
89
|
-
|
|
90
77
|
# 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`.
|
|
93
78
|
#
|
|
94
79
|
# @param records [Enumerable] any enumerable object (Array, AR::Relation, etc.)
|
|
95
|
-
# @return [QueryBuilder
|
|
80
|
+
# @return [QueryBuilder] a new query builder
|
|
96
81
|
def self.build_query(records)
|
|
97
|
-
|
|
98
|
-
builder_class.new(records)
|
|
82
|
+
QueryBuilder.new(records)
|
|
99
83
|
end
|
|
100
84
|
|
|
101
85
|
# 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
86
|
#
|
|
105
87
|
# @param condition [Hash] the query condition
|
|
106
|
-
# @return [
|
|
88
|
+
# @return [Matcher] a new matcher
|
|
107
89
|
def self.build_matcher(condition)
|
|
108
|
-
|
|
109
|
-
matcher_class.new(condition)
|
|
90
|
+
Matcher.new(condition)
|
|
110
91
|
end
|
|
111
92
|
|
|
112
93
|
# Registers a class to support `.mongory` query DSL.
|
|
@@ -128,51 +109,20 @@ module Mongory
|
|
|
128
109
|
Matchers.enable_symbol_snippets!
|
|
129
110
|
end
|
|
130
111
|
|
|
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
|
-
|
|
158
112
|
# Adds a `#mongory` method to the target class via inclusion.
|
|
159
113
|
#
|
|
160
114
|
# Typically used internally by `Mongory.register(...)`.
|
|
161
115
|
module ClassExtention
|
|
162
116
|
# Returns a query builder scoped to `self`.
|
|
163
117
|
#
|
|
164
|
-
# @return [QueryBuilder
|
|
118
|
+
# @return [QueryBuilder]
|
|
165
119
|
def mongory
|
|
166
120
|
Mongory.build_query(self)
|
|
167
121
|
end
|
|
168
122
|
end
|
|
169
123
|
end
|
|
170
124
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
require_relative 'mongory/c_matcher'
|
|
176
|
-
rescue LoadError => e
|
|
177
|
-
warn("Mongory C extension is disabled because mongory_ext is not loaded: #{e.message}")
|
|
178
|
-
end
|
|
125
|
+
abi = RUBY_VERSION.split('.').first(2).join('.')
|
|
126
|
+
require_relative "core/#{abi}/mongory_ext"
|
|
127
|
+
require_relative 'mongory/matcher'
|
|
128
|
+
require_relative 'mongory/query_builder'
|
data/script/bench.rb
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'benchmark'
|
|
4
|
+
require_relative '../lib/mongory'
|
|
5
|
+
|
|
6
|
+
Mongory.register(Array)
|
|
7
|
+
Mongory.enable_symbol_snippets!
|
|
8
|
+
|
|
9
|
+
N = 100_000
|
|
10
|
+
ROUNDS = 5
|
|
11
|
+
|
|
12
|
+
records = Array.new(N) do |i|
|
|
13
|
+
{
|
|
14
|
+
'age' => 10 + (i % 50),
|
|
15
|
+
'status' => (i % 3).zero? ? 'active' : 'inactive'
|
|
16
|
+
}
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
puts "Ruby: #{RUBY_VERSION} (#{RUBY_PLATFORM}), records=#{N}, rounds=#{ROUNDS}"
|
|
20
|
+
|
|
21
|
+
def bench(label)
|
|
22
|
+
# Warmup
|
|
23
|
+
yield
|
|
24
|
+
t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
25
|
+
count = 0
|
|
26
|
+
ROUNDS.times { count = yield }
|
|
27
|
+
elapsed_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0) * 1000.0) / ROUNDS
|
|
28
|
+
ns_per_rec = (elapsed_ms * 1_000_000.0) / N
|
|
29
|
+
puts format(
|
|
30
|
+
'%<label>-34s %<ms>8.2f ms / 100k (%<ns>6.1f ns/rec, matched=%<count>d)',
|
|
31
|
+
label: label,
|
|
32
|
+
ms: elapsed_ms,
|
|
33
|
+
ns: ns_per_rec,
|
|
34
|
+
count: count
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
bench('Plain Ruby (simple)') do
|
|
39
|
+
records.count { |r| r['age'].is_a?(Numeric) && r['age'] >= 18 }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
simple_m = Mongory::Matcher.new({ age: { :$gte => 18 } })
|
|
43
|
+
bench('Mongory::Matcher (simple)') do
|
|
44
|
+
records.count { |r| simple_m.match?(r) }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
bench('Plain Ruby ($or)') do
|
|
48
|
+
records.count { |r| (r['age'].is_a?(Numeric) && r['age'] >= 18) || r['status'] == 'active' }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
or_m = Mongory::Matcher.new({ :$or => [{ age: { :$gte => 18 } }, { status: 'active' }] })
|
|
52
|
+
bench('Mongory::Matcher ($or)') do
|
|
53
|
+
records.count { |r| or_m.match?(r) }
|
|
54
|
+
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: 2.0.0
|
|
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-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A Mongo-like in-memory query DSL for Ruby
|
|
14
14
|
email:
|
|
@@ -37,6 +37,7 @@ files:
|
|
|
37
37
|
- examples/benchmark.rb
|
|
38
38
|
- ext/mongory_ext/extconf.rb
|
|
39
39
|
- ext/mongory_ext/mongory-core/include/mongory-core.h
|
|
40
|
+
- ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h
|
|
40
41
|
- ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h
|
|
41
42
|
- ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h
|
|
42
43
|
- ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h
|
|
@@ -57,31 +58,31 @@ files:
|
|
|
57
58
|
- ext/mongory_ext/mongory-core/src/foundations/utils.c
|
|
58
59
|
- ext/mongory_ext/mongory-core/src/foundations/utils.h
|
|
59
60
|
- ext/mongory_ext/mongory-core/src/foundations/value.c
|
|
60
|
-
- ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c
|
|
61
|
-
- ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.h
|
|
62
61
|
- ext/mongory_ext/mongory-core/src/matchers/base_matcher.c
|
|
63
62
|
- ext/mongory_ext/mongory-core/src/matchers/base_matcher.h
|
|
64
63
|
- ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c
|
|
65
64
|
- ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h
|
|
66
65
|
- ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c
|
|
67
66
|
- ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h
|
|
68
|
-
- ext/mongory_ext/mongory-core/src/matchers/
|
|
69
|
-
- ext/mongory_ext/mongory-core/src/matchers/
|
|
67
|
+
- ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c
|
|
68
|
+
- ext/mongory_ext/mongory-core/src/matchers/existence_matcher.h
|
|
70
69
|
- ext/mongory_ext/mongory-core/src/matchers/external_matcher.c
|
|
71
70
|
- ext/mongory_ext/mongory-core/src/matchers/external_matcher.h
|
|
71
|
+
- ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c
|
|
72
|
+
- ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.h
|
|
72
73
|
- ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c
|
|
73
74
|
- ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h
|
|
74
|
-
- ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c
|
|
75
|
-
- ext/mongory_ext/mongory-core/src/matchers/literal_matcher.h
|
|
76
75
|
- ext/mongory_ext/mongory-core/src/matchers/matcher.c
|
|
77
76
|
- ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c
|
|
78
77
|
- ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h
|
|
79
78
|
- ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c
|
|
80
79
|
- ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h
|
|
80
|
+
- ext/mongory_ext/mongory-core/src/matchers/value_matcher.c
|
|
81
|
+
- ext/mongory_ext/mongory-core/src/matchers/value_matcher.h
|
|
81
82
|
- ext/mongory_ext/mongory_ext.c
|
|
82
83
|
- ext/mongory_ext/mongory_ext_internal.h
|
|
83
84
|
- ext/mongory_ext/rb_mongory_adapters.c
|
|
84
|
-
- ext/mongory_ext/
|
|
85
|
+
- ext/mongory_ext/rb_mongory_matcher.c
|
|
85
86
|
- ext/mongory_ext/rb_mongory_value.c
|
|
86
87
|
- lib/generators/mongory/install/install_generator.rb
|
|
87
88
|
- lib/generators/mongory/install/templates/initializer.rb.erb
|
|
@@ -89,8 +90,6 @@ files:
|
|
|
89
90
|
- lib/generators/mongory/matcher/templates/matcher.rb.erb
|
|
90
91
|
- lib/generators/mongory/matcher/templates/matcher_spec.rb.erb
|
|
91
92
|
- lib/mongory.rb
|
|
92
|
-
- lib/mongory/c_matcher.rb
|
|
93
|
-
- lib/mongory/c_query_builder.rb
|
|
94
93
|
- lib/mongory/converters.rb
|
|
95
94
|
- lib/mongory/converters/abstract_converter.rb
|
|
96
95
|
- lib/mongory/converters/condition_converter.rb
|
|
@@ -98,41 +97,18 @@ files:
|
|
|
98
97
|
- lib/mongory/converters/data_converter.rb
|
|
99
98
|
- lib/mongory/converters/key_converter.rb
|
|
100
99
|
- lib/mongory/converters/value_converter.rb
|
|
100
|
+
- lib/mongory/matcher.rb
|
|
101
101
|
- lib/mongory/matchers.rb
|
|
102
102
|
- lib/mongory/matchers/abstract_matcher.rb
|
|
103
|
-
- lib/mongory/matchers/abstract_multi_matcher.rb
|
|
104
|
-
- lib/mongory/matchers/and_matcher.rb
|
|
105
|
-
- lib/mongory/matchers/array_record_matcher.rb
|
|
106
|
-
- lib/mongory/matchers/elem_match_matcher.rb
|
|
107
|
-
- lib/mongory/matchers/eq_matcher.rb
|
|
108
|
-
- lib/mongory/matchers/every_matcher.rb
|
|
109
|
-
- lib/mongory/matchers/exists_matcher.rb
|
|
110
|
-
- lib/mongory/matchers/field_matcher.rb
|
|
111
|
-
- lib/mongory/matchers/gt_matcher.rb
|
|
112
|
-
- lib/mongory/matchers/gte_matcher.rb
|
|
113
|
-
- lib/mongory/matchers/hash_condition_matcher.rb
|
|
114
|
-
- lib/mongory/matchers/in_matcher.rb
|
|
115
|
-
- lib/mongory/matchers/literal_matcher.rb
|
|
116
|
-
- lib/mongory/matchers/lt_matcher.rb
|
|
117
|
-
- lib/mongory/matchers/lte_matcher.rb
|
|
118
|
-
- lib/mongory/matchers/ne_matcher.rb
|
|
119
|
-
- lib/mongory/matchers/nin_matcher.rb
|
|
120
|
-
- lib/mongory/matchers/not_matcher.rb
|
|
121
|
-
- lib/mongory/matchers/or_matcher.rb
|
|
122
|
-
- lib/mongory/matchers/present_matcher.rb
|
|
123
|
-
- lib/mongory/matchers/regex_matcher.rb
|
|
124
|
-
- lib/mongory/matchers/size_matcher.rb
|
|
125
103
|
- lib/mongory/mongoid.rb
|
|
126
104
|
- lib/mongory/query_builder.rb
|
|
127
|
-
- lib/mongory/query_matcher.rb
|
|
128
105
|
- lib/mongory/query_operator.rb
|
|
129
|
-
- lib/mongory/rails.rb
|
|
130
106
|
- lib/mongory/utils.rb
|
|
131
107
|
- lib/mongory/utils/context.rb
|
|
132
|
-
- lib/mongory/utils/debugger.rb
|
|
133
|
-
- lib/mongory/utils/rails_patch.rb
|
|
134
108
|
- lib/mongory/utils/singleton_builder.rb
|
|
109
|
+
- lib/mongory/utils/tree_renderer.rb
|
|
135
110
|
- lib/mongory/version.rb
|
|
111
|
+
- script/bench.rb
|
|
136
112
|
- scripts/build_with_core.sh
|
|
137
113
|
- sig/mongory.rbs
|
|
138
114
|
homepage: https://mongoryhq.github.io/mongory-rb/
|
data/lib/mongory/c_matcher.rb
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mongory
|
|
4
|
-
# This class is the C extension of Mongory::QueryMatcher.
|
|
5
|
-
# It is used to match records using the C extension.
|
|
6
|
-
# @example
|
|
7
|
-
# matcher = Mongory::CMatcher.new(condition)
|
|
8
|
-
# matcher.match?(record) #=> true
|
|
9
|
-
# # or
|
|
10
|
-
# collection.mongory.c.where(condition).to_a
|
|
11
|
-
class CMatcher
|
|
12
|
-
# @!method self.new(condition)
|
|
13
|
-
# @param condition [Hash] the condition
|
|
14
|
-
# @return [Mongory::CMatcher] a new matcher
|
|
15
|
-
# @note This method is implemented in the C extension
|
|
16
|
-
# @!method self.trace_result_colorful=(colorful)
|
|
17
|
-
# @param colorful [Boolean] whether to enable colorful trace result
|
|
18
|
-
# @return [void]
|
|
19
|
-
# @note This method is implemented in the C extension
|
|
20
|
-
#
|
|
21
|
-
# @!method match?(record)
|
|
22
|
-
# @param record [Object] the record to match against
|
|
23
|
-
# @return [Boolean] true if the record matches the condition, false otherwise
|
|
24
|
-
# @note This method is implemented in the C extension
|
|
25
|
-
# @!method explain
|
|
26
|
-
# @return [void]
|
|
27
|
-
# @note This method will print matcher tree structure
|
|
28
|
-
# @note This method is implemented in the C extension
|
|
29
|
-
# @!method trace
|
|
30
|
-
# @return [Boolean] true if the record matches the condition, false otherwise
|
|
31
|
-
# @note This method will print matching process
|
|
32
|
-
# @note This method is implemented in the C extension
|
|
33
|
-
# @!method enable_trace
|
|
34
|
-
# @return [void]
|
|
35
|
-
# @note This method will enable trace
|
|
36
|
-
# @note This method is implemented in the C extension
|
|
37
|
-
# @!method disable_trace
|
|
38
|
-
# @return [void]
|
|
39
|
-
# @note This method will disable trace
|
|
40
|
-
# @note This method is implemented in the C extension
|
|
41
|
-
# @!method print_trace
|
|
42
|
-
# @return [void]
|
|
43
|
-
# @note This method will print trace result
|
|
44
|
-
# @note This method is implemented in the C extension
|
|
45
|
-
# @!method condition
|
|
46
|
-
# @return [Hash] the condition
|
|
47
|
-
# @note This method is implemented in the C extension
|
|
48
|
-
# @!method context
|
|
49
|
-
# @return [Utils::Context] the context
|
|
50
|
-
# @note This method is implemented in the C extension
|
|
51
|
-
|
|
52
|
-
# @return [Proc] a Proc that performs the matching operation
|
|
53
|
-
def to_proc
|
|
54
|
-
Proc.new { |record| match?(record) }
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
end
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative 'query_builder'
|
|
4
|
-
|
|
5
|
-
module Mongory
|
|
6
|
-
# Mongory::CQueryBuilder is a query builder for Mongory::CMatcher.
|
|
7
|
-
# It is used to build a query for a Mongory::CMatcher.
|
|
8
|
-
class CQueryBuilder < QueryBuilder
|
|
9
|
-
def each
|
|
10
|
-
return to_enum(:each) unless block_given?
|
|
11
|
-
|
|
12
|
-
@records.each do |record|
|
|
13
|
-
@context.current_record = record
|
|
14
|
-
yield record if @matcher.match?(record)
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
alias_method :fast, :each
|
|
19
|
-
|
|
20
|
-
def explain
|
|
21
|
-
@matcher.match?(@records.first)
|
|
22
|
-
@matcher.explain
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def trace
|
|
26
|
-
return to_enum(:trace) unless block_given?
|
|
27
|
-
|
|
28
|
-
@matcher.enable_trace
|
|
29
|
-
@records.each do |record|
|
|
30
|
-
@context.current_record = record
|
|
31
|
-
yield record if @matcher.match?(record)
|
|
32
|
-
end
|
|
33
|
-
ensure
|
|
34
|
-
@matcher.print_trace
|
|
35
|
-
@matcher.disable_trace
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
private
|
|
39
|
-
|
|
40
|
-
def set_matcher(condition = {})
|
|
41
|
-
@matcher = CMatcher.new(condition, context: @context)
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
end
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mongory
|
|
4
|
-
module Matchers
|
|
5
|
-
# AbstractMultiMatcher is an abstract base class for matchers that operate
|
|
6
|
-
# on multiple subconditions. It provides a generic match loop that applies
|
|
7
|
-
# a logical operator (e.g., `all?`, `any?`) over a list of sub-matchers.
|
|
8
|
-
#
|
|
9
|
-
# Subclasses must define two methods:
|
|
10
|
-
# - `#build_sub_matcher`: how to construct a matcher from each condition
|
|
11
|
-
# - `#operator`: which enumerator method to use (e.g., :all?, :any?)
|
|
12
|
-
#
|
|
13
|
-
# Sub-matchers are cached using `define_instance_cache_method` to prevent
|
|
14
|
-
# repeated construction.
|
|
15
|
-
#
|
|
16
|
-
# @abstract
|
|
17
|
-
# @see AbstractMatcher
|
|
18
|
-
class AbstractMultiMatcher < AbstractMatcher
|
|
19
|
-
# A Proc that always returns true, used as a default for empty AND conditions
|
|
20
|
-
# @return [Proc] A proc that always returns true
|
|
21
|
-
TRUE_PROC = Proc.new { |_| true }.freeze
|
|
22
|
-
|
|
23
|
-
# A Proc that always returns false, used as a default for empty OR conditions
|
|
24
|
-
# @return [Proc] A proc that always returns false
|
|
25
|
-
FALSE_PROC = Proc.new { |_| false }.freeze
|
|
26
|
-
|
|
27
|
-
# Enables auto-unwrap logic.
|
|
28
|
-
# When used, `.build` may unwrap to first matcher if only one is present.
|
|
29
|
-
#
|
|
30
|
-
# @private
|
|
31
|
-
# @return [void]
|
|
32
|
-
def self.enable_unwrap!
|
|
33
|
-
@enable_unwrap = true
|
|
34
|
-
singleton_class.alias_method :build, :build_or_unwrap
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
private_class_method :enable_unwrap!
|
|
38
|
-
|
|
39
|
-
# Builds a matcher and conditionally unwraps it.
|
|
40
|
-
# If unwrapping is enabled and there is only one submatcher,
|
|
41
|
-
# returns that submatcher instead of the multi-matcher wrapper.
|
|
42
|
-
#
|
|
43
|
-
# @param args [Array] arguments passed to the constructor
|
|
44
|
-
# @param context [Context] the query context
|
|
45
|
-
# @return [AbstractMatcher] the constructed matcher or its unwrapped submatcher
|
|
46
|
-
def self.build_or_unwrap(*args, context: Context.new)
|
|
47
|
-
matcher = new(*args, context: context)
|
|
48
|
-
return matcher unless @enable_unwrap
|
|
49
|
-
|
|
50
|
-
matcher = matcher.matchers.first if matcher.matchers.count == 1
|
|
51
|
-
matcher
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
# Recursively checks all submatchers for validity.
|
|
55
|
-
# Raises an error if any submatcher is invalid.
|
|
56
|
-
#
|
|
57
|
-
# @raise [Mongory::TypeError] if any submatcher is invalid
|
|
58
|
-
# @return [void]
|
|
59
|
-
def check_validity!
|
|
60
|
-
matchers.each(&:check_validity!)
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
# Overrides base render_tree to recursively print all submatchers.
|
|
64
|
-
# Each child matcher will be displayed under this multi-matcher node.
|
|
65
|
-
#
|
|
66
|
-
# @param prefix [String] current line prefix for tree alignment
|
|
67
|
-
# @param is_last [Boolean] whether this node is the last sibling
|
|
68
|
-
# @return [void]
|
|
69
|
-
def render_tree(prefix = '', is_last: true)
|
|
70
|
-
super
|
|
71
|
-
|
|
72
|
-
new_prefix = "#{prefix}#{is_last ? ' ' : '│ '}"
|
|
73
|
-
last_index = matchers.count - 1
|
|
74
|
-
matchers.each_with_index do |matcher, index|
|
|
75
|
-
matcher.render_tree(new_prefix, is_last: index == last_index)
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
def priority
|
|
80
|
-
1 + matchers.sum(&:priority)
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
private
|
|
84
|
-
|
|
85
|
-
# Recursively combines multiple matcher procs with AND logic.
|
|
86
|
-
# This method optimizes the combination of multiple matchers by building
|
|
87
|
-
# a balanced tree of AND operations.
|
|
88
|
-
#
|
|
89
|
-
# @param left [Proc] The left matcher proc to combine
|
|
90
|
-
# @param rest [Array<Proc>] The remaining matcher procs to combine
|
|
91
|
-
# @return [Proc] A new proc that combines all matchers with AND logic
|
|
92
|
-
# @example
|
|
93
|
-
# combine_procs_with_and(proc1, proc2, proc3)
|
|
94
|
-
# #=> proc { |record| proc1.call(record) && proc2.call(record) && proc3.call(record) }
|
|
95
|
-
def combine_procs_with_and(left = TRUE_PROC, *rest)
|
|
96
|
-
return left if rest.empty?
|
|
97
|
-
|
|
98
|
-
right = combine_procs_with_and(*rest)
|
|
99
|
-
Proc.new do |record|
|
|
100
|
-
left.call(record) && right.call(record)
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
# Recursively combines multiple matcher procs with OR logic.
|
|
105
|
-
# This method optimizes the combination of multiple matchers by building
|
|
106
|
-
# a balanced tree of OR operations.
|
|
107
|
-
#
|
|
108
|
-
# @param left [Proc] The left matcher proc to combine
|
|
109
|
-
# @param rest [Array<Proc>] The remaining matcher procs to combine
|
|
110
|
-
# @return [Proc] A new proc that combines all matchers with OR logic
|
|
111
|
-
# @example
|
|
112
|
-
# combine_procs_with_or(proc1, proc2, proc3)
|
|
113
|
-
# #=> proc { |record| proc1.call(record) || proc2.call(record) || proc3.call(record) }
|
|
114
|
-
def combine_procs_with_or(left = FALSE_PROC, *rest)
|
|
115
|
-
return left if rest.empty?
|
|
116
|
-
|
|
117
|
-
right = combine_procs_with_or(*rest)
|
|
118
|
-
Proc.new do |record|
|
|
119
|
-
left.call(record) || right.call(record)
|
|
120
|
-
end
|
|
121
|
-
end
|
|
122
|
-
end
|
|
123
|
-
end
|
|
124
|
-
end
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mongory
|
|
4
|
-
module Matchers
|
|
5
|
-
# AndMatcher implements the `$and` logical operator.
|
|
6
|
-
#
|
|
7
|
-
# It evaluates an array of subconditions and returns true only if *all* of them match.
|
|
8
|
-
# For empty conditions, it returns true (using TRUE_PROC), following MongoDB's behavior.
|
|
9
|
-
#
|
|
10
|
-
# Unlike other matchers, AndMatcher flattens the underlying matcher tree by
|
|
11
|
-
# delegating each subcondition to a `HashConditionMatcher`, and further extracting
|
|
12
|
-
# all inner matchers. Duplicate matchers are deduplicated by `uniq_key`.
|
|
13
|
-
#
|
|
14
|
-
# This allows the matcher trace (`.explain`) to render as a flat list of independent conditions.
|
|
15
|
-
#
|
|
16
|
-
# @example Basic usage
|
|
17
|
-
# matcher = AndMatcher.build([
|
|
18
|
-
# { age: { :$gte => 18 } },
|
|
19
|
-
# { name: /foo/ }
|
|
20
|
-
# ])
|
|
21
|
-
# matcher.match?(record) #=> true if both match
|
|
22
|
-
#
|
|
23
|
-
# @example Empty conditions
|
|
24
|
-
# matcher = AndMatcher.build([])
|
|
25
|
-
# matcher.match?(record) #=> true (uses TRUE_PROC)
|
|
26
|
-
#
|
|
27
|
-
# @see AbstractMultiMatcher
|
|
28
|
-
class AndMatcher < AbstractMultiMatcher
|
|
29
|
-
# Constructs a HashConditionMatcher for each subcondition.
|
|
30
|
-
# Conversion is disabled to avoid double-processing.
|
|
31
|
-
enable_unwrap!
|
|
32
|
-
|
|
33
|
-
# Creates a raw Proc that performs the AND operation.
|
|
34
|
-
# The Proc combines all subcondition Procs and returns true only if all match.
|
|
35
|
-
# For empty conditions, returns TRUE_PROC.
|
|
36
|
-
#
|
|
37
|
-
# @return [Proc] a Proc that performs the AND operation
|
|
38
|
-
def raw_proc
|
|
39
|
-
combine_procs_with_and(*matchers.map(&:to_proc))
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# Returns the flattened list of all matchers from each subcondition.
|
|
43
|
-
#
|
|
44
|
-
# Each condition is passed to a HashConditionMatcher, then recursively flattened.
|
|
45
|
-
# All matchers are then deduplicated using `uniq_key`.
|
|
46
|
-
#
|
|
47
|
-
# @return [Array<AbstractMatcher>] A flattened, deduplicated list of matchers
|
|
48
|
-
# @see AbstractMatcher#uniq_key
|
|
49
|
-
define_instance_cache_method(:matchers) do
|
|
50
|
-
@condition.flat_map do |condition|
|
|
51
|
-
HashConditionMatcher.new(condition, context: @context).matchers
|
|
52
|
-
end.uniq(&:uniq_key).sort_by(&:priority)
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
# Ensures the condition is an array of hashes.
|
|
56
|
-
#
|
|
57
|
-
# @raise [Mongory::TypeError] if not valid
|
|
58
|
-
# @return [void]
|
|
59
|
-
def check_validity!
|
|
60
|
-
raise TypeError, '$and needs an array' unless @condition.is_a?(Array)
|
|
61
|
-
|
|
62
|
-
@condition.each do |sub_condition|
|
|
63
|
-
raise TypeError, '$and needs an array of hash' unless sub_condition.is_a?(Hash)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
super
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
register(:and, '$and', AndMatcher)
|
|
71
|
-
end
|
|
72
|
-
end
|