X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.graph.db%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fdb%2FStreamingTransferableGraphImportProcess.java;h=3599eefc0ecc81c9cb6d012bf31415f48bcfc34f;hp=48cebf342c2cfe5566a3fb325bd33994437b20f7;hb=c26409b1caf2f1e560d37c5befd11b442399c3fe;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 diff --git a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/StreamingTransferableGraphImportProcess.java b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/StreamingTransferableGraphImportProcess.java index 48cebf342..3599eefc0 100644 --- a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/StreamingTransferableGraphImportProcess.java +++ b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/StreamingTransferableGraphImportProcess.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012, 2016 Association for Decentralized Information Management + * Copyright (c) 2012, 2017 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,17 +8,16 @@ * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation - * Semantum Oy + * Semantum Oy - e.g. #7016 *******************************************************************************/ package org.simantics.graph.db; import java.io.DataInput; -import java.io.DataOutput; -import java.io.DataOutputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -30,6 +29,7 @@ import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.mutable.Variant; import org.simantics.databoard.serialization.Serializer; import org.simantics.databoard.type.Datatype; +import org.simantics.databoard.util.URIStringUtils; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; @@ -45,6 +45,7 @@ import org.simantics.db.service.ClusterBuilder2; import org.simantics.db.service.ClusterBuilderFactory; import org.simantics.db.service.ClusteringSupport; import org.simantics.db.service.SerialisationSupport; +import org.simantics.db.service.XSupport; import org.simantics.graph.db.TransferableGraphSource.TransferableGraphSourceProcedure; import org.simantics.graph.db.TransferableGraphSource.TransferableGraphSourceValueProcedure; import org.simantics.graph.representation.Extensions; @@ -57,37 +58,15 @@ import org.simantics.graph.representation.Root; import org.simantics.graph.representation.TransferableGraphUtils; import org.simantics.graph.utils.TGResourceUtil; import org.simantics.graph.utils.TGResourceUtil.LongAdapter; +import org.simantics.utils.datastructures.Pair; +import org.slf4j.LoggerFactory; + +import gnu.trove.map.TIntObjectMap; +import gnu.trove.map.hash.TIntObjectHashMap; public class StreamingTransferableGraphImportProcess implements TransferableGraphImporter { - - public static String LOG_FILE = "transferableGraphs.log"; - final static private boolean LOG = false; - - static DataOutput log; - static { - - if (LOG) { - - try { - FileOutputStream stream = new FileOutputStream(LOG_FILE); - log = new DataOutputStream(stream); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - - } - - private static void log(String line) { - if (LOG) { - try { - log.writeUTF(line + "\n"); - } catch (IOException e) { - e.printStackTrace(); - } - } - } + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(StreamingTransferableGraphImportProcess.class); Resource indexRoot; TransferableGraphSource tg; @@ -95,32 +74,36 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap IImportAdvisor2 advisor; ClusterBuilder2 builder; final TGResourceUtil resourceUtil = new TGResourceUtil(); - + int[] handles; - - Set missingExternals = new HashSet(); - + + Map allMissingExternals = new HashMap<>(); + Set missingExternals = new HashSet<>(); + Map resolvedParents = new HashMap<>(); + TIntObjectHashMap existingInternalMap = new TIntObjectHashMap<>(); + int resourceCount; Identity[] identities; TreeMap extensions; - + // Builtins Resource RootLibrary; Resource String; + Resource ExternalEntity; Resource Library; - + Resource InstanceOf; Resource ConsistsOf; Resource PartOf; Resource HasName; Resource NameOf; - + public StreamingTransferableGraphImportProcess(Session session, VirtualGraph vg, TransferableGraphSource tg, IImportAdvisor2 advisor) { this.tg = tg; this.vg = vg; this.advisor = advisor; } - + public void readIdentities(ReadGraph g) throws Exception { extensions = tg.getExtensions(); resourceCount = tg.getResourceCount(); @@ -135,7 +118,7 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap } }); } - + public void findBuiltins(WriteOnlyGraph g) throws DatabaseException { RootLibrary = g.getBuiltin("http:/"); String = g.getBuiltin(CoreInitialization.LAYER0 + "String"); @@ -145,8 +128,9 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf"); HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName"); NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf"); + ExternalEntity = g.getBuiltin(CoreInitialization.LAYER0 + "ExternalEntity"); } - + public void findBuiltins(ReadGraph g) throws DatabaseException { RootLibrary = g.getBuiltin("http:/"); String = g.getBuiltin(CoreInitialization.LAYER0 + "String"); @@ -156,59 +140,18 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf"); HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName"); NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf"); + ExternalEntity = g.getBuiltin(CoreInitialization.LAYER0 + "ExternalEntity"); } -// /* Preparation that is used when the core is empty. -// */ -// void initialPrepare(WriteOnlyGraph graph) throws DatabaseException { -// findBuiltins(graph); -// -// resources = new Resource[tg.resourceCount]; -// -// int Root = -1; -// int SimanticsDomain = -1; -// int Layer0 = -1; -// -// for(Identity identity : tg.identities) { -// if(identity.definition instanceof Internal) { -// Internal def = (Internal)identity.definition; -// Resource res = null; -// if(def.parent == Layer0) { -// try { -// res = graph.getBuiltin(CoreInitialization.LAYER0 + def.name); -// } catch(ResourceNotFoundException e) { -// } -// } -// else if(def.parent == SimanticsDomain) { -// if(def.name.equals("Layer0-1.0")) -// Layer0 = identity.resource; -// } -// else if(def.parent == Root) { -// if(def.name.equals("www.simantics.org")) -// SimanticsDomain = identity.resource; -// } -// -// if(res == null) -// res = createChild(graph, resources[def.parent], def.name); -// else -// createChild(graph, res, resources[def.parent], def.name); -// resources[identity.resource] = res; -// } -// else if(identity.definition instanceof Root) { -// Root = identity.resource; -// resources[identity.resource] = RootLibrary; -// } -// } -// } - - void addMissing(String external) { - Set removals = new HashSet(); + void addMissing(int handleIndex, String external) { + allMissingExternals.put(external, handleIndex); + Set removals = new HashSet<>(); for(String ext : missingExternals) if(ext.startsWith(external)) return; for(String ext : missingExternals) if(external.startsWith(ext)) removals.add(ext); missingExternals.removeAll(removals); missingExternals.add(external); } - + void prepare(ReadGraph graph) throws Exception { Resource target = advisor.getTarget(); @@ -225,7 +168,8 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap ClusterBuilderFactory factory = graph.getService(ClusterBuilderFactory.class); ClusterBuilder2 builder = factory.create(vg, false); - this.handles = new int[resourceCount]; + this.handles = new int[resourceCount]; + TIntObjectMap identityMap = TransferableGraphUtils.mapIdentities(identities); for(Identity identity : identities) { IdentityDefinition definition = identity.definition; @@ -243,23 +187,28 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap Resource parent = handle != 0 ? builder.resource(handle) : null; // TODO: escape should be removed when names become well-behaving if(parent != null) { + resolvedParents.put(graph.getURI(parent), parent); Map childMap = graph .syncRequest(new UnescapedChildMapOfResource(parent), - new TransientCacheAsyncListener>()); + TransientCacheAsyncListener.instance()); Resource child = childMap.get(def.name); if(child == null) { - addMissing(graph.getURI(parent) + "/" + def.name); + addMissing(identity.resource, graph.getURI(parent) + "/" + def.name); } else { handles[identity.resource] = builder.handle(child); } } else { - addMissing(TransferableGraphUtils.getURI(resourceCount, identities, def.parent) + "/" + def.name); + addMissing(identity.resource, TransferableGraphUtils.getURI(resourceCount, identityMap, def.parent) + "/" + def.name); } } } } else if(definition instanceof Internal) { - // Do not do anything for now + String uri = TransferableGraphUtils.getURI(resourceCount, identityMap, identity.resource); + Resource existing = graph.getPossibleResource(uri); + if(existing != null) { + existingInternalMap.put(identity.resource, existing); + } } else if(definition instanceof Root) { Root root = (Root)definition; @@ -279,7 +228,7 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap } } - if(!missingExternals.isEmpty()) throw new MissingDependencyException(this); + //if(!missingExternals.isEmpty()) throw new MissingDependencyException(this); } @@ -292,7 +241,7 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap graph.claim(child, HasName, NameOf, nameResource); return child; } - + int[] getClustering() { Variant v = extensions.get(Extensions.CLUSTERING); if(v == null) return null; @@ -318,7 +267,7 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap boolean needTranslation(Datatype type) { return resourceUtil.mayHaveResource(type); } - + void findClusterSet(WriteOnlyGraph graph, Resource rootLibrary, int[] clustering, int[] clusterSets, long[] clusters, int id) throws DatabaseException { ClusteringSupport support = graph.getService(ClusteringSupport.class); if(id == Extensions.ROOT_LIBRARY_CLUSTER_SET || id == Extensions.INDEX_ROOT_CLUSTER_SET) return; @@ -352,11 +301,47 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap } } } - + + void createMissing(final WriteOnlyGraph graph) throws Exception { + + if(allMissingExternals.isEmpty()) return; + + XSupport xs = graph.getService(XSupport.class); + Pair serviceMode = xs.getServiceMode(); + xs.setServiceMode(true, false); + try { + ArrayList missing = new ArrayList<>(allMissingExternals.keySet()); + Collections.sort(missing); + for(String uri : missing) { + String[] parts = URIStringUtils.splitURI(uri); + + Resource parent = resolvedParents.get(parts[0]); + // TODO: proper exception message + if(parent == null) throw new IllegalStateException("!!"); + + Resource childResource = graph.newResource(); + graph.claim(childResource, InstanceOf, null, ExternalEntity); + + Resource nameResource = graph.newResource(); + graph.claim(nameResource, InstanceOf, null, String); + graph.claimValue(nameResource, parts[1], WriteBindings.STRING); + graph.claim(childResource, HasName, NameOf, nameResource); + + graph.claim(parent, ConsistsOf, PartOf, childResource); + + resolvedParents.put(uri, childResource); + + handles[allMissingExternals.get(uri)] = builder.handle(childResource); + } + } finally { + xs.setServiceMode(serviceMode.first, serviceMode.second); + } + } + void write(final WriteOnlyGraph graph) throws Exception { final SerialisationSupport ss = graph.getService(SerialisationSupport.class); - + ClusterBuilderFactory factory = graph.getService(ClusterBuilderFactory.class); if(advisor instanceof IImportAdvisor2) { boolean allowImmutable = ((IImportAdvisor2)advisor).allowImmutableModifications(); @@ -365,6 +350,8 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap builder = factory.create(vg, false); } + createMissing(graph); + final int[] handles = this.handles; int[] clustering = getClustering(); @@ -439,10 +426,17 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap } else if(definition instanceof Internal) { Internal def = (Internal)definition; - if(handles[identity.resource] != 0) - handles[identity.resource] = builder.handle(advisor.createChild(graph, this, builder.resource(handles[def.parent]), builder.resource(handles[identity.resource]), def.name)); - else - handles[identity.resource] = builder.handle(advisor.createChild(graph, this, builder.resource(handles[def.parent]), null, def.name)); + + Resource external = existingInternalMap.get(identity.resource); + if(external != null) { + handles[identity.resource] = builder.handle(external); + } else { + if(handles[identity.resource] != 0) + handles[identity.resource] = builder.handle(advisor.createChild(graph, this, builder.resource(handles[def.parent]), builder.resource(handles[identity.resource]), def.name)); + else + handles[identity.resource] = builder.handle(advisor.createChild(graph, this, builder.resource(handles[def.parent]), null, def.name)); + } + } else if(definition instanceof Root) { @@ -492,7 +486,7 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap }); tg.getValueCount(); - + class ValueProcedure extends InputStream implements TransferableGraphSourceValueProcedure { private TGResourceUtil util = new TGResourceUtil(); @@ -541,7 +535,7 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap try { builder.appendValue(value); } catch (DatabaseException e) { - e.printStackTrace(); + LOGGER.error("Failed to write value into database", e); } return value; } @@ -558,8 +552,12 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap tg.forValues2(null, new ValueProcedure()); + for(Resource r : existingInternalMap.valueCollection()) { + graph.deny(r, InstanceOf, null, ExternalEntity, null); + } + } - + @Override public long[] getResourceIds(SerialisationSupport serializer) throws DatabaseException { final int count = handles.length;