1 package fi.vtt.simantics.procore.internal;
3 import java.io.InputStream;
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;
15 public class ClusterTranslatorImpl implements ClusterTranslator {
17 final private SessionImplSocket session;
18 final private ClusterStream clusterStream;
19 final private ClusterTable clusterTable;
21 ClusterTranslatorImpl(SessionImplSocket session) {
22 this.session = session;
23 this.clusterStream = session.clusterStream;
24 this.clusterTable = session.clusterTable;
28 public byte[] getValueEx(int resourceIndex, long clusterId)
29 throws DatabaseException {
30 return session.graphSession.getResourceValue(resourceIndex, clusterTable.makeClusterUID(clusterId));
34 public InputStream getValueStreamEx(int resourceIndex, long clusterId)
35 throws DatabaseException {
36 return session.graphSession.getResourceValueStream(resourceIndex, clusterTable.makeClusterUID(clusterId), 0, 0);
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);
45 public long getValueSizeEx(int resourceIndex, long clusterId)
46 throws DatabaseException {
47 return session.graphSession.getResourceValueSize(resourceIndex, clusterTable.makeClusterUID(clusterId));
49 public int wait4RequestsLess(int limit)
50 throws DatabaseException {
51 return session.graphSession.wait4RequestsLess(limit);
54 public int createClusterKeyByClusterUID(ClusterUID clusterUID, long clusterId) {
55 return clusterTable.makeProxy(clusterUID, clusterId).getClusterKey();
58 public int getClusterKeyByClusterUIDOrMake(ClusterUID clusterUID) {
59 return clusterTable.getClusterKeyByClusterUIDOrMakeProxy(clusterUID);
62 public int getClusterKeyByClusterUIDOrMake(long id1, long id2) {
63 return clusterTable.getClusterKeyByClusterUIDOrMakeProxy(id1, id2);
66 public ClusterBase getClusterByClusterUIDOrMake(ClusterUID clusterUID) {
67 return clusterTable.getClusterByClusterUIDOrMakeProxy(clusterUID);
70 final public ClusterBase getClusterByClusterId(long clusterId) {
71 ClusterBase cluster = clusterTable.getClusterByClusterId(clusterId);
75 return clusterTable.getLoadOrThrow(clusterId);
76 } catch (DatabaseException e) {
78 Logger.defaultLogError("Cluster not found by id. cluster id=" + clusterId, e);
84 public ClusterBase getClusterByClusterKey(int clusterKey) {
85 ClusterBase proxy = clusterTable.getClusterByClusterKey(clusterKey);
87 return null; // throw new ResourceNotFoundException(id);
92 public ClusterBase getClusterByResourceKey(int resourceKey) {
94 ClusterBase proxy = clusterTable.getClusterByResourceKey(resourceKey);
96 return null; // throw new ResourceNotFoundException(id);
102 public long getClusterIdOrCreate(ClusterUID clusterUID) {
103 return clusterTable.getClusterIdOrCreate(clusterUID);
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");
113 clusterStream.createResource(impl.cc, resourceIndex, clusterTable.makeClusterUID(clusterId));
117 public void addStatementIndex(Object cluster, int resourceKey, ClusterUID clusterUID, byte op) {
118 ClusterImpl impl = (ClusterImpl)cluster;
119 if (null == impl.change)
121 clusterStream.addStatementIndex(impl.change, resourceKey, clusterUID, op);
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);
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);
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);
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);
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);
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);
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);
175 public void setStreamOff(boolean setOff) {
176 clusterStream.setOff(setOff);
180 public boolean getStreamOff() {
181 return clusterStream.getOff();
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);
193 public void setDeleted(Object cluster, boolean deleted) {
194 ClusterImpl impl = (ClusterImpl)cluster;
195 //clusterTable.markDeleted(impl, deleted);
197 impl.cc = new ClusterChange(clusterStream, impl);
198 clusterStream.setDeleted(impl.cc, impl.change, deleted);
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);
209 public ResourceImpl getResource(int id) {
210 return new ResourceImpl(session.resourceSupport, id);
214 public ResourceImpl getResource(int callerThread, int id) {
216 return new ResourceImpl(session.resourceSupport, id);
220 public Session getSession() {
225 public IClusterTable getClusterTable() {