Skip to main content

rocksdb-dedup

基于 RocksDB + 布隆过滤器的高性能持久化去重服务 Python 客户端。

特性

  • 原子 check_and_set 操作,保证去重准确性
  • 批量操作接口,高 QPS 场景下减少 RPC 开销
  • 多租户 namespace 隔离
  • 支持过期自动清理
  • 布隆过滤器自动扩容
  • 预生成 gRPC 代码,零编译步骤,开箱即用

安装

pip install rocksdb-dedup

或从源码安装:

pip install clients/python

注意:运行时只依赖 grpcio,不需要安装 grpcio-tools 或 protoc 编译器。

快速开始

from dedup_client import DedupClient
import time

# 连接服务
with DedupClient("localhost:50051") as client:
    ns = "spider_news"
    expire_at = int(time.time() * 1000) + 30 * 86400 * 1000  # 30天后过期

    # 核心接口:检查并写入
    result = client.check_and_set(
        namespace=ns,
        key=b"https://example.com/page/1",
        expire_at_ms=expire_at,
        metadata=b'{"depth": 3, "source": "seed"}',
    )

    if result == "inserted":
        print("新 URL,开始抓取")
    elif result == "duplicate":
        print("重复 URL,跳过")
    elif result == "expired_and_replaced":
        print("已过期,重新抓取")

API 列表

方法 说明
check_and_set(namespace, key, expire_at_ms, metadata) 原子检查并写入
batch_check_and_set(items) 批量检查并写入
exists(namespace, key) 仅检查是否存在
batch_exists(namespace, keys) 批量检查存在性
get(namespace, key) 获取详细信息
delete(namespace, key) 删除 key
batch_delete(namespace, keys) 批量删除
stats(namespace) 获取服务统计

批量操作示例

# 批量检查并写入
items = [
    ("spider_news", b"https://example.com/1", expire_at, None),
    ("spider_news", b"https://example.com/2", expire_at, b"meta2"),
    ("spider_news", b"https://example.com/3", expire_at, None),
]
results = client.batch_check_and_set(items)
# results: ["inserted", "inserted", "inserted"]

# 批量检查存在性
keys = [b"https://example.com/1", b"https://example.com/999"]
exists_list = client.batch_exists("spider_news", keys)
# exists_list: [True, False]

服务状态

stats = client.stats()
print(f"总 key 数: {stats['total_keys']:,}")
print(f"布隆容量: {stats['bloom_capacity']:,}")
print(f"误判率: {stats['bloom_fpp']:.4%}")
print(f"QPS: {stats['qps_estimate']:,}")

配置选项

超时设置

# 全局默认超时
client = DedupClient("localhost:50051", timeout=10.0)

# 单次调用也可以通过修改 client.timeout 临时调整
client.timeout = 30.0
client.stats()

开发

重新生成 gRPC 代码

仅在修改了 proto/dedup.proto 时需要:

pip install -e ".[dev]"
python -m grpc_tools.protoc -I../../proto --python_out=./src/dedup_client --grpc_python_out=./src/dedup_client ../../proto/dedup.proto

生成后需要手动将 dedup_pb2_grpc.py 中的绝对导入改为相对导入:

# 将 import dedup_pb2 as dedup__pb2
# 改为 from . import dedup_pb2 as dedup__pb2

运行测试

# 先启动服务端
cargo run -- serve --addr 0.0.0.0:50051 --db-path ./data/dedup

# 再运行集成测试
python -m pytest tests/

项目结构

clients/python/
├── pyproject.toml          # 打包配置
├── README.md               # 本文档
├── src/
│   └── dedup_client/       # Python 包
│       ├── __init__.py     # 导出 DedupClient
│       ├── client.py       # 客户端实现
│       ├── dedup_pb2.py    # 预生成的 proto 消息定义
│       ├── dedup_pb2_grpc.py  # 预生成的 gRPC 存根
│       └── py.typed        # PEP 561 类型标记
└── tests/                  # 集成测试
    ├── test_integration.py
    └── test_restart.py

License

MIT

Metadata

Release files for rocksdb-dedup 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rocksdb-dedup 0.1.0
File Size Uploaded Provenance
rocksdb_dedup-0.1.0.tar.gz 20.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for rocksdb-dedup 0.1.0
File Interpreter ABI Platform
rocksdb_dedup-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 35.8 kB

Release files / rocksdb_dedup-0.1.0.tar.gz

Download URL rocksdb_dedup-0.1.0.tar.gz
Size 20.3 kB
Tags Source
SHA-256 checksum
How to use checksums
df0b46469a581c972603c85d1b30125bb8b13fa157a8fc3a689077ff75c2dc04
BLAKE2b-256 checksum
How to use checksums
3f180dc5691a8d75af34c29ad7103c4d58f0dd1367852830029769d1ed3ef511
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release files / rocksdb_dedup-0.1.0-py3-none-any.whl

Download URL rocksdb_dedup-0.1.0-py3-none-any.whl
Size 15.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
57a8fe151bac5ada17df5aeab130eeb20a5bbdf16f2d472020aec72947cad5d2
BLAKE2b-256 checksum
How to use checksums
3e8a8bf1a789d61944c66d535064dac877cf0384ba6e891a862ef420eaa46f3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Security for rocksdb-dedup 0.1.0

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page