mongory 0.9.0-x86_64-linux → 2.0.0-x86_64-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.
- 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/core/2.6/mongory_ext.so +0 -0
- data/lib/core/2.7/mongory_ext.so +0 -0
- data/lib/core/3.0/mongory_ext.so +0 -0
- data/lib/core/3.1/mongory_ext.so +0 -0
- data/lib/core/3.2/mongory_ext.so +0 -0
- data/lib/core/3.3/mongory_ext.so +0 -0
- data/lib/core/3.4/mongory_ext.so +0 -0
- data/lib/core/4.0/mongory_ext.so +0 -0
- 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/docs/clang_bridge.md
CHANGED
|
@@ -1,54 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
# Clang Bridge (Native C99 Engine)
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
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
|
-
|
|
11
|
+
## Architecture
|
|
22
12
|
|
|
23
|
-
|
|
13
|
+
The Clang bridge connects the Ruby DSL to `mongory-core` via a compact C layer exposing two key entry points:
|
|
24
14
|
|
|
25
|
-
|
|
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
|
-
|
|
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
|
|
25
|
+
## Basic Usage
|
|
52
26
|
|
|
53
27
|
```ruby
|
|
54
28
|
records = [
|
|
@@ -57,43 +31,38 @@ records = [
|
|
|
57
31
|
{ 'name' => 'Bob', 'age' => 21 }
|
|
58
32
|
]
|
|
59
33
|
|
|
60
|
-
#
|
|
61
|
-
query = records.mongory.
|
|
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
|
|
65
|
-
query.
|
|
66
|
-
query.
|
|
67
|
-
query.
|
|
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
|
|
70
|
-
matcher = Mongory::
|
|
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
|
|
48
|
+
## Tracing and Debugging
|
|
81
49
|
|
|
82
50
|
```ruby
|
|
83
|
-
matcher = Mongory::
|
|
51
|
+
matcher = Mongory::Matcher.new(:$or => [{ :name.regex => /^J/ }, { :age.gt => 20 }])
|
|
84
52
|
matcher.enable_trace
|
|
85
53
|
records.each { |r| matcher.match?(r) }
|
|
86
54
|
matcher.print_trace # prints detailed trace
|
|
87
55
|
matcher.disable_trace
|
|
88
56
|
|
|
89
|
-
#
|
|
57
|
+
# Structured explain/trace trees on a single Matcher
|
|
58
|
+
plan = matcher.explain_tree # => Hash ({ name:, condition:, priority:, children:, ... })
|
|
59
|
+
tree = matcher.trace_tree(records.first) # => Hash ({ ..., record:, result: :pass | :fail | :skip })
|
|
60
|
+
|
|
61
|
+
# Print single-record trace tree to STDOUT and return boolean match result
|
|
90
62
|
matcher.trace(records.first)
|
|
91
63
|
```
|
|
92
64
|
|
|
93
65
|
## Notes
|
|
94
66
|
|
|
95
67
|
- 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
|
-
|
|
68
|
+
- 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
|
-
##
|
|
3
|
+
## Reproducible Benchmarks
|
|
4
4
|
|
|
5
|
-
|
|
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
|
-
|
|
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
|
-
|
|
11
|
+
### Measured Results (Apple M3, macOS arm64, Ruby `4.0.5`, 100,000 records)
|
|
53
12
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
-
|
|
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
|
-
|
|
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.
|
data/examples/benchmark.rb
CHANGED
|
@@ -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::
|
|
63
|
-
puts "\nSimple query (Mongory::
|
|
62
|
+
# Simple query (Mongory::Matcher) test
|
|
63
|
+
puts "\nSimple query (Mongory::Matcher) (#{size} records):"
|
|
64
64
|
gc_handler do
|
|
65
|
-
matcher = Mongory::
|
|
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::
|
|
76
|
-
puts "\nSimple query (Mongory::
|
|
75
|
+
# Simple query (Mongory::QueryBuilder) test
|
|
76
|
+
puts "\nSimple query (Mongory::QueryBuilder) (#{size} records):"
|
|
77
77
|
gc_handler do
|
|
78
|
-
builder = Mongory::
|
|
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
|
|
@@ -136,9 +136,9 @@ end
|
|
|
136
136
|
raise "count mismatch" if builder.count != count_of_complex_query
|
|
137
137
|
end
|
|
138
138
|
|
|
139
|
-
puts "\nComplex query (Mongory) use
|
|
139
|
+
puts "\nComplex query (Mongory) use Matcher (#{size} records):"
|
|
140
140
|
gc_handler do
|
|
141
|
-
matcher = Mongory::
|
|
141
|
+
matcher = Mongory::Matcher.new({
|
|
142
142
|
'$or' => [
|
|
143
143
|
{ :age.gte => 18 },
|
|
144
144
|
{ status: 'active' }
|
|
@@ -153,9 +153,9 @@ end
|
|
|
153
153
|
raise "count mismatch" if records.count { |r| matcher.match?(r) } != count_of_complex_query
|
|
154
154
|
end
|
|
155
155
|
|
|
156
|
-
puts "\nComplex query (Mongory) use
|
|
156
|
+
puts "\nComplex query (Mongory) use QueryBuilder (#{size} records):"
|
|
157
157
|
gc_handler do
|
|
158
|
-
builder = records.mongory.
|
|
158
|
+
builder = records.mongory.or(
|
|
159
159
|
{ :age.gte => 18 },
|
|
160
160
|
{ status: 'active' }
|
|
161
161
|
)
|
|
@@ -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
|
-
* `
|
|
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
|
|
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);
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* `mongory_array` with capacity information and the actual item storage.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
#include <stdint.h>
|
|
11
12
|
#include <string.h>
|
|
12
13
|
#include "array_private.h"
|
|
13
14
|
#include <mongory-core/foundations/array.h>
|
|
@@ -36,6 +37,10 @@
|
|
|
36
37
|
*/
|
|
37
38
|
bool mongory_array_resize(mongory_array *self, size_t size) {
|
|
38
39
|
mongory_array_private *internal = (mongory_array_private *)self;
|
|
40
|
+
if (size > SIZE_MAX / sizeof(mongory_value *)) {
|
|
41
|
+
self->pool->error = &MONGORY_ALLOC_ERROR;
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
39
44
|
|
|
40
45
|
// Allocate a new, larger block of memory for the items.
|
|
41
46
|
mongory_value **new_items = MG_ALLOC_ARY(self->pool, mongory_value*, size);
|
|
@@ -73,10 +78,18 @@ static inline bool mongory_array_grow_if_needed(mongory_array *self, size_t size
|
|
|
73
78
|
if (size < internal->capacity) {
|
|
74
79
|
return true;
|
|
75
80
|
}
|
|
81
|
+
if (size == SIZE_MAX) {
|
|
82
|
+
self->pool->error = &MONGORY_ALLOC_ERROR;
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
76
85
|
|
|
77
86
|
// Calculate new capacity, typically doubling, until it's sufficient.
|
|
78
|
-
size_t new_capacity = internal->capacity
|
|
87
|
+
size_t new_capacity = internal->capacity > 0 ? internal->capacity : MONGORY_ARRAY_INIT_SIZE;
|
|
79
88
|
while (new_capacity <= size) {
|
|
89
|
+
if (new_capacity > SIZE_MAX / 2) {
|
|
90
|
+
new_capacity = size + 1;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
80
93
|
new_capacity *= 2;
|
|
81
94
|
}
|
|
82
95
|
|
|
@@ -232,18 +245,31 @@ mongory_array *mongory_array_nested_wrap(mongory_memory_pool *pool, int argc, ..
|
|
|
232
245
|
}
|
|
233
246
|
va_list args;
|
|
234
247
|
va_start(args, argc);
|
|
248
|
+
bool ok = true;
|
|
235
249
|
for (int i = 0; i < argc; i++) {
|
|
236
250
|
mongory_value *value = va_arg(args, mongory_value *);
|
|
237
|
-
array->push(array, value)
|
|
251
|
+
if (ok && (!value || !array->push(array, value))) {
|
|
252
|
+
ok = false;
|
|
253
|
+
}
|
|
238
254
|
}
|
|
239
255
|
va_end(args);
|
|
256
|
+
if (!ok || pool->error != NULL) {
|
|
257
|
+
return NULL;
|
|
258
|
+
}
|
|
240
259
|
return array;
|
|
241
260
|
}
|
|
242
261
|
|
|
243
262
|
typedef size_t(*mongory_array_sort_cb)(mongory_value *value, void *ctx);
|
|
244
263
|
|
|
245
264
|
static mongory_value** mongory_array_merge_sort_finalize(mongory_memory_pool *pool, mongory_value **left, mongory_value **right, size_t count, void *ctx, mongory_array_sort_cb callback) {
|
|
265
|
+
if (!left || !right) {
|
|
266
|
+
return NULL;
|
|
267
|
+
}
|
|
246
268
|
mongory_value **result = MG_ALLOC_ARY(pool, mongory_value*, count);
|
|
269
|
+
if (!result) {
|
|
270
|
+
pool->error = &MONGORY_ALLOC_ERROR;
|
|
271
|
+
return NULL;
|
|
272
|
+
}
|
|
247
273
|
size_t i = 0, j = 0, k = 0;
|
|
248
274
|
while (i < count / 2 && j < count - count / 2) {
|
|
249
275
|
if (callback(left[i], ctx) < callback(right[j], ctx)) {
|
|
@@ -267,19 +293,35 @@ static mongory_value** mongory_array_merge_sort(mongory_memory_pool *pool, mongo
|
|
|
267
293
|
}
|
|
268
294
|
size_t mid = count / 2;
|
|
269
295
|
mongory_value **left = mongory_array_merge_sort(pool, origin_items, mid, ctx, callback);
|
|
296
|
+
if (!left) {
|
|
297
|
+
return NULL;
|
|
298
|
+
}
|
|
270
299
|
mongory_value **right = mongory_array_merge_sort(pool, origin_items + mid, count - mid, ctx, callback);
|
|
300
|
+
if (!right) {
|
|
301
|
+
return NULL;
|
|
302
|
+
}
|
|
271
303
|
return mongory_array_merge_sort_finalize(pool, left, right, count, ctx, callback);
|
|
272
304
|
}
|
|
273
305
|
|
|
274
306
|
mongory_array *mongory_array_sort_by(mongory_array *self, mongory_memory_pool *temp_pool, void *ctx, mongory_array_sort_cb callback) {
|
|
307
|
+
if (!self || !temp_pool) {
|
|
308
|
+
return NULL;
|
|
309
|
+
}
|
|
275
310
|
mongory_array_private *internal = (mongory_array_private *)self;
|
|
276
311
|
|
|
277
312
|
mongory_value **new_items = mongory_array_merge_sort(temp_pool, internal->items, self->count, ctx, callback);
|
|
278
|
-
|
|
279
|
-
|
|
313
|
+
if (!new_items) {
|
|
314
|
+
self->pool->error = &MONGORY_ALLOC_ERROR;
|
|
315
|
+
return NULL;
|
|
316
|
+
}
|
|
317
|
+
mongory_array_private *new_array = (mongory_array_private *)mongory_array_new_with_capacity(self->pool, internal->capacity);
|
|
318
|
+
if (!new_array) {
|
|
319
|
+
return NULL;
|
|
320
|
+
}
|
|
280
321
|
new_array->base.count = self->count;
|
|
281
|
-
|
|
282
|
-
|
|
322
|
+
if (self->count > 0) {
|
|
323
|
+
memcpy(new_array->items, new_items, sizeof(mongory_value *) * self->count);
|
|
324
|
+
}
|
|
283
325
|
return &new_array->base;
|
|
284
326
|
}
|
|
285
327
|
|
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
#include "../matchers/base_matcher.h" // For mongory_matcher_build_func
|
|
13
13
|
#include "../matchers/compare_matcher.h" // For specific matcher constructors
|
|
14
14
|
#include "../matchers/composite_matcher.h" // For specific matcher constructors
|
|
15
|
-
#include "../matchers/
|
|
15
|
+
#include "../matchers/existence_matcher.h" // For specific matcher constructors
|
|
16
16
|
#include "../matchers/inclusion_matcher.h" // For specific matcher constructors
|
|
17
|
-
#include "../matchers/
|
|
17
|
+
#include "../matchers/value_matcher.h" // For specific matcher constructors
|
|
18
18
|
#include "../matchers/external_matcher.h" // For specific matcher constructors
|
|
19
19
|
#include "config_private.h" // For mongory_regex_adapter, mongory_value_converter, etc.
|
|
20
20
|
#include "mongory-core/foundations/memory_pool.h"
|
|
@@ -66,6 +66,8 @@ mongory_matcher_custom_adapter mongory_custom_matcher_adapter = {
|
|
|
66
66
|
};
|
|
67
67
|
|
|
68
68
|
bool mongory_matcher_trace_result_colorful = true;
|
|
69
|
+
static size_t mongory_compile_max_depth = MONGORY_DEFAULT_MAX_DEPTH;
|
|
70
|
+
static size_t mongory_compile_max_nodes = MONGORY_DEFAULT_MAX_NODES;
|
|
69
71
|
|
|
70
72
|
/**
|
|
71
73
|
* @brief Initializes the internal memory pool if it hasn't been already.
|
|
@@ -245,6 +247,32 @@ void mongory_matcher_trace_result_colorful_set(bool colorful) {
|
|
|
245
247
|
mongory_matcher_trace_result_colorful = colorful;
|
|
246
248
|
}
|
|
247
249
|
|
|
250
|
+
void mongory_max_depth_set(size_t max_depth) {
|
|
251
|
+
mongory_config_lock();
|
|
252
|
+
mongory_compile_max_depth = max_depth;
|
|
253
|
+
mongory_config_unlock();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
size_t mongory_max_depth_get(void) {
|
|
257
|
+
mongory_config_lock();
|
|
258
|
+
size_t val = mongory_compile_max_depth;
|
|
259
|
+
mongory_config_unlock();
|
|
260
|
+
return val;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
void mongory_max_nodes_set(size_t max_nodes) {
|
|
264
|
+
mongory_config_lock();
|
|
265
|
+
mongory_compile_max_nodes = max_nodes;
|
|
266
|
+
mongory_config_unlock();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
size_t mongory_max_nodes_get(void) {
|
|
270
|
+
mongory_config_lock();
|
|
271
|
+
size_t val = mongory_compile_max_nodes;
|
|
272
|
+
mongory_config_unlock();
|
|
273
|
+
return val;
|
|
274
|
+
}
|
|
275
|
+
|
|
248
276
|
/**
|
|
249
277
|
* @brief Initializes all core Mongory library components.
|
|
250
278
|
* This includes the internal memory pool, regex adapter, matcher mapping table,
|