]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterWriteOnly.java
Merge "Save cluster sets only when creating DB snapshots"
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ClusterWriteOnly.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package fi.vtt.simantics.procore.internal;
13
14 import java.io.InputStream;
15
16 import org.simantics.db.Resource;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.ClusterI;
19 import org.simantics.db.impl.ClusterSupport;
20 import org.simantics.db.impl.ClusterTraitsBase;
21 import org.simantics.db.impl.ForEachObjectContextProcedure;
22 import org.simantics.db.impl.ForEachObjectProcedure;
23 import org.simantics.db.impl.ForPossibleRelatedValueContextProcedure;
24 import org.simantics.db.impl.ForPossibleRelatedValueProcedure;
25 import org.simantics.db.impl.Table;
26 import org.simantics.db.impl.graph.ReadGraphImpl;
27 import org.simantics.db.procedure.AsyncMultiProcedure;
28 import org.simantics.db.procore.cluster.ClusterBig;
29 import org.simantics.db.procore.cluster.ClusterImpl;
30 import org.simantics.db.procore.cluster.ClusterTraits;
31 import org.simantics.db.service.ClusterUID;
32 import org.simantics.utils.datastructures.Callback;
33
34 import fi.vtt.simantics.procore.DebugPolicy;
35
36 final public class ClusterWriteOnly extends ClusterImpl {
37
38     final SessionImplSocket sessionImpl;
39     final ClusterTable clusterTable;
40     private final int clusterKeyHigh;
41     private short currentIndex;
42
43     ClusterWriteOnly(ClusterUID clusterUID, int clusterKey, SessionImplSocket sessionImpl) {
44         super(clusterUID , clusterKey, sessionImpl.clusterTranslator);
45         if(DebugPolicy.REPORT_CLUSTER_EVENTS)
46             new Exception(clusterUID.toString()).printStackTrace();
47         assert(sessionImpl != null);
48         this.sessionImpl = sessionImpl;
49         this.clusterKeyHigh = ClusterTraits.getClusterBits(clusterKey);
50         this.cc = new ClusterChange(sessionImpl.clusterStream, this);
51         clusterTable = sessionImpl.clusterTable;
52         currentIndex = 1;
53         setImportance(Long.MAX_VALUE);
54     }
55     boolean isNew() {
56         return true;
57     }
58     @Override
59     public void releaseMemory() {
60     }
61     @Override
62     public void compact() {
63     }
64     @Override
65     public boolean isLoaded() {
66         return true; // write only cluster is never loaded
67     }
68     @Override
69     public boolean isWriteOnly() {
70         return true;
71     }
72     @Override
73     public int createResource(ClusterSupport support) throws DatabaseException {
74         cc.createResource(currentIndex);
75 //        System.err.println("write only resource [" + clusterId + "] " + currentIndex);
76         if(DebugPolicy.REPORT_RESOURCE_ID_ALLOCATION)
77             System.out.println("[RID_ALLOCATION]: ClusterWriteOnly[" + clusterId + "] allocates " + currentIndex);
78         return clusterKeyHigh + currentIndex++;
79     }
80     @Override
81     public boolean hasResource(int r, ClusterSupport support)
82     throws DatabaseException {
83         int resourceIndex = ClusterTraits.getResourceIndexFromResourceKey(r);
84         if (ClusterTraits.getClusterKeyFromResourceKey(r) != this.clusterKey)
85             return false;
86         if (resourceIndex > 0 && resourceIndex <= currentIndex)
87             return true;
88         else
89             return false;
90     }
91     private void addChange(int s, int p, int o, ClusterSupport a, byte operation) throws DatabaseException {
92
93         change.op0 = operation;
94         change.key0 = s;
95         change.key1 = p;
96         change.key2 = o;
97
98         if(ClusterTraits.isCluster(clusterKeyHigh, p)) {
99             change.clusterUID1 = getClusterUID();
100         } else {
101             change.clusterUID1 = clusterTable.getClusterUIDByResourceKey(p);
102         }
103
104         if(ClusterTraits.isCluster(clusterKeyHigh, o)) {
105             change.clusterUID2 = getClusterUID();
106         } else {
107             change.clusterUID2 = clusterTable.getClusterUIDByResourceKey(o);
108         }
109
110         cc.addChange(change);
111     }
112     @Override
113     public ClusterI addRelation(int s, int p, int o, ClusterSupport a) throws DatabaseException {
114         addChange(s, p, o, a, ClusterChange.ADD_OPERATION);
115         return this;
116     }
117     @Override
118     synchronized public boolean removeRelation(int s, int p, int o, ClusterSupport a) throws DatabaseException {
119         addChange(s, p, o, a, ClusterChange.REMOVE_OPERATION);
120         return true;
121     }
122     @Override
123     synchronized public void denyRelation(int s, int p, int o, ClusterSupport a) throws DatabaseException {
124         addChange(s, p, o, a, ClusterChange.REMOVE_OPERATION);
125     }
126     @Override
127     synchronized public ClusterI setValue(int s, byte[] value, int length, ClusterSupport a)
128     throws DatabaseException {
129         sessionImpl.clusterTranslator.addStatementIndex(this, s, getClusterUID(), ClusterStream.SET_OPERATION);
130         sessionImpl.clusterTranslator.setValue(this, clusterId, value, length);
131         return this;
132     }
133     @Override
134     public ClusterI modiValueEx(int s, long voffset, int length, byte[] value, int offset, ClusterSupport support)
135     throws DatabaseException {
136         sessionImpl.clusterTranslator.addStatementIndex(this, s, getClusterUID(), ClusterStream.MODI_OPERATION);
137         support.modiValue(this, getClusterId(), voffset, length, value, offset);
138         return this;
139     }
140     @Override
141     public byte[] readValueEx(int s, long voffset, int length, ClusterSupport support)
142     throws DatabaseException {
143         throw new Error("Not implemented");
144     }
145     @Override
146     public long getValueSizeEx(int rResourceId, ClusterSupport support)
147     throws DatabaseException {
148         throw new Error("Not implemented");
149     }
150     @Override
151     synchronized public void setValueEx(int s)
152     throws DatabaseException {
153     }
154     @Override
155     synchronized public boolean removeValue(int s, ClusterSupport a) {
156         throw new Error("Not implemented");
157     }
158     @Override
159     synchronized public ClusterI getClusterByResourceKey(int resourceKey, ClusterSupport support) {
160         int clusterShortId = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(resourceKey);
161         if (this.clusterKey == clusterShortId)
162             return this;
163         return support.getClusterByResourceKey(resourceKey);
164     }
165     @Override
166     synchronized public int getNumberOfResources(ClusterSupport support) {
167         return currentIndex - 1;
168     }
169     @Override
170     public boolean isEmpty() {
171         return true;
172     }
173     @Override
174     public long getUsedSpace()
175     throws DatabaseException {
176         throw new DatabaseException("Not implemented.");
177     }
178     @Override
179     public void decreaseReferenceCount(int amount) {
180     }
181     @Override
182     public void increaseReferenceCount(int amount) {
183     }
184     @Override
185     public int getReferenceCount() {
186         return 1;
187     }
188     @Override
189     public byte[] getValue(int sr, ClusterSupport a) {
190         throw new Error("Not implemented.");
191     }
192     @Override
193     public InputStream getValueStream(int resourceKey, ClusterSupport support) throws DatabaseException {
194         throw new Error("Not implemented.");
195     }
196     @Override
197     public boolean hasValue(int r, ClusterSupport a) {
198         throw new Error("Not implemented.");
199     }
200     @Override
201     public void printDebugInfo(String message, ClusterSupport support) {
202         throw new Error("Not implemented.");
203     }
204     @Override
205     public void load() {
206     }
207     @Override
208     public void load(Callback<DatabaseException> r) {
209     }
210
211     @Override
212     public int getSingleObject(int resourceKey, int predicateKey, ClusterSupport support) throws DatabaseException {
213         throw new DatabaseException("Not implemented.");
214     }
215     @Override
216     public <T> int getSingleObject(int resourceKey,
217             ForPossibleRelatedValueProcedure<T> procedure,
218             ClusterSupport support) throws DatabaseException {
219         throw new DatabaseException("Not implemented.");
220     }
221     @Override
222     public <C, T> int getSingleObject(int resourceKey,
223             ForPossibleRelatedValueContextProcedure<C, T> procedure,
224             ClusterSupport support) throws DatabaseException {
225         throw new DatabaseException("Not implemented.");
226     }
227     @Override
228     public void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey,
229             AsyncMultiProcedure<Resource> procedure) throws DatabaseException {
230         throw new DatabaseException("Not implemented.");
231     }
232     @Override
233     public <Context> boolean forObjects(int resourceKey, int predicateKey, int objectIndex,
234             ObjectProcedure<Context> procedure, Context context, ClusterSupport support) throws DatabaseException {
235         throw new DatabaseException("Not implemented.");
236     }
237     @Override
238     public void forObjects(ReadGraphImpl graph, int resourceKey,
239             ForEachObjectProcedure procedure) throws DatabaseException {
240         throw new DatabaseException("Not implemented.");
241     }
242     @Override
243     public <C> void forObjects(ReadGraphImpl graph, int resourceKey, C context,
244             ForEachObjectContextProcedure<C> procedure) throws DatabaseException {
245         throw new DatabaseException("Not implemented.");
246     }
247     @Override
248     public <Context> boolean forObjects(int resourceKey, int predicateKey,
249             ObjectProcedure<Context> procedure, Context context, ClusterSupport support) throws DatabaseException {
250         throw new DatabaseException("Not implemented.");
251     }
252     @Override
253     public <Context> boolean forPredicates(int resourceKey,
254             PredicateProcedure<Context> procedure, Context context, ClusterSupport support) throws DatabaseException {
255         throw new DatabaseException("Not implemented.");
256     }
257     @Override
258     public int getCompleteObjectKey(int resourceKey, ClusterSupport support) throws DatabaseException {
259         throw new DatabaseException("Not implemented.");
260     }
261     @Override
262     public CompleteTypeEnum getCompleteType(int resourceKey, ClusterSupport support)
263             throws DatabaseException {
264         throw new DatabaseException("Not implemented.");
265     }
266     @Override
267     public boolean isComplete(int resourceKey, ClusterSupport support) throws DatabaseException {
268         throw new DatabaseException("Not implemented.");
269     }
270     @Override
271     public boolean hasVirtual() {
272         return false;
273     }
274     @Override
275     public void markVirtual() {
276     }
277     @Override
278     public void load(ClusterSupport session, Runnable callback) {
279         throw new Error();
280     }
281     @Override
282     public boolean contains(int resource) {
283         throw new Error();
284     }
285
286     @Override
287     public ClusterTypeEnum getType() {
288         return ClusterTypeEnum.WRITEONLY;
289     }
290     @Override
291     public int execute(int valueToModify) throws DatabaseException {
292         throw new Error("Not supported");
293     }
294     @Override
295     public ClusterBig toBig(ClusterSupport support) throws DatabaseException {
296         throw new Error("Not supported");
297     }
298     @Override
299     public void checkDirectReference(int dr) throws DatabaseException {
300         throw new Error("Not supported");
301     }
302     @Override
303     public void checkForeingIndex(int fi) throws DatabaseException {
304         throw new Error("Not supported");
305     }
306     @Override
307     public void checkObjectSetReference(int or) throws DatabaseException {
308         throw new Error("Not supported");
309     }
310     @Override
311     public boolean getImmutable() {
312         return false;
313     }
314     @Override
315     public void setImmutable(boolean immutable, ClusterSupport support) {
316         sessionImpl.clusterTranslator.setImmutable(this, immutable);
317     }
318     @Override
319     public boolean getDeleted() {
320         return false;
321     }
322     @Override
323     public void setDeleted(boolean deleted, ClusterSupport support) {
324         sessionImpl.clusterTranslator.setDeleted(this, deleted);
325     }
326     @Override
327     public void checkValueInit() throws DatabaseException {
328         throw new UnsupportedOperationException();
329         
330     }
331     @Override
332     public void checkCompleteSetReference(int cr) throws DatabaseException {
333         throw new UnsupportedOperationException();
334         
335     }
336     @Override
337     public void checkPredicateIndex(int pi) throws DatabaseException {
338         throw new UnsupportedOperationException();
339         
340     }
341     @Override
342     public void checkValue(int capacity, int index) throws DatabaseException {
343         throw new UnsupportedOperationException();
344         
345     }
346     @Override
347     public void checkValueFini() throws DatabaseException {
348         throw new UnsupportedOperationException();
349         
350     }
351     @Override
352     public Table<?> getPredicateTable() {
353         throw new UnsupportedOperationException();
354     }
355     @Override
356     public Table<?> getForeignTable() {
357         throw new UnsupportedOperationException();
358     }
359     @Override
360     public Table<?> getCompleteTable() {
361         throw new UnsupportedOperationException();
362     }
363     @Override
364     public Table<?> getValueTable() {
365         throw new UnsupportedOperationException();
366     }
367     @Override
368     public int makeResourceKey(int pRef) throws DatabaseException {
369         throw new UnsupportedOperationException();
370     }
371     @Override
372     public Table<?> getObjectTable() {
373         throw new UnsupportedOperationException();
374     }
375 }
376