mongory 0.1.0__tar.gz
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.
- mongory-0.1.0/LICENSE +21 -0
- mongory-0.1.0/MANIFEST.in +8 -0
- mongory-0.1.0/PKG-INFO +153 -0
- mongory-0.1.0/README.md +126 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/benchmarks/basic_test.c +85 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/benchmarks/bench_suite.c +206 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +122 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +186 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +79 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +95 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +127 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +175 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +103 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/include/mongory-core.h +12 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/array.c +294 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/array_private.h +20 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/config.c +307 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/config_private.h +49 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/error.c +38 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +347 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +45 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +63 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +43 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/table.c +497 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/utils.c +217 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/utils.h +70 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/foundations/value.c +545 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +125 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +103 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +241 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +83 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +627 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +131 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +160 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.h +48 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +187 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +58 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +185 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.h +47 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +126 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +46 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/matcher.c +504 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +106 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +29 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +61 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +23 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +376 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +97 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/test_helper/test_helper.c +233 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/src/test_helper/test_helper.h +34 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/conformance/mongory_spec_conformance.c +380 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/fuzz/fuzz_matcher.c +223 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_and_matcher_test.c +19 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_array_test.c +141 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_compare_matcher_test.c +158 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_condition_matcher_test.c +470 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_config_test.c +118 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_elem_match_matcher_test.c +19 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_every_matcher_test.c +19 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_existence_matcher_test.c +99 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_inclusion_matcher_test.c +139 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_memory_pool_test.c +160 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_or_matcher_test.c +19 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_regex_matcher_test.c +173 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_string_buffer_test.c +175 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_table_test.c +132 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_thread_safety_test.c +303 -0
- mongory-0.1.0/ext/mongory_ext/mongory-core/tests/mongory_value_test.c +282 -0
- mongory-0.1.0/ext/mongory_ext/mongory_ext.c +229 -0
- mongory-0.1.0/ext/mongory_ext/mongory_ext_internal.h +119 -0
- mongory-0.1.0/ext/mongory_ext/py_mongory_adapters.c +245 -0
- mongory-0.1.0/ext/mongory_ext/py_mongory_cmatcher.c +331 -0
- mongory-0.1.0/ext/mongory_ext/py_mongory_value.c +373 -0
- mongory-0.1.0/pyproject.toml +47 -0
- mongory-0.1.0/setup.cfg +4 -0
- mongory-0.1.0/setup.py +33 -0
- mongory-0.1.0/src/mongory/__init__.py +242 -0
- mongory-0.1.0/src/mongory/__init__.pyi +62 -0
- mongory-0.1.0/src/mongory/_normalize.py +267 -0
- mongory-0.1.0/src/mongory/_tree.py +64 -0
- mongory-0.1.0/src/mongory/op.py +66 -0
- mongory-0.1.0/src/mongory/op.pyi +20 -0
- mongory-0.1.0/src/mongory/operator.py +23 -0
- mongory-0.1.0/src/mongory/operator.pyi +20 -0
- mongory-0.1.0/src/mongory/py.typed +0 -0
- mongory-0.1.0/src/mongory.egg-info/PKG-INFO +153 -0
- mongory-0.1.0/src/mongory.egg-info/SOURCES.txt +90 -0
- mongory-0.1.0/src/mongory.egg-info/dependency_links.txt +1 -0
- mongory-0.1.0/src/mongory.egg-info/top_level.txt +1 -0
- mongory-0.1.0/tests/test_conformance.py +89 -0
- mongory-0.1.0/tests/test_fuzz.py +70 -0
- mongory-0.1.0/tests/test_matcher.py +327 -0
mongory-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 Frank Liao (mongoryhq)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
include LICENSE
|
|
2
|
+
include README.md
|
|
3
|
+
include pyproject.toml
|
|
4
|
+
include setup.py
|
|
5
|
+
recursive-include ext/mongory_ext *.c *.h
|
|
6
|
+
recursive-include src/mongory *.py *.pyi py.typed
|
|
7
|
+
recursive-include tests *.py
|
|
8
|
+
global-exclude __pycache__ *.py[cod] *.so *.pyd *.dylib *.bundle
|
mongory-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mongory
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: C99-accelerated in-memory Mongo-style rule engine & query matcher with explain and trace debugging
|
|
5
|
+
Author-email: Frank Liao <koten0224@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://mongoryhq.github.io/mongory-central/
|
|
8
|
+
Project-URL: Repository, https://github.com/mongoryhq/mongory-py
|
|
9
|
+
Project-URL: Issues, https://github.com/mongoryhq/mongory-py/issues
|
|
10
|
+
Keywords: mongory,mongodb,query,matcher,filter,rule-engine,in-memory,c-extension
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: C
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# mongory-py
|
|
29
|
+
|
|
30
|
+
**C99-accelerated Python in-memory Mongo-style rule engine & query matcher** with built-in **Explain & Trace** debugging — powered by [`mongory-core`](https://github.com/mongoryhq/mongory-core) and verified against [`mongory-spec`](https://github.com/mongoryhq/mongory-spec).
|
|
31
|
+
|
|
32
|
+
**[Try the Interactive Playground & Visual Explain/Trace Debugger ↗](https://mongoryhq.github.io/mongory-central/)**
|
|
33
|
+
|
|
34
|
+
[](https://mongoryhq.github.io/mongory-central/)
|
|
35
|
+
|
|
36
|
+
> **Zero database required:** Compile a PyMongo-style `dict` filter once into a reusable `mongory.Matcher` backed by `mongory-core`'s C99 arena engine and evaluate Python `dict`s, `dataclass`es, or custom objects in memory.
|
|
37
|
+
> **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-rb` (Ruby)](https://github.com/mongoryhq/mongory-rb) · [`mongory-core` (C99)](https://github.com/mongoryhq/mongory-core) · [`mongory-spec`](https://github.com/mongoryhq/mongory-spec)
|
|
38
|
+
|
|
39
|
+
## Features
|
|
40
|
+
|
|
41
|
+
- **Native C99 Engine (`mongory-core`)**: Shallow `PyObject*` wrapping with arena memory pools and zero runtime Python dependencies.
|
|
42
|
+
- **PyMongo & `re.compile` Ergonomics**: Pass raw `dict` conditions (including compiled `re.Pattern` objects), use `mongory.compile(cond)` or one-shot `mongory.match(cond, record)`, and pass `Matcher` directly to Python's built-in `filter(matcher, records)`.
|
|
43
|
+
- **Operator Constants (`mongory.op` / `mongory.operator`)**: Optional uppercase constants (`op.GTE`, `op.IN`, `op.OR`, `op.ELEM_MATCH`, etc.) for typo-free conditions with zero overhead.
|
|
44
|
+
- **Built-in `explain()` / `trace()` & `explain_tree()` / `trace_tree()`**: Inspect priority-ordered execution plans and per-record `PASS` / `FAIL` / `SKIP` evaluation trees.
|
|
45
|
+
- **Compile-Scoped Extensions & Safety Limits**: Custom `converters`, custom `operators`, `max_depth` (default `64`), `max_nodes` (default `4096`), and full PEP 561 `.pyi` type stubs.
|
|
46
|
+
|
|
47
|
+
## Installation
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
pip install mongory
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Requires Python 3.10 or later.
|
|
54
|
+
|
|
55
|
+
## Quick Start
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
import re
|
|
59
|
+
import mongory
|
|
60
|
+
from mongory import op
|
|
61
|
+
|
|
62
|
+
users = [
|
|
63
|
+
{"name": "Ann", "age": 30, "tags": ["py", "go"], "active": True},
|
|
64
|
+
{"name": "Bob", "age": 15, "tags": ["ruby"], "active": True},
|
|
65
|
+
{"name": "Cid", "age": 42, "tags": ["py", "c"], "active": False},
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
# 1. One-shot matching
|
|
69
|
+
is_adult = mongory.match({"age": {op.GTE: 18}}, users[0]) # True
|
|
70
|
+
|
|
71
|
+
# 2. Compile once, reuse across collections / streams
|
|
72
|
+
matcher = mongory.compile(
|
|
73
|
+
mongory.and_(
|
|
74
|
+
{"active": True},
|
|
75
|
+
mongory.or_(
|
|
76
|
+
{"tags": "py"},
|
|
77
|
+
{"name": re.compile(r"^b", re.IGNORECASE)},
|
|
78
|
+
),
|
|
79
|
+
)
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
matched = list(matcher.filter(users)) # [Ann, Bob]
|
|
83
|
+
top1 = list(matcher.filter(users, limit=1)) # [Ann]
|
|
84
|
+
first = matcher.find_one(users) # Ann
|
|
85
|
+
total = matcher.count(users) # 2
|
|
86
|
+
|
|
87
|
+
# Matcher also implements __call__:
|
|
88
|
+
via_builtin = list(filter(matcher, users)) # [Ann, Bob]
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Debugging with `explain` & `trace`
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
matcher = mongory.compile({
|
|
95
|
+
"name": {op.REGEX: "^A"},
|
|
96
|
+
"age": {op.GTE: 18},
|
|
97
|
+
"active": True,
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
print(matcher.explain())
|
|
101
|
+
|
|
102
|
+
matched, trace_str = matcher.trace({"name": "Bob", "age": 15, "active": False})
|
|
103
|
+
print(matched) # False
|
|
104
|
+
print(trace_str)
|
|
105
|
+
|
|
106
|
+
# Or retrieve structured dicts for logging / UIs:
|
|
107
|
+
plan_tree = matcher.explain_tree()
|
|
108
|
+
matched, trace_tree = matcher.trace_tree({"name": "Bob", "age": 15, "active": False})
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Compile-Scoped Custom Operators & Converters
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
import mongory
|
|
115
|
+
from mongory import op
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class Money:
|
|
119
|
+
def __init__(self, currency: str, amount: int) -> None:
|
|
120
|
+
self.currency = currency
|
|
121
|
+
self.amount = amount
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def compile_money_gte(target: Money):
|
|
125
|
+
if not isinstance(target, Money):
|
|
126
|
+
raise ValueError("$moneyGte requires Money")
|
|
127
|
+
return lambda val, exists: (
|
|
128
|
+
exists
|
|
129
|
+
and isinstance(val, Money)
|
|
130
|
+
and val.currency == target.currency
|
|
131
|
+
and val.amount >= target.amount
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
matcher = mongory.compile(
|
|
136
|
+
{"price": {"$moneyGte": Money("USD", 100)}, "tags": {op.IN: ["vip"]}},
|
|
137
|
+
converters={set: sorted},
|
|
138
|
+
operators={"$moneyGte": compile_money_gte},
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
print(matcher.match({"price": Money("USD", 150), "tags": {"vip", "new"}})) # True
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Development & Conformance Tests
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
python3 scripts/build_ext.py
|
|
148
|
+
MONGORY_SPEC_DIR=../mongory-spec python3 -m unittest discover -s tests -v
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## License
|
|
152
|
+
|
|
153
|
+
MIT
|
mongory-0.1.0/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# mongory-py
|
|
2
|
+
|
|
3
|
+
**C99-accelerated Python in-memory Mongo-style rule engine & query matcher** with built-in **Explain & Trace** debugging — powered by [`mongory-core`](https://github.com/mongoryhq/mongory-core) and verified against [`mongory-spec`](https://github.com/mongoryhq/mongory-spec).
|
|
4
|
+
|
|
5
|
+
**[Try the Interactive Playground & Visual Explain/Trace Debugger ↗](https://mongoryhq.github.io/mongory-central/)**
|
|
6
|
+
|
|
7
|
+
[](https://mongoryhq.github.io/mongory-central/)
|
|
8
|
+
|
|
9
|
+
> **Zero database required:** Compile a PyMongo-style `dict` filter once into a reusable `mongory.Matcher` backed by `mongory-core`'s C99 arena engine and evaluate Python `dict`s, `dataclass`es, or custom objects in memory.
|
|
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-rb` (Ruby)](https://github.com/mongoryhq/mongory-rb) · [`mongory-core` (C99)](https://github.com/mongoryhq/mongory-core) · [`mongory-spec`](https://github.com/mongoryhq/mongory-spec)
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- **Native C99 Engine (`mongory-core`)**: Shallow `PyObject*` wrapping with arena memory pools and zero runtime Python dependencies.
|
|
15
|
+
- **PyMongo & `re.compile` Ergonomics**: Pass raw `dict` conditions (including compiled `re.Pattern` objects), use `mongory.compile(cond)` or one-shot `mongory.match(cond, record)`, and pass `Matcher` directly to Python's built-in `filter(matcher, records)`.
|
|
16
|
+
- **Operator Constants (`mongory.op` / `mongory.operator`)**: Optional uppercase constants (`op.GTE`, `op.IN`, `op.OR`, `op.ELEM_MATCH`, etc.) for typo-free conditions with zero overhead.
|
|
17
|
+
- **Built-in `explain()` / `trace()` & `explain_tree()` / `trace_tree()`**: Inspect priority-ordered execution plans and per-record `PASS` / `FAIL` / `SKIP` evaluation trees.
|
|
18
|
+
- **Compile-Scoped Extensions & Safety Limits**: Custom `converters`, custom `operators`, `max_depth` (default `64`), `max_nodes` (default `4096`), and full PEP 561 `.pyi` type stubs.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install mongory
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Requires Python 3.10 or later.
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
import re
|
|
32
|
+
import mongory
|
|
33
|
+
from mongory import op
|
|
34
|
+
|
|
35
|
+
users = [
|
|
36
|
+
{"name": "Ann", "age": 30, "tags": ["py", "go"], "active": True},
|
|
37
|
+
{"name": "Bob", "age": 15, "tags": ["ruby"], "active": True},
|
|
38
|
+
{"name": "Cid", "age": 42, "tags": ["py", "c"], "active": False},
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
# 1. One-shot matching
|
|
42
|
+
is_adult = mongory.match({"age": {op.GTE: 18}}, users[0]) # True
|
|
43
|
+
|
|
44
|
+
# 2. Compile once, reuse across collections / streams
|
|
45
|
+
matcher = mongory.compile(
|
|
46
|
+
mongory.and_(
|
|
47
|
+
{"active": True},
|
|
48
|
+
mongory.or_(
|
|
49
|
+
{"tags": "py"},
|
|
50
|
+
{"name": re.compile(r"^b", re.IGNORECASE)},
|
|
51
|
+
),
|
|
52
|
+
)
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
matched = list(matcher.filter(users)) # [Ann, Bob]
|
|
56
|
+
top1 = list(matcher.filter(users, limit=1)) # [Ann]
|
|
57
|
+
first = matcher.find_one(users) # Ann
|
|
58
|
+
total = matcher.count(users) # 2
|
|
59
|
+
|
|
60
|
+
# Matcher also implements __call__:
|
|
61
|
+
via_builtin = list(filter(matcher, users)) # [Ann, Bob]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Debugging with `explain` & `trace`
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
matcher = mongory.compile({
|
|
68
|
+
"name": {op.REGEX: "^A"},
|
|
69
|
+
"age": {op.GTE: 18},
|
|
70
|
+
"active": True,
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
print(matcher.explain())
|
|
74
|
+
|
|
75
|
+
matched, trace_str = matcher.trace({"name": "Bob", "age": 15, "active": False})
|
|
76
|
+
print(matched) # False
|
|
77
|
+
print(trace_str)
|
|
78
|
+
|
|
79
|
+
# Or retrieve structured dicts for logging / UIs:
|
|
80
|
+
plan_tree = matcher.explain_tree()
|
|
81
|
+
matched, trace_tree = matcher.trace_tree({"name": "Bob", "age": 15, "active": False})
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Compile-Scoped Custom Operators & Converters
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
import mongory
|
|
88
|
+
from mongory import op
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class Money:
|
|
92
|
+
def __init__(self, currency: str, amount: int) -> None:
|
|
93
|
+
self.currency = currency
|
|
94
|
+
self.amount = amount
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def compile_money_gte(target: Money):
|
|
98
|
+
if not isinstance(target, Money):
|
|
99
|
+
raise ValueError("$moneyGte requires Money")
|
|
100
|
+
return lambda val, exists: (
|
|
101
|
+
exists
|
|
102
|
+
and isinstance(val, Money)
|
|
103
|
+
and val.currency == target.currency
|
|
104
|
+
and val.amount >= target.amount
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
matcher = mongory.compile(
|
|
109
|
+
{"price": {"$moneyGte": Money("USD", 100)}, "tags": {op.IN: ["vip"]}},
|
|
110
|
+
converters={set: sorted},
|
|
111
|
+
operators={"$moneyGte": compile_money_gte},
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
print(matcher.match({"price": Money("USD", 150), "tags": {"vip", "new"}})) # True
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Development & Conformance Tests
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
python3 scripts/build_ext.py
|
|
121
|
+
MONGORY_SPEC_DIR=../mongory-spec python3 -m unittest discover -s tests -v
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
MIT
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
#include "../src/test_helper/test_helper.h"
|
|
2
|
+
#include <mongory-core.h>
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
#include <stdlib.h>
|
|
5
|
+
#include <string.h>
|
|
6
|
+
#include <time.h>
|
|
7
|
+
|
|
8
|
+
void setUp(void) {}
|
|
9
|
+
void tearDown(void) {}
|
|
10
|
+
|
|
11
|
+
char *valid_statuses[] = {"active", "inactive"};
|
|
12
|
+
|
|
13
|
+
cJSON *mongory_create_json_test_record(void) {
|
|
14
|
+
cJSON *root = cJSON_CreateObject();
|
|
15
|
+
int age = rand() % 100 + 1;
|
|
16
|
+
int status_index = rand() % 2;
|
|
17
|
+
char *status = valid_statuses[status_index];
|
|
18
|
+
cJSON_AddNumberToObject(root, "age", age);
|
|
19
|
+
cJSON_AddStringToObject(root, "status", status);
|
|
20
|
+
if (age >= 18 || strcmp(status, "active") == 0) {
|
|
21
|
+
cJSON_AddTrueToObject(root, "expected");
|
|
22
|
+
} else {
|
|
23
|
+
cJSON_AddFalseToObject(root, "expected");
|
|
24
|
+
}
|
|
25
|
+
return root;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
bool mongory_array_benchmark_match_test(mongory_value *item, void *acc) {
|
|
29
|
+
mongory_matcher *matcher = (mongory_matcher *)acc;
|
|
30
|
+
cJSON *json_record = (cJSON *)item;
|
|
31
|
+
cJSON *expected_item = cJSON_GetObjectItem(json_record, "expected");
|
|
32
|
+
if (expected_item == NULL) {
|
|
33
|
+
printf("Expected item is NULL\n");
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
mongory_memory_pool *record_pool = mongory_memory_pool_new();
|
|
37
|
+
mongory_value *json_value = cjson_to_mongory_value_shallow_convert(record_pool, json_record);
|
|
38
|
+
bool result = matcher->match(matcher, json_value);
|
|
39
|
+
int expected = cJSON_IsTrue(expected_item);
|
|
40
|
+
if (result != expected) {
|
|
41
|
+
printf("Result: %d, Expected: %d\n", result, expected);
|
|
42
|
+
char *json_string = cJSON_Print(json_record);
|
|
43
|
+
printf("JSON string: %s\n", json_string);
|
|
44
|
+
free(json_string);
|
|
45
|
+
}
|
|
46
|
+
record_pool->free(record_pool);
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
bool mongory_array_free_cjson_object(mongory_value *item, void *acc) {
|
|
51
|
+
(void)acc;
|
|
52
|
+
cJSON *json_record = (cJSON *)item;
|
|
53
|
+
cJSON_Delete(json_record);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
int main() {
|
|
58
|
+
srand(time(NULL));
|
|
59
|
+
mongory_init();
|
|
60
|
+
mongory_value_converter_shallow_convert_set((mongory_shallow_convert_func)cjson_to_mongory_value_shallow_convert);
|
|
61
|
+
mongory_value_converter_deep_convert_set((mongory_deep_convert_func)cjson_to_mongory_value_deep_convert);
|
|
62
|
+
|
|
63
|
+
mongory_memory_pool *matcher_pool = mongory_memory_pool_new();
|
|
64
|
+
mongory_value *condition =
|
|
65
|
+
json_string_to_mongory_value(matcher_pool, "{\"$or\": [{\"age\": {\"$gte\": 18}}, {\"status\": \"active\"}]}");
|
|
66
|
+
mongory_matcher *matcher = mongory_matcher_new(matcher_pool, condition, NULL);
|
|
67
|
+
mongory_array *json_array = mongory_array_new(matcher_pool);
|
|
68
|
+
|
|
69
|
+
int times = 100000;
|
|
70
|
+
for (int i = 0; i < times; i++) {
|
|
71
|
+
json_array->push(json_array, (mongory_value *)mongory_create_json_test_record());
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
printf("Starting benchmark...\n");
|
|
75
|
+
double time_start = clock();
|
|
76
|
+
json_array->each(json_array, matcher, mongory_array_benchmark_match_test);
|
|
77
|
+
double time_end = clock();
|
|
78
|
+
printf("Time taken: %f seconds\n", (time_end - time_start) / CLOCKS_PER_SEC);
|
|
79
|
+
printf("Benchmark done.\n");
|
|
80
|
+
|
|
81
|
+
json_array->each(json_array, NULL, mongory_array_free_cjson_object);
|
|
82
|
+
matcher_pool->free(matcher_pool);
|
|
83
|
+
mongory_cleanup();
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
#include <mongory-core.h>
|
|
2
|
+
#include <stdio.h>
|
|
3
|
+
#include <stdlib.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
#include <time.h>
|
|
6
|
+
|
|
7
|
+
#define BENCH_RECORDS 10000
|
|
8
|
+
#define BENCH_ROUNDS 200
|
|
9
|
+
|
|
10
|
+
typedef bool (*plain_pred_fn)(mongory_value *record, void *ctx);
|
|
11
|
+
|
|
12
|
+
typedef struct {
|
|
13
|
+
const char *name;
|
|
14
|
+
mongory_value *cond;
|
|
15
|
+
plain_pred_fn plain;
|
|
16
|
+
} bench_case;
|
|
17
|
+
|
|
18
|
+
static double elapsed_ns(struct timespec start, struct timespec end) {
|
|
19
|
+
return (double)(end.tv_sec - start.tv_sec) * 1e9 + (double)(end.tv_nsec - start.tv_nsec);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static char *pool_strdup(mongory_memory_pool *pool, const char *s) {
|
|
23
|
+
size_t len = strlen(s) + 1;
|
|
24
|
+
char *copy = (char *)pool->alloc(pool, len);
|
|
25
|
+
memcpy(copy, s, len);
|
|
26
|
+
return copy;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static inline mongory_value *tbl_get(mongory_value *v, const char *k) {
|
|
30
|
+
if (!v || v->type != MONGORY_TYPE_TABLE) return NULL;
|
|
31
|
+
return v->data.t->get(v->data.t, (char *)k);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static bool plain_simple(mongory_value *rec, void *ctx) {
|
|
35
|
+
(void)ctx;
|
|
36
|
+
mongory_value *age = tbl_get(rec, "age");
|
|
37
|
+
return age && age->type == MONGORY_TYPE_INT && age->data.i >= 18;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static bool plain_or(mongory_value *rec, void *ctx) {
|
|
41
|
+
(void)ctx;
|
|
42
|
+
if (plain_simple(rec, NULL)) return true;
|
|
43
|
+
mongory_value *status = tbl_get(rec, "status");
|
|
44
|
+
return status && status->type == MONGORY_TYPE_STRING && strcmp(status->data.s, "active") == 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static bool plain_nested_and(mongory_value *rec, void *ctx) {
|
|
48
|
+
(void)ctx;
|
|
49
|
+
mongory_value *status = tbl_get(rec, "status");
|
|
50
|
+
if (!status || status->type != MONGORY_TYPE_STRING || strcmp(status->data.s, "active") != 0) return false;
|
|
51
|
+
mongory_value *age = tbl_get(rec, "age");
|
|
52
|
+
if (!age || age->type != MONGORY_TYPE_INT || age->data.i >= 40) return false;
|
|
53
|
+
mongory_value *profile = tbl_get(rec, "profile");
|
|
54
|
+
mongory_value *city = tbl_get(profile, "city");
|
|
55
|
+
return city && city->type == MONGORY_TYPE_STRING && strcmp(city->data.s, "taipei") == 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static bool plain_in_array_field(mongory_value *rec, void *ctx) {
|
|
59
|
+
(void)ctx;
|
|
60
|
+
mongory_value *tags = tbl_get(rec, "tags");
|
|
61
|
+
if (!tags || tags->type != MONGORY_TYPE_ARRAY) return false;
|
|
62
|
+
mongory_array *arr = tags->data.a;
|
|
63
|
+
for (size_t i = 0; i < arr->count; i++) {
|
|
64
|
+
mongory_value *el = arr->get(arr, i);
|
|
65
|
+
if (el && el->type == MONGORY_TYPE_STRING &&
|
|
66
|
+
(strcmp(el->data.s, "go") == 0 || strcmp(el->data.s, "rust") == 0)) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static bool plain_large_in_500(mongory_value *rec, void *ctx) {
|
|
74
|
+
mongory_array *list = (mongory_array *)ctx;
|
|
75
|
+
mongory_value *name = tbl_get(rec, "name");
|
|
76
|
+
if (!name || name->type != MONGORY_TYPE_STRING) return false;
|
|
77
|
+
for (size_t i = 0; i < list->count; i++) {
|
|
78
|
+
mongory_value *item = list->get(list, i);
|
|
79
|
+
if (item && item->type == MONGORY_TYPE_STRING && strcmp(name->data.s, item->data.s) == 0) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
int main(void) {
|
|
87
|
+
mongory_init();
|
|
88
|
+
mongory_memory_pool *pool = mongory_memory_pool_new();
|
|
89
|
+
|
|
90
|
+
mongory_value **records = (mongory_value **)malloc(sizeof(mongory_value *) * BENCH_RECORDS);
|
|
91
|
+
for (int i = 0; i < BENCH_RECORDS; i++) {
|
|
92
|
+
mongory_value *profile = MG_TABLE_WRAP(pool, 1, "city", mongory_value_wrap_s(pool, (i % 2 == 0) ? "taipei" : "tokyo"));
|
|
93
|
+
mongory_value *tags = (i % 4 == 0)
|
|
94
|
+
? MG_ARRAY_WRAP(pool, 2, mongory_value_wrap_s(pool, "go"), mongory_value_wrap_s(pool, "ts"))
|
|
95
|
+
: MG_ARRAY_WRAP(pool, 2, mongory_value_wrap_s(pool, "ruby"), mongory_value_wrap_s(pool, "c"));
|
|
96
|
+
|
|
97
|
+
char name_buf[32];
|
|
98
|
+
if (i % 5 == 0) {
|
|
99
|
+
snprintf(name_buf, sizeof(name_buf), "user%c", "abcdef"[i % 6]);
|
|
100
|
+
} else {
|
|
101
|
+
snprintf(name_buf, sizeof(name_buf), "other_%d", i);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
records[i] = MG_TABLE_WRAP(pool, 6,
|
|
105
|
+
"id", mongory_value_wrap_i(pool, i),
|
|
106
|
+
"name", mongory_value_wrap_s(pool, pool_strdup(pool, name_buf)),
|
|
107
|
+
"age", mongory_value_wrap_i(pool, 10 + (i % 50)),
|
|
108
|
+
"status", mongory_value_wrap_s(pool, (i % 3 == 0) ? "active" : "inactive"),
|
|
109
|
+
"tags", tags,
|
|
110
|
+
"profile", profile
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
mongory_array *large_in_arr = mongory_array_new(pool);
|
|
115
|
+
for (int i = 0; i < 500; i++) {
|
|
116
|
+
char item[32];
|
|
117
|
+
snprintf(item, sizeof(item), "user_%d", i);
|
|
118
|
+
large_in_arr->push(large_in_arr, mongory_value_wrap_s(pool, pool_strdup(pool, item)));
|
|
119
|
+
}
|
|
120
|
+
mongory_value *large_in_list = mongory_value_wrap_a(pool, large_in_arr);
|
|
121
|
+
|
|
122
|
+
bench_case cases[] = {
|
|
123
|
+
{
|
|
124
|
+
"simple",
|
|
125
|
+
MG_TABLE_WRAP(pool, 1, "age", MG_TABLE_WRAP(pool, 1, "$gte", mongory_value_wrap_i(pool, 18))),
|
|
126
|
+
plain_simple
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
"or",
|
|
130
|
+
MG_TABLE_WRAP(pool, 1, "$or", MG_ARRAY_WRAP(pool, 2,
|
|
131
|
+
MG_TABLE_WRAP(pool, 1, "age", MG_TABLE_WRAP(pool, 1, "$gte", mongory_value_wrap_i(pool, 18))),
|
|
132
|
+
MG_TABLE_WRAP(pool, 1, "status", mongory_value_wrap_s(pool, "active"))
|
|
133
|
+
)),
|
|
134
|
+
plain_or
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"nested_and",
|
|
138
|
+
MG_TABLE_WRAP(pool, 3,
|
|
139
|
+
"profile", MG_TABLE_WRAP(pool, 1, "city", mongory_value_wrap_s(pool, "taipei")),
|
|
140
|
+
"status", mongory_value_wrap_s(pool, "active"),
|
|
141
|
+
"age", MG_TABLE_WRAP(pool, 1, "$lt", mongory_value_wrap_i(pool, 40))
|
|
142
|
+
),
|
|
143
|
+
plain_nested_and
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
"in_array_field",
|
|
147
|
+
MG_TABLE_WRAP(pool, 1, "tags", MG_TABLE_WRAP(pool, 1, "$in",
|
|
148
|
+
MG_ARRAY_WRAP(pool, 2, mongory_value_wrap_s(pool, "go"), mongory_value_wrap_s(pool, "rust")))),
|
|
149
|
+
plain_in_array_field
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
"large_in_500",
|
|
153
|
+
MG_TABLE_WRAP(pool, 1, "name", MG_TABLE_WRAP(pool, 1, "$in", large_in_list)),
|
|
154
|
+
plain_large_in_500
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
printf("mongory-core benchmark: %d records x %d rounds\n", BENCH_RECORDS, BENCH_ROUNDS);
|
|
159
|
+
int num_cases = (int)(sizeof(cases) / sizeof(cases[0]));
|
|
160
|
+
for (int c = 0; c < num_cases; c++) {
|
|
161
|
+
mongory_matcher *matcher = mongory_matcher_new(pool, cases[c].cond, NULL);
|
|
162
|
+
if (!matcher) {
|
|
163
|
+
fprintf(stderr, "Failed to compile case %s\n", cases[c].name);
|
|
164
|
+
return 1;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Warmup
|
|
168
|
+
for (int i = 0; i < BENCH_RECORDS; i++) {
|
|
169
|
+
cases[c].plain(records[i], large_in_arr);
|
|
170
|
+
mongory_matcher_match(matcher, records[i]);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
struct timespec p0, p1, m0, m1;
|
|
174
|
+
clock_gettime(CLOCK_MONOTONIC, &p0);
|
|
175
|
+
int plain_matched = 0;
|
|
176
|
+
for (int r = 0; r < BENCH_ROUNDS; r++) {
|
|
177
|
+
int round_matched = 0;
|
|
178
|
+
for (int i = 0; i < BENCH_RECORDS; i++) {
|
|
179
|
+
if (cases[c].plain(records[i], large_in_arr)) round_matched++;
|
|
180
|
+
}
|
|
181
|
+
plain_matched = round_matched;
|
|
182
|
+
}
|
|
183
|
+
clock_gettime(CLOCK_MONOTONIC, &p1);
|
|
184
|
+
|
|
185
|
+
clock_gettime(CLOCK_MONOTONIC, &m0);
|
|
186
|
+
int matched = 0;
|
|
187
|
+
for (int r = 0; r < BENCH_ROUNDS; r++) {
|
|
188
|
+
int round_matched = 0;
|
|
189
|
+
for (int i = 0; i < BENCH_RECORDS; i++) {
|
|
190
|
+
if (mongory_matcher_match(matcher, records[i])) round_matched++;
|
|
191
|
+
}
|
|
192
|
+
matched = round_matched;
|
|
193
|
+
}
|
|
194
|
+
clock_gettime(CLOCK_MONOTONIC, &m1);
|
|
195
|
+
|
|
196
|
+
double plain_ns = elapsed_ns(p0, p1) / ((double)BENCH_ROUNDS * BENCH_RECORDS);
|
|
197
|
+
double mongory_ns = elapsed_ns(m0, m1) / ((double)BENCH_ROUNDS * BENCH_RECORDS);
|
|
198
|
+
printf("%-16s plain=%6.1f ns/rec mongory=%6.1f ns/rec (ratio=%.2fx, matched=%d/%d)\n",
|
|
199
|
+
cases[c].name, plain_ns, mongory_ns, mongory_ns / plain_ns, matched, plain_matched);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
free(records);
|
|
203
|
+
pool->free(pool);
|
|
204
|
+
mongory_cleanup();
|
|
205
|
+
return 0;
|
|
206
|
+
}
|