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.1

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.1
File Size Uploaded Provenance
rocksdb_dedup-0.1.1.tar.gz 20.3 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 35.9 kB

Release files / rocksdb_dedup-0.1.1.tar.gz

Download URL rocksdb_dedup-0.1.1.tar.gz
Size 20.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4d75a1d7acdb14e7eeadb7be622b8443d5262f0a1905f1cfd46213d4efa2705e
BLAKE2b-256 checksum
How to use checksums
afcb1d0efe6bc609ce26db594024c11d489640201658dbdc0d155bd15cfd9450
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.1-py3-none-any.whl

Download URL rocksdb_dedup-0.1.1-py3-none-any.whl
Size 15.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0ca48ff9bcbdff65da7102085523805b0ea57d453dc3d522d3b8dc64ae2ef6d0
BLAKE2b-256 checksum
How to use checksums
e92da14fb385fb314a53ef5e3d04174178f81768fa09d0cba7bffb29547f629e
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.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.

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