-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
284 lines (248 loc) · 7.62 KB
/
index.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
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
280
281
282
283
284
// @copyright Tencent MT Team
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var chunkSize = 12;
var helper = require('./lib/helper');
/**
* 根据旧文件和新文件差异,生成基于旧文件的增量内容
*
* @public
*
* @param {string} oldpath 旧文件路径 required
* @param {string} newpath 新文件路径 required
* @param {object} config 可选配置
* oldfile_type: 'path|content'
* newfile_type: 'path|content'
* chunkSize: number
* output: filename
*
* @return {object}
* status: {boolean} 是否生成,false 为对应文件不存在时生成失败。
* signal: {number} 任务执行结果.
* 1 **旧**版本文件不存在
* 2 **新**版本文件不存在
* 3 当需要写入文件时,写入失败
* 4 调用参数类型有误
* 10 增量文件内容获取成功, (需要写入文件时同时写入成功)
* code: {string}当 signal 为 true 时 则为生成的增量内容,否则为失败原因
*/
exports.build = function(oldfilepath, newfilepath, config) {
var ret = {
status: false,
signal: -1,
code: ''
};
config = config || {};
// path, content
config.oldfile_type = config.oldfile_type ||'path';
config.newfile_type = config.newfile_type || 'path';
if (config.oldfile_type === 'path' && (!oldfilepath || !fs.existsSync(oldfilepath))) {
ret.signal = 1;
ret.code = '目标文件 ' + oldfilepath + ' 不存在, 不需要生成增量文件';
} else if (config.newfile_type === 'path' && (!newfilepath || !fs.existsSync(newfilepath))) {
ret.signal = 2;
ret.code = '目标文件新版本 ' + newfilepath + ' 不存在, 请检查配置';
} else {
if (config.chunkSize) {
chunkSize = config.chunkSize;
}
ret.code = JSON.stringify(makeIncDataFile(oldfilepath, newfilepath, config));
if (config.output) {
try {
helper.insure(path.dirname(config.output));
fs.writeFileSync(config.output, ret.code);
set();
} catch (e) {
ret.code = e.code + ', 写入失败, 请检查是否有权限写入';
ret.signal = 3;
}
} else {
set();
}
}
return ret;
function set() {
ret.signal = 10;
ret.status = true;
}
};
function makeIncDataFile(oldFile, newFile, config) {
var oldFileContent = config.oldfile_type === 'path' ? fs.readFileSync(oldFile, {
encoding: 'utf8'
}) : oldFile;
var newFileContent = config.newfile_type === 'path' ? fs.readFileSync(newFile, {
encoding: 'utf8'
}) : newFile;
var resultFile = {
modify: true,
chunkSize: chunkSize
};
var strDataArray = [];
//计算新旧两个文件,如果相同则说明文件没有改动,则直接返回空数组
if (getMd5(oldFileContent) == getMd5(newFileContent)) {
resultFile.modify = false;
resultFile.data = strDataArray;
return resultFile;
}
var oldChecksum = oldFileChecksum(oldFileContent, chunkSize);
//var diffArray = searchChunk(newFile, oldChecksum, chunkSize);
var diffArray = searchChunk(newFileContent, oldChecksum, chunkSize);
var arrayData = "";
var lastitem = null;
var matchCount = 0;
var size = diffArray.length;
for (var i = 0; i < size; i++) {
var item = diffArray[i];
if (item.isMatch) {
//如果第一个匹配,
if (lastitem == null || !lastitem.isMatch) {
arrayData = "[" + item.data + ",";
matchCount = 1;
} else if (lastitem.isMatch && lastitem.data + 1 == item.data) {
matchCount++;
} else if (lastitem.isMatch && (lastitem.data + 1) != item.data) {
arrayData += matchCount + "]";
strDataArray.push(JSON.parse(arrayData));
arrayData = "[" + item.data + ",";
matchCount = 1;
}
if (i == (size - 1)) {
arrayData += matchCount + "]";
strDataArray.push(JSON.parse(arrayData));
arrayData = "";
}
} else {
if (matchCount > 0) {
arrayData += matchCount + "]";
strDataArray.push(JSON.parse(arrayData));
arrayData = "";
matchCount = 0;
}
//"
var data = item.data;
strDataArray.push(data);
}
lastitem = item;
}
resultFile.data = strDataArray;
return resultFile;
}
function diffItem(m, dt) {
this.isMatch = m;
this.data = dt;
}
function doExactNewData(incDataArray, data) {
var di = new diffItem(false, data);
incDataArray.push(di);
}
function doExactMatch(incDataArray, chunkNo) {
// 写块匹配
var di = new diffItem(true, chunkNo);
incDataArray.push(di);
}
function searchChunk(newFileContent, checksumArray, chunkSize) {
var incDataArray = [];
//chunk
var buffer = null;
//用于缓存两个匹配块之间的新增数据
var outBuffer = "";
// 指向块后的第一个字符
var currentIndex = 0;
var strInput = newFileContent;
var tLen = strInput.length;
var lastmatchNo = 0;
while (currentIndex <= tLen) {
var endIndex = currentIndex + chunkSize;
if (endIndex > tLen) {
endIndex = tLen;
}
buffer = strInput.substring(currentIndex, endIndex);
var chunkMd5 = getMd5(buffer);
var matchTrunkIndex = checkMatchIndex(chunkMd5, checksumArray, lastmatchNo);
//若果是最后一个
if (endIndex > tLen - 1) {
//先把新块压入队列
if (outBuffer.length > 0) {
doExactNewData(incDataArray, outBuffer);
outBuffer = "";
}
if (buffer.length > 0) {
doExactNewData(incDataArray, buffer);
}
currentIndex = currentIndex + chunkSize;
}
//如果找到匹配块
else if (matchTrunkIndex >= 0) {
//先把新块压入队列
if (outBuffer.length > 0) {
doExactNewData(incDataArray, outBuffer);
outBuffer = "";
}
doExactMatch(incDataArray, matchTrunkIndex);
currentIndex = currentIndex + chunkSize;
} else {
outBuffer = outBuffer + strInput.substring(currentIndex, currentIndex + 1);
currentIndex++;
}
if (matchTrunkIndex >= 0) {
lastmatchNo = matchTrunkIndex;
}
}
return incDataArray;
}
function oldFileChecksum(fileC, chunkSize) {
var txt = fileC;
var checksumArray = {}; // object
var currentIndex = 0;
var len = txt.length;
var chunkNo = 0;
while (currentIndex < len) {
var chunk = txt.substr(currentIndex, chunkSize);
var chunkMd5 = getMd5(chunk);
//用map来解决冲突,
var numArray = checksumArray[chunkMd5];
//如果没有过一个一样的
if (typeof(numArray) == 'undefined') {
numArray = [];
}
numArray.push(chunkNo);
checksumArray[chunkMd5] = numArray;
currentIndex = currentIndex + chunkSize;
chunkNo++;
}
return checksumArray;
}
function getMatchNo(numArray, lastmatchNo) {
if (numArray.length == 1) {
return numArray[0];
} else {
var lastNo = numArray[0];
var reNo = 0;
for (var i = 0; i < numArray.length; i++) {
var curNo = numArray[i];
if (curNo >= lastmatchNo && lastNo <= lastmatchNo) {
return (lastmatchNo - lastNo) >= (curNo - lastmatchNo) ? curNo : lastNo;
} else if (curNo >= lastmatchNo && lastNo >= lastmatchNo) {
return lastNo;
} else if (curNo <= lastmatchNo && lastNo <= lastmatchNo) {
reNo = curNo;
} else {
reNo = curNo;
}
lastNo = curNo;
}
return reNo;
}
}
function checkMatchIndex(chunkMd5, checksumArray, lastmatchNo) {
var numArray = checksumArray[chunkMd5];
if (typeof(numArray) == 'undefined') {
return -1;
} else {
return getMatchNo(numArray, lastmatchNo);
}
}
function getMd5(c) {
return crypto.createHash('md5').update(c).digest('hex');
}