Skip to main content

基于 Polars/Pandas 的持久化内存数据库,支持 WAL 和快照恢复

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mathdb-0.1.1.tar.gz (35.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mathdb-0.1.1-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file mathdb-0.1.1.tar.gz.

File metadata

  • Download URL: mathdb-0.1.1.tar.gz
  • Upload date:
  • Size: 35.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for mathdb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5be3cda0fbcc342bb00f850cf406a7cd5ccd9cd807a039dc4d82aef78db4be79
MD5 a901cac52d97b4b51a5b088f54980b90
BLAKE2b-256 3d39b8953c82e08da7b04b8ecc23fb01a5db1e66673b20bc44453d8bb007b3b0

See more details on using hashes here.

File details

Details for the file mathdb-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mathdb-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for mathdb-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 81b3220f3e2152ca4b49c65537894d405d8d728868172e36a38d379c87f712d0
MD5 6a9ad330f7e1624f74cf1ec9788408cd
BLAKE2b-256 171249f9ddb55577d8da3f281d904f9e7af25809eb33e7682960a3254d588785

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page