Skip to content

Commit 7d46f32

Browse files
committed
Merge branch 'dev'
2 parents ee2e416 + 2cedcb8 commit 7d46f32

File tree

3 files changed

+117
-16
lines changed

3 files changed

+117
-16
lines changed

index.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ const argv = UtilArgs.getArgv();
1111
const lhsQueue = UtilQueue.createQueue({ concurrency: argv.n })
1212
const lhsFilePattern = UtilPath.resolve(argv.l, '**/*')
1313
const lhsFilePaths = await UtilPath.glob(lhsFilePattern)
14-
const lhsFileSummary = UtilCompare.getFileSummary(argv.l, lhsFilePaths, lhsQueue)
14+
const lhsFileSummary = await UtilCompare.getFileSummary(argv.l, lhsFilePaths, lhsQueue)
1515
const rhsQueue = UtilQueue.createQueue({ concurrency: argv.m })
1616
const rhsFilePattern = UtilPath.resolve(argv.r, '**/*')
1717
const rhsFilePaths = await UtilPath.glob(rhsFilePattern)
18-
const rhsFileSummary = UtilCompare.getFileSummary(argv.r, rhsFilePaths, rhsQueue)
18+
const rhsFileSummary = await UtilCompare.getFileSummary(argv.r, rhsFilePaths, rhsQueue)
1919

2020
// save file summary
2121
const lhsFileSummaryOutputPath = UtilPath.resolve(argv.o, './file-summary-lhs.json')
@@ -26,12 +26,17 @@ const argv = UtilArgs.getArgv();
2626
// compare summary
2727
const compareSummary = UtilCompare.getCompareSummary(lhsFileSummary, rhsFileSummary)
2828

29+
// revalidate compare summary
30+
const compareRevalidateQueueLhs = UtilQueue.createQueue({ concurrency: argv.n })
31+
const compareRevalidateQueueRhs = UtilQueue.createQueue({ concurrency: argv.m })
32+
const compareRevalidateSummary = await UtilCompare.revalidateCompareSummary(compareSummary, compareRevalidateQueueLhs, compareRevalidateQueueRhs)
33+
2934
// save compare summary
3035
const compareSummaryOutputPath = UtilPath.resolve(argv.o, './compare-summary.json')
31-
UtilFs.writeJson(compareSummaryOutputPath, compareSummary)
36+
UtilFs.writeJson(compareSummaryOutputPath, compareRevalidateSummary)
3237

3338
// compare report
34-
const compareReport = UtilCompare.getCompareReport(compareSummary)
39+
const compareReport = UtilCompare.getCompareReport(compareRevalidateSummary)
3540

3641
// save compare report
3742
const compareReportOutputPath = UtilPath.resolve(argv.o, './compare-report.html')

util/compare.js

+60-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ import UtilPath from './path.js'
33
import UtilHash from './hash.js'
44
import UtilProgress from './progress.js'
55

6-
function getFileSummary (base = '', paths = [], queue = null) {
6+
async function getFileSummary (base = '', paths = [], queue = null) {
77
const countTotal = paths.length
88
const progress = UtilProgress.createProgressbar({ total: countTotal })
99
const summary = []
10+
const hashChunkConfig = UtilHash.getHashChunkPlots({ name: 'single plot', offset: 0, limit: 100 * 1024 })
1011

1112
_.each(paths, async path => {
1213
await queue.add(() => {
1314
const filename = UtilPath.relative(base, path)
14-
const md5 = UtilHash.getHash(path, { offset: 0, limit: 100 * 1024 })
15+
const md5 = UtilHash.getHashByChunks(path, {
16+
chunks: hashChunkConfig
17+
})
1518

1619
const pack = {
1720
path,
@@ -24,6 +27,44 @@ function getFileSummary (base = '', paths = [], queue = null) {
2427
})
2528
})
2629

30+
await queue.onIdle()
31+
32+
return summary
33+
}
34+
35+
async function rehashFileSummary (files = [], queue = null) {
36+
const countTotal = files.length
37+
const progress = UtilProgress.createProgressbar({ total: countTotal })
38+
const summary = []
39+
40+
_.each(files, async file => {
41+
await queue.add(() => {
42+
const path = file.path
43+
const chunkConfig = UtilHash.getHashChunkPlots({
44+
name: 'distribution plots',
45+
file: path,
46+
chunks: 10,
47+
offset: 0,
48+
limit: 10 * 1024
49+
})
50+
const filename = file.filename
51+
const md5 = UtilHash.getHashByChunks(path, {
52+
chunks: chunkConfig
53+
})
54+
55+
const pack = {
56+
path,
57+
filename,
58+
md5
59+
}
60+
61+
summary.push(pack)
62+
progress.tick()
63+
})
64+
})
65+
66+
await queue.onIdle()
67+
2768
return summary
2869
}
2970

@@ -62,6 +103,21 @@ function getCompareSummary (lhs = [], rhs = []) {
62103
return summary
63104
}
64105

106+
async function revalidateCompareSummary (summary = [], lhsQueue = null, rhsQueue = null) {
107+
const summaryUnsame = _.filter(summary, v => v.type !== 'same')
108+
const summarySame = _.filter(summary, v => v.type === 'same')
109+
110+
const lhs = _.chain(summarySame).map(v => v.lhs || []).flattenDeep().value()
111+
const lhsRehashed = await rehashFileSummary(lhs, lhsQueue)
112+
const rhs = _.chain(summarySame).map(v => v.rhs || []).flattenDeep().value()
113+
const rhsRehashed = await rehashFileSummary(rhs, rhsQueue)
114+
const summaryRehashed = getCompareSummary(lhsRehashed, rhsRehashed)
115+
116+
const summaryNew = _.concat([], summaryUnsame, summaryRehashed)
117+
118+
return summaryNew
119+
}
120+
65121
function getCompareReport (summary = []) {
66122
const template = `<!DOCTYPE html>
67123
<html>
@@ -190,6 +246,8 @@ function getCompareReport (summary = []) {
190246

191247
export default {
192248
getFileSummary,
249+
rehashFileSummary,
193250
getCompareSummary,
251+
revalidateCompareSummary,
194252
getCompareReport
195253
}

util/hash.js

+48-10
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,60 @@ import _ from 'lodash'
22
import MD5 from 'md5'
33
import FsExtra from 'fs-extra'
44

5-
function getHash (path = '', opts = {}) {
6-
const offset = +opts.offset
7-
const offsetVal = _.isFinite(offset) ? offset : 0
8-
const limit = +opts.limit
9-
const limitVal = _.isFinite(limit) ? limit : 102400
5+
function getHashByChunks (path = '', opts = {}) {
106
const fd = FsExtra.openSync(path, 'r')
117
const fstat = FsExtra.fstatSync(fd)
12-
const bufSize = fstat.size - offsetVal > limitVal ? limitVal : fstat.size - offsetVal
13-
const buf = Buffer.alloc(bufSize)
14-
FsExtra.readSync(fd, buf, 0, bufSize, offsetVal)
8+
9+
const chunks = opts.chunks || []
10+
const chunksBuf = _
11+
.chain(chunks)
12+
.map(chunk => {
13+
const bufOffset = +chunk.offset || 0
14+
const bufLimit = +chunk.limit || 40960
15+
const bufSize = Math.max(Math.min(bufLimit, fstat.size - bufOffset), 0)
16+
const buf = Buffer.alloc(bufSize)
17+
FsExtra.readSync(fd, buf, 0, bufSize, bufOffset)
18+
19+
return buf
20+
})
21+
.value()
22+
const mergedBuf = Buffer.concat(chunksBuf)
1523
FsExtra.closeSync(fd)
16-
const md5 = MD5(buf)
24+
const md5 = MD5(mergedBuf)
1725

1826
return md5
1927
}
2028

29+
function getHashChunkPlots (opts = {}) {
30+
let plots = []
31+
const name = opts.name || []
32+
33+
if (name === 'single plot') {
34+
const offset = opts.offset
35+
const limit = opts.limit
36+
plots.push({ offset, limit })
37+
} else if (name === 'distribution plots') {
38+
const path = opts.file
39+
const chunks = opts.chunks
40+
const offset = opts.offset
41+
const limit = opts.limit
42+
const fd = FsExtra.openSync(path, 'r')
43+
const fstat = FsExtra.fstatSync(fd)
44+
const fsize = fstat.size
45+
46+
plots = _.map(_.times(10), v => {
47+
const plotStart = Math.floor(v * fsize / chunks)
48+
const plotOffset = plotStart + offset
49+
50+
return { offset: plotOffset, limit }
51+
})
52+
FsExtra.closeSync(fd)
53+
}
54+
55+
return plots
56+
}
57+
2158
export default {
22-
getHash
59+
getHashByChunks,
60+
getHashChunkPlots
2361
}

0 commit comments

Comments
 (0)