MathDB
基于 Polars/Pandas 的持久化内存数据库,支持 WAL 和快照恢复。
特性
- 🚀 高性能: 直接从内存中使用 Polars DataFrame 读取
- 💾 持久化: WAL 段 + 周期性快照用于崩溃恢复
- ⏱️ 近零丢失: 最大数据丢失窗口 ≤ 3 秒
- 🔒 并发控制: 单写入者 + 多读者 (RWLock)
- 🔄 完整 CRUD: 支持 Insert、Update、Delete、Upsert 操作
安装
pip install mathdb
快速开始
from mathdb import Table, TableSchema, ColumnDef
# 定义表结构
schema = TableSchema(
name="users",
columns=[
ColumnDef("id", "Int64", primary_key=True),
ColumnDef("name", "Utf8"),
ColumnDef("age", "Int32"),
]
)
# 创建持久化表
with Table(schema, data_dir="./data") as table:
# CRUD 操作
table.insert({"id": 1, "name": "Alice", "age": 30})
table.update(pk=1, row={"id": 1, "name": "Alice", "age": 31})
table.upsert({"id": 2, "name": "Bob", "age": 25})
table.delete(pk=1)
# 使用原生 Polars API 查询
import polars as pl
df = table.to_dataframe()
result = df.filter(pl.col("age") > 20).group_by("name").count()
# 重新打开时自动恢复数据
with Table(schema, data_dir="./data") as table:
print(f"恢复了 {table.count()} 条记录")
配置选项
# 自定义刷盘间隔(默认 3 秒)
table = Table(schema, data_dir="./data", flush_interval=1.0)
# 禁用自动刷盘(手动控制)
table = Table(schema, data_dir="./data", flush_interval=0)
table.insert({"id": 1, "name": "Alice", "age": 30})
table.flush() # 手动刷盘
架构
┌─────────────────────────────────────────────────────────────┐
│ Table │
│ ┌─────────────┐ ┌──────────────┐ ┌───────────────────┐ │
│ │ Reader │ │ WriterActor │ │ Flusher/Snapshotter│ │
│ └──────┬──────┘ └──────┬───────┘ └─────────┬─────────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ CurrentTable (Polars DataFrame) │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌───────────────────────────────┐
│ 磁盘: WAL + 快照 │
└───────────────────────────────┘
开发
# 安装开发依赖
pip install -e ".[dev]"
# 运行测试
pytest
# 运行测试并生成覆盖率报告
pytest --cov=mathdb --cov-report=html
许可证
MIT
Metadata
Release files for mathdb 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mathdb-0.1.1.tar.gz | 35.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mathdb-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 58.9 kB
Release files / mathdb-0.1.1.tar.gz
| Download URL | mathdb-0.1.1.tar.gz |
|---|---|
| Size | 35.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5be3cda0fbcc342bb00f850cf406a7cd5ccd9cd807a039dc4d82aef78db4be79
|
|
BLAKE2b-256 checksum How to use checksums |
3d39b8953c82e08da7b04b8ecc23fb01a5db1e66673b20bc44453d8bb007b3b0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.9
|
Release files / mathdb-0.1.1-py3-none-any.whl
| Download URL | mathdb-0.1.1-py3-none-any.whl |
|---|---|
| Size | 23.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
81b3220f3e2152ca4b49c65537894d405d8d728868172e36a38d379c87f712d0
|
|
BLAKE2b-256 checksum How to use checksums |
171249f9ddb55577d8da3f281d904f9e7af25809eb33e7682960a3254d588785
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.9
|
Security for mathdb 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.