JackDB 0.3.2__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.
- jackdb-0.3.2/LICENSE +21 -0
- jackdb-0.3.2/PKG-INFO +302 -0
- jackdb-0.3.2/README.md +288 -0
- jackdb-0.3.2/pyproject.toml +27 -0
- jackdb-0.3.2/setup.cfg +4 -0
- jackdb-0.3.2/src/JackDB.egg-info/PKG-INFO +302 -0
- jackdb-0.3.2/src/JackDB.egg-info/SOURCES.txt +19 -0
- jackdb-0.3.2/src/JackDB.egg-info/dependency_links.txt +1 -0
- jackdb-0.3.2/src/JackDB.egg-info/requires.txt +2 -0
- jackdb-0.3.2/src/JackDB.egg-info/top_level.txt +1 -0
- jackdb-0.3.2/src/jackdb/__init__.py +8 -0
- jackdb-0.3.2/src/jackdb/api.py +75 -0
- jackdb-0.3.2/src/jackdb/api_models.py +34 -0
- jackdb-0.3.2/src/jackdb/commit_record.py +54 -0
- jackdb-0.3.2/src/jackdb/core.py +500 -0
- jackdb-0.3.2/src/jackdb/exceptions.py +13 -0
- jackdb-0.3.2/src/jackdb/logger.py +4 -0
- jackdb-0.3.2/src/jackdb/main.py +23 -0
- jackdb-0.3.2/src/jackdb/operations.py +37 -0
- jackdb-0.3.2/tests/test_core.py +655 -0
- jackdb-0.3.2/tests/test_items.py +73 -0
jackdb-0.3.2/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jack Kennedy
|
|
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.
|
jackdb-0.3.2/PKG-INFO
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: JackDB
|
|
3
|
+
Version: 0.3.2
|
|
4
|
+
Summary: Simple embedded key-value database
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Repository, https://github.com/gamecube7561/JackDB
|
|
7
|
+
Project-URL: Issues, https://github.com/gamecube7561/JackDB/issues
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: fastapi[standard]>=0.162.0
|
|
12
|
+
Requires-Dist: pydantic>=2.14.2
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# JackDB
|
|
16
|
+
|
|
17
|
+
JackDB is an embedded Python key-value database backed by a JSON Lines
|
|
18
|
+
append-only log. Data is held in memory, replayed from disk when opened, and
|
|
19
|
+
persisted through explicit or automatic commits. Reads see an instance's
|
|
20
|
+
committed state. JackDB can compact the log and expose the database through
|
|
21
|
+
FastAPI size, get, put, set, and del endpoints.
|
|
22
|
+
|
|
23
|
+
[](https://github.com/gamecube7561/JackDB/actions/workflows/pylint.yml)
|
|
24
|
+
[](https://github.com/gamecube7561/JackDB/actions/workflows/tests.yml)
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
Requires Python 3.10+ and a Unix-like system such as Linux or macOS. File
|
|
28
|
+
locking uses `fcntl`, so native Windows is currently unsupported.
|
|
29
|
+
|
|
30
|
+
By default, JackDB attempts to commit each write immediately.
|
|
31
|
+
|
|
32
|
+
From the repository root:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
python -m pip install .
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from jackdb import JackDB
|
|
42
|
+
|
|
43
|
+
with JackDB("./mydb.log") as db:
|
|
44
|
+
db.do_operation({"op": "put", "k": "user:1", "v": {"name": "Aoba"}})
|
|
45
|
+
db.do_operation({"op": "set", "k": "user:1", "f": "state", "v": "NY"})
|
|
46
|
+
user = db.do_operation({"op": "get", "k": "user:1"})
|
|
47
|
+
print(user) # {'name': 'Aoba', 'state': 'NY'}
|
|
48
|
+
```
|
|
49
|
+
Installing the project uses `pyproject.toml`, which declares
|
|
50
|
+
`fastapi[standard]` and `pydantic`. FastAPI's standard extras include Uvicorn.
|
|
51
|
+
`requirements.txt` contains an exported, pinned dependency set.
|
|
52
|
+
|
|
53
|
+
## Usage
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from jackdb import JackDB
|
|
57
|
+
|
|
58
|
+
with JackDB("./mydb.log", auto_commit=False) as db:
|
|
59
|
+
receipt = db.do_operation({"op": "put", "k": "user:1", "v": {"name": "Aoba"}})
|
|
60
|
+
print(receipt) # {'status': 'pending'}
|
|
61
|
+
db.do_operation({"op": "set", "k": "user:1", "f": "state", "v": "NY"})
|
|
62
|
+
db.commit()
|
|
63
|
+
user = db.do_operation({"op": "get", "k": "user:1"})
|
|
64
|
+
print(user) # {'name': 'Aoba', 'state': 'NY'}
|
|
65
|
+
print(len(db)) # 1
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`from jackdb.main import JackDB` also remains supported. Operations accept
|
|
69
|
+
Python dictionaries or JSON strings. Use string keys and JSON-serializable
|
|
70
|
+
values. Replay rejects nonempty non-string keys, even if the live write
|
|
71
|
+
accepted them. Tuples in values become lists after reopening.
|
|
72
|
+
|
|
73
|
+
### Constructor
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from jackdb import JackDB
|
|
77
|
+
|
|
78
|
+
JackDB(replay_path="./mydb.log", auto_commit=True, commit_size=1, db_name=None)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
| Argument | Behavior |
|
|
82
|
+
|---------------|-----------------------------------------------------------------------------------------------------------------------|
|
|
83
|
+
| `replay_path` | Required nonempty string path. Creates the log if absent; its parent directory must already exist. |
|
|
84
|
+
| `auto_commit` | Enables automatic commits during `do_operation()`. Defaults to `True`. |
|
|
85
|
+
| `commit_size` | Commits when pending records **reach** this number. The default `1` commits on the first pending mutation. |
|
|
86
|
+
| `db_name` | A nonempty name builds a FastAPI app and attaches it as `db.api`. By default, both `db_name` and `db.api` are `None`. |
|
|
87
|
+
|
|
88
|
+
### Operations
|
|
89
|
+
|
|
90
|
+
Call these through `db.do_operation(record)`:
|
|
91
|
+
|
|
92
|
+
| `op` | Required fields | Read result or successful commit result |
|
|
93
|
+
|------------|----------------------|----------------------------------------------------------------------------------|
|
|
94
|
+
| `put` | `k`, `v` | Stores or overwrites a value and returns it. |
|
|
95
|
+
| `del` | `k` | Removes a key; returns `True` if present, otherwise `False`. |
|
|
96
|
+
| `get` | `k` | Returns a value, or `None` when absent. |
|
|
97
|
+
| `get_many` | `k` (a list of keys) | Returns `(key, value)` tuples in input order; missing values are `None`. |
|
|
98
|
+
| `set` | `k`, `f`, `v` | Sets a field on an existing dictionary value and returns the updated dictionary. |
|
|
99
|
+
|
|
100
|
+
`put`, `del`, and `set` queue a copy of the operation and return
|
|
101
|
+
`{"status": "pending"}`, even when the call triggers an automatic commit.
|
|
102
|
+
`get` and `get_many` read committed values and add no records.
|
|
103
|
+
A stored `None` and a missing key both return `None`; use `key in db` to
|
|
104
|
+
check existence. The database also supports `len(db)`, `db[key]`, iteration
|
|
105
|
+
over keys, and `db.keys()`. `db[key]` raises `KeyError` for a missing key.
|
|
106
|
+
Reads return deep copies of stored values. The lower-level `do_put`, `do_del`,
|
|
107
|
+
and `do_set` methods operate on a supplied dictionary and bypass journaling.
|
|
108
|
+
|
|
109
|
+
### Persistence and recovery
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from jackdb import JackDB
|
|
113
|
+
|
|
114
|
+
db = JackDB("./mydb.log", auto_commit=False)
|
|
115
|
+
results = db.execute_and_commit({"op": "put", "k": "cfg", "v": [1, 2, 3]})
|
|
116
|
+
print(results[-1].result) # [1, 2, 3]
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
`commit()` applies pending mutations in order to a working copy, appends
|
|
120
|
+
accepted records, and publishes the updated in-memory state after flushing
|
|
121
|
+
and syncing the log. It returns a list of `CommitQueueItem` objects in queue
|
|
122
|
+
order, or an empty list when there are no pending operations. Each item
|
|
123
|
+
exposes `.result` on success or `.error` if the operation raised
|
|
124
|
+
`JdbException` during application. For example, a rejected `set` is skipped
|
|
125
|
+
while other valid writes in the batch can still commit.
|
|
126
|
+
|
|
127
|
+
Use `execute_and_commit()` with a mutation to queue it and immediately commit
|
|
128
|
+
it alongside any earlier pending writes. To inspect a batch of queued writes,
|
|
129
|
+
disable automatic commits and inspect the result of an explicit `commit()`.
|
|
130
|
+
Pending writes can be lost if the process exits before committing. The
|
|
131
|
+
context manager commits on exit even if its body raises an exception.
|
|
132
|
+
|
|
133
|
+
The log stores one operation per line:
|
|
134
|
+
|
|
135
|
+
```jsonl
|
|
136
|
+
{"op":"put","k":"user:1","v":{"name":"Aoba","score":10}}
|
|
137
|
+
{"op":"del","k":"user:2"}
|
|
138
|
+
{"op":"set","k":"user:1","f":"state","v":"NY"}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Opening a database replays records in order, with later operations taking
|
|
142
|
+
precedence. Replay skips blank lines, malformed JSON, and invalid UTF-8
|
|
143
|
+
and continues reading later records. On open, JackDB truncates these lines
|
|
144
|
+
when they follow the last line that parses as JSON. This removes incomplete
|
|
145
|
+
final writes as well as malformed trailing lines that end with a newline.
|
|
146
|
+
A line that parses as JSON but is rejected during replay, such as an unknown
|
|
147
|
+
operation, is skipped and preserved. On open, JackDB adds a newline to a
|
|
148
|
+
log that lacks a final line break before replaying it.
|
|
149
|
+
|
|
150
|
+
### Compaction and concurrency
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
from jackdb import JackDB
|
|
154
|
+
|
|
155
|
+
db = JackDB("./mydb.log", auto_commit=False)
|
|
156
|
+
db.compact()
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
`compact()` rewrites the log as one `put` record per live key, removing
|
|
160
|
+
superseded records and deletion tombstones. It includes pending mutations,
|
|
161
|
+
so successful compaction also persists those writes. If applying a pending
|
|
162
|
+
operation raises an error, compaction raises it. Use `commit()` to get
|
|
163
|
+
individual operation results. The replacement is written and synced in a temporary
|
|
164
|
+
file beside the log, installed with `os.replace()`, and followed by a directory
|
|
165
|
+
sync. Filesystem and serialization errors propagate to the caller.
|
|
166
|
+
|
|
167
|
+
A reentrant lock serializes `do_operation()`, `commit()`, and `compact()`
|
|
168
|
+
within an instance. A sidecar file at `<replay_path>.lock` serializes log
|
|
169
|
+
loading, commits, and compaction across cooperating processes. A commit with
|
|
170
|
+
pending writes replays the log if it has changed since the instance last
|
|
171
|
+
loaded or wrote it. Compaction always replays the latest log before applying
|
|
172
|
+
pending writes.
|
|
173
|
+
|
|
174
|
+
Other instances do not automatically refresh their in-memory data after a
|
|
175
|
+
writer commits. File locking does not make multiple operations a transaction
|
|
176
|
+
or provide fully synchronized reads across instances. Direct `do_*` calls
|
|
177
|
+
also bypass the instance lock.
|
|
178
|
+
|
|
179
|
+
### Error handling
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
from jackdb.exceptions import JdbException
|
|
183
|
+
from jackdb import JackDB
|
|
184
|
+
|
|
185
|
+
try:
|
|
186
|
+
db = JackDB("./mydb.log", auto_commit=False)
|
|
187
|
+
db.do_operation({"op": "unknown"})
|
|
188
|
+
except JdbException as error:
|
|
189
|
+
print(error.message, error.error_code)
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
| Error code | Cause |
|
|
193
|
+
|------------|-------------------------------------------------|
|
|
194
|
+
| `1` | `set` targets a value that is not a dictionary. |
|
|
195
|
+
| `2` | `set` targets a missing key. |
|
|
196
|
+
| `3` | Unsupported operation name. |
|
|
197
|
+
| `4` | Missing operation key or required field. |
|
|
198
|
+
| `5` | Operation text is not valid JSON. |
|
|
199
|
+
| `6` | Database name must be set before serving API. |
|
|
200
|
+
| `7` | Replay found a nonempty non-string key. |
|
|
201
|
+
|
|
202
|
+
An empty log path raises `ValueError`. Filesystem errors and unsupported
|
|
203
|
+
Python input types may raise standard exceptions; operation validation does
|
|
204
|
+
not cover every possible malformed input.
|
|
205
|
+
|
|
206
|
+
JackDB uses Python's standard logging. Configure the `jackdb` logger in your
|
|
207
|
+
application to see diagnostics for replay, commits, and compaction.
|
|
208
|
+
|
|
209
|
+
## HTTP API
|
|
210
|
+
|
|
211
|
+
A named database attaches a FastAPI application. To run it with Uvicorn:
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
from jackdb import JackDB
|
|
215
|
+
import uvicorn
|
|
216
|
+
|
|
217
|
+
if __name__ == "__main__":
|
|
218
|
+
db = JackDB("./mydb.log", db_name="demo")
|
|
219
|
+
uvicorn.run(db.api, host="127.0.0.1", port=8000)
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
`GET http://127.0.0.1:8000/demo/size` returns the current number of keys as a
|
|
223
|
+
JSON integer. FastAPI's interactive documentation is available at `/docs`.
|
|
224
|
+
|
|
225
|
+
`GET /demo/get/{key}` returns the stored JSON value, or `null` if the key is
|
|
226
|
+
missing. `PUT /demo/put/{key}` accepts a body such as
|
|
227
|
+
`{"value": "hello"}` or `{"value": {"name": "Aoba", "score": 10}}` and
|
|
228
|
+
returns the stored value. `PUT /demo/set/{key}` accepts a body such as
|
|
229
|
+
`{"field": "score", "value": 123}`. SET adds or updates a field on an existing
|
|
230
|
+
JSON object and returns the updated object. A missing key or a value that is
|
|
231
|
+
not an object returns HTTP 409. Each successful PUT commits pending operations
|
|
232
|
+
before responding, regardless of `auto_commit`.
|
|
233
|
+
|
|
234
|
+
`DELETE /demo/del/{key}` takes no request body, removes the key if present,
|
|
235
|
+
and commits pending operations. It returns HTTP 200 with
|
|
236
|
+
`{"message": "Item successfully deleted.", "id": "<key>"}` even if the key
|
|
237
|
+
was already absent.
|
|
238
|
+
|
|
239
|
+
Both PUT endpoints require a `value` field and accept strings, integers,
|
|
240
|
+
fractional numbers, JSON objects, arrays, and booleans. Objects and arrays can
|
|
241
|
+
contain nested JSON values, including null. A top-level null or an omitted
|
|
242
|
+
`value` returns HTTP 422. SET also requires a string `field`; a missing field
|
|
243
|
+
or a non-string field returns HTTP 422. The embedded storage API additionally
|
|
244
|
+
supports top-level null. Keys must fit in a single URL path segment;
|
|
245
|
+
slash-containing keys are not supported.
|
|
246
|
+
|
|
247
|
+
`db.serve()` builds or replaces `db.api`; it does not start a server. To attach
|
|
248
|
+
an API later, set a nonempty `db.db_name` and call `db.serve()`. Applications
|
|
249
|
+
can also use `serve_routes(name, db)` from `jackdb.api` directly.
|
|
250
|
+
|
|
251
|
+
## Project layout
|
|
252
|
+
|
|
253
|
+
```text
|
|
254
|
+
src/jackdb/__init__.py # Public JackDB export and version
|
|
255
|
+
src/jackdb/main.py # Public JackDB subclass and API attachment
|
|
256
|
+
src/jackdb/core.py # Storage, replay, operations, commit, compaction
|
|
257
|
+
src/jackdb/api.py # FastAPI application and routes
|
|
258
|
+
src/jackdb/api_models.py # Models for FastAPI
|
|
259
|
+
src/jackdb/operations.py # Operation specifications and validation
|
|
260
|
+
src/jackdb/commit_record.py # Queued operations and commit results
|
|
261
|
+
src/jackdb/logger.py # Library logger
|
|
262
|
+
src/jackdb/exceptions.py # JdbException
|
|
263
|
+
tests/test_core.py # Custom runner with 24 database/API checks
|
|
264
|
+
tests/test_items.py # Test data
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
The dependency graph is `main → api → core`, with `main → core` as well.
|
|
268
|
+
The API accepts the core `JackDB` type, and the public class inherits its
|
|
269
|
+
database methods. The core module has no dependency on the API layer.
|
|
270
|
+
|
|
271
|
+
## Development
|
|
272
|
+
|
|
273
|
+
Run commands from the repository root. With [uv](https://docs.astral.sh/uv/):
|
|
274
|
+
|
|
275
|
+
```bash
|
|
276
|
+
uv sync
|
|
277
|
+
PYTHONPATH=src uv run --no-sync python tests/test_core.py
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Setting `PYTHONPATH=src` ensures tests use the checkout, including on systems
|
|
281
|
+
where editable-install discovery is unreliable. The custom runner prints
|
|
282
|
+
passed and failed counts and exits with status 1 if any check fails.
|
|
283
|
+
|
|
284
|
+
To run the same lint command as GitHub Actions:
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
python -m pip install pylint .
|
|
288
|
+
pylint $(git ls-files '*.py')
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
GitHub Actions runs Pylint on pushes for Python 3.10 through 3.14.
|
|
292
|
+
|
|
293
|
+
After changing dependencies in `pyproject.toml`, update the lockfile and
|
|
294
|
+
regenerate the requirements export. The lockfile is local and ignored by Git.
|
|
295
|
+
|
|
296
|
+
```bash
|
|
297
|
+
uv lock
|
|
298
|
+
uv export --locked --no-dev --no-emit-project --no-hashes -o requirements.txt
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
Keep `pyproject.toml` as the source of dependency declarations and regenerate
|
|
302
|
+
`requirements.txt` instead of editing the export by hand.
|
jackdb-0.3.2/README.md
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# JackDB
|
|
2
|
+
|
|
3
|
+
JackDB is an embedded Python key-value database backed by a JSON Lines
|
|
4
|
+
append-only log. Data is held in memory, replayed from disk when opened, and
|
|
5
|
+
persisted through explicit or automatic commits. Reads see an instance's
|
|
6
|
+
committed state. JackDB can compact the log and expose the database through
|
|
7
|
+
FastAPI size, get, put, set, and del endpoints.
|
|
8
|
+
|
|
9
|
+
[](https://github.com/gamecube7561/JackDB/actions/workflows/pylint.yml)
|
|
10
|
+
[](https://github.com/gamecube7561/JackDB/actions/workflows/tests.yml)
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
Requires Python 3.10+ and a Unix-like system such as Linux or macOS. File
|
|
14
|
+
locking uses `fcntl`, so native Windows is currently unsupported.
|
|
15
|
+
|
|
16
|
+
By default, JackDB attempts to commit each write immediately.
|
|
17
|
+
|
|
18
|
+
From the repository root:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
python -m pip install .
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from jackdb import JackDB
|
|
28
|
+
|
|
29
|
+
with JackDB("./mydb.log") as db:
|
|
30
|
+
db.do_operation({"op": "put", "k": "user:1", "v": {"name": "Aoba"}})
|
|
31
|
+
db.do_operation({"op": "set", "k": "user:1", "f": "state", "v": "NY"})
|
|
32
|
+
user = db.do_operation({"op": "get", "k": "user:1"})
|
|
33
|
+
print(user) # {'name': 'Aoba', 'state': 'NY'}
|
|
34
|
+
```
|
|
35
|
+
Installing the project uses `pyproject.toml`, which declares
|
|
36
|
+
`fastapi[standard]` and `pydantic`. FastAPI's standard extras include Uvicorn.
|
|
37
|
+
`requirements.txt` contains an exported, pinned dependency set.
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from jackdb import JackDB
|
|
43
|
+
|
|
44
|
+
with JackDB("./mydb.log", auto_commit=False) as db:
|
|
45
|
+
receipt = db.do_operation({"op": "put", "k": "user:1", "v": {"name": "Aoba"}})
|
|
46
|
+
print(receipt) # {'status': 'pending'}
|
|
47
|
+
db.do_operation({"op": "set", "k": "user:1", "f": "state", "v": "NY"})
|
|
48
|
+
db.commit()
|
|
49
|
+
user = db.do_operation({"op": "get", "k": "user:1"})
|
|
50
|
+
print(user) # {'name': 'Aoba', 'state': 'NY'}
|
|
51
|
+
print(len(db)) # 1
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`from jackdb.main import JackDB` also remains supported. Operations accept
|
|
55
|
+
Python dictionaries or JSON strings. Use string keys and JSON-serializable
|
|
56
|
+
values. Replay rejects nonempty non-string keys, even if the live write
|
|
57
|
+
accepted them. Tuples in values become lists after reopening.
|
|
58
|
+
|
|
59
|
+
### Constructor
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from jackdb import JackDB
|
|
63
|
+
|
|
64
|
+
JackDB(replay_path="./mydb.log", auto_commit=True, commit_size=1, db_name=None)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
| Argument | Behavior |
|
|
68
|
+
|---------------|-----------------------------------------------------------------------------------------------------------------------|
|
|
69
|
+
| `replay_path` | Required nonempty string path. Creates the log if absent; its parent directory must already exist. |
|
|
70
|
+
| `auto_commit` | Enables automatic commits during `do_operation()`. Defaults to `True`. |
|
|
71
|
+
| `commit_size` | Commits when pending records **reach** this number. The default `1` commits on the first pending mutation. |
|
|
72
|
+
| `db_name` | A nonempty name builds a FastAPI app and attaches it as `db.api`. By default, both `db_name` and `db.api` are `None`. |
|
|
73
|
+
|
|
74
|
+
### Operations
|
|
75
|
+
|
|
76
|
+
Call these through `db.do_operation(record)`:
|
|
77
|
+
|
|
78
|
+
| `op` | Required fields | Read result or successful commit result |
|
|
79
|
+
|------------|----------------------|----------------------------------------------------------------------------------|
|
|
80
|
+
| `put` | `k`, `v` | Stores or overwrites a value and returns it. |
|
|
81
|
+
| `del` | `k` | Removes a key; returns `True` if present, otherwise `False`. |
|
|
82
|
+
| `get` | `k` | Returns a value, or `None` when absent. |
|
|
83
|
+
| `get_many` | `k` (a list of keys) | Returns `(key, value)` tuples in input order; missing values are `None`. |
|
|
84
|
+
| `set` | `k`, `f`, `v` | Sets a field on an existing dictionary value and returns the updated dictionary. |
|
|
85
|
+
|
|
86
|
+
`put`, `del`, and `set` queue a copy of the operation and return
|
|
87
|
+
`{"status": "pending"}`, even when the call triggers an automatic commit.
|
|
88
|
+
`get` and `get_many` read committed values and add no records.
|
|
89
|
+
A stored `None` and a missing key both return `None`; use `key in db` to
|
|
90
|
+
check existence. The database also supports `len(db)`, `db[key]`, iteration
|
|
91
|
+
over keys, and `db.keys()`. `db[key]` raises `KeyError` for a missing key.
|
|
92
|
+
Reads return deep copies of stored values. The lower-level `do_put`, `do_del`,
|
|
93
|
+
and `do_set` methods operate on a supplied dictionary and bypass journaling.
|
|
94
|
+
|
|
95
|
+
### Persistence and recovery
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from jackdb import JackDB
|
|
99
|
+
|
|
100
|
+
db = JackDB("./mydb.log", auto_commit=False)
|
|
101
|
+
results = db.execute_and_commit({"op": "put", "k": "cfg", "v": [1, 2, 3]})
|
|
102
|
+
print(results[-1].result) # [1, 2, 3]
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`commit()` applies pending mutations in order to a working copy, appends
|
|
106
|
+
accepted records, and publishes the updated in-memory state after flushing
|
|
107
|
+
and syncing the log. It returns a list of `CommitQueueItem` objects in queue
|
|
108
|
+
order, or an empty list when there are no pending operations. Each item
|
|
109
|
+
exposes `.result` on success or `.error` if the operation raised
|
|
110
|
+
`JdbException` during application. For example, a rejected `set` is skipped
|
|
111
|
+
while other valid writes in the batch can still commit.
|
|
112
|
+
|
|
113
|
+
Use `execute_and_commit()` with a mutation to queue it and immediately commit
|
|
114
|
+
it alongside any earlier pending writes. To inspect a batch of queued writes,
|
|
115
|
+
disable automatic commits and inspect the result of an explicit `commit()`.
|
|
116
|
+
Pending writes can be lost if the process exits before committing. The
|
|
117
|
+
context manager commits on exit even if its body raises an exception.
|
|
118
|
+
|
|
119
|
+
The log stores one operation per line:
|
|
120
|
+
|
|
121
|
+
```jsonl
|
|
122
|
+
{"op":"put","k":"user:1","v":{"name":"Aoba","score":10}}
|
|
123
|
+
{"op":"del","k":"user:2"}
|
|
124
|
+
{"op":"set","k":"user:1","f":"state","v":"NY"}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Opening a database replays records in order, with later operations taking
|
|
128
|
+
precedence. Replay skips blank lines, malformed JSON, and invalid UTF-8
|
|
129
|
+
and continues reading later records. On open, JackDB truncates these lines
|
|
130
|
+
when they follow the last line that parses as JSON. This removes incomplete
|
|
131
|
+
final writes as well as malformed trailing lines that end with a newline.
|
|
132
|
+
A line that parses as JSON but is rejected during replay, such as an unknown
|
|
133
|
+
operation, is skipped and preserved. On open, JackDB adds a newline to a
|
|
134
|
+
log that lacks a final line break before replaying it.
|
|
135
|
+
|
|
136
|
+
### Compaction and concurrency
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
from jackdb import JackDB
|
|
140
|
+
|
|
141
|
+
db = JackDB("./mydb.log", auto_commit=False)
|
|
142
|
+
db.compact()
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
`compact()` rewrites the log as one `put` record per live key, removing
|
|
146
|
+
superseded records and deletion tombstones. It includes pending mutations,
|
|
147
|
+
so successful compaction also persists those writes. If applying a pending
|
|
148
|
+
operation raises an error, compaction raises it. Use `commit()` to get
|
|
149
|
+
individual operation results. The replacement is written and synced in a temporary
|
|
150
|
+
file beside the log, installed with `os.replace()`, and followed by a directory
|
|
151
|
+
sync. Filesystem and serialization errors propagate to the caller.
|
|
152
|
+
|
|
153
|
+
A reentrant lock serializes `do_operation()`, `commit()`, and `compact()`
|
|
154
|
+
within an instance. A sidecar file at `<replay_path>.lock` serializes log
|
|
155
|
+
loading, commits, and compaction across cooperating processes. A commit with
|
|
156
|
+
pending writes replays the log if it has changed since the instance last
|
|
157
|
+
loaded or wrote it. Compaction always replays the latest log before applying
|
|
158
|
+
pending writes.
|
|
159
|
+
|
|
160
|
+
Other instances do not automatically refresh their in-memory data after a
|
|
161
|
+
writer commits. File locking does not make multiple operations a transaction
|
|
162
|
+
or provide fully synchronized reads across instances. Direct `do_*` calls
|
|
163
|
+
also bypass the instance lock.
|
|
164
|
+
|
|
165
|
+
### Error handling
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
from jackdb.exceptions import JdbException
|
|
169
|
+
from jackdb import JackDB
|
|
170
|
+
|
|
171
|
+
try:
|
|
172
|
+
db = JackDB("./mydb.log", auto_commit=False)
|
|
173
|
+
db.do_operation({"op": "unknown"})
|
|
174
|
+
except JdbException as error:
|
|
175
|
+
print(error.message, error.error_code)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
| Error code | Cause |
|
|
179
|
+
|------------|-------------------------------------------------|
|
|
180
|
+
| `1` | `set` targets a value that is not a dictionary. |
|
|
181
|
+
| `2` | `set` targets a missing key. |
|
|
182
|
+
| `3` | Unsupported operation name. |
|
|
183
|
+
| `4` | Missing operation key or required field. |
|
|
184
|
+
| `5` | Operation text is not valid JSON. |
|
|
185
|
+
| `6` | Database name must be set before serving API. |
|
|
186
|
+
| `7` | Replay found a nonempty non-string key. |
|
|
187
|
+
|
|
188
|
+
An empty log path raises `ValueError`. Filesystem errors and unsupported
|
|
189
|
+
Python input types may raise standard exceptions; operation validation does
|
|
190
|
+
not cover every possible malformed input.
|
|
191
|
+
|
|
192
|
+
JackDB uses Python's standard logging. Configure the `jackdb` logger in your
|
|
193
|
+
application to see diagnostics for replay, commits, and compaction.
|
|
194
|
+
|
|
195
|
+
## HTTP API
|
|
196
|
+
|
|
197
|
+
A named database attaches a FastAPI application. To run it with Uvicorn:
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
from jackdb import JackDB
|
|
201
|
+
import uvicorn
|
|
202
|
+
|
|
203
|
+
if __name__ == "__main__":
|
|
204
|
+
db = JackDB("./mydb.log", db_name="demo")
|
|
205
|
+
uvicorn.run(db.api, host="127.0.0.1", port=8000)
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
`GET http://127.0.0.1:8000/demo/size` returns the current number of keys as a
|
|
209
|
+
JSON integer. FastAPI's interactive documentation is available at `/docs`.
|
|
210
|
+
|
|
211
|
+
`GET /demo/get/{key}` returns the stored JSON value, or `null` if the key is
|
|
212
|
+
missing. `PUT /demo/put/{key}` accepts a body such as
|
|
213
|
+
`{"value": "hello"}` or `{"value": {"name": "Aoba", "score": 10}}` and
|
|
214
|
+
returns the stored value. `PUT /demo/set/{key}` accepts a body such as
|
|
215
|
+
`{"field": "score", "value": 123}`. SET adds or updates a field on an existing
|
|
216
|
+
JSON object and returns the updated object. A missing key or a value that is
|
|
217
|
+
not an object returns HTTP 409. Each successful PUT commits pending operations
|
|
218
|
+
before responding, regardless of `auto_commit`.
|
|
219
|
+
|
|
220
|
+
`DELETE /demo/del/{key}` takes no request body, removes the key if present,
|
|
221
|
+
and commits pending operations. It returns HTTP 200 with
|
|
222
|
+
`{"message": "Item successfully deleted.", "id": "<key>"}` even if the key
|
|
223
|
+
was already absent.
|
|
224
|
+
|
|
225
|
+
Both PUT endpoints require a `value` field and accept strings, integers,
|
|
226
|
+
fractional numbers, JSON objects, arrays, and booleans. Objects and arrays can
|
|
227
|
+
contain nested JSON values, including null. A top-level null or an omitted
|
|
228
|
+
`value` returns HTTP 422. SET also requires a string `field`; a missing field
|
|
229
|
+
or a non-string field returns HTTP 422. The embedded storage API additionally
|
|
230
|
+
supports top-level null. Keys must fit in a single URL path segment;
|
|
231
|
+
slash-containing keys are not supported.
|
|
232
|
+
|
|
233
|
+
`db.serve()` builds or replaces `db.api`; it does not start a server. To attach
|
|
234
|
+
an API later, set a nonempty `db.db_name` and call `db.serve()`. Applications
|
|
235
|
+
can also use `serve_routes(name, db)` from `jackdb.api` directly.
|
|
236
|
+
|
|
237
|
+
## Project layout
|
|
238
|
+
|
|
239
|
+
```text
|
|
240
|
+
src/jackdb/__init__.py # Public JackDB export and version
|
|
241
|
+
src/jackdb/main.py # Public JackDB subclass and API attachment
|
|
242
|
+
src/jackdb/core.py # Storage, replay, operations, commit, compaction
|
|
243
|
+
src/jackdb/api.py # FastAPI application and routes
|
|
244
|
+
src/jackdb/api_models.py # Models for FastAPI
|
|
245
|
+
src/jackdb/operations.py # Operation specifications and validation
|
|
246
|
+
src/jackdb/commit_record.py # Queued operations and commit results
|
|
247
|
+
src/jackdb/logger.py # Library logger
|
|
248
|
+
src/jackdb/exceptions.py # JdbException
|
|
249
|
+
tests/test_core.py # Custom runner with 24 database/API checks
|
|
250
|
+
tests/test_items.py # Test data
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
The dependency graph is `main → api → core`, with `main → core` as well.
|
|
254
|
+
The API accepts the core `JackDB` type, and the public class inherits its
|
|
255
|
+
database methods. The core module has no dependency on the API layer.
|
|
256
|
+
|
|
257
|
+
## Development
|
|
258
|
+
|
|
259
|
+
Run commands from the repository root. With [uv](https://docs.astral.sh/uv/):
|
|
260
|
+
|
|
261
|
+
```bash
|
|
262
|
+
uv sync
|
|
263
|
+
PYTHONPATH=src uv run --no-sync python tests/test_core.py
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
Setting `PYTHONPATH=src` ensures tests use the checkout, including on systems
|
|
267
|
+
where editable-install discovery is unreliable. The custom runner prints
|
|
268
|
+
passed and failed counts and exits with status 1 if any check fails.
|
|
269
|
+
|
|
270
|
+
To run the same lint command as GitHub Actions:
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
python -m pip install pylint .
|
|
274
|
+
pylint $(git ls-files '*.py')
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
GitHub Actions runs Pylint on pushes for Python 3.10 through 3.14.
|
|
278
|
+
|
|
279
|
+
After changing dependencies in `pyproject.toml`, update the lockfile and
|
|
280
|
+
regenerate the requirements export. The lockfile is local and ignored by Git.
|
|
281
|
+
|
|
282
|
+
```bash
|
|
283
|
+
uv lock
|
|
284
|
+
uv export --locked --no-dev --no-emit-project --no-hashes -o requirements.txt
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
Keep `pyproject.toml` as the source of dependency declarations and regenerate
|
|
288
|
+
`requirements.txt` instead of editing the export by hand.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "JackDB"
|
|
3
|
+
version = "0.3.2"
|
|
4
|
+
description = "Simple embedded key-value database"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
dependencies = [
|
|
10
|
+
"fastapi[standard]>=0.162.0",
|
|
11
|
+
"pydantic>=2.14.2",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[project.urls]
|
|
15
|
+
Repository = "https://github.com/gamecube7561/JackDB"
|
|
16
|
+
Issues = "https://github.com/gamecube7561/JackDB/issues"
|
|
17
|
+
|
|
18
|
+
[build-system]
|
|
19
|
+
requires = ["setuptools>=77"]
|
|
20
|
+
build-backend = "setuptools.build_meta"
|
|
21
|
+
|
|
22
|
+
[tool.setuptools.packages.find]
|
|
23
|
+
where = ["src"]
|
|
24
|
+
|
|
25
|
+
[tool.pylint.main]
|
|
26
|
+
# Work around Python 3.14 skipping uv's hidden-flagged editable-install .pth.
|
|
27
|
+
init-hook = 'import sys, os; p = os.path.join(os.getcwd(), "src"); sys.path.insert(0, p) if os.path.isdir(p) and p not in sys.path else None'
|
jackdb-0.3.2/setup.cfg
ADDED