< 返回版块

shenjinti 发表于 2026-06-15 14:02

Tags:key-value,LSM,IndexedDB

FlowDB 介绍:一个纯 Rust 的嵌入式 LSM 引擎与 JSON 文档数据库

产品能力:一个引擎,两种用法

FlowDB 提供两层 API:

底层:LSM-Tree Key-Value 引擎

use flowdb::{Engine, Config};

let engine = Engine::open(Config::new("/tmp/mydb"))?;
engine.put(b"key", b"value")?;
let val = engine.get(b"key")?;         // Option<Vec<u8>>
engine.delete(b"key")?;

支持前缀扫描、范围扫描、批量写入、迭代器。这是一个完整的 LSM 引擎,但功能定位很克制——不像 RocksDB 那样有数百个配置项。

上层:JsonDB 文档层

JsonDB 是构建在 LSM 引擎之上的 JSON 文档数据库,兼容 IndexedDB API 范式(object store + index):

use flowdb::{Engine, Config, JsonDB, Transaction, QueryBuilder};
use serde_json::json;

let engine = Engine::open(Config::new("/tmp/myjsondb"))?;
let jsondb = JsonDB::open(engine)?;

// 创建 store 和索引
let store = jsondb.create_object_store("users", "id")?;
jsondb.create_index("users", "email", true)?;  // unique index

// 写入文档
jsondb.put("users", json!({"id": "u1", "name": "Alice", "email": "alice@x.com", "age": 30}))?;

// 按主键查询
let doc = jsondb.get::<Value>("users", "u1")?;

// 按索引查询
let doc = jsondb.get_by_index::<Value>("users", "email", "alice@x.com")?;

// 范围扫描 + 排序
let results = jsondb.range_by_index::<Value>(
    "users", "age", 20..=40, SortDir::Asc, None
)?;

// 声明式查询 (自动选索引)
let results = QueryBuilder::new("users")
    .where_eq("email", "bob@x.com")
    .collect(&jsondb)?;

核心特性:

  • 多索引支持(唯一/非唯一,单字段/复合)
  • 嵌套字段查询("address.city" 点号路径)
  • 事务支持(OCC + MVCC,批量原子提交,自动回滚)
  • 自动递增主键
  • 范围扫描、分页、排序

与 RocksDB 的对比

M 系列芯片, 100K records, 128B value:

操作 FlowDB RocksDB 倍数
顺序写 4.5M ops/s 3.1M ops/s 1.42x
8线程并发写 9.4M ops/s 4.7M ops/s 2.02x
点查询 6.0M ops/s 549K ops/s 10.95x
前缀扫描 ~200条 72K ops/s 11K ops/s 6.39x
全表扫描 200K条 65 ops/s 40 ops/s 1.63x

JsonDB 文档层性能

操作 吞吐
单文档写入 ~121 docs/s
批量写入 (100/批) ~7,057 docs/s
主键点查 ~244,741 ops/s
索引等值查找 ~9,402 queries/s

(写入瓶颈在 fsync,可用 SyncMode::IntervalMs 或批量提升)

为什么点查比 RocksDB 快 11 倍?

  1. Vec memtable — 读路径上活跃数据无 BTreeMap 遍历开销
  2. Key 级 bloom — 命中率远高于 (key, ts) 级
  3. 无 CGo/CXX 跨语言开销 — 纯 Rust 调用,无需 FFI
  4. 配置极简 — 不做 RocksDB 那种上百项调优,默认就快
  5. BlockMetaIndex 二维索引 — 按 key 和按时间双索引加速

GitHub: https://github.com/restsend/flowdb crates.io: flowdb

欢迎 issue / PR / 讨论。


Ext Link: https://github.com/restsend/flowdb

评论区

写评论
abbycin 2026-06-15 16:55

100K 的 kv 和 128B 的 value 就下结论是否有些草率了?

1 共 1 条评论, 1 页