turboquant-db 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.
Files changed (35) hide show
  1. turboquant_db-0.1.0/.github/workflows/ci.yml +53 -0
  2. turboquant_db-0.1.0/.github/workflows/publish.yml +44 -0
  3. turboquant_db-0.1.0/.gitignore +9 -0
  4. turboquant_db-0.1.0/CHANGELOG.md +5 -0
  5. turboquant_db-0.1.0/LICENSE +191 -0
  6. turboquant_db-0.1.0/PKG-INFO +318 -0
  7. turboquant_db-0.1.0/README.md +288 -0
  8. turboquant_db-0.1.0/pyproject.toml +55 -0
  9. turboquant_db-0.1.0/setup.cfg +4 -0
  10. turboquant_db-0.1.0/src/turbodb/__init__.py +25 -0
  11. turboquant_db-0.1.0/src/turbodb/collection.py +318 -0
  12. turboquant_db-0.1.0/src/turbodb/db.py +80 -0
  13. turboquant_db-0.1.0/src/turbodb/exceptions.py +36 -0
  14. turboquant_db-0.1.0/src/turbodb/filters.py +89 -0
  15. turboquant_db-0.1.0/src/turbodb/locking.py +72 -0
  16. turboquant_db-0.1.0/src/turbodb/metadata.py +176 -0
  17. turboquant_db-0.1.0/src/turbodb/results.py +31 -0
  18. turboquant_db-0.1.0/src/turboquant_db.egg-info/PKG-INFO +318 -0
  19. turboquant_db-0.1.0/src/turboquant_db.egg-info/SOURCES.txt +33 -0
  20. turboquant_db-0.1.0/src/turboquant_db.egg-info/dependency_links.txt +1 -0
  21. turboquant_db-0.1.0/src/turboquant_db.egg-info/requires.txt +6 -0
  22. turboquant_db-0.1.0/src/turboquant_db.egg-info/top_level.txt +1 -0
  23. turboquant_db-0.1.0/tests/__init__.py +0 -0
  24. turboquant_db-0.1.0/tests/conftest.py +29 -0
  25. turboquant_db-0.1.0/tests/test_collection.py +143 -0
  26. turboquant_db-0.1.0/tests/test_concurrency.py +87 -0
  27. turboquant_db-0.1.0/tests/test_crash_safety.py +70 -0
  28. turboquant_db-0.1.0/tests/test_db.py +75 -0
  29. turboquant_db-0.1.0/tests/test_exceptions.py +39 -0
  30. turboquant_db-0.1.0/tests/test_filters.py +107 -0
  31. turboquant_db-0.1.0/tests/test_integration.py +138 -0
  32. turboquant_db-0.1.0/tests/test_locking.py +42 -0
  33. turboquant_db-0.1.0/tests/test_metadata.py +95 -0
  34. turboquant_db-0.1.0/tests/test_results.py +33 -0
  35. turboquant_db-0.1.0/uv.lock +490 -0
@@ -0,0 +1,53 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main, master]
6
+ pull_request:
7
+ branches: [main, master]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ python-version: ["3.10", "3.11", "3.12", "3.13"]
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+
18
+ - name: Set up Python ${{ matrix.python-version }}
19
+ uses: actions/setup-python@v5
20
+ with:
21
+ python-version: ${{ matrix.python-version }}
22
+
23
+ - name: Install dependencies
24
+ run: |
25
+ python -m pip install --upgrade pip
26
+ pip install -e ".[dev]"
27
+
28
+ - name: Lint
29
+ run: ruff check src/
30
+
31
+ - name: Test
32
+ run: pytest tests/ -x -q
33
+
34
+ build:
35
+ runs-on: ubuntu-latest
36
+ steps:
37
+ - uses: actions/checkout@v4
38
+
39
+ - name: Set up Python
40
+ uses: actions/setup-python@v5
41
+ with:
42
+ python-version: "3.12"
43
+
44
+ - name: Install build tools
45
+ run: pip install build
46
+
47
+ - name: Build sdist and wheel
48
+ run: python -m build
49
+
50
+ - name: Check package
51
+ run: |
52
+ pip install twine
53
+ twine check dist/*
@@ -0,0 +1,44 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ jobs:
8
+ build:
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v4
12
+
13
+ - name: Set up Python
14
+ uses: actions/setup-python@v5
15
+ with:
16
+ python-version: "3.12"
17
+
18
+ - name: Install build tools
19
+ run: pip install build
20
+
21
+ - name: Build sdist and wheel
22
+ run: python -m build
23
+
24
+ - name: Upload artifacts
25
+ uses: actions/upload-artifact@v4
26
+ with:
27
+ name: dist
28
+ path: dist/
29
+
30
+ publish:
31
+ needs: build
32
+ runs-on: ubuntu-latest
33
+ environment: pypi
34
+ permissions:
35
+ id-token: write
36
+ steps:
37
+ - name: Download artifacts
38
+ uses: actions/download-artifact@v4
39
+ with:
40
+ name: dist
41
+ path: dist/
42
+
43
+ - name: Publish to PyPI
44
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,9 @@
1
+ __pycache__/
2
+ *.egg-info/
3
+ .eggs/
4
+ dist/
5
+ build/
6
+ .pytest_cache/
7
+ .ruff_cache/
8
+ *.pyc
9
+ .venv/
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (unreleased)
4
+
5
+ Initial release.
@@ -0,0 +1,191 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to the Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by the Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding any notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ Copyright 2026 msilverblatt
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License");
182
+ you may not use this file except in compliance with the License.
183
+ You may obtain a copy of the License at
184
+
185
+ http://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable law or agreed to in writing, software
188
+ distributed under the License is distributed on an "AS IS" BASIS,
189
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
+ See the License for the specific language governing permissions and
191
+ limitations under the License.
@@ -0,0 +1,318 @@
1
+ Metadata-Version: 2.4
2
+ Name: turboquant-db
3
+ Version: 0.1.0
4
+ Summary: Lightweight embedded vector database built on TurboQuant
5
+ Author: msilverblatt
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/msilverblatt/turbo-db
8
+ Project-URL: Repository, https://github.com/msilverblatt/turbo-db
9
+ Project-URL: Issues, https://github.com/msilverblatt/turbo-db/issues
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Database
19
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
20
+ Classifier: Typing :: Typed
21
+ Requires-Python: >=3.10
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: numpy>=1.24
25
+ Requires-Dist: turboquant-py>=0.1.0
26
+ Provides-Extra: dev
27
+ Requires-Dist: pytest>=7.0; extra == "dev"
28
+ Requires-Dist: ruff>=0.4; extra == "dev"
29
+ Dynamic: license-file
30
+
31
+ # turboquant-db
32
+
33
+ Lightweight embedded vector database built on [turboquant-py](https://github.com/msilverblatt/turboquant-py). Drop-in replacement for ChromaDB with 16x vector compression.
34
+
35
+ turboquant-db stores vectors using TurboQuant's near-optimal quantization (1-4 bits per coordinate) and metadata in SQLite. It provides a ChromaDB-compatible API with collections, metadata filtering, and concurrent read/write support — all in a few hundred lines of Python with no dependencies beyond turboquant-py and the standard library.
36
+
37
+ ## Installation
38
+
39
+ ```bash
40
+ pip install turboquant-db
41
+ ```
42
+
43
+ ## Quick Start
44
+
45
+ ```python
46
+ import numpy as np
47
+ from turbodb import TurboDB
48
+
49
+ db = TurboDB("./my_db")
50
+ collection = db.create_collection("docs", dim=384)
51
+
52
+ collection.add(
53
+ ids=["doc1", "doc2"],
54
+ vectors=np.random.randn(2, 384),
55
+ metadatas=[{"source": "wiki", "year": 2024}, {"source": "arxiv", "year": 2025}],
56
+ )
57
+
58
+ results = collection.query(vector=np.random.randn(384), k=5)
59
+ for r in results:
60
+ print(f"{r.id}: {r.score:.3f} — {r.metadata}")
61
+ ```
62
+
63
+ ## API Reference
64
+
65
+ ### `TurboDB(path)`
66
+
67
+ Open or create a database at the given directory path.
68
+
69
+ ```python
70
+ db = TurboDB("./my_db")
71
+ ```
72
+
73
+ **Methods:**
74
+
75
+ | Method | Description |
76
+ |---|---|
77
+ | `create_collection(name, dim, metric, bit_width)` | Create a new collection |
78
+ | `get_collection(name)` | Open an existing collection |
79
+ | `get_or_create_collection(name, dim, metric, bit_width)` | Get or create a collection |
80
+ | `delete_collection(name)` | Delete a collection and all its data |
81
+ | `list_collections()` | List all collection names |
82
+
83
+ ---
84
+
85
+ ### `Collection`
86
+
87
+ A named group of quantized vectors with metadata.
88
+
89
+ ```python
90
+ collection = db.create_collection("docs", dim=384, metric="cosine", bit_width=2)
91
+ ```
92
+
93
+ | Parameter | Type | Default | Description |
94
+ |---|---|---|---|
95
+ | `name` | `str` | required | Collection name |
96
+ | `dim` | `int` | required | Vector dimensionality |
97
+ | `metric` | `str` | `"cosine"` | Distance metric: `"cosine"`, `"ip"`, or `"l2"` |
98
+ | `bit_width` | `int` | `2` | Bits per coordinate (1-4). Lower = more compression, less accuracy |
99
+
100
+ **Methods:**
101
+
102
+ #### `add(ids, vectors, metadatas)`
103
+
104
+ Add vectors with string IDs and metadata dicts. IDs must be unique.
105
+
106
+ ```python
107
+ collection.add(
108
+ ids=["doc1", "doc2"],
109
+ vectors=np.random.randn(2, 384),
110
+ metadatas=[{"source": "wiki"}, {"source": "arxiv"}],
111
+ )
112
+ ```
113
+
114
+ #### `upsert(ids, vectors, metadatas)`
115
+
116
+ Insert or replace vectors. If an ID already exists, its vector and metadata are replaced.
117
+
118
+ ```python
119
+ collection.upsert(
120
+ ids=["doc1"],
121
+ vectors=new_vector,
122
+ metadatas=[{"source": "updated"}],
123
+ )
124
+ ```
125
+
126
+ #### `query(vector, k, where, format)`
127
+
128
+ Search for the top-k most similar vectors, optionally filtering by metadata.
129
+
130
+ ```python
131
+ results = collection.query(vector=query_vec, k=10)
132
+ results[0].id # "doc2"
133
+ results[0].score # 0.934
134
+ results[0].metadata # {"source": "arxiv"}
135
+ ```
136
+
137
+ Returns a list of `QueryResult` objects sorted by descending score.
138
+
139
+ #### `get(ids)`
140
+
141
+ Retrieve metadata by IDs without performing a search.
142
+
143
+ ```python
144
+ items = collection.get(ids=["doc1", "doc2"])
145
+ # [{"id": "doc1", "position": 0, "metadata": {"source": "wiki"}}, ...]
146
+ ```
147
+
148
+ #### `delete(ids, where)`
149
+
150
+ Delete vectors by IDs, metadata filter, or both.
151
+
152
+ ```python
153
+ collection.delete(ids=["doc1"])
154
+ collection.delete(where={"source": {"$eq": "wiki"}})
155
+ ```
156
+
157
+ #### `compact()`
158
+
159
+ Rewrite storage to reclaim space from deleted vectors.
160
+
161
+ ```python
162
+ collection.compact()
163
+ ```
164
+
165
+ #### `count()` / `name` / `dim` / `metric`
166
+
167
+ ```python
168
+ collection.count() # number of live vectors
169
+ collection.name # "docs"
170
+ collection.dim # 384
171
+ collection.metric # "cosine"
172
+ ```
173
+
174
+ ---
175
+
176
+ ### `QueryResult`
177
+
178
+ Frozen dataclass returned by `query()`.
179
+
180
+ | Attribute | Type | Description |
181
+ |---|---|---|
182
+ | `id` | `str` | Vector ID |
183
+ | `score` | `float` | Similarity score (higher = more similar for cosine/ip) |
184
+ | `metadata` | `dict` | Associated metadata |
185
+
186
+ ---
187
+
188
+ ### ChromaDB compatibility
189
+
190
+ Pass `format="chroma"` to get results in ChromaDB's column-oriented format:
191
+
192
+ ```python
193
+ results = collection.query(vector=query_vec, k=10, format="chroma")
194
+ results["ids"][0] # ["doc2", "doc5", ...]
195
+ results["distances"][0] # [0.934, 0.891, ...]
196
+ results["metadatas"][0] # [{"source": "arxiv"}, ...]
197
+ ```
198
+
199
+ This makes migration straightforward — change the import, update the constructor, and add `format="chroma"` to your query calls. Remove `format="chroma"` at your own pace.
200
+
201
+ ## Metadata Filtering
202
+
203
+ Filter syntax matches ChromaDB and Pinecone conventions:
204
+
205
+ ```python
206
+ # Comparison operators
207
+ collection.query(vector=v, k=10, where={"year": {"$eq": 2025}})
208
+ collection.query(vector=v, k=10, where={"year": {"$ne": 2024}})
209
+ collection.query(vector=v, k=10, where={"year": {"$gt": 2023}})
210
+ collection.query(vector=v, k=10, where={"year": {"$gte": 2024}})
211
+ collection.query(vector=v, k=10, where={"year": {"$lt": 2026}})
212
+ collection.query(vector=v, k=10, where={"year": {"$lte": 2025}})
213
+
214
+ # Set operators
215
+ collection.query(vector=v, k=10, where={"source": {"$in": ["wiki", "arxiv"]}})
216
+ collection.query(vector=v, k=10, where={"source": {"$nin": ["blog"]}})
217
+
218
+ # Logical combinators
219
+ collection.query(vector=v, k=10, where={
220
+ "$and": [
221
+ {"year": {"$gte": 2024}},
222
+ {"source": {"$eq": "arxiv"}},
223
+ ]
224
+ })
225
+
226
+ collection.query(vector=v, k=10, where={
227
+ "$or": [
228
+ {"source": {"$eq": "wiki"}},
229
+ {"year": {"$gt": 2024}},
230
+ ]
231
+ })
232
+ ```
233
+
234
+ Multiple top-level fields are implicitly ANDed:
235
+
236
+ ```python
237
+ # Equivalent to $and
238
+ collection.query(vector=v, k=10, where={"year": {"$gte": 2024}, "source": {"$eq": "arxiv"}})
239
+ ```
240
+
241
+ ## Distance Metrics
242
+
243
+ | Metric | Description | Score interpretation |
244
+ |---|---|---|
245
+ | `cosine` (default) | Cosine similarity | 1.0 = identical, 0.0 = orthogonal |
246
+ | `ip` | Inner product | Higher = more similar |
247
+ | `l2` | Squared L2 distance | Lower = more similar |
248
+
249
+ All metrics use TurboQuant's inner-product quantizer under the hood. Cosine normalizes vectors on add; L2 is computed from stored norms and inner products.
250
+
251
+ ## Compression
252
+
253
+ turbo-db compresses vectors using TurboQuant's Lloyd-Max quantization with random orthogonal rotation:
254
+
255
+ | Bit-width | Compression ratio | Use case |
256
+ |---|---|---|
257
+ | 1 | 32x | Maximum compression, rough similarity |
258
+ | 2 (default) | 16x | Good balance of quality and size |
259
+ | 3 | 10.7x | Higher accuracy |
260
+ | 4 | 8x | Near-lossless similarity search |
261
+
262
+ At the default 2-bit setting, a collection of 1M 384-dimensional vectors uses ~9.6 MB for vector data, compared to ~1.5 GB uncompressed.
263
+
264
+ ## Storage
265
+
266
+ Each database is a directory. Each collection is a subdirectory:
267
+
268
+ ```
269
+ my_db/
270
+ ├── docs/
271
+ │ ├── vectors/ # Quantized vectors (numpy arrays)
272
+ │ ├── metadata.db # SQLite: IDs, metadata, positions
273
+ │ └── lock # Write lock file
274
+ ├── embeddings/
275
+ │ └── ...
276
+ └── turbodb.json # Database config
277
+ ```
278
+
279
+ Metadata is stored in SQLite with WAL mode for concurrent read/write access. Vector data uses turboquant-py's bit-packed numpy format.
280
+
281
+ ## Concurrency
282
+
283
+ - **Multiple readers + one writer**: SQLite WAL mode allows concurrent reads during writes
284
+ - **Write serialization**: File locking ensures one write operation at a time per collection
285
+ - **Crash safety**: Vectors are written before metadata is committed. On restart, orphaned vectors are automatically trimmed to match SQLite state
286
+
287
+ ## Migrating from ChromaDB
288
+
289
+ ```python
290
+ # Before (ChromaDB)
291
+ import chromadb
292
+ client = chromadb.PersistentClient(path="./db")
293
+ collection = client.create_collection("docs")
294
+ collection.add(ids=["a"], embeddings=[[1, 2, 3]], metadatas=[{"k": "v"}])
295
+ results = collection.query(query_embeddings=[[1, 2, 3]], n_results=5)
296
+
297
+ # After (turboquant-db)
298
+ from turbodb import TurboDB
299
+ db = TurboDB("./db")
300
+ collection = db.create_collection("docs", dim=3)
301
+ collection.add(ids=["a"], vectors=[[1, 2, 3]], metadatas=[{"k": "v"}])
302
+ results = collection.query(vector=[1, 2, 3], k=5)
303
+ # Or with Chroma-compat format:
304
+ results = collection.query(vector=[1, 2, 3], k=5, format="chroma")
305
+ ```
306
+
307
+ Key differences:
308
+ - `embeddings` → `vectors`
309
+ - `query_embeddings` → `vector` (single vector, not nested list)
310
+ - `n_results` → `k`
311
+ - `dim` is required on `create_collection`
312
+ - Results are `QueryResult` objects by default (use `format="chroma"` for column dicts)
313
+
314
+ ## References
315
+
316
+ - **TurboQuant:** [arXiv:2504.19874](https://arxiv.org/abs/2504.19874)
317
+ - **QJL:** [arXiv:2406.03482](https://arxiv.org/abs/2406.03482)
318
+ - **turboquant-py:** [github.com/msilverblatt/turboquant-py](https://github.com/msilverblatt/turboquant-py)