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