1 /*******************************************************************************
2 * Copyright (c) 2012, 2017 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - e.g. #7016
12 *******************************************************************************/
13 package org.simantics.graph.db;
15 import java.io.DataInput;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.HashSet;
24 import java.util.TreeMap;
26 import org.simantics.databoard.Bindings;
27 import org.simantics.databoard.adapter.AdaptException;
28 import org.simantics.databoard.binding.Binding;
29 import org.simantics.databoard.binding.mutable.Variant;
30 import org.simantics.databoard.serialization.Serializer;
31 import org.simantics.databoard.type.Datatype;
32 import org.simantics.databoard.util.URIStringUtils;
33 import org.simantics.db.ReadGraph;
34 import org.simantics.db.Resource;
35 import org.simantics.db.Session;
36 import org.simantics.db.VirtualGraph;
37 import org.simantics.db.WriteOnlyGraph;
38 import org.simantics.db.common.WriteBindings;
39 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
40 import org.simantics.db.common.request.PossibleIndexRoot;
41 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
42 import org.simantics.db.common.utils.Logger;
43 import org.simantics.db.exception.DatabaseException;
44 import org.simantics.db.service.ClusterBuilder2;
45 import org.simantics.db.service.ClusterBuilderFactory;
46 import org.simantics.db.service.ClusteringSupport;
47 import org.simantics.db.service.SerialisationSupport;
48 import org.simantics.db.service.XSupport;
49 import org.simantics.graph.db.TransferableGraphSource.TransferableGraphSourceProcedure;
50 import org.simantics.graph.db.TransferableGraphSource.TransferableGraphSourceValueProcedure;
51 import org.simantics.graph.representation.Extensions;
52 import org.simantics.graph.representation.External;
53 import org.simantics.graph.representation.Identity;
54 import org.simantics.graph.representation.IdentityDefinition;
55 import org.simantics.graph.representation.Internal;
56 import org.simantics.graph.representation.Optional;
57 import org.simantics.graph.representation.Root;
58 import org.simantics.graph.representation.TransferableGraphUtils;
59 import org.simantics.graph.utils.TGResourceUtil;
60 import org.simantics.graph.utils.TGResourceUtil.LongAdapter;
61 import org.simantics.utils.datastructures.Pair;
62 import org.slf4j.LoggerFactory;
64 import gnu.trove.map.TIntObjectMap;
65 import gnu.trove.map.hash.TIntObjectHashMap;
67 public class StreamingTransferableGraphImportProcess implements TransferableGraphImporter {
69 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(StreamingTransferableGraphImportProcess.class);
72 TransferableGraphSource tg;
74 IImportAdvisor2 advisor;
75 TGStatusMonitor monitor;
76 ClusterBuilder2 builder;
77 final TGResourceUtil resourceUtil = new TGResourceUtil();
81 Map<String,Integer> allMissingExternals = new HashMap<>();
82 Set<String> missingExternals = new HashSet<>();
83 Map<String,Resource> resolvedParents = new HashMap<>();
84 TIntObjectHashMap<Resource> existingInternalMap = new TIntObjectHashMap<>();
87 Identity[] identities;
88 TreeMap<String, Variant> extensions;
93 Resource ExternalEntity;
102 public StreamingTransferableGraphImportProcess(Session session, VirtualGraph vg, TransferableGraphSource tg, IImportAdvisor2 advisor) {
103 this(session, vg, tg, advisor, null);
106 public StreamingTransferableGraphImportProcess(Session session, VirtualGraph vg, TransferableGraphSource tg, IImportAdvisor2 advisor, TGStatusMonitor monitor) {
109 this.advisor = advisor;
110 this.monitor = monitor;
113 private int updatePercentage(int percentage, int done, int total) {
114 if (monitor != null && (done & 63) == 0) {
115 int current = 100*done / total;
116 if (current > percentage) {
117 percentage = current;
118 monitor.status(percentage);
124 public void readIdentities(ReadGraph g) throws Exception {
125 extensions = tg.getExtensions();
126 resourceCount = tg.getResourceCount();
127 identities = new Identity[tg.getIdentityCount()];
128 tg.forIdentities(g, new TransferableGraphSourceProcedure<Identity>() {
133 public void execute(Identity value) throws Exception {
134 identities[counter++] = value;
139 public void findBuiltins(WriteOnlyGraph g) throws DatabaseException {
140 RootLibrary = g.getBuiltin("http:/");
141 String = g.getBuiltin(CoreInitialization.LAYER0 + "String");
142 Library = g.getBuiltin(CoreInitialization.LAYER0 + "Library");
143 InstanceOf = g.getBuiltin(CoreInitialization.LAYER0 + "InstanceOf");
144 ConsistsOf = g.getBuiltin(CoreInitialization.LAYER0 + "ConsistsOf");
145 PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf");
146 HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName");
147 NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf");
148 ExternalEntity = g.getBuiltin(CoreInitialization.LAYER0 + "ExternalEntity");
151 public void findBuiltins(ReadGraph g) throws DatabaseException {
152 RootLibrary = g.getBuiltin("http:/");
153 String = g.getBuiltin(CoreInitialization.LAYER0 + "String");
154 Library = g.getBuiltin(CoreInitialization.LAYER0 + "Library");
155 InstanceOf = g.getBuiltin(CoreInitialization.LAYER0 + "InstanceOf");
156 ConsistsOf = g.getBuiltin(CoreInitialization.LAYER0 + "ConsistsOf");
157 PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf");
158 HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName");
159 NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf");
160 ExternalEntity = g.getBuiltin(CoreInitialization.LAYER0 + "ExternalEntity");
163 void addMissing(int handleIndex, String external) {
164 allMissingExternals.put(external, handleIndex);
165 Set<String> removals = new HashSet<>();
166 for(String ext : missingExternals) if(ext.startsWith(external + "/")) return;
167 for(String ext : missingExternals) if(external.startsWith(ext + "/")) removals.add(ext);
168 missingExternals.removeAll(removals);
169 missingExternals.add(external);
172 void prepare(ReadGraph graph) throws Exception {
174 Resource target = advisor.getTarget();
176 indexRoot = graph.syncRequest(new PossibleIndexRoot(target));
179 readIdentities(graph);
181 // System.err.println("ext: " + extensions);
182 // System.err.println("rc: " + resourceCount);
183 // System.err.println("ic: " + identities.length);
185 ClusterBuilderFactory factory = graph.getService(ClusterBuilderFactory.class);
186 ClusterBuilder2 builder = factory.create(vg, false);
188 this.handles = new int[resourceCount];
189 TIntObjectMap<Identity> identityMap = TransferableGraphUtils.mapIdentities(identities);
191 for(Identity identity : identities) {
192 IdentityDefinition definition = identity.definition;
193 if(definition instanceof External) {
194 External def = (External)definition;
195 if(def.parent == -1) {
196 handles[identity.resource] = builder.handle(RootLibrary);
198 if("@inverse".equals(def.name)) {
199 int parent = handles[def.parent];
200 int child = builder.handle(graph.getInverse(builder.resource(parent)));
201 handles[identity.resource] = child;
203 int handle = handles[def.parent];
204 Resource parent = handle != 0 ? builder.resource(handle) : null;
205 // TODO: escape should be removed when names become well-behaving
207 resolvedParents.put(graph.getURI(parent), parent);
208 Map<String,Resource> childMap = graph
209 .syncRequest(new UnescapedChildMapOfResource(parent),
210 TransientCacheAsyncListener.instance());
211 Resource child = childMap.get(def.name);
213 addMissing(identity.resource, graph.getURI(parent) + "/" + URIStringUtils.escape(def.name));
215 handles[identity.resource] = builder.handle(child);
218 addMissing(identity.resource, TransferableGraphUtils.getURI(resourceCount, identityMap, def.parent) + "/" + URIStringUtils.escape(def.name));
223 else if(definition instanceof Internal) {
224 String uri = TransferableGraphUtils.getURI(resourceCount, identityMap, identity.resource);
225 Resource existing = graph.getPossibleResource(uri);
226 if(existing != null) {
227 existingInternalMap.put(identity.resource, existing);
230 else if(definition instanceof Root) {
231 Root root = (Root)definition;
232 if(root.name.equals(""))
233 handles[identity.resource] = builder.handle(RootLibrary);
235 Resource existing = advisor.analyzeRoot(graph, root);
237 handles[identity.resource] = builder.handle(existing);
240 else if(definition instanceof Optional) {
241 External def = (External)definition;
242 Resource parent = builder.resource(handles[def.parent]);
244 handles[identity.resource] = builder.handle(graph.syncRequest(new UnescapedChildMapOfResource(parent)).get(def.name));
248 if (!missingExternals.isEmpty() && failOnMissingEntities())
249 throw new MissingDependencyException(this);
252 private boolean failOnMissingEntities() {
253 return "true".equalsIgnoreCase(
255 "org.simantics.tg.import.failOnMissingEntities",
260 public Resource createChild(WriteOnlyGraph graph, Resource parent, Resource child, String name) throws DatabaseException {
261 //System.err.println("child " + parent + " - " + child + " = " + name);
262 if(child == null) child = graph.newResource();
263 Resource nameResource = graph.newResource();
264 graph.claim(nameResource, InstanceOf, null, String);
265 graph.claimValue(nameResource, name, WriteBindings.STRING);
266 graph.claim(child, HasName, NameOf, nameResource);
267 graph.claim(parent, ConsistsOf, PartOf, child);
271 int[] getClustering() {
272 Variant v = extensions.get(Extensions.CLUSTERING);
273 if(v == null) return null;
275 return (int[])v.getValue(Bindings.INT_ARRAY);
276 } catch (AdaptException e) {
277 Logger.defaultLogError(e);
282 int[] getClusterSets() {
283 Variant v = extensions.get(Extensions.CLUSTER_SETS);
284 if(v == null) return null;
286 return (int[])v.getValue(Bindings.INT_ARRAY);
287 } catch (AdaptException e) {
288 Logger.defaultLogError(e);
293 boolean needTranslation(Datatype type) {
294 return resourceUtil.mayHaveResource(type);
297 void findClusterSet(WriteOnlyGraph graph, Resource rootLibrary, int[] clustering, int[] clusterSets, long[] clusters, int id) throws DatabaseException {
298 ClusteringSupport support = graph.getService(ClusteringSupport.class);
299 if(id == Extensions.ROOT_LIBRARY_CLUSTER_SET || id == Extensions.INDEX_ROOT_CLUSTER_SET) return;
300 Resource indexRootClusterSetResource = rootLibrary;
301 if(indexRoot != null && support.isClusterSet(indexRoot)) {
302 indexRootClusterSetResource = indexRoot;
304 graph.setClusterSet4NewResource(rootLibrary);
305 graph.flushCluster();
307 int indexRootCsHandle = builder.handle(indexRootClusterSetResource);
308 for(int pos=0,index=0;index<clustering.length;index++) {
309 pos += clustering[index];
311 int cs = clusterSets[index];
312 if(handles[id] == 0) {
314 if(cs == Extensions.ROOT_LIBRARY_CLUSTER_SET) csHandle = builder.handle(rootLibrary);
315 else if(cs == Extensions.INDEX_ROOT_CLUSTER_SET) {
316 if(indexRoot == null) throw new DatabaseException("No index root was available in TG import.");
317 csHandle = indexRootCsHandle;
320 findClusterSet(graph, rootLibrary, clustering, clusterSets, clusters, cs);
321 csHandle = handles[cs];
324 if(clusters[index] != 0)
325 builder.selectCluster(clusters[index]);
327 builder.newCluster(csHandle);
329 handles[id] = builder.newResource(csHandle);
330 clusters[index] = support.getCluster(builder.resource(handles[id]));
332 builder.createClusterSet(handles[id]);
339 void createMissing(final WriteOnlyGraph graph) throws Exception {
341 if(allMissingExternals.isEmpty()) return;
343 XSupport xs = graph.getService(XSupport.class);
344 Pair<Boolean,Boolean> serviceMode = xs.getServiceMode();
345 xs.setServiceMode(true, false);
347 ArrayList<String> missing = new ArrayList<>(allMissingExternals.keySet());
348 Collections.sort(missing);
349 for(String uri : missing) {
350 String[] parts = URIStringUtils.splitURI(uri);
351 // URIStringUtils.splitURI returns root URI in non-standard format, so fix it manually as a workaround
352 if (parts[0].equals("http://")) {
356 Resource parent = resolvedParents.get(parts[0]);
357 // TODO: proper exception message
359 throw new IllegalStateException("Missing URI: " + uri);
362 Resource childResource = graph.newResource();
363 graph.claim(childResource, InstanceOf, null, ExternalEntity);
365 Resource nameResource = graph.newResource();
366 graph.claim(nameResource, InstanceOf, null, String);
367 graph.claimValue(nameResource, URIStringUtils.unescape(parts[1]), WriteBindings.STRING);
368 graph.claim(childResource, HasName, NameOf, nameResource);
370 graph.claim(parent, ConsistsOf, PartOf, childResource);
372 resolvedParents.put(uri, childResource);
374 handles[allMissingExternals.get(uri)] = builder.handle(childResource);
377 xs.setServiceMode(serviceMode.first, serviceMode.second);
381 void write(final WriteOnlyGraph graph) throws Exception {
383 final SerialisationSupport ss = graph.getService(SerialisationSupport.class);
385 ClusterBuilderFactory factory = graph.getService(ClusterBuilderFactory.class);
386 if(advisor instanceof IImportAdvisor2) {
387 boolean allowImmutable = ((IImportAdvisor2)advisor).allowImmutableModifications();
388 builder = factory.create(vg, allowImmutable);
390 builder = factory.create(vg, false);
393 createMissing(graph);
395 final int[] handles = this.handles;
397 int[] clustering = getClustering();
398 if(clustering != null) {
400 int[] clusterSets = getClusterSets();
401 if(clusterSets != null) {
403 assert(clustering.length == clusterSets.length);
405 long[] clusters = new long[clustering.length];
408 for(int i=0;i<clusterSets.length;i++) {
409 findClusterSet(graph, graph.getRootLibrary(), clustering, clusterSets, clusters, clusterSets[i]);
412 // Then create all resources
414 for(int j=0;j<clustering.length;j++) {
415 int c = clustering[j];
416 int s = clusterSets[j];
418 if(s == Extensions.ROOT_LIBRARY_CLUSTER_SET)
419 setHandle = builder.handle(graph.getRootLibrary());
420 else if(s == Extensions.INDEX_ROOT_CLUSTER_SET) {
421 if(indexRoot == null) throw new DatabaseException("No index root was available in TG import.");
422 setHandle = builder.handle(indexRoot);
424 else setHandle = handles[s];
425 // Preserve clustering only for internal resources
427 builder.selectCluster(clusters[j]);
429 builder.newCluster(setHandle);
430 for(int r=0;r<c;r++, i++)
431 if(handles[i] == 0) handles[i] = builder.newResource();
434 for(;i<handles.length;++i)
435 if(handles[i] == 0) handles[i] = builder.newResource();
440 for(int c : clustering) {
441 builder.newCluster();
442 for(int r=0;r<c;r++, i++)
443 if(handles[i] == 0) handles[i] = builder.newResource();
446 for(;i<handles.length;++i)
447 if(handles[i] == 0) handles[i] = builder.newResource();
453 // Create blank resources
454 for(int i=0;i<handles.length;++i)
455 if(handles[i] == 0) handles[i] = builder.newResource();
459 // Internal identities
460 for(Identity identity : identities) {
461 IdentityDefinition definition = identity.definition;
462 // if(handles[identity.resource] != 0)
464 if(definition instanceof External) {
465 // Already done everything
467 else if(definition instanceof Internal) {
468 Internal def = (Internal)definition;
469 Resource external = existingInternalMap.get(identity.resource);
470 if(external != null) {
471 handles[identity.resource] = builder.handle(external);
473 if(handles[identity.resource] != 0)
474 handles[identity.resource] = builder.handle(advisor.createChild(graph, this, builder.resource(handles[def.parent]), builder.resource(handles[identity.resource]), def.name));
476 handles[identity.resource] = builder.handle(advisor.createChild(graph, this, builder.resource(handles[def.parent]), null, def.name));
480 else if(definition instanceof Root) {
482 Root root = (Root)definition;
483 if(handles[identity.resource] != 0)
484 handles[identity.resource] = builder.handle(advisor.createRoot(graph, root, builder.resource(handles[identity.resource])));
486 handles[identity.resource] = builder.handle(advisor.createRoot(graph, root, null));
488 else if(definition instanceof Optional) {
489 Optional def = (Optional)definition;
490 if(handles[identity.resource] != 0) {
491 Resource child = advisor.createChild(graph, this, builder.resource(handles[def.parent]), builder.resource(handles[identity.resource]), def.name);
492 graph.claim(child, InstanceOf, null, Library); // ???
493 handles[identity.resource] = builder.handle(child);
495 Resource child = advisor.createChild(graph, this, builder.resource(handles[def.parent]), null, def.name);
496 graph.claim(child, InstanceOf, null, Library); // ???
497 handles[identity.resource] = builder.handle(child);
503 int[] percentage = { 0 };
505 int statementCount = tg.getStatementCount();
506 tg.forStatements(null, new TransferableGraphSourceProcedure<int[]>() {
509 public void execute(int[] value) throws Exception {
516 int subject = handles[sub];
517 int predicate = handles[pred];
518 int object = handles[obj];
520 builder.addStatement(graph, subject, predicate, object);
522 int inverse = handles[inv];
523 builder.addStatement(graph, object, inverse, subject);
526 // Count from 0% -> 50% => total = statementCount*2
527 percentage[0] = updatePercentage(percentage[0], done[0]++, statementCount*2);
533 int valueCount = tg.getValueCount();
536 class ValueProcedure extends InputStream implements TransferableGraphSourceValueProcedure {
538 private TGResourceUtil util = new TGResourceUtil();
539 private DataInput source;
542 public void execute(int _resource, Datatype type, DataInput stream) throws Exception {
546 //int file = _resource & 0x80000000;
547 int resource = _resource & 0x7FFFFFFF;
549 Binding binding = Bindings.getBinding(type);
550 Serializer s = Bindings.getSerializer(binding);
552 builder.beginValue(handles[resource]);
553 if(util.mayHaveResource(type)) {
554 Object value = s.deserialize(stream);
555 util.adaptValue( binding, value, new LongAdapter() {
557 public long adapt(long in) {
559 return ss.getRandomAccessId(handles[(int)in]);
560 } catch (DatabaseException e) {
561 throw new IllegalStateException(e);
565 byte[] bytes = s.serialize(value);
566 for(byte b : bytes) {
568 if(val < 0) val += 256;
569 builder.appendValue(val);
580 public int read() throws IOException {
581 int value = source.readUnsignedByte();
583 builder.appendValue(value);
584 } catch (DatabaseException e) {
585 LOGGER.error("Failed to write value into database", e);
591 public void rawCopy(int resource, int length, DataInput input) throws Exception {
592 builder.beginValue(handles[resource]);
593 for (int i = 0; i < length; ++i)
594 builder.appendValue(input.readUnsignedByte());
599 private void work() {
600 // Count from 50% -> 100% => [valueCount, valueCount*2)
601 percentage[0] = updatePercentage(percentage[0], valueCount + done[0]++, valueCount*2);
605 tg.forValues2(null, new ValueProcedure());
607 for(Resource r : existingInternalMap.valueCollection()) {
608 graph.deny(r, InstanceOf, null, ExternalEntity, null);
614 public long[] getResourceIds(SerialisationSupport serializer) throws DatabaseException {
615 final int count = handles.length;
616 long[] resourceIds = new long[count];
617 for(int i=0;i<count;++i)
618 resourceIds[i] = serializer.getRandomAccessId(handles[i]);