1 package fi.vtt.simantics.procore.internal;
3 import org.simantics.db.Database;
4 import org.simantics.db.Resource;
5 import org.simantics.db.Session;
6 import org.simantics.db.WriteOnlyGraph;
7 import org.simantics.db.common.utils.Logger;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.exception.ServiceException;
10 import org.simantics.db.impl.graph.DelayedWriteGraph;
11 import org.simantics.db.impl.graph.WriteGraphImpl;
12 import org.simantics.db.procore.cluster.ClusterImpl;
13 import org.simantics.db.procore.cluster.TestCluster;
14 import org.simantics.db.procore.protocol.Constants;
15 import org.simantics.db.request.WriteTraits;
16 import org.simantics.db.service.ClusterUID;
17 import org.simantics.db.service.SerialisationSupport;
18 import org.simantics.db.service.XSupport;
20 public class XSupportImpl implements XSupport {
21 final private boolean DEBUG = false;
22 final private SessionImplSocket session;
24 XSupportImpl(SessionImplSocket session) {
25 this.session = session;
29 public void corruptPageTableAndDie() throws DatabaseException {
30 execute("corruptPageTableAndDie");
34 public void corruptCluster(Resource r) throws DatabaseException {
35 ClusterImpl cluster = getCluster(r);
36 long clusterId = Constants.NewClusterId;
37 if (null == cluster.cc)
38 cluster.cc = new ClusterChange(session.clusterStream, cluster);
39 session.clusterStream.corruptCluster(cluster.cc, clusterId);
43 public int corruptClusterTable(long clusterId)
44 throws DatabaseException {
45 return session.clusterTable.makeProxy(ClusterUID.make(0,666), clusterId).getClusterKey();
49 public void flushCluster(Resource r) throws ServiceException {
50 session.writeSupport.flushCluster(r);
54 public void breakConnection() throws DatabaseException {
55 throw new DatabaseException("XSupport.breakConnection not implemented.");
59 public void setClusterStreamOff(boolean setOff)
60 throws DatabaseException {
61 session.clusterTranslator.setStreamOff(setOff);
65 public int clearMetadataCache()
66 throws DatabaseException {
67 return session.graphSession.metadataCache.clear();
71 public <T> void commitAndContinue(WriteOnlyGraph wograph, WriteTraits wtraits) {
72 if(wograph instanceof DelayedWriteGraph) {
73 DelayedWriteGraph dw = (DelayedWriteGraph)wograph;
74 dw.addCommitAndContinue();
76 session.state.commitAndContinue2(wograph.getService(WriteGraphImpl.class), session.clusterStream, wtraits);
81 public boolean getImmutable(Resource resource)
82 throws DatabaseException {
83 if(!resource.isPersistent()) return false;
84 ClusterImpl clusterImpl = getCluster(resource);
85 return clusterImpl.getImmutable();
89 public void setImmutable(Resource resource, boolean immutable)
90 throws DatabaseException {
91 ClusterImpl clusterImpl = getCluster(resource);
92 clusterImpl.setImmutable(immutable, session.clusterTranslator);
94 private ClusterImpl getCluster(Resource resource)
95 throws DatabaseException {
98 int key = session.getService(SerialisationSupport.class).getTransientId(resource);
99 return (ClusterImpl)session.clusterTranslator.getClusterByResourceKey(key);
103 public void setServiceMode(boolean allow, boolean create) {
105 new Throwable("printing stack trace").printStackTrace();
106 System.out.println("XSupportImpl.setServiceMode allow=" + allow + " create=" + create + ", old mode=" + session.serviceMode);
108 int newServiceMode = (allow ? 1:0) + (create ? 2:0);
109 if(newServiceMode != session.serviceMode) {
110 session.serviceMode = newServiceMode;
111 session.writeSupport.flushCluster();
112 session.clusterSetsSupport.clear();
116 public Resource convertDelayedResourceToResource(Resource resource) {
117 return DelayedWriteGraph.convertDelayedResource(resource);
120 public String execute(String command)
121 throws DatabaseException {
122 boolean transaction = true;
124 session.state.startReadTransaction(Integer.MIN_VALUE);
125 } catch (Throwable t) {
126 Logger.defaultLogError("Trying to muddle on.", t);
130 return session.graphSession.execute(command);
133 session.state.stopReadTransaction();
137 public void testCluster(Session session)
138 throws DatabaseException {
139 TestCluster.test(session);
142 public ClusterUID[] listClusters() throws DatabaseException {
143 return session.graphSession.listClusters();
146 public void deleteCluster(ClusterUID clusterUID) throws DatabaseException {
147 ClusterImpl clusterImpl = session.clusterTable.getClusterByClusterUIDOrMakeProxy(clusterUID);
148 //clusterImpl.setDeleted(true, session.clusterTranslator);
149 session.clusterTranslator.setDeleted(clusterImpl, true);
152 public void purge() throws DatabaseException {
155 if (null == session.graphSession)
157 Database db = session.graphSession.dbSession.getDatabase();
162 public boolean rolledback() {
163 return session.graphSession.rolledback();