moonseekzst

    Seekable Zstandard archives and byte-range extraction in MoonBit

    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    14 hours ago
    Downloads
    6

    #moonseekzst

    Seekable Zstandard 文件读写和原文字节区间提取库,面向大型日志与二进制归档。打开归档时读取尾部与索引,随后按原文偏移定位命中的独立压缩帧。核心库与帧编解码均为 MoonBit。原生命令行的本地文件适配使用标准文件 I/O。

    #用法

    发布后可在另一模块运行 moon add Sakuraqwq1/moonseekzst@0.1.0。在源码目录可直接构建本模块,依赖为 gaato/zstd@0.6.0。

    在调用方的 MoonBit 包中将 Sakuraqwq1/moonseekzst 导入为 @seek。

    fn demo(input : Bytes) -> Bytes raise @seek.ArchiveError {
    let packed = @seek.compress(input, frame_size=1024, checksums=true)
    let archive = @seek.Archive::open(@seek.Source::from_bytes(packed))
    archive.read_range(0L, input.length().min(32))
    }

    • compress 写出内存归档。write_archive 从 Source 按帧读取,逐帧写给调用方提供的 sink,末尾写 Seek Table。source 和 sink 由调用方管理。
    • Source::new 接收 Int64 文件大小和精确读取回调。一次操作内,数据源应保持内容稳定。
    • Archive::open 支持有或无校验和的索引。frame_count、plain_size、has_checksums 和 frame 可查询索引。
    • read_range 的起点是零基 Int64 原文偏移,长度用 Int32。读取时检查单帧边界、输出上限、实际解压长度和索引校验和。样机的 Archive::extract 仍可传入自定义解码器。
    • 写入默认每帧 1 MiB,可指定 1 字节至 32 MiB。索引默认最多 100 万项,读取默认每个命中帧的压缩和解压长度各不超过 64 MiB。内存归档与每次提取受 MoonBit Bytes 长度限制。

    #本地文件 CLI

    moon run --target native cmd/moonseekzst -- pack app.log app.zst 1024 moon run --target native cmd/moonseekzst -- info app.zst moon run --target native cmd/moonseekzst -- extract app.zst 1001 91 slice.bin --trace-reads

    pack 默认写索引校验和,末尾可传 --no-checksum。CLI 使用 64 位文件寻址,--trace-reads 输出压缩偏移和实际读取长度。输出路径需可新建。输入和输出文件名支持空格,Windows 复跑覆盖了中文文件名。

    #干净环境验收

    需要 MoonBit、Python 3.9+ 和 Zstd CLI。实测版本见 DEPENDENCIES.md。使用验证包时,在模块目录运行:

    $support = (Resolve-Path ../test-support).Path python tools/acceptance.py --support $support --dependency-dir "$support/dependency-recovery" --work-dir "$env:TEMP/moonseekzst-acceptance"

    离线流程核对三个 Mooncakes 原包的 SHA-256,并建立一次性的 MoonBit 工作区。如果没有现成的参考工具,脚本用 PATH 上的 GCC 从随包提供的 Zstd v1.5.7 源码构建。也可用 --moon、--zstd、--reference-tool 指定工具位置。有网络的环境可省去 --dependency-dir,按模块声明解析依赖。

    验收读取既有样本与清单,把构建、命令及结果写入 --work-dir 下独立目录。查阅其中的 acceptance-results.json 获取完整命令、字节对比哈希、负例失败阶段和真实文件读取记录。

    #v0.1.0 实测 · 2026-12-01

    验证nativewasm-gc
    核心单测13/1313/13
    官方归档 → 正式库7/7 切片7/7 切片
    正式库 → 官方 Seekable 跨帧提取91 字节一致91 字节一致
    正式库 → 普通 zstd -dc5,767 字节一致5,767 字节一致
    损坏归档6 项打开拒绝、2 项读取拒绝6 项打开拒绝、2 项读取拒绝

    读取负例都先成功打开,再请求原文 [1024, 2048)。第 7 项报告索引声明 1,025 字节,实际解压出 1,024 字节。第 8 项报告第二帧的索引校验和失败。

    原生 CLI 对三帧归档提取 [1040, 1072) 时,实际文件读取为 (1797, 9)、(1765, 32)、(592, 592)。三个元组依次是尾部、表头与表项、命中的第二帧。跨帧提取 [997, 1074) 只多读一个命中帧。每组结果均与原文逐字节相同。

    ArchiveError

    pub suberror ArchiveError {
    InvalidIndex(String)
    InvalidRange
    InvalidFrame(Int, String)
    ChecksumMismatch(Int)
    LimitExceeded(String)
    SourceError(String)
    SinkError(String)
    }

    ArchiveError::describe

    fn ArchiveError::describe(self : ArchiveError) -> String

    Describe an archive error including the affected frame when available.

    ArchiveError::sink_error

    fn ArchiveError::sink_error(detail : String) -> ArchiveError

    Report a failed write from a streaming archive sink.

    Archive

    pub struct Archive {
    // private fields
    }

    Archive::extract

    fn Archive::extract(self : Archive, offset : Int64, length : Int, decode : (BytesView, Int) -> Bytes raise, max_frame_bytes? : Int) -> Bytes raise ArchiveError

    The decoder must consume exactly one frame and honor the given output bound.

    Archive::frame

    fn Archive::frame(self : Archive, index : Int) -> Frame?

    Archive::frame_count

    fn Archive::frame_count(self : Archive) -> Int

    Archive::has_checksums

    fn Archive::has_checksums(self : Archive) -> Bool

    Archive::open

    fn Archive::open(source : Source, max_frames? : Int) -> Archive raise ArchiveError

    Read the footer and the preceding table bytes. No data frames are read.

    Archive::plain_size

    fn Archive::plain_size(self : Archive) -> Int64

    Archive::read_range

    fn Archive::read_range(self : Archive, offset : Int64, length : Int, max_frame_bytes? : Int) -> Bytes raise ArchiveError

    Extract [offset, offset + length) with the built-in Zstandard decoder.

    Entry

    pub(all) struct Entry {
    compressed_size : UInt
    plain_size : UInt
    checksum : UInt?
    }

    Frame

    pub struct Frame {
    compressed_offset : Int64
    plain_offset : Int64
    entry : Entry
    }

    Source

    pub struct Source {
    length : Int64
    read_at : (Int64, Int) -> Bytes raise
    }

    The callback reads exactly the requested bytes from an immutable snapshot.

    Source::from_bytes

    fn Source::from_bytes(bytes : Bytes) -> Source

    Source::new

    fn Source::new(length : Int64, read_at : (Int64, Int) -> Bytes raise) -> Source

    compress

    fn compress(input : Bytes, frame_size? : Int, checksums? : Bool) -> Bytes raise ArchiveError

    Write a complete Seekable Zstd archive to memory.

    encode_table

    fn encode_table(entries : Array[Entry], checksums? : Bool) -> Bytes raise ArchiveError

    pack_frames

    fn pack_frames(frames : Array[(Bytes, Bytes)], checksums? : Bool) -> Bytes raise ArchiveError

    Wrap frames supplied by a codec. Each pair is (compressed, uncompressed).

    write_archive

    fn write_archive(source : Source, write : (Bytes) -> Unit raise ArchiveError, frame_size? : Int, checksums? : Bool) -> Unit raise ArchiveError

    Encode one frame at a time and send the frames and final seek table to a sink. The caller is responsible for discarding an incomplete sink on failure.