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 ClusterBuilder2 builder;
76 final TGResourceUtil resourceUtil = new TGResourceUtil();
80 Map<String,Integer> allMissingExternals = new HashMap<>();
81 Set<String> missingExternals = new HashSet<>();
82 Map<String,Resource> resolvedParents = new HashMap<>();
83 TIntObjectHashMap<Resource> existingInternalMap = new TIntObjectHashMap<>();
86 Identity[] identities;
87 TreeMap<String, Variant> extensions;
92 Resource ExternalEntity;
101 public StreamingTransferableGraphImportProcess(Session session, VirtualGraph vg, TransferableGraphSource tg, IImportAdvisor2 advisor) {
104 this.advisor = advisor;
107 public void readIdentities(ReadGraph g) throws Exception {
108 extensions = tg.getExtensions();
109 resourceCount = tg.getResourceCount();
110 identities = new Identity[tg.getIdentityCount()];
111 tg.forIdentities(g, new TransferableGraphSourceProcedure<Identity>() {
116 public void execute(Identity value) throws Exception {
117 identities[counter++] = value;
122 public void findBuiltins(WriteOnlyGraph g) throws DatabaseException {
123 RootLibrary = g.getBuiltin("http:/");
124 String = g.getBuiltin(CoreInitialization.LAYER0 + "String");
125 Library = g.getBuiltin(CoreInitialization.LAYER0 + "Library");
126 InstanceOf = g.getBuiltin(CoreInitialization.LAYER0 + "InstanceOf");
127 ConsistsOf = g.getBuiltin(CoreInitialization.LAYER0 + "ConsistsOf");
128 PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf");
129 HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName");
130 NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf");
131 ExternalEntity = g.getBuiltin(CoreInitialization.LAYER0 + "ExternalEntity");
134 public void findBuiltins(ReadGraph g) throws DatabaseException {
135 RootLibrary = g.getBuiltin("http:/");
136 String = g.getBuiltin(CoreInitialization.LAYER0 + "String");
137 Library = g.getBuiltin(CoreInitialization.LAYER0 + "Library");
138 InstanceOf = g.getBuiltin(CoreInitialization.LAYER0 + "InstanceOf");
139 ConsistsOf = g.getBuiltin(CoreInitialization.LAYER0 + "ConsistsOf");
140 PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf");
141 HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName");
142 NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf");
143 ExternalEntity = g.getBuiltin(CoreInitialization.LAYER0 + "ExternalEntity");
146 void addMissing(int handleIndex, String external) {
147 allMissingExternals.put(external, handleIndex);
148 Set<String> removals = new HashSet<>();
149 for(String ext : missingExternals) if(ext.startsWith(external)) return;
150 for(String ext : missingExternals) if(external.startsWith(ext)) removals.add(ext);
151 missingExternals.removeAll(removals);
152 missingExternals.add(external);
155 void prepare(ReadGraph graph) throws Exception {
157 Resource target = advisor.getTarget();
159 indexRoot = graph.syncRequest(new PossibleIndexRoot(target));
162 readIdentities(graph);
164 // System.err.println("ext: " + extensions);
165 // System.err.println("rc: " + resourceCount);
166 // System.err.println("ic: " + identities.length);
168 ClusterBuilderFactory factory = graph.getService(ClusterBuilderFactory.class);
169 ClusterBuilder2 builder = factory.create(vg, false);
171 this.handles = new int[resourceCount];
172 TIntObjectMap<Identity> identityMap = TransferableGraphUtils.mapIdentities(identities);
174 for(Identity identity : identities) {
175 IdentityDefinition definition = identity.definition;
176 if(definition instanceof External) {
177 External def = (External)definition;
178 if(def.parent == -1) {
179 handles[identity.resource] = builder.handle(RootLibrary);
181 if("@inverse".equals(def.name)) {
182 int parent = handles[def.parent];
183 int child = builder.handle(graph.getInverse(builder.resource(parent)));
184 handles[identity.resource] = child;
186 int handle = handles[def.parent];
187 Resource parent = handle != 0 ? builder.resource(handle) : null;
188 // TODO: escape should be removed when names become well-behaving
190 resolvedParents.put(graph.getURI(parent), parent);
191 Map<String,Resource> childMap = graph
192 .syncRequest(new UnescapedChildMapOfResource(parent),
193 TransientCacheAsyncListener.instance());
194 Resource child = childMap.get(def.name);
196 addMissing(identity.resource, graph.getURI(parent) + "/" + def.name);
198 handles[identity.resource] = builder.handle(child);
201 addMissing(identity.resource, TransferableGraphUtils.getURI(resourceCount, identityMap, def.parent) + "/" + def.name);
206 else if(definition instanceof Internal) {
207 String uri = TransferableGraphUtils.getURI(resourceCount, identityMap, identity.resource);
208 Resource existing = graph.getPossibleResource(uri);
209 if(existing != null) {
210 existingInternalMap.put(identity.resource, existing);
213 else if(definition instanceof Root) {
214 Root root = (Root)definition;
215 if(root.name.equals(""))
216 handles[identity.resource] = builder.handle(RootLibrary);
218 Resource existing = advisor.analyzeRoot(graph, root);
220 handles[identity.resource] = builder.handle(existing);
223 else if(definition instanceof Optional) {
224 External def = (External)definition;
225 Resource parent = builder.resource(handles[def.parent]);
227 handles[identity.resource] = builder.handle(graph.syncRequest(new UnescapedChildMapOfResource(parent)).get(def.name));
231 //if(!missingExternals.isEmpty()) throw new MissingDependencyException(this);
236 public Resource createChild(WriteOnlyGraph graph, Resource parent, Resource child, String name) throws DatabaseException {
237 if(child == null) child = graph.newResource();
238 Resource nameResource = graph.newResource();
239 graph.claim(nameResource, InstanceOf, null, String);
240 graph.claimValue(nameResource, name, WriteBindings.STRING);
241 graph.claim(child, HasName, NameOf, nameResource);
245 int[] getClustering() {
246 Variant v = extensions.get(Extensions.CLUSTERING);
247 if(v == null) return null;
249 return (int[])v.getValue(Bindings.INT_ARRAY);
250 } catch (AdaptException e) {
251 Logger.defaultLogError(e);
256 int[] getClusterSets() {
257 Variant v = extensions.get(Extensions.CLUSTER_SETS);
258 if(v == null) return null;
260 return (int[])v.getValue(Bindings.INT_ARRAY);
261 } catch (AdaptException e) {
262 Logger.defaultLogError(e);
267 boolean needTranslation(Datatype type) {
268 return resourceUtil.mayHaveResource(type);
271 void findClusterSet(WriteOnlyGraph graph, Resource rootLibrary, int[] clustering, int[] clusterSets, long[] clusters, int id) throws DatabaseException {
272 ClusteringSupport support = graph.getService(ClusteringSupport.class);
273 if(id == Extensions.ROOT_LIBRARY_CLUSTER_SET || id == Extensions.INDEX_ROOT_CLUSTER_SET) return;
274 for(int pos=0,index=0;index<clustering.length;index++) {
275 pos += clustering[index];
277 int cs = clusterSets[index];
278 if(handles[id] == 0) {
280 if(cs == Extensions.ROOT_LIBRARY_CLUSTER_SET) csHandle = builder.handle(rootLibrary);
281 else if(cs == Extensions.INDEX_ROOT_CLUSTER_SET) {
282 if(indexRoot == null) throw new DatabaseException("No index root was available in TG import.");
283 csHandle = builder.handle(indexRoot);
286 findClusterSet(graph, rootLibrary, clustering, clusterSets, clusters, cs);
287 csHandle = handles[cs];
290 if(clusters[index] != 0)
291 builder.selectCluster(clusters[index]);
293 builder.newCluster(csHandle);
295 handles[id] = builder.newResource(csHandle);
296 clusters[index] = support.getCluster(builder.resource(handles[id]));
298 builder.createClusterSet(handles[id]);
305 void createMissing(final WriteOnlyGraph graph) throws Exception {
307 if(allMissingExternals.isEmpty()) return;
309 XSupport xs = graph.getService(XSupport.class);
310 Pair<Boolean,Boolean> serviceMode = xs.getServiceMode();
311 xs.setServiceMode(true, false);
313 ArrayList<String> missing = new ArrayList<>(allMissingExternals.keySet());
314 Collections.sort(missing);
315 for(String uri : missing) {
316 String[] parts = URIStringUtils.splitURI(uri);
318 Resource parent = resolvedParents.get(parts[0]);
319 // TODO: proper exception message
320 if(parent == null) throw new IllegalStateException("!!");
322 Resource childResource = graph.newResource();
323 graph.claim(childResource, InstanceOf, null, ExternalEntity);
325 Resource nameResource = graph.newResource();
326 graph.claim(nameResource, InstanceOf, null, String);
327 graph.claimValue(nameResource, parts[1], WriteBindings.STRING);
328 graph.claim(childResource, HasName, NameOf, nameResource);
330 graph.claim(parent, ConsistsOf, PartOf, childResource);
332 resolvedParents.put(uri, childResource);
334 handles[allMissingExternals.get(uri)] = builder.handle(childResource);
337 xs.setServiceMode(serviceMode.first, serviceMode.second);
341 void write(final WriteOnlyGraph graph) throws Exception {
343 final SerialisationSupport ss = graph.getService(SerialisationSupport.class);
345 ClusterBuilderFactory factory = graph.getService(ClusterBuilderFactory.class);
346 if(advisor instanceof IImportAdvisor2) {
347 boolean allowImmutable = ((IImportAdvisor2)advisor).allowImmutableModifications();
348 builder = factory.create(vg, allowImmutable);
350 builder = factory.create(vg, false);
353 createMissing(graph);
355 final int[] handles = this.handles;
357 int[] clustering = getClustering();
358 if(clustering != null) {
360 int[] clusterSets = getClusterSets();
361 if(clusterSets != null) {
363 assert(clustering.length == clusterSets.length);
365 long[] clusters = new long[clustering.length];
368 for(int i=0;i<clusterSets.length;i++) {
369 findClusterSet(graph, graph.getRootLibrary(), clustering, clusterSets, clusters, clusterSets[i]);
372 // Then create all resources
374 for(int j=0;j<clustering.length;j++) {
375 int c = clustering[j];
376 int s = clusterSets[j];
378 if(s == Extensions.ROOT_LIBRARY_CLUSTER_SET)
379 setHandle = builder.handle(graph.getRootLibrary());
380 else if(s == Extensions.INDEX_ROOT_CLUSTER_SET) {
381 if(indexRoot == null) throw new DatabaseException("No index root was available in TG import.");
382 setHandle = builder.handle(indexRoot);
384 else setHandle = handles[s];
385 // Preserve clustering only for internal resources
387 builder.selectCluster(clusters[j]);
389 builder.newCluster(setHandle);
390 for(int r=0;r<c;r++, i++)
391 if(handles[i] == 0) handles[i] = builder.newResource();
394 for(;i<handles.length;++i)
395 if(handles[i] == 0) handles[i] = builder.newResource();
400 for(int c : clustering) {
401 builder.newCluster();
402 for(int r=0;r<c;r++, i++)
403 if(handles[i] == 0) handles[i] = builder.newResource();
406 for(;i<handles.length;++i)
407 if(handles[i] == 0) handles[i] = builder.newResource();
413 // Create blank resources
414 for(int i=0;i<handles.length;++i)
415 if(handles[i] == 0) handles[i] = builder.newResource();
419 // Internal identities
420 for(Identity identity : identities) {
421 IdentityDefinition definition = identity.definition;
422 // if(handles[identity.resource] != 0)
424 if(definition instanceof External) {
425 // Already done everything
427 else if(definition instanceof Internal) {
428 Internal def = (Internal)definition;
430 Resource external = existingInternalMap.get(identity.resource);
431 if(external != null) {
432 handles[identity.resource] = builder.handle(external);
434 if(handles[identity.resource] != 0)
435 handles[identity.resource] = builder.handle(advisor.createChild(graph, this, builder.resource(handles[def.parent]), builder.resource(handles[identity.resource]), def.name));
437 handles[identity.resource] = builder.handle(advisor.createChild(graph, this, builder.resource(handles[def.parent]), null, def.name));
441 else if(definition instanceof Root) {
443 Root root = (Root)definition;
444 if(handles[identity.resource] != 0)
445 handles[identity.resource] = builder.handle(advisor.createRoot(graph, root, builder.resource(handles[identity.resource])));
447 handles[identity.resource] = builder.handle(advisor.createRoot(graph, root, null));
449 else if(definition instanceof Optional) {
450 Optional def = (Optional)definition;
451 if(handles[identity.resource] != 0) {
452 Resource child = advisor.createChild(graph, this, builder.resource(handles[def.parent]), builder.resource(handles[identity.resource]), def.name);
453 graph.claim(child, InstanceOf, null, Library); // ???
454 handles[identity.resource] = builder.handle(child);
456 Resource child = advisor.createChild(graph, this, builder.resource(handles[def.parent]), null, def.name);
457 graph.claim(child, InstanceOf, null, Library); // ???
458 handles[identity.resource] = builder.handle(child);
463 tg.getStatementCount();
464 tg.forStatements(null, new TransferableGraphSourceProcedure<int[]>() {
467 public void execute(int[] value) throws Exception {
474 int subject = handles[sub];
475 int predicate = handles[pred];
476 int object = handles[obj];
478 builder.addStatement(graph, subject, predicate, object);
480 int inverse = handles[inv];
481 builder.addStatement(graph, object, inverse, subject);
490 class ValueProcedure extends InputStream implements TransferableGraphSourceValueProcedure {
492 private TGResourceUtil util = new TGResourceUtil();
493 private DataInput source;
496 public void execute(int _resource, Datatype type, DataInput stream) throws Exception {
500 //int file = _resource & 0x80000000;
501 int resource = _resource & 0x7FFFFFFF;
503 Binding binding = Bindings.getBinding(type);
504 Serializer s = Bindings.getSerializer(binding);
506 builder.beginValue(handles[resource]);
507 if(util.mayHaveResource(type)) {
508 Object value = s.deserialize(stream);
509 util.adaptValue( binding, value, new LongAdapter() {
511 public long adapt(long in) {
513 return ss.getRandomAccessId(handles[(int)in]);
514 } catch (DatabaseException e) {
515 throw new IllegalStateException(e);
519 byte[] bytes = s.serialize(value);
520 for(byte b : bytes) {
522 if(val < 0) val += 256;
523 builder.appendValue(val);
533 public int read() throws IOException {
534 int value = source.readUnsignedByte();
536 builder.appendValue(value);
537 } catch (DatabaseException e) {
538 LOGGER.error("Failed to write value into database", e);
544 public void rawCopy(int resource, int length, DataInput input) throws Exception {
545 builder.beginValue(handles[resource]);
546 for (int i = 0; i < length; ++i)
547 builder.appendValue(input.readUnsignedByte());
553 tg.forValues2(null, new ValueProcedure());
555 for(Resource r : existingInternalMap.valueCollection()) {
556 graph.deny(r, InstanceOf, null, ExternalEntity, null);
562 public long[] getResourceIds(SerialisationSupport serializer) throws DatabaseException {
563 final int count = handles.length;
564 long[] resourceIds = new long[count];
565 for(int i=0;i<count;++i)
566 resourceIds[i] = serializer.getRandomAccessId(handles[i]);