]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ExternalValueSupportImpl.java
isImmutable can NPE
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ExternalValueSupportImpl.java
1 package fi.vtt.simantics.procore.internal;
2
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;
11
12 import fi.vtt.simantics.procore.DebugPolicy;
13
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();
23     }
24     private final void check4Translator() {
25         if (null == clusterTranslator)
26             this.clusterTranslator = session.clusterTranslator;
27     }
28     @Override
29     public void writeValue(WriteGraph graph, Resource resource, long offset, int length, byte[] bytes)
30     throws DatabaseException {
31         check4Translator();
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);
36     }
37 // This breaks undo.
38 //    @Override
39 //    public void moveValue(WriteGraph graph, Resource resource)
40 //    throws DatabaseException {
41 //        this.writeValue(graph, resource, (1L<<58)-1, 0, new byte[0]);
42 //    }
43 // This breaks undo.
44 //    @Override
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);
50 //    }
51     @Override
52     public byte[] readValue(ReadGraph graph, Resource resource, long offset, int length)
53     throws DatabaseException {
54         check4Translator();
55         ResourceImpl resourceImpl = (ResourceImpl)resource;
56         ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);
57         return cluster.readValueEx(resourceImpl.id, offset, length, clusterTranslator);
58     }
59     public long getValueSize(ReadGraph graph, Resource resource)
60     throws DatabaseException {
61         check4Translator();
62         ResourceImpl resourceImpl = (ResourceImpl)resource;
63         ClusterI cluster = clusterTable.getClusterByResourceKey(resourceImpl.id);
64         long size = cluster.getValueSizeEx(resourceImpl.id, clusterTranslator);
65         if (DEBUG)
66             System.out.println("DEBUG:  resource=" + resource + " value length=" + size + ".");
67         return size;
68     }
69     @Override
70     public void removeValue(WriteGraph graph, Resource resource)
71     throws DatabaseException {
72         check4Translator();
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);
77     }
78     @Override
79     public int wait4RequestsLess(int limit)
80     throws DatabaseException {
81         check4Translator();
82         return clusterTranslator.wait4RequestsLess(limit);
83     }
84 }