-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCompare.java
184 lines (147 loc) · 6.9 KB
/
Compare.java
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
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Compare {
ArrayList<Text> projectTextList;
List<String> databasePaths;
int minNumLines;
Output result;
String databaseDir;
String projectDir;
public Compare(int numLinesMatch, String databaseDirIn, String projectDirIn) {
databaseDir = databaseDirIn;
projectDir = projectDirIn;
minNumLines = numLinesMatch;
}
public void installTextFiles(List<String> db_PathList) {
databasePaths = db_PathList;
}
public void installTextFiles(ArrayList<Text> projectList, List<String> db_PathList) {
projectTextList = projectList;
databasePaths = db_PathList;
}
public Output importResults(Output result, ArrayList<Result> threadResult) {
for (Result r : threadResult) {
result.addClone(r.file1, r.lineStart1, r.lineEnd1, r.file2,
r.lineStart2, r.lineEnd2, r.length, r.statementRaw1, r.statementStart1, r.statementEnd1,
r.statementRaw2, r.statementStart2, r.statementEnd2, r.totalHashValue);
}
return result;
}
public void compareMeshed (Output outputObject, int mode, int gapSize, int blockSize) {
result = outputObject;
System.out.println("\nComparing for " + databasePaths.size() + " files");
// perform comparison within the blocks
System.out.println("Processing within blocks");
for (int i = 0; i < databasePaths.size(); i = i + blockSize) {
int nextMark = i + blockSize;
if (nextMark > databasePaths.size()) {
nextMark = databasePaths.size();
}
// load up memory first
ArrayList<Text> thisBlock = new ArrayList<Text>();
for (int j = i; j < nextMark; j++) {
thisBlock.add(Database.loadSingleFile(databasePaths.get(j), databaseDir, minNumLines, false));
}
// perform local comparison
for (int j = 0; j < thisBlock.size() - 1; j++) {
System.out.print(j + "\r");
Text text1 = thisBlock.get(j);
for (int k = j + 1; k < thisBlock.size(); k++) {
Text text2 = thisBlock.get(k);
ArrayList<Result> threadResult =
TextCompare.textCompare(text1, text2, mode, gapSize, minNumLines, projectDir, databaseDir);
result = importResults(result, threadResult);
}
}
}
// perform comparison between the blocks
System.out.println("Processing between blocks");
for (int i = 0; i < databasePaths.size(); i = i + blockSize) {
int nextMark = i + blockSize;
if (nextMark > databasePaths.size()) {
// we are on the last block, terminate
break;
}
System.out.println(nextMark);
// load this block into memory
ArrayList<Text> thisBlock = new ArrayList<Text>();
for (int j = i; j < nextMark; j++) {
thisBlock.add(Database.loadSingleFile(databasePaths.get(j), databaseDir, minNumLines, false));
}
// between comparsion
for (int j = 0; j < thisBlock.size(); j++) {
Text text1 = thisBlock.get(j);
for (int k = nextMark; k < databasePaths.size(); k++) {
Text text2 = Database.loadSingleFile(databasePaths.get(k), databaseDir, minNumLines, false);
ArrayList<Result> threadResult =
TextCompare.textCompare(text1, text2, mode, gapSize, minNumLines, projectDir, databaseDir);
result = importResults(result, threadResult);
}
}
}
System.out.println("");
}
public Output compareBetween (Output outputObject, int mode, int gapSize, int numberThreads) {
result = outputObject;
System.out.println("\nComparing against " + databasePaths.size() + " database files");
if (numberThreads > 0) {
System.out.println("Using multithreading");
int processedNumber = 0;
while (processedNumber < databasePaths.size()) {
// try load four cpu
ExecutorService executor = Executors.newWorkStealingPool();
Set<Callable<ArrayList<Result>>> callables = new HashSet<Callable<ArrayList<Result>>>();
for (int i = 0; i < numberThreads && processedNumber < databasePaths.size(); i++) {
Text text1 = Database.loadSingleFile(databasePaths.get(processedNumber), databaseDir, minNumLines, false);
callables.add(new RunnableDemo("Thread-" + Integer.toString(processedNumber), projectTextList,
text1, mode, gapSize, minNumLines, projectDir, databaseDir));
processedNumber = processedNumber + 1;
}
// invoke and close everything
try {
List<Future<ArrayList<Result>>> futures = executor.invokeAll(callables);
for (Future<ArrayList<Result>> future : futures) {
ArrayList<Result> threadResult = future.get();
result = importResults(result, threadResult);
}
} catch (InterruptedException e) {
System.out.println("Interrupted thread exception");
System.out.println(e.getMessage());
} catch (ExecutionException e) {
System.out.println("Execution exception in thread");
System.out.println(e.getMessage());
e.printStackTrace(System.out);
} finally {
executor.shutdownNow();
}
executor.shutdown();
System.out.print((processedNumber+1) + "\r");
}
} else {
System.out.println("Disabled therading");
for (int i = 0; i < databasePaths.size(); i++) {
// outer loop is the database
Text text1 = Database.loadSingleFile(databasePaths.get(i), databaseDir, minNumLines, false);
for (int j = 0; j < projectTextList.size(); j++) {
// inner loop is the project
Text text2 = projectTextList.get(j);
ArrayList<Result> threadResult =
TextCompare.textCompare(text2, text1, mode, gapSize, minNumLines, projectDir, databaseDir);
result = importResults(result, threadResult);
}
System.out.print((i+1) + "\r");
}
}
return result;
}
}