-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamAggregate.java
More file actions
74 lines (68 loc) · 3.21 KB
/
Copy pathStreamAggregate.java
File metadata and controls
74 lines (68 loc) · 3.21 KB
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
// Constant-memory aggregation over a huge FundsXML file via StAX (native Java,
// no JAXB, no DOM). Pull parser — never holds more than the current element.
//
// Standalone & cross-platform — run from the repo root with the Maven Wrapper:
// ./mvnw -q -pl Large_File_Processing/java compile exec:java \
// -Dexec.args="<fundsxml.xml>"
// (Memory-bounded: set a small heap via MAVEN_OPTS=-Xmx64m to prove it.)
//
// FundsXML 4.x has no XML namespace — match the bare "Position" local name.
// Security: external entities / DTDs disabled (XXE-safe).
import java.io.FileInputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
public class StreamAggregate {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("usage: StreamAggregate <fundsxml.xml>");
System.exit(2);
}
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
long n = 0;
double sumValue = 0, sumPct = 0;
boolean inPosition = false, inTotalValue = false;
String currentLeaf = null;
try (FileInputStream in = new FileInputStream(args[0])) {
XMLStreamReader r = xif.createXMLStreamReader(in);
while (r.hasNext()) {
switch (r.next()) {
case XMLStreamConstants.START_ELEMENT: {
String name = r.getLocalName();
if ("Position".equals(name)) { inPosition = true; n++; }
else if ("TotalValue".equals(name)) inTotalValue = true;
currentLeaf = name;
break;
}
case XMLStreamConstants.CHARACTERS: {
if (!inPosition || r.isWhiteSpace()) break;
String t = r.getText().trim();
if (t.isEmpty()) break;
if (inTotalValue && "Amount".equals(currentLeaf))
sumValue += Double.parseDouble(t);
else if ("TotalPercentage".equals(currentLeaf))
sumPct += Double.parseDouble(t);
break;
}
case XMLStreamConstants.END_ELEMENT: {
String name = r.getLocalName();
if ("Position".equals(name)) inPosition = false;
else if ("TotalValue".equals(name)) inTotalValue = false;
currentLeaf = null;
break;
}
default:
}
}
r.close();
}
Runtime rt = Runtime.getRuntime();
long usedMiB = (rt.totalMemory() - rt.freeMemory()) / (1024 * 1024);
System.out.printf("positions : %d%n", n);
System.out.printf("sum value (EUR): %.2f%n", sumValue);
System.out.printf("sum percentage : %.4f%n", sumPct);
System.out.printf("heap in use : %d MiB%n", usedMiB);
}
}