mongory 0.9.1-aarch64-linux → 2.0.1-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 (98) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +115 -420
  4. data/SUBMODULE_INTEGRATION.md +7 -20
  5. data/docs/clang_bridge.md +28 -63
  6. data/docs/migration.md +17 -0
  7. data/docs/performance.md +14 -51
  8. data/examples/benchmark.rb +10 -26
  9. data/ext/mongory_ext/extconf.rb +1 -1
  10. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +62 -0
  11. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +31 -0
  12. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +3 -0
  13. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +20 -0
  14. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +31 -4
  15. data/ext/mongory_ext/mongory-core/src/foundations/array.c +48 -6
  16. data/ext/mongory_ext/mongory-core/src/foundations/config.c +30 -2
  17. data/ext/mongory_ext/mongory-core/src/foundations/error.c +10 -0
  18. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +78 -19
  19. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +73 -19
  20. data/ext/mongory_ext/mongory-core/src/foundations/table.c +8 -3
  21. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +44 -55
  22. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +7 -0
  23. data/ext/mongory_ext/mongory-core/src/foundations/value.c +3 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +68 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +14 -0
  26. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +9 -4
  27. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +132 -29
  28. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +1 -1
  29. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.c → existence_matcher.c} +10 -12
  30. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.h → existence_matcher.h} +4 -4
  31. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -6
  32. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.c → implicit_array_matcher.c} +57 -38
  33. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.h → implicit_array_matcher.h} +5 -5
  34. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +8 -10
  35. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +337 -96
  36. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +59 -21
  37. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +7 -1
  38. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +4 -4
  39. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -1
  40. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.c → value_matcher.c} +104 -60
  41. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.h → value_matcher.h} +13 -13
  42. data/ext/mongory_ext/mongory_ext.c +2 -2
  43. data/ext/mongory_ext/mongory_ext_internal.h +2 -3
  44. data/ext/mongory_ext/{rb_mongory_cmatcher.c → rb_mongory_matcher.c} +145 -91
  45. data/ext/mongory_ext/rb_mongory_value.c +67 -0
  46. data/lib/core/2.6/mongory_ext.so +0 -0
  47. data/lib/core/2.7/mongory_ext.so +0 -0
  48. data/lib/core/3.0/mongory_ext.so +0 -0
  49. data/lib/core/3.1/mongory_ext.so +0 -0
  50. data/lib/core/3.2/mongory_ext.so +0 -0
  51. data/lib/core/3.3/mongory_ext.so +0 -0
  52. data/lib/core/3.4/mongory_ext.so +0 -0
  53. data/lib/core/4.0/mongory_ext.so +0 -0
  54. data/lib/generators/mongory/install/templates/initializer.rb.erb +0 -5
  55. data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
  56. data/lib/mongory/converters/condition_converter.rb +1 -1
  57. data/lib/mongory/converters/converted.rb +28 -6
  58. data/lib/mongory/converters/key_converter.rb +2 -0
  59. data/lib/mongory/matcher.rb +50 -0
  60. data/lib/mongory/matchers/abstract_matcher.rb +18 -192
  61. data/lib/mongory/matchers.rb +68 -84
  62. data/lib/mongory/query_builder.rb +29 -42
  63. data/lib/mongory/utils/context.rb +10 -15
  64. data/lib/mongory/utils/tree_renderer.rb +98 -0
  65. data/lib/mongory/utils.rb +2 -67
  66. data/lib/mongory/version.rb +1 -1
  67. data/lib/mongory.rb +9 -59
  68. data/script/bench.rb +54 -0
  69. data/scripts/build_with_core.sh +0 -1
  70. metadata +13 -37
  71. data/lib/mongory/c_matcher.rb +0 -57
  72. data/lib/mongory/c_query_builder.rb +0 -44
  73. data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -150
  74. data/lib/mongory/matchers/and_matcher.rb +0 -73
  75. data/lib/mongory/matchers/array_record_matcher.rb +0 -105
  76. data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
  77. data/lib/mongory/matchers/eq_matcher.rb +0 -48
  78. data/lib/mongory/matchers/every_matcher.rb +0 -56
  79. data/lib/mongory/matchers/exists_matcher.rb +0 -53
  80. data/lib/mongory/matchers/field_matcher.rb +0 -160
  81. data/lib/mongory/matchers/gt_matcher.rb +0 -41
  82. data/lib/mongory/matchers/gte_matcher.rb +0 -41
  83. data/lib/mongory/matchers/hash_condition_matcher.rb +0 -80
  84. data/lib/mongory/matchers/in_matcher.rb +0 -84
  85. data/lib/mongory/matchers/literal_matcher.rb +0 -126
  86. data/lib/mongory/matchers/lt_matcher.rb +0 -41
  87. data/lib/mongory/matchers/lte_matcher.rb +0 -41
  88. data/lib/mongory/matchers/ne_matcher.rb +0 -39
  89. data/lib/mongory/matchers/nin_matcher.rb +0 -64
  90. data/lib/mongory/matchers/not_matcher.rb +0 -40
  91. data/lib/mongory/matchers/or_matcher.rb +0 -69
  92. data/lib/mongory/matchers/present_matcher.rb +0 -55
  93. data/lib/mongory/matchers/regex_matcher.rb +0 -109
  94. data/lib/mongory/matchers/size_matcher.rb +0 -54
  95. data/lib/mongory/query_matcher.rb +0 -93
  96. data/lib/mongory/rails.rb +0 -15
  97. data/lib/mongory/utils/debugger.rb +0 -125
  98. data/lib/mongory/utils/rails_patch.rb +0 -22
@@ -52,7 +52,7 @@ sudo yum groupinstall "Development Tools" || sudo dnf groupinstall "Development
52
52
  sudo yum install ruby-devel || sudo dnf install ruby-devel
53
53
  ```
54
54
 
55
- > Optional: CMake and cJSON are only needed if you want to run the core tests/benchmarks inside the `mongory-core` submodule (e.g., via CMake/ctest).
55
+ > Optional: CMake is only needed if you want to run the core C tests/benchmarks inside the `mongory-core` submodule (e.g., via CMake/ctest).
56
56
 
57
57
  ### 3. Build the project
58
58
 
@@ -97,7 +97,6 @@ If you prefer to control the build manually:
97
97
  git submodule update --init --recursive
98
98
 
99
99
  # 2. (Optional) Only needed if you run core tests/benchmarks inside the submodule
100
- # Note: This step requires cJSON; typical mongory-rb usage does not.
101
100
  # cd ext/mongory_ext/mongory-core
102
101
  # ./build.sh --test
103
102
  # cd ../../..
@@ -177,10 +176,10 @@ Debug mode will:
177
176
  require 'mongory'
178
177
 
179
178
  condition = { "age" => { "$gt" => 18 } }
180
- matcher = Mongory::CMatcher.new(condition)
179
+ matcher = Mongory::Matcher.new(condition)
181
180
 
182
181
  data = { "name" => "John", "age" => 25 }
183
- result = matcher.match(data) # => true/false
182
+ result = matcher.match?(data) # => true/false
184
183
 
185
184
  # Optional: get explanation (runs explain on the C side, currently returns nil)
186
185
  matcher.explain
@@ -196,26 +195,14 @@ matcher.explain
196
195
  git submodule update --init --recursive
197
196
  ```
198
197
 
199
- **2. cJSON library not found (only required if you run mongory-core CMake tests)**
200
- ```bash
201
- # macOS
202
- brew install cjson
203
-
204
- # Ubuntu/Debian
205
- sudo apt install libcjson-dev
206
-
207
- # Verify installation
208
- pkg-config --exists libcjson && echo "Found" || echo "Not found"
209
- ```
210
-
211
- **3. Build errors**
198
+ **2. Build errors**
212
199
  ```bash
213
200
  # Clean and rebuild
214
201
  ./scripts/build_with_core.sh --clean
215
202
  ./scripts/build_with_core.sh --debug
216
203
  ```
217
204
 
218
- **4. C extension fails to load**
205
+ **3. C extension fails to load**
219
206
  - Check Ruby version compatibility (>= 2.6.0)
220
207
  - Ensure all system deps are installed
221
208
  - Inspect error messages and compiler warnings
@@ -281,8 +268,8 @@ jobs:
281
268
  run: bundle exec rspec
282
269
 
283
270
  # Only needed if you also run mongory-core C tests/benchmarks (optional):
284
- # - name: Install CMake & cJSON (only if running core tests)
285
- # run: sudo apt install -y cmake libcjson-dev
271
+ # - name: Install CMake (only if running core tests)
272
+ # run: sudo apt install -y cmake
286
273
  # - name: Build mongory-core tests (optional)
287
274
  # run: |
288
275
  # cd ext/mongory_ext/mongory-core
data/docs/clang_bridge.md CHANGED
@@ -1,54 +1,28 @@
1
- ### C Extension (Optional but Recommended)
1
+ # Clang Bridge (Native C99 Engine)
2
2
 
3
- Mongory-rb includes an optional high-performance C extension powered by [mongory-core](https://github.com/mongoryhq/mongory-core):
3
+ As of `mongory-rb v2.0.0`, Mongory uses the C99 [`mongory-core`](https://github.com/mongoryhq/mongory-core) native extension as its sole matching engine.
4
4
 
5
- **System Dependencies:**
6
- - C99-compatible compiler (gcc/clang)
5
+ **System Dependencies (only when compiling from source):**
6
+ - C99-compatible compiler (`gcc` / `clang`)
7
7
  - CMake >= 3.12 (optional; only needed if you want to build `mongory-core` standalone or run its native tests)
8
8
 
9
- **Installation:**
10
- ```bash
11
- # macOS
12
- brew install cmake
13
-
14
- # Ubuntu/Debian
15
- sudo apt install cmake build-essential
16
-
17
- # CentOS/RHEL
18
- sudo yum install cmake gcc make
19
- ```
9
+ > Note: Precompiled native gems are distributed for Linux, macOS (`arm64` & `x86_64`), and Windows (Ruby 2.6–4.0). When compiling from source, Ruby's `mkmf` (`ext/mongory_ext/extconf.rb`) compiles `mongory-core` sources directly — CMake is not required for gem installation.
20
10
 
21
- The C extension provides significant performance improvements for large datasets. If not available, Mongory-rb automatically falls back to pure Ruby implementation.
11
+ ## Architecture
22
12
 
23
- Note: The Ruby C extension is built via Ruby's `mkmf` (see `ext/mongory_ext/extconf.rb`) and compiles `mongory-core` sources directly. You do not need CMake for normal gem installation.
13
+ The Clang bridge connects the Ruby DSL to `mongory-core` via a compact C layer exposing two key entry points:
24
14
 
25
- # Clang Bridge (C Extension)
15
+ - `Mongory::Matcher`: the compiled matcher class backed by `mongory-core`.
16
+ - `Mongory::QueryBuilder`: the chainable Ruby DSL (`records.mongory.where(...)`) backed by `Mongory::Matcher`.
26
17
 
27
- The Clang bridge connects the Ruby DSL to the `mongory-core` engine via a compact C layer. It exposes two key entry points:
28
-
29
- - `Mongory::CMatcher`: a low-level matcher API backed by C.
30
- - `QueryBuilder#c`: an ergonomic switch that reuses your current Ruby condition and executes it through `CMatcher`.
31
-
32
- ## Build/Install
18
+ ## Build / Install
33
19
 
34
20
  ```bash
35
21
  bundle install
36
22
  bundle exec rake compile
37
- # or, when installing the gem, the extension will compile automatically if toolchain is present
38
- ```
39
-
40
- ## Quick check
41
-
42
- ```ruby
43
- require 'mongory'
44
- if defined?(Mongory::CMatcher)
45
- puts 'C extension available'
46
- else
47
- puts 'C extension not available, using pure Ruby'
48
- end
49
23
  ```
50
24
 
51
- ## Basic usage
25
+ ## Basic Usage
52
26
 
53
27
  ```ruby
54
28
  records = [
@@ -57,43 +31,34 @@ records = [
57
31
  { 'name' => 'Bob', 'age' => 21 }
58
32
  ]
59
33
 
60
- # Switch existing Ruby query to C path
61
- query = records.mongory.c # => returns Mongory::CQueryBuilder
62
- .where(:age.gte => 18)
34
+ # Chainable query builder (uses Mongory::Matcher internally)
35
+ query = records.mongory.where(:age.gte => 18)
63
36
 
64
- query.each.to_a # enumerate matches via C
65
- query.fast.to_a # alias of each
66
- query.trace.to_a # print value compare progression
67
- query.explain # print core-level matcher tree
37
+ query.each.to_a # enumerate matches via mongory-core
38
+ query.explain_tree # return structured matcher tree Hash
39
+ query.explain # print matcher tree to STDOUT
40
+ query.trace_tree # return Array<Hash> of structured per-record trace trees (with recursive :skip)
41
+ query.trace.to_a # print trace trees to STDOUT and yield matches
68
42
 
69
- # Or use CMatcher directly
70
- matcher = Mongory::CMatcher.new(:age.gte => 18)
43
+ # Or use Mongory::Matcher directly
44
+ matcher = Mongory::Matcher.new(:age.gte => 18)
71
45
  records.select { |r| matcher.match?(r) }
72
-
73
- # Or enable the C extension globally by default in Mongory.configure:
74
- Mongory.configure do |mc|
75
- mc.enable_c_extension_by_default!
76
- end
77
- records.mongory.where(:age.gte => 18).to_a # automatically uses CQueryBuilder
78
46
  ```
79
47
 
80
- ## Tracing and debugging
48
+ ## Tracing and Debugging
81
49
 
82
50
  ```ruby
83
- matcher = Mongory::CMatcher.new(:$or => [ { :name.regex => /^J/ }, { :age.gt => 20 } ])
84
- matcher.enable_trace
85
- records.each { |r| matcher.match?(r) }
86
- matcher.print_trace # prints detailed trace
87
- matcher.disable_trace
51
+ matcher = Mongory::Matcher.new(:$or => [{ :name.regex => /^J/ }, { :age.gt => 20 }])
88
52
 
89
- # Or trace single record compare progression
53
+ # Structured explain/trace trees on a single Matcher (stateless & thread-safe)
54
+ plan = matcher.explain_tree # => Hash ({ name:, condition:, priority:, children:, ... })
55
+ tree = matcher.trace_tree(records.first) # => Hash ({ ..., record:, result: :pass | :fail | :skip })
56
+
57
+ # Print single-record trace tree to STDOUT and return boolean match result
90
58
  matcher.trace(records.first)
91
59
  ```
92
60
 
93
61
  ## Notes
94
62
 
95
63
  - Regexes use Ruby's `Regexp` internally; string patterns are compiled once and cached.
96
- - Context (`Mongory::Utils::Context`) is shared between Ruby and C during matching, enabling custom converters.
97
- - If the extension fails to load, `Mongory::CQueryBuilder` is unavailable and `.c` will not be used; the Ruby path continues to work.
98
-
99
-
64
+ - Context (`Mongory::Utils::Context`) is shared between Ruby and C during matching, enabling custom converters and custom Ruby matchers (`Mongory::Matchers::AbstractMatcher`).
data/docs/migration.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Migration Guide
2
2
 
3
+ ## Upgrading from `mongory` v0.x / v1.x to v2.0.0
4
+
5
+ `mongory-rb v2.0.0` removes the Pure-Ruby built-in matcher engine and unifies on the C99 `mongory-core` native extension as the sole engine.
6
+
7
+ | v0.x / v1.x | v2.0.0 |
8
+ |---|---|
9
+ | `Mongory::CMatcher` / `Mongory::QueryMatcher` | `Mongory::Matcher` |
10
+ | `Mongory::CQueryBuilder` | `Mongory::QueryBuilder` (now backed by `Mongory::Matcher` directly) |
11
+ | `records.mongory.c.where(...)` | `records.mongory.where(...)` |
12
+ | `Mongory.enable_c_extension_by_default!` / `disable_c_extension_by_default!` / `c_extension_by_default?` | Removed (C engine is always active) |
13
+ | `Mongory.debugger` / `Mongory::Utils::Debugger` | Removed (use `#explain`, `#explain_tree`, `#trace`, `#trace_tree`) |
14
+ | Pure-Ruby fallback when `mongory_ext` is missing | Removed (`require 'mongory'` raises `LoadError` if native extension is missing) |
15
+
16
+ Custom Ruby matchers subclassing `Mongory::Matchers::AbstractMatcher`, `Mongory::Matchers.register`, `Mongory.enable_symbol_snippets!`, and all `Converters` remain supported without changes.
17
+
18
+ ---
19
+
3
20
  ## From Array#select
4
21
  ```ruby
5
22
  # Before
data/docs/performance.md CHANGED
@@ -1,61 +1,24 @@
1
1
  # Performance & Benchmarks
2
2
 
3
- ## C Extension vs Pure Ruby
3
+ ## Reproducible Benchmarks
4
4
 
5
- - C extension provides 3-10x performance improvement for large datasets
6
- - Automatic fallback to pure Ruby if C extension unavailable
7
- - Check availability: `defined?(Mongory::CMatcher)` or attempt to require the extension
8
- - Memory management handled by mongory-core's memory pool
5
+ Run the included benchmark script in `mongory-rb` to measure `Mongory::Matcher` on your local Ruby runtime:
9
6
 
10
- ## Memory Usage
11
-
12
- - Mongory operates entirely in memory
13
- - Consider your data size and memory constraints
14
- - Proc-based implementation reduces memory usage
15
- - Context system provides better memory management
16
-
17
- ## Query Optimization
18
-
19
- - Complex conditions are evaluated in sequence
20
- - Use `explain` to analyze query performance
21
- - Empty conditions are optimized with cached Procs
22
- - Context system allows fine-grained control over conversion
23
-
24
- ## Benchmarks
25
-
26
- ```ruby
27
- # Plain Ruby Simple query (100000 records)
28
- records.select { |r| r['age'].is_a?(Numeric) && r['age'] >= 18 } # ~9ms
29
-
30
- # Plain Ruby Complex query (100000 records)
31
- records.select do |r|
32
- next false unless r.key?('age') && r.key?('status')
33
-
34
- r['age'].is_a?(Numeric) && r['age'] >= 18 || r['status'] == 'active'
35
- end # ~20ms
36
-
37
- # Simple query (100000 records)
38
- records.mongory.where(:age.gte => 18) # ~119ms
39
-
40
- # Complex query (100000 records)
41
- records.mongory.where(:$or => [{:age.gte => 18}, {:status => 'active'}]) # ~107ms
42
-
43
- # Complex query with fast mode (100000 records)
44
- records.mongory.where(:$or => [{:age.gte => 18}, {:status => 'active'}]).fast # ~63ms
45
-
46
- # Simple query with C extension (100000 records)
47
- records.mongory.c.where(:age.gte => 18) # ~15ms
48
- # Complex query with C extension (100000 records)
49
- records.mongory.c.where(:$or => [{:age.gte => 18}, {:status => 'active'}]) # ~23ms, same with plain ruby
7
+ ```bash
8
+ ruby script/bench.rb
50
9
  ```
51
10
 
52
- Note: Performance varies based on:
11
+ ### Measured Results (Apple M3, macOS arm64, Ruby `4.0.5`, 100,000 records)
53
12
 
54
- - Data size
55
- - Query complexity
56
- - Hardware specifications
57
- - Ruby version
13
+ | Engine / Case | Simple (`age >= 18`) | `$or` (`age >= 18` or `status == "active"`) |
14
+ |---|---|---|
15
+ | Plain Ruby `Array#count` | `7.26 ms` (`72.6 ns/rec`) | `6.85 ms` (`68.5 ns/rec`) |
16
+ | `Mongory::Matcher` (`mongory-core` C99) | `7.67 ms` (`76.7 ns/rec`) | `12.92 ms` (`129.2 ns/rec`) |
58
17
 
59
- Benchmark in your environment to validate.
18
+ > For cross-language benchmarks (`mongory-js`, `mongory-go`), see [Mongory Central](https://mongoryhq.github.io/mongory-central/#benchmarks).
60
19
 
20
+ ## Optimization Tips
61
21
 
22
+ - **Reuse `Mongory::Matcher` Instances**: Compile once with `Mongory::Matcher.new(condition)` and call `#match?` across records to avoid recompiling the condition tree.
23
+ - **Priority Ordering & Dynamic Re-Sorting**: Mongory automatically orders cheaper equality/existence checks before regex or array scans, and re-sorts siblings if a lazy array matcher is constructed.
24
+ - **Inspect Plans**: Use `.explain` / `.trace` to verify cheap fields short-circuit early.
@@ -59,10 +59,10 @@ end
59
59
  raise "count mismatch" if builder.to_a.count != count_of_simple_query
60
60
  end
61
61
 
62
- # Simple query (Mongory::CMatcher) test
63
- puts "\nSimple query (Mongory::CMatcher) (#{size} records):"
62
+ # Simple query (Mongory::Matcher) test
63
+ puts "\nSimple query (Mongory::Matcher) (#{size} records):"
64
64
  gc_handler do
65
- matcher = Mongory::CMatcher.new({:age.gte => 18})
65
+ matcher = Mongory::Matcher.new({:age.gte => 18})
66
66
  5.times do
67
67
  result = Benchmark.measure do
68
68
  records.select { |r| matcher.match?(r) }
@@ -72,10 +72,10 @@ end
72
72
  raise "count mismatch" if records.count { |r| matcher.match?(r) } != count_of_simple_query
73
73
  end
74
74
 
75
- # Simple query (Mongory::CQueryBuilder) test
76
- puts "\nSimple query (Mongory::CQueryBuilder) (#{size} records):"
75
+ # Simple query (Mongory::QueryBuilder) test
76
+ puts "\nSimple query (Mongory::QueryBuilder) (#{size} records):"
77
77
  gc_handler do
78
- builder = Mongory::CQueryBuilder.new(records).where({:age.gte => 18})
78
+ builder = Mongory::QueryBuilder.new(records).where({:age.gte => 18})
79
79
  5.times do
80
80
  result = Benchmark.measure do
81
81
  builder.to_a
@@ -120,25 +120,9 @@ end
120
120
  raise "count mismatch" if builder.count != count_of_complex_query
121
121
  end
122
122
 
123
- # Complex query (Mongory) test
124
- puts "\nComplex query (Mongory) fast mode (#{size} records):"
123
+ puts "\nComplex query (Mongory) use Matcher (#{size} records):"
125
124
  gc_handler do
126
- builder = records.mongory.or(
127
- { :age.gte => 18 },
128
- { status: 'active' }
129
- ).fast
130
- 5.times do
131
- result = Benchmark.measure do
132
- builder.to_a
133
- end
134
- puts result
135
- end
136
- raise "count mismatch" if builder.count != count_of_complex_query
137
- end
138
-
139
- puts "\nComplex query (Mongory) use CMatcher (#{size} records):"
140
- gc_handler do
141
- matcher = Mongory::CMatcher.new({
125
+ matcher = Mongory::Matcher.new({
142
126
  '$or' => [
143
127
  { :age.gte => 18 },
144
128
  { status: 'active' }
@@ -153,9 +137,9 @@ end
153
137
  raise "count mismatch" if records.count { |r| matcher.match?(r) } != count_of_complex_query
154
138
  end
155
139
 
156
- puts "\nComplex query (Mongory) use CQueryBuilder (#{size} records):"
140
+ puts "\nComplex query (Mongory) use QueryBuilder (#{size} records):"
157
141
  gc_handler do
158
- builder = records.mongory.c.or(
142
+ builder = records.mongory.or(
159
143
  { :age.gte => 18 },
160
144
  { status: 'active' }
161
145
  )
@@ -17,7 +17,7 @@ unless Dir.exist?(core_dir)
17
17
  exit 1
18
18
  end
19
19
 
20
- # mongory-rb does not require cJSON; only mongory-core's tests use it, so we don't check cJSON here
20
+ # mongory-core itself has zero third-party C library dependencies; extconf.rb compiles it directly
21
21
 
22
22
  # Normalize compiler flags across GCC/Clang on CI (Ruby 2.6 on Ubuntu may inject clang-only warn flags)
23
23
  def scrub_flags_from_config!(keys, patterns)
@@ -0,0 +1,62 @@
1
+ #ifndef MONGORY_ADAPTERS_JSON_H
2
+ #define MONGORY_ADAPTERS_JSON_H
3
+
4
+ /**
5
+ * @file json.h
6
+ * @brief Optional raw JSON adapter for mongory-core (enabled via -DMONGORY_WITH_JSON=ON).
7
+ *
8
+ * Provides lazy, shallow-wrapped matching directly over raw UTF-8 JSON bytes
9
+ * backed by yyjson, avoiding full DOM-to-mongory_table materialization.
10
+ */
11
+
12
+ #include "mongory-core/foundations/memory_pool.h"
13
+ #include "mongory-core/foundations/value.h"
14
+ #include "mongory-core/matchers/matcher.h"
15
+ #include <stdbool.h>
16
+ #include <stddef.h>
17
+
18
+ /**
19
+ * @brief Compiles a mongory_matcher directly from a raw JSON condition string.
20
+ *
21
+ * Parses `json_str` (`len` bytes) and builds a matcher in `pool`.
22
+ * On parse or compile failure, sets `pool->error` and returns NULL.
23
+ */
24
+ mongory_matcher *mongory_matcher_new_from_json(mongory_memory_pool *pool,
25
+ const char *json_str,
26
+ size_t len,
27
+ void *extern_ctx);
28
+
29
+ /**
30
+ * @brief Matches a raw UTF-8 JSON document string directly against `matcher`.
31
+ *
32
+ * Uses a lazy shallow wrapper so only keys/elements actually accessed by
33
+ * `matcher` are wrapped on demand. Safe to call concurrently from multiple
34
+ * threads on a shared `matcher`.
35
+ *
36
+ * @param matcher Compiled matcher (must be non-NULL).
37
+ * @param json_str Pointer to UTF-8 JSON bytes (NUL-termination not required).
38
+ * @param len Byte length of `json_str`.
39
+ * @return true if `json_str` is valid JSON and satisfies `matcher`, false otherwise.
40
+ */
41
+ bool mongory_matcher_match_json(mongory_matcher *matcher, const char *json_str, size_t len);
42
+
43
+ /**
44
+ * @brief Reusable scratch context for high-throughput batch JSON filtering.
45
+ *
46
+ * Reuses a single internal scratch memory pool and yyjson buffer across calls
47
+ * to avoid per-record heap allocations. Create one context per thread.
48
+ */
49
+ typedef struct mongory_json_match_ctx mongory_json_match_ctx;
50
+
51
+ mongory_json_match_ctx *mongory_json_match_ctx_new(void);
52
+ void mongory_json_match_ctx_free(mongory_json_match_ctx *ctx);
53
+
54
+ /**
55
+ * @brief Matches a raw UTF-8 JSON document using a reusable `ctx`.
56
+ */
57
+ bool mongory_matcher_match_json_with_ctx(mongory_matcher *matcher,
58
+ mongory_json_match_ctx *ctx,
59
+ const char *json_str,
60
+ size_t len);
61
+
62
+ #endif /* MONGORY_ADAPTERS_JSON_H */
@@ -21,6 +21,12 @@
21
21
  *
22
22
  * This function is called when a regex match is required.
23
23
  *
24
+ * @note Security / ReDoS guidance: `mongory-core` is regex-engine agnostic.
25
+ * When accepting untrusted patterns or inputs, adapters backed by backtracking
26
+ * engines MUST enforce match/backtrack limits (e.g. PCRE2
27
+ * `pcre2_set_match_limit` & `pcre2_set_depth_limit`, Oniguruma
28
+ * `onig_set_retry_limit_in_match`) or use a linear-time engine such as RE2.
29
+ *
24
30
  * @param pool The memory pool to use for any allocations during the match.
25
31
  * @param pattern The regex pattern (as a mongory_value, typically a string or
26
32
  * regex type).
@@ -183,4 +189,29 @@ void mongory_custom_matcher_lookup_func_set(bool (*lookup)(char *key));
183
189
 
184
190
  void mongory_matcher_trace_result_colorful_set(bool colorful);
185
191
 
192
+ #define MONGORY_DEFAULT_MAX_DEPTH 64
193
+ #define MONGORY_DEFAULT_MAX_NODES 4096
194
+
195
+ /**
196
+ * @brief Sets the maximum condition nesting depth allowed during matcher compilation.
197
+ * Pass 0 to disable the depth limit. Defaults to MONGORY_DEFAULT_MAX_DEPTH (64).
198
+ */
199
+ void mongory_max_depth_set(size_t max_depth);
200
+
201
+ /**
202
+ * @brief Returns the configured maximum condition nesting depth (0 = unlimited).
203
+ */
204
+ size_t mongory_max_depth_get(void);
205
+
206
+ /**
207
+ * @brief Sets the maximum number of matcher nodes allowed per compiled matcher.
208
+ * Pass 0 to disable the node limit. Defaults to MONGORY_DEFAULT_MAX_NODES (4096).
209
+ */
210
+ void mongory_max_nodes_set(size_t max_nodes);
211
+
212
+ /**
213
+ * @brief Returns the configured maximum matcher node count (0 = unlimited).
214
+ */
215
+ size_t mongory_max_nodes_get(void);
216
+
186
217
  #endif /* MONGORY_FOUNDATIONS_CONFIG_H */
@@ -35,6 +35,7 @@
35
35
  _(MONGORY_ERROR_INVALID_ARGUMENT, 15, "Invalid Argument Error") \
36
36
  _(MONGORY_ERROR_IO, 16, "I/O Error") \
37
37
  _(MONGORY_ERROR_PARSE, 17, "Parse Error") \
38
+ _(MONGORY_ERROR_LIMIT_EXCEEDED, 18, "Limit Exceeded Error") \
38
39
  _(MONGORY_ERROR_UNKNOWN, 99, "Unknown Error")
39
40
 
40
41
  /**
@@ -75,5 +76,7 @@ typedef struct mongory_error {
75
76
  } mongory_error;
76
77
 
77
78
  extern mongory_error MONGORY_ALLOC_ERROR;
79
+ extern mongory_error MONGORY_DEPTH_LIMIT_ERROR;
80
+ extern mongory_error MONGORY_NODE_LIMIT_ERROR;
78
81
 
79
82
  #endif /* MONGORY_ERROR */
@@ -80,6 +80,8 @@ struct mongory_memory_pool {
80
80
  is a static error object. */
81
81
  };
82
82
 
83
+ #define MONGORY_DEFAULT_MAX_CHUNK_SIZE (4u * 1024u * 1024u)
84
+
83
85
  /**
84
86
  * @brief Creates a new mongory_memory_pool instance.
85
87
  *
@@ -92,4 +94,22 @@ struct mongory_memory_pool {
92
94
  */
93
95
  mongory_memory_pool *mongory_memory_pool_new();
94
96
 
97
+ /**
98
+ * @brief Configures chunk growth cap and total byte budget for a core memory pool.
99
+ *
100
+ * Passing `0` for `max_chunk_size` restores `MONGORY_DEFAULT_MAX_CHUNK_SIZE`.
101
+ * Passing `0` for `max_total_bytes` disables the total byte limit (unlimited).
102
+ * Safe no-op if `pool` is NULL or uses a non-core custom pool implementation.
103
+ *
104
+ * @param pool The target memory pool.
105
+ * @param max_chunk_size Maximum size for geometric chunk doubling (in bytes).
106
+ * @param max_total_bytes Maximum cumulative chunk bytes allowed (0 = unlimited).
107
+ */
108
+ void mongory_memory_pool_set_limits(mongory_memory_pool *pool, size_t max_chunk_size, size_t max_total_bytes);
109
+
110
+ /**
111
+ * @brief Returns total chunk bytes currently allocated by a core memory pool.
112
+ */
113
+ size_t mongory_memory_pool_total_bytes(const mongory_memory_pool *pool);
114
+
95
115
  #endif /* MONGORY_MEMORY_POOL */
@@ -21,7 +21,7 @@ typedef struct mongory_matcher mongory_matcher;
21
21
  *
22
22
  * This function typically serves as a high-level entry point for creating
23
23
  * matchers. In the current implementation, it delegates to
24
- * `mongory_matcher_table_cond_new`, implying that conditions are often
24
+ * `mongory_matcher_condition_new`, implying that conditions are often
25
25
  * table-based (similar to query documents).
26
26
  *
27
27
  * @param pool The memory pool to use for the matcher's allocations.
@@ -41,21 +41,48 @@ mongory_matcher *mongory_matcher_new(mongory_memory_pool *pool, mongory_value *c
41
41
  bool mongory_matcher_match(mongory_matcher *matcher, mongory_value *value);
42
42
 
43
43
  /**
44
- * @brief Explains a matcher.
44
+ * @brief Builds and returns the structured explain tree for a matcher.
45
+ *
46
+ * Each node is a `mongory_value` wrapping a `mongory_table` with keys:
47
+ * `"name"`, `"condition"`, `"priority"`, `"children"` (array of child nodes),
48
+ * and `"field"` (for Field matchers).
49
+ *
50
+ * @param matcher The matcher to explain.
51
+ * @param temp_pool The temporary pool to allocate the tree from.
52
+ * @return A `mongory_value*` table representing the root explain node, or NULL on error.
53
+ */
54
+ mongory_value *mongory_matcher_explain_tree(mongory_matcher *matcher, mongory_memory_pool *temp_pool);
55
+
56
+ /**
57
+ * @brief Explains a matcher by printing `mongory_matcher_explain_tree` to stdout.
45
58
  * @param matcher The matcher to explain.
46
59
  * @param temp_pool The temporary pool to use for the explanation.
47
60
  */
48
61
  void mongory_matcher_explain(mongory_matcher *matcher, mongory_memory_pool *temp_pool);
49
62
 
50
63
  /**
51
- * @brief Traces a matcher.
64
+ * @brief Traces a matcher against `value` and returns the structured trace tree.
65
+ *
66
+ * Each node is a `mongory_value` wrapping a `mongory_table` with keys:
67
+ * `"name"`, `"condition"`, `"priority"`, `"result"` (`"pass"`, `"fail"`, or `"skip"`),
68
+ * `"children"` (array of child nodes), optional `"field"` (for Field matchers),
69
+ * and optional `"record"` (omitted for skipped nodes or missing values).
70
+ *
71
+ * @param matcher The matcher to trace.
72
+ * @param value The value to trace.
73
+ * @return A `mongory_value*` table representing the root trace node, or NULL on error.
74
+ */
75
+ mongory_value *mongory_matcher_trace_tree(mongory_matcher *matcher, mongory_value *value);
76
+
77
+ /**
78
+ * @brief Traces a matcher, prints the trace tree to stdout, and returns the match result.
52
79
  * @param matcher The matcher to trace.
53
80
  * @param value The value to trace.
54
81
  */
55
82
  bool mongory_matcher_trace(mongory_matcher *matcher, mongory_value *value);
56
83
 
57
84
  /**
58
- * @brief Prints the trace of a matcher.
85
+ * @brief Prints the trace of a matcher by rendering its recorded trace tree to stdout.
59
86
  * @param matcher The matcher to print the trace of.
60
87
  */
61
88
  void mongory_matcher_print_trace(mongory_matcher *matcher);