-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ts
More file actions
279 lines (229 loc) · 10.6 KB
/
Copy pathmain.ts
File metadata and controls
279 lines (229 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* Example 11: Delta Strategies
*
* Demonstrates storage optimization using the DeltaApi and
* low-level delta compression utilities.
*
* Topics covered:
* - Understanding blob delta compression
* - Using DeltaApi for storage optimization
* - Batch operations for atomic repacking
* - Delta chain inspection and management
* - Low-level delta computation with createDeltaRanges/applyDelta
*
* Run with: pnpm start
*/
import {
createMemoryHistoryWithOperations,
FileMode,
type HistoryWithOperations,
} from "@statewalker/vcs-core";
const encoder = new TextEncoder();
const decoder = new TextDecoder();
// ============================================================
// Setup: Create repository with similar content
// ============================================================
console.log("=== Setup: Create Repository with Similar Content ===\n");
const history: HistoryWithOperations = createMemoryHistoryWithOperations();
await history.initialize();
await history.refs.setSymbolic("HEAD", "refs/heads/main");
const now = Date.now() / 1000;
// Create multiple versions of the same file (ideal for delta compression)
const versions = [
"# Project Documentation\n\nThis is version 1 of the documentation.\nIt contains basic project information.\n\n## Getting Started\n\nInstall dependencies and run the project.\n",
"# Project Documentation\n\nThis is version 2 of the documentation.\nIt contains updated project information.\n\n## Getting Started\n\nInstall dependencies and run the project.\n\n## Configuration\n\nSet up your config.json with the required fields.\n",
"# Project Documentation\n\nThis is version 3 of the documentation.\nIt contains comprehensive project information.\n\n## Getting Started\n\nInstall dependencies and run the project.\n\n## Configuration\n\nSet up your config.json with the required fields.\n\n## API Reference\n\nSee the API docs for endpoint details.\n",
];
const blobIds: string[] = [];
let parentCommitId = "";
for (let i = 0; i < versions.length; i++) {
const blobId = await history.blobs.store([encoder.encode(versions[i])]);
blobIds.push(blobId);
const treeId = await history.trees.store([
{ mode: FileMode.REGULAR_FILE, name: "README.md", id: blobId },
]);
const commitId = await history.commits.store({
tree: treeId,
parents: parentCommitId !== "" ? [parentCommitId] : [],
author: {
name: "Dev",
email: "dev@example.com",
timestamp: now + i * 3600,
tzOffset: "+0000",
},
committer: {
name: "Dev",
email: "dev@example.com",
timestamp: now + i * 3600,
tzOffset: "+0000",
},
message: `Version ${i + 1}`,
});
parentCommitId = commitId;
}
await history.refs.set("refs/heads/main", parentCommitId);
console.log(" Created 3 commits with incrementally evolving README.md");
for (let i = 0; i < blobIds.length; i++) {
const size = await history.blobs.size(blobIds[i]);
console.log(` Version ${i + 1}: blob ${blobIds[i].slice(0, 7)} (${size} bytes)`);
}
// ============================================================
// Step 1: Understanding DeltaApi
// ============================================================
console.log("\n=== Step 1: Understanding DeltaApi ===\n");
console.log(" The DeltaApi provides blob delta operations for storage optimization.");
console.log(" Only blobs support delta compression in internal storage.");
console.log(" Trees and commits are always stored as-is for fast access.\n");
// Check backend capabilities
console.log(` Backend capabilities:`);
console.log(` nativeBlobDeltas: ${history.capabilities.nativeBlobDeltas}`);
console.log(` randomAccess: ${history.capabilities.randomAccess}`);
console.log(` atomicBatch: ${history.capabilities.atomicBatch}`);
console.log(` nativeGitFormat: ${history.capabilities.nativeGitFormat}`);
// ============================================================
// Step 2: Check Delta State
// ============================================================
console.log("\n=== Step 2: Check Delta State ===\n");
// Check if any blobs are currently stored as deltas
for (const blobId of blobIds) {
const isDelta = await history.delta.isDelta(blobId);
console.log(` ${blobId.slice(0, 7)} isDelta: ${isDelta}`);
}
// Enumerate all delta relationships
let deltaCount = 0;
for await (const rel of history.delta.listDeltas()) {
console.log(
` Delta: ${rel.targetId.slice(0, 7)} -> ${rel.baseId.slice(0, 7)} ` +
`(depth=${rel.depth}, ratio=${rel.ratio.toFixed(2)})`,
);
deltaCount++;
}
console.log(` Total delta relationships: ${deltaCount}`);
// ============================================================
// Step 3: Batch Operations
// ============================================================
console.log("\n=== Step 3: Batch Operations ===\n");
console.log(" Batch operations allow atomic delta changes.");
console.log(" All changes are applied together when endBatch() is called.");
console.log(" If cancelBatch() is called, all changes are discarded.\n");
// Start a batch
history.delta.startBatch();
console.log(" Batch started.");
// In a real scenario, you would use findBlobDelta and deltifyBlob:
//
// const candidates = async function*() {
// yield blobIds[0]; // try v1 as base
// };
// const result = await history.delta.blobs.findBlobDelta(blobIds[1], candidates());
// if (result) {
// await history.delta.blobs.deltifyBlob(blobIds[1], result.baseId, result.delta);
// }
//
// For this example, we'll just demonstrate the batch pattern:
console.log(" (In production, deltify blobs here using findBlobDelta + deltifyBlob)");
// Cancel the batch since this is a demo
history.delta.cancelBatch();
console.log(" Batch cancelled (demo - no actual deltas applied).\n");
console.log(" Batch pattern for GC/repacking:");
console.log(" delta.startBatch()");
console.log(" for each blob: findBlobDelta + deltifyBlob");
console.log(" delta.endBatch() // atomic commit");
console.log(" (or delta.cancelBatch() on error)");
// ============================================================
// Step 4: Delta Chain Inspection
// ============================================================
console.log("\n=== Step 4: Delta Chain Inspection ===\n");
console.log(" getDeltaChain() reveals the chain of base objects needed");
console.log(" to reconstruct a blob. Deeper chains trade space for read latency.\n");
for (const blobId of blobIds) {
const chain = await history.delta.getDeltaChain(blobId);
if (chain) {
console.log(` ${blobId.slice(0, 7)}: depth=${chain.depth}, totalSize=${chain.totalSize}`);
console.log(` baseIds: ${chain.baseIds.map((id) => id.slice(0, 7)).join(" -> ")}`);
} else {
console.log(` ${blobId.slice(0, 7)}: stored as full object (no delta chain)`);
}
}
console.log("\n getDependents() shows which blobs depend on a given base.");
console.log(" Important for safe deletion: a base cannot be deleted while");
console.log(" dependents exist.\n");
for (const blobId of blobIds) {
const dependents: string[] = [];
for await (const depId of history.delta.getDependents(blobId)) {
dependents.push(depId.slice(0, 7));
}
console.log(
` ${blobId.slice(0, 7)} dependents: ${dependents.length > 0 ? dependents.join(", ") : "none"}`,
);
}
// ============================================================
// Step 5: Low-Level Delta Utilities
// ============================================================
console.log("\n=== Step 5: Low-Level Delta Utilities ===\n");
console.log(" The vcs-utils package provides raw delta computation.");
console.log(" These operate on byte arrays, independent of storage.\n");
const { createDeltaRanges, createDelta, applyDelta } = await import("@statewalker/vcs-utils/diff");
const baseContent = encoder.encode(versions[0]);
const targetContent = encoder.encode(versions[1]);
// Compute delta ranges (what to copy from source, what to insert from target)
// DeltaRange uses .from ("source" = copy, "target" = insert) and .len
const ranges = [...createDeltaRanges(baseContent, targetContent)];
let copyCount = 0;
let insertCount = 0;
let copyBytes = 0;
let insertBytes = 0;
for (const range of ranges) {
if (range.from === "source") {
copyCount++;
copyBytes += range.len;
} else {
insertCount++;
insertBytes += range.len;
}
}
console.log(` Base size: ${baseContent.length} bytes`);
console.log(` Target size: ${targetContent.length} bytes`);
console.log(` Delta ranges: ${ranges.length} total`);
console.log(` Copy: ${copyCount} ranges (${copyBytes} bytes from base)`);
console.log(` Insert: ${insertCount} ranges (${insertBytes} bytes literal)\n`);
// Create delta instructions (Delta objects: start, copy, insert, finish)
const deltaInstructions = [...createDelta(baseContent, targetContent, ranges)];
// Count instruction types for display
let deltaDataSize = 0;
for (const instr of deltaInstructions) {
if (instr.type === "copy") deltaDataSize += instr.len;
if (instr.type === "insert") deltaDataSize += instr.data.length;
}
console.log(` Delta instructions: ${deltaInstructions.length}`);
console.log(` Delta data size: ${deltaDataSize} bytes`);
const savings = targetContent.length - deltaDataSize;
console.log(` Savings: ${savings} bytes`);
// Apply delta instructions to reconstruct the target
const reconstructed = [...applyDelta(baseContent, deltaInstructions)];
const reconstructedContent = new Uint8Array(
reconstructed.reduce((sum, chunk) => sum + chunk.length, 0),
);
let offset = 0;
for (const chunk of reconstructed) {
reconstructedContent.set(chunk, offset);
offset += chunk.length;
}
const matches = decoder.decode(reconstructedContent) === decoder.decode(targetContent);
console.log(` Reconstruction: ${matches ? "matches original" : "MISMATCH!"}`);
// ============================================================
// Summary
// ============================================================
console.log("\n=== Summary: When to Use Delta Compression ===\n");
console.log(" Use DeltaApi when:");
console.log(" - Running garbage collection (GC)");
console.log(" - Optimizing storage after many commits");
console.log(" - Repacking objects for better compression");
console.log(" - Analyzing storage efficiency\n");
console.log(" Use low-level delta utils when:");
console.log(" - Building custom pack file writers");
console.log(" - Implementing wire-level transport");
console.log(" - Computing diffs between arbitrary byte buffers\n");
console.log(" Key insight: Only blobs have delta support in internal storage.");
console.log(" Pack serialization (wire format) can still use deltas for all types.");
await history.close();
console.log("\nExample completed successfully!");