hotdata-materialized 0.1.0__py3-none-any.whl
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.
- hotdata_materialized/__init__.py +44 -0
- hotdata_materialized/_arrow.py +44 -0
- hotdata_materialized/_sql.py +42 -0
- hotdata_materialized/client.py +50 -0
- hotdata_materialized/conf.py +70 -0
- hotdata_materialized/decorator.py +237 -0
- hotdata_materialized/exceptions.py +22 -0
- hotdata_materialized/fingerprint.py +77 -0
- hotdata_materialized/indexes.py +33 -0
- hotdata_materialized/py.typed +0 -0
- hotdata_materialized/registry.py +246 -0
- hotdata_materialized/store.py +405 -0
- hotdata_materialized-0.1.0.dist-info/METADATA +227 -0
- hotdata_materialized-0.1.0.dist-info/RECORD +16 -0
- hotdata_materialized-0.1.0.dist-info/WHEEL +5 -0
- hotdata_materialized-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hotdata-materialized
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Materialize expensive Django query results into Hotdata: miss runs locally, hit returns a queryable frame.
|
|
5
|
+
Author: Hotdata
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://hotdata.dev
|
|
8
|
+
Keywords: django,hotdata,cache,materialized,analytics,parquet
|
|
9
|
+
Classifier: Framework :: Django
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
Requires-Dist: Django>=3.2
|
|
15
|
+
Requires-Dist: hotdata[arrow]>=0.8
|
|
16
|
+
Requires-Dist: pyarrow>=14.0.1
|
|
17
|
+
Provides-Extra: test
|
|
18
|
+
Requires-Dist: pytest>=7.0; extra == "test"
|
|
19
|
+
Requires-Dist: duckdb>=1.0; extra == "test"
|
|
20
|
+
Requires-Dist: flake8>=4.0; extra == "test"
|
|
21
|
+
Requires-Dist: mypy>=1.5; extra == "test"
|
|
22
|
+
Requires-Dist: build>=1.0; extra == "test"
|
|
23
|
+
Provides-Extra: demo
|
|
24
|
+
Requires-Dist: psycopg[binary]>=3.1; extra == "demo"
|
|
25
|
+
|
|
26
|
+
# hotdata-materialized
|
|
27
|
+
|
|
28
|
+
Materialize expensive Django query results into [Hotdata](https://hotdata.dev).
|
|
29
|
+
On a cache **miss** your code runs in your environment as usual and the result
|
|
30
|
+
is captured into Hotdata as parquet — in the background, after your response
|
|
31
|
+
has already returned. On a **hit** your database is never touched: the data
|
|
32
|
+
comes back as a `pyarrow.Table` you can turn into a dataframe, or query with
|
|
33
|
+
SQL server-side.
|
|
34
|
+
|
|
35
|
+
Think materialized views, not Redis: entries are snapshots of expensive
|
|
36
|
+
analytical results, with a TTL lifecycle, each living in its own Hotdata
|
|
37
|
+
managed database. The host application needs **no migrations and no Redis** —
|
|
38
|
+
entry metadata lives in a small Hotdata managed database (the registry),
|
|
39
|
+
created on first use.
|
|
40
|
+
|
|
41
|
+
Measured against TPC-H on a Neon Postgres (see [demo/](demo/)):
|
|
42
|
+
|
|
43
|
+
| | Direct on Postgres | Miss (perceived) | Hit |
|
|
44
|
+
|---|---|---|---|
|
|
45
|
+
| Q1 pricing summary (full scan, 4 rows) | ~1.4 s | ~1.4 s | **~80 ms** |
|
|
46
|
+
| Revenue by part (50,000 rows) | ~2.1 s | ~2.0 s | **~260 ms** |
|
|
47
|
+
|
|
48
|
+
A miss costs the same as not caching (the persist runs write-behind); a hit
|
|
49
|
+
is 8–18× faster with zero load on your database.
|
|
50
|
+
|
|
51
|
+
## Install
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install hotdata-materialized
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
# settings.py
|
|
59
|
+
HOTDATA_MATERIALIZED = {
|
|
60
|
+
"API_KEY": env("HOTDATA_API_KEY"), # hd_... key from app.hotdata.dev
|
|
61
|
+
"WORKSPACE_ID": "work...",
|
|
62
|
+
# optional:
|
|
63
|
+
# "API_URL": "https://api.hotdata.dev",
|
|
64
|
+
# "REGISTRY_DATABASE": "materialized_registry",
|
|
65
|
+
# "INLINE_THRESHOLD_BYTES": 2 * 1024 * 1024,
|
|
66
|
+
# "BACKGROUND": True, # write-behind persists (default)
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Using it in a Django view
|
|
71
|
+
|
|
72
|
+
Wrap the expensive part in `@materialize`; call it from your view:
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
# reports.py
|
|
76
|
+
from django.db.models import Count, Sum
|
|
77
|
+
from hotdata_materialized import materialize
|
|
78
|
+
|
|
79
|
+
from .models import Order
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@materialize(ttl=3600)
|
|
83
|
+
def revenue_by_region():
|
|
84
|
+
return (
|
|
85
|
+
Order.objects
|
|
86
|
+
.filter(status="complete")
|
|
87
|
+
.values("region")
|
|
88
|
+
.annotate(orders=Count("id"), revenue=Sum("total"))
|
|
89
|
+
.order_by("-revenue")
|
|
90
|
+
)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
# views.py
|
|
95
|
+
from django.http import JsonResponse
|
|
96
|
+
from .reports import revenue_by_region
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def revenue_view(request):
|
|
100
|
+
frame = revenue_by_region()
|
|
101
|
+
return JsonResponse({"rows": frame.to_pylist(), "cached": frame.cached})
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
On a **hit** the function never runs and your database is never touched. On a
|
|
105
|
+
**miss** the function runs exactly as it would uncached, the caller gets the
|
|
106
|
+
result immediately, and the persist happens in a background thread. Either
|
|
107
|
+
way you get a `MaterializedFrame`:
|
|
108
|
+
|
|
109
|
+
- `frame.arrow()` — the data as a `pyarrow.Table` (`frame.df()` for pandas,
|
|
110
|
+
`frame.to_pylist()` for dicts, `len(frame)` for the row count)
|
|
111
|
+
- `frame.sql("SELECT region, revenue FROM this ORDER BY revenue DESC LIMIT 5")`
|
|
112
|
+
— SQL runs server-side against the cached entry; `this` names the data.
|
|
113
|
+
Needs a persisted entry: available on hits (on a fresh miss the
|
|
114
|
+
write-behind persist has to land first; `background=False` avoids that)
|
|
115
|
+
- `frame.cached` — which path served it; `frame.entry` — the registry record
|
|
116
|
+
|
|
117
|
+
The wrapped function can return an iterable of dicts (a `.values()` queryset),
|
|
118
|
+
a `pyarrow.Table`, or a pandas DataFrame. Decorator knobs: `ttl=` (seconds,
|
|
119
|
+
`None` = never expires), `version=` (bump to bust the cache on code changes),
|
|
120
|
+
`key=` (human-readable label), `key_fn=` (stable identity for arguments that
|
|
121
|
+
aren't JSON-serializable), `background=False` (block until persisted).
|
|
122
|
+
Fail-open by design: if Hotdata is unreachable, the function runs and its
|
|
123
|
+
result is served uncached — the cache degrades to "no cache," never to
|
|
124
|
+
"no page."
|
|
125
|
+
|
|
126
|
+
## Search
|
|
127
|
+
|
|
128
|
+
Declare a search index on the decorator and the entry gets it when it
|
|
129
|
+
persists (builds run server-side; a freshly missed entry may error on search
|
|
130
|
+
for a few seconds until its index is ready):
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from hotdata_materialized import BM25, Vector, materialize
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@materialize(ttl=3600, index=BM25("notes"))
|
|
137
|
+
def incidents():
|
|
138
|
+
return Incident.objects.values("id", "notes", "severity")
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
incidents().search("checkout errors outage", column="notes", limit=5)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Vector (semantic) search embeds your text server-side via a workspace
|
|
145
|
+
embedding provider — pass the indexed source column and a natural-language
|
|
146
|
+
query:
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
@materialize(ttl=3600, index=Vector("notes", provider="emb_...", metric="cosine"))
|
|
150
|
+
def incidents_semantic():
|
|
151
|
+
return Incident.objects.values("id", "notes")
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
incidents_semantic().vector_search("the site was down", column="notes", limit=5)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Both return the matching rows as a `pyarrow.Table` with a relevance column
|
|
158
|
+
(`score` / `distance`). One platform rule to know: an embedding-backed vector
|
|
159
|
+
index cannot share an entry with other indexes — the decorator rejects that
|
|
160
|
+
combination up front.
|
|
161
|
+
|
|
162
|
+
## Evicting entries
|
|
163
|
+
|
|
164
|
+
The decorator composes public primitives (`fingerprint_call` /
|
|
165
|
+
`fingerprint_queryset`, `Registry`, `EntryStore`) that you can use directly —
|
|
166
|
+
for example to drop an entry explicitly:
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
from hotdata_materialized import fingerprint_call
|
|
170
|
+
from hotdata_materialized.decorator import get_runtime
|
|
171
|
+
|
|
172
|
+
registry, store = get_runtime()
|
|
173
|
+
store.evict(fingerprint_call(revenue_by_region))
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Expired entries stop being served immediately (the `is_expired` check) and
|
|
177
|
+
their databases carry a server-side expiry backstop; a sweep command that
|
|
178
|
+
deletes them proactively is on the roadmap below.
|
|
179
|
+
|
|
180
|
+
## How it works
|
|
181
|
+
|
|
182
|
+
One managed database (the **registry**) holds one row per entry: fingerprint,
|
|
183
|
+
status, TTL, the entry's database id, and — for small results — the data
|
|
184
|
+
itself as an inline Arrow payload, so a chart-sized hit is served in a single
|
|
185
|
+
API round trip. Larger results live in a per-entry managed database and are
|
|
186
|
+
fetched as an Arrow IPC stream via a result id minted at persist time (no
|
|
187
|
+
re-query on hits). Failures are loud and fail toward "no cache": a failed
|
|
188
|
+
persist leaves no registry row, and the next request simply misses again.
|
|
189
|
+
|
|
190
|
+
See [DESIGN.md](DESIGN.md) for the architecture and the accepted
|
|
191
|
+
trade-offs.
|
|
192
|
+
|
|
193
|
+
## Roadmap
|
|
194
|
+
|
|
195
|
+
- [x] Content-addressed fingerprinting (querysets and function calls)
|
|
196
|
+
- [x] Remote registry — no migrations or local state in the host app
|
|
197
|
+
- [x] Entry store with write-behind persists
|
|
198
|
+
- [x] Arrow-native reads (inline payloads, persisted-result fetch)
|
|
199
|
+
- [x] `@materialize` decorator and `MaterializedFrame`
|
|
200
|
+
(`.arrow()/.df()/.to_pylist()/.sql()`)
|
|
201
|
+
- [x] TPC-H demo and benchmark
|
|
202
|
+
- [x] CI: pytest, mypy, flake8
|
|
203
|
+
- [ ] Stale-while-revalidate refresh (rebuild protocol)
|
|
204
|
+
- [ ] Sweep command for expired entries
|
|
205
|
+
- [ ] Chainable queryset facade on the frame (`.filter()/.order_by()`)
|
|
206
|
+
- [x] Vector/BM25 index declarations, `frame.search()` and
|
|
207
|
+
`frame.vector_search()`
|
|
208
|
+
- [ ] Async view support (`await frame.aarrow()`)
|
|
209
|
+
- [ ] PyPI release
|
|
210
|
+
|
|
211
|
+
## Demo
|
|
212
|
+
|
|
213
|
+
[demo/](demo/) is a small Django project that benchmarks TPC-H queries on a
|
|
214
|
+
Neon Postgres directly vs. through the cache:
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
cd demo
|
|
218
|
+
python manage.py compare # Q1: tiny result, inline hit
|
|
219
|
+
python manage.py compare --scenario parts # 50k rows: remote Arrow hit
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Development
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
uv venv && uv pip install -e '.[test]'
|
|
226
|
+
uv run pytest
|
|
227
|
+
```
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
hotdata_materialized/__init__.py,sha256=UVSCllwCcrCbnxG3fBJNSTZ7F76ktnVW9YizmVEf4Bk,1094
|
|
2
|
+
hotdata_materialized/_arrow.py,sha256=IDikCxB1mlut5AVHr6_GyTLnaMPL6UrmIFBiKfLvsUw,1238
|
|
3
|
+
hotdata_materialized/_sql.py,sha256=JGo_QdReoSQnKlw8Pso9ReCzcUXnH3-EKbFK9fmHRDo,1359
|
|
4
|
+
hotdata_materialized/client.py,sha256=nu7nT96ZKwxr3gbZisqiZwf1uhNhFsrXudsFr8RIBWI,1337
|
|
5
|
+
hotdata_materialized/conf.py,sha256=WYTYyZSNKjRybgql_3C7xTtbYG9rrAOpBYSMrWR8uso,2575
|
|
6
|
+
hotdata_materialized/decorator.py,sha256=wnggMaGj8P105pNGpT4LJe7AKx7eOKxxu4KgZDg9BCU,8769
|
|
7
|
+
hotdata_materialized/exceptions.py,sha256=5P6oD63bZfj8dXMUd57Sg27paGvhvZwlvYeh4-qmPWE,617
|
|
8
|
+
hotdata_materialized/fingerprint.py,sha256=6uv1V8ZUUy2KYyrFBP-28KEjPLNpAJck5eIkl74EoL0,2551
|
|
9
|
+
hotdata_materialized/indexes.py,sha256=_aBuBBlyUcfbIlLmf-IUVzQ7VVPg6aYjWoR_bdI4Ykk,1002
|
|
10
|
+
hotdata_materialized/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
hotdata_materialized/registry.py,sha256=kGTzCtslDOQFyM2i3tZFeu2R4gnKVGmxo8jjmNRjvuU,9133
|
|
12
|
+
hotdata_materialized/store.py,sha256=HWxlQ7YuAOMcHyPjQoaHFEn3MG2VPACII29zac5XAKE,16965
|
|
13
|
+
hotdata_materialized-0.1.0.dist-info/METADATA,sha256=QuEWvNTW2oDXYXclq5gjsYOxhGRYJDFQZK-dGVJYWro,8088
|
|
14
|
+
hotdata_materialized-0.1.0.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
|
|
15
|
+
hotdata_materialized-0.1.0.dist-info/top_level.txt,sha256=fvVnvrG1y6hzzlsJ8gVO6MvVxfrVAThdepbe0Ih3QU0,21
|
|
16
|
+
hotdata_materialized-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hotdata_materialized
|