-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathDupResultSet.cs
580 lines (499 loc) · 18.3 KB
/
DupResultSet.cs
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#define DETAIL
using System;
using System.Text;
using System.Diagnostics;
using System.IO;
using DigitalPlatform.IO;
namespace DigitalPlatform.LibraryServer
{
/*
[Serializable()]
public class DupLine
{
public string Path = "";
public int Weight = 0;
public DupLine(string strPath,
int nWeight)
{
this.Path = strPath;
this.Weight = nWeight;
}
}*/
// 行对象
public class DupLineItem : Item
{
int m_nLength = 0;
byte[] m_buffer = null;
public string Path = "";
public int Weight = 0;
public int Threshold = 0;
#if DETAIL
public string Detail { get; set; }
#endif
/*
public DupLineItem(string strPath,
int nWeight)
{
this.Weight = nWeight;
this.Path = strPath;
}
* */
public override void BuildBuffer()
{
//
byte[] baWeight = BitConverter.GetBytes((Int32)this.Weight);
Debug.Assert(baWeight.Length == 4, "");
//
byte[] baThreshold = BitConverter.GetBytes((Int32)this.Threshold);
Debug.Assert(baThreshold.Length == 4, "");
//
byte[] baPath = Encoding.UTF8.GetBytes(this.Path);
int nPathBytes = baPath.Length;
//
byte[] baPathLength = BitConverter.GetBytes((Int32)nPathBytes);
Debug.Assert(baPathLength.Length == 4, "");
#if DETAIL
//
byte[] baDetail = Encoding.UTF8.GetBytes(this.Detail);
int nDetailBytes = baDetail.Length;
//
byte[] baDetailLength = BitConverter.GetBytes((Int32)nDetailBytes);
Debug.Assert(baDetailLength.Length == 4, "");
#endif
this.Length = 4/*weight*/
+ 4/*threshold*/
+ 4/*length of path content */
+ nPathBytes
#if DETAIL
+ 4
+ nDetailBytes
#endif
;
m_buffer = new byte[this.Length];
int offs = 0;
Array.Copy(baWeight, m_buffer, baWeight.Length);
offs += 4;
Array.Copy(baThreshold, 0, m_buffer, offs, 4);
offs += 4;
Array.Copy(baPathLength, 0, m_buffer, offs, 4);
offs += 4;
Array.Copy(baPath, 0, m_buffer,offs, nPathBytes);
offs += nPathBytes;
#if DETAIL
Array.Copy(baDetailLength, 0, m_buffer, offs, 4);
offs += 4;
Array.Copy(baDetail, 0, m_buffer, offs, nDetailBytes);
offs += nDetailBytes;
#endif
}
/*
public DupLineItem FileLine
{
get
{
return m_line;
}
set
{
m_line = value;
this.m_strLineKey = m_line.Text;
byte[] baKey = Encoding.UTF8.GetBytes(this.m_strLineKey);
int nKeyBytes = baKey.Length;
// 初始化二进制内容
MemoryStream s = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(s, m_line);
this.Length = (int)s.Length + 4 + nKeyBytes; // 算上了length所占bytes
m_buffer = new byte[(int)s.Length];
s.Seek(0, SeekOrigin.Begin);
s.Read(m_buffer, 0, m_buffer.Length);
s.Close();
}
}
* */
public override int Length
{
get
{
return m_nLength;
}
set
{
m_nLength = value;
}
}
public override void ReadData(Stream stream)
{
if (this.Length == 0)
throw new Exception("length尚未初始化");
// 读入Weight
byte[] weightbuffer = new byte[4];
stream.Read(weightbuffer, 0, 4);
this.Weight = BitConverter.ToInt32(weightbuffer, 0);
// 读入Threshold
byte[] shresholdbuffer = new byte[4];
stream.Read(shresholdbuffer, 0, 4);
this.Threshold = BitConverter.ToInt32(shresholdbuffer, 0);
{
// 读入path length
byte[] lengthbuffer = new byte[4];
stream.Read(lengthbuffer, 0, 4);
int nPathLength = BitConverter.ToInt32(lengthbuffer, 0);
// 读入path content
if (nPathLength > 0)
{
byte[] pathbuffer = new byte[nPathLength];
stream.Read(pathbuffer, 0, nPathLength);
this.Path = Encoding.UTF8.GetString(pathbuffer);
}
else
this.Path = "";
}
#if DETAIL
{
// 读入 detail length
byte[] lengthbuffer = new byte[4];
stream.Read(lengthbuffer, 0, 4);
int nPathLength = BitConverter.ToInt32(lengthbuffer, 0);
// 读入detail content
if (nPathLength > 0)
{
byte[] pathbuffer = new byte[nPathLength];
stream.Read(pathbuffer, 0, nPathLength);
this.Detail = Encoding.UTF8.GetString(pathbuffer);
}
else
this.Detail = "";
}
#endif
}
public override void ReadCompareData(Stream stream)
{
if (this.Length == 0)
throw new Exception("length尚未初始化");
// 读入Weight
byte[] weightbuffer = new byte[4];
stream.Read(weightbuffer, 0, 4);
this.Weight = BitConverter.ToInt32(weightbuffer, 0);
// 读入Threshold
byte[] shresholdbuffer = new byte[4];
stream.Read(shresholdbuffer, 0, 4);
this.Threshold = BitConverter.ToInt32(shresholdbuffer, 0);
// 读入path length
byte[] lengthbuffer = new byte[4];
stream.Read(lengthbuffer, 0, 4);
int nPathLength = BitConverter.ToInt32(lengthbuffer, 0);
// 读入path content
if (nPathLength > 0)
{
byte[] pathbuffer = new byte[nPathLength];
stream.Read(pathbuffer, 0, nPathLength);
this.Path = Encoding.UTF8.GetString(pathbuffer);
}
else
this.Path = "";
}
public override void WriteData(Stream stream)
{
if (m_buffer == null)
BuildBuffer();
if (m_buffer == null)
{
throw (new Exception("m_buffer尚未初始化"));
}
// 写入Length个bytes的内容
stream.Write(m_buffer, 0, this.Length);
}
// 实现IComparable接口的CompareTo()方法,
// obj: An object to compare with this instance
// 返回值 A 32-bit signed integer that indicates the relative order of the comparands. The return value has these meanings:
// Less than zero: This instance is less than obj.
// Zero: This instance is equal to obj.
// Greater than zero: This instance is greater than obj.
// 异常: ArgumentException,obj is not the same type as this instance.
public override int CompareTo(object obj)
{
DupLineItem item = (DupLineItem)obj;
// 小在前
return String.Compare(this.Path, item.Path);
}
// 按照权值排序
public int CompareWeightTo(object obj)
{
DupLineItem item = (DupLineItem)obj;
int delta = this.Weight - item.Weight;
if (delta != 0)
return -1 * delta; // 大在前
// 如权值相同,再按照路径排序
// 小在前
return String.Compare(this.Path, item.Path);
}
// 按照差额排序。所谓差额就是权值和阈值的差额
public int CompareOverThresholdTo(object obj)
{
DupLineItem item = (DupLineItem)obj;
int over1 = this.Weight - this.Threshold;
int over2 = item.Weight - item.Threshold;
int delta = over1 - over2;
if (delta != 0)
return -1 * delta; // 大在前
// 如差额相同,再按照路径排序
// 小在前
return String.Compare(this.Path, item.Path);
}
}
/// <summary>
/// 用于查重的结果集文件对象
/// 主要特征是,每个事项都包含一个权值整数字段
/// </summary>
public class DupResultSet : ItemFileBase
{
// 2017/4/14
// 辅助记忆是否排序过
public bool Sorted { get; set; }
// 排序风格
public DupResultSetSortStyle SortStyle = DupResultSetSortStyle.Path;
public DupResultSet()
{
}
public override Item NewItem()
{
return new DupLineItem();
}
public string Dump()
{
return "";
}
// 使得可以按照多种风格排序
public override int Compare(long lPtr1, long lPtr2)
{
if (lPtr1 < 0 && lPtr2 < 0)
return 0;
else if (lPtr1 >= 0 && lPtr2 < 0)
return 1;
else if (lPtr1 < 0 && lPtr2 >= 0)
return -1;
DupLineItem item1 = (DupLineItem)GetCompareItemByOffset(lPtr1);
DupLineItem item2 = (DupLineItem)GetCompareItemByOffset(lPtr2);
if (this.SortStyle == DupResultSetSortStyle.Path)
return item1.CompareTo(item2);
else if (this.SortStyle == DupResultSetSortStyle.Weight)
return item1.CompareWeightTo(item2);
else if (this.SortStyle == DupResultSetSortStyle.OverThreshold)
return item1.CompareOverThresholdTo(item2);
else
{
Debug.Assert(false, "invalid sort style");
return 0;
}
}
// 功能: 合并两个数组
// parameters:
// strStyle 运算风格 OR , AND , SUB
// sourceLeft 源左边结果集
// sourceRight 源右边结果集
// targetLeft 目标左边结果集
// targetMiddle 目标中间结果集
// targetRight 目标右边结果集
// bOutputDebugInfo 是否输出处理信息
// strDebugInfo 处理信息
// return
// -1 出错
// 0 成功
public static int Merge(string strStyle,
DupResultSet sourceLeft,
DupResultSet sourceRight,
DupResultSet targetLeft,
DupResultSet targetMiddle,
DupResultSet targetRight,
bool bOutputDebugInfo,
out string strDebugInfo,
out string strError)
{
strDebugInfo = "";
strError = "";
if (sourceLeft.m_streamSmall == null)
{
throw new Exception("sourceLeft结果集对象未建索引");
}
if (sourceRight.m_streamSmall == null)
{
throw new Exception("sourceRight结果集对象未建索引");
}
if (bOutputDebugInfo == true)
{
strDebugInfo += "strStyle值:" + strStyle + "<br/>";
strDebugInfo += "sourceLeft结果集:" + sourceLeft.Dump() + "<br/>";
strDebugInfo += "sourceRight结果集:" + sourceRight.Dump() + "<br/>";
}
if (String.Compare(strStyle, "OR", true) == 0)
{
if (targetLeft != null || targetRight != null)
{
Exception ex = new Exception("DpResultSetManager::Merge()中是不是参数用错了?当strStyle参数值为\"OR\"时,targetLeft参数和targetRight无效,值应为null");
throw (ex);
}
}
DupLineItem dpRecordLeft;
DupLineItem dpRecordRight;
int i = 0;
int j = 0;
int ret;
while (true)
{
dpRecordLeft = null;
dpRecordRight = null;
if (i >= sourceLeft.Count)
{
if (bOutputDebugInfo == true)
{
strDebugInfo += "i大于等于sourceLeft的个数,将i改为-1<br/>";
}
i = -1;
}
else if (i != -1)
{
try
{
dpRecordLeft = (DupLineItem)sourceLeft[i];
if (bOutputDebugInfo == true)
{
strDebugInfo += "取出sourceLeft集合中第" + Convert.ToString(i) + "个元素,Path为" + dpRecordLeft.Path + "<br/>";
}
}
catch (Exception e)
{
Exception ex = new Exception("取SourceLeft集合出错:i=" + Convert.ToString(i) + "----Count=" + Convert.ToString(sourceLeft.Count) + ", internel error :" + e.Message + "<br/>");
throw (ex);
}
}
if (j >= sourceRight.Count)
{
if (bOutputDebugInfo == true)
{
strDebugInfo += "j大于等于sourceRight的个数,将j改为-1<br/>";
}
j = -1;
}
else if (j != -1)
{
try
{
dpRecordRight = (DupLineItem)sourceRight[j];
if (bOutputDebugInfo == true)
{
strDebugInfo += "取出sourceRight集合中第" + Convert.ToString(j) + "个元素,Path为" + dpRecordRight.Path + "<br/>";
}
}
catch
{
Exception ex = new Exception("j=" + Convert.ToString(j) + "----Count=" + Convert.ToString(sourceLeft.Count) + sourceRight.GetHashCode() + "<br/>");
throw (ex);
}
}
if (i == -1 && j == -1)
{
if (bOutputDebugInfo == true)
{
strDebugInfo += "i,j都等于-1跳出<br/>";
}
break;
}
if (dpRecordLeft == null)
{
if (bOutputDebugInfo == true)
{
strDebugInfo += "dpRecordLeft为null,设ret等于1<br/>";
}
ret = 1;
}
else if (dpRecordRight == null)
{
if (bOutputDebugInfo == true)
{
strDebugInfo += "dpRecordRight为null,设ret等于-1<br/>";
}
ret = -1;
}
else
{
ret = dpRecordLeft.CompareTo(dpRecordRight); //MyCompareTo(oldOneKey); //改CompareTO
if (bOutputDebugInfo == true)
{
strDebugInfo += "dpRecordLeft与dpRecordRight均不为null,比较两条记录得到ret等于" + Convert.ToString(ret) + "<br/>";
}
}
if (String.Compare(strStyle, "OR", true) == 0
&& targetMiddle != null)
{
if (ret == 0)
{
// 左右任意取一个就可以,但是要加上权值 2007/7/2
dpRecordLeft.Weight += dpRecordRight.Weight;
#if DETAIL
MergeDetail(dpRecordLeft, dpRecordRight);
#endif
targetMiddle.Add(dpRecordLeft);
i++;
j++;
}
else if (ret < 0)
{
targetMiddle.Add(dpRecordLeft);
i++;
}
else if (ret > 0)
{
targetMiddle.Add(dpRecordRight);
j++;
}
continue;
}
if (ret == 0 && targetMiddle != null)
{
if (bOutputDebugInfo == true)
{
strDebugInfo += "ret等于0,加到targetMiddle里面<br/>";
}
// 左右任意取一个就可以,但是要加上权值 2007/7/2
dpRecordLeft.Weight += dpRecordRight.Weight;
targetMiddle.Add(dpRecordLeft);
i++;
j++;
}
if (ret < 0)
{
if (bOutputDebugInfo == true)
{
strDebugInfo += "ret小于0,加到targetLeft里面<br/>";
}
if (targetLeft != null && dpRecordLeft != null)
targetLeft.Add(dpRecordLeft);
i++;
}
if (ret > 0)
{
if (bOutputDebugInfo == true)
{
strDebugInfo += "ret大于0,加到targetRight里面<br/>";
}
if (targetRight != null && dpRecordRight != null)
targetRight.Add(dpRecordRight);
j++;
}
}
return 0;
}
// 合并两个事项的 Detail 部分
static void MergeDetail(DupLineItem left, DupLineItem right)
{
left.Detail = left.Detail + " OR " + right.Detail;
}
}
public enum DupResultSetSortStyle
{
Path = 0, // 按照路径排序
Weight = 1, // 按照权值排序。如果权值相同,则按路径排序
OverThreshold = 2, // 按照权值和阈值的差额来排序。如果这个差额相同,则按路径排序
}
}