-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhahah.java
382 lines (314 loc) · 16.3 KB
/
hahah.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
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
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.flipkart.zjsonpatch.DiffFlags;
import com.flipkart.zjsonpatch.JsonDiff;
import com.flipkart.zjsonpatch.JsonPatch;
import com.flipkart.zjsonpatch.JsonPatchApplicationException;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.collections4.CollectionUtils;
import static java.lang.System.currentTimeMillis;
public class hahah {
private ObjectMapper objectMapper = new ObjectMapper();
private static List<String> PATHS_TO_IGNORE = null;
EnumSet<DiffFlags> flags = DiffFlags.dontNormalizeOpIntoMoveAndCopy();
public hahah() {
initializePathsToIgnore();
}
private void initializePathsToIgnore() {
if (PATHS_TO_IGNORE != null) {
return;
}
try {
DataInputStream inputStream = new DataInputStream(new FileInputStream("pathsToIgnoreWhileDiff.txt"));
} catch (Exception e) {
System.out.println("Error initializing pathsToIgnore");
}
if (PATHS_TO_IGNORE == null) {
throw new RuntimeException("Unable to read files to check pathsToIgnore");
}
}
public String getDifferences(String documentToUpdate, String currentDocument) {
try {
JsonNode beforeNode = objectMapper.readTree(objectMapper.writeValueAsBytes(currentDocument));
JsonNode afterNode = objectMapper.readTree(objectMapper.writeValueAsBytes(documentToUpdate));
JsonNode patchNode = JsonDiff.asJson(beforeNode, afterNode, flags);
StringBuilder comment = new StringBuilder();
for (int i = 0; i < patchNode.size(); i++) {
final int tempIndexForLambda = i;
if (PATHS_TO_IGNORE.stream().filter(tag -> patchNode.get(tempIndexForLambda).get("path").toString().contains(tag)).count() > 0) {
continue;
} else {
String operation = patchNode.get(i).get("op").toString();
String pathName = patchNode.get(i).get("path").toString();
String value = patchNode.get(i).get("value").toString();
if (operation.contains("replace")) {
comment.append("Updated " + pathName + " to " + value + "\n");
//System.out.println("Updated " + pathName + " to " +value);
} else if (operation.contains("remove")) {
comment.append("Removed " + value + " from " + pathName + "\n");
//System.out.println("Removed " + value + " from " + pathName);
} else if (operation.contains("add")) {
comment.append("Added " + value + " to " + pathName + "\n");
//System.out.println("Added " + value + " to " + pathName);
}
}
}
return comment.toString();
} catch (IOException e) {
System.out.println("Exception is: " + e);
}
return null;
}
public String merge(String sourceDocument, String currentDocument, String newDocument) throws Exception {
JsonNode sourceNode = objectMapper.readTree(objectMapper.writeValueAsBytes(sourceDocument));
JsonNode currentNode = objectMapper.readTree(objectMapper.writeValueAsBytes(currentDocument));
JsonNode newNode = objectMapper.readTree(objectMapper.writeValueAsBytes(newDocument));
JsonNode mergedNode = merge(sourceNode, currentNode, newNode);
if (mergedNode == null) {
throw new Exception("Cannot merge nodes");
}
return (String) objectMapper.readValue(objectMapper.writeValueAsString(mergedNode), sourceDocument.getClass());
}
/**
* @param sourceNode sourceNode for both current and new node
* @param currentNode current node in repository
* @param newNode new Node that user wants to save
* @throws IOException
*/
private JsonNode merge(JsonNode sourceNode, JsonNode currentNode, JsonNode newNode) throws IOException {
JsonNode diff1 = JsonDiff.asJson(sourceNode, newNode, flags);
JsonNode diff2 = JsonDiff.asJson(sourceNode, currentNode, flags);
if (isNoMergeConflict(diff1, diff2, currentNode, newNode)) {
String result = concateDiffs(diff1, diff2);
JsonNode diffs = createSetofDiffs(objectMapper.readTree(result));
System.out.println("fin " + diffs);
return (applyPatch(sourceNode, diffs));
}
return null;
}
private String concateDiffs(JsonNode diff1, JsonNode diff2) {
String str1 = diff1.toString().substring(1, diff1.toString().length() - 1);
String str2 = diff2.toString().substring(1, diff2.toString().length() - 1);
List<String> strs = Arrays.asList(str1, str2);
String result = "[";
for(String str : strs){
if(str != null && str.length() > 0){
result += str + ",";
}
}
result = result.substring(0, result.length()-1) + "]";
return result;
}
private String removeIndexFromPath(String path) {
int index = getArrayIndexFromPath(path.substring(1, path.length()-1));
return index == - 1 ? path : path.substring(0, path.length()-Integer.toString(index).length()-1);
}
private JsonNode createSetofDiffs(JsonNode diffs) throws IOException {
String stringOfDiffs = "";
Map<String, JsonNode> map = new HashMap<>();
for (JsonNode diff : diffs) {
String value = diff.get("value").toString();
//dont add to set if the (value and path) exist as a combo
if (map.containsKey(value) && isSamePath(map, diff, value)) {
continue;
} else {
if(value.contains("delete")){
continue;
}
map.put(value, diff);
stringOfDiffs += diff.toString() + ",";
}
}
stringOfDiffs = "[" + stringOfDiffs.substring(0, stringOfDiffs.length() - 1) + "]";
return objectMapper.readTree(stringOfDiffs);
}
private JsonNode applyPatch(JsonNode currentNode, JsonNode diff1) {
JsonNode result = null;
long startTime = currentTimeMillis();
try {
result = JsonPatch.apply(diff1, currentNode);
} catch (JsonPatchApplicationException e) {
System.out.println("Unable to apply patch" + e);
} finally {
System.out.println("Time taken to apply patch: "+(currentTimeMillis() - startTime)+"}, patch applied: "+result != null+"");
}
return result;
}
private int getArrayIndexFromPath(String path) {
Matcher matcher = Pattern.compile("(\\d+$)").matcher(path);
String index = "";
if(matcher.find()){
index = matcher.group(0);
}
try {
return Integer.parseInt(index);
} catch (NumberFormatException e) {
return -1;
}
}
private Boolean isPathAnArray(String path){
return path.matches(".*?\\/\\d+$");
}
private void modifyPathForArrays(HashMap<String, JsonNode> diff1Map, HashMap<String, JsonNode> diff2Map, JsonNode diff1, JsonNode diff2, JsonNode currentNode, JsonNode newNode) {
HashMap<String, Integer> maxIndexForPath = new HashMap<>();
//init path frequency map (path, maxIndexForPath)
getMaxIndexFromPath(diff1Map, diff1, maxIndexForPath);
getMaxIndexFromPath(diff2Map, diff2, maxIndexForPath);
for (Map.Entry<String, JsonNode> entry : diff1Map.entrySet()) {
String path = entry.getValue().get("path").asText();
String operation = entry.getValue().get("op").toString();
String pathLiteral = entry.getKey().substring(1, entry.getKey().length() - 1);
if(isSameValidationRule(diff2Map, path) && path.contains("validationRules")){
throw new RuntimeException("Validation rule: data loss");
}
if (isSamePath(diff2Map, entry.getKey()) && isDifferentValue(diff2Map, entry) && operation.contains("replace")){
if(!path.contains("routingMessageContext") && !isPathAnArray(pathLiteral)){
continue;
}
modifyReplaceWithAddOperation(diff1Map, diff2Map, maxIndexForPath, entry);
//System.out.println(diff2Map.get(entry.getKey()));
continue;
}
//if its an add/remove operation, paths match, and the value is different
if (isSamePath(diff2Map, entry.getKey()) && isDifferentValue(diff2Map, entry) && (operation.contains("add") || operation.contains("remove"))) {
if(isPathAnArray(path) && path.contains("validationRules") && operation.contains("remove")){
throw new RuntimeException("Validation rule: data loss");
}
modifyAddOperation(diff2Map, maxIndexForPath, entry);
//System.out.println(diff2Map.get(entry.getKey()));
}
}
}
private void modifyReplaceWithAddOperation(HashMap<String, JsonNode> diff1Map, HashMap<String, JsonNode> diff2Map, HashMap<String, Integer> maxIndexForPath, Map.Entry<String, JsonNode> entry) {
int maxInArray = maxIndexForPath.get(removeIndexFromPath(entry.getKey())) + 1;
((ObjectNode) diff1Map.get(entry.getKey())).put("op", "add");
((ObjectNode) diff1Map.get(entry.getKey())).put("path", removeIndexFromPath(entry.getKey().substring(1, entry.getKey().length() - 2)) + maxInArray);
((ObjectNode) diff1Map.get(entry.getKey())).put("value", diff2Map.get(entry.getKey()).get("value"));
maxIndexForPath.put(removeIndexFromPath(entry.getKey()), maxIndexForPath.get(removeIndexFromPath(entry.getKey())) + 1);
}
private void modifyAddOperation(HashMap<String, JsonNode> diff2Map, HashMap<String, Integer> maxIndexForPath, Map.Entry<String, JsonNode> entry) {
int maxInArray = maxIndexForPath.get(removeIndexFromPath(entry.getKey())) + 1;
((ObjectNode) diff2Map.get(entry.getKey())).put("path", removeIndexFromPath(entry.getKey().substring(1, entry.getKey().length() - 2)) + maxInArray);
maxIndexForPath.put(removeIndexFromPath(entry.getKey()), maxIndexForPath.get(removeIndexFromPath(entry.getKey())) + 1);
}
private boolean isDifferentValue(HashMap<String, JsonNode> diff2Map, Map.Entry<String, JsonNode> entry) {
return entry.getValue().get("value") != null
&& diff2Map.get(entry.getKey()).get("value") != null
&& entry.getValue().get("value").toString() != diff2Map.get(entry.getKey()).get("value").toString() ;
}
private boolean isSamePath(HashMap<String, JsonNode> diffMap, String value) {
return diffMap.containsKey(value);
}
private boolean isSameValidationRule(HashMap<String, JsonNode> diff2Map, String path) {
for(String path2 : diff2Map.keySet()){
if(path2.contains("validationRules") && isSameValidationRule(path, path2)){
return true;
}
}
return false;
}
private boolean isSameValidationRule(String path1, String path2) {
Matcher matcher1 = Pattern.compile("(\\d+\\/validationRules)").matcher(path1);
Matcher matcher2 = Pattern.compile("(\\d+\\/validationRules)").matcher(path2);
if(matcher1.find()){
path1 = matcher1.group(0);
}
if(matcher2.find()){
path2 = matcher2.group(0);
}
return path1.contains(path2);
}
private boolean isSamePath(Map<String, JsonNode> map, JsonNode diff, String value) {
return removeIndexFromPath(map.get(value).get("path").toString()).contains(removeIndexFromPath(diff.get("path").toString()));
}
private void getMaxIndexFromPath(HashMap<String, JsonNode> diffMap, JsonNode diff, HashMap<String, Integer> count) {
for (int i = 0; i < diff.size(); i++) {
String path = diff.get(i).get("path").toString();
diffMap.put(path, diff.get(i));
int index = getArrayIndexFromPath(diff.get(i).get("path").asText());
if (count.containsKey(removeIndexFromPath(path))) {
count.put(removeIndexFromPath(path), Math.max(count.get(removeIndexFromPath(path)), index));
} else {
count.put(removeIndexFromPath(path), index);
}
}
}
private boolean isNoMergeConflict(JsonNode diff1, JsonNode diff2, JsonNode currentNode, JsonNode newNode) {
System.out.println("**********************");
System.out.println("Pub Changes: " + diff2);
System.out.println("New Changes: " + diff1);
HashMap<String, JsonNode> diff1Map = new HashMap<>();
HashMap<String, JsonNode> diff2Map = new HashMap<>();
modifyPathForArrays(diff1Map, diff2Map, diff1, diff2, currentNode, newNode);
long startTime = currentTimeMillis();
Map<String, JsonNode> paths1 = new HashMap();
Map<String, JsonNode> paths2 = new HashMap();
addAllPaths(diff1, paths1);
addAllPaths(diff2, paths2);
Collection<String> intersection = CollectionUtils.intersection(paths1.keySet(), paths2.keySet());
removeCurrentBranchVersion(intersection);
removePathsWithSameValues(intersection, paths1, paths2);
System.out.println("Time taken to determine if there is no conflict: "+(currentTimeMillis() - startTime)+", conflict found: "+!intersection.isEmpty()+", common changes: " + intersection+")");
if(!intersection.isEmpty()){
throw new RuntimeException(intersection.toString());
}
return intersection.isEmpty();
}
private void removePathsWithSameValues(Collection<String> intersection, Map<String, JsonNode> paths1, Map<String, JsonNode> paths2) {
intersection.removeIf(path -> hasSameValueInDiffs(paths1.get(path), paths2.get(path)));
}
private boolean hasSameValueInDiffs(JsonNode diff1, JsonNode diff2) {
if (diff1 == null && diff2 == null) return true;
if (diff1 == null || diff2 == null) return false;
return diff1.equals(diff2);
}
private void removeCurrentBranchVersion(Collection<String> intersection) {
intersection.removeAll(PATHS_TO_IGNORE);
}
private void addAllPaths(JsonNode diff1, Map<String, JsonNode> paths) {
for (JsonNode jsonNode : diff1) {
if (jsonNode.has("path")) {
paths.put(jsonNode.get("op").asText() + jsonNode.get("path").asText(), jsonNode);
}
addAllPaths(jsonNode, paths);
}
}
private void addAllPathsWithoutOpertation(JsonNode diff1, Map<String, JsonNode> paths) {
for (JsonNode jsonNode : diff1) {
if (jsonNode.has("path")) {
paths.put(jsonNode.get("path").asText(), jsonNode);
}
addAllPaths(jsonNode, paths);
}
}
public boolean hasChanged(Object oldRule, Object newRule, List<String> pathsToIgnore, List<Pattern> regexPaths) {
try {
JsonNode oldNode = objectMapper.readTree(objectMapper.writeValueAsBytes(oldRule));
JsonNode newNode = objectMapper.readTree(objectMapper.writeValueAsBytes(newRule));
JsonNode diff = JsonDiff.asJson(oldNode, newNode);
Map<String, JsonNode> paths = new HashMap<>();
addAllPathsWithoutOpertation(diff, paths);
removeAllPathsToIgnore(paths.keySet(), pathsToIgnore, regexPaths);
return !paths.isEmpty();
} catch (IOException e) {
throw new RuntimeException("Unable to determine whether changes require wip", e);
}
}
private void removeAllPathsToIgnore(Set<String> paths, List<String> pathsToIgnore, List<Pattern> regexPaths) {
paths.removeAll(pathsToIgnore);
paths.removeAll(PATHS_TO_IGNORE);
regexPaths.forEach(regex -> removePathMatchingRegex(paths, regex));
}
private void removePathMatchingRegex(Set<String> paths, Pattern regex) {
paths.removeIf(path -> regex.matcher(path).matches());
}
public static void main(String args[])
{
}
}