X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.project%2Fsrc%2Forg%2Fsimantics%2Fproject%2Fmanagement%2FGraphBundle.java;h=82764ca42f975461af66b9066c3b09217027644c;hp=952e50cb08f9d8b5e81385a646be6ecffa6f43a1;hb=b913419ca9037bf9734c56a5f079024c3a1cd177;hpb=a8d030f7db1f59a5b51cdc34f18de7c0a0ee8549 diff --git a/bundles/org.simantics.project/src/org/simantics/project/management/GraphBundle.java b/bundles/org.simantics.project/src/org/simantics/project/management/GraphBundle.java index 952e50cb0..82764ca42 100644 --- a/bundles/org.simantics.project/src/org/simantics/project/management/GraphBundle.java +++ b/bundles/org.simantics.project/src/org/simantics/project/management/GraphBundle.java @@ -11,11 +11,10 @@ *******************************************************************************/ package org.simantics.project.management; +import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.simantics.databoard.Bindings; -import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.error.BindingException; import org.simantics.databoard.binding.error.RuntimeBindingException; import org.simantics.db.ReadGraph; @@ -26,6 +25,8 @@ import org.simantics.db.common.utils.Transaction; import org.simantics.db.exception.DatabaseException; import org.simantics.graph.representation.TransferableGraph1; import org.simantics.layer0.DatabaseManagementResource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * GraphBundle represents a bundle graph that may exist in memory @@ -58,6 +59,8 @@ import org.simantics.layer0.DatabaseManagementResource; */ public class GraphBundle implements Comparable { + private static final Logger LOGGER = LoggerFactory.getLogger(GraphBundle.class); + /** Versioned Id pattern */ static String ID_PATTERN_STRING = "[a-zA-Z_0-9\\-]+(?:\\.[a-zA-Z_0-9\\-]+)*"; static String VERSION_PATTERN_STRING = "(\\d+).(\\d+).(\\d+).([a-zA-Z_0-9\\-]+)"; @@ -68,6 +71,9 @@ public class GraphBundle implements Comparable { /** User-friendly name */ String name; + /** If {@link #graph} is null then this may be defined to fetch the data on-demand */ + Supplier graphSource; + /** Actual graph */ TransferableGraph1 graph; @@ -93,21 +99,19 @@ public class GraphBundle implements Comparable { boolean immutable = true; GraphBundle() {} - + public GraphBundle(String name, TransferableGraph1 data, String versionedId) - throws RuntimeBindingException { - try { + throws RuntimeBindingException { + try { // Assert version id is correct Matcher m = VERSIONED_ID_PATTERN.matcher(versionedId); if (!m.matches()) { throw new IllegalArgumentException("Illegal VersionId \""+versionedId+"\", / is expected."); } - - Binding binding = Bindings.getBindingUnchecked( TransferableGraph1.class ); - + this.name = name; - this.graph = data; - this.hashcode = data != null ? binding.hashValue( data ) : 0; + this.graph = data; + this.hashcode = hash(data); this.id = m.group(1); this.major = Integer.valueOf( m.group(2) ); this.minor = Integer.valueOf( m.group(3) ); @@ -118,22 +122,21 @@ public class GraphBundle implements Comparable { } catch (BindingException e) { // Unexpected throw new RuntimeBindingException(e); - } + } } - + public GraphBundle(String name, TransferableGraph1 data, String id, String version) - throws RuntimeBindingException { + throws RuntimeBindingException { Matcher m = ID_PATTERN.matcher(id); if (!m.matches()) throw new IllegalArgumentException("Illegal Id, got \""+id+"\""); m = VERSION_PATTERN.matcher(version); if (!m.matches()) throw new IllegalArgumentException("Illegal Version, got \""+id+"\", / is expected."); - try { - Binding binding = Bindings.getBindingUnchecked( TransferableGraph1.class ); + try { this.name = name; - this.graph = data; - this.hashcode = binding.hashValue( data ); + this.graph = data; + this.hashcode = hash(data); this.id = id; this.major = Integer.valueOf( m.group(1) ); this.minor = Integer.valueOf( m.group(2) ); @@ -146,7 +149,30 @@ public class GraphBundle implements Comparable { throw new RuntimeBindingException(e); } } - + + public GraphBundle(String name, Supplier source, int hashCode, String id, String version) { + Matcher m = ID_PATTERN.matcher(id); + if (!m.matches()) + throw new IllegalArgumentException("Illegal Id, got \""+id+"\""); + m = VERSION_PATTERN.matcher(version); + if (!m.matches()) + throw new IllegalArgumentException("Illegal Version, got \""+id+"\", / is expected."); + this.name = name; + this.graphSource = source; + this.hashcode = hashCode; + this.id = id; + this.major = Integer.valueOf( m.group(1) ); + this.minor = Integer.valueOf( m.group(2) ); + this.service = Integer.valueOf( m.group(3) ); + if (m.group(4) != null) { + this.qualifier = m.group(4); + } + } + + private int hash(TransferableGraph1 data) throws BindingException { + return data == null ? 0 : TransferableGraph1.BINDING.hashValue( data ); + } + public String getName() { return name; } @@ -180,15 +206,18 @@ public class GraphBundle implements Comparable { */ public TransferableGraph1 getGraph() { if (graph == null) { - ReadGraph g = Transaction.readGraph(); - if (g == null) - throw new IllegalStateException("No read transaction available"); - try { - Binding tg_binding = Bindings.getBindingUnchecked( TransferableGraph1.class ); - DatabaseManagementResource DatabaseManagement = DatabaseManagementResource.getInstance(g); - graph = g.getRelatedValue(resource, DatabaseManagement.HasFile, tg_binding); - } catch (DatabaseException e) { - e.printStackTrace(); + if (graphSource != null) { + graph = graphSource.get(); + } + if (graph == null) { + ReadGraph g = Transaction.readGraph(); + if (g == null) + throw new IllegalStateException("No read transaction available"); + try { + graph = readTg(g); + } catch (DatabaseException e) { + LOGGER.error("Failed to read transferable graph from " + resource, e); + } } } return graph; @@ -200,18 +229,21 @@ public class GraphBundle implements Comparable { graph = processor.syncRequest(new ResourceRead(resource) { @Override public TransferableGraph1 perform(ReadGraph graph) throws DatabaseException { - Binding tg_binding = Bindings.getBindingUnchecked( TransferableGraph1.class ); - DatabaseManagementResource DatabaseManagement = DatabaseManagementResource.getInstance(graph); - return graph.getRelatedValue(resource, DatabaseManagement.HasFile, tg_binding); + return readTg(graph); } }); } catch (DatabaseException e) { - e.printStackTrace(); + LOGGER.error("Failed to read transferable graph from " + resource, e); } } return graph; } - + + private TransferableGraph1 readTg(ReadGraph graph) throws DatabaseException { + DatabaseManagementResource DatabaseManagement = DatabaseManagementResource.getInstance(graph); + return graph.getRelatedValue(resource, DatabaseManagement.HasFile, TransferableGraph1.BINDING); + } + public int getHashcode() { return hashcode; }