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 org.simantics.acorn.cluster;
14 import java.io.ByteArrayInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Arrays;
18 import java.util.function.Consumer;
20 import org.simantics.acorn.exception.IllegalAcornStateException;
21 import org.simantics.acorn.internal.ClusterChange;
22 import org.simantics.acorn.internal.ClusterStream;
23 import org.simantics.acorn.internal.ClusterSupport2;
24 import org.simantics.acorn.internal.DebugPolicy;
25 import org.simantics.db.Resource;
26 import org.simantics.db.common.utils.Logger;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.exception.ExternalValueException;
29 import org.simantics.db.exception.ValidationException;
30 import org.simantics.db.impl.ClusterI;
31 import org.simantics.db.impl.ClusterSupport;
32 import org.simantics.db.impl.ClusterTraitsBase;
33 import org.simantics.db.impl.ForEachObjectContextProcedure;
34 import org.simantics.db.impl.ForEachObjectProcedure;
35 import org.simantics.db.impl.ForPossibleRelatedValueContextProcedure;
36 import org.simantics.db.impl.ForPossibleRelatedValueProcedure;
37 import org.simantics.db.impl.IClusterTable;
38 import org.simantics.db.impl.Table;
39 import org.simantics.db.impl.TableHeader;
40 import org.simantics.db.impl.graph.ReadGraphImpl;
41 import org.simantics.db.procedure.AsyncContextMultiProcedure;
42 import org.simantics.db.procedure.AsyncMultiProcedure;
43 import org.simantics.db.procore.cluster.ClusterMapSmall;
44 import org.simantics.db.procore.cluster.ClusterTraits;
45 import org.simantics.db.procore.cluster.ClusterTraitsSmall;
46 import org.simantics.db.procore.cluster.CompleteTableSmall;
47 import org.simantics.db.procore.cluster.ForeignTableSmall;
48 import org.simantics.db.procore.cluster.ObjectTable;
49 import org.simantics.db.procore.cluster.OutOfSpaceException;
50 import org.simantics.db.procore.cluster.PredicateTable;
51 import org.simantics.db.procore.cluster.ResourceTableSmall;
52 import org.simantics.db.procore.cluster.ValueTableSmall;
53 import org.simantics.db.service.Bytes;
54 import org.simantics.db.service.ClusterUID;
55 import org.simantics.db.service.ResourceUID;
57 import gnu.trove.map.hash.TIntShortHashMap;
58 import gnu.trove.procedure.TIntProcedure;
59 import gnu.trove.set.hash.TIntHashSet;
61 final public class ClusterSmall extends ClusterImpl {
62 private static final int TABLE_HEADER_SIZE = TableHeader.HEADER_SIZE + TableHeader.EXTRA_SIZE;
63 private static final int RESOURCE_TABLE_OFFSET = 0;
64 private static final int PREDICATE_TABLE_OFFSET = RESOURCE_TABLE_OFFSET + TABLE_HEADER_SIZE;
65 private static final int OBJECT_TABLE_OFFSET = PREDICATE_TABLE_OFFSET + TABLE_HEADER_SIZE;
66 private static final int VALUE_TABLE_OFFSET = OBJECT_TABLE_OFFSET + TABLE_HEADER_SIZE;
67 private static final int FLAT_TABLE_OFFSET = VALUE_TABLE_OFFSET + TABLE_HEADER_SIZE;
68 private static final int COMPLETE_TABLE_OFFSET = FLAT_TABLE_OFFSET + TABLE_HEADER_SIZE;
69 private static final int FOREIGN_TABLE_OFFSET = COMPLETE_TABLE_OFFSET + TABLE_HEADER_SIZE;
70 private static final int INT_HEADER_SIZE = FOREIGN_TABLE_OFFSET + TABLE_HEADER_SIZE;
71 private final int clusterBits;
72 private final ResourceTableSmall resourceTable;
73 private final PredicateTable predicateTable;
74 private final ObjectTable objectTable;
75 private final ValueTableSmall valueTable;
76 private final ForeignTableSmall foreignTable;
77 private final CompleteTableSmall completeTable;
78 private final ClusterMapSmall clusterMap;
79 private final int[] headerTable;
80 public final ClusterSupport2 clusterSupport;
81 private boolean proxy;
82 private boolean deleted = false;
84 protected ClusterSmall() {
86 this.headerTable = null;
87 this.resourceTable = null;
88 this.foreignTable = null;
89 this.predicateTable = null;
90 this.objectTable = null;
91 this.valueTable = null;
92 this.completeTable = null;
93 this.clusterMap = null;
94 this.clusterSupport = null;
99 public ClusterSmall(IClusterTable clusterTable, ClusterUID clusterUID, int clusterKey, ClusterSupport2 support) {
100 super(clusterTable, clusterUID, clusterKey, support);
101 if(DebugPolicy.REPORT_CLUSTER_EVENTS)
102 new Exception(clusterUID.toString()).printStackTrace();
104 this.headerTable = null;
105 this.resourceTable = null;
106 this.foreignTable = null;
107 this.predicateTable = null;
108 this.objectTable = null;
109 this.valueTable = null;
110 this.completeTable = null;
111 this.clusterMap = null;
112 this.clusterSupport = support;
113 this.clusterBits = 0;
115 // new Exception("ClusterSmall " + clusterKey).printStackTrace();
117 ClusterSmall(ClusterUID clusterUID, int clusterKey, ClusterSupport2 support, IClusterTable clusterTable) {
118 super(clusterTable, clusterUID, clusterKey, support);
119 if(DebugPolicy.REPORT_CLUSTER_EVENTS)
120 new Exception(clusterUID.toString()).printStackTrace();
122 this.clusterSupport = support;
123 this.headerTable = new int[INT_HEADER_SIZE];
124 this.resourceTable = new ResourceTableSmall(this, headerTable, RESOURCE_TABLE_OFFSET);
125 this.foreignTable = new ForeignTableSmall(this, headerTable, FOREIGN_TABLE_OFFSET);
126 this.predicateTable = new PredicateTable(this, headerTable, PREDICATE_TABLE_OFFSET);
127 this.objectTable = new ObjectTable(this, headerTable, OBJECT_TABLE_OFFSET);
128 this.valueTable = new ValueTableSmall(this, headerTable, VALUE_TABLE_OFFSET);
129 this.completeTable = new CompleteTableSmall(this, headerTable, COMPLETE_TABLE_OFFSET);
130 this.clusterMap = new ClusterMapSmall(this, foreignTable);
131 this.clusterBits = ClusterTraitsBase.getClusterBits(clusterKey);
132 // if(clusterTable != null)
133 // this.importance = -clusterTable.timeCounter();
136 // new Exception("ClusterSmall " + clusterKey).printStackTrace();
138 protected ClusterSmall(IClusterTable clusterTable, long[] longs, int[] ints, byte[] bytes, ClusterSupport2 support, int clusterKey)
139 throws DatabaseException {
140 super(clusterTable, checkValidity(-1, longs, ints, bytes), clusterKey, support);
142 this.clusterSupport = support;
143 if (ints.length < INT_HEADER_SIZE)
144 throw new IllegalArgumentException("Too small integer table for cluster.");
145 this.headerTable = ints;
146 if(DebugPolicy.REPORT_CLUSTER_EVENTS) new Exception(Long.toString(clusterId)).printStackTrace();
147 this.resourceTable = new ResourceTableSmall(this, ints, RESOURCE_TABLE_OFFSET, longs);
148 this.foreignTable = new ForeignTableSmall(this, headerTable, FOREIGN_TABLE_OFFSET, longs);
149 this.predicateTable = new PredicateTable(this, ints, PREDICATE_TABLE_OFFSET, ints);
150 this.objectTable = new ObjectTable(this, ints, OBJECT_TABLE_OFFSET, ints);
151 this.valueTable = new ValueTableSmall(this, ints, VALUE_TABLE_OFFSET, bytes);
152 this.completeTable = new CompleteTableSmall(this, headerTable, COMPLETE_TABLE_OFFSET, ints);
153 this.clusterMap = new ClusterMapSmall(this, foreignTable);
154 this.clusterBits = ClusterTraitsBase.getClusterBits(clusterKey);
155 // if(clusterTable != null) {
156 // this.importance = clusterTable.timeCounter();
157 // clusterTable.markImmutable(this, getImmutable());
159 // new Exception("ClusterSmall " + clusterKey).printStackTrace();
162 System.out.println("Cluster " + clusterId);
163 System.out.println("-size:" + getUsedSpace());
164 System.out.println(" -rt:" + (resourceTable.getTableCapacity() * 8 + 8));
165 System.out.println(" -ft:" + foreignTable.getTableCapacity() * 8);
166 System.out.println(" -pt:" + predicateTable.getTableCapacity() * 4);
167 System.out.println(" -ot:" + objectTable.getTableCapacity() * 4);
168 System.out.println(" -ct:" + completeTable.getTableCapacity() * 4);
169 System.out.println(" -vt:" + valueTable.getTableCapacity());
171 System.out.println("-resourceTable:");
172 System.out.println(" -resourceCount=" + resourceTable.getResourceCount());
173 System.out.println(" -size=" + resourceTable.getTableSize());
174 System.out.println(" -capacity=" + resourceTable.getTableCapacity());
175 System.out.println(" -count=" + resourceTable.getTableCount());
176 System.out.println(" -size=" + resourceTable.getTableSize());
177 //resourceTable.analyse();
179 public void checkDirectReference(int dr)
180 throws DatabaseException {
181 if (!ClusterTraits.statementIndexIsDirect(dr))
182 throw new ValidationException("Reference is not direct. Reference=" + dr);
183 if (ClusterTraits.isFlat(dr))
184 throw new ValidationException("Reference is flat. Reference=" + dr);
185 if (ClusterTraits.isLocal(dr)) {
186 if (dr < 1 || dr > resourceTable.getUsedSize())
187 throw new ValidationException("Illegal local reference. Reference=" + dr);
189 int fi = ClusterTraits.getForeignIndexFromReference(dr);
190 int ri = ClusterTraits.getResourceIndexFromForeignReference(dr);
191 if (fi < 1 || fi > foreignTable.getUsedSize())
192 throw new ValidationException("Illegal foreign reference. Reference=" + dr + " foreign index=" + fi);
193 if (ri < 1 || ri > ClusterTraits.getMaxNumberOfResources())
194 throw new ValidationException("Illegal foreign reference. Reference=" + dr + " resource index=" + ri);
197 public void checkPredicateIndex(int pi)
198 throws DatabaseException {
199 // predicateTable.checkPredicateSetIndex(this, pi);
201 public void checkObjectSetReference(int or)
202 throws DatabaseException {
203 if (ClusterTraits.statementIndexIsDirect(or))
204 throw new ValidationException("Illegal object set reference. Reference=" + or);
205 int oi = ClusterTraits.statementIndexGet(or);
206 this.objectTable.checkObjectSetIndex(this, oi);
209 public void checkValueInit()
210 throws DatabaseException {
211 valueTable.checkValueInit();
213 public void checkValue(int capacity, int index)
214 throws DatabaseException {
215 valueTable.checkValue(capacity, index);
217 public void checkValueFini()
218 throws DatabaseException {
219 valueTable.checkValueFini();
221 public void checkForeingIndex(int fi)
222 throws DatabaseException {
223 if (fi<1 || fi > foreignTable.getUsedSize())
224 throw new ValidationException("Illegal foreign index=" + fi);
226 public void checkCompleteSetReference(int cr)
227 throws DatabaseException {
228 if (!ClusterTraits.completeReferenceIsMultiple(cr))
229 throw new ValidationException("Illegal complete set reference. Reference=" + cr);
231 this.completeTable.checkCompleteSetIndex(this, ci);
234 throws DatabaseException {
235 // this.completeTable.check(this);
236 // this.objectTable.check(this);
237 // // Must be after object table check.
238 // this.predicateTable.check(this);
239 // this.resourceTable.check(this);
242 public CompleteTypeEnum getCompleteType(int resourceKey, ClusterSupport support)
243 throws DatabaseException {
244 final int resourceRef = getLocalReference(resourceKey);
245 CompleteTypeEnum ct = resourceTable.getCompleteType(resourceRef);
247 System.out.println("ClusterSmall.getCompleteType rk=" + resourceKey + " ct=" + ct);
252 public int getCompleteObjectKey(int resourceKey, ClusterSupport support)
253 throws DatabaseException {
254 final int resourceIndexOld = getLocalReference(resourceKey);
255 short completeRef = resourceTable.getCompleteObjectRef(resourceIndexOld);
258 if (0 == completeRef)
259 throw new DatabaseException("Resource's complete object refernce is null. Resource key=" + resourceKey + ".");
260 ClusterI.CompleteTypeEnum completeType = resourceTable.getCompleteType(resourceIndexOld);
261 if (completeType == ClusterI.CompleteTypeEnum.NotComplete)
262 throw new DatabaseException("Resource has multiple complete objects. Resource key=" + resourceKey + ".");
263 if (ClusterTraitsSmall.resourceRefIsLocal(completeRef)) {
264 clusterIndex = clusterKey;
265 resourceIndex = completeRef;
266 } else { // Resource has one complete statement.
267 ResourceUID resourceUID = clusterMap.getForeignResourceUID(completeRef);
268 ClusterUID uid = resourceUID.asCID();
269 clusterIndex = clusterSupport.getClusterKeyByUID(0, uid.second);
270 //ClusterI c = clusterTable.getClusterByClusterUIDOrMakeProxy(uid);
271 //clusterIndex = c.getClusterKey();
272 //assert(clusterIndex == clusterTable.getClusterByClusterUIDOrMakeProxy(uid).getClusterKey());
273 resourceIndex = resourceUID.getIndex();
275 int key = ClusterTraits.createResourceKey(clusterIndex, resourceIndex);
277 System.out.println("ClusterSmall.complete object rk=" + resourceKey + " ck=" + key);
282 public boolean isComplete(int resourceKey, ClusterSupport support)
283 throws DatabaseException {
284 final int resourceRef = getLocalReference(resourceKey);
285 final ClusterI.CompleteTypeEnum completeType = resourceTable.getCompleteType(resourceRef);
286 boolean complete = completeType != ClusterI.CompleteTypeEnum.NotComplete;
288 System.out.println("ClusterSmall.key=" + resourceKey + " isComplete=" + complete);
291 public int getSingleObject(int resourceKey, int predicateKey, int objectIndex, ClusterSupport support) throws DatabaseException {
293 System.out.println("ClusterSmall.getSingleObject: rk=" + resourceKey + " pk=" + predicateKey);
294 if (0 == objectIndex) {
295 final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);
296 final short pRef = getInternalReferenceOrZero2(predicateKey, support);
297 final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
298 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
300 return objectTable.getSingleObject(objectIndex, support, this);
303 public void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, AsyncMultiProcedure<Resource> procedure,
304 ClusterSupport support) throws DatabaseException {
306 System.out.println("ClusterSmall.forObjects1: rk=" + resourceKey + " pk=" + predicateKey);
307 if (0 == objectIndex) {
308 final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
309 final int pRef = getInternalReferenceOrZero2(predicateKey, support);
310 final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
311 resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
314 objectTable.foreachObject(graph, objectIndex, procedure, this);
317 public <C> void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, C context, AsyncContextMultiProcedure<C, Resource> procedure,
318 ClusterSupport support) throws DatabaseException {
320 System.out.println("ClusterSmall.forObjects1: rk=" + resourceKey + " pk=" + predicateKey);
321 if (0 == objectIndex) {
322 final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
323 final int pRef = getInternalReferenceOrZero2(predicateKey, support);
324 final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
325 resourceTable.foreachObject(resourceIndex, graph, context, procedure, support, pRef, pCompleteType, completeTable, this);
328 objectTable.foreachObject(graph, objectIndex, context, procedure, this);
332 public <Context> boolean forObjects(int resourceKey, int predicateKey, int objectIndex, ObjectProcedure<Context> procedure,
333 Context context, ClusterSupport support) throws DatabaseException {
335 System.out.println("ClusterSmall.forObjects2: rk=" + resourceKey + " pk=" + predicateKey);
336 if (0 == objectIndex) {
337 final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
338 final short pRef = getInternalReferenceOrZero2(predicateKey, support);
339 final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
340 return resourceTable.foreachObject(resourceIndex, procedure, context, support, this, pRef, pCompleteType, completeTable);
342 return objectTable.foreachObject(objectIndex, procedure, context, support, this);
346 public int getSingleObject(int resourceKey, int predicateKey, ClusterSupport support) throws DatabaseException {
348 System.out.println("ClusterSmall.getSingleObject2: rk=" + resourceKey + " pk=" + predicateKey);
349 final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
350 final short pRef = getInternalReferenceOrZero2(predicateKey, support);
351 final int completeType = ClusterTraitsBase.getCompleteTypeIntFromResourceKey(predicateKey);
352 final ClusterI.CompleteTypeEnum pCompleteType = CompleteTypeEnum.make(completeType);
353 if (completeType > 0)
354 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
355 final int predicateIndex = (int)resourceTable.table[(resourceIndex<<1) - 1 + resourceTable.offset] & 0xFFFFFF;
356 if (0 == predicateIndex) // All relevant data is in resource table.
357 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
358 int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
359 return getSingleObject(resourceKey, predicateKey, objectIndex, support);
363 public <T> int getSingleObject(int resourceKey, ForPossibleRelatedValueProcedure<T> procedure, ClusterSupport support) throws DatabaseException {
364 final short resourceIndex = (short)ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
365 final int predicateKey = procedure.predicateKey;
366 int clusterKey = ClusterTraitsBase.getClusterMaskFromResourceKey(resourceKey);
368 if(procedure.clusterKey[0] == clusterKey) {
369 pRef = (short)procedure.predicateReference[0];
371 pRef = getInternalReferenceOrZero2(predicateKey, support);
372 procedure.clusterKey[0] = clusterKey;
373 procedure.predicateReference[0] = pRef;
376 final ClusterI.CompleteTypeEnum pCompleteType = procedure.completeType;
377 if (CompleteTypeEnum.NotComplete != pCompleteType)
378 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
379 final int predicateIndex = (int)resourceTable.table[(resourceIndex<<1) - 1 + resourceTable.offset] & 0xFFFFFF;
380 if (0 == predicateIndex) // All relevant data is in resource table.
381 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
382 int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
383 return getSingleObject(resourceKey, predicateKey, objectIndex, support);
387 public <C, T> int getSingleObject(int resourceKey, ForPossibleRelatedValueContextProcedure<C, T> procedure, ClusterSupport support) throws DatabaseException {
388 final short resourceIndex = (short)ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
389 final int predicateKey = procedure.predicateKey;
390 int clusterKey = ClusterTraitsBase.getClusterMaskFromResourceKey(resourceKey);
392 if(procedure.clusterKey[0] == clusterKey) {
393 pRef = (short)procedure.predicateReference[0];
395 pRef = getInternalReferenceOrZero2(predicateKey, support);
396 procedure.clusterKey[0] = clusterKey;
397 procedure.predicateReference[0] = pRef;
399 final ClusterI.CompleteTypeEnum pCompleteType = procedure.completeType;
400 if (CompleteTypeEnum.NotComplete != pCompleteType)
401 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
402 final int predicateIndex = (int)resourceTable.table[(resourceIndex<<1) - 1 + resourceTable.offset] & 0xFFFFFF;
403 if (0 == predicateIndex) // All relevant data is in resource table.
404 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
405 int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
406 return getSingleObject(resourceKey, predicateKey, objectIndex, support);
410 public void forObjects(ReadGraphImpl graph, int resourceKey,
411 int predicateKey, AsyncMultiProcedure<Resource> procedure) throws DatabaseException {
413 throw new UnsupportedOperationException();
415 // SessionImplSocket session = (SessionImplSocket)graph.getSession();
416 // ClusterSupport support = session.clusterTranslator;
418 // System.out.println("ClusterSmall.forObjects3: rk=" + resourceKey + " pk=" + predicateKey);
419 // final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
420 // final int pRef = getInternalReferenceOrZero2(predicateKey, support);
421 // final int completeType = ClusterTraitsBase.getCompleteTypeIntFromResourceKey(predicateKey);
422 // final ClusterI.CompleteTypeEnum pCompleteType = CompleteTypeEnum.make(completeType);
423 // if (completeType > 0) {
424 // resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
427 // final int predicateIndex = (int)resourceTable.table[(resourceIndex<<1) - 1 + resourceTable.offset] & 0xFFFFFF;
428 // if (0 == predicateIndex) {
429 // resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
432 // int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
433 // forObjects(graph, resourceKey, predicateKey, objectIndex, procedure, support);
436 public void forObjects(ReadGraphImpl graph, int resourceKey, ForEachObjectProcedure procedure) throws DatabaseException {
438 throw new UnsupportedOperationException();
440 // final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
441 // final int predicateKey = procedure.predicateKey;
442 // int clusterKey = ClusterTraitsBase.getClusterMaskFromResourceKey(resourceKey);
444 // if(procedure.clusterKey[0] == clusterKey) {
445 // pRef = procedure.predicateReference[0];
447 // SessionImplSocket session = (SessionImplSocket)graph.getSession();
448 // ClusterSupport support = session.clusterTranslator;
449 // pRef = getInternalReferenceOrZero2(predicateKey, support);
450 // procedure.clusterKey[0] = clusterKey;
451 // procedure.predicateReference[0] = pRef;
453 // final ClusterI.CompleteTypeEnum pCompleteType = procedure.completeType;
454 // if (ClusterI.CompleteTypeEnum.NotComplete != pCompleteType) {
455 // SessionImplSocket session = (SessionImplSocket)graph.getSession();
456 // ClusterSupport support = session.clusterTranslator;
457 // resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
460 // final int predicateIndex = (int)resourceTable.table[(resourceIndex<<1) - 1 + resourceTable.offset] & 0xFFFFFF;
461 // if (0 == predicateIndex) {
462 // SessionImplSocket session = (SessionImplSocket)graph.getSession();
463 // ClusterSupport support = session.clusterTranslator;
464 // resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
467 // int hashBase = predicateIndex + predicateTable.offset;
468 // if (predicateTable.table[hashBase-1] < 0) {
469 // int objectIndex = TableIntArraySet2.get(predicateTable.table, hashBase, pRef & 0xFFFF);
470 // //int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
471 // SessionImplSocket session = (SessionImplSocket)graph.getSession();
472 // ClusterSupport support = session.clusterTranslator;
473 // forObjects(graph, resourceKey, predicateKey, objectIndex, procedure, support);
475 // procedure.finished(graph);
480 public <C> void forObjects(ReadGraphImpl graph, int resourceKey, C context, ForEachObjectContextProcedure<C> procedure) throws DatabaseException {
482 throw new UnsupportedOperationException();
484 // final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
485 // final int predicateKey = procedure.predicateKey;
486 // int clusterKey = ClusterTraitsBase.getClusterMaskFromResourceKey(resourceKey);
488 // if(procedure.clusterKey[0] == clusterKey) {
489 // pRef = procedure.predicateReference[0];
491 // SessionImplSocket session = (SessionImplSocket)graph.getSession();
492 // ClusterSupport support = session.clusterTranslator;
493 // pRef = getInternalReferenceOrZero2(predicateKey, support);
494 // procedure.clusterKey[0] = clusterKey;
495 // procedure.predicateReference[0] = pRef;
498 // final ClusterI.CompleteTypeEnum pCompleteType = procedure.completeType;
499 // if (ClusterI.CompleteTypeEnum.NotComplete != pCompleteType) {
500 // SessionImplSocket session = (SessionImplSocket)graph.getSession();
501 // ClusterSupport support = session.clusterTranslator;
502 // resourceTable.foreachObject(resourceIndex, graph, context, procedure, support, pRef, pCompleteType, completeTable, this);
505 // final int predicateIndex = (int)resourceTable.table[(resourceIndex<<1) - 1 + resourceTable.offset] & 0xFFFFFF;
506 // if (0 == predicateIndex) {
507 // SessionImplSocket session = (SessionImplSocket)graph.getSession();
508 // ClusterSupport support = session.clusterTranslator;
509 // resourceTable.foreachObject(resourceIndex, graph, context, procedure, support, pRef, pCompleteType, completeTable, this);
512 // int hashBase = predicateIndex + predicateTable.offset;
513 // if(predicateTable.table[hashBase-1] < 0) {
514 // int objectIndex = TableIntArraySet2.get(predicateTable.table, hashBase, pRef & 0xFFFF);
515 // SessionImplSocket session = (SessionImplSocket)graph.getSession();
516 // ClusterSupport support = session.clusterTranslator;
517 // forObjects(graph, resourceKey, predicateKey, objectIndex, context, procedure, support);
519 // int objectIndex = TableIntSet2.get(predicateTable.table, hashBase, pRef & 0xFFFF);
520 // SessionImplSocket session = (SessionImplSocket)graph.getSession();
521 // ClusterSupport support = session.clusterTranslator;
522 // forObjects(graph, resourceKey, predicateKey, objectIndex, context, procedure, support);
526 public <Context> boolean forObjects(int resourceKey, int predicateKey,
527 ObjectProcedure<Context> procedure, Context context, ClusterSupport support)
528 throws DatabaseException {
530 System.out.println("ClusterSmall.forObjects4: rk=" + resourceKey + " pk=" + predicateKey);
531 final short resourceIndex = (short)ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
532 final short pRef = getInternalReferenceOrZero2(predicateKey, support);
533 final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
534 // PredicateType is complete i.e. all relevant data is in resource table.
535 if (ClusterI.CompleteTypeEnum.NotComplete != pCompleteType) {
537 System.out.println("ClusterSmall.forObjects: complete type was " + pCompleteType + " cluster=" + getClusterUID());
538 return resourceTable.foreachObject(resourceIndex, procedure, context, support, this, pRef, pCompleteType, completeTable);
540 final int predicateIndex = resourceTable.getPredicateIndex(resourceIndex);
541 if (0 == predicateIndex) { // All relevant data is in resource table.
543 System.out.println("ClusterSmall.forObjects: no predicate table " + pCompleteType);
544 return resourceTable.foreachObject(resourceIndex, procedure, context, support, this, pRef, pCompleteType, completeTable);
546 int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
547 return forObjects(resourceKey, predicateKey, objectIndex, procedure, context, support);
550 public <Context> boolean forPredicates(int resourceKey,
551 PredicateProcedure<Context> procedure, Context context, ClusterSupport support)
552 throws DatabaseException {
554 System.out.println("ClusterSmall.forPredicates: rk=" + resourceKey );
555 final int resourceIndex = getLocalReference(resourceKey);
556 final int predicateIndex = resourceTable.getPredicateIndex(resourceIndex);
557 if (0 == predicateIndex)
558 return resourceTable.foreachPredicate(resourceIndex,
559 procedure, context, support, this, completeTable);
561 boolean broken = resourceTable.foreachPredicate(resourceIndex,
562 procedure, context, support, this, completeTable);
566 return predicateTable.foreachPredicate(predicateIndex,
567 procedure, context, support, this);
571 public ClusterI addRelation(int sResourceKey, ClusterUID puid, int pResourceKey, ClusterUID ouid, int oResourceKey, ClusterSupport support) throws DatabaseException {
574 throw new UnsupportedOperationException();
575 // ClusterImpl cluster = clusterTable.load2(clusterId, clusterKey);
576 // return cluster.addRelation(sResourceKey, pResourceKey, oResourceKey, support);
582 short sri = getLocalReferenceAnd(sResourceKey, support, ClusterChange.ADD_OPERATION);
583 short pri = getReferenceOrCreateIfForeign(pResourceKey, puid, support, ClusterStream.NULL_OPERATION);
584 short ori = getReferenceOrCreateIfForeign(oResourceKey, ouid, support, ClusterStream.NULL_OPERATION);
585 ClusterI.CompleteTypeEnum completeType = ClusterTraitsBase.getCompleteTypeFromResourceKey(pResourceKey);
586 ret = addRelationInternal(sri, pri, ori, completeType);
587 calculateModifiedId();
588 } catch (OutOfSpaceException e) {
589 boolean streamOff = support.getStreamOff();
591 support.cancelStatement(this);
592 support.setStreamOff(true);
594 ClusterI cluster = toBig(clusterSupport);
596 support.setStreamOff(false);
597 ClusterI cluster2 = cluster.addRelation(sResourceKey, pResourceKey, oResourceKey, support);
598 if (cluster != cluster2)
599 throw new DatabaseException("Internal error. Contact application support.");
604 support.addStatement(this);
607 support.cancelStatement(this);
614 public ClusterI addRelation(int sResourceKey, int pResourceKey, int oResourceKey, ClusterSupport support) throws DatabaseException {
617 System.out.println("add rk=" + sResourceKey + " pk=" + pResourceKey + " ok=" + oResourceKey);
620 throw new UnsupportedOperationException();
621 // ClusterImpl cluster = clusterTable.load2(clusterId, clusterKey);
622 // return cluster.addRelation(sResourceKey, pResourceKey, oResourceKey, support);
628 short sri = getLocalReferenceAnd(sResourceKey, support, ClusterChange.ADD_OPERATION);
629 short pri = getReferenceOrCreateIfForeign(pResourceKey, support, ClusterStream.NULL_OPERATION);
630 short ori = getReferenceOrCreateIfForeign(oResourceKey, support, ClusterStream.NULL_OPERATION);
631 ClusterI.CompleteTypeEnum completeType = ClusterTraitsBase.getCompleteTypeFromResourceKey(pResourceKey);
632 ret = addRelationInternal(sri, pri, ori, completeType);
633 calculateModifiedId();
634 } catch (OutOfSpaceException e) {
635 boolean streamOff = support.getStreamOff();
637 support.cancelStatement(this);
638 support.setStreamOff(true);
640 ClusterI cluster = toBig(clusterSupport);
642 support.setStreamOff(false);
643 ClusterI cluster2 = cluster.addRelation(sResourceKey, pResourceKey, oResourceKey, support);
644 if (cluster != cluster2)
645 throw new DatabaseException("Internal error. Contact application support.");
650 support.addStatement(this);
653 support.cancelStatement(this);
658 public boolean removeRelation(int sResourceKey, int pResourceKey, int oResourceKey, ClusterSupport support)
659 throws DatabaseException {
661 short sri = getLocalReferenceAnd(sResourceKey, support, ClusterChange.REMOVE_OPERATION);
662 short pri = getInternalReferenceOrZeroAnd(pResourceKey, support, ClusterStream.NULL_OPERATION);
663 short ori = getInternalReferenceOrZeroAnd(oResourceKey, support, ClusterStream.NULL_OPERATION);
665 if (0 != pri && 0 != ori) {
666 ClusterI.CompleteTypeEnum completeType = ClusterTraitsBase.getCompleteTypeFromResourceKey(pResourceKey);
667 ret = removeRelationInternal(sri, pri, ori, completeType, support);
668 calculateModifiedId();
671 support.removeStatement(this);
673 support.cancelStatement(this);
678 public void denyRelation(int sResourceKey, int pResourceKey, int oResourceKey, ClusterSupport support)
679 throws DatabaseException {
680 short s = checkResourceKeyIsOursAndGetResourceIndexIf(sResourceKey, support);
681 ResourceReferenceAndCluster p = checkResourceKeyAndGetResourceIndexIf(pResourceKey, support);
682 ResourceReferenceAndCluster o = checkResourceKeyAndGetResourceIndexIf(oResourceKey, support);
683 if (0 == s || 0 == p.reference || 0 == o.reference)
686 ClusterI.CompleteTypeEnum completeType = ClusterTraitsBase.getCompleteTypeFromResourceKey(pResourceKey);
687 boolean ret = removeRelationInternal(s, p.reference, o.reference, completeType, support);
689 support.addStatementIndex(this, sResourceKey, getClusterUID(), ClusterChange.REMOVE_OPERATION);
690 support.addStatementIndex(this, pResourceKey, p.clusterUID, ClusterStream.NULL_OPERATION);
691 support.addStatementIndex(this, oResourceKey, o.clusterUID, ClusterStream.NULL_OPERATION);
692 support.removeStatement(this);
694 calculateModifiedId();
699 public InputStream getValueStream(int resourceKey, ClusterSupport support) throws DatabaseException {
701 System.out.println("ClusterSmall.getValue " + resourceKey);
702 int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);
704 byte[] buffer = resourceTable.getValue(valueTable, resourceIndex);
705 if(buffer == null) return null;
706 return new ByteArrayInputStream(buffer);
707 } catch (ExternalValueException e) {
708 return support.getValueStreamEx(resourceIndex, clusterUID.second);
712 public byte[] getValue(int resourceKey, ClusterSupport support) throws DatabaseException {
714 System.out.println("ClusterSmall.getValue " + resourceKey);
715 int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);
717 return resourceTable.getValue(valueTable, resourceIndex);
718 } catch (ExternalValueException e) {
719 return clusterSupport.impl.getResourceFile(clusterUID.asBytes(), resourceIndex);
720 //return support.getValueEx(resourceIndex, clusterUID.second);
724 public boolean hasValue(int resourceKey, ClusterSupport support)
725 throws DatabaseException {
726 int resourceIndex = getLocalReference(resourceKey);
727 return resourceTable.hasValue(resourceIndex);
730 public boolean removeValue(int resourceKey, ClusterSupport support)
731 throws DatabaseException {
732 int resourceIndex = getLocalReferenceAnd(resourceKey, support, ClusterChange.DELETE_OPERATION);
733 support.removeValue(this);
734 calculateModifiedId();
735 return resourceTable.removeValue(valueTable, resourceIndex);
738 public ClusterI setValue(int rResourceId, byte[] value, int length, ClusterSupport support)
739 throws DatabaseException {
740 int resourceIndex = getLocalReferenceAnd(rResourceId, support, ClusterStream.SET_OPERATION);
741 support.setValue(this, getClusterId(), value, length);
743 resourceTable.setValue(valueTable, resourceIndex, value, length);
744 calculateModifiedId();
746 } catch (OutOfSpaceException e) {
747 boolean streamOff = support.getStreamOff();
749 support.setStreamOff(true);
750 ClusterI cluster = toBig(support);
751 cluster.setValue(rResourceId, value, length, support);
753 support.setStreamOff(false);
758 public ClusterI modiValueEx(int rResourceId, long voffset, int length, byte[] value, int offset, ClusterSupport support)
759 throws DatabaseException {
760 int resourceIndex = getLocalReferenceAnd(rResourceId, support, ClusterStream.MODI_OPERATION);
761 support.modiValue(this, getClusterId(), voffset, length, value, offset);
762 resourceTable.setValueEx(valueTable, resourceIndex);
763 calculateModifiedId();
767 public byte[] readValueEx(int rResourceId, long voffset, int length, ClusterSupport support)
768 throws DatabaseException {
769 int resourceIndex = getLocalReference(rResourceId);
770 boolean isExternal = resourceTable.isValueEx(valueTable, resourceIndex);
772 throw new DatabaseException("ClusterI.readValue supported only for external value. Resource key=" + rResourceId);
773 return support.getValueEx(resourceIndex, getClusterId(), voffset, length);
776 public boolean isValueEx(int resourceKey) throws DatabaseException {
777 int resourceIndex = getLocalReference(resourceKey);
778 return resourceTable.isValueEx(valueTable, resourceIndex);
781 public long getValueSizeEx(int rResourceId, ClusterSupport support)
782 throws DatabaseException, ExternalValueException {
783 int resourceIndex = getLocalReference(rResourceId);
784 boolean isExternal = resourceTable.isValueEx(valueTable, resourceIndex);
786 throw new ExternalValueException("ClusterI.getValueSizeEx supported only for external value. Resource key=" + rResourceId);
787 return support.getValueSizeEx(resourceIndex, getClusterId());
790 public void setValueEx(int rResourceId)
791 throws DatabaseException {
792 int resourceIndex = getLocalReference(rResourceId);
793 resourceTable.setValueEx(valueTable, resourceIndex);
796 public int createResource(ClusterSupport support)
797 throws DatabaseException {
800 throw new UnsupportedOperationException();
801 // ClusterImpl cluster = clusterTable.load2(clusterId, clusterKey);
802 // return cluster.createResource(support);
805 short resourceIndex = resourceTable.createResource();
806 calculateModifiedId();
807 if(DebugPolicy.REPORT_RESOURCE_ID_ALLOCATION)
808 System.out.println("[RID_ALLOCATION]: ClusterSmall[" + clusterId + "] allocates " + resourceIndex);
809 support.createResource(this, resourceIndex, getClusterId());
810 return ClusterTraits.createResourceKey(clusterKey, resourceIndex);
813 public boolean hasResource(int resourceKey, ClusterSupport support) {
814 int clusterKey = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(resourceKey);
815 if (this.clusterKey != clusterKey) // foreign resource
819 resourceIndex = ClusterTraits.getResourceIndexFromResourceKey(resourceKey);
820 } catch (DatabaseException e) {
823 if (resourceIndex > 0 & resourceIndex <= resourceTable.getTableCount())
829 public int getNumberOfResources(ClusterSupport support)
830 throws DatabaseException {
833 throw new UnsupportedOperationException();
834 // ClusterImpl cluster = clusterTable.load2(clusterId, clusterKey);
835 // return cluster.getNumberOfResources(support);
838 return resourceTable.getUsedSize();
841 public int getNumberOfResources() throws IllegalAcornStateException {
843 throw new IllegalAcornStateException("proxy == true for " + clusterId);
845 return resourceTable.getUsedSize();
849 public long getUsedSpace() {
850 if(isEmpty()) return 0;
851 long rt = resourceTable.getTableCapacity() * 8 + 8; // (8 = cluster id)
852 long ft = foreignTable.getTableCapacity() * 8;
853 long pt = predicateTable.getTableCapacity() * 4;
854 long ot = objectTable.getTableCapacity() * 4;
855 long ct = completeTable.getTableCapacity() * 4;
856 long vt = valueTable.getTableCapacity() * 1;
857 long cm = clusterMap.getUsedSpace();
858 return rt + ft + pt + ot + ct + vt + cm;
861 public boolean isEmpty() {
862 if(resourceTable == null) return true;
863 return resourceTable.getTableCount() == 0;
866 public void printDebugInfo(String message, ClusterSupport support)
867 throws DatabaseException {
868 throw new DatabaseException("Not implemented!");
870 private short getInternalReferenceOrZero2(int resourceKey, ClusterSupport support) throws DatabaseException {
871 int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);
872 if (!ClusterTraitsBase.isCluster(clusterBits, resourceKey)) {
873 return clusterMap.getForeignReferenceOrZero(resourceKey);
875 return (short)resourceIndex;
878 private short getInternalReferenceOrZeroAnd(int resourceKey, ClusterSupport support, byte op)
879 throws DatabaseException {
880 int clusterKey = ClusterTraits.getClusterKeyFromResourceKey(resourceKey);
881 int resourceIndex = ClusterTraits.getResourceIndexFromResourceKey(resourceKey);
882 if (this.clusterKey != clusterKey) { // foreign resource
883 ClusterUID clusterUID = clusterSupport.getClusterUIDByResourceKey(resourceKey);
884 short foreignRef = clusterMap.getForeignReferenceOrZero(resourceKey);
885 support.addStatementIndex(this, resourceKey, clusterUID, op);
888 support.addStatementIndex(this, resourceKey, getClusterUID(), op);
889 return (short)resourceIndex;
891 private final short getLocalReference(int resourceKey) throws DatabaseException {
892 return ClusterTraits.getResourceIndexFromResourceKeyNoThrow(resourceKey);
894 private final short getLocalReferenceAnd(int resourceKey, ClusterSupport support, byte op)
895 throws DatabaseException {
896 short resourceIndex = getLocalReference(resourceKey);
897 support.addStatementIndex(this, resourceKey, getClusterUID(), op);
898 return resourceIndex;
900 private short checkResourceKeyIsOursAndGetResourceIndexIf(int resourceKey, ClusterSupport support)
901 throws DatabaseException {
902 int clusterShortId = ClusterTraits.getClusterKeyFromResourceKey(resourceKey);
903 if (this.clusterKey != clusterShortId)
905 int resourceIndex = ClusterTraits.getResourceIndexFromResourceKey(resourceKey);
906 return (short)resourceIndex;
908 private short getReferenceOrCreateIfForeign(int resourceKey, ClusterUID clusterUID, ClusterSupport support, byte op)
909 throws DatabaseException {
910 int clusterKey = ClusterTraits.getClusterKeyFromResourceKey(resourceKey);
911 short resourceIndex = (short)ClusterTraits.getResourceIndexFromResourceKey(resourceKey);
912 if (this.clusterKey != clusterKey) {
913 support.addStatementIndex(this, resourceKey, clusterUID, op);
914 short ref = clusterMap.getForeignReferenceOrCreateByResourceKey(resourceKey, clusterUID);
917 support.addStatementIndex(this, resourceKey, getClusterUID(), op);
918 return resourceIndex;
920 private short getReferenceOrCreateIfForeign(int resourceKey, ClusterSupport support, byte op)
921 throws DatabaseException {
922 int clusterKey = ClusterTraits.getClusterKeyFromResourceKey(resourceKey);
923 short resourceIndex = (short)ClusterTraits.getResourceIndexFromResourceKey(resourceKey);
924 if (this.clusterKey != clusterKey) {
925 ClusterUID clusterUID = clusterSupport.getClusterUIDByResourceKey(resourceKey);
926 support.addStatementIndex(this, resourceKey, clusterUID, op);
927 short ref = clusterMap.getForeignReferenceOrCreateByResourceKey(resourceKey, clusterUID);
930 support.addStatementIndex(this, resourceKey, getClusterUID(), op);
931 return resourceIndex;
933 private class ResourceReferenceAndCluster {
934 ResourceReferenceAndCluster(short reference, ClusterUID clusterUID) {
935 this.reference = reference;
936 this.clusterUID = clusterUID;
938 public final short reference;
939 public final ClusterUID clusterUID;
941 private ResourceReferenceAndCluster checkResourceKeyAndGetResourceIndexIf(int resourceKey, ClusterSupport support)
942 throws DatabaseException {
943 int clusterKey = ClusterTraits.getClusterKeyFromResourceKey(resourceKey);
944 short resourceIndex = (short)ClusterTraits.getResourceIndexFromResourceKey(resourceKey);
945 if (this.clusterKey != clusterKey) { // foreign resource
946 ClusterI foreignCluster = support.getClusterByClusterKey(clusterKey);
947 ClusterUID clusterUID = foreignCluster.getClusterUID();
948 short ref = clusterMap.getForeignReferenceOrZero(resourceKey);
949 return new ResourceReferenceAndCluster(ref, clusterUID);
951 return new ResourceReferenceAndCluster(resourceIndex, getClusterUID());
954 static long fTime = 0;
957 final public int execute(int resourceReference) throws DatabaseException {
958 short resourceRef = (short)resourceReference;
960 if (ClusterTraitsSmall.resourceRefIsLocal(resourceRef)) {
961 key = clusterBits | resourceRef;
963 short foreignIndex = ClusterTraitsSmall.resourceRefGetForeignIndex((short)resourceRef);
964 //long start = System.nanoTime();
965 ResourceUID resourceUID = foreignTable.getResourceUID(foreignIndex);
966 int clusterKey = clusterSupport.getClusterKeyByClusterUIDOrMake(resourceUID.asCID());
967 // ClusterBase cluster = clusterSupport.getClusterByClusterUIDOrMake(resourceUID.asCID());
968 key = ClusterTraitsBase.createResourceKey(clusterKey, resourceUID.getIndex());
969 //fTime += System.nanoTime() - start;
970 //System.err.println("fTime: " + 1e-9*fTime);
973 System.out.println("ClusterSmall.execute key=" + key);
977 private boolean addRelationInternal(short sReference, short pReference, short oReference, ClusterI.CompleteTypeEnum completeType)
978 throws DatabaseException {
979 int predicateIndex = resourceTable.addStatement(sReference, pReference, oReference, predicateTable, objectTable, completeType, completeTable);
980 if (0 == predicateIndex)
981 return true; // added to resourceTable
982 else if (0 > predicateIndex)
983 return false; // old complete statemenent
984 int newPredicateIndex = predicateTable.addPredicate(predicateIndex, 0xFFFF & pReference, 0xFFFF & oReference, objectTable);
985 if (0 == newPredicateIndex)
987 if (predicateIndex != newPredicateIndex)
988 resourceTable.setPredicateIndex(sReference, newPredicateIndex);
991 private boolean removeRelationInternal(int sResourceIndex, short pResourceIndex,
992 short oResourceIndex, ClusterI.CompleteTypeEnum completeType, ClusterSupport support)
993 throws DatabaseException {
994 int predicateIndex = resourceTable.getPredicateIndex(sResourceIndex);
995 if (0 == predicateIndex || ClusterI.CompleteTypeEnum.NotComplete != completeType)
996 return resourceTable.removeStatementFromCache(sResourceIndex,
997 pResourceIndex, oResourceIndex, completeType, completeTable);
998 PredicateTable.Status ret = predicateTable.removePredicate(predicateIndex, 0xFFFF & pResourceIndex, 0xFFFF & oResourceIndex, objectTable);
1000 case NothingRemoved:
1002 case PredicateRemoved: {
1003 if (0 == predicateTable.getPredicateSetSize(predicateIndex))
1004 resourceTable.setPredicateIndex(sResourceIndex, 0);
1005 // intentionally dropping to next case
1009 resourceTable.removeStatement(sResourceIndex,
1010 pResourceIndex, oResourceIndex,
1011 completeType, completeTable,
1012 predicateTable, objectTable, support);
1016 public void load() {
1017 throw new Error("Not supported.");
1021 public void load(Consumer<DatabaseException> r) {
1022 throw new Error("Not supported.");
1025 public boolean contains(int resourceKey) {
1026 return ClusterTraitsBase.isCluster(clusterBits, resourceKey);
1029 public void load(final ClusterSupport support, final Runnable callback) {
1031 throw new UnsupportedOperationException();
1034 // clusterTable.load2(clusterId, clusterKey);
1036 // } catch (DatabaseException e) {
1037 // e.printStackTrace();
1042 public ClusterI getClusterByResourceKey(int resourceKey,
1043 ClusterSupport support) {
1047 public void increaseReferenceCount(int amount) {
1051 public void decreaseReferenceCount(int amount) {
1055 public int getReferenceCount() {
1059 public void releaseMemory() {
1062 public void compact() {
1063 clusterMap.compact();
1066 public boolean isLoaded() {
1070 // public ClusterImpl tryLoad(SessionImplSocket sessionImpl) {
1072 // throw new UnsupportedOperationException();
1073 // assert(Constants.ReservedClusterId != clusterId);
1075 // return clusterTable.tryLoad(clusterId, clusterKey);
1081 public ClusterBig toBig(ClusterSupport support)
1082 throws DatabaseException {
1084 System.out.println("DEBUG: toBig cluster=" + clusterId);
1085 new Exception().printStackTrace();
1087 ClusterBig big = new ClusterBig(clusterSupport, getClusterUID(), clusterKey, (ClusterSupport2)support);
1089 // if(big.cc != null)
1090 // big.cc.clusterImpl = this;
1091 resourceTable.toBig(big, support, this);
1092 big.foreignLookup = this.foreignLookup;
1093 big.change = this.change;
1095 this.foreignLookup = null;
1101 public ClusterTypeEnum getType() {
1102 return ClusterTypeEnum.SMALL;
1105 public boolean getImmutable() {
1106 int status = resourceTable.getClusterStatus();
1107 return (status & ClusterStatus.ImmutableMaskSet) == 1;
1110 public void setImmutable(boolean immutable, ClusterSupport support) {
1111 if(resourceTable != null) {
1112 int status = resourceTable.getClusterStatus();
1114 status |= ClusterStatus.ImmutableMaskSet;
1116 status &= ClusterStatus.ImmutableMaskClear;
1117 resourceTable.setClusterStatus(status);
1119 support.setImmutable(this, immutable);
1123 public String toString() {
1125 final TIntHashSet set = new TIntHashSet();
1126 TIntShortHashMap map = foreignTable.getResourceHashMap();
1127 map.forEachKey(new TIntProcedure() {
1129 public boolean execute(int value) {
1130 set.add(value & 0xfffff000);
1134 return "ClusterSmall[" + getClusterUID() + " - " + getClusterId() + " - " + getNumberOfResources() + " - " + foreignTable.getResourceHashMap().size() + " - " + set.size() + "]";
1135 } catch (DatabaseException e) {
1137 return "ClusterSmall[" + getNumberOfResources() + "]";
1138 } catch (IllegalAcornStateException e1) {
1139 Logger.defaultLogError(e1);
1140 e1.printStackTrace();
1141 return "An exception occured!!";
1147 // bytes (b) | headers(i) | predicateTable (i) | objectTable (i) | completeTable (i) | resourceTable (l) | foreignTable (l)
1150 public byte[] storeBytes() throws IOException {
1152 int byteSize = valueTable.getTableSize();
1153 int longSize = LONG_HEADER_SIZE + resourceTable.getTableSize() + foreignTable.getTableSize();
1154 int intSize = INT_HEADER_SIZE + predicateTable.getTableSize() + objectTable.getTableSize() + completeTable.getTableSize();
1156 byte[] raw = new byte[12 + byteSize + 8*longSize + 4*intSize];
1158 int[] currentHeader = Arrays.copyOf(headerTable, INT_HEADER_SIZE);
1160 Bytes.writeLE(raw, 0, byteSize);
1161 Bytes.writeLE(raw, 4, intSize);
1162 Bytes.writeLE(raw, 8, longSize);
1164 int rawPos = valueTable.storeBytes(raw, 0, 12);
1166 int intBase = rawPos;
1168 rawPos += 4*INT_HEADER_SIZE;
1169 rawPos = predicateTable.storeBytes(raw, (rawPos-intBase)>>2, rawPos);
1170 rawPos = objectTable.storeBytes(raw, (rawPos-intBase)>>2, rawPos);
1171 rawPos = completeTable.storeBytes(raw, (rawPos-intBase)>>2, rawPos);
1173 int longBase = rawPos;
1175 rawPos += 8*LONG_HEADER_SIZE;
1176 rawPos = resourceTable.storeBytes(raw, (rawPos-longBase)>>3, rawPos);
1177 rawPos = foreignTable.storeBytes(raw, (rawPos-longBase)>>3, rawPos);
1179 Bytes.writeLE8(raw, longBase, -1);
1180 Bytes.writeLE8(raw, longBase+8, LONG_HEADER_VERSION);
1181 Bytes.writeLE8(raw, longBase+16, 0);
1182 Bytes.writeLE8(raw, longBase+24, clusterUID.second);
1185 for(int i=0;i<INT_HEADER_SIZE;i++) {
1186 int v = headerTable[i];
1187 Bytes.writeLE(raw, intBase, v);
1191 for(int i=0;i<INT_HEADER_SIZE;i++)
1192 headerTable[i] = currentHeader[i];
1199 public ClusterTables store() throws IOException {
1201 ClusterTables result = new ClusterTables();
1203 int[] currentHeader = Arrays.copyOf(headerTable, INT_HEADER_SIZE);
1205 int byteSize = valueTable.getTableSize();
1206 byte[] byteBytes = new byte[byteSize];
1207 valueTable.store(byteBytes, 0);
1209 result.bytes = byteBytes;
1211 int longSize = LONG_HEADER_SIZE + resourceTable.getTableSize() + foreignTable.getTableSize();
1212 long[] longBytes = new long[longSize];
1215 longBytes[1] = LONG_HEADER_VERSION;
1217 longBytes[3] = clusterUID.second;
1219 int longPos = resourceTable.store(longBytes, LONG_HEADER_SIZE);
1220 foreignTable.store(longBytes, longPos);
1222 result.longs = longBytes;
1224 int intSize = INT_HEADER_SIZE + predicateTable.getTableSize() + objectTable.getTableSize() + completeTable.getTableSize();
1225 int[] intBytes = new int[intSize];
1226 int intPos = INT_HEADER_SIZE;
1227 intPos = predicateTable.store(intBytes, intPos);
1228 intPos = objectTable.store(intBytes, intPos);
1229 intPos = completeTable.store(intBytes, intPos);
1231 for(int i=0;i<INT_HEADER_SIZE;i++) {
1232 int v = headerTable[i];
1234 //Bytes.writeLE(intBytes, i<<2, v);
1237 result.ints = intBytes;
1239 for(int i=0;i<INT_HEADER_SIZE;i++)
1240 headerTable[i] = currentHeader[i];
1247 protected int getResourceTableCount() {
1248 return resourceTable.getTableCount();
1252 public boolean getDeleted() {
1253 if (deleted) return true;
1254 int status = resourceTable.getClusterStatus();
1255 return (status & ClusterStatus.DeletedMaskSet) == ClusterStatus.DeletedMaskSet;
1258 public void setDeleted(boolean set, ClusterSupport support) {
1260 if(resourceTable != null) {
1261 int status = resourceTable.getClusterStatus();
1263 status |= ClusterStatus.DeletedMaskSet;
1265 status &= ClusterStatus.DeletedMaskClear;
1266 resourceTable.setClusterStatus(status);
1268 if (null != support)
1269 support.setDeleted(this, set);
1273 public Table<?> getPredicateTable() {
1274 return predicateTable;
1278 public Table<?> getForeignTable() {
1279 return foreignTable;
1283 public int makeResourceKey(int pRef) throws DatabaseException {
1284 throw new UnsupportedOperationException();
1288 public Table<?> getCompleteTable() {
1289 return completeTable;
1293 public Table<?> getValueTable() {
1298 public Table<?> getObjectTable() {
1304 class ClusterStatus {
1305 public static final int ImmutableMaskClear = 0xFFFFFFFE;
1306 public static final int ImmutableMaskSet = 0x00000001;
1307 public static final int DeletedMaskClear = 0xFFFFFFFD;
1308 public static final int DeletedMaskSet = 0x00000002;