mongory 0.9.0-aarch64-linux → 2.0.0-aarch64-linux

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.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/README.md +121 -422
  4. data/SUBMODULE_INTEGRATION.md +2 -2
  5. data/docs/clang_bridge.md +28 -59
  6. data/docs/migration.md +17 -0
  7. data/docs/performance.md +14 -51
  8. data/examples/benchmark.rb +10 -10
  9. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +62 -0
  10. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +31 -0
  11. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +3 -0
  12. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +20 -0
  13. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +31 -4
  14. data/ext/mongory_ext/mongory-core/src/foundations/array.c +48 -6
  15. data/ext/mongory_ext/mongory-core/src/foundations/config.c +30 -2
  16. data/ext/mongory_ext/mongory-core/src/foundations/error.c +10 -0
  17. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +78 -19
  18. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +73 -19
  19. data/ext/mongory_ext/mongory-core/src/foundations/table.c +8 -3
  20. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +44 -55
  21. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +7 -0
  22. data/ext/mongory_ext/mongory-core/src/foundations/value.c +3 -0
  23. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +69 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +15 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +9 -4
  26. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +167 -31
  27. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +7 -1
  28. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.c → existence_matcher.c} +10 -12
  29. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.h → existence_matcher.h} +4 -4
  30. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +20 -6
  31. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.c → implicit_array_matcher.c} +57 -38
  32. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.h → implicit_array_matcher.h} +5 -5
  33. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +8 -10
  34. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +337 -96
  35. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +59 -21
  36. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +7 -1
  37. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +6 -5
  38. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -1
  39. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.c → value_matcher.c} +120 -60
  40. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.h → value_matcher.h} +13 -13
  41. data/ext/mongory_ext/mongory_ext.c +2 -2
  42. data/ext/mongory_ext/mongory_ext_internal.h +2 -2
  43. data/ext/mongory_ext/{rb_mongory_cmatcher.c → rb_mongory_matcher.c} +157 -23
  44. data/lib/core/2.6/mongory_ext.so +0 -0
  45. data/lib/core/2.7/mongory_ext.so +0 -0
  46. data/lib/core/3.0/mongory_ext.so +0 -0
  47. data/lib/core/3.1/mongory_ext.so +0 -0
  48. data/lib/core/3.2/mongory_ext.so +0 -0
  49. data/lib/core/3.3/mongory_ext.so +0 -0
  50. data/lib/core/3.4/mongory_ext.so +0 -0
  51. data/lib/core/4.0/mongory_ext.so +0 -0
  52. data/lib/generators/mongory/install/templates/initializer.rb.erb +0 -5
  53. data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
  54. data/lib/mongory/converters/converted.rb +28 -6
  55. data/lib/mongory/converters/key_converter.rb +2 -0
  56. data/lib/mongory/matcher.rb +63 -0
  57. data/lib/mongory/matchers/abstract_matcher.rb +18 -164
  58. data/lib/mongory/matchers.rb +68 -84
  59. data/lib/mongory/query_builder.rb +30 -41
  60. data/lib/mongory/utils/tree_renderer.rb +108 -0
  61. data/lib/mongory/utils.rb +2 -67
  62. data/lib/mongory/version.rb +1 -1
  63. data/lib/mongory.rb +9 -59
  64. data/script/bench.rb +54 -0
  65. metadata +13 -37
  66. data/lib/mongory/c_matcher.rb +0 -57
  67. data/lib/mongory/c_query_builder.rb +0 -44
  68. data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -124
  69. data/lib/mongory/matchers/and_matcher.rb +0 -72
  70. data/lib/mongory/matchers/array_record_matcher.rb +0 -105
  71. data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
  72. data/lib/mongory/matchers/eq_matcher.rb +0 -48
  73. data/lib/mongory/matchers/every_matcher.rb +0 -56
  74. data/lib/mongory/matchers/exists_matcher.rb +0 -53
  75. data/lib/mongory/matchers/field_matcher.rb +0 -160
  76. data/lib/mongory/matchers/gt_matcher.rb +0 -41
  77. data/lib/mongory/matchers/gte_matcher.rb +0 -41
  78. data/lib/mongory/matchers/hash_condition_matcher.rb +0 -79
  79. data/lib/mongory/matchers/in_matcher.rb +0 -84
  80. data/lib/mongory/matchers/literal_matcher.rb +0 -121
  81. data/lib/mongory/matchers/lt_matcher.rb +0 -41
  82. data/lib/mongory/matchers/lte_matcher.rb +0 -41
  83. data/lib/mongory/matchers/ne_matcher.rb +0 -39
  84. data/lib/mongory/matchers/nin_matcher.rb +0 -64
  85. data/lib/mongory/matchers/not_matcher.rb +0 -40
  86. data/lib/mongory/matchers/or_matcher.rb +0 -68
  87. data/lib/mongory/matchers/present_matcher.rb +0 -55
  88. data/lib/mongory/matchers/regex_matcher.rb +0 -109
  89. data/lib/mongory/matchers/size_matcher.rb +0 -54
  90. data/lib/mongory/query_matcher.rb +0 -93
  91. data/lib/mongory/rails.rb +0 -15
  92. data/lib/mongory/utils/debugger.rb +0 -125
  93. 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/debugger'
4
+ require_relative 'utils/tree_renderer'
6
5
  require_relative 'utils/context'
7
6
 
8
7
  module Mongory
9
- # Utility helpers shared across Mongory internals.
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mongory
4
- VERSION = '0.9.0'
4
+ VERSION = '2.0.0'
5
5
  end
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, CQueryBuilder] a new query builder
80
+ # @return [QueryBuilder] a new query builder
96
81
  def self.build_query(records)
97
- builder_class = c_extension_by_default? ? CQueryBuilder : QueryBuilder
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 [QueryMatcher, CMatcher] a new matcher
88
+ # @return [Matcher] a new matcher
107
89
  def self.build_matcher(condition)
108
- matcher_class = c_extension_by_default? ? CMatcher : QueryMatcher
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, CQueryBuilder]
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
- begin
172
- abi = RUBY_VERSION.split('.').first(2).join('.')
173
- require_relative "core/#{abi}/mongory_ext"
174
- require_relative 'mongory/c_query_builder'
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.9.0
4
+ version: 2.0.0
5
5
  platform: aarch64-linux
6
6
  authors:
7
7
  - koten0224
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-16 00:00:00.000000000 Z
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:
@@ -36,6 +36,7 @@ files:
36
36
  - examples/benchmark.rb
37
37
  - ext/mongory_ext/extconf.rb
38
38
  - ext/mongory_ext/mongory-core/include/mongory-core.h
39
+ - ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h
39
40
  - ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h
40
41
  - ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h
41
42
  - ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h
@@ -56,31 +57,31 @@ files:
56
57
  - ext/mongory_ext/mongory-core/src/foundations/utils.c
57
58
  - ext/mongory_ext/mongory-core/src/foundations/utils.h
58
59
  - ext/mongory_ext/mongory-core/src/foundations/value.c
59
- - ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c
60
- - ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.h
61
60
  - ext/mongory_ext/mongory-core/src/matchers/base_matcher.c
62
61
  - ext/mongory_ext/mongory-core/src/matchers/base_matcher.h
63
62
  - ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c
64
63
  - ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h
65
64
  - ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c
66
65
  - ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h
67
- - ext/mongory_ext/mongory-core/src/matchers/existance_matcher.c
68
- - ext/mongory_ext/mongory-core/src/matchers/existance_matcher.h
66
+ - ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c
67
+ - ext/mongory_ext/mongory-core/src/matchers/existence_matcher.h
69
68
  - ext/mongory_ext/mongory-core/src/matchers/external_matcher.c
70
69
  - ext/mongory_ext/mongory-core/src/matchers/external_matcher.h
70
+ - ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c
71
+ - ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.h
71
72
  - ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c
72
73
  - ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h
73
- - ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c
74
- - ext/mongory_ext/mongory-core/src/matchers/literal_matcher.h
75
74
  - ext/mongory_ext/mongory-core/src/matchers/matcher.c
76
75
  - ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c
77
76
  - ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h
78
77
  - ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c
79
78
  - ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h
79
+ - ext/mongory_ext/mongory-core/src/matchers/value_matcher.c
80
+ - ext/mongory_ext/mongory-core/src/matchers/value_matcher.h
80
81
  - ext/mongory_ext/mongory_ext.c
81
82
  - ext/mongory_ext/mongory_ext_internal.h
82
83
  - ext/mongory_ext/rb_mongory_adapters.c
83
- - ext/mongory_ext/rb_mongory_cmatcher.c
84
+ - ext/mongory_ext/rb_mongory_matcher.c
84
85
  - ext/mongory_ext/rb_mongory_value.c
85
86
  - lib/core/2.6/mongory_ext.so
86
87
  - lib/core/2.7/mongory_ext.so
@@ -96,8 +97,6 @@ files:
96
97
  - lib/generators/mongory/matcher/templates/matcher.rb.erb
97
98
  - lib/generators/mongory/matcher/templates/matcher_spec.rb.erb
98
99
  - lib/mongory.rb
99
- - lib/mongory/c_matcher.rb
100
- - lib/mongory/c_query_builder.rb
101
100
  - lib/mongory/converters.rb
102
101
  - lib/mongory/converters/abstract_converter.rb
103
102
  - lib/mongory/converters/condition_converter.rb
@@ -105,42 +104,19 @@ files:
105
104
  - lib/mongory/converters/data_converter.rb
106
105
  - lib/mongory/converters/key_converter.rb
107
106
  - lib/mongory/converters/value_converter.rb
107
+ - lib/mongory/matcher.rb
108
108
  - lib/mongory/matchers.rb
109
109
  - lib/mongory/matchers/abstract_matcher.rb
110
- - lib/mongory/matchers/abstract_multi_matcher.rb
111
- - lib/mongory/matchers/and_matcher.rb
112
- - lib/mongory/matchers/array_record_matcher.rb
113
- - lib/mongory/matchers/elem_match_matcher.rb
114
- - lib/mongory/matchers/eq_matcher.rb
115
- - lib/mongory/matchers/every_matcher.rb
116
- - lib/mongory/matchers/exists_matcher.rb
117
- - lib/mongory/matchers/field_matcher.rb
118
- - lib/mongory/matchers/gt_matcher.rb
119
- - lib/mongory/matchers/gte_matcher.rb
120
- - lib/mongory/matchers/hash_condition_matcher.rb
121
- - lib/mongory/matchers/in_matcher.rb
122
- - lib/mongory/matchers/literal_matcher.rb
123
- - lib/mongory/matchers/lt_matcher.rb
124
- - lib/mongory/matchers/lte_matcher.rb
125
- - lib/mongory/matchers/ne_matcher.rb
126
- - lib/mongory/matchers/nin_matcher.rb
127
- - lib/mongory/matchers/not_matcher.rb
128
- - lib/mongory/matchers/or_matcher.rb
129
- - lib/mongory/matchers/present_matcher.rb
130
- - lib/mongory/matchers/regex_matcher.rb
131
- - lib/mongory/matchers/size_matcher.rb
132
110
  - lib/mongory/mongoid.rb
133
111
  - lib/mongory/query_builder.rb
134
- - lib/mongory/query_matcher.rb
135
112
  - lib/mongory/query_operator.rb
136
- - lib/mongory/rails.rb
137
113
  - lib/mongory/utils.rb
138
114
  - lib/mongory/utils/context.rb
139
- - lib/mongory/utils/debugger.rb
140
- - lib/mongory/utils/rails_patch.rb
141
115
  - lib/mongory/utils/singleton_builder.rb
116
+ - lib/mongory/utils/tree_renderer.rb
142
117
  - lib/mongory/version.rb
143
118
  - mongory.gemspec
119
+ - script/bench.rb
144
120
  - scripts/build_with_core.sh
145
121
  - sig/mongory.rbs
146
122
  homepage: https://mongoryhq.github.io/mongory-rb/
@@ -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