mongory 0.9.1 → 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.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +115 -420
  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 +68 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +14 -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 +132 -29
  27. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +1 -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 +18 -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 +4 -4
  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} +104 -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/generators/mongory/install/templates/initializer.rb.erb +0 -5
  45. data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
  46. data/lib/mongory/converters/converted.rb +28 -6
  47. data/lib/mongory/converters/key_converter.rb +2 -0
  48. data/lib/mongory/matcher.rb +63 -0
  49. data/lib/mongory/matchers/abstract_matcher.rb +18 -192
  50. data/lib/mongory/matchers.rb +68 -84
  51. data/lib/mongory/query_builder.rb +30 -41
  52. data/lib/mongory/utils/tree_renderer.rb +108 -0
  53. data/lib/mongory/utils.rb +2 -67
  54. data/lib/mongory/version.rb +1 -1
  55. data/lib/mongory.rb +9 -59
  56. data/script/bench.rb +54 -0
  57. metadata +13 -37
  58. data/lib/mongory/c_matcher.rb +0 -57
  59. data/lib/mongory/c_query_builder.rb +0 -44
  60. data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -150
  61. data/lib/mongory/matchers/and_matcher.rb +0 -73
  62. data/lib/mongory/matchers/array_record_matcher.rb +0 -105
  63. data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
  64. data/lib/mongory/matchers/eq_matcher.rb +0 -48
  65. data/lib/mongory/matchers/every_matcher.rb +0 -56
  66. data/lib/mongory/matchers/exists_matcher.rb +0 -53
  67. data/lib/mongory/matchers/field_matcher.rb +0 -160
  68. data/lib/mongory/matchers/gt_matcher.rb +0 -41
  69. data/lib/mongory/matchers/gte_matcher.rb +0 -41
  70. data/lib/mongory/matchers/hash_condition_matcher.rb +0 -80
  71. data/lib/mongory/matchers/in_matcher.rb +0 -84
  72. data/lib/mongory/matchers/literal_matcher.rb +0 -126
  73. data/lib/mongory/matchers/lt_matcher.rb +0 -41
  74. data/lib/mongory/matchers/lte_matcher.rb +0 -41
  75. data/lib/mongory/matchers/ne_matcher.rb +0 -39
  76. data/lib/mongory/matchers/nin_matcher.rb +0 -64
  77. data/lib/mongory/matchers/not_matcher.rb +0 -40
  78. data/lib/mongory/matchers/or_matcher.rb +0 -69
  79. data/lib/mongory/matchers/present_matcher.rb +0 -55
  80. data/lib/mongory/matchers/regex_matcher.rb +0 -109
  81. data/lib/mongory/matchers/size_matcher.rb +0 -54
  82. data/lib/mongory/query_matcher.rb +0 -93
  83. data/lib/mongory/rails.rb +0 -15
  84. data/lib/mongory/utils/debugger.rb +0 -125
  85. data/lib/mongory/utils/rails_patch.rb +0 -22
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # NinMatcher implements the `$nin` (not in) operator.
6
- #
7
- # It succeeds only if the record does not match any value in the condition array.
8
- # If the record is an array, it fails if any element overlaps with the condition.
9
- # If the record is a single value (including `nil`), it fails if it is included in the condition.
10
- #
11
- # @example Match single value
12
- # matcher = NinMatcher.build([1, 2, 3])
13
- # matcher.match?(4) #=> true
14
- # matcher.match?(2) #=> false
15
- #
16
- # @example Match nil
17
- # matcher = NinMatcher.build([nil])
18
- # matcher.match?(nil) #=> false
19
- #
20
- # @example Match with array
21
- # matcher = NinMatcher.build([2, 4])
22
- # matcher.match?([1, 3, 5]) #=> true
23
- # matcher.match?([4, 5]) #=> false
24
- #
25
- # @see AbstractMatcher
26
- class NinMatcher < InMatcher
27
- def self.build(condition, context: Context.new)
28
- return super unless condition.is_a?(Range)
29
-
30
- end_op = condition.exclude_end? ? '$gte' : '$gt'
31
- head, tail = [condition.first, condition.last].sort
32
- OrMatcher.build([{ '$lt' => head }, { end_op => tail }], context: context)
33
- end
34
-
35
- # Creates a raw Proc that performs the not-in matching operation.
36
- # The Proc checks if the record has no elements in common with the condition array.
37
- #
38
- # @return [Proc] A proc that performs not-in matching
39
- def raw_proc
40
- condition = build_condition_set
41
-
42
- Proc.new do |record|
43
- if record.is_a?(Array)
44
- is_blank?(condition & record)
45
- else
46
- !condition.include?(record)
47
- end
48
- end
49
- end
50
-
51
- # Ensures the condition is a valid array or range.
52
- #
53
- # @raise [TypeError] if the condition is not an array nor a range
54
- # @return [void]
55
- def check_validity!
56
- return if @condition.is_a?(Array)
57
-
58
- raise TypeError, '$nin needs an array or range'
59
- end
60
- end
61
-
62
- register(:nin, '$nin', NinMatcher)
63
- end
64
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # NotMatcher implements the `$not` logical operator.
6
- #
7
- # It returns true if the wrapped matcher fails, effectively inverting the result.
8
- #
9
- # It delegates to LiteralMatcher and simply negates the outcome.
10
- #
11
- # This allows constructs like:
12
- # { age: { :$not => { :$gte => 30 } } }
13
- #
14
- # @example
15
- # matcher = NotMatcher.build({ :$gte => 10 })
16
- # matcher.match?(5) #=> true
17
- # matcher.match?(15) #=> false
18
- #
19
- # @see LiteralMatcher
20
- class NotMatcher < LiteralMatcher
21
- # Creates a raw Proc that performs the not-matching operation.
22
- # The Proc inverts the result of the wrapped matcher.
23
- #
24
- # @return [Proc] A proc that performs not-matching
25
- def raw_proc
26
- super_proc = super
27
-
28
- Proc.new do |record|
29
- !super_proc.call(record)
30
- end
31
- end
32
-
33
- def priority
34
- 1 + super
35
- end
36
- end
37
-
38
- register(:not, '$not', NotMatcher)
39
- end
40
- end
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # OrMatcher implements the `$or` logical operator.
6
- #
7
- # It evaluates an array of subconditions and returns true
8
- # if *any one* of them matches. For empty conditions, it returns false
9
- # (using FALSE_PROC).
10
- #
11
- # Each subcondition is handled by a HashConditionMatcher with conversion disabled,
12
- # since the parent matcher already manages data conversion.
13
- #
14
- # This matcher inherits submatcher dispatch and evaluation logic
15
- # from AbstractMultiMatcher.
16
- #
17
- # @example
18
- # matcher = OrMatcher.build([
19
- # { age: { :$lt => 18 } },
20
- # { admin: true }
21
- # ])
22
- # matcher.match?(record) #=> true if either condition matches
23
- #
24
- # @example Empty conditions
25
- # matcher = OrMatcher.build([])
26
- # matcher.match?(record) #=> false (uses FALSE_PROC)
27
- #
28
- # @see AbstractMultiMatcher
29
- class OrMatcher < AbstractMultiMatcher
30
- enable_unwrap!
31
-
32
- # Creates a raw Proc that performs the or-matching operation.
33
- # The Proc combines all submatcher Procs and returns true if any match.
34
- # For empty conditions, returns FALSE_PROC.
35
- #
36
- # @return [Proc] a Proc that performs the or-matching operation
37
- def raw_proc
38
- combine_procs_with_or(*matchers.map(&:to_proc))
39
- end
40
-
41
- # Builds an array of matchers from the subconditions.
42
- # Each subcondition is wrapped in a HashConditionMatcher.
43
- #
44
- # @return [Array<AbstractMatcher>] array of submatchers
45
- define_instance_cache_method(:matchers) do
46
- children = @condition.map do |condition|
47
- HashConditionMatcher.build(condition, context: @context)
48
- end
49
- sort_and_adopt(children)
50
- end
51
-
52
- # Ensures the condition is an array of hashes.
53
- #
54
- # @raise [Mongory::TypeError] if not valid
55
- # @return [void]
56
- def check_validity!
57
- raise TypeError, '$or needs an array' unless @condition.is_a?(Array)
58
-
59
- @condition.each do |sub_condition|
60
- raise TypeError, '$or needs an array of hash' unless sub_condition.is_a?(Hash)
61
- end
62
-
63
- super
64
- end
65
- end
66
-
67
- register(:or, '$or', OrMatcher)
68
- end
69
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # PresentMatcher implements the `$present` operator.
6
- #
7
- # It returns true if the record value is considered "present"
8
- # (i.e., not nil, not empty, not KEY_NOT_FOUND), and matches
9
- # the expected boolean condition.
10
- #
11
- # This is similar to `$exists`, but evaluates truthiness
12
- # of the value instead of mere existence.
13
- #
14
- # @example
15
- # matcher = PresentMatcher.build(true)
16
- # matcher.match?('hello') #=> true
17
- # matcher.match?(nil) #=> false
18
- # matcher.match?([]) #=> false
19
- #
20
- # matcher = PresentMatcher.build(false)
21
- # matcher.match?(nil) #=> true
22
- #
23
- # @see AbstractMatcher
24
- class PresentMatcher < AbstractMatcher
25
- # Creates a raw Proc that performs the presence check.
26
- # The Proc checks if the record's presence matches the condition.
27
- #
28
- # @return [Proc] A proc that performs presence check
29
- def raw_proc
30
- condition = @condition
31
-
32
- Proc.new do |record|
33
- record = nil if record == KEY_NOT_FOUND
34
- is_present?(record) == condition
35
- end
36
- end
37
-
38
- def priority
39
- 2
40
- end
41
-
42
- # Ensures that the condition value is a boolean.
43
- #
44
- # @raise [TypeError] if condition is not true or false
45
- # @return [void]
46
- def check_validity!
47
- return if [true, false].include?(@condition)
48
-
49
- raise TypeError, '$present needs a boolean'
50
- end
51
- end
52
-
53
- register(:present, '$present', PresentMatcher)
54
- end
55
- end
@@ -1,109 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # RegexMatcher implements the `$regex` operator and also handles raw Regexp values.
6
- #
7
- # This matcher checks whether a string record matches a regular expression.
8
- # It supports both:
9
- # - Explicit queries using `:field.regex => /pattern/i`
10
- # - Implicit literal Regexp values like `{ field: /pattern/i }`
11
- #
12
- # If a string is provided instead of a Regexp, it will be converted via `Regexp.new(...)`.
13
- # This ensures consistent behavior for queries like `:field.regex => "foo"` and `:field.regex => /foo/`.
14
- #
15
- # @example Basic regex matching
16
- # matcher = RegexMatcher.build(/^foo/)
17
- # matcher.match?('foobar') #=> true
18
- # matcher.match?('barfoo') #=> false
19
- #
20
- # @example Case-insensitive matching
21
- # matcher = RegexMatcher.build(/admin/i)
22
- # matcher.match?("ADMIN") #=> true
23
- #
24
- # @example String pattern
25
- # matcher = RegexMatcher.build("^foo")
26
- # matcher.match?("foobar") #=> true
27
- #
28
- # @example Non-string input
29
- # matcher = RegexMatcher.build(/\d+/)
30
- # matcher.match?(123) #=> false (not a string)
31
- #
32
- # @see AbstractMatcher
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
-
63
- # Initializes the matcher with a regex pattern.
64
- # Converts string patterns to Regexp objects.
65
- #
66
- # @param condition [String, Regexp] the regex pattern to match against
67
- # @param context [Context] the query context
68
- # @raise [TypeError] if condition is not a string or Regexp
69
- def initialize(condition, context: Context.new)
70
- super
71
- @condition = Regexp.new(condition) if condition.is_a?(String)
72
- end
73
-
74
- # Creates a raw Proc that performs the regex matching operation.
75
- # The Proc checks if the record is a string that matches the pattern.
76
- # Returns false for non-string inputs or if the match fails.
77
- #
78
- # @return [Proc] a Proc that performs regex matching
79
- def raw_proc
80
- condition = @condition
81
-
82
- Proc.new do |record|
83
- next false unless record.is_a?(String)
84
-
85
- record.match?(condition)
86
- rescue StandardError
87
- false
88
- end
89
- end
90
-
91
- def priority
92
- @condition.source.start_with?('^') ? 8 : 20
93
- end
94
-
95
- # Ensures the condition is a valid regex pattern (Regexp or String).
96
- #
97
- # @raise [TypeError] if condition is not a string or Regexp
98
- # @return [void]
99
- def check_validity!
100
- return if @condition.is_a?(Regexp)
101
- return if @condition.is_a?(String)
102
-
103
- raise TypeError, '$regex needs a Regexp or string'
104
- end
105
- end
106
-
107
- register(:regex, '$regex', RegexMatcher)
108
- end
109
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # Matcher for the `$size` operator.
6
- #
7
- # This matcher expects the input to be an array, and delegates the comparison
8
- # to a literal matcher using the array's size as the value.
9
- #
10
- # For example, the condition `{ tags: { '$size' => 3 } }` will match any
11
- # document where `tags` is an array of length 3.
12
- #
13
- # ### Supported compound usages:
14
- #
15
- # ```ruby
16
- # Mongory.where(tags: { '$size' => 3 }) # exactly 3 elements
17
- # Mongory.where(tags: { '$size' => { '$gt' => 1 } }) # more than 1
18
- # Mongory.where(comments: { '$size' => { '$gt' => 1, '$lte' => 5 } }) # more than 1, up to 5 elements
19
- # Mongory.where(tags: { '$size' => { '$in' => [1, 2, 3] } }) # 1, 2, or 3 elements
20
- # ```
21
- #
22
- # @see LiteralMatcher
23
- #
24
- # @note Ruby's Symbol class already defines a `#size` method,
25
- # that will return the size of the symbol object.
26
- # So, this is the only operator that cannot be used with
27
- # the symbol snippet syntax (e.g. `:tags.size`).
28
- #
29
- # Use string key syntax instead: `:"tags.$size" => ...`
30
- class SizeMatcher < LiteralMatcher
31
- # Creates a raw Proc that performs the size matching operation.
32
- #
33
- # The returned Proc checks if the input is an Array. If so, it calculates
34
- # the array's size and passes it to the wrapped literal matcher Proc.
35
- #
36
- # @return [Proc] A proc that performs size-based matching
37
- def raw_proc
38
- super_proc = super
39
-
40
- Proc.new do |record|
41
- next false unless record.is_a?(Array)
42
-
43
- super_proc.call(record.size)
44
- end
45
- end
46
-
47
- def priority
48
- 2 + super
49
- end
50
- end
51
-
52
- register(:size, '$size', SizeMatcher)
53
- end
54
- end
@@ -1,93 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'utils'
4
-
5
- module Mongory
6
- # The top-level matcher for compiled query conditions.
7
- #
8
- # Delegates to {Matchers::LiteralMatcher} after transforming input
9
- # via {Converters::ConditionConverter}.
10
- #
11
- # Typically used internally by `QueryBuilder`.
12
- #
13
- # Conversion via Mongory.data_converter is applied to the record
14
- # before matching to ensure consistent data types.
15
- #
16
- # @example Basic matching
17
- # matcher = QueryMatcher.new({ :age.gte => 18 })
18
- # matcher.match?(record) # => true/false
19
- #
20
- # @example Complex condition
21
- # matcher = QueryMatcher.new({
22
- # :age.gte => 18,
23
- # :$or => [
24
- # { :name => /J/ },
25
- # { :name.eq => 'Bob' }
26
- # ]
27
- # })
28
- #
29
- # @see Matchers::LiteralMatcher
30
- # @see Converters::ConditionConverter
31
- class QueryMatcher < Matchers::HashConditionMatcher
32
- # Initializes a new query matcher with the given condition.
33
- # The condition is converted using Mongory.condition_converter
34
- # before being passed to the parent matcher.
35
- #
36
- # @param condition [Hash<Symbol, Object>] a query condition using operator-style symbol keys,
37
- # e.g. { :age.gt => 18 }, which will be parsed by `Mongory.condition_converter`.
38
- # @param context [Context] The query context containing configuration and current record
39
- # @option context [Hash] :config The query configuration
40
- # @option context [Object] :current_record The current record being processed
41
- # @option context [Boolean] :need_convert Whether the record needs to be converted
42
- def initialize(condition, context: Utils::Context.new)
43
- super(Mongory.condition_converter.convert(condition), context: context)
44
- end
45
-
46
- # Returns a Proc that can be used for fast matching.
47
- # The Proc converts the record using Mongory.data_converter
48
- # and delegates to the superclass's raw_proc.
49
- #
50
- # @return [Proc] A proc that performs query matching with context awareness
51
- # @note The proc includes error handling and context-based record conversion
52
- def raw_proc
53
- super_proc = super
54
- need_convert = @context.need_convert
55
- data_converter = Mongory.data_converter
56
-
57
- Proc.new do |record|
58
- record = data_converter.convert(record) if need_convert
59
- super_proc.call(record)
60
- rescue StandardError
61
- false
62
- end
63
- end
64
-
65
- # Renders the full matcher tree for the current query.
66
- # This method is intended to be the public entry point for rendering
67
- # the matcher tree. It does not accept any arguments and internally
68
- # handles rendering via the configured pretty-print logic.
69
- #
70
- # Subclasses or internal matchers should implement their own
71
- # `#render_tree(prefix, is_last:)` for internal recursion.
72
- #
73
- # @return [void]
74
- # @see Matchers::LiteralMatcher#render_tree
75
- def render_tree
76
- super
77
- end
78
-
79
- # Prepares the query for execution by ensuring all necessary matchers are initialized.
80
- # This is called before query execution to avoid premature matcher tree construction.
81
- #
82
- # @return [void]
83
- alias_method :prepare_query, :check_validity!
84
-
85
- # Overrides the parent class's check_validity! to prevent premature matcher tree construction.
86
- # This matcher does not require validation, so this is a no-op.
87
- #
88
- # @return [void]
89
- def check_validity!
90
- # No-op for this matcher
91
- end
92
- end
93
- end
data/lib/mongory/rails.rb DELETED
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails'
4
- require_relative 'utils/rails_patch'
5
-
6
- module Mongory
7
- # @see Utils::RailsPatch
8
- class Railtie < Rails::Railtie
9
- initializer 'mongory.patch_utils' do
10
- [Mongory::Utils, *Mongory::Utils.included_classes].each do |klass|
11
- klass.prepend(Mongory::Utils::RailsPatch)
12
- end
13
- end
14
- end
15
- end
@@ -1,125 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Utils
5
- # Debugger provides an internal tracing system for query matching.
6
- # It tracks matcher evaluation in a tree structure and provides tree-like format output.
7
- #
8
- # It captures each matcher evaluation with indentation and visualizes the hierarchy,
9
- # helping developers understand nested matcher flows (e.g. `$and`, `$or`, etc).
10
- #
11
- # Usage:
12
- # Debugger.instance.with_indent { ... } # wraps around matcher logic
13
- # Debugger.instance.display # prints trace tree after execution
14
- #
15
- # Note:
16
- # The trace is recorded in post-order (leaf nodes enter first),
17
- # and `reorder_traces_for_display` is used to transform it into a
18
- # pre-order format suitable for display.
19
- #
20
- # This class is a singleton and should be accessed via `Debugger.instance`.
21
- #
22
- # @example Enable debugging
23
- # Debugger.instance.enable
24
- # Mongory::QueryBuilder.new(...).filter(...)
25
- #
26
- class Debugger < SingletonBuilder
27
- include Singleton
28
-
29
- def initialize
30
- super(self.class.name)
31
- @indent_level = -1
32
- @trace_entries = []
33
- end
34
-
35
- # Enables debug mode by aliasing `to_proc` to `debug_proc`.
36
- # @return [void]
37
- # @note Changes the behavior of to_proc to use debug_proc instead of cached_proc
38
- def enable
39
- Matchers::AbstractMatcher.alias_method :to_proc, :debug_proc
40
- end
41
-
42
- # Disables debug mode by restoring `to_proc` to `cached_proc`.
43
- # @return [void]
44
- # @note Restores the original behavior of to_proc
45
- def disable
46
- Matchers::AbstractMatcher.alias_method :to_proc, :cached_proc
47
- end
48
-
49
- # Wraps a matcher evaluation block with indentation control.
50
- #
51
- # It increments the internal indent level before the block,
52
- # and decrements it after. The yielded block's return value
53
- # is used as trace content and pushed onto the trace_entries.
54
- #
55
- # @yieldreturn [String] the trace content to log
56
- # @return [Object] the result of the block
57
- def with_indent
58
- @indent_level += 1
59
- display_string = yield
60
- @trace_entries << TraceEntry.new(display_string, @indent_level)
61
- ensure
62
- @indent_level -= 1
63
- end
64
-
65
- # Prints the visualized trace tree to STDOUT.
66
- #
67
- # This processes the internal trace_entries (post-order) into
68
- # a structured pre-order list that represents nested matcher evaluation.
69
- # @return [void]
70
- def display
71
- reorder_traces_for_display(@trace_entries).each do |trace|
72
- puts trace.formatted
73
- end
74
-
75
- @trace_entries.clear
76
- nil
77
- end
78
-
79
- # Recursively reorders trace lines by indentation level to produce a
80
- # display-friendly structure where parents precede children.
81
- #
82
- # The original trace is built in post-order (leaf first), and this method
83
- # transforms it into a pre-order structure suitable for tree display.
84
- #
85
- # @param traces [Array<TraceEntry>] the raw trace lines (leaf-first)
86
- # @param level [Integer] current processing level
87
- # @return [Array<TraceEntry>] reordered trace lines for display
88
- def reorder_traces_for_display(traces, level = 0)
89
- result = []
90
- group = []
91
- traces.each do |trace|
92
- if trace.level == level
93
- result << trace
94
- result.concat reorder_traces_for_display(group, level + 1)
95
- group = []
96
- else
97
- group << trace
98
- end
99
- end
100
-
101
- result
102
- end
103
-
104
- def clear
105
- # Clears the internal trace buffer.
106
- # This is useful when re-running a query and avoiding stale trace output.
107
- # @return [void]
108
- @trace_entries.clear
109
- end
110
-
111
- # @private
112
- # A trace entry representing a single matcher output at a given indentation level.
113
- #
114
- # @!attribute text
115
- # @return [String] the string to be printed
116
- # @!attribute level
117
- # @return [Integer] the indentation level of this entry
118
- TraceEntry = Struct.new(:text, :level) do
119
- def formatted
120
- "#{' ' * level}#{text}"
121
- end
122
- end
123
- end
124
- end
125
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Utils
5
- # Only loaded when Rails is present
6
- module RailsPatch
7
- # Use Object#present? which defined in Rails.
8
- # @param target[Object]
9
- # @return [Boolean]
10
- def is_present?(target)
11
- target.present?
12
- end
13
-
14
- # Use Object#blank? which defined in Rails.
15
- # @param target[Object]
16
- # @return [Boolean]
17
- def is_blank?(target)
18
- target.blank?
19
- end
20
- end
21
- end
22
- end