]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ExternalValueSupportImpl.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ExternalValueSupportImpl.java
diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ExternalValueSupportImpl.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ExternalValueSupportImpl.java
new file mode 100644 (file)
index 0000000..a4f8d59
--- /dev/null
@@ -0,0 +1,84 @@
+package fi.vtt.simantics.procore.internal;\r
+\r
+import org.simantics.db.ExternalValueSupport;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.impl.ClusterI;\r
+import org.simantics.db.impl.ClusterTranslator;\r
+import org.simantics.db.impl.ResourceImpl;\r
+\r
+import fi.vtt.simantics.procore.DebugPolicy;\r
+\r
+public class ExternalValueSupportImpl implements ExternalValueSupport {\r
+    private final boolean DEBUG = DebugPolicy.REPORT_CLUSTER_EVENTS;\r
+    private final SessionImplSocket session;\r
+    private ClusterTranslator clusterTranslator;\r
+    private final ClusterTable clusterTable;\r
+    ExternalValueSupportImpl(SessionImplSocket session) {\r
+        this.session = session;\r
+        this.clusterTranslator = session.clusterTranslator;\r
+        this.clusterTable = session.getClusterTable();\r
+    }\r
+    private final void check4Translator() {\r
+        if (null == clusterTranslator)\r
+            this.clusterTranslator = session.clusterTranslator;\r
+    }\r
+    @Override\r
+    public void writeValue(WriteGraph graph, Resource resource, long offset, int length, byte[] bytes)\r
+    throws DatabaseException {\r
+        check4Translator();\r
+        assert(length <= bytes.length);\r
+        ResourceImpl resourceImpl = (ResourceImpl)resource;\r
+        ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);\r
+        cluster.modiValueEx(resourceImpl.id, offset, length, bytes, 0, clusterTranslator);\r
+    }\r
+// This breaks undo.\r
+//    @Override\r
+//    public void moveValue(WriteGraph graph, Resource resource)\r
+//    throws DatabaseException {\r
+//        this.writeValue(graph, resource, (1L<<58)-1, 0, new byte[0]);\r
+//    }\r
+// This breaks undo.\r
+//    @Override\r
+//    public void commitAndContinue(WriteGraph graph, WriteTraits wtraits, Resource resource)\r
+//    throws DatabaseException {\r
+//        XSupport xs = graph.getService(XSupport.class);\r
+//        xs.commitAndContinue(graph, wtraits);\r
+//        clusterTranslator.wait4RequestsLess(1);\r
+//    }\r
+    @Override\r
+    public byte[] readValue(ReadGraph graph, Resource resource, long offset, int length)\r
+    throws DatabaseException {\r
+        check4Translator();\r
+        ResourceImpl resourceImpl = (ResourceImpl)resource;\r
+        ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);\r
+        return cluster.readValueEx(resourceImpl.id, offset, length, clusterTranslator);\r
+    }\r
+    public long getValueSize(ReadGraph graph, Resource resource)\r
+    throws DatabaseException {\r
+        check4Translator();\r
+        ResourceImpl resourceImpl = (ResourceImpl)resource;\r
+        ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);\r
+        long size = cluster.getValueSizeEx(resourceImpl.id, clusterTranslator);\r
+        if (DEBUG)\r
+            System.out.println("DEBUG:  resource=" + resource + " value length=" + size + ".");\r
+        return size;\r
+    }\r
+    @Override\r
+    public void removeValue(WriteGraph graph, Resource resource)\r
+    throws DatabaseException {\r
+        check4Translator();\r
+        ResourceImpl resourceImpl = (ResourceImpl)resource;\r
+        ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);\r
+        cluster.modiValueEx(resourceImpl.id, 0, 0, new byte[0], 0, clusterTranslator);\r
+        cluster.removeValue(resourceImpl.id, clusterTranslator);\r
+    }\r
+    @Override\r
+    public int wait4RequestsLess(int limit)\r
+    throws DatabaseException {\r
+        check4Translator();\r
+        return clusterTranslator.wait4RequestsLess(limit);\r
+    }\r
+}\r