]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTranslatorImpl.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ClusterTranslatorImpl.java
1 package fi.vtt.simantics.procore.internal;
2
3 import java.io.InputStream;
4
5 import org.simantics.db.Session;
6 import org.simantics.db.common.utils.Logger;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.impl.ClusterBase;
9 import org.simantics.db.impl.ClusterTranslator;
10 import org.simantics.db.impl.IClusterTable;
11 import org.simantics.db.impl.ResourceImpl;
12 import org.simantics.db.procore.cluster.ClusterImpl;
13 import org.simantics.db.service.ClusterUID;
14
15 public class ClusterTranslatorImpl implements ClusterTranslator {
16
17     final private SessionImplSocket session;
18     final private ClusterStream clusterStream;
19     final private ClusterTable clusterTable;
20
21     ClusterTranslatorImpl(SessionImplSocket session) {
22         this.session = session;
23         this.clusterStream = session.clusterStream;
24         this.clusterTable = session.clusterTable;
25     }
26
27     @Override
28     public byte[] getValueEx(int resourceIndex, long clusterId)
29     throws DatabaseException {
30         return session.graphSession.getResourceValue(resourceIndex, clusterTable.makeClusterUID(clusterId));
31     }
32
33     @Override
34     public InputStream getValueStreamEx(int resourceIndex, long clusterId)
35     throws DatabaseException {
36         return session.graphSession.getResourceValueStream(resourceIndex, clusterTable.makeClusterUID(clusterId), 0, 0);
37     }
38
39     @Override
40     public byte[] getValueEx(int resourceIndex, long clusterId, long voffset, int length)
41     throws DatabaseException {
42         return session.graphSession.getResourceValue(resourceIndex, clusterTable.makeClusterUID(clusterId), voffset, length);
43     }
44
45     public long getValueSizeEx(int resourceIndex, long clusterId)
46     throws DatabaseException {
47         return session.graphSession.getResourceValueSize(resourceIndex, clusterTable.makeClusterUID(clusterId));
48     }
49     public int wait4RequestsLess(int limit)
50     throws DatabaseException {
51         return session.graphSession.wait4RequestsLess(limit);
52     }
53     @Override
54     public int createClusterKeyByClusterUID(ClusterUID clusterUID, long clusterId) {
55         return clusterTable.makeProxy(clusterUID, clusterId).getClusterKey();
56     }
57     @Override
58     public int getClusterKeyByClusterUIDOrMake(ClusterUID clusterUID) {
59         return clusterTable.getClusterKeyByClusterUIDOrMakeProxy(clusterUID);
60     }
61     @Override
62     public int getClusterKeyByClusterUIDOrMake(long id1, long id2) {
63         return clusterTable.getClusterKeyByClusterUIDOrMakeProxy(id1, id2);
64     }
65     @Override
66     public ClusterBase getClusterByClusterUIDOrMake(ClusterUID clusterUID) {
67         return clusterTable.getClusterByClusterUIDOrMakeProxy(clusterUID);
68     }
69     @Override
70     final public ClusterBase getClusterByClusterId(long clusterId) {
71         ClusterBase cluster =  clusterTable.getClusterByClusterId(clusterId);
72         if (null != cluster)
73             return cluster;
74         try {
75             return clusterTable.getLoadOrThrow(clusterId);
76         } catch (DatabaseException e) {
77             e.printStackTrace();
78             Logger.defaultLogError("Cluster not found by id. cluster id=" + clusterId, e);
79         }
80         return null;
81     }
82
83     @Override
84     public ClusterBase getClusterByClusterKey(int clusterKey) {
85         ClusterBase proxy = clusterTable.getClusterByClusterKey(clusterKey);
86         if (null == proxy)
87             return null; // throw new ResourceNotFoundException(id);
88         return proxy;
89     }
90
91     @Override
92     public ClusterBase getClusterByResourceKey(int resourceKey) {
93
94         ClusterBase proxy = clusterTable.getClusterByResourceKey(resourceKey);
95         if (null == proxy)
96             return null; // throw new ResourceNotFoundException(id);
97
98         return proxy;
99
100     }
101     @Override
102     public long getClusterIdOrCreate(ClusterUID clusterUID) {
103         return clusterTable.getClusterIdOrCreate(clusterUID);
104     }
105     @Override
106     public void createResource(Object cluster, short resourceIndex, long clusterId) {
107         ClusterImpl impl = (ClusterImpl)cluster;
108         if(impl.cc == null) impl.cc = new ClusterChange(clusterStream, impl);
109         if (null == clusterStream) {
110             System.out.println("Temporary skip of clusterStream.createResource");
111             return;
112         } else
113             clusterStream.createResource(impl.cc, resourceIndex, clusterTable.makeClusterUID(clusterId));
114     }
115
116     @Override
117     public void addStatementIndex(Object cluster, int resourceKey, ClusterUID clusterUID, byte op) {
118         ClusterImpl impl = (ClusterImpl)cluster;
119         if (null == impl.change)
120             return;
121         clusterStream.addStatementIndex(impl.change, resourceKey, clusterUID, op);
122     }
123
124     @Override
125     public void addStatement(Object cluster) {
126         ClusterImpl impl = (ClusterImpl)cluster;
127         if(impl.cc == null) impl.cc = new ClusterChange(clusterStream, impl);
128         clusterStream.addStatement(impl.cc, impl.change);
129     }
130
131
132     @Override
133     public void cancelStatement(Object cluster) {
134         ClusterImpl impl = (ClusterImpl)cluster;
135         if(impl.cc == null) impl.cc = new ClusterChange(clusterStream, impl);
136         clusterStream.cancelStatement(impl.change);
137     }
138
139     @Override
140     public void removeStatement(Object cluster) {
141         ClusterImpl impl = (ClusterImpl)cluster;
142         if(impl.cc == null) impl.cc = new ClusterChange(clusterStream, impl);
143         clusterStream.removeStatement(impl.cc, impl.change);
144     }
145
146     @Override
147     public void cancelValue(Object cluster) {
148         ClusterImpl impl = (ClusterImpl)cluster;
149         if(impl.cc == null) impl.cc = new ClusterChange(clusterStream, impl);
150         clusterStream.cancelValue(impl.change);
151     }
152
153     @Override
154     public void removeValue(Object cluster) {
155         ClusterImpl impl = (ClusterImpl)cluster;
156         if(impl.cc == null) impl.cc = new ClusterChange(clusterStream, impl);
157         clusterStream.removeValue(impl.cc, impl.change);
158     }
159
160     @Override
161     public void setValue(Object cluster, long clusterId, byte[] bytes, int length) {
162         ClusterImpl impl = (ClusterImpl)cluster;
163         if(impl.cc == null) impl.cc = new ClusterChange(clusterStream, impl);
164         clusterStream.setValue(impl.cc, impl.change, clusterId, bytes, length);
165     }
166
167     @Override
168     public void modiValue(Object cluster, long clusterId, long voffset, int length, byte[] bytes, int offset) {
169         ClusterImpl impl = (ClusterImpl)cluster;
170         if(impl.cc == null) impl.cc = new ClusterChange(clusterStream, impl);
171         clusterStream.modiValue(impl.cc, impl.change, clusterId, voffset, length, bytes, offset);
172     }
173
174     @Override
175     public void setStreamOff(boolean setOff) {
176         clusterStream.setOff(setOff);
177     }
178
179     @Override
180     public boolean getStreamOff() {
181         return clusterStream.getOff();
182     }
183
184     @Override
185     public void setImmutable(Object cluster, boolean immutable) {
186         ClusterImpl impl = (ClusterImpl)cluster;
187         clusterTable.markImmutable(impl, immutable);
188         if(impl.cc == null) impl.cc = new ClusterChange(clusterStream, impl);
189         clusterStream.setImmutable(impl.cc, impl.change, immutable);
190     }
191
192     @Override
193     public void setDeleted(Object cluster, boolean deleted) {
194         ClusterImpl impl = (ClusterImpl)cluster;
195         //clusterTable.markDeleted(impl, deleted);
196         if (impl.cc == null)
197             impl.cc = new ClusterChange(clusterStream, impl);
198         clusterStream.setDeleted(impl.cc, impl.change, deleted);
199     }
200
201     @Override
202     public void undoValueEx(Object cluster, int resourceIndex) {
203         ClusterImpl impl = (ClusterImpl)cluster;
204         if(impl.cc == null) impl.cc = new ClusterChange(clusterStream, impl);
205         clusterStream.undoValueEx(impl.cc, impl.change, resourceIndex);
206     }
207
208     @Override
209     public ResourceImpl getResource(int id) {
210         return new ResourceImpl(session.resourceSupport, id);
211     }
212
213     @Override
214     public ResourceImpl getResource(int callerThread, int id) {
215         assert (id != 0);
216         return new ResourceImpl(session.resourceSupport, id);
217     }
218
219     @Override
220     public Session getSession() {
221         return session;
222     }
223
224     @Override
225     public IClusterTable getClusterTable() {
226         return clusterTable;
227     }
228
229 }