walpack 0.2.0
walpack: ^0.2.0 copied to clipboard
High-performance, crash-safe, single-file binary container with chunked storage and append-only write-ahead log (WAL) for Dart.
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:walpack/walpack.dart';
Future<void> main() async {
final dir = Directory.systemTemp.createTempSync('walpack_basic_');
final path = '${dir.path}/design.wpk';
try {
final pack = await WalPack.openFile(
path,
options: const WalPackOptions(appId: 'PICO'),
);
// 1. Atomic transaction mixing typed chunks, dedup blobs, and WAL ops.
final repeatedLayout = Uint8List.fromList(
utf8.encode('Frame(x=0,y=0,w=120,h=40)\n' * 64),
);
await pack.commit((tx) {
tx
..putChunkWith('page/home', (w) {
w
..writeVarString('Home Screen')
..writeVarUint(42)
..writeFloat64(1440);
})
..putChunk('page/layout', repeatedLayout, codecId: codecIdLz4)
..putBlobIfAbsent(
'asset/logo',
Uint8List.fromList([0x89, 0x50, 0x4E, 0x47]),
)
..appendOpWith(
opType: 1,
encode: (w) => w
..writeVarString('rename')
..writeVarString('Home Screen'),
);
});
// 2. Typed zero-copy read via pro_binary.
final home = await pack.readChunkWith('page/home', (r) {
final title = r.readVarString();
final nodeCount = r.readVarUint();
final width = r.readFloat64();
return (title: title, nodeCount: nodeCount, width: width);
});
final meta = pack.chunkMeta('page/layout')!;
stdout
..writeln('Opened $path (gen=${pack.generation}, seq=${pack.lastSeq})')
..writeln('Decoded home: $home')
..writeln(
'LZ4 page/layout: raw=${meta.rawLength}B -> '
'stored=${meta.storedLength}B',
);
// 3. Copy-on-Write compaction and full verification.
final compacted = await pack.compact();
final report = await pack.verifyIntegrity();
stdout.writeln(
'Compacted gen=${compacted.generation}, '
'totalBytes=${compacted.totalBytes}, valid=${report.isValid}',
);
await pack.close();
} finally {
if (dir.existsSync()) {
dir.deleteSync(recursive: true);
}
}
}