-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: differential update — use content defined chunking
- Loading branch information
Showing
16 changed files
with
355 additions
and
136 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
.idea/runConfigurations/Debug_differential_update_builder.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { createHash } from "crypto" | ||
import { read } from "fs-extra-p" | ||
import { FileChunks } from "builder-util-runtime/out/blockMapApi" | ||
import { Rabin } from "rabin-bindings" | ||
|
||
export class ContentDefinedChunker { | ||
async computeChunks(fd: number, start: number, end: number, name: string): Promise<FileChunks> { | ||
console.log(name) | ||
|
||
const fileSize = end - start | ||
const buffer = Buffer.allocUnsafe(Math.min(4 * 1024 * 1024, fileSize)) | ||
|
||
const rabin = Rabin() | ||
const avgBits = 12 | ||
const min = 8 * 1024 | ||
// see note in the nsis.ts about archive dict size | ||
const max = 32 * 1024 | ||
rabin.configure(avgBits, min, max) | ||
|
||
const checksums: Array<string> = [] | ||
const allSizes: Array<number> = [] | ||
|
||
let tailBufferData: Buffer | null = null | ||
let readOffset = start | ||
while (true) { | ||
const actualBufferSize = Math.min(end - readOffset, buffer.length) | ||
await read(fd, buffer, 0, actualBufferSize, readOffset) | ||
|
||
const dataBuffer: Buffer = buffer.length === actualBufferSize ? buffer : buffer.slice(0, actualBufferSize) | ||
const sizes: Array<number> = [] | ||
rabin.fingerprint([dataBuffer], sizes) | ||
|
||
let chunkStart = 0 | ||
for (const size of sizes) { | ||
allSizes.push(size) | ||
let chunkEnd = chunkStart + size | ||
|
||
const hash = createHash("sha256") | ||
if (tailBufferData !== null) { | ||
hash.update(tailBufferData) | ||
// if there is the tail data (already processed by rabin data), first size includes it | ||
chunkEnd -= tailBufferData.length | ||
tailBufferData = null | ||
} | ||
hash.update(dataBuffer.slice(chunkStart, chunkEnd)) | ||
checksums.push(hash.digest("base64")) | ||
chunkStart = chunkEnd | ||
} | ||
|
||
const tailSize = actualBufferSize - chunkStart | ||
if (tailSize !== 0) { | ||
if (tailBufferData !== null) { | ||
throw new Error(`Internal error (${name}): tailBufferData must be null`) | ||
} | ||
tailBufferData = dataBuffer.slice(chunkStart, chunkStart + tailSize) | ||
} | ||
|
||
readOffset += actualBufferSize | ||
if (readOffset >= end) { | ||
if (tailBufferData !== null) { | ||
allSizes.push(tailSize) | ||
checksums.push(computeChecksum(tailBufferData)) | ||
} | ||
break | ||
} | ||
else if (tailBufferData !== null) { | ||
// copy data | ||
tailBufferData = Buffer.from(tailBufferData) | ||
} | ||
} | ||
|
||
const totalSize = allSizes.reduce((accumulator, currentValue) => accumulator + currentValue) | ||
if (totalSize !== fileSize) { | ||
throw new Error(`Internal error (${name}): size mismatch: expected: ${fileSize}, got: ${totalSize}`) | ||
} | ||
|
||
return {checksums, sizes: allSizes} | ||
} | ||
} | ||
|
||
function computeChecksum(chunk: Buffer) { | ||
// node-base91 doesn't make a lot of sense - 29KB vs 30KB Because for base64 string value in the yml never escaped, but node-base91 often escaped (single quotes) and it adds extra 2 symbols. | ||
return createHash("sha256") | ||
.update(chunk) | ||
.digest("base64") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,18 @@ | ||
export const BLOCK_MAP_FILE_NAME = "_blockMap.yml" | ||
export const SIGNATURE_HEADER_SIZE = 12 /* signature + 2 bytes version + 4 bytes CRC */ + 20 | ||
|
||
export interface BlockMap { | ||
blockSize: number | ||
hashMethod: "sha256" | "md5" | ||
|
||
compressionLevel: 9 | 1 | ||
export interface FileChunks { | ||
checksums: Array<string> | ||
sizes: Array<number> | ||
} | ||
|
||
export interface BlockMap { | ||
version: "1" | "2" | ||
files: Array<BlockMapFile> | ||
} | ||
|
||
export interface BlockMapFile { | ||
export interface BlockMapFile extends FileChunks { | ||
name: string | ||
offset: number | ||
size: number | ||
|
||
// size of block 64K, last block size `size % (64 * 1024)` | ||
blocks: Array<string> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.