]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/XSupportImpl.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / XSupportImpl.java
diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/XSupportImpl.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/XSupportImpl.java
new file mode 100644 (file)
index 0000000..662effb
--- /dev/null
@@ -0,0 +1,160 @@
+package fi.vtt.simantics.procore.internal;\r
+\r
+import org.simantics.db.Database;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.WriteOnlyGraph;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ServiceException;\r
+import org.simantics.db.impl.graph.DelayedWriteGraph;\r
+import org.simantics.db.impl.graph.WriteGraphImpl;\r
+import org.simantics.db.procore.cluster.ClusterImpl;\r
+import org.simantics.db.procore.cluster.TestCluster;\r
+import org.simantics.db.procore.protocol.Constants;\r
+import org.simantics.db.request.WriteTraits;\r
+import org.simantics.db.service.ClusterUID;\r
+import org.simantics.db.service.SerialisationSupport;\r
+import org.simantics.db.service.XSupport;\r
+\r
+public class XSupportImpl implements XSupport {\r
+    final private boolean DEBUG = false;\r
+    final private SessionImplSocket session;\r
+\r
+    XSupportImpl(SessionImplSocket session) {\r
+        this.session = session;\r
+    }\r
+\r
+    @Override\r
+    public void corruptPageTableAndDie() throws DatabaseException {\r
+        execute("corruptPageTableAndDie");\r
+    }\r
+\r
+    @Override\r
+    public void corruptCluster(Resource r) throws DatabaseException {\r
+        ClusterImpl cluster = getCluster(r);\r
+        long clusterId = Constants.NewClusterId;\r
+        if (null == cluster.cc)\r
+            cluster.cc = new ClusterChange(session.clusterStream, cluster);\r
+        session.clusterStream.corruptCluster(cluster.cc, clusterId);\r
+    }\r
+\r
+    @Override\r
+    public int corruptClusterTable(long clusterId)\r
+    throws DatabaseException {\r
+        return session.clusterTable.makeProxy(ClusterUID.make(0,666), clusterId).getClusterKey();\r
+    }\r
+\r
+    @Override\r
+    public void flushCluster(Resource r) throws ServiceException {\r
+        session.writeSupport.flushCluster(r);\r
+    }\r
+\r
+    @Override\r
+    public void breakConnection() throws DatabaseException {\r
+        throw new DatabaseException("XSupport.breakConnection not implemented.");\r
+    }\r
+\r
+    @Override\r
+    public void setClusterStreamOff(boolean setOff)\r
+    throws DatabaseException {\r
+        session.clusterTranslator.setStreamOff(setOff);\r
+    }\r
+\r
+    @Override\r
+    public int clearMetadataCache()\r
+    throws DatabaseException {\r
+        return session.graphSession.metadataCache.clear();\r
+    }\r
+\r
+    @Override\r
+    public <T> void commitAndContinue(WriteOnlyGraph wograph, WriteTraits wtraits) {\r
+       if(wograph instanceof DelayedWriteGraph) {\r
+               DelayedWriteGraph dw = (DelayedWriteGraph)wograph;\r
+               dw.addCommitAndContinue();\r
+       } else {\r
+               session.state.commitAndContinue2(wograph.getService(WriteGraphImpl.class), session.clusterStream, wtraits);\r
+       }\r
+    }\r
+\r
+    @Override\r
+    public boolean getImmutable(Resource resource)\r
+    throws DatabaseException {\r
+        if(!resource.isPersistent()) return false;\r
+        ClusterImpl clusterImpl = getCluster(resource);\r
+        return clusterImpl.getImmutable();\r
+    }\r
+\r
+    @Override\r
+    public void setImmutable(Resource resource, boolean immutable)\r
+    throws DatabaseException {\r
+        ClusterImpl clusterImpl = getCluster(resource);\r
+        clusterImpl.setImmutable(immutable, session.clusterTranslator);\r
+    }\r
+    private ClusterImpl getCluster(Resource resource)\r
+    throws DatabaseException {\r
+        if (null == resource)\r
+            return null;\r
+        int key = session.getService(SerialisationSupport.class).getTransientId(resource);\r
+        return (ClusterImpl)session.clusterTranslator.getClusterByResourceKey(key);\r
+    }\r
+\r
+    @Override\r
+    public void setServiceMode(boolean allow, boolean create) {\r
+        if  (DEBUG) {\r
+            new Throwable("printing stack trace").printStackTrace();\r
+            System.out.println("XSupportImpl.setServiceMode allow=" + allow + " create=" + create + ", old mode=" + session.serviceMode);\r
+        }\r
+        int newServiceMode = (allow ? 1:0) + (create ? 2:0);\r
+        if(newServiceMode != session.serviceMode) {\r
+               session.serviceMode = newServiceMode; \r
+               session.writeSupport.flushCluster();\r
+               session.clusterSetsSupport.clear();\r
+        }\r
+    }\r
+    @Override\r
+    public Resource convertDelayedResourceToResource(Resource resource) {\r
+        return DelayedWriteGraph.convertDelayedResource(resource);\r
+    }\r
+    @Override\r
+    public String execute(String command)\r
+    throws DatabaseException {\r
+        boolean transaction = true;\r
+        try {\r
+            session.state.startReadTransaction(Integer.MIN_VALUE);\r
+        } catch (Throwable t) {\r
+            Logger.defaultLogError("Trying to muddle on.", t);\r
+            transaction = false;\r
+        }\r
+        try {\r
+            return session.graphSession.execute(command);\r
+        } finally {\r
+            if (transaction)\r
+                session.state.stopReadTransaction();\r
+        }\r
+    }\r
+    @Override\r
+    public void testCluster(Session session)\r
+    throws DatabaseException {\r
+        TestCluster.test(session);\r
+    }\r
+    @Override\r
+    public ClusterUID[] listClusters() throws DatabaseException {\r
+        return session.graphSession.listClusters();\r
+    }\r
+    @Override\r
+    public void deleteCluster(ClusterUID clusterUID) throws DatabaseException {\r
+        ClusterImpl clusterImpl = session.clusterTable.getClusterByClusterUIDOrMakeProxy(clusterUID);\r
+        //clusterImpl.setDeleted(true, session.clusterTranslator);\r
+        session.clusterTranslator.setDeleted(clusterImpl, true);\r
+    }\r
+    @Override\r
+    public void purge() throws DatabaseException {\r
+        if (null == session)\r
+            return;\r
+        if (null == session.graphSession)\r
+            return;\r
+        Database db = session.graphSession.dbSession.getDatabase();\r
+        db.purgeDatabase();\r
+    }\r
+}\r