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