Metadata-Version: 2.4
Name: kernodb
Version: 1.0.0a5
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Intended Audience :: Developers
Classifier: Topic :: Database
Classifier: Typing :: Typed
Summary: Kerno: an embedded database of units, revisions and relations, for AI memory
Keywords: database,embedded,memory,ai,storage
License-Expression: MIT OR Apache-2.0
Requires-Python: >=3.9
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Changelog, https://github.com/Rixmerz/kernodb/blob/main/CHANGELOG.md
Project-URL: Documentation, https://github.com/Rixmerz/kernodb/tree/main/bindings/python#readme
Project-URL: Homepage, https://github.com/Rixmerz/kernodb
Project-URL: Issues, https://github.com/Rixmerz/kernodb/issues
Project-URL: Repository, https://github.com/Rixmerz/kernodb

# kernodb (Python)

Python bindings for [Kerno](https://github.com/Rixmerz/kernodb): an embedded, local-first
database of **units** (typed records), their **revisions** (every change is kept), the
**relations** between them, and queries over all three. It runs inside your process and keeps
its data in a directory on disk. There is no server.

The package is `kernodb`; the module you import is `kerno`.

> **Alpha (1.0.0a5).** It does not open a database written by 1.0.0a4 or any earlier alpha (see
> [Upgrading](#upgrading-from-an-earlier-alpha)), and the keyword index is opt-in per type. The on-disk
> format is frozen, the API is not. Expect breaking changes between alpha releases. See the [changelog](https://github.com/Rixmerz/kernodb/blob/main/CHANGELOG.md).

Guides: [Kerno as a revisioned history next to another database](https://github.com/Rixmerz/kernodb/blob/main/docs/guides/history-alongside.md),
[one unit per entity, keyed by a logical key](https://github.com/Rixmerz/kernodb/blob/main/docs/guides/one-unit-per-entity.md),
and the [coverage of the `kerno` facade in the Python and Node bindings](https://github.com/Rixmerz/kernodb/blob/main/docs/bindings-coverage.md).

## Install

```bash
pip install --pre kernodb        # --pre: only alpha versions exist so far
```

The package is named `kernodb` but the module you import is `kerno` (`import kerno`); `import kernodb`
raises `ModuleNotFoundError`.

Wheels are published for CPython 3.9+ (one abi3 wheel per platform) on Linux x86_64 and
aarch64 (glibc; musl from the next release), macOS x86_64 and arm64, and Windows x86_64.
On any other platform pip builds from the source distribution, which needs a Rust toolchain.

Durable commits are verified on Linux (ext4, xfs) only; elsewhere the database works but that
guarantee is not verified (see [Durability](#durability)).

## Quickstart

```python
import kerno

with kerno.open("./memory-db") as db:                 # creates the directory if needed
    note = db.store("note", "Rust has no GC")         # (unit_type, payload) -> id
    topic = db.store("topic", b"memory")              # str is stored as UTF-8, bytes as they are

    db.relate(note, topic, "about")                   # (from, to, relation_type) -> relation id
    print([r.to_id for r in db.related(note)])        # outgoing relations

    db.update(note, "Rust has no garbage collector")  # a new revision; the old one is kept
    for rev in db.history(note, with_payload=True):   # newest first
        print(rev.revision, rev.kind, rev.payload)

    print([u.id for u in db.query("note")])           # every active unit of a type
```

Leaving the `with` block closes the database; without it, call `db.close()`.

## Concepts

- **Keyword index**: off by default; `set_fulltext(type, True)` turns it on for one type.
- **Unit**: a record with an id (32 hex digits), a `unit_type` (1–64 bytes), a payload (up to
  1 MiB of bytes) and a lifecycle: `active`, `archived` or `deleted`.
- **Revision**: every write to a unit (create, update, archive, unarchive, delete, restore) adds
  one. Nothing is overwritten. A delete leaves a tombstone, and the history stays readable.
- **Relation**: a directed, typed edge between two units. Parallel relations and self-loops
  are allowed unless you ask for `unique=True`.
- **Change set**: one atomic commit. `commit(ops)` puts many writes in one change set (at most
  10,000 operations). A reader sees all of them or none, and a crash keeps all or none.
- **Commit sequence**: every change set has a number that only grows. `read_as_of(id, seq)`
  reads a unit as it was right after commit `seq`. `last_commit_seq()` is "now".

## Common tasks

### Write many things at once (and fast)

```python
out = db.commit(
    [
        ("store", "symbol", payload_a),        # op 0
        ("store", "symbol", payload_b),        # op 1
        ("relate", 0, 1, "calls"),             # an end is the position of an earlier "store" or "upsert" op
        ("relate", existing_id, 0, "calls"),   # ... or an existing unit id
        ("update_if_changed", other_id, new),  # writes nothing if the payload is the same
        ("delete_cascade", old_id),            # removes its relations too
    ],
    reason="index run 42", actor="indexer",    # recorded with every revision
    detailed=True,
)
out["units"], out["relations"], out["unchanged"], out["upserts"], out["commit_seq"]
```

All batch ops: `("store", type, payload)`, `("update", id, payload)`,
`("update_if_changed", id, payload)`, `("delete", id)`, `("delete_cascade", id)`,
`("unrelate", relation_id)`, `("relate", from, to, type)`, `("relate_unique", from, to, type)`,
`("archive", id)`, `("unarchive", id)`, `("restore", id, revision)`,
`("upsert", type, field, document)` (see below; its result is in `out["upserts"]`, not `units`).

### Keep one unit per natural key (upsert)

Ids are random, so a logical key (an email, a path) needs a lookup. Declare the field `unique`
and `upsert` does it for you: it revises the active unit that holds the same value, or stores a
new one.

```python
db.define_schema("user", [{"name": "email", "type": "text", "unique": True}, {"name": "name", "type": "text"}])
r = db.upsert("user", "email", {"email": "a@example.com", "name": "Ada"})
r                      # {"id": "...", "outcome": "created"}
db.upsert("user", "email", {"email": "a@example.com", "name": "Ada"})["outcome"]     # "unchanged": no revision
db.upsert("user", "email", {"email": "a@example.com", "name": "Ada L."})["outcome"]  # "updated": a new revision
db.history(r["id"], order="asc")                 # create, update
```

In `commit`, `("upsert", type, field, document)` runs in the same change set as the other ops, and
several upserts of one key in a batch make one unit (the last document wins). A field that is
not declared `unique` raises `NotUniqueError`; a document without a value in it raises
`SchemaError`. Only active units hold a key: an archived or deleted one does not.

### Look things up by a field

```python
db.define_schema("user", [
    {"name": "email", "type": "text", "required": True, "unique": True},  # unique implies indexed
    {"name": "age", "type": "int", "indexed": True},
])
uid = db.store_document("user", {"email": "a@example.com", "age": 31})
db.find("user", "email", "a@example.com")      # -> [Unit]
db.read_document(uid)                          # -> {"email": ..., "age": 31}
db.execute("FIND user AS u WHERE u.age > 30")  # the query language: {"aliases", "rows"}
```

Field types: `bool`, `int`, `float`, `text`, `bytes`. A type has one schema. Defining the same fields again (in any order) is accepted and writes nothing, so
you can define on every start; different fields raise `SchemaExistsError`. A field indexed later with
`create_index` makes the first field list a different schema. An indexed text or bytes value holds at most
254 bytes (a 0x00 byte counts twice); a longer one raises `SchemaError`.
Indexes can be added later with `create_index`. The query language is described in
[docs/query-language.md](https://github.com/Rixmerz/kernodb/blob/main/docs/query-language.md).

Range lookups and query plans work on the same indexed fields:

```python
db.find_range("user", "age", 18, 65)           # both ends included; None leaves an end open
db.find_range("user", "age", low=65)           # age >= 65, ordered by age; `limit=` caps it
db.explain("FIND user AS u WHERE u.age > 30")  # how `execute` would run it, without running it
# {"candidates": [{"access": "type_scan", ...}, {"access": "field_index", "field": "age", "cost": ...}],
#  "chosen": 1, "joins": [], ...}
```

### Search by keyword, vector and relations

```python
db.set_fulltext("note", True)                   # keyword (BM25) search finds only these types
hits = db.hybrid_search(
    "garbage collector",                        # keyword signal
    vector=query_vec, vectors={uid: vec, ...},  # vector signal: your vectors, searched exactly
    seeds=[topic_id],                           # relation signal: units near these seeds
)
hits[0].id, hits[0].score                       # best first; score = sum of the signal scores
hits[0].keyword_score, hits[0].vector_score, hits[0].relation_score   # None: the signal missed it
```

Kerno does not store vectors: you pass the `{unit id: vector}` dict you keep in step with your
units, as in the Rust API. Signals are fused by weighted reciprocal rank fusion; each adds
`weight / (rrf_k + rank)`. Only active units are hits. Types you did not enable with
`set_fulltext` never match the keyword signal. `text_types=["note"]` narrows the keyword signal to some
unit types.

```python
hits = db.search_text("garbage collector", limit=10)                       # TextHit(id, score), best first
notes = db.search_text("garbage collector", types=["note", "memo"])        # only those unit types
```

`search_text` raises instead of returning `[]` when it could never match: `NotIndexedError` (no type, or a
requested type, is keyword-indexed), `UnknownTypeError` (a type no unit, schema or `set_fulltext` ever
named), `InvalidArgumentError` (`limit` below 1, a query with no word, an empty `types`). A search that can
match and finds nothing returns `[]`. A unit scores the same with or without `types`.

### Read the past

```python
seq = db.last_commit_seq()
db.update_document(uid, {"email": "a@example.com", "age": 32})
db.read_as_of(uid, seq).payload   # the payload before the update (raw document bytes)
db.history(uid, limit=10)         # newest first; NotFoundError for an id no unit ever had

# Page through a long history: [from_revision, to_revision), newest first or order="asc"
page = db.history(uid, limit=50)
older = db.history(uid, limit=50, to_revision=page[-1].revision)

diff = db.compare(uid, 1, 3)      # both revisions with payloads: diff.a, diff.b, diff.same_content
db.read(uid).oldest_revision      # the oldest revision still kept; asking for an older one
                                  # raises PrunedError
```

Archived and deleted units stay out of `query()` unless you ask: `db.query("note",
lifecycle="archived")` (or `"deleted"`, or `"all"`). A deleted unit comes back with
`payload=None`; read its history to see what it held. `find` and `find_range` see only active
units.

### Check integrity

```python
report = db.verify()
report["clean"]       # False if anything is wrong
report["findings"]    # what is wrong (ids and counts, never content)
```

Damage in the storage files (a block that fails its checksum) is reported as
`report["storage_error"]`. Reads that touch a damaged block raise `kerno.CorruptionError` and
never return wrong data.

## API reference

| Call | Returns |
|------|---------|
| `kerno.open(path, read_only=False, max_batch_ops=None, verify_payload_on_read=False, durability="sync", close_compaction=True, bulk_without_wal=False)` | a `Database`. A read-only open writes nothing and takes no lock, so it runs beside a writer and reads the commits made up to the open (see `refresh`); a write on it raises `ReadOnlyError`, and a directory with no database raises `DatabaseNotFoundError`. `max_batch_ops` changes the 10,000-operation limit of one `commit`. `verify_payload_on_read=True` makes every read also check each payload against its content hash (`verify()`, history reads and export always do). `durability="buffered"` does not wait for stable storage on each commit (see [Durability](#durability)). `close_compaction=False` makes `close()` only flush, for a bulk load that closes right after its last commit. `bulk_without_wal=True` needs `durability="buffered"` on a read-write handle (anything else raises `InvalidArgumentError`): a large commit then skips the log, is durable once its table is written, and until then a crash or a failed flush can lose it and every commit after it |
| | A read-only open loads each table of the database at the first read that needs it, not at open: a damaged table is reported by that read (`CorruptionError`), and `verify()` still checks them all |
| `db.store(unit_type, payload)` | the new unit's id |
| `db.read(id)` | `Unit` (`id`, `unit_type`, `schema_version`, `lifecycle`, `revision`, `oldest_revision`, `created_at_us`, `updated_at_us`, `payload`) or `None`. `oldest_revision` is the oldest revision retention still keeps |
| `db.update(id, payload, if_changed=False)` | `True` when a revision was written |
| `db.delete(id, cascade=False)` | `None`. Without `cascade`, a unit with relations raises `HasRelationsError` |
| `db.relate(from, to, type, unique=False)` | the relation id. `unique` reuses an existing one |
| `db.unrelate(relation_id)` | `None` |
| `db.related(id, direction="out", relation_type=None)` | `list[Relation]` (`id`, `from_id`, `to_id`, `relation_type`); `NotFoundError` for an id no unit ever had, `InvalidArgumentError` for an invalid type name |
| `db.neighbors(id, direction="out", relation_type=None)` | `list[tuple[Relation, Unit]]`: the relations of `related`, each with the unit at its other end, in one call |
| `db.relations(relation_type=None)` | every live relation, in one scan. Use it instead of `related` per unit |
| `db.query(unit_type=None, schema_version=None, lifecycle="active")` | `list[Unit]` that match, in id order. `lifecycle` is `"active"`, `"archived"`, `"deleted"` or `"all"`; the last three read every unit's head, so they cost time linear in the store |
| `db.commit(ops, reason=None, actor=None, source=None, detailed=False)` | stored ids, or a dict with `detailed=True` |
| `db.history(id, limit=100, with_payload=False, from_revision=None, to_revision=None, order="desc")` | `list[Revision]` (`revision`, `kind`, `commit_seq`, `created_at_us`, `lifecycle`, `schema_version`, `content_hash`, `payload`) in `[from_revision, to_revision)`, at most `limit`. A `from_revision` below `oldest_revision` raises `PrunedError`; an id no unit ever had raises `NotFoundError`; `from_revision` above `to_revision` raises `InvalidArgumentError`. For a deleted unit whose payloads `compact()` dropped, `with_payload=True` lists the revisions with `payload` `None` |
| `db.compare(id, a, b)` | `Comparison` (`a`, `b`: the two `Revision`s with payloads; `same_content`). Takes the unit id, as the Rust call does |
| `db.read_as_of(id, commit_seq)` | the `Revision` in force then, or `None` before creation. A sequence past the last commit reads now |
| `db.set_fulltext(type, enabled)` | `None`. Keyword-index a type (off by default); its active units follow in the same commit |
| `db.fulltext(type)` | `bool`: whether the type is keyword-indexed |
| `db.search_text(query, limit=10, types=None)` | `list[TextHit]` (`id`, `score`), best first, over the keyword-indexed types or only those in `types`. Raises when it could never match (see above) |
| `db.set_substring(type, enabled)` | `None`. Trigram-index a type for substring search (off by default); its active units follow in the same commit |
| `db.substring(type)` | `bool`: whether the type is substring-indexed |
| `db.search_substring(needle, limit=None, types=None)` | `list[str]`: the ids of the units whose payload holds `needle` (`str` or `bytes`), in id order, at most `limit`. ASCII letters match in either case, like SQL's `LIKE '%needle%'`; the first 256 KiB of a payload are searched. `types=None` searches the substring-indexed types, or every type when none is; a type without the trigram index is searched by scanning its payloads, with the same answers. Raises `UnknownTypeError` for a type nothing ever used, and `InvalidArgumentError` for an empty needle or one over 1024 bytes |
| `db.last_commit_seq()` | the sequence of the last commit |
| `db.refresh()` | on a `read_only=True` database: pick up the commits a writer made since the open; returns the sequence now read |
| `db.define_schema(type, fields)` / `db.create_index(type, field)` | `None` |
| `db.store_document(type, dict)` / `db.update_document(id, dict)` / `db.read_document(id)` | id / `None` / `dict` or `None` |
| `unit.document()` | the `dict` of a `Unit` you already hold (from `find`, `query`, `execute`, `neighbors`), decoded without another read; `None` for a deleted unit |
| `db.upsert(type, field, dict)` | `{"id", "outcome"}`: store the document or revise the active unit holding the same value of the unique `field`; `outcome` is `"created"`, `"updated"` or `"unchanged"` |
| `db.find(type, field, value)` | `list[Unit]` whose indexed field equals `value` |
| `db.find_documents(type, field, value, fields=None)` | `find`, with each hit as its document `dict`, without a `Unit` per hit. `fields` (a list of names, or a `{field: key}` mapping that renames them) keeps only those fields. A field declared `"covering": True` in the schema (only with `indexed` or `unique`) keeps documents of at most 2048 bytes in its index entries, so a lookup reads no unit; with `verify_payload_on_read` the units are read and checked as `find` does |
| `db.neighbor_documents(type, field, value, direction="out", type_prefix=None, fields=None)` | `find`, then the neighbours of each unit found, as `[(relation_type, document)]`; `type_prefix` keeps the relation types that start with it |
| `db.set_read_cache_bytes(n)` | `None`. The bytes of units already read and checked that the handle keeps to answer the same reads again until the next commit (32 MiB by default); `0` turns it off. A negative or non-whole `n` raises `InvalidArgumentError` |
| `db.find_range(type, field, low=None, high=None, limit=None)` | `list[Unit]` whose indexed field is in `[low, high]` (both included, `None` = open end), ordered by value then id. No `limit` means no cap; `limit=0`, or `low` above `high`, raises `InvalidArgumentError` |
| `db.execute(query)` | `{"aliases": [...], "rows": [[Unit, ...], ...]}` |
| `db.explain(query)` | the plan `execute` would follow, without running it: `root_alias`, `root_type`, `candidates` (`access` `"type_scan"` or `"field_index"`, `field`, `cost`), `chosen` (an index into `candidates`), `joins`, `planner_version` |
| `db.hybrid_search(text=None, vector=None, vectors=None, seeds=None, *, text_types=None, keyword_weight=1, vector_weight=1, relation_weight=1, limit=10, candidates=100, rrf_k=60, max_depth=2, metric="cosine")` | `list[HybridHit]`, best first: `id`, `score`, `keyword_score`, `vector_score`, `relation_score`, `keyword_rank`, `vector_rank`, `relation_rank` (a signal that missed the unit is `None`). `text_types` restricts the keyword signal to those unit types (a list of names, with the errors of `search_text`). `rrf_k` is a whole number from 0 to 2**32 - 1. `vector` and `vectors` go together; `metric` is `"cosine"` or `"dot"` |
| `db.verify()` | `dict`: `clean`, `commit_seq`, `units`, `revisions`, `relations`, `findings`, maybe `storage_error` |
| `db.compact()` | `{"before", "after"}` stored bytes: `before` is the size when the call was made (every commit so far counts), `after` the size once the space of deleted units' payloads and retention is given back; `after` can be larger than `before` when there is little to reclaim |
| `db.run_retention(default=None, by_type=None)` | prune old revisions; a rule is a dict with one of `keep_last` (newest N, 1 or more), `keep_for` (seconds) or `keep_since` (commit sequence); returns `{"units_retained", "revisions_pruned", "payloads_dropped", "bytes_logically_freed", "commit_seq"}`. Pruned revisions raise `PrunedError` in `read_as_of`, `history(from_revision=)` and `compare`. A bad rule raises `InvalidArgumentError` and writes nothing; a `None` rule is no rule. The policy is not stored: pass it each time |
| `db.close()` | `None`. Closing twice is harmless |

Type stubs ship with the package (`py.typed`).

## Retention and the oldest revision

Retention (dropping old revisions) is a call, not a setting: `db.run_retention(by_type={"note": {"keep_last": 5}})`
prunes now, and the policy is not stored, so pass it each time you prune. Afterwards
`read_as_of`, `history(from_revision=)` and `compare` raise `PrunedError` for a pruned revision.
Before reading an old revision, compare it with `db.read(id).oldest_revision`, or catch
`PrunedError`. A unit that was never pruned has `oldest_revision == 1`.

## Errors

Engine failures raise `kerno.KernoError` or one of its subclasses:

| Exception | When |
|---|---|
| `NotFoundError` | no unit, relation or revision with that id |
| `DeletedError` | the unit is deleted. A unit in the wrong lifecycle for an op (`unarchive` of an active unit) raises `InvalidArgumentError`, not this |
| `LockedError` | another handle or process holds the directory |
| `CorruptionError` | stored bytes failed a checksum or a structure rule |
| `InvalidArgumentError` | also a `ValueError`. A malformed id, a bad type name, direction, order, lifecycle, metric, retention rule or batch op, a bad whole number (negative, over 2**64 - 1, a fraction), a value no document can hold (`None`, an int outside 64 bits), a text search value over 254 bytes, `refresh` on a handle that is not read-only, ... Its subclasses below name the common cases |
| `ClosedError` | any call on a closed database; also a `ValueError` |
| `SchemaExistsError` | `define_schema` for a type that already has a different schema |
| `NotIndexedError` | `find`, `find_range` on a field that is not indexed; `search_text` or a keyword signal while no type (or no requested type) has `set_fulltext` |
| `UnknownTypeError` | `search_text` or `hybrid_search(text_types=)` names a type that no unit, schema or `set_fulltext` ever named |
| `NotUniqueError` | `upsert` by a field the schema does not declare unique (or a type with no schema) |
| `BadEndpointError` | a batch `relate` end that is not an earlier `store` or `upsert` op |
| `PayloadTooLargeError` | a payload over 1 MiB |
| `ChangeSetTooLargeError` | a batch over 10,000 ops (the default; `max_batch_ops` changes it) or over the engine's bytes per commit (1 GiB). A subclass of `PayloadTooLargeError` |
| `ReadOnlyError` | a write through a handle opened with `read_only=True`. An empty `commit([])`, or a `run_retention` that has nothing to prune, writes nothing and does not raise |
| `DatabaseNotFoundError` | `read_only=True` on a directory with no database |
| `HasRelationsError` | deleting a unit that still has relations, without `cascade` |
| `PrunedError` | retention removed the revision asked for |
| `HistoryUnavailableError` | `read_as_of` or `compare` of a deleted unit's revision after `compact()`, which dropped its payloads. `history(with_payload=True)` of such a unit lists the revisions with `payload` `None` instead |
| `SchemaError` | the type's schema refused a document, or a typed unit got a payload that is not a document |

Every argument refusal is a `KernoError`: a malformed id, direction, order, lifecycle, metric, retention rule or
batch op (a tuple that names no op or has the wrong number of items), or a `vector` without `vectors` (or the
reverse), raises `InvalidArgumentError`, which is also a `ValueError`; a call on a closed database raises
`ClosedError`, also a `ValueError`. `kerno.__version__` is the installed version. An argument of the wrong Python type (`None` or an `int` for a `str`, a `list` for a
payload), or a `str` with a lone surrogate, which has no UTF-8 form, raises `InvalidArgumentError`
(never a bare `TypeError` or `UnicodeEncodeError`).
A payload or document `bytes` value is `bytes`, `bytearray` or a `memoryview`, never a list of ints.

## Performance

These numbers are from one Linux machine (ext4), with every commit synced to disk, comparing Kerno 1.0.0a2 to SQLite in
WAL mode with `synchronous=FULL` storing the same data (with a revision table). The full
method is in
[docs/testing/python-vs-sqlite.md](https://github.com/Rixmerz/kernodb/blob/main/docs/testing/python-vs-sqlite.md).

| Workload | Kerno vs SQLite |
|---|---|
| one write per commit | about the same |
| point read by id, history of a unit | 0.8–1.4x |
| writes in one batch | about 3x slower |
| relations in one batch | 6–11x slower |
| disk | about 2x smaller here; LZ4 blocks halve a real store (66 → 35 MB) |

What makes the most difference:

1. **Batch your writes.** One `commit([...])` is about 10x faster than the same writes one by
   one, because each commit syncs to disk.
2. **The keyword index is opt-in.** Since 1.0.0-alpha.2 no type is indexed for keyword search
   (BM25) unless you call `db.set_fulltext("my_type", True)`. Indexing costs writes several
   times over and about 3.5x the disk, so turn it on only for the types you search by text.
   The setting is stored in the database, and switching a type that already has units
   indexes (or clears) them in the same commit.
3. **Read relations in bulk** with `relations(type)` instead of `related(id)` per unit.
4. **Mind the batch limit.** A commit holds 10,000 operations by default. Split larger work, or open with `max_batch_ops=` when it must be atomic.

## Durability

`kerno.open` asks for synchronous commits by default: when a commit returns, it is on stable storage.
With `durability="buffered"` a commit does not wait for stable storage (no `fsync` per commit, like
SQLite's `synchronous=NORMAL` in WAL mode): after a power cut the last commits may be missing, never
torn, and `close()` still makes everything durable. The synchronous guarantee is verified on Linux (ext4, xfs) only. On other platforms the database opens
and works without that verification. In our tests, killing the process with `kill -9` in the
middle of a stream of commits never lost an acknowledged commit and never left half a batch.

## Concurrency

One read-write `Database` holds a database directory at a time: a second read-write open raises
`LockedError`. A read-only open takes no lock, so readers run beside the writer, in other processes
or in the same one. Inside the process, a `Database` can be shared between threads. Calls release
the GIL while they touch disk, any number of readers run while one writer commits, and a read
never sees half of a change set.

## Limits

- Payload: 1 MiB. Unit and relation type: 1–64 bytes (no NUL in a relation type).
- One change set: a unit is written by at most one operation of it (a second `update`, `delete`, `archive`, ... of the same unit is `InvalidArgumentError`; relations to it are not limited). It holds 10,000 operations by default (`kerno.open(path, max_batch_ops=...)` changes it, up to 2^20); the engine also limits the bytes of one commit.
- `find` and `find_range` return active units only. The Rust API has no index of archived or
  deleted units, so `query(lifecycle=...)` reads every unit's head instead of looking one up.
- Deleting a unit keeps its revisions' history entries; `compact()` drops the payloads of deleted units
  (an as-of read of one of their old revisions is then no longer possible) and gives their space back.
  Until you call it, deletes do not shrink the store.
- An indexed text or bytes value holds at most 254 bytes (a 0x00 byte counts twice).
- Integers in documents are 64-bit (-2**63 to 2**63 - 1); a counter or revision argument is a whole number from 0 to 2**64 - 1 (`limit` and `keep_last` from 1).

## Upgrading from an earlier alpha

This build does not open a database written by 1.0.0-alpha.4 or any earlier alpha. Their units layer
is 9 or below (alpha.1: 1 to 6) and this build needs 12 (a database that uses a `covering` field is at
13). Opening one fails with an error whose message says the database's `layer_version` predates the
format of this build and to export and import it (`LayerTooOld`; from Python a `KernoError`). Nothing in
the directory changes. To carry it over, export it with the command-line tool of the build that wrote it
and import it with the one of this build:

```sh
kerno old-db export old.kexp        # kerno CLI of the build that wrote old-db
kerno import old.kexp new-db        # kerno CLI of this build
```

Then turn on the keyword index of every type you search by text (it is off by default since
1.0.0-alpha.2), and define again the schemas and indexes: an export holds neither.

## Development

```bash
cd bindings/python
python3 -m venv .venv && . .venv/bin/activate
pip install maturin pytest hypothesis
maturin develop --release
pytest
```

The crate is excluded from the cargo workspace (it carries its own `Cargo.lock` and is built by
maturin). Check it on its own with `cargo clippy --all-targets -- -D warnings`.

