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.SyncContextMultiProcedure;
42 import org.simantics.db.procedure.SyncMultiProcedure;
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 fi.vtt.simantics.procore.internal.SessionImplSocket;
58 import gnu.trove.map.hash.TIntShortHashMap;
59 import gnu.trove.procedure.TIntProcedure;
60 import gnu.trove.set.hash.TIntHashSet;
62 final public class ClusterSmall extends ClusterImpl {
63 private static final int TABLE_HEADER_SIZE = TableHeader.HEADER_SIZE + TableHeader.EXTRA_SIZE;
64 private static final int RESOURCE_TABLE_OFFSET = 0;
65 private static final int PREDICATE_TABLE_OFFSET = RESOURCE_TABLE_OFFSET + TABLE_HEADER_SIZE;
66 private static final int OBJECT_TABLE_OFFSET = PREDICATE_TABLE_OFFSET + TABLE_HEADER_SIZE;
67 private static final int VALUE_TABLE_OFFSET = OBJECT_TABLE_OFFSET + TABLE_HEADER_SIZE;
68 private static final int FLAT_TABLE_OFFSET = VALUE_TABLE_OFFSET + TABLE_HEADER_SIZE;
69 private static final int COMPLETE_TABLE_OFFSET = FLAT_TABLE_OFFSET + TABLE_HEADER_SIZE;
70 private static final int FOREIGN_TABLE_OFFSET = COMPLETE_TABLE_OFFSET + TABLE_HEADER_SIZE;
71 private static final int INT_HEADER_SIZE = FOREIGN_TABLE_OFFSET + TABLE_HEADER_SIZE;
72 private final int clusterBits;
73 private final ResourceTableSmall resourceTable;
74 private final PredicateTable predicateTable;
75 private final ObjectTable objectTable;
76 private final ValueTableSmall valueTable;
77 private final ForeignTableSmall foreignTable;
78 private final CompleteTableSmall completeTable;
79 private final ClusterMapSmall clusterMap;
80 private final int[] headerTable;
81 public final ClusterSupport2 clusterSupport;
82 private boolean proxy;
83 private boolean deleted = false;
85 protected ClusterSmall() {
87 this.headerTable = null;
88 this.resourceTable = null;
89 this.foreignTable = null;
90 this.predicateTable = null;
91 this.objectTable = null;
92 this.valueTable = null;
93 this.completeTable = null;
94 this.clusterMap = null;
95 this.clusterSupport = null;
100 public ClusterSmall(IClusterTable clusterTable, ClusterUID clusterUID, int clusterKey, ClusterSupport2 support) {
101 super(clusterTable, clusterUID, clusterKey, support);
102 if(DebugPolicy.REPORT_CLUSTER_EVENTS)
103 new Exception(clusterUID.toString()).printStackTrace();
105 this.headerTable = null;
106 this.resourceTable = null;
107 this.foreignTable = null;
108 this.predicateTable = null;
109 this.objectTable = null;
110 this.valueTable = null;
111 this.completeTable = null;
112 this.clusterMap = null;
113 this.clusterSupport = support;
114 this.clusterBits = 0;
116 // new Exception("ClusterSmall " + clusterKey).printStackTrace();
118 ClusterSmall(ClusterUID clusterUID, int clusterKey, ClusterSupport2 support, IClusterTable clusterTable) {
119 super(clusterTable, clusterUID, clusterKey, support);
120 if(DebugPolicy.REPORT_CLUSTER_EVENTS)
121 new Exception(clusterUID.toString()).printStackTrace();
123 this.clusterSupport = support;
124 this.headerTable = new int[INT_HEADER_SIZE];
125 this.resourceTable = new ResourceTableSmall(this, headerTable, RESOURCE_TABLE_OFFSET);
126 this.foreignTable = new ForeignTableSmall(this, headerTable, FOREIGN_TABLE_OFFSET);
127 this.predicateTable = new PredicateTable(this, headerTable, PREDICATE_TABLE_OFFSET);
128 this.objectTable = new ObjectTable(this, headerTable, OBJECT_TABLE_OFFSET);
129 this.valueTable = new ValueTableSmall(this, headerTable, VALUE_TABLE_OFFSET);
130 this.completeTable = new CompleteTableSmall(this, headerTable, COMPLETE_TABLE_OFFSET);
131 this.clusterMap = new ClusterMapSmall(this, foreignTable);
132 this.clusterBits = ClusterTraitsBase.getClusterBits(clusterKey);
133 // if(clusterTable != null)
134 // this.importance = -clusterTable.timeCounter();
137 // new Exception("ClusterSmall " + clusterKey).printStackTrace();
139 protected ClusterSmall(IClusterTable clusterTable, long[] longs, int[] ints, byte[] bytes, ClusterSupport2 support, int clusterKey)
140 throws DatabaseException {
141 super(clusterTable, checkValidity(-1, longs, ints, bytes), clusterKey, support);
143 this.clusterSupport = support;
144 if (ints.length < INT_HEADER_SIZE)
145 throw new IllegalArgumentException("Too small integer table for cluster.");
146 this.headerTable = ints;
147 if(DebugPolicy.REPORT_CLUSTER_EVENTS) new Exception(Long.toString(clusterId)).printStackTrace();
148 this.resourceTable = new ResourceTableSmall(this, ints, RESOURCE_TABLE_OFFSET, longs);
149 this.foreignTable = new ForeignTableSmall(this, headerTable, FOREIGN_TABLE_OFFSET, longs);
150 this.predicateTable = new PredicateTable(this, ints, PREDICATE_TABLE_OFFSET, ints);
151 this.objectTable = new ObjectTable(this, ints, OBJECT_TABLE_OFFSET, ints);
152 this.valueTable = new ValueTableSmall(this, ints, VALUE_TABLE_OFFSET, bytes);
153 this.completeTable = new CompleteTableSmall(this, headerTable, COMPLETE_TABLE_OFFSET, ints);
154 this.clusterMap = new ClusterMapSmall(this, foreignTable);
155 this.clusterBits = ClusterTraitsBase.getClusterBits(clusterKey);
156 // if(clusterTable != null) {
157 // this.importance = clusterTable.timeCounter();
158 // clusterTable.markImmutable(this, getImmutable());
160 // new Exception("ClusterSmall " + clusterKey).printStackTrace();
163 System.out.println("Cluster " + clusterId);
164 System.out.println("-size:" + getUsedSpace());
165 System.out.println(" -rt:" + (resourceTable.getTableCapacity() * 8 + 8));
166 System.out.println(" -ft:" + foreignTable.getTableCapacity() * 8);
167 System.out.println(" -pt:" + predicateTable.getTableCapacity() * 4);
168 System.out.println(" -ot:" + objectTable.getTableCapacity() * 4);
169 System.out.println(" -ct:" + completeTable.getTableCapacity() * 4);
170 System.out.println(" -vt:" + valueTable.getTableCapacity());
172 System.out.println("-resourceTable:");
173 System.out.println(" -resourceCount=" + resourceTable.getResourceCount());
174 System.out.println(" -size=" + resourceTable.getTableSize());
175 System.out.println(" -capacity=" + resourceTable.getTableCapacity());
176 System.out.println(" -count=" + resourceTable.getTableCount());
177 System.out.println(" -size=" + resourceTable.getTableSize());
178 //resourceTable.analyse();
180 public void checkDirectReference(int dr)
181 throws DatabaseException {
182 if (!ClusterTraits.statementIndexIsDirect(dr))
183 throw new ValidationException("Reference is not direct. Reference=" + dr);
184 if (ClusterTraits.isFlat(dr))
185 throw new ValidationException("Reference is flat. Reference=" + dr);
186 if (ClusterTraits.isLocal(dr)) {
187 if (dr < 1 || dr > resourceTable.getUsedSize())
188 throw new ValidationException("Illegal local reference. Reference=" + dr);
190 int fi = ClusterTraits.getForeignIndexFromReference(dr);
191 int ri = ClusterTraits.getResourceIndexFromForeignReference(dr);
192 if (fi < 1 || fi > foreignTable.getUsedSize())
193 throw new ValidationException("Illegal foreign reference. Reference=" + dr + " foreign index=" + fi);
194 if (ri < 1 || ri > ClusterTraits.getMaxNumberOfResources())
195 throw new ValidationException("Illegal foreign reference. Reference=" + dr + " resource index=" + ri);
198 public void checkPredicateIndex(int pi)
199 throws DatabaseException {
200 // predicateTable.checkPredicateSetIndex(this, pi);
202 public void checkObjectSetReference(int or)
203 throws DatabaseException {
204 if (ClusterTraits.statementIndexIsDirect(or))
205 throw new ValidationException("Illegal object set reference. Reference=" + or);
206 int oi = ClusterTraits.statementIndexGet(or);
207 this.objectTable.checkObjectSetIndex(this, oi);
210 public void checkValueInit()
211 throws DatabaseException {
212 valueTable.checkValueInit();
214 public void checkValue(int capacity, int index)
215 throws DatabaseException {
216 valueTable.checkValue(capacity, index);
218 public void checkValueFini()
219 throws DatabaseException {
220 valueTable.checkValueFini();
222 public void checkForeingIndex(int fi)
223 throws DatabaseException {
224 if (fi<1 || fi > foreignTable.getUsedSize())
225 throw new ValidationException("Illegal foreign index=" + fi);
227 public void checkCompleteSetReference(int cr)
228 throws DatabaseException {
229 if (!ClusterTraits.completeReferenceIsMultiple(cr))
230 throw new ValidationException("Illegal complete set reference. Reference=" + cr);
232 this.completeTable.checkCompleteSetIndex(this, ci);
235 throws DatabaseException {
236 // this.completeTable.check(this);
237 // this.objectTable.check(this);
238 // // Must be after object table check.
239 // this.predicateTable.check(this);
240 // this.resourceTable.check(this);
243 public CompleteTypeEnum getCompleteType(int resourceKey, ClusterSupport support)
244 throws DatabaseException {
245 final int resourceRef = getLocalReference(resourceKey);
246 CompleteTypeEnum ct = resourceTable.getCompleteType(resourceRef);
248 System.out.println("ClusterSmall.getCompleteType rk=" + resourceKey + " ct=" + ct);
253 public int getCompleteObjectKey(int resourceKey, ClusterSupport support)
254 throws DatabaseException {
255 final int resourceIndexOld = getLocalReference(resourceKey);
256 short completeRef = resourceTable.getCompleteObjectRef(resourceIndexOld);
259 if (0 == completeRef)
260 throw new DatabaseException("Resource's complete object refernce is null. Resource key=" + resourceKey + ".");
261 ClusterI.CompleteTypeEnum completeType = resourceTable.getCompleteType(resourceIndexOld);
262 if (completeType == ClusterI.CompleteTypeEnum.NotComplete)
263 throw new DatabaseException("Resource has multiple complete objects. Resource key=" + resourceKey + ".");
264 if (ClusterTraitsSmall.resourceRefIsLocal(completeRef)) {
265 clusterIndex = clusterKey;
266 resourceIndex = completeRef;
267 } else { // Resource has one complete statement.
268 ResourceUID resourceUID = clusterMap.getForeignResourceUID(completeRef);
269 ClusterUID uid = resourceUID.asCID();
270 clusterIndex = clusterSupport.getClusterKeyByUID(0, uid.second);
271 //ClusterI c = clusterTable.getClusterByClusterUIDOrMakeProxy(uid);
272 //clusterIndex = c.getClusterKey();
273 //assert(clusterIndex == clusterTable.getClusterByClusterUIDOrMakeProxy(uid).getClusterKey());
274 resourceIndex = resourceUID.getIndex();
276 int key = ClusterTraits.createResourceKey(clusterIndex, resourceIndex);
278 System.out.println("ClusterSmall.complete object rk=" + resourceKey + " ck=" + key);
283 public boolean isComplete(int resourceKey, ClusterSupport support)
284 throws DatabaseException {
285 final int resourceRef = getLocalReference(resourceKey);
286 final ClusterI.CompleteTypeEnum completeType = resourceTable.getCompleteType(resourceRef);
287 boolean complete = completeType != ClusterI.CompleteTypeEnum.NotComplete;
289 System.out.println("ClusterSmall.key=" + resourceKey + " isComplete=" + complete);
292 public int getSingleObject(int resourceKey, int predicateKey, int objectIndex, ClusterSupport support) throws DatabaseException {
294 System.out.println("ClusterSmall.getSingleObject: rk=" + resourceKey + " pk=" + predicateKey);
295 if (0 == objectIndex) {
296 final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);
297 final short pRef = getInternalReferenceOrZero2(predicateKey, support);
298 final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
299 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
301 return objectTable.getSingleObject(objectIndex, support, this);
304 public void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, SyncMultiProcedure<Resource> procedure,
305 ClusterSupport support) throws DatabaseException {
307 System.out.println("ClusterSmall.forObjects1: rk=" + resourceKey + " pk=" + predicateKey);
308 if (0 == objectIndex) {
309 final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
310 final int pRef = getInternalReferenceOrZero2(predicateKey, support);
311 final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
312 resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
315 objectTable.foreachObject(graph, objectIndex, procedure, this);
318 public <C> void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, C context, SyncContextMultiProcedure<C, Resource> procedure,
319 ClusterSupport support) throws DatabaseException {
321 System.out.println("ClusterSmall.forObjects1: rk=" + resourceKey + " pk=" + predicateKey);
322 if (0 == objectIndex) {
323 final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
324 final int pRef = getInternalReferenceOrZero2(predicateKey, support);
325 final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
326 resourceTable.foreachObject(resourceIndex, graph, context, procedure, support, pRef, pCompleteType, completeTable, this);
329 objectTable.foreachObject(graph, objectIndex, context, procedure, this);
333 public <Context> boolean forObjects(int resourceKey, int predicateKey, int objectIndex, ObjectProcedure<Context> procedure,
334 Context context, ClusterSupport support) throws DatabaseException {
336 System.out.println("ClusterSmall.forObjects2: rk=" + resourceKey + " pk=" + predicateKey);
337 if (0 == objectIndex) {
338 final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
339 final short pRef = getInternalReferenceOrZero2(predicateKey, support);
340 final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
341 return resourceTable.foreachObject(resourceIndex, procedure, context, support, this, pRef, pCompleteType, completeTable);
343 return objectTable.foreachObject(objectIndex, procedure, context, support, this);
347 public int getSingleObject(int resourceKey, int predicateKey, ClusterSupport support) throws DatabaseException {
349 System.out.println("ClusterSmall.getSingleObject2: rk=" + resourceKey + " pk=" + predicateKey);
350 final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
351 final short pRef = getInternalReferenceOrZero2(predicateKey, support);
352 final int completeType = ClusterTraitsBase.getCompleteTypeIntFromResourceKey(predicateKey);
353 final ClusterI.CompleteTypeEnum pCompleteType = CompleteTypeEnum.make(completeType);
354 if (completeType > 0)
355 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
356 final int predicateIndex = (int)resourceTable.table[(resourceIndex<<1) - 1 + resourceTable.offset] & 0xFFFFFF;
357 if (0 == predicateIndex) // All relevant data is in resource table.
358 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
359 int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
360 return getSingleObject(resourceKey, predicateKey, objectIndex, support);
364 public <T> int getSingleObject(int resourceKey, ForPossibleRelatedValueProcedure<T> procedure, ClusterSupport support) throws DatabaseException {
365 final short resourceIndex = (short)ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
366 final int predicateKey = procedure.predicateKey;
367 int clusterKey = ClusterTraitsBase.getClusterMaskFromResourceKey(resourceKey);
369 if(procedure.clusterKey[0] == clusterKey) {
370 pRef = (short)procedure.predicateReference[0];
372 pRef = getInternalReferenceOrZero2(predicateKey, support);
373 procedure.clusterKey[0] = clusterKey;
374 procedure.predicateReference[0] = pRef;
377 final ClusterI.CompleteTypeEnum pCompleteType = procedure.completeType;
378 if (CompleteTypeEnum.NotComplete != pCompleteType)
379 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
380 final int predicateIndex = (int)resourceTable.table[(resourceIndex<<1) - 1 + resourceTable.offset] & 0xFFFFFF;
381 if (0 == predicateIndex) // All relevant data is in resource table.
382 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
383 int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
384 return getSingleObject(resourceKey, predicateKey, objectIndex, support);
388 public <C, T> int getSingleObject(int resourceKey, ForPossibleRelatedValueContextProcedure<C, T> procedure, ClusterSupport support) throws DatabaseException {
389 final short resourceIndex = (short)ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
390 final int predicateKey = procedure.predicateKey;
391 int clusterKey = ClusterTraitsBase.getClusterMaskFromResourceKey(resourceKey);
393 if(procedure.clusterKey[0] == clusterKey) {
394 pRef = (short)procedure.predicateReference[0];
396 pRef = getInternalReferenceOrZero2(predicateKey, support);
397 procedure.clusterKey[0] = clusterKey;
398 procedure.predicateReference[0] = pRef;
400 final ClusterI.CompleteTypeEnum pCompleteType = procedure.completeType;
401 if (CompleteTypeEnum.NotComplete != pCompleteType)
402 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
403 final int predicateIndex = (int)resourceTable.table[(resourceIndex<<1) - 1 + resourceTable.offset] & 0xFFFFFF;
404 if (0 == predicateIndex) // All relevant data is in resource table.
405 return resourceTable.getSingleObject(resourceIndex, support, pRef, pCompleteType, completeTable, this);
406 int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
407 return getSingleObject(resourceKey, predicateKey, objectIndex, support);
411 public void forObjects(ReadGraphImpl graph, int resourceKey,
412 int predicateKey, SyncMultiProcedure<Resource> procedure) throws DatabaseException {
414 SessionImplSocket session = (SessionImplSocket)graph.getSession();
415 ClusterSupport support = session.clusterTranslator;
417 System.out.println("ClusterSmall.forObjects3: rk=" + resourceKey + " pk=" + predicateKey);
418 final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
419 final int pRef = getInternalReferenceOrZero2(predicateKey, support);
420 final int completeType = ClusterTraitsBase.getCompleteTypeIntFromResourceKey(predicateKey);
421 final ClusterI.CompleteTypeEnum pCompleteType = CompleteTypeEnum.make(completeType);
422 if (completeType > 0) {
423 resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
426 final int predicateIndex = (int)resourceTable.table[(resourceIndex<<1) - 1 + resourceTable.offset] & 0xFFFFFF;
427 if (0 == predicateIndex) {
428 resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
431 int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
432 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, cluster mismatch.");
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, cluster mismatch.");
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;