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