1 package fi.vtt.simantics.procore.internal;
3 import java.io.IOException;
5 import org.simantics.db.Database;
6 import org.simantics.db.Resource;
7 import org.simantics.db.Session;
8 import org.simantics.db.WriteOnlyGraph;
9 import org.simantics.db.common.utils.Logger;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.exception.ServiceException;
12 import org.simantics.db.impl.graph.DelayedWriteGraph;
13 import org.simantics.db.impl.graph.WriteGraphImpl;
14 import org.simantics.db.procore.cluster.ClusterImpl;
15 import org.simantics.db.procore.cluster.TestCluster;
16 import org.simantics.db.procore.protocol.Constants;
17 import org.simantics.db.request.WriteTraits;
18 import org.simantics.db.service.ClusterUID;
19 import org.simantics.db.service.SerialisationSupport;
20 import org.simantics.db.service.XSupport;
21 import org.simantics.utils.datastructures.Pair;
23 public class XSupportImpl implements XSupport {
24 final private boolean DEBUG = false;
25 final private SessionImplSocket session;
27 XSupportImpl(SessionImplSocket session) {
28 this.session = session;
32 public void corruptPageTableAndDie() throws DatabaseException {
33 execute("corruptPageTableAndDie");
37 public void corruptCluster(Resource r) throws DatabaseException {
38 ClusterImpl cluster = getCluster(r);
39 long clusterId = Constants.NewClusterId;
40 if (null == cluster.cc)
41 cluster.cc = new ClusterChange(session.clusterStream, cluster);
42 session.clusterStream.corruptCluster(cluster.cc, clusterId);
46 public int corruptClusterTable(long clusterId)
47 throws DatabaseException {
48 return session.clusterTable.makeProxy(ClusterUID.make(0,666), clusterId).getClusterKey();
52 public void flushCluster(Resource r) throws ServiceException {
53 session.writeSupport.flushCluster(r);
57 public void breakConnection() throws DatabaseException {
58 throw new DatabaseException("XSupport.breakConnection not implemented.");
62 public void setClusterStreamOff(boolean setOff)
63 throws DatabaseException {
64 session.clusterTranslator.setStreamOff(setOff);
68 public int clearMetadataCache()
69 throws DatabaseException {
70 return session.graphSession.metadataCache.clear();
74 public <T> void commitAndContinue(WriteOnlyGraph wograph, WriteTraits wtraits) {
75 if(wograph instanceof DelayedWriteGraph) {
76 DelayedWriteGraph dw = (DelayedWriteGraph)wograph;
77 dw.addCommitAndContinue();
79 session.state.commitAndContinue2(wograph.getService(WriteGraphImpl.class), session.clusterStream, wtraits);
84 public boolean getImmutable(Resource resource)
85 throws DatabaseException {
86 if(!resource.isPersistent()) return false;
87 ClusterImpl clusterImpl = getCluster(resource);
88 return clusterImpl.getImmutable();
92 public void setImmutable(Resource resource, boolean immutable)
93 throws DatabaseException {
94 ClusterImpl clusterImpl = getCluster(resource);
95 clusterImpl.setImmutable(immutable, session.clusterTranslator);
97 private ClusterImpl getCluster(Resource resource)
98 throws DatabaseException {
101 int key = session.getService(SerialisationSupport.class).getTransientId(resource);
102 return (ClusterImpl)session.clusterTranslator.getClusterByResourceKey(key);
106 public void setServiceMode(boolean allow, boolean create) {
108 new Throwable("printing stack trace").printStackTrace();
109 System.out.println("XSupportImpl.setServiceMode allow=" + allow + " create=" + create + ", old mode=" + session.serviceMode);
111 int newServiceMode = (allow ? 1:0) + (create ? 2:0);
112 if(newServiceMode != session.serviceMode) {
113 session.serviceMode = newServiceMode;
114 session.writeSupport.flushCluster();
115 session.clusterSetsSupport.clear();
120 public Pair<Boolean, Boolean> getServiceMode() {
121 return Pair.make((session.serviceMode & 1) == 1, (session.serviceMode & 2) == 2);
125 public Resource convertDelayedResourceToResource(Resource resource) {
126 return DelayedWriteGraph.convertDelayedResource(resource);
129 public String execute(String command)
130 throws DatabaseException {
131 boolean transaction = true;
133 session.state.startReadTransaction(Integer.MIN_VALUE);
134 } catch (Throwable t) {
135 Logger.defaultLogError("Trying to muddle on.", t);
139 return session.graphSession.execute(command);
142 session.state.stopReadTransaction();
146 public void testCluster(Session session)
147 throws DatabaseException {
148 TestCluster.test(session);
151 public ClusterUID[] listClusters() throws DatabaseException {
152 return session.graphSession.listClusters();
155 public void deleteCluster(ClusterUID clusterUID) throws DatabaseException {
156 ClusterImpl clusterImpl = session.clusterTable.getClusterByClusterUIDOrMakeProxy(clusterUID);
157 //clusterImpl.setDeleted(true, session.clusterTranslator);
158 session.clusterTranslator.setDeleted(clusterImpl, true);
161 public void purge() throws DatabaseException {
164 if (null == session.graphSession)
166 Database db = session.graphSession.dbSession.getDatabase();
171 public boolean rolledback() {
172 return session.graphSession.rolledback();
176 public boolean isClusterLoaded(ClusterUID clusterUID) throws DatabaseException {
177 ClusterImpl clusterImpl = session.clusterTable.getClusterByClusterUIDOrMakeProxy(clusterUID);
178 return clusterImpl.isLoaded();
182 public void saveQueries() throws DatabaseException {
184 session.queryProvider2.save();
185 } catch (IOException e) {
186 throw new DatabaseException(e);