]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/VirtualClusterBuilderImpl2.java
Dispose ClientChangesImpl ChangeSets to minimize memory footprint
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / VirtualClusterBuilderImpl2.java
1 package fi.vtt.simantics.procore.internal;
2
3 import gnu.trove.list.array.TByteArrayList;
4
5 import org.simantics.db.Resource;
6 import org.simantics.db.VirtualGraph;
7 import org.simantics.db.WriteOnlyGraph;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.exception.RuntimeDatabaseException;
10 import org.simantics.db.impl.ResourceImpl;
11 import org.simantics.db.impl.graph.WriteGraphImpl;
12 import org.simantics.db.impl.graph.WriteSupport;
13 import org.simantics.db.service.ClusterBuilder2;
14 import org.simantics.db.service.SerialisationSupport;
15
16 public class VirtualClusterBuilderImpl2 implements ClusterBuilder2 {
17
18         final private SessionImplSocket session;
19         final private VirtualGraph vg;
20 //    final private ClusterSupport cs;
21     final private SerialisationSupport ss;
22 //    final private ClusterStream stream;
23 //    final private WriteSupport support;
24 //    final private WriteSupport support;
25     @SuppressWarnings("unused")
26     final private boolean allowImmutables;
27     
28     VirtualClusterBuilderImpl2(SessionImplSocket session, VirtualGraph vg, boolean allowImmutables) {
29         this.session = session;
30         this.vg = vg;
31         this.allowImmutables = allowImmutables;
32         ss = session.getService(SerialisationSupport.class);
33     }
34     
35     private WriteSupport getSupport() {
36         WriteGraphImpl graph = session.writeState.getGraph();
37         return graph.writeSupport;
38     }
39
40     @Override
41     public void newCluster() throws DatabaseException {
42         // Do nothing
43     }
44
45     @Override
46     public void selectCluster(long cluster) throws DatabaseException {
47         // Do nothing
48     }
49     
50     @Override
51     public void newCluster(int setHandle) throws DatabaseException {
52         // Do nothing
53     }
54     
55     @Override
56     public void createClusterSet(int resource) throws DatabaseException {
57         // Do nothing
58     }
59     
60     @Override
61     public int newResource() throws DatabaseException {
62         Resource result = getSupport().createResource(vg); 
63         return handle(result);
64     }
65
66     @Override
67     public int newResource(int set) throws DatabaseException {
68         Resource result = getSupport().createResource(vg, resource(set));
69         return handle(result);
70     }
71     
72     @Override
73     public int resource(Resource res) throws DatabaseException {
74         ResourceImpl r = (ResourceImpl)res;
75         return r.id;
76     }
77
78     @Override
79     public void addStatement(WriteOnlyGraph graph, int subject, int predicate, int object) throws DatabaseException {
80         getSupport().claim(vg, subject, predicate, object);
81     }
82     
83 //    public void applyPredicate(ClusterImpl impl, int predicate) {
84 //        
85 //        int clusterKey = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(predicate);
86 //        ClusterImpl cluster = clusterArray[clusterKey]; 
87 //        
88 //        impl.change.addStatementIndex1(predicate, cluster.clusterUID, (byte)0, impl.foreignLookup);
89 //        
90 //    }
91 //
92 //    public void applyObject(ClusterImpl impl, int object) {
93 //
94 //        int clusterKey = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(object);
95 //        ClusterImpl cluster = clusterArray[clusterKey]; 
96 //        
97 //        impl.change.addStatementIndex2(object, cluster.clusterUID, (byte)0, impl.foreignLookup);
98 //        
99 //    }
100
101     @Override
102     public Resource resource(int key) {
103         try {
104             return ss.getResource(key);
105         } catch (DatabaseException e) {
106             throw new RuntimeDatabaseException(e);
107         }
108     }
109     
110     @Override
111     public int handle(Resource r) {
112         return ((ResourceImpl)r).id;
113     }
114
115
116     TByteArrayList valueBytes = new TByteArrayList();
117     
118 //    byte[] buffer = new byte[65536];
119 //    int bufferOffset = 0;
120     int valueSubject = 0;
121 //    int valueOffset = 0;
122     
123     @Override
124     public void beginValue(int subject) {
125         valueBytes.clear();
126         valueSubject = subject;
127     }
128         
129     @Override
130     public void appendValue(int byteValue) throws DatabaseException {
131         valueBytes.add((byte)byteValue);
132     }
133
134     @Override
135     public void endValue() throws DatabaseException {
136         byte[] bytes = valueBytes.toArray();
137         getSupport().claimValue(vg, valueSubject, bytes, bytes.length);
138     }
139     
140 }