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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/README.md +121 -422
  4. data/SUBMODULE_INTEGRATION.md +2 -2
  5. data/docs/clang_bridge.md +28 -59
  6. data/docs/migration.md +17 -0
  7. data/docs/performance.md +14 -51
  8. data/examples/benchmark.rb +10 -10
  9. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +62 -0
  10. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +31 -0
  11. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +3 -0
  12. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +20 -0
  13. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +31 -4
  14. data/ext/mongory_ext/mongory-core/src/foundations/array.c +48 -6
  15. data/ext/mongory_ext/mongory-core/src/foundations/config.c +30 -2
  16. data/ext/mongory_ext/mongory-core/src/foundations/error.c +10 -0
  17. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +78 -19
  18. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +73 -19
  19. data/ext/mongory_ext/mongory-core/src/foundations/table.c +8 -3
  20. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +44 -55
  21. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +7 -0
  22. data/ext/mongory_ext/mongory-core/src/foundations/value.c +3 -0
  23. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +69 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +15 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +9 -4
  26. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +167 -31
  27. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +7 -1
  28. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.c → existence_matcher.c} +10 -12
  29. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.h → existence_matcher.h} +4 -4
  30. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +20 -6
  31. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.c → implicit_array_matcher.c} +57 -38
  32. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.h → implicit_array_matcher.h} +5 -5
  33. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +8 -10
  34. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +337 -96
  35. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +59 -21
  36. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +7 -1
  37. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +6 -5
  38. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -1
  39. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.c → value_matcher.c} +120 -60
  40. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.h → value_matcher.h} +13 -13
  41. data/ext/mongory_ext/mongory_ext.c +2 -2
  42. data/ext/mongory_ext/mongory_ext_internal.h +2 -2
  43. data/ext/mongory_ext/{rb_mongory_cmatcher.c → rb_mongory_matcher.c} +157 -23
  44. data/lib/core/2.6/mongory_ext.so +0 -0
  45. data/lib/core/2.7/mongory_ext.so +0 -0
  46. data/lib/core/3.0/mongory_ext.so +0 -0
  47. data/lib/core/3.1/mongory_ext.so +0 -0
  48. data/lib/core/3.2/mongory_ext.so +0 -0
  49. data/lib/core/3.3/mongory_ext.so +0 -0
  50. data/lib/core/3.4/mongory_ext.so +0 -0
  51. data/lib/core/4.0/mongory_ext.so +0 -0
  52. data/lib/generators/mongory/install/templates/initializer.rb.erb +0 -5
  53. data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
  54. data/lib/mongory/converters/converted.rb +28 -6
  55. data/lib/mongory/converters/key_converter.rb +2 -0
  56. data/lib/mongory/matcher.rb +63 -0
  57. data/lib/mongory/matchers/abstract_matcher.rb +18 -164
  58. data/lib/mongory/matchers.rb +68 -84
  59. data/lib/mongory/query_builder.rb +30 -41
  60. data/lib/mongory/utils/tree_renderer.rb +108 -0
  61. data/lib/mongory/utils.rb +2 -67
  62. data/lib/mongory/version.rb +1 -1
  63. data/lib/mongory.rb +9 -59
  64. data/script/bench.rb +54 -0
  65. metadata +13 -37
  66. data/lib/mongory/c_matcher.rb +0 -57
  67. data/lib/mongory/c_query_builder.rb +0 -44
  68. data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -124
  69. data/lib/mongory/matchers/and_matcher.rb +0 -72
  70. data/lib/mongory/matchers/array_record_matcher.rb +0 -105
  71. data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
  72. data/lib/mongory/matchers/eq_matcher.rb +0 -48
  73. data/lib/mongory/matchers/every_matcher.rb +0 -56
  74. data/lib/mongory/matchers/exists_matcher.rb +0 -53
  75. data/lib/mongory/matchers/field_matcher.rb +0 -160
  76. data/lib/mongory/matchers/gt_matcher.rb +0 -41
  77. data/lib/mongory/matchers/gte_matcher.rb +0 -41
  78. data/lib/mongory/matchers/hash_condition_matcher.rb +0 -79
  79. data/lib/mongory/matchers/in_matcher.rb +0 -84
  80. data/lib/mongory/matchers/literal_matcher.rb +0 -121
  81. data/lib/mongory/matchers/lt_matcher.rb +0 -41
  82. data/lib/mongory/matchers/lte_matcher.rb +0 -41
  83. data/lib/mongory/matchers/ne_matcher.rb +0 -39
  84. data/lib/mongory/matchers/nin_matcher.rb +0 -64
  85. data/lib/mongory/matchers/not_matcher.rb +0 -40
  86. data/lib/mongory/matchers/or_matcher.rb +0 -68
  87. data/lib/mongory/matchers/present_matcher.rb +0 -55
  88. data/lib/mongory/matchers/regex_matcher.rb +0 -109
  89. data/lib/mongory/matchers/size_matcher.rb +0 -54
  90. data/lib/mongory/query_matcher.rb +0 -93
  91. data/lib/mongory/rails.rb +0 -15
  92. data/lib/mongory/utils/debugger.rb +0 -125
  93. data/lib/mongory/utils/rails_patch.rb +0 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2144ae1fbdc5a722e784cc0510d1c8a9c0c6fb8dd20105a59fc56ced9defa0e7
4
- data.tar.gz: 3615514859a9f09c2dc047d1e7e131022d6eed5d787176ecf16f6c398500d6af
3
+ metadata.gz: e64ae2eabef6a6670ec7d9efc0d963282630638aed6690cbc50cad00fa23604a
4
+ data.tar.gz: f47534dac649a1216b3f0557db8bed374628db16d8ae77800987bd09165481c7
5
5
  SHA512:
6
- metadata.gz: 7b636f0b7a96e631dcb0438287546a0ee513ff39f5275b795799fcbf2a70ffd4964fa31af1180858bbec153716dc85d87c33dabab05b10565a18eac89f605ba8
7
- data.tar.gz: ebb6ea699893f18d00e68a9b5026d776c6975cf65aa2c73e2706b2ee74eb96b9b99671b71a29e62710c4db9cf8a6bb82856381f74157d98bfcae44403920e273
6
+ metadata.gz: 325e796ad086ccdfe8109d49ab3190982495dcc1a3e490709a70f9857cc93c02b558a205ea51dd27f6745bf4b541257b1137b12dd56c08c3ada90c3de577de45
7
+ data.tar.gz: 74b1de73cbf99c2e1193b21d2a283d41fde9dff1f4cbd820b998b5679aa27fefc7725e4fb6d1c1f1c3ae20616698f176128e1df3c5bcc36713e27fb23eeddee5
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
- Or add to your Gemfile:
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
- #### Before installing: install build tools
5
+ **[Try the Interactive Playground & Visual Explain/Trace Debugger ↗](https://mongoryhq.github.io/mongory-central/)**
56
6
 
57
- Mongory ships with an optional native C extension.
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
+ [![Mongory Playground & Visual Trace](https://mongoryhq.github.io/mongory-central/playground-trace.png)](https://mongoryhq.github.io/mongory-central/)
67
8
 
68
- If your platform excludes, make sure your system has a C build toolchain (gcc/clang and make). Install the toolchain with the following commands for your platform:
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
- - Debian/Ubuntu (including ruby:*-slim base images)
71
- ```bash
72
- apt-get update && apt-get install -y build-essential
73
- ```
12
+ ## Features
74
13
 
75
- - Alpine
76
- ```bash
77
- apk add --no-cache build-base
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
- - CentOS/RHEL
81
- ```bash
82
- yum groupinstall -y "Development Tools"
83
- ```
20
+ ## Installation
84
21
 
85
- - Fedora
86
22
  ```bash
87
- dnf groupinstall -y "Development Tools"
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
- #### Rails Generator
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
- This will generate `config/initializers/mongory.rb` and set up:
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
- If you prefer to create matchers manually, here's an example:
36
+ ## Quick Start
215
37
 
216
38
  ```ruby
217
- class ClassInMatcher < Mongory::Matchers::AbstractMatcher
218
- def match(subject)
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
- [{a: 1}].mongory.where(:a.class_in => [Integer]).first
233
- # => { a: 1 }
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
- ## Core Concepts & API Reference
240
- #### Registering Models
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
- To allow calling `.mongory` on collections, use `register`:
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
- ```ruby
245
- Mongory.register(Array)
246
- Mongory.register(ActiveRecord::Relation)
247
- User.where(status: 'active').mongory.where(:age.gte => 18, :name.regex => "^S.+")
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
- This injects a `.mongory` method via an internal extension module.
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 a condition to filter records | `where(age: { :$gte => 18 })` |
256
- | `not` | Adds a negated condition | `not(age: { :$lt => 18 })` |
257
- | `and` | Combines conditions with `$and` | `and({ age: { :$gte => 18 } }, { name: /J/ })` |
258
- | `or` | Combines conditions with `$or` | `or({ age: { :$gte => 18 } }, { name: /J/ })` |
259
- | `any_of` | Combines conditions with `$or` inside an `$and` block | `any_of({ age: { :$gte => 18 } }, { name: /J/ })` |
260
- | `in` | Checks if a value is in a set | `in(age: [18, 19, 20])` |
261
- | `nin` | Checks if a value is not in a set | `nin(age: [18, 19, 20])` |
262
- | `limit` | Limits the number of records returned. This method executes immediately and affects subsequent conditions. | `limit(2)` |
263
- | `pluck` | Extracts selected fields from matching records | `pluck(:name)` |
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
- #### Context Configuration
78
+ ## Debugging: `explain`, `trace`, `explain_tree` & `trace_tree`
267
79
 
268
- The `with_context` method allows you to customize the query execution environment:
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
- { :status => 'active' },
294
- { :name.regex => /^J/ }
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
- Output:
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
- This helps you understand how your query is being processed and can be useful for debugging complex conditions.
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
- # Execute your query
316
- query = Mongory.build_query(users).where(age: { :$gt => 18 })
317
- query.trace do |user|
318
- puts user
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
- The debug output will show detailed matching process with full class names:
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
- # $present: Check if field is present (not nil, not empty)
361
- records.mongory.where(:name.present => true) # name is present
362
- records.mongory.where(:name.present => false) # name is not present
156
+ class ClassInMatcher < Mongory::Matchers::AbstractMatcher
157
+ def match(subject)
158
+ @condition.any? { |klass| subject.is_a?(klass) }
159
+ end
363
160
 
364
- # $every: Check if all array elements match condition
365
- records.mongory.where(:tags.every => { :priority.gt => 5 }) # all tags have priority > 5
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
- ## FAQ
170
+ ## Guides & Further Reading
369
171
 
370
- ### Q: How does Mongory compare to MongoDB?
371
- A: Mongory provides similar query syntax but operates entirely in memory. It's ideal for:
372
- - Small to medium datasets
373
- - Complex in-memory filtering
374
- - Testing MongoDB-like queries without a database
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
- ### Q: Can I use Mongory with large datasets?
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
- ### Q: How do I handle errors?
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
- ## Troubleshooting
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).
@@ -177,10 +177,10 @@ Debug mode will:
177
177
  require 'mongory'
178
178
 
179
179
  condition = { "age" => { "$gt" => 18 } }
180
- matcher = Mongory::CMatcher.new(condition)
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