This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merger.js
104 lines (82 loc) · 2.25 KB
/
merger.js
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
var fs = require("fs")
var filesFlags = "-f",
outputFlag = "-o"
var activeArg = ""
var output,
input = []
process.argv.forEach(function(arg, idx){
if (arg === filesFlags) {
activeArg = filesFlags
} else if ( arg === outputFlag ) {
activeArg = outputFlag
} else {
if ( activeArg === filesFlags ) {
input.push(arg)
} else if ( activeArg === outputFlag ) {
output = arg
}
}
})
var inpLength = input.length
if ( inpLength === 0 )
throw new Error("No files specified with the flag -f")
if ( !output )
throw new Error("No output file specified")
var rdbVersion = process.env.rdbVer || 6,
checksumLength = 0
if ( rdbVersion > 4 )
checksumLength = 8
var readStreams = input.map(function(filename, idx){
var options = { encoding: null },
stats = fs.statSync(filename),
lastByte = stats.size - 1 - checksumLength
if ( idx === 0 ){
options.start = 0
options.end = lastByte - 1
} else if ( idx === inpLength - 1) {
options.start = 11
options.end = lastByte
} else {
options.start = 11
options.end = lastByte - 1
}
if ( idx === 0 && idx === inpLength - 1 ) {
options.start = 0
options.end = lastByte
}
return { stream: fs.createReadStream(filename, options), length: options.end - options.start + 1 }
})
// erase old file
try {
fs.unlinkSync(output)
} catch(e) {
// could be no file, we care not
}
var position = 0
var writeStreams = readStreams.map(function(handler){
var stream = handler.stream,
length = handler.length
var options = {
flags: "a",
encoding: null,
start: position
}
position += length
var writeStream = fs.createWriteStream(output, options)
stream.pipe(writeStream)
writeStream.on("finish", printReport)
return writeStream
})
var toBeCalled = writeStreams.length
var exec = require('child_process').exec,
child
function printReport() {
if ( --toBeCalled > 0 )
return
child = exec(__dirname + "/redis_crc64/crc64 " + output,
function(error, stdout, stderr){
if ( error ) throw error
console.log(stdout)
process.exit(0)
})
}