1 package fi.vtt.simantics.procore.internal;
3 import org.simantics.db.ExternalValueSupport;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.WriteGraph;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.impl.ClusterI;
9 import org.simantics.db.impl.ClusterTranslator;
10 import org.simantics.db.impl.ResourceImpl;
12 import fi.vtt.simantics.procore.DebugPolicy;
14 public class ExternalValueSupportImpl implements ExternalValueSupport {
15 private final boolean DEBUG = DebugPolicy.REPORT_CLUSTER_EVENTS;
16 private final SessionImplSocket session;
17 private ClusterTranslator clusterTranslator;
18 private final ClusterTable clusterTable;
19 ExternalValueSupportImpl(SessionImplSocket session) {
20 this.session = session;
21 this.clusterTranslator = session.clusterTranslator;
22 this.clusterTable = session.getClusterTable();
24 private final void check4Translator() {
25 if (null == clusterTranslator)
26 this.clusterTranslator = session.clusterTranslator;
29 public void writeValue(WriteGraph graph, Resource resource, long offset, int length, byte[] bytes)
30 throws DatabaseException {
32 assert(length <= bytes.length);
33 ResourceImpl resourceImpl = (ResourceImpl)resource;
34 ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);
35 cluster.modiValueEx(resourceImpl.id, offset, length, bytes, 0, clusterTranslator);
39 // public void moveValue(WriteGraph graph, Resource resource)
40 // throws DatabaseException {
41 // this.writeValue(graph, resource, (1L<<58)-1, 0, new byte[0]);
45 // public void commitAndContinue(WriteGraph graph, WriteTraits wtraits, Resource resource)
46 // throws DatabaseException {
47 // XSupport xs = graph.getService(XSupport.class);
48 // xs.commitAndContinue(graph, wtraits);
49 // clusterTranslator.wait4RequestsLess(1);
52 public byte[] readValue(ReadGraph graph, Resource resource, long offset, int length)
53 throws DatabaseException {
55 ResourceImpl resourceImpl = (ResourceImpl)resource;
56 ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);
57 return cluster.readValueEx(resourceImpl.id, offset, length, clusterTranslator);
59 public long getValueSize(ReadGraph graph, Resource resource)
60 throws DatabaseException {
62 ResourceImpl resourceImpl = (ResourceImpl)resource;
63 ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);
64 long size = cluster.getValueSizeEx(resourceImpl.id, clusterTranslator);
66 System.out.println("DEBUG: resource=" + resource + " value length=" + size + ".");
70 public void removeValue(WriteGraph graph, Resource resource)
71 throws DatabaseException {
73 ResourceImpl resourceImpl = (ResourceImpl)resource;
74 ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);
75 cluster.modiValueEx(resourceImpl.id, 0, 0, new byte[0], 0, clusterTranslator);
76 cluster.removeValue(resourceImpl.id, clusterTranslator);
79 public int wait4RequestsLess(int limit)
80 throws DatabaseException {
82 return clusterTranslator.wait4RequestsLess(limit);