Skip to content
pro100andreyPublic

Repository files navigation

walpack

Crash-safe, single-file binary container library for Dart. Pure Dart · No bundled native libraries · VM, AOT, Flutter, Web & WASM

Built on pro_binary, walpack pairs an immutable chunked Base Snapshot (WLPK v1) with an append-only CRC32-C Write-Ahead Log (WAL) for sub-millisecond cold opens, lazy chunk reads, $O(1)$ saves, binary-search indexes, and multi-process live synchronization.


Why walpack?

  • One Portable Data File: Base Snapshot, sorted Base TOC + Base Index, and append-only WAL live inside a single portable .wpk file (multi-process coordination adds a tiny <file>.lock beside it by default; see TempDirLockStrategy to keep the directory single-file).
  • Instant Lazy Opens & Coalesced Reads: Cold open skips large blobs until requested; readChunks() and index queries coalesce nearby reads into contiguous transfers, backed by an optional readCacheBytes LRU cache.
  • $O(1)$ Crash-Safe Saves: Commits append one scatter-gather CRC32-C-sealed frame (crc32cCombine + automatic rollback on partial writes + Copy-on-Write compact() via OS rename()).
  • Native Sorted & Secondary Indexes: Built-in $O(\log N + K)$ keysWithPrefix(), keysInRange(), queryIntIndex(), and queryStringIndex() kept live across compacted snapshots and WAL deltas.
  • Fast Pure-Dart Codecs & Checksums: Slicing-by-8 CRC32-C (~1.12 GiB/s), 32-bit stripe XXH32 (~3.1 GiB/s), pure-Dart LZ4 block compression (~1.3 GiB/s decode), plus native ZLib on dart:io.
  • Multi-Process Ready: Live change streams (watch()), lock-generation tracking (sync()), Optimistic Concurrency Control with automatic retry (commitWithRetry), consistent backups (backupToFile, exportBytes), and pluggable backends (IoStorageBackend, MemoryStorageBackend, package:walpack/testing.dart).

Rules of thumb: keep all handles to a file in one isolate per process; don't mutate buffers passed to putChunk until the future completes (read results are read-only views); enable CompactionPolicy.auto() in apps (compaction is manual by default).


Install

dependencies:
  walpack: ^0.2.0

Quick Start

import 'dart:typed_data';
import 'package:walpack/walpack.dart';

Future<void> main() async {
  final pack = await WalPack.openFile(
    'project.wpk',
    options: const WalPackOptions(appId: 'PICO'),
  );

  await pack.commit((tx) {
    tx
      ..putChunkWith(
        'page/home',
        (w) => w
          ..writeVarString('Home Screen')
          ..writeVarUint(42),
        intIndexes: const {'order': 1},
      )
      ..putBlobIfAbsent('asset/logo', Uint8List.fromList([0x89, 0x50, 0x4E, 0x47]))
      ..appendOpWith(opType: 1, encode: (w) => w.writeVarString('init'));
  });

  final ordered = pack.queryIntIndex('order', min: 1, max: 10); // ['page/home']
  final home = await pack.readChunkWith(
    ordered.first,
    (r) => (title: r.readVarString(), nodes: r.readVarUint()),
  );
  print(home); // (title: Home Screen, nodes: 42)

  await pack.compact();
  await pack.close();
}

Performance Highlights (Dart AOT)

Measured on Linux x86_64 with dart compile exe — full results and tuning guide in doc/performance.md:

Benchmark Result Notes
Cold open 50.0 MiB disk pack 0.171 ms Reads header, TOC & WAL index only
WAL 64B op append (Memory / Disk NoLock) 2.06 µs / 4.50 µs ~222k–485k tx/s uncontended
Multi-handle sync() (up-to-date check) 7.08 µs Lock-generation check, no data reopen
Lazy disk chunk read (1 MiB, CRC32-C) 1,103 µs 907 MiB/s verified sequential transfer
CRC32-C / XXH32 (pure Dart, 1 MiB) 1,124 MiB/s / 3,098 MiB/s Slicing-by-8 + algebraic crc32cCombine
CoW compact() (25 MiB pass-through + swap) 32.02 ms 781 MiB/s without decompressing chunks

Documentation & Examples

Guide / Example Topics Covered
Getting Started & Recipes Installation, pro_binary chunks, secondary indexes, watch() / sync(), OCC retry, compaction, autosave, backups, Web
Architecture & Concurrency Snapshots + WAL, read-coalescing, IoMode, POSIX fcntl & Windows locks
Binary Format (WLPK v1) Superblock, sorted TOC & Base Index, WAL CommitFrame, WalPackLimits
Performance & Tuning Full AOT benchmarks, caching, codecs, locking & memory characteristics
Runnable Examples main.dart · multiprocess_example.dart · custom_backend_example.dart

License

MIT License — see LICENSE.

Releases

Contributors

Languages