1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package fi.vtt.simantics.procore.internal;
14 import java.io.InputStream;
15 import java.util.function.Consumer;
17 import org.simantics.db.Resource;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.impl.ClusterI;
20 import org.simantics.db.impl.ClusterSupport;
21 import org.simantics.db.impl.ClusterTraitsBase;
22 import org.simantics.db.impl.ForEachObjectContextProcedure;
23 import org.simantics.db.impl.ForEachObjectProcedure;
24 import org.simantics.db.impl.ForPossibleRelatedValueContextProcedure;
25 import org.simantics.db.impl.ForPossibleRelatedValueProcedure;
26 import org.simantics.db.impl.Table;
27 import org.simantics.db.impl.graph.ReadGraphImpl;
28 import org.simantics.db.procedure.SyncMultiProcedure;
29 import org.simantics.db.procore.cluster.ClusterBig;
30 import org.simantics.db.procore.cluster.ClusterImpl;
31 import org.simantics.db.procore.cluster.ClusterTraits;
32 import org.simantics.db.service.ClusterUID;
34 import fi.vtt.simantics.procore.DebugPolicy;
36 final public class ClusterWriteOnly extends ClusterImpl {
38 final SessionImplSocket sessionImpl;
39 final ClusterTable clusterTable;
40 private final int clusterKeyHigh;
41 private short currentIndex;
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;
53 setImportance(Long.MAX_VALUE);
59 public void releaseMemory() {
62 public void compact() {
65 public boolean isLoaded() {
66 return true; // write only cluster is never loaded
69 public boolean isWriteOnly() {
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++;
81 public boolean hasResource(int r, ClusterSupport support)
82 throws DatabaseException {
83 int resourceIndex = ClusterTraits.getResourceIndexFromResourceKey(r);
84 if (ClusterTraits.getClusterKeyFromResourceKey(r) != this.clusterKey)
86 if (resourceIndex > 0 && resourceIndex <= currentIndex)
91 private void addChange(int s, int p, int o, ClusterSupport a, byte operation) throws DatabaseException {
93 change.op0 = operation;
98 if(ClusterTraits.isCluster(clusterKeyHigh, p)) {
99 change.clusterUID1 = getClusterUID();
101 change.clusterUID1 = clusterTable.getClusterUIDByResourceKey(p);
104 if(ClusterTraits.isCluster(clusterKeyHigh, o)) {
105 change.clusterUID2 = getClusterUID();
107 change.clusterUID2 = clusterTable.getClusterUIDByResourceKey(o);
110 cc.addChange(change);
113 public ClusterI addRelation(int s, int p, int o, ClusterSupport a) throws DatabaseException {
114 addChange(s, p, o, a, ClusterChange.ADD_OPERATION);
118 synchronized public boolean removeRelation(int s, int p, int o, ClusterSupport a) throws DatabaseException {
119 addChange(s, p, o, a, ClusterChange.REMOVE_OPERATION);
123 synchronized public void denyRelation(int s, int p, int o, ClusterSupport a) throws DatabaseException {
124 addChange(s, p, o, a, ClusterChange.REMOVE_OPERATION);
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);
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);
141 public byte[] readValueEx(int s, long voffset, int length, ClusterSupport support)
142 throws DatabaseException {
143 throw new Error("Not implemented");
146 public long getValueSizeEx(int rResourceId, ClusterSupport support)
147 throws DatabaseException {
148 throw new Error("Not implemented");
151 synchronized public void setValueEx(int s)
152 throws DatabaseException {
155 synchronized public boolean removeValue(int s, ClusterSupport a) {
156 throw new Error("Not implemented");
159 synchronized public ClusterI getClusterByResourceKey(int resourceKey, ClusterSupport support) {
160 int clusterShortId = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(resourceKey);
161 if (this.clusterKey == clusterShortId)
163 return support.getClusterByResourceKey(resourceKey);
166 synchronized public int getNumberOfResources(ClusterSupport support) {
167 return currentIndex - 1;
170 public boolean isEmpty() {
174 public long getUsedSpace()
175 throws DatabaseException {
176 throw new DatabaseException("Not implemented.");
179 public void decreaseReferenceCount(int amount) {
182 public void increaseReferenceCount(int amount) {
185 public int getReferenceCount() {
189 public byte[] getValue(int sr, ClusterSupport a) {
190 throw new Error("Not implemented.");
193 public InputStream getValueStream(int resourceKey, ClusterSupport support) throws DatabaseException {
194 throw new Error("Not implemented.");
197 public boolean hasValue(int r, ClusterSupport a) {
198 throw new Error("Not implemented.");
201 public void printDebugInfo(String message, ClusterSupport support) {
202 throw new Error("Not implemented.");
208 public void load(Consumer<DatabaseException> r) {
212 public int getSingleObject(int resourceKey, int predicateKey, ClusterSupport support) throws DatabaseException {
213 throw new DatabaseException("Not implemented.");
216 public <T> int getSingleObject(int resourceKey,
217 ForPossibleRelatedValueProcedure<T> procedure,
218 ClusterSupport support) throws DatabaseException {
219 throw new DatabaseException("Not implemented.");
222 public <C, T> int getSingleObject(int resourceKey,
223 ForPossibleRelatedValueContextProcedure<C, T> procedure,
224 ClusterSupport support) throws DatabaseException {
225 throw new DatabaseException("Not implemented.");
228 public void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey,
229 SyncMultiProcedure<Resource> procedure) throws DatabaseException {
230 throw new DatabaseException("Not implemented.");
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.");
238 public void forObjects(ReadGraphImpl graph, int resourceKey,
239 ForEachObjectProcedure procedure) throws DatabaseException {
240 throw new DatabaseException("Not implemented.");
243 public <C> void forObjects(ReadGraphImpl graph, int resourceKey, C context,
244 ForEachObjectContextProcedure<C> procedure) throws DatabaseException {
245 throw new DatabaseException("Not implemented.");
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.");
253 public <Context> boolean forPredicates(int resourceKey,
254 PredicateProcedure<Context> procedure, Context context, ClusterSupport support) throws DatabaseException {
255 throw new DatabaseException("Not implemented.");
258 public int getCompleteObjectKey(int resourceKey, ClusterSupport support) throws DatabaseException {
259 throw new DatabaseException("Not implemented.");
262 public CompleteTypeEnum getCompleteType(int resourceKey, ClusterSupport support)
263 throws DatabaseException {
264 throw new DatabaseException("Not implemented.");
267 public boolean isComplete(int resourceKey, ClusterSupport support) throws DatabaseException {
268 throw new DatabaseException("Not implemented.");
271 public boolean hasVirtual() {
275 public void markVirtual() {
278 public void load(ClusterSupport session, Runnable callback) {
282 public boolean contains(int resource) {
287 public ClusterTypeEnum getType() {
288 return ClusterTypeEnum.WRITEONLY;
291 public int execute(int valueToModify) throws DatabaseException {
292 throw new Error("Not supported");
295 public ClusterBig toBig(ClusterSupport support) throws DatabaseException {
296 throw new Error("Not supported");
299 public void checkDirectReference(int dr) throws DatabaseException {
300 throw new Error("Not supported");
303 public void checkForeingIndex(int fi) throws DatabaseException {
304 throw new Error("Not supported");
307 public void checkObjectSetReference(int or) throws DatabaseException {
308 throw new Error("Not supported");
311 public boolean getImmutable() {
315 public void setImmutable(boolean immutable, ClusterSupport support) {
316 sessionImpl.clusterTranslator.setImmutable(this, immutable);
319 public boolean getDeleted() {
323 public void setDeleted(boolean deleted, ClusterSupport support) {
324 sessionImpl.clusterTranslator.setDeleted(this, deleted);
327 public void checkValueInit() throws DatabaseException {
328 throw new UnsupportedOperationException();
332 public void checkCompleteSetReference(int cr) throws DatabaseException {
333 throw new UnsupportedOperationException();
337 public void checkPredicateIndex(int pi) throws DatabaseException {
338 throw new UnsupportedOperationException();
342 public void checkValue(int capacity, int index) throws DatabaseException {
343 throw new UnsupportedOperationException();
347 public void checkValueFini() throws DatabaseException {
348 throw new UnsupportedOperationException();
352 public Table<?> getPredicateTable() {
353 throw new UnsupportedOperationException();
356 public Table<?> getForeignTable() {
357 throw new UnsupportedOperationException();
360 public Table<?> getCompleteTable() {
361 throw new UnsupportedOperationException();
364 public Table<?> getValueTable() {
365 throw new UnsupportedOperationException();
368 public int makeResourceKey(int pRef) throws DatabaseException {
369 throw new UnsupportedOperationException();
372 public Table<?> getObjectTable() {
373 throw new UnsupportedOperationException();