]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ExternalValueSupportImpl.java
Merge commit '31664b6'
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ExternalValueSupportImpl.java
1 package fi.vtt.simantics.procore.internal;\r
2 \r
3 import org.simantics.db.ExternalValueSupport;\r
4 import org.simantics.db.ReadGraph;\r
5 import org.simantics.db.Resource;\r
6 import org.simantics.db.WriteGraph;\r
7 import org.simantics.db.exception.DatabaseException;\r
8 import org.simantics.db.impl.ClusterI;\r
9 import org.simantics.db.impl.ClusterTranslator;\r
10 import org.simantics.db.impl.ResourceImpl;\r
11 \r
12 import fi.vtt.simantics.procore.DebugPolicy;\r
13 \r
14 public class ExternalValueSupportImpl implements ExternalValueSupport {\r
15     private final boolean DEBUG = DebugPolicy.REPORT_CLUSTER_EVENTS;\r
16     private final SessionImplSocket session;\r
17     private ClusterTranslator clusterTranslator;\r
18     private final ClusterTable clusterTable;\r
19     ExternalValueSupportImpl(SessionImplSocket session) {\r
20         this.session = session;\r
21         this.clusterTranslator = session.clusterTranslator;\r
22         this.clusterTable = session.getClusterTable();\r
23     }\r
24     private final void check4Translator() {\r
25         if (null == clusterTranslator)\r
26             this.clusterTranslator = session.clusterTranslator;\r
27     }\r
28     @Override\r
29     public void writeValue(WriteGraph graph, Resource resource, long offset, int length, byte[] bytes)\r
30     throws DatabaseException {\r
31         check4Translator();\r
32         assert(length <= bytes.length);\r
33         ResourceImpl resourceImpl = (ResourceImpl)resource;\r
34         ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);\r
35         cluster.modiValueEx(resourceImpl.id, offset, length, bytes, 0, clusterTranslator);\r
36     }\r
37 // This breaks undo.\r
38 //    @Override\r
39 //    public void moveValue(WriteGraph graph, Resource resource)\r
40 //    throws DatabaseException {\r
41 //        this.writeValue(graph, resource, (1L<<58)-1, 0, new byte[0]);\r
42 //    }\r
43 // This breaks undo.\r
44 //    @Override\r
45 //    public void commitAndContinue(WriteGraph graph, WriteTraits wtraits, Resource resource)\r
46 //    throws DatabaseException {\r
47 //        XSupport xs = graph.getService(XSupport.class);\r
48 //        xs.commitAndContinue(graph, wtraits);\r
49 //        clusterTranslator.wait4RequestsLess(1);\r
50 //    }\r
51     @Override\r
52     public byte[] readValue(ReadGraph graph, Resource resource, long offset, int length)\r
53     throws DatabaseException {\r
54         check4Translator();\r
55         ResourceImpl resourceImpl = (ResourceImpl)resource;\r
56         ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);\r
57         return cluster.readValueEx(resourceImpl.id, offset, length, clusterTranslator);\r
58     }\r
59     public long getValueSize(ReadGraph graph, Resource resource)\r
60     throws DatabaseException {\r
61         check4Translator();\r
62         ResourceImpl resourceImpl = (ResourceImpl)resource;\r
63         ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);\r
64         long size = cluster.getValueSizeEx(resourceImpl.id, clusterTranslator);\r
65         if (DEBUG)\r
66             System.out.println("DEBUG:  resource=" + resource + " value length=" + size + ".");\r
67         return size;\r
68     }\r
69     @Override\r
70     public void removeValue(WriteGraph graph, Resource resource)\r
71     throws DatabaseException {\r
72         check4Translator();\r
73         ResourceImpl resourceImpl = (ResourceImpl)resource;\r
74         ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);\r
75         cluster.modiValueEx(resourceImpl.id, 0, 0, new byte[0], 0, clusterTranslator);\r
76         cluster.removeValue(resourceImpl.id, clusterTranslator);\r
77     }\r
78     @Override\r
79     public int wait4RequestsLess(int limit)\r
80     throws DatabaseException {\r
81         check4Translator();\r
82         return clusterTranslator.wait4RequestsLess(limit);\r
83     }\r
84 }\r