use of apoc.export.util.BatchTransaction in project neo4j-apoc-procedures by neo4j-contrib.
the class XmlGraphMLReader method parseXML.
public long parseXML(Reader input) throws XMLStreamException {
Map<String, Long> cache = new HashMap<>(1024 * 32);
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty("javax.xml.stream.isCoalescing", true);
XMLEventReader reader = inputFactory.createXMLEventReader(input);
PropertyContainer last = null;
Map<String, Key> nodeKeys = new HashMap<>();
Map<String, Key> relKeys = new HashMap<>();
int count = 0;
try (BatchTransaction tx = new BatchTransaction(gdb, batchSize * 10, reporter)) {
while (reader.hasNext()) {
XMLEvent event = (XMLEvent) reader.next();
if (event.isStartElement()) {
StartElement element = event.asStartElement();
String name = element.getName().getLocalPart();
if (name.equals("graphml") || name.equals("graph"))
continue;
if (name.equals("key")) {
String id = getAttribute(element, ID);
Key key = new Key(id, getAttribute(element, NAME), getAttribute(element, TYPE), getAttribute(element, LIST), getAttribute(element, FOR));
XMLEvent next = peek(reader);
if (next.isStartElement() && next.asStartElement().getName().getLocalPart().equals("default")) {
reader.nextEvent().asStartElement();
key.setDefault(reader.nextEvent().asCharacters().getData());
}
if (key.forNode)
nodeKeys.put(id, key);
else
relKeys.put(id, key);
continue;
}
if (name.equals("data")) {
if (last == null)
continue;
String id = getAttribute(element, KEY);
boolean isNode = last instanceof Node;
Key key = isNode ? nodeKeys.get(id) : relKeys.get(id);
if (key == null)
key = Key.defaultKey(id, isNode);
Object value = key.defaultValue;
XMLEvent next = peek(reader);
if (next.isCharacters()) {
value = key.parseValue(reader.nextEvent().asCharacters().getData());
}
if (value != null) {
if (this.labels && isNode && id.equals("labels")) {
addLabels((Node) last, value.toString());
} else if (!this.labels || isNode || !id.equals("label")) {
last.setProperty(key.name, value);
if (reporter != null)
reporter.update(0, 0, 1);
}
}
continue;
}
if (name.equals("node")) {
tx.increment();
String id = getAttribute(element, ID);
Node node = gdb.createNode();
if (this.labels) {
String labels = getAttribute(element, LABELS);
addLabels(node, labels);
}
if (storeNodeIds)
node.setProperty("id", id);
setDefaults(nodeKeys, node);
last = node;
cache.put(id, node.getId());
if (reporter != null)
reporter.update(1, 0, 0);
count++;
continue;
}
if (name.equals("edge")) {
tx.increment();
String source = getAttribute(element, SOURCE);
String target = getAttribute(element, TARGET);
String label = getAttribute(element, LABEL);
Node from = gdb.getNodeById(cache.get(source));
Node to = gdb.getNodeById(cache.get(target));
RelationshipType type = label != null ? RelationshipType.withName(label) : defaultRelType;
Relationship relationship = from.createRelationshipTo(to, type);
setDefaults(relKeys, relationship);
if (reporter != null)
reporter.update(0, 1, 0);
count++;
last = relationship;
}
}
}
}
return count;
}
Aggregations