Metadata-Version: 2.4
Name: redb
Version: 0.6.0
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Rust
License-File: LICENSE-APACHE
License-File: LICENSE-MIT
Summary: Python bindings for redb, an embedded key-value store
Home-Page: https://www.redb.org
License-Expression: MIT OR Apache-2.0
Requires-Python: >=3.12
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM

# redb

Python bindings for [redb](https://github.com/cberner/redb), a simple, portable, high-performance,
ACID, embedded key-value store.

The bindings use the same file format as the Rust crate, so a database can be read and written
from both languages.

```bash
pip install redb
```

Requires Python 3.12 or later.

```python
import redb

TABLE = redb.TableDefinition("my_data", redb.str, redb.u64)

with redb.Database.create("my_db.redb") as db:
    with db.begin_write() as txn:
        table = txn.open_table(TABLE)
        table["my_key"] = 123

    read_txn = db.begin_read()
    table = read_txn.open_table(TABLE)
    assert table["my_key"] == 123
```

Leaving the `with` block closes the database, as `close()` does. Close every database you open
rather than leaving it to the garbage collector: a process can exit without collecting it, for
example through `os._exit()`, as `multiprocessing` workers started with `fork` do, or while a
daemon thread still uses it. The file is then left needing a repair. The next writer to open it
repairs it, and until then `redb.open_read_only()` raises `redb.RepairAborted`.

## Features
* Tables support `table[key]`, `table[key] = value`, `del table[key]`, `key in table` and
  `len(table)` as a `dict` does, along with `get()`, `range()`, `first()`, `last()`,
  `pop_first()`, `pop_last()` and `retain()`. Iterating a table, or a `range()` of it, yields
  `(key, value)` pairs in key order
* Fully ACID-compliant transactions. Leaving a `with db.begin_write()` block commits the
  transaction, or aborts it if an exception was raised
* Concurrent readers and a writer, in one process or in several
* Savepoints and rollbacks
* Compaction and integrity checking

## Types
Keys and values are typed. The types are `redb.bytes`, `redb.str`, `redb.bool`, `redb.u64`,
`redb.i64`, `redb.u128`, `redb.i128`, and `redb.f64`. `f64` can only be used for values, since
floats have no total order. The Python builtins `bytes`, `str`, `bool` and `float` can be used in
place of the redb type of the same name. Keys are sorted in redb's order: integers numerically,
`bytes` and `str` lexicographically.

## Multiple processes
One process can open a database for writing with `redb.open()` while any number of other
processes open it for reading with `redb.open_read_only()`. Each read transaction sees the latest
commit.

To allow more than one writer process, open the database with `concurrency=redb.MultiWriter` in
every process. Write transactions run one at a time, across processes as within one:
`begin_write()` waits while another is in progress. Waiting writers are not served in order, so a
writer that begins again as soon as it commits can keep the others waiting.

```python
import redb

db = redb.open("my_db.redb", concurrency=redb.MultiWriter)
```

A database, and the transactions and tables opened from it, belong to the process that opened
it. A forked child, such as a `multiprocessing` worker started with `fork`, must open the database
itself: using an inherited one raises `redb.UsedAfterFork`.

## Development
The bindings are built with [maturin](https://www.maturin.rs) and developed with
[uv](https://docs.astral.sh/uv/), which installs maturin and the test dependencies itself.
`just install_py` builds the bindings into `crates/redb-python/.venv`, `just test_py` runs the
tests, and `just fuzz_py` runs the Hypothesis-based fuzzer.

## License

Licensed under either of the Apache License, Version 2.0 or the MIT License, at your option.

