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;
19 import org.simantics.utils.datastructures.Pair;
21 public class XSupportImpl implements XSupport {
22 final private boolean DEBUG = false;
23 final private SessionImplSocket session;
25 XSupportImpl(SessionImplSocket session) {
26 this.session = session;
30 public void corruptPageTableAndDie() throws DatabaseException {
31 execute("corruptPageTableAndDie");
35 public void corruptCluster(Resource r) throws DatabaseException {
36 ClusterImpl cluster = getCluster(r);
37 long clusterId = Constants.NewClusterId;
38 if (null == cluster.cc)
39 cluster.cc = new ClusterChange(session.clusterStream, cluster);
40 session.clusterStream.corruptCluster(cluster.cc, clusterId);
44 public int corruptClusterTable(long clusterId)
45 throws DatabaseException {
46 return session.clusterTable.makeProxy(ClusterUID.make(0,666), clusterId).getClusterKey();
50 public void flushCluster(Resource r) throws ServiceException {
51 session.writeSupport.flushCluster(r);
55 public void breakConnection() throws DatabaseException {
56 throw new DatabaseException("XSupport.breakConnection not implemented.");
60 public void setClusterStreamOff(boolean setOff)
61 throws DatabaseException {
62 session.clusterTranslator.setStreamOff(setOff);
66 public int clearMetadataCache()
67 throws DatabaseException {
68 return session.graphSession.metadataCache.clear();
72 public <T> void commitAndContinue(WriteOnlyGraph wograph, WriteTraits wtraits) {
73 if(wograph instanceof DelayedWriteGraph) {
74 DelayedWriteGraph dw = (DelayedWriteGraph)wograph;
75 dw.addCommitAndContinue();
77 session.state.commitAndContinue2(wograph.getService(WriteGraphImpl.class), session.clusterStream, wtraits);
82 public boolean getImmutable(Resource resource)
83 throws DatabaseException {
84 if(!resource.isPersistent()) return false;
85 ClusterImpl clusterImpl = getCluster(resource);
86 return clusterImpl.getImmutable();
90 public void setImmutable(Resource resource, boolean immutable)
91 throws DatabaseException {
92 ClusterImpl clusterImpl = getCluster(resource);
93 clusterImpl.setImmutable(immutable, session.clusterTranslator);
95 private ClusterImpl getCluster(Resource resource)
96 throws DatabaseException {
99 int key = session.getService(SerialisationSupport.class).getTransientId(resource);
100 return (ClusterImpl)session.clusterTranslator.getClusterByResourceKey(key);
104 public void setServiceMode(boolean allow, boolean create) {
106 new Throwable("printing stack trace").printStackTrace();
107 System.out.println("XSupportImpl.setServiceMode allow=" + allow + " create=" + create + ", old mode=" + session.serviceMode);
109 int newServiceMode = (allow ? 1:0) + (create ? 2:0);
110 if(newServiceMode != session.serviceMode) {
111 session.serviceMode = newServiceMode;
112 session.writeSupport.flushCluster();
113 session.clusterSetsSupport.clear();
118 public Pair<Boolean, Boolean> getServiceMode() {
119 return Pair.make((session.serviceMode & 1) == 1, (session.serviceMode & 2) == 2);
123 public Resource convertDelayedResourceToResource(Resource resource) {
124 return DelayedWriteGraph.convertDelayedResource(resource);
127 public String execute(String command)
128 throws DatabaseException {
129 boolean transaction = true;
131 session.state.startReadTransaction(Integer.MIN_VALUE);
132 } catch (Throwable t) {
133 Logger.defaultLogError("Trying to muddle on.", t);
137 return session.graphSession.execute(command);
140 session.state.stopReadTransaction();
144 public void testCluster(Session session)
145 throws DatabaseException {
146 TestCluster.test(session);
149 public ClusterUID[] listClusters() throws DatabaseException {
150 return session.graphSession.listClusters();
153 public void deleteCluster(ClusterUID clusterUID) throws DatabaseException {
154 ClusterImpl clusterImpl = session.clusterTable.getClusterByClusterUIDOrMakeProxy(clusterUID);
155 //clusterImpl.setDeleted(true, session.clusterTranslator);
156 session.clusterTranslator.setDeleted(clusterImpl, true);
159 public void purge() throws DatabaseException {
162 if (null == session.graphSession)
164 Database db = session.graphSession.dbSession.getDatabase();
169 public boolean rolledback() {
170 return session.graphSession.rolledback();