月壤:纯 MoonBit LSM-tree 嵌入式 KV 存储(WAL + 跳表 MemTable + SSTable + 合并压缩)。
名称取自月球表面覆盖的松散岩屑层「月壤」(lunar regolith)——写入如 月壤般层层堆积,压缩则如压实成岩。
moon add QYF795/regolith # 依赖方式使用(mooncakes.io 已发布)fn main raise {
let db = @QYF795/regolith.Db::open("data")
db.put(b"key", b"value")
println(show(db.get(b"key"))) // Some(Some(b"value"))
db.flush() // 持久化确认(进程级;掉电级见 docs/durability.md)
}
// Option[Option[Bytes]] 的三态渲染(core 0.10.15 起 Option 的 to_string
// 已弃用,用 match 渲染即可):
fn show(r : Option[Option[Bytes]]) -> String {
match r {
None => "None"
Some(None) => "Some(None)"
Some(Some(v)) => "Some(Some(\{v.to_string()}))"
}
}moon test --target js
moon test --target wasm-gcmoon run --target js src/cli -- set hello regolith
moon run --target js src/cli -- get hello # regolith
moon run --target js src/cli -- batch put a 1 put b 2 del c # 多键一次原子提交
moon run --target js src/cli -- scan
moon run --target js src/cli -- scan_prefix hel # hello=regolith
moon run --target js src/cli -- scan_range h i # hello=regolith
moon run --target js src/cli -- statsfn main raise {
let db = @QYF795/regolith.Db::open("demo-data") // 默认选项
db.put(b"key", b"value")
println(show(db.get(b"key"))) // Some(Some(b"value"))
// 快照:冻结当前状态;后续写入不影响快照上的读写
let snap = db.snapshot()
// 多键一次原子提交(全有或全无)
db.write(@QYF795/regolith.WriteBatch::new().put(b"a", b"1").delete(b"key"))
db.flush() // 主动持久化(进程级)
println(show(snap.get(b"key"))) // 仍是 Some(Some(b"value"))(冻结视图)
println(show(db.get(b"key"))) // Some(None)(活库已推进)
snap.close() // 释放快照钉住的表文件
}let opts = @QYF795/regolith.Options::default()
.with_memtable_limit(4 << 20) // memtable 字节上限,超限自动 flush
.with_compaction_trigger(4) // L0 表数触发 compact 的阈值
.with_l2_size_trigger(4 << 20) // L1 字节触发 compact_l2 的阈值
.with_l2_size_ratio(4) // L1/L2 大小比(写放大控制)
.with_table_size_limit(2 << 20) // 压缩输出单表字节上限,超出按键序拆分
.with_restart_interval(16) // v3 键压缩块内重启点间隔(条目数)src/ 引擎实现(WAL / MemTable / SSTable / Db / 合并迭代器 / CLI / demo)
tools/ 开发工具(sim_crash.py:崩溃一致性全状态模拟器)
docs/ [磁盘格式](docs/format.md) / [验收清单](docs/acceptance.md) / [设计文档](docs/design.md) / [持久性契约](docs/durability.md)pub(all) struct Bloom {
m_bits : Int
bits : Bytes
}pub(all) struct Db {
storage : Storage
opts : Options
memtable : MemTable
wal : Wal
next_seq : Ref[UInt64]
l0_counter : Ref[Int]
l0_names : Ref[Array[String]]
l0_readers : Ref[Array[SstReader]]
l1_tables : Ref[Array[Table]]
l2_tables : Ref[Array[Table]]
table_gen : Ref[Int]
l1_bytes : Ref[Int]
l2_bytes : Ref[Int]
snapshots : Ref[Array[Snapshot]]
next_snap_id : Ref[UInt64]
pending : Ref[Array[String]]
}pub(all) struct FailpointStorage {
inner : MemoryStorage
armed_kind : Ref[FaultKind?]
armed_name : Ref[String?]
fired : Ref[Bool]
}fn FailpointStorage::arm_on(self : FailpointStorage, kind : FaultKind, name_prefix : String) -> Unitpub(all) struct FileStorage {
dir : String
}pub(all) struct Options {
memtable_limit : Int
block_size : Int
l0_compaction_trigger : Int
l2_size_trigger : Int
l2_size_ratio : Int
table_size_limit : Int
restart_interval : Int
}pub(all) struct Stats {
memtable_bytes : Int
wal_bytes : Int
l0_files : Int
l1_files : Int
l1_bytes : Int
l2_files : Int
l2_bytes : Int
}pub(all) struct Table {
name : String
reader : SstReader
seq : UInt64
min_key : Bytes
max_key : Bytes
bytes : Int
}fn crc32(data : BytesView) -> UIntfn crc32_init(data : BytesView, init : UInt) -> UIntfn is_future_format(bytes : Bytes) -> BoolInstall
Download zip月壤:纯 MoonBit LSM-tree 嵌入式 KV 存储(WAL + 跳表 MemTable + SSTable + 合并压缩)。