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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c7962701ff00626932360c9cc22319eb2e6d05eda1fecf03519ffdf71a95141b
|
|
4
|
+
data.tar.gz: f3cd343120122c310f84d7721e5c2d2ad5549ae66dea51bda631627e2d7dc86b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bca990c63b9c2ee92106fa6748b521cf68f055b46561d673bd9f545c27ebfe4f93e3e786201496438673cdd8e4aca4a9d783f80a2af2af479922e5d5a91e5610
|
|
7
|
+
data.tar.gz: 464df9807c24e8ab99025b1a4007e70a325f1d8cad35f307061ecb40532cbdc89ea44b6997ea6d55299f027c1ee998b1f300a45b9d65a78c284f20408c33ecf4
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [2.0.0] - 2026-11-22
|
|
4
|
+
|
|
5
|
+
Unifies `mongory-rb` on the `mongory-core` C99 native extension as the sole matching engine, eliminating Pure-Ruby dual-engine maintenance.
|
|
6
|
+
|
|
7
|
+
### Breaking Changes
|
|
8
|
+
- **C Extension Only (No Pure-Ruby Fallback)**: Removed the 22 built-in Pure-Ruby matchers, `Mongory::QueryMatcher`, `Mongory::CQueryBuilder`, and `Mongory::Utils::Debugger` (`Mongory.debugger`). Loading `mongory` now requires the compiled `mongory_ext` native extension directly.
|
|
9
|
+
- **Renamed `Mongory::CMatcher` → `Mongory::Matcher`**: `Mongory.build_matcher(condition)` now always returns `Mongory::Matcher`, and `Mongory.build_query(records)` / `.mongory` always return `Mongory::QueryBuilder` backed by `Mongory::Matcher`.
|
|
10
|
+
- **Removed C-Extension Toggle APIs**: Removed `Mongory.enable_c_extension_by_default!`, `Mongory.use_c_extension_by_default!`, `Mongory.disable_c_extension_by_default!`, `Mongory.c_extension_by_default?`, and `QueryBuilder#c`.
|
|
11
|
+
- **Custom Matchers Preserved**: `Mongory::Matchers::AbstractMatcher`, `Mongory::Matchers.register`, `Mongory.enable_symbol_snippets!`, and all `Converters` remain supported and bridge directly into `mongory-core`.
|
|
12
|
+
|
|
13
|
+
### Core & Maintenance
|
|
14
|
+
- Updated `ext/mongory_ext/mongory-core` submodule to latest `main` (`ac678b3`: OOM hardening, `MaxDepth`/`MaxNodes` safety limits, sanitizer/fuzzing CI, and macOS Universal Binary CI).
|
|
15
|
+
|
|
16
|
+
## [0.9.1] - 2026-11-18
|
|
17
|
+
|
|
18
|
+
Aligns with `mongory-core` `v0.2.1`.
|
|
19
|
+
|
|
20
|
+
### Fixes
|
|
21
|
+
- **`QueryMatcher` (`LiteralMatcher` & `AbstractMultiMatcher`)**:
|
|
22
|
+
- Reflect lazily built `array_record_matcher` priority in `LiteralMatcher#priority` and `render_tree`.
|
|
23
|
+
- Propagate priority increases up enclosing matchers (`FieldMatcher`, `NotMatcher`, `SizeMatcher`, `ElemMatchMatcher`, `EveryMatcher`, `HashConditionMatcher`, `AndMatcher`, `OrMatcher`, `ArrayRecordMatcher`, `QueryMatcher`), stably re-sort child `@matchers`, and invalidate memoized `@cached_proc` / `@debug_proc` closures so subsequent evaluations short-circuit on cheaper conditions first.
|
|
24
|
+
- **Core (`mongory-core` submodule `v0.2.1`)**:
|
|
25
|
+
- Propagate lazy `array_record_matcher` priority delta up ancestor matchers and copy-on-write re-sort enclosing composite matchers (`mongory_matcher_resort_composite`) with atomic acquire/release publication.
|
|
26
|
+
- **CI**:
|
|
27
|
+
- Auto-create GitHub Release after gem push in `.github/workflows/build.yml`.
|
|
28
|
+
|
|
3
29
|
## [0.9.0] - 2026-11-17
|
|
4
30
|
|
|
5
31
|
Aligns with mongory-spec `0.2.0` and `mongory-core` `v0.2.0`.
|
data/README.md
CHANGED
|
@@ -1,303 +1,98 @@
|
|
|
1
|
-
# Mongory
|
|
2
|
-
### Let you query everywhere !
|
|
3
|
-
|
|
4
|
-
This is a Mongo-like in-memory query DSL for Ruby.
|
|
5
|
-
|
|
6
|
-
Mongory lets you filter and query in-memory collections using syntax and semantics similar to MongoDB. It is designed for expressive chaining, symbolic operators, and composable matchers.
|
|
7
|
-
|
|
8
|
-
## Table of Contents
|
|
9
|
-
|
|
10
|
-
- Overview & Positioning
|
|
11
|
-
- [Positioning](#positioning)
|
|
12
|
-
- Getting Started
|
|
13
|
-
- [Requirements](#requirements)
|
|
14
|
-
- [Installation & Quick Start](#installation--quick-start)
|
|
15
|
-
- [Integration with MongoDB](#integration-with-mongodb)
|
|
16
|
-
- Usage & Concepts
|
|
17
|
-
- [Creating Custom Matchers](#creating-custom-matchers)
|
|
18
|
-
- [Core Concepts & API Reference](#core-concepts--api-reference)
|
|
19
|
-
- [Handling Dots in Field Names](docs/field_names.md)
|
|
20
|
-
- [Advanced Usage](docs/advanced_usage.md)
|
|
21
|
-
- [Debugging](#debugging)
|
|
22
|
-
- [Clang Bridge (C Extension)](docs/clang_bridge.md)
|
|
23
|
-
- Performance
|
|
24
|
-
- [Performance & Benchmarks](docs/performance.md)
|
|
25
|
-
- [Supported Operators](#supported-operators)
|
|
26
|
-
- Guides
|
|
27
|
-
- [Best Practices](#best-practices)
|
|
28
|
-
- [Limitations](#limitations)
|
|
29
|
-
- [FAQ](#faq)
|
|
30
|
-
- [Troubleshooting](#troubleshooting)
|
|
31
|
-
- [Migration Guide](docs/migration.md)
|
|
32
|
-
- Project
|
|
33
|
-
- [Contributing](#contributing)
|
|
34
|
-
- [Code of Conduct](#code-of-conduct)
|
|
35
|
-
- [License](#license)
|
|
36
|
-
|
|
37
|
-
## Requirements
|
|
38
|
-
|
|
39
|
-
- Ruby >= 2.6.0
|
|
40
|
-
- No external database required
|
|
41
|
-
|
|
42
|
-
## Installation & Quick Start
|
|
43
|
-
|
|
44
|
-
### Installation
|
|
45
|
-
Install manually:
|
|
46
|
-
```bash
|
|
47
|
-
gem install mongory
|
|
48
|
-
```
|
|
1
|
+
# Mongory (Ruby)
|
|
49
2
|
|
|
50
|
-
|
|
51
|
-
```ruby
|
|
52
|
-
gem 'mongory'
|
|
53
|
-
```
|
|
3
|
+
**Cross-language, in-memory Mongo-style rule engine & query matcher with built-in Explain & Trace debugging** — powered by the C99 [`mongory-core`](https://github.com/mongoryhq/mongory-core) native extension with cross-language parity via [`mongory-spec`](https://github.com/mongoryhq/mongory-spec).
|
|
54
4
|
|
|
55
|
-
|
|
5
|
+
**[Try the Interactive Playground & Visual Explain/Trace Debugger ↗](https://mongoryhq.github.io/mongory-central/)**
|
|
56
6
|
|
|
57
|
-
Mongory
|
|
58
|
-
Before installing Mongory, make sure these following precompiled package support your platform:
|
|
59
|
-
- x86_64-linux-musl (Linux)
|
|
60
|
-
- aarch64-linux-musl (Linux)
|
|
61
|
-
- x86_64-linux (Linux)
|
|
62
|
-
- aarch64-linux (Linux)
|
|
63
|
-
- x86_64-darwin (MacOS Intel)
|
|
64
|
-
- arm64-darwin (MacOS Apple Silicon)
|
|
65
|
-
- x64-mingw32 (Windows)
|
|
66
|
-
- x64-mingw-ucrt (Windows)
|
|
7
|
+
[](https://mongoryhq.github.io/mongory-central/)
|
|
67
8
|
|
|
68
|
-
|
|
9
|
+
> **Zero database required:** Evaluate MongoDB-style conditions (`$gte`, `$elemMatch`, `$every`, `$present`, `$regex`, etc.) directly against in-memory Ruby Arrays, Hashes, custom objects, and ActiveRecord/Mongoid relations.
|
|
10
|
+
> **Ecosystem:** [Interactive Playground](https://mongoryhq.github.io/mongory-central/) · [`mongory-go` (Go)](https://github.com/mongoryhq/mongory-go) · [`mongory-js` (TypeScript/JS)](https://github.com/mongoryhq/mongory-js) · [`mongory-core` (C99)](https://github.com/mongoryhq/mongory-core) · [`mongory-spec`](https://github.com/mongoryhq/mongory-spec)
|
|
69
11
|
|
|
70
|
-
|
|
71
|
-
```bash
|
|
72
|
-
apt-get update && apt-get install -y build-essential
|
|
73
|
-
```
|
|
12
|
+
## Features
|
|
74
13
|
|
|
75
|
-
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
14
|
+
- **Chainable Ruby DSL & Raw JSON Support**: Compose queries with `.where`, `.any_of`, `.or`, `.not`, and optional Symbol snippets (`:age.gte => 18`), or pass parsed JSON conditions directly from front-end rules.
|
|
15
|
+
- **All 17 Mongory Operators**: `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`, `$in`, `$nin`, `$exists`, `$present`, `$and`, `$or`, `$not`, `$elemMatch`, `$every`, `$size`, `$regex` (with `$options`: `i`, `m`, `s`).
|
|
16
|
+
- **Priority-Ordered Execution & Visual Debugging**: Automatically sorts sibling conditions so cheaper checks short-circuit before expensive regex or array scans; inspect compiled trees with `.explain` and per-record evaluation with `.trace`.
|
|
17
|
+
- **Native C99 Engine (`Mongory::Matcher`)**: Powered directly by `mongory-core` with zero Pure-Ruby engine overhead, while still supporting custom Ruby matchers via `Mongory::Matchers::AbstractMatcher`.
|
|
18
|
+
- **Precompiled Native Gems (Ruby 2.6–4.0)**: Ships prebuilt binaries for Linux (`x86_64`/`aarch64`, glibc & musl), macOS (`x86_64`/`arm64`), and Windows (`x64-mingw32`/`ucrt`).
|
|
79
19
|
|
|
80
|
-
|
|
81
|
-
```bash
|
|
82
|
-
yum groupinstall -y "Development Tools"
|
|
83
|
-
```
|
|
20
|
+
## Installation
|
|
84
21
|
|
|
85
|
-
- Fedora
|
|
86
22
|
```bash
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
- Amazon Linux
|
|
91
|
-
```bash
|
|
92
|
-
yum groupinstall -y "Development Tools"
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
- macOS
|
|
96
|
-
```bash
|
|
97
|
-
xcode-select --install
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
### Basic Usage
|
|
101
|
-
```ruby
|
|
102
|
-
records = [
|
|
103
|
-
{ 'name' => 'Jack', 'age' => 18, 'gender' => 'M' },
|
|
104
|
-
{ 'name' => 'Jill', 'age' => 15, 'gender' => 'F' },
|
|
105
|
-
{ 'name' => 'Bob', 'age' => 21, 'gender' => 'M' },
|
|
106
|
-
{ 'name' => 'Mary', 'age' => 18, 'gender' => 'F' }
|
|
107
|
-
]
|
|
108
|
-
|
|
109
|
-
# Basic query with conditions
|
|
110
|
-
result = records.mongory
|
|
111
|
-
.where(:age.gte => 18)
|
|
112
|
-
.or({ :name => /J/ }, { :name.eq => 'Bob' })
|
|
113
|
-
.to_a
|
|
114
|
-
|
|
115
|
-
# Or input condition via json string from front-end
|
|
116
|
-
result = records.mongory
|
|
117
|
-
.where(JSON.parse('{
|
|
118
|
-
"age": {"$gt": 18},
|
|
119
|
-
"$or": [
|
|
120
|
-
{"name": {"$regex": "J"}},
|
|
121
|
-
{"name": "Bob"}
|
|
122
|
-
]
|
|
123
|
-
}'))
|
|
124
|
-
.to_a
|
|
125
|
-
|
|
126
|
-
# Using limit to restrict results
|
|
127
|
-
# Note: limit executes immediately and affects subsequent conditions
|
|
128
|
-
limited = records.mongory
|
|
129
|
-
.limit(2) # Only process first 2 records
|
|
130
|
-
.where(:age.gte => 18) # Conditions apply to limited set
|
|
23
|
+
gem install mongory
|
|
24
|
+
# or in Gemfile:
|
|
25
|
+
gem 'mongory'
|
|
131
26
|
```
|
|
132
27
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
You can install a starter configuration with:
|
|
28
|
+
In a Rails project, generate the initializer (`config/initializers/mongory.rb`):
|
|
136
29
|
|
|
137
30
|
```bash
|
|
138
31
|
rails g mongory:install
|
|
139
32
|
```
|
|
140
33
|
|
|
141
|
-
|
|
142
|
-
- Optional symbol operator snippets (e.g. `:age.gt => 18`)
|
|
143
|
-
- Class registration (e.g. `Array`, `ActiveRecord::Relation`, etc.)
|
|
144
|
-
- Custom value/key converters for your ORM
|
|
145
|
-
|
|
146
|
-
## Positioning
|
|
147
|
-
|
|
148
|
-
Mongory is designed to serve two types of users:
|
|
149
|
-
|
|
150
|
-
1. For MongoDB users:
|
|
151
|
-
- Seamless integration with familiar query syntax
|
|
152
|
-
- Extends query capabilities for non-indexed fields
|
|
153
|
-
- No additional learning cost
|
|
154
|
-
|
|
155
|
-
2. For non-MongoDB users:
|
|
156
|
-
- Initial learning cost for MongoDB-style syntax
|
|
157
|
-
- Long-term benefits:
|
|
158
|
-
- Improved code readability
|
|
159
|
-
- Better development efficiency
|
|
160
|
-
- Lower maintenance costs
|
|
161
|
-
- Ideal for teams valuing code quality and maintainability
|
|
162
|
-
|
|
163
|
-
### Integration with MongoDB
|
|
164
|
-
|
|
165
|
-
Mongory is designed to complement MongoDB, not replace it. Here's how to use them together:
|
|
166
|
-
|
|
167
|
-
1. Use MongoDB for:
|
|
168
|
-
- Queries with indexes
|
|
169
|
-
- Persistent data operations
|
|
170
|
-
- Large-scale data processing
|
|
171
|
-
|
|
172
|
-
2. Use Mongory for:
|
|
173
|
-
- Queries without indexes
|
|
174
|
-
- Complex in-memory calculations
|
|
175
|
-
- Temporary data filtering needs
|
|
176
|
-
|
|
177
|
-
Example:
|
|
178
|
-
```ruby
|
|
179
|
-
# First use MongoDB for indexed queries
|
|
180
|
-
users = User.where(status: 'active') # Uses MongoDB index
|
|
181
|
-
|
|
182
|
-
# Then use Mongory for non-indexed fields
|
|
183
|
-
active_users = users.mongory
|
|
184
|
-
.where(:last_login.gte => 1.week.ago) # No index on last_login
|
|
185
|
-
.where(:tags.elem_match => { :name => 'ruby' }) # Complex array query
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
### Creating Custom Matchers
|
|
189
|
-
#### Using the Generator
|
|
190
|
-
|
|
191
|
-
You can generate a new matcher using:
|
|
192
|
-
|
|
193
|
-
```bash
|
|
194
|
-
rails g mongory:matcher class_in
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
This will:
|
|
198
|
-
1. Create a new matcher file at `lib/mongory/matchers/class_in_matcher.rb`
|
|
199
|
-
2. Create a spec file at `spec/mongory/matchers/class_in_matcher_spec.rb`
|
|
200
|
-
3. Update `config/initializers/mongory.rb` to require the new matcher
|
|
201
|
-
|
|
202
|
-
The generated matcher will:
|
|
203
|
-
- Be named `ClassInMatcher`
|
|
204
|
-
- Register the operator as `$classIn` makes it available in queries
|
|
205
|
-
- Enable `:field.class_in` query snippet (If symbol snippet enabled)
|
|
206
|
-
|
|
207
|
-
Example usage of the generated matcher:
|
|
208
|
-
```ruby
|
|
209
|
-
records.mongory.where(:value.class_in => [Integer, String])
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
#### Manual Creation
|
|
34
|
+
> **Note:** Precompiled native gems are installed automatically on supported Linux, macOS, and Windows platforms (Ruby 2.6–4.0). On older Apple Silicon macOS system Ruby (`universal-darwin`), run `gem install mongory --platform arm64-darwin` or `bundle lock --add-platform arm64-darwin`. When compiling from source on other platforms, ensure `gcc`/`clang` and `make` are installed.
|
|
213
35
|
|
|
214
|
-
|
|
36
|
+
## Quick Start
|
|
215
37
|
|
|
216
38
|
```ruby
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
@condition.any? { |klass| subject.is_a?(klass) }
|
|
220
|
-
end
|
|
221
|
-
|
|
222
|
-
def check_validity!
|
|
223
|
-
raise TypeError, '$classIn needs an array.' unless @condition.is_a?(Array)
|
|
224
|
-
@condition.each do |klass|
|
|
225
|
-
raise TypeError, '$classIn needs an array of class.' unless klass.is_a?(Class)
|
|
226
|
-
end
|
|
227
|
-
end
|
|
228
|
-
end
|
|
229
|
-
|
|
230
|
-
Mongory::Matchers.register(:class_in, '$classIn', ClassInMatcher)
|
|
39
|
+
require 'mongory'
|
|
40
|
+
require 'json'
|
|
231
41
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
You can define any matcher behavior and attach it to a `$operator` of your choice.
|
|
237
|
-
Matchers can be composed, validated, and traced just like built-in ones.
|
|
42
|
+
Mongory.register(Array)
|
|
43
|
+
Mongory.enable_symbol_snippets!
|
|
238
44
|
|
|
239
|
-
|
|
240
|
-
|
|
45
|
+
records = [
|
|
46
|
+
{ 'name' => 'Jack', 'age' => 18, 'tags' => %w[ruby go], 'active' => true },
|
|
47
|
+
{ 'name' => 'Jill', 'age' => 15, 'tags' => %w[js], 'active' => true },
|
|
48
|
+
{ 'name' => 'Bob', 'age' => 21, 'tags' => %w[ruby], 'active' => false }
|
|
49
|
+
]
|
|
241
50
|
|
|
242
|
-
|
|
51
|
+
# 1. Chainable DSL with symbol operator snippets
|
|
52
|
+
matched = records.mongory
|
|
53
|
+
.where(:age.gte => 18, :active => true)
|
|
54
|
+
.any_of({ :tags => 'ruby' }, { :name => /^J/ })
|
|
55
|
+
.to_a
|
|
56
|
+
# => [{ "name" => "Jack", ... }]
|
|
243
57
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
58
|
+
# 2. Direct JSON condition (e.g. rule payload from API / front-end)
|
|
59
|
+
rule = JSON.parse('{"age": {"$gte": 18}, "$or": [{"name": {"$regex": "^J"}}, {"name": "Bob"}]}')
|
|
60
|
+
records.mongory.where(rule).pluck('name')
|
|
61
|
+
# => ["Jack", "Bob"]
|
|
248
62
|
```
|
|
249
63
|
|
|
250
|
-
|
|
251
|
-
Internally, the query is compiled into a matcher tree.
|
|
64
|
+
### Query Builder Methods
|
|
252
65
|
|
|
253
66
|
| Method | Description | Example |
|
|
254
67
|
|--------|-------------|---------|
|
|
255
|
-
| `where` | Adds
|
|
256
|
-
| `
|
|
257
|
-
| `
|
|
258
|
-
| `
|
|
259
|
-
| `
|
|
260
|
-
| `in` |
|
|
261
|
-
| `
|
|
262
|
-
| `
|
|
263
|
-
| `
|
|
264
|
-
| `with_context` | Sets a custom context for the query. Useful for controlling data conversion and sharing configuration across matchers. | `with_context(merchant: merchant)` |
|
|
68
|
+
| `where` | Adds an `$and` filter condition | `where(:age.gte => 18, status: 'active')` |
|
|
69
|
+
| `any_of` | Adds an `$or` branch inside the current `$and` | `any_of({ role: 'admin' }, { :score.gte => 90 })` |
|
|
70
|
+
| `or` | Combines the current query with `$or` conditions | `or({ age: 18 }, { name: 'Bob' })` |
|
|
71
|
+
| `and` | Combines conditions with `$and` | `and({ :age.gte => 18 }, { active: true })` |
|
|
72
|
+
| `not` | Negates a condition (`$not`) | `not(:status => 'banned')` |
|
|
73
|
+
| `in` / `nin` | Matches values in / not in a list | `in(role: %w[admin editor])` |
|
|
74
|
+
| `limit` | Limits processed records (executes at its position in the chain) | `limit(10)` |
|
|
75
|
+
| `pluck` | Extracts one or more fields from matching records | `pluck(:name)` |
|
|
76
|
+
| `with_context` | Passes shared context/options to matchers | `with_context(tenant: current_tenant)` |
|
|
265
77
|
|
|
266
|
-
|
|
78
|
+
## Debugging: `explain`, `trace`, `explain_tree` & `trace_tree`
|
|
267
79
|
|
|
268
|
-
|
|
80
|
+
Use `explain_tree` and `trace_tree` to inspect structured Ruby Hashes (for logging, JSON APIs, or UIs), or `explain` and `trace` to print those trees directly to STDOUT:
|
|
269
81
|
|
|
270
82
|
```ruby
|
|
271
|
-
# Share configuration across matchers
|
|
272
|
-
records.mongory
|
|
273
|
-
.with_context(custom_option: true)
|
|
274
|
-
.where(:status => 'active')
|
|
275
|
-
.where(:age.gte => 18)
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
This will share a mutatable, but stable context object to all matchers in matcher tree.
|
|
279
|
-
To get your custom option, using `@context.config` in your custom matcher.
|
|
280
|
-
|
|
281
|
-
## Debugging
|
|
282
|
-
|
|
283
|
-
You can use `explain` to visualize the matcher tree structure:
|
|
284
|
-
```ruby
|
|
285
|
-
records = [
|
|
286
|
-
{ name: 'John', age: 25, status: 'active' },
|
|
287
|
-
{ name: 'Jane', age: 30, status: 'inactive' }
|
|
288
|
-
]
|
|
289
|
-
|
|
290
83
|
query = records.mongory
|
|
291
84
|
.where(:age.gte => 18)
|
|
292
|
-
.any_of(
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
85
|
+
.any_of({ status: 'active' }, { :name.regex => /^J/ })
|
|
86
|
+
|
|
87
|
+
# Structured explain tree (Hash)
|
|
88
|
+
plan = query.explain_tree
|
|
89
|
+
# => { name: "Query", condition: ..., priority: ..., children: [...] }
|
|
296
90
|
|
|
91
|
+
# Prints the explain tree to STDOUT
|
|
297
92
|
query.explain
|
|
298
93
|
```
|
|
299
|
-
|
|
300
|
-
```
|
|
94
|
+
|
|
95
|
+
```text
|
|
301
96
|
And: {"age"=>{"$gte"=>18}, "$or"=>[{"status"=>"active"}, {"name"=>{"$regex"=>/^J/}}]}
|
|
302
97
|
├─ Field: "age" to match: {"$gte"=>18}
|
|
303
98
|
│ └─ Gte: 18
|
|
@@ -308,176 +103,80 @@ And: {"age"=>{"$gte"=>18}, "$or"=>[{"status"=>"active"}, {"name"=>{"$regex"=>/^J
|
|
|
308
103
|
└─ Regex: /^J/
|
|
309
104
|
```
|
|
310
105
|
|
|
311
|
-
|
|
106
|
+
```ruby
|
|
107
|
+
# Structured trace trees across all records (Array<Hash>);
|
|
108
|
+
# short-circuited branches are recursively expanded with result: :skip.
|
|
109
|
+
trees = query.trace_tree
|
|
110
|
+
# => [
|
|
111
|
+
# {
|
|
112
|
+
# name: "Query",
|
|
113
|
+
# condition: ...,
|
|
114
|
+
# record: { "name" => "Bob", "age" => 15, "status" => "inactive" },
|
|
115
|
+
# result: :fail, # :pass | :fail | :skip
|
|
116
|
+
# priority: ...,
|
|
117
|
+
# children: [...]
|
|
118
|
+
# },
|
|
119
|
+
# ...
|
|
120
|
+
# ]
|
|
121
|
+
|
|
122
|
+
# Prints each record's trace tree to STDOUT and yields matching records
|
|
123
|
+
query.trace { |record| puts record['name'] }
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Single-record structured tracing is also available on matchers directly via `matcher.explain_tree` and `matcher.trace_tree(record)` on `Mongory::Matcher`.
|
|
127
|
+
|
|
128
|
+
## Supported Operators
|
|
129
|
+
|
|
130
|
+
| Category | Operators | Notes |
|
|
131
|
+
|----------|-----------|-------|
|
|
132
|
+
| Comparison | `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte` | Cross-type `Integer`/`Float` equality; implicit `$elemMatch` on arrays |
|
|
133
|
+
| Inclusion | `$in`, `$nin` | Scalar membership or array intersection |
|
|
134
|
+
| Logical | `$and`, `$or`, `$not` | Priority-sorted sibling evaluation with dynamic re-sorting |
|
|
135
|
+
| Array | `$elemMatch`, `$every`, `$size` | `$every` requires a non-empty array where all elements match; `$size` accepts an integer or range object |
|
|
136
|
+
| Existence | `$exists`, `$present` | `$present` checks non-nil and non-empty (`String`, `Array`, `Hash`) |
|
|
137
|
+
| Regex | `$regex` (`$options`: `i`, `m`, `s`) | Accepts `Regexp` or pattern string with optional `$options` |
|
|
138
|
+
|
|
139
|
+
> **Differences from MongoDB:** `$every` (returns `false` on `[]`) and `$present` are Mongory extensions; `$options` only permits `i`/`m`/`s`; unknown `$` operators raise `Mongory::TypeError`. See [`mongory-spec`](https://github.com/mongoryhq/mongory-spec#37-differences-from-mongodb-query-syntax).
|
|
140
|
+
|
|
141
|
+
## Custom Matchers & Configuration
|
|
142
|
+
|
|
143
|
+
Configure registered collection classes and converters in `Mongory.configure`:
|
|
312
144
|
|
|
313
|
-
Or use trace for detailed matching process:
|
|
314
145
|
```ruby
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
146
|
+
Mongory.configure do |mc|
|
|
147
|
+
mc.enable_symbol_snippets!
|
|
148
|
+
mc.register(Array)
|
|
149
|
+
mc.register(ActiveRecord::Relation)
|
|
319
150
|
end
|
|
320
151
|
```
|
|
321
152
|
|
|
322
|
-
|
|
323
|
-
```
|
|
324
|
-
QueryMatcher Matched, condition: {"age"=>{"$gt"=>18}}, record: {"age"=>25}
|
|
325
|
-
AndMatcher Matched, condition: {"age"=>{"$gt"=>18}}, record: {"age"=>25}
|
|
326
|
-
FieldMatcher Matched, condition: {"$gt"=>18}, field: "age", record: {"age"=>25}
|
|
327
|
-
GtMatcher Matched, condition: 18, record: 25
|
|
328
|
-
```
|
|
153
|
+
Define a custom operator with `Mongory::Matchers::AbstractMatcher` (or run `rails g mongory:matcher class_in`):
|
|
329
154
|
|
|
330
|
-
The debug output includes:
|
|
331
|
-
- The matcher tree structure with full class names
|
|
332
|
-
- Each matcher's condition and record value
|
|
333
|
-
- Color-coded results (green for matched, red for mismatched, purple for errors)
|
|
334
|
-
- Field names highlighted in gray background
|
|
335
|
-
- Detailed matching process for each record
|
|
336
|
-
|
|
337
|
-
### Supported Operators
|
|
338
|
-
|
|
339
|
-
| Category | Operators |
|
|
340
|
-
|--------------|-------------------------------------|
|
|
341
|
-
| Comparison | `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte` |
|
|
342
|
-
| Set | `$in`, `$nin` |
|
|
343
|
-
| Boolean | `$and`, `$or`, `$not` |
|
|
344
|
-
| Pattern | `$regex` |
|
|
345
|
-
| Presence | `$exists`, `$present` |
|
|
346
|
-
| Nested Match | `$elemMatch`, `$every` |
|
|
347
|
-
| Other | `$size` |
|
|
348
|
-
|
|
349
|
-
Note: Some operators are Mongory-specific and not available in MongoDB:
|
|
350
|
-
- `$present`: Checks if a field is considered "present" (not nil, not empty, not KEY_NOT_FOUND)
|
|
351
|
-
- Similar to `$exists` but evaluates truthiness of the value
|
|
352
|
-
- Example: `where(:name.present => true)`
|
|
353
|
-
- `$every`: Checks if all elements in an array match the given condition
|
|
354
|
-
- Similar to `$elemMatch` but requires all elements to match
|
|
355
|
-
- At least one element in an array, or returns false
|
|
356
|
-
- Example: `where(:tags.every => { :priority.gt => 5 })`
|
|
357
|
-
|
|
358
|
-
Example:
|
|
359
155
|
```ruby
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
156
|
+
class ClassInMatcher < Mongory::Matchers::AbstractMatcher
|
|
157
|
+
def match(subject)
|
|
158
|
+
@condition.any? { |klass| subject.is_a?(klass) }
|
|
159
|
+
end
|
|
363
160
|
|
|
364
|
-
|
|
365
|
-
|
|
161
|
+
def check_validity!
|
|
162
|
+
raise Mongory::TypeError, '$classIn needs an Array of Classes' unless
|
|
163
|
+
@condition.is_a?(Array) && @condition.all? { |k| k.is_a?(Class) }
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
Mongory::Matchers.register(:class_in, '$classIn', ClassInMatcher)
|
|
366
168
|
```
|
|
367
169
|
|
|
368
|
-
##
|
|
170
|
+
## Guides & Further Reading
|
|
369
171
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
-
|
|
373
|
-
-
|
|
374
|
-
-
|
|
172
|
+
- [Handling Dots in Field Names](docs/field_names.md)
|
|
173
|
+
- [Advanced Usage & Converters](docs/advanced_usage.md)
|
|
174
|
+
- [Clang Bridge (C Extension Internals)](docs/clang_bridge.md)
|
|
175
|
+
- [Performance & Benchmarks](docs/performance.md)
|
|
176
|
+
- [Migration Guide](docs/migration.md)
|
|
375
177
|
|
|
376
|
-
|
|
377
|
-
A: Yes, but consider:
|
|
378
|
-
- Memory usage
|
|
379
|
-
- Query complexity
|
|
380
|
-
- Caching strategies
|
|
381
|
-
- Using `limit` early in the chain
|
|
178
|
+
## Contributing & License
|
|
382
179
|
|
|
383
|
-
|
|
384
|
-
```ruby
|
|
385
|
-
begin
|
|
386
|
-
result = records.mongory.where(invalid: :condition)
|
|
387
|
-
rescue Mongory::Error => e
|
|
388
|
-
# Handle error
|
|
389
|
-
end
|
|
390
|
-
```
|
|
180
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/mongoryhq/mongory-rb). Everyone interacting in the project is expected to follow the [Code of Conduct](CODE_OF_CONDUCT.md).
|
|
391
181
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
1. **Debugging Queries**
|
|
395
|
-
```ruby
|
|
396
|
-
records.mongory.where(:age => 18).trace.to_a
|
|
397
|
-
```
|
|
398
|
-
If using C extension:
|
|
399
|
-
```ruby
|
|
400
|
-
records.mongory.c.where(:age => 18).trace.to_a
|
|
401
|
-
```
|
|
402
|
-
|
|
403
|
-
2. **Common Issues**
|
|
404
|
-
- Symbol snippets not working? Call `Mongory.enable_symbol_snippets!`
|
|
405
|
-
- Complex queries slow? Use `explain` to analyze
|
|
406
|
-
- Memory issues? Consider pagination or streaming
|
|
407
|
-
|
|
408
|
-
## Best Practices
|
|
409
|
-
|
|
410
|
-
1. **Query Composition**
|
|
411
|
-
```ruby
|
|
412
|
-
# Good: Use method chaining
|
|
413
|
-
records.mongory
|
|
414
|
-
.where(:age.gte => 18)
|
|
415
|
-
.where(:status => 'active')
|
|
416
|
-
.limit(10)
|
|
417
|
-
|
|
418
|
-
# Bad: Avoid redundant query creation
|
|
419
|
-
query = records.mongory.where(:age.gte => 18)
|
|
420
|
-
query = query.where(:status => 'active') # Unnecessary
|
|
421
|
-
```
|
|
422
|
-
|
|
423
|
-
2. **Performance Tips**
|
|
424
|
-
```ruby
|
|
425
|
-
# Use limit to restrict result set
|
|
426
|
-
records.mongory.limit(100).where(:age.gte => 18)
|
|
427
|
-
|
|
428
|
-
# Use fast mode for better performance
|
|
429
|
-
records.mongory.where(:age.gte => 18).fast
|
|
430
|
-
|
|
431
|
-
# Use explain to analyze complex queries
|
|
432
|
-
query = records.mongory.where(:$or => [...])
|
|
433
|
-
query.explain
|
|
434
|
-
```
|
|
435
|
-
|
|
436
|
-
3. **Code Organization**
|
|
437
|
-
```ruby
|
|
438
|
-
# Encapsulate common queries as methods
|
|
439
|
-
class User
|
|
440
|
-
def active_adults
|
|
441
|
-
friends.mongory
|
|
442
|
-
.where(:age.gte => 18)
|
|
443
|
-
.where(:status => 'active')
|
|
444
|
-
end
|
|
445
|
-
end
|
|
446
|
-
```
|
|
447
|
-
|
|
448
|
-
## Limitations
|
|
449
|
-
|
|
450
|
-
1. **Data Size**
|
|
451
|
-
- Suitable for small to medium datasets
|
|
452
|
-
- Large datasets may impact performance
|
|
453
|
-
- Proc-based implementation helps with memory usage
|
|
454
|
-
- Context system provides better resource management
|
|
455
|
-
|
|
456
|
-
2. **Query Complexity**
|
|
457
|
-
- Complex queries may affect performance
|
|
458
|
-
- Not all MongoDB operators are supported
|
|
459
|
-
- Proc-based implementation improves complex query performance
|
|
460
|
-
- Context system allows better control over query execution
|
|
461
|
-
|
|
462
|
-
3. **Memory Usage**
|
|
463
|
-
- All operations are performed in memory
|
|
464
|
-
- Consider memory constraints
|
|
465
|
-
|
|
466
|
-
## Contributing
|
|
467
|
-
|
|
468
|
-
Contributions are welcome! Here's how you can help:
|
|
469
|
-
|
|
470
|
-
1. **Fork the repository**.
|
|
471
|
-
2. **Create a new branch** for each significant change.
|
|
472
|
-
3. **Write tests** for your changes.
|
|
473
|
-
4. **Send a pull request**.
|
|
474
|
-
|
|
475
|
-
Please ensure your code adheres to the project's style guide and that all tests pass before submitting.
|
|
476
|
-
|
|
477
|
-
## Code of Conduct
|
|
478
|
-
|
|
479
|
-
Everyone interacting in the Mongory-rb project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](https://github.com/mongoryhq/mongory-rb/blob/main/CODE_OF_CONDUCT.md).
|
|
480
|
-
|
|
481
|
-
## License
|
|
482
|
-
|
|
483
|
-
MIT. See LICENSE file.
|
|
182
|
+
Released under the [MIT License](LICENSE).
|
data/SUBMODULE_INTEGRATION.md
CHANGED
|
@@ -177,10 +177,10 @@ Debug mode will:
|
|
|
177
177
|
require 'mongory'
|
|
178
178
|
|
|
179
179
|
condition = { "age" => { "$gt" => 18 } }
|
|
180
|
-
matcher = Mongory::
|
|
180
|
+
matcher = Mongory::Matcher.new(condition)
|
|
181
181
|
|
|
182
182
|
data = { "name" => "John", "age" => 25 }
|
|
183
|
-
result = matcher.match(data) # => true/false
|
|
183
|
+
result = matcher.match?(data) # => true/false
|
|
184
184
|
|
|
185
185
|
# Optional: get explanation (runs explain on the C side, currently returns nil)
|
|
186
186
|
matcher.explain
|