This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpcsvgraph_reparent.groovy
322 lines (288 loc) · 11.2 KB
/
httpcsvgraph_reparent.groovy
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
import com.google.common.collect.*;
import java.util.regex.*;
import org.apache.commons.csv.CSVParser;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.FileUtils;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.json.JSONException;
import org.json.JSONObject;
/** */
public class HttpCsvGraphReparent {
@javax.ws.rs.Path("")
public static class MyResource { // Must be public
@GET
@javax.ws.rs.Path("")
@Produces("application/json")
// We can only call this once between each browser refresh. Our method
// should in theory not need to know the other siblings.
public Response moveUp(@QueryParam("child") String iChild,
@QueryParam("newParent") String newParent,
@QueryParam("heapFile") String heapFile)
throws JSONException, IOException {
reparent(iChild, newParent, "/home/sarnobat/www/" + heapFile);
System.out
.println("HttpCsvGraphReparent.MyResource.moveUp() 2");
JSONObject jsonObject = new JSONObject();
return Response.ok().header("Access-Control-Allow-Origin", "*")
.type("application/json")
.entity(jsonObject.toString()).build();
}
@GET
@javax.ws.rs.Path("/health")
@Produces("application/json")
public Response health() {
Response r = Response.ok().header("Access-Control-Allow-Origin", "*").type("application/json").entity(new JSONObject().toString()).build();
return r;
}
private static Collection<String> getOtherSiblings(String iNodeToMoveUp, String filepath) throws IOException {
System.out.println("HttpCsvGraphReparent.MyResource.getOtherSiblings()");
String parent = getParent(iNodeToMoveUp);
return remove(getChildren(parent, filepath), iNodeToMoveUp);
}
private static Collection<String> remove(Collection<String> iNodes,
String iExclude) {
System.out.println("HttpCsvGraphReparent.MyResource.remove() begin");
ImmutableSet.Builder<String> reduced = ImmutableSet.builder();
for (String aNode : iNodes) {
if (!aNode.equals(iExclude)) {
reduced.add(aNode);
}
}
System.out.println("HttpCsvGraphReparent.MyResource.remove() end 1");
Collection<String> c = reduced.build();
System.out.println("HttpCsvGraphReparent.MyResource.remove() end 2");
return c;
}
private static Collection<String> getChildren(String iParentNode, String filepath) throws IOException {
System.out
.println("HttpCsvGraphReparent.MyResource.getChildren() begin");
List<String> children = new LinkedList<String>();
System.out.println("HttpCsvGraphReparent.MyResource.getChildren() 1");
List<String> lines;
try {
lines = getFileLines(filepath);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
System.out.println("HttpCsvGraphReparent.MyResource.getChildren() 2");
for (String line : lines) {
if (line.contains(iParentNode)) { // avoid expensive parsing
String[] relationship = new CSVParser(
new StringReader(line)).getLine();
String aNode = relationship[0];
System.out
.println("HttpCsvGraphReparent.MyResource.getChildren() aNode = " + aNode);
if (aNode.equals(iParentNode)) {
children.add(relationship[1]);
}
}
}
System.out
.println("HttpCsvGraphReparent.MyResource.getChildren() end");
return children;
}
private static String getParent(String iChildNode, String filepath) throws IOException {
System.out
.println("HttpCsvGraphReparent.MyResource.getParent()");
List<String> lines = getFileLines(filepath);
String parent = null;
for (String line : lines) {
if (line.contains(iChildNode)) { // avoid expensive parsing
String[] relationship = new CSVParser(
new StringReader(line)).getLine();
String aNode = relationship[1];
if (aNode.equals(iChildNode)) {
parent = relationship[0];
break;
}
}
}
if (parent == null) {
throw new RuntimeException("Developer Error : couldn't find original parent of node to be moved (so we can't determine the nodes' siblings)");
}
return parent;
}
private void reparent(String iNodeToReparent, String newParent, String heapFile)
throws IOException {
if (newParent == null) {
System.err.println("[DEBUG] 7: " + iNodeToReparent);
JSONObject jsonObject = new JSONObject();
jsonObject.put("error", "Found no new parent");
throw new RuntimeException("No new parent");
}
System.err.println("[DEBUG] 4: " + iNodeToReparent);
if (iNodeToReparent.endsWith("\"")) {
System.err.println("[DEBUG] 5: " + iNodeToReparent);
throw new RuntimeException("Developer Error 1 (I don't remember why this is invalid)");
}
List<String> beforeRemoval = getFileLines(heapFile);
List<String> afterRemoval = removeRelationshipWithParent(iNodeToReparent, beforeRemoval);
// System.err.println("[DEBUG] 7.5: " + iNodeToReparent);
int removed = beforeRemoval.size() - afterRemoval.size();
if (removed > 0) {
// System.err.println("[DEBUG] 8: will remove " + removed);
String s = "\""+newParent+"\",\"" +iNodeToReparent+ "\"";
List<String> lines = new LinkedList<String>();
lines.addAll(afterRemoval);
lines.add(s);
// System.err.println("[DEBUG] 8.5: checking for cycles ");
_checkForCycles : {
Map<String, String> childToParent = parseLinesAndReverse(lines);
// System.err.println("[DEBUG] 8.6");
// if (childToParent.size() < lines.size()) {
// System.err.println("[DEBUG] 8.7");
// // children must be unique, so if the same child appears twice,
// // the keyset only stores the last added one
// System.err.println("[ERROR] Cycle found");
// throw new RuntimeException("Cycle found");
// } else {
// System.err.println("[DEBUG] No cycles found");
// }
}
// System.err.println("[DEBUG] 9: " + s);
FileUtils.writeLines(Paths.get(filepath).toFile(), lines, false);
System.err.println("[DEBUG] Successfully removed from file: " + iNodeToReparent);
} else {
System.err.println("[DEBUG] 10: Couldn't find parent row for " + iNodeToReparent);
throw new RuntimeException("Nothing got removed");
}
}
private static Map<String, String> parseLinesAndReverse(List<String> lines) {
// System.err.println("[DEBUG] parseLinesAndReverse() - begin ");
Map<String, String> childToParent = new HashMap<String, String>();
// System.err.println("[DEBUG] parseLinesAndReverse() - 1");
Pattern p = Pattern.compile("\"(.*)\",\"(.*)\"");
// System.err.println("[DEBUG] parseLinesAndReverse() - 2");
for (String line : lines) {
// System.err.println("[DEBUG] parseLinesAndReverse() - 3");
Matcher m = p.matcher(line);
// System.err.println("[DEBUG] parseLinesAndReverse() - 4");
if (!m.find()) {
System.err.println("[DEBUG] parseLinesAndReverse() - 4.01 no match: " + line);
continue;
}
String parent = "";
try {
parent = m.group(1);
} catch (Exception e) {
System.err.println("[DEBUG] parseLinesAndReverse() - 4.1 " + line);
e.printStackTrace();
throw e;
}
// System.err.println("[DEBUG] parseLinesAndReverse() - 5");
String child = m.group(2);
// System.err.println("[DEBUG] parseLinesAndReverse() - 6");
// System.err.println("[DEBUG] parent = " + parent);
// System.err.println("[DEBUG] child = " + child);
if (childToParent.containsKey(child)) {
if (!childToParent.get(child).equals(parent)) {
System.err.println("[WARNING] duplicate line (ideally it shouldn't have): " + line);
System.err.println("[DEBUG] Child already has a parent: " + child);
System.err.println("[DEBUG] Parent is: " + childToParent.get(child));
System.err.println("[DEBUG] Attempted to change parent to: " + parent);
throw new RuntimeException("Child already has a parent " + child);
}
}
childToParent.put(child, parent);
}
// System.err.println("[DEBUG] parseLinesAndReverse() - end ");
return childToParent;
}
private static List<String> getFileLines(String filepath) throws IOException {
System.out
.println("HttpCsvGraphReparent.MyResource.getFileLines()");
try {
return ImmutableList.copyOf(FileUtils.readLines(new File(
filepath), Charset.defaultCharset()));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
private List<String> removeRelationshipWithParent(String iChildNodeToRemove,
List<String> beforeRemoval) {
System.out
.println("HttpCsvGraphReparent.MyResource.removeRelationshipWithParent() - " + iChildNodeToRemove);
List<String> lines = new LinkedList<String>();
lines.addAll(beforeRemoval);
try {
for (String line : beforeRemoval) {
//System.err.println("[DEBUG] 5.5: " + line);
if (line.endsWith("\"" + iChildNodeToRemove + "\"")) {
System.err.println("[DEBUG] 6: " + iChildNodeToRemove);
lines.remove(line);
// System.err.println("[DEBUG] 6.5: " + iChildNodeToRemove);
}
}
} catch (Exception e) {
e.printStackTrace();
}
List<String> afterRemoval = ImmutableList.copyOf(lines);
return afterRemoval;
}
}
@SuppressWarnings("unused")
public static void main(String[] args) throws URISyntaxException,
IOException, KeyManagementException, UnrecoverableKeyException,
NoSuchAlgorithmException, KeyStoreException, CertificateException,
InterruptedException {
String port = null;
_parseOptions: {
Options options = new Options().addOption("h", "help", false,
"show help.").addOption("f", "file", true,
"use FILE to write incoming data to");
// This doesn't work with java 7
// "hasarg" is needed when the option takes a value
options.addOption(Option.builder("p").longOpt("port").hasArg()
.required().build());
try {
CommandLine cmd = new DefaultParser().parse(options, args);
port = cmd.getOptionValue("p", "4444");
if (cmd.hasOption("h")) {
// This prints out some help
HelpFormatter formater = new HelpFormatter();
formater.printHelp("httpgrep.groovy", options);
System.exit(0);
}
} catch (ParseException e) {
e.printStackTrace();
System.exit(-1);
}
}
try {
String url = "http://localhost:" + port + "/";
JdkHttpServerFactory.createHttpServer(new URI(url),
new ResourceConfig(MyResource.class));
} catch (Exception e) {
e.printStackTrace();
System.err.println("Port already listened on.");
System.exit(-1);
}
}
}