Heuristic:3641397194 wq Gpt6 Astra Backup Deduplication
| Knowledge Sources | |
|---|---|
| Domains | Optimization, File_System |
| Last Updated | 2026-10-26 14:00 GMT |
Overview
Store backup file content as content-addressed blobs indexed by SHA-256 hash, deduplicating identical files across transactions to minimize disk usage.
Description
The `SeatTransactions` class stores backup snapshots in a `.coldcoffee-history/` directory. Instead of storing each backed-up file inline in the snapshot JSON, it writes file content to `.coldcoffee-history/blobs/{sha256}` and stores only a `blob:` reference (e.g., `blob:abc123...`) in the snapshot. Before writing a blob, it checks if a file with that hash already exists; if so, the existing blob is reused. The `retain` method verifies that the hash of the buffer matches the expected digest before writing, and the `content` method verifies the hash again on read. This means that if the same file is backed up across multiple seat installs or re-installs, only one physical copy is stored.
Usage
Use this heuristic when implementing file backup/restore systems that may store the same file content multiple times. It is especially effective when users repeatedly install/uninstall seat packs that modify the same config files — the original content is backed up once and referenced by all subsequent snapshots.
The Insight (Rule of Thumb)
- Action: Store backup content as `blob:{sha256}` in `.coldcoffee-history/blobs/`; check existence before writing
- Value: SHA-256 hash as the blob filename and deduplication key
- Trade-off: Two extra hash computations per file (write + read verify); minor CPU cost for significant disk savings
- Compatibility: Works for both text and binary files; binary files stored as raw bytes
Reasoning
Seat pack installation modifies user config files. Uninstalling requires restoring the original content. Without deduplication, each install creates a new snapshot containing the full original file content. If a user installs 11 seats that all modify the same `settings.json`, that's 11 copies of the same file. Content-addressed storage means identical content is stored once. The SHA-256 hash serves as both the deduplication key and an integrity check — the `retain` method throws if `hash(buffer) !== digest`, and `content` throws if the stored blob's hash doesn't match the expected digest, detecting corruption.
Code Evidence
Blob storage and deduplication from `seat-transactions.js:266-278`:
blob(digest) {
if (!/^[a-f0-9]{64}$/.test(digest)) throw new Error('备份资源编号错误');
return within(this.requireRoot(), `.coldcoffee-history/blobs/${digest}`);
}
retain(buffer, digest) {
if (hash(buffer) !== digest) throw new Error('备份前资源校验失败');
const file = this.blob(digest);
const existing = read(file);
if (existing && hash(existing) !== digest) throw new Error('历史资源备份损坏;请保留目录检查');
if (!existing) atomic(file, buffer); // Only write if blob doesn't exist
return 'blob:' + digest;
}
Content retrieval with integrity check from `seat-transactions.js:280-286`:
content(value, digest) {
if (value === null) { if (digest !== null) throw new Error('备份空文件标记错误'); return null; }
if (typeof value !== 'string') throw new Error('备份文件编码错误');
const buffer = value.startsWith('blob:') ? read(this.blob(value.slice(5))) : Buffer.from(value, 'base64');
if (!buffer || hash(buffer) !== digest) throw new Error('备份完整性检查失败');
return buffer;
}
Hash function from `seat-transactions.js:8`:
const hash = (buffer) => crypto.createHash('sha256').update(buffer).digest('hex');