]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/XSupportImpl.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / XSupportImpl.java
1 package fi.vtt.simantics.procore.internal;
2
3 import java.io.IOException;
4
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;
22
23 public class XSupportImpl implements XSupport {
24     final private boolean DEBUG = false;
25     final private SessionImplSocket session;
26
27     XSupportImpl(SessionImplSocket session) {
28         this.session = session;
29     }
30
31     @Override
32     public void corruptPageTableAndDie() throws DatabaseException {
33         execute("corruptPageTableAndDie");
34     }
35
36     @Override
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);
43     }
44
45     @Override
46     public int corruptClusterTable(long clusterId)
47     throws DatabaseException {
48         return session.clusterTable.makeProxy(ClusterUID.make(0,666), clusterId).getClusterKey();
49     }
50
51     @Override
52     public void flushCluster(Resource r) throws ServiceException {
53         session.writeSupport.flushCluster(r);
54     }
55
56     @Override
57     public void breakConnection() throws DatabaseException {
58         throw new DatabaseException("XSupport.breakConnection not implemented.");
59     }
60
61     @Override
62     public void setClusterStreamOff(boolean setOff)
63     throws DatabaseException {
64         session.clusterTranslator.setStreamOff(setOff);
65     }
66
67     @Override
68     public int clearMetadataCache()
69     throws DatabaseException {
70         return session.graphSession.metadataCache.clear();
71     }
72
73     @Override
74     public <T> void commitAndContinue(WriteOnlyGraph wograph, WriteTraits wtraits) {
75         if(wograph instanceof DelayedWriteGraph) {
76                 DelayedWriteGraph dw = (DelayedWriteGraph)wograph;
77                 dw.addCommitAndContinue();
78         } else {
79                 session.state.commitAndContinue2(wograph.getService(WriteGraphImpl.class), session.clusterStream, wtraits);
80         }
81     }
82
83     @Override
84     public boolean getImmutable(Resource resource)
85     throws DatabaseException {
86         if(!resource.isPersistent()) return false;
87         ClusterImpl clusterImpl = getCluster(resource);
88         return clusterImpl.getImmutable();
89     }
90
91     @Override
92     public void setImmutable(Resource resource, boolean immutable)
93     throws DatabaseException {
94         ClusterImpl clusterImpl = getCluster(resource);
95         clusterImpl.setImmutable(immutable, session.clusterTranslator);
96     }
97     private ClusterImpl getCluster(Resource resource)
98     throws DatabaseException {
99         if (null == resource)
100             return null;
101         int key = session.getService(SerialisationSupport.class).getTransientId(resource);
102         return (ClusterImpl)session.clusterTranslator.getClusterByResourceKey(key);
103     }
104
105     @Override
106     public void setServiceMode(boolean allow, boolean create) {
107         if  (DEBUG) {
108             new Throwable("printing stack trace").printStackTrace();
109             System.out.println("XSupportImpl.setServiceMode allow=" + allow + " create=" + create + ", old mode=" + session.serviceMode);
110         }
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();
116         }
117     }
118     
119     @Override
120     public Pair<Boolean, Boolean> getServiceMode() {
121         return Pair.make((session.serviceMode & 1) == 1, (session.serviceMode & 2) == 2);
122     }
123     
124     @Override
125     public Resource convertDelayedResourceToResource(Resource resource) {
126         return DelayedWriteGraph.convertDelayedResource(resource);
127     }
128     @Override
129     public String execute(String command)
130     throws DatabaseException {
131         boolean transaction = true;
132         try {
133             session.state.startReadTransaction(Integer.MIN_VALUE);
134         } catch (Throwable t) {
135             Logger.defaultLogError("Trying to muddle on.", t);
136             transaction = false;
137         }
138         try {
139             return session.graphSession.execute(command);
140         } finally {
141             if (transaction)
142                 session.state.stopReadTransaction();
143         }
144     }
145     @Override
146     public void testCluster(Session session)
147     throws DatabaseException {
148         TestCluster.test(session);
149     }
150     @Override
151     public ClusterUID[] listClusters() throws DatabaseException {
152         return session.graphSession.listClusters();
153     }
154     @Override
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);
159     }
160     @Override
161     public void purge() throws DatabaseException {
162         if (null == session)
163             return;
164         if (null == session.graphSession)
165             return;
166         Database db = session.graphSession.dbSession.getDatabase();
167         db.purgeDatabase();
168     }
169
170     @Override
171     public boolean rolledback() {
172         return session.graphSession.rolledback();
173     }
174
175         @Override
176         public boolean isClusterLoaded(ClusterUID clusterUID) throws DatabaseException {
177                 ClusterImpl clusterImpl = session.clusterTable.getClusterByClusterUIDOrMakeProxy(clusterUID);
178                 return clusterImpl.isLoaded();
179         }
180
181     @Override
182     public void saveQueries() throws DatabaseException {
183         try {
184             session.queryProvider2.save();
185         } catch (IOException e) {
186             throw new DatabaseException(e);
187         }
188     }
189
190 }