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