]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/cluster/ClusterSmall.java
List SCL references in order of reference location in search results
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / cluster / ClusterSmall.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.acorn.cluster;
13
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;
19
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;
56
57 import gnu.trove.map.hash.TIntShortHashMap;
58 import gnu.trove.procedure.TIntProcedure;
59 import gnu.trove.set.hash.TIntHashSet;
60
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;
83     
84     protected ClusterSmall() {
85         this.proxy = true;
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;
95         this.clusterBits = 0;
96         this.importance = 0;
97     }
98     
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();
103         this.proxy = true;
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;
114         this.importance = 0;
115 //        new Exception("ClusterSmall " + clusterKey).printStackTrace();
116     }
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();
121         this.proxy = false;
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();
134 //        else
135                 this.importance = 0;
136 //        new Exception("ClusterSmall " + clusterKey).printStackTrace();
137     }
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);
141         this.proxy = false;
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());
158 //        }
159 //        new Exception("ClusterSmall " + clusterKey).printStackTrace();
160     }
161     void analyse() {
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());
170
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();
178     }
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);
188         } else {
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);
195         }
196     }
197     public void checkPredicateIndex(int pi)
198     throws DatabaseException {
199         //        predicateTable.checkPredicateSetIndex(this, pi);
200     }
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);
207     }
208
209     public void checkValueInit()
210     throws DatabaseException {
211         valueTable.checkValueInit();
212     }
213     public void checkValue(int capacity, int index)
214     throws DatabaseException {
215         valueTable.checkValue(capacity, index);
216     }
217     public void checkValueFini()
218     throws DatabaseException {
219         valueTable.checkValueFini();
220     }
221     public void checkForeingIndex(int fi)
222     throws DatabaseException {
223         if (fi<1 || fi > foreignTable.getUsedSize())
224             throw new ValidationException("Illegal foreign index=" + fi);
225     }
226     public void checkCompleteSetReference(int cr)
227     throws DatabaseException {
228         if (!ClusterTraits.completeReferenceIsMultiple(cr))
229             throw new ValidationException("Illegal complete set reference. Reference=" + cr);
230         int ci = cr;
231         this.completeTable.checkCompleteSetIndex(this, ci);
232     }
233     public void check()
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);
240     }
241     @Override
242     public CompleteTypeEnum getCompleteType(int resourceKey, ClusterSupport support)
243     throws DatabaseException {
244         final int resourceRef = getLocalReference(resourceKey);
245         CompleteTypeEnum ct = resourceTable.getCompleteType(resourceRef);
246         if (DEBUG)
247             System.out.println("ClusterSmall.getCompleteType rk=" + resourceKey + " ct=" + ct);
248         return ct;
249     }
250
251     @Override
252     public int getCompleteObjectKey(int resourceKey, ClusterSupport support)
253     throws DatabaseException {
254         final int resourceIndexOld = getLocalReference(resourceKey);
255         short completeRef = resourceTable.getCompleteObjectRef(resourceIndexOld);
256         int clusterIndex;
257         int resourceIndex;
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();
274         }
275         int key = ClusterTraits.createResourceKey(clusterIndex, resourceIndex);
276         if (DEBUG)
277             System.out.println("ClusterSmall.complete object rk=" + resourceKey + " ck=" + key);
278         return key;
279     }
280
281     @Override
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;
287         if (DEBUG)
288             System.out.println("ClusterSmall.key=" + resourceKey + " isComplete=" + complete);
289         return complete;
290     }
291     public int getSingleObject(int resourceKey, int predicateKey, int objectIndex, ClusterSupport support) throws DatabaseException {
292         if (DEBUG)
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);
299         }
300         return objectTable.getSingleObject(objectIndex, support, this);
301     }
302
303     public void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, AsyncMultiProcedure<Resource> procedure,
304             ClusterSupport support) throws DatabaseException {
305         if (DEBUG)
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);
312             return;
313         }
314         objectTable.foreachObject(graph, objectIndex, procedure, this);
315     }
316
317     public <C> void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, C context, AsyncContextMultiProcedure<C, Resource> procedure,
318             ClusterSupport support) throws DatabaseException {
319         if (DEBUG)
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);
326             return;
327         }
328         objectTable.foreachObject(graph, objectIndex, context, procedure, this);
329     }
330     
331     @Override
332     public <Context> boolean forObjects(int resourceKey, int predicateKey, int objectIndex, ObjectProcedure<Context> procedure,
333             Context context, ClusterSupport support) throws DatabaseException {
334         if (DEBUG)
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);
341         }
342         return objectTable.foreachObject(objectIndex, procedure, context, support, this);
343     }
344
345     @Override
346     public int getSingleObject(int resourceKey, int predicateKey, ClusterSupport support) throws DatabaseException {
347         if (DEBUG)
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);
360     }
361
362     @Override
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);
367         short pRef = 0;
368         if(procedure.clusterKey[0] == clusterKey) {
369             pRef = (short)procedure.predicateReference[0];
370         } else {
371             pRef = getInternalReferenceOrZero2(predicateKey, support);
372             procedure.clusterKey[0] = clusterKey;
373             procedure.predicateReference[0] = pRef;
374         }
375         
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);
384     }
385
386     @Override
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);
391         short pRef = 0;
392         if(procedure.clusterKey[0] == clusterKey) {
393             pRef = (short)procedure.predicateReference[0];
394         } else {
395             pRef = getInternalReferenceOrZero2(predicateKey, support);
396             procedure.clusterKey[0] = clusterKey;
397             procedure.predicateReference[0] = pRef;
398         }
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);
407     }
408
409     @Override
410     public void forObjects(ReadGraphImpl graph, int resourceKey,
411             int predicateKey, AsyncMultiProcedure<Resource> procedure) throws DatabaseException {
412         
413         throw new UnsupportedOperationException();
414         
415 //        SessionImplSocket session = (SessionImplSocket)graph.getSession();
416 //        ClusterSupport support = session.clusterTranslator;
417 //        if (DEBUG)
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);
425 //            return;
426 //        }
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);
430 //            return;
431 //        }
432 //        int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
433 //        forObjects(graph, resourceKey, predicateKey, objectIndex, procedure, support);
434     }
435
436     public void forObjects(ReadGraphImpl graph, int resourceKey, ForEachObjectProcedure procedure) throws DatabaseException {
437         
438         throw new UnsupportedOperationException();
439
440 //        final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
441 //        final int predicateKey = procedure.predicateKey;
442 //        int clusterKey = ClusterTraitsBase.getClusterMaskFromResourceKey(resourceKey);
443 //        int pRef = 0;
444 //        if(procedure.clusterKey[0] == clusterKey) {
445 //            pRef = procedure.predicateReference[0];
446 //        } else {
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;
452 //        }
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);
458 //            return;
459 //        }
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);
465 //            return;
466 //        }
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);
474 //        } else {
475 //            procedure.finished(graph);
476 ////            graph.dec();
477 //        }
478     }
479
480     public <C> void forObjects(ReadGraphImpl graph, int resourceKey, C context, ForEachObjectContextProcedure<C> procedure) throws DatabaseException {
481         
482         throw new UnsupportedOperationException();
483
484 //        final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
485 //        final int predicateKey = procedure.predicateKey;
486 //        int clusterKey = ClusterTraitsBase.getClusterMaskFromResourceKey(resourceKey);
487 //        int pRef = 0;
488 //        if(procedure.clusterKey[0] == clusterKey) {
489 //            pRef = procedure.predicateReference[0];
490 //        } else {
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;
496 //        }
497 //        
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);
503 //            return;
504 //        }
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);
510 //            return;
511 //        }
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);
518 //        } else {
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);
523 //        }
524     }
525     @Override
526     public <Context> boolean forObjects(int resourceKey, int predicateKey,
527             ObjectProcedure<Context> procedure, Context context, ClusterSupport support)
528     throws DatabaseException {
529         if (DEBUG)
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) { 
536             if (DEBUG)
537                 System.out.println("ClusterSmall.forObjects: complete type was " + pCompleteType + " cluster=" + getClusterUID());
538             return resourceTable.foreachObject(resourceIndex, procedure, context, support, this, pRef, pCompleteType, completeTable);
539         }
540         final int predicateIndex = resourceTable.getPredicateIndex(resourceIndex);
541         if (0 == predicateIndex) { // All relevant data is in resource table.
542             if (DEBUG)
543                 System.out.println("ClusterSmall.forObjects: no predicate table " + pCompleteType);
544             return resourceTable.foreachObject(resourceIndex, procedure, context, support, this, pRef, pCompleteType, completeTable);
545         }
546         int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
547         return forObjects(resourceKey, predicateKey, objectIndex, procedure, context, support);
548     }
549     @Override
550     public <Context> boolean forPredicates(int resourceKey,
551             PredicateProcedure<Context> procedure, Context context, ClusterSupport support)
552     throws DatabaseException {
553         if (DEBUG)
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);
560         else {
561             boolean broken = resourceTable.foreachPredicate(resourceIndex,
562                     procedure, context, support, this, completeTable);
563             if (broken)
564                 return true;
565         }
566         return predicateTable.foreachPredicate(predicateIndex,
567                 procedure, context, support, this);
568     }
569     
570     @Override
571     public ClusterI addRelation(int sResourceKey, ClusterUID puid, int pResourceKey, ClusterUID ouid, int oResourceKey, ClusterSupport support) throws DatabaseException {
572
573         if(proxy) {
574                 throw new UnsupportedOperationException();
575 //            ClusterImpl cluster = clusterTable.load2(clusterId, clusterKey);
576 //            return cluster.addRelation(sResourceKey, pResourceKey, oResourceKey, support);
577         }
578
579         //        check();
580         boolean ret;
581         try {
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();
590             if (!streamOff) {
591                 support.cancelStatement(this);
592                 support.setStreamOff(true);
593             }
594             ClusterI cluster = toBig(clusterSupport);
595             if (!streamOff)
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.");
600             return cluster;
601         }
602 //        check();
603         if (ret) {
604             support.addStatement(this);
605             return this;
606         } else {
607             support.cancelStatement(this);
608             return null;
609         }
610         
611     }
612
613     @Override
614     public ClusterI addRelation(int sResourceKey, int pResourceKey, int oResourceKey, ClusterSupport support) throws DatabaseException {
615         
616         if (DEBUG)
617             System.out.println("add rk=" + sResourceKey + " pk=" + pResourceKey + " ok=" + oResourceKey);
618         
619         if(proxy) {
620                 throw new UnsupportedOperationException();
621 //            ClusterImpl cluster = clusterTable.load2(clusterId, clusterKey);
622 //            return cluster.addRelation(sResourceKey, pResourceKey, oResourceKey, support);
623         }
624
625         //        check();
626         boolean ret;
627         try {
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();
636             if (!streamOff) {
637                 support.cancelStatement(this);
638                 support.setStreamOff(true);
639             }
640             ClusterI cluster = toBig(clusterSupport);
641             if (!streamOff)
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.");
646             return cluster;
647         }
648 //        check();
649         if (ret) {
650             support.addStatement(this);
651             return this;
652         } else {
653             support.cancelStatement(this);
654             return null;
655         }
656     }
657     @Override
658     public boolean removeRelation(int sResourceKey, int pResourceKey, int oResourceKey, ClusterSupport support)
659     throws DatabaseException {
660         //        check();
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);
664         boolean ret = false;
665         if (0 != pri && 0 != ori) {
666             ClusterI.CompleteTypeEnum completeType = ClusterTraitsBase.getCompleteTypeFromResourceKey(pResourceKey);
667             ret = removeRelationInternal(sri, pri, ori, completeType, support);
668             calculateModifiedId();
669         }
670         if (ret)
671             support.removeStatement(this);
672         else
673             support.cancelStatement(this);
674         //        check();
675         return ret;
676     }
677     @Override
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)
684             return;
685         //        check();
686         ClusterI.CompleteTypeEnum completeType = ClusterTraitsBase.getCompleteTypeFromResourceKey(pResourceKey);
687         boolean ret = removeRelationInternal(s, p.reference, o.reference, completeType, support);
688         if (ret) {
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);
693         }
694         calculateModifiedId();
695         //        check();
696         return;
697     }
698     @Override
699     public InputStream getValueStream(int resourceKey, ClusterSupport support) throws DatabaseException {
700         if (DEBUG)
701             System.out.println("ClusterSmall.getValue " + resourceKey);
702         int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);
703         try {
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);
709         }
710     }
711     @Override
712     public byte[] getValue(int resourceKey, ClusterSupport support) throws DatabaseException {
713         if (DEBUG)
714             System.out.println("ClusterSmall.getValue " + resourceKey);
715         int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);
716         try {
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);
721         }
722     }
723     @Override
724     public boolean hasValue(int resourceKey, ClusterSupport support)
725     throws DatabaseException {
726         int resourceIndex = getLocalReference(resourceKey);
727         return resourceTable.hasValue(resourceIndex);
728     }
729     @Override
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);
736     }
737     @Override
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);
742         try {
743             resourceTable.setValue(valueTable, resourceIndex, value, length);
744             calculateModifiedId();
745             return this;
746         } catch (OutOfSpaceException e) {
747             boolean streamOff = support.getStreamOff();
748             if (!streamOff)
749                 support.setStreamOff(true);
750             ClusterI cluster = toBig(support);
751             cluster.setValue(rResourceId, value, length, support);
752             if (!streamOff)
753                 support.setStreamOff(false);
754             return cluster;
755         }
756     }
757     @Override
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();
764         return this;
765     }
766     @Override
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);
771         if (!isExternal)
772             throw new DatabaseException("ClusterI.readValue supported only for external value. Resource key=" + rResourceId);
773         return support.getValueEx(resourceIndex, getClusterId(), voffset, length);
774     }
775     @Override
776     public boolean isValueEx(int resourceKey) throws DatabaseException {
777         int resourceIndex = getLocalReference(resourceKey);
778         return resourceTable.isValueEx(valueTable, resourceIndex);
779     }
780     @Override
781     public long getValueSizeEx(int rResourceId, ClusterSupport support)
782     throws DatabaseException, ExternalValueException {
783         int resourceIndex = getLocalReference(rResourceId);
784         boolean isExternal = resourceTable.isValueEx(valueTable, resourceIndex);
785         if (!isExternal)
786             throw new ExternalValueException("ClusterI.getValueSizeEx supported only for external value. Resource key=" + rResourceId);
787         return support.getValueSizeEx(resourceIndex, getClusterId());
788     }
789     @Override
790     public void setValueEx(int rResourceId)
791     throws DatabaseException {
792         int resourceIndex = getLocalReference(rResourceId);
793         resourceTable.setValueEx(valueTable, resourceIndex);
794     }
795     @Override
796     public int createResource(ClusterSupport support)
797     throws DatabaseException {
798
799         if(proxy) {
800                 throw new UnsupportedOperationException();
801 //              ClusterImpl cluster = clusterTable.load2(clusterId, clusterKey);
802 //            return cluster.createResource(support);
803         }
804         
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);
811     }
812     @Override
813     public boolean hasResource(int resourceKey, ClusterSupport support) {
814         int clusterKey = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(resourceKey);
815         if (this.clusterKey != clusterKey) // foreign resource
816             return false;
817         int resourceIndex;
818         try {
819             resourceIndex = ClusterTraits.getResourceIndexFromResourceKey(resourceKey);
820         } catch (DatabaseException e) {
821             return false;
822         }
823         if (resourceIndex > 0 & resourceIndex <= resourceTable.getTableCount())
824             return true;
825         else
826             return false;
827     }
828     @Override
829     public int getNumberOfResources(ClusterSupport support)
830     throws DatabaseException  {
831         
832         if(proxy) {
833                 throw new UnsupportedOperationException();
834 //            ClusterImpl cluster = clusterTable.load2(clusterId, clusterKey);
835 //            return cluster.getNumberOfResources(support);
836         }
837         
838         return resourceTable.getUsedSize();
839     }
840
841     public int getNumberOfResources() throws IllegalAcornStateException {
842         if(proxy)
843             throw new IllegalAcornStateException("proxy == true for " + clusterId);
844         
845         return resourceTable.getUsedSize();
846     }
847
848     @Override
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;
859     }
860     @Override
861     public boolean isEmpty() {
862         if(resourceTable == null) return true;
863         return resourceTable.getTableCount() == 0;
864     }
865     @Override
866     public void printDebugInfo(String message, ClusterSupport support)
867     throws DatabaseException {
868         throw new DatabaseException("Not implemented!");
869     }
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);
874         } else {
875             return (short)resourceIndex;
876         }
877     }
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);
886             return foreignRef;
887         }
888         support.addStatementIndex(this, resourceKey, getClusterUID(), op);
889         return (short)resourceIndex;
890     }
891     private final short getLocalReference(int resourceKey) throws DatabaseException {
892         return ClusterTraits.getResourceIndexFromResourceKeyNoThrow(resourceKey);
893     }
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;
899     }
900     private short checkResourceKeyIsOursAndGetResourceIndexIf(int resourceKey, ClusterSupport support)
901     throws DatabaseException {
902         int clusterShortId = ClusterTraits.getClusterKeyFromResourceKey(resourceKey);
903         if (this.clusterKey != clusterShortId)
904             return 0;
905         int resourceIndex = ClusterTraits.getResourceIndexFromResourceKey(resourceKey);
906         return (short)resourceIndex;
907     }
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);
915             return ref;
916         }
917         support.addStatementIndex(this, resourceKey, getClusterUID(), op);
918         return resourceIndex;
919     }
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);
928             return ref;
929         }
930         support.addStatementIndex(this, resourceKey, getClusterUID(), op);
931         return resourceIndex;
932     }
933     private class ResourceReferenceAndCluster {
934         ResourceReferenceAndCluster(short reference, ClusterUID clusterUID) {
935             this.reference = reference;
936             this.clusterUID = clusterUID;
937         }
938         public final short reference;
939         public final ClusterUID clusterUID;
940     }
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);
950         }
951         return new ResourceReferenceAndCluster(resourceIndex, getClusterUID());
952     }
953
954     static long fTime = 0;
955     
956     @Override
957     final public int execute(int resourceReference) throws DatabaseException {
958         short resourceRef = (short)resourceReference;
959         int key;
960         if (ClusterTraitsSmall.resourceRefIsLocal(resourceRef)) {
961             key = clusterBits | resourceRef;
962         } else {
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);
971         }
972         if (DEBUG)
973             System.out.println("ClusterSmall.execute key=" + key);
974         return key;
975     }
976
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)
986             return false;
987         if (predicateIndex != newPredicateIndex)
988             resourceTable.setPredicateIndex(sReference, newPredicateIndex);
989         return true;
990     }
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);
999         switch (ret) {
1000         case NothingRemoved:
1001             return false;
1002         case PredicateRemoved: {
1003             if (0 == predicateTable.getPredicateSetSize(predicateIndex))
1004                 resourceTable.setPredicateIndex(sResourceIndex, 0);
1005             // intentionally dropping to next case
1006         } default:
1007             break;
1008         }
1009         resourceTable.removeStatement(sResourceIndex,
1010                 pResourceIndex, oResourceIndex,
1011                 completeType, completeTable,
1012                 predicateTable, objectTable, support);
1013         return true;
1014     }
1015     @Override
1016     public void load() {
1017         throw new Error("Not supported.");
1018     }
1019
1020     @Override
1021     public void load(Consumer<DatabaseException> r) {
1022         throw new Error("Not supported.");
1023     }
1024
1025     public boolean contains(int resourceKey) {
1026         return ClusterTraitsBase.isCluster(clusterBits, resourceKey);
1027     }
1028     @Override
1029     public void load(final ClusterSupport support, final Runnable callback) {
1030
1031         throw new UnsupportedOperationException();
1032
1033 //      try {
1034 //            clusterTable.load2(clusterId, clusterKey);
1035 //            callback.run();
1036 //        } catch (DatabaseException e) {
1037 //            e.printStackTrace();
1038 //        }
1039         
1040     }
1041     @Override
1042     public ClusterI getClusterByResourceKey(int resourceKey,
1043             ClusterSupport support) {
1044         throw new Error();
1045     }
1046     @Override
1047     public void increaseReferenceCount(int amount) {
1048         throw new Error();
1049     }
1050     @Override
1051     public void decreaseReferenceCount(int amount) {
1052         throw new Error();
1053     }
1054     @Override
1055     public int getReferenceCount() {
1056         throw new Error();
1057     }
1058     @Override
1059     public void releaseMemory() {
1060     }
1061     @Override
1062     public void compact() {
1063         clusterMap.compact();
1064     }
1065     @Override
1066     public boolean isLoaded() {
1067         return !proxy;
1068     }
1069
1070 //    public ClusterImpl tryLoad(SessionImplSocket sessionImpl) {
1071 //
1072 //      throw new UnsupportedOperationException();
1073 //        assert(Constants.ReservedClusterId != clusterId);
1074 //
1075 //        return clusterTable.tryLoad(clusterId, clusterKey);
1076 //        
1077 //    }
1078     
1079
1080     @Override
1081     public ClusterBig toBig(ClusterSupport support)
1082     throws DatabaseException {
1083         if (DEBUG) {
1084             System.out.println("DEBUG: toBig cluster=" + clusterId);
1085             new Exception().printStackTrace();
1086         }
1087         ClusterBig big = new ClusterBig(clusterSupport, getClusterUID(), clusterKey, (ClusterSupport2)support);
1088         big.cc = this.cc;
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;
1094         this.cc = null;
1095         this.foreignLookup = null;
1096         this.change = null;
1097         return big;
1098     }
1099     
1100     @Override
1101     public ClusterTypeEnum getType() {
1102         return ClusterTypeEnum.SMALL;
1103     }
1104     @Override
1105     public boolean getImmutable() {
1106         int status = resourceTable.getClusterStatus();
1107         return (status & ClusterStatus.ImmutableMaskSet) == 1;
1108     }
1109     @Override
1110     public void setImmutable(boolean immutable, ClusterSupport support) {
1111         if(resourceTable != null) {
1112             int status = resourceTable.getClusterStatus();
1113             if (immutable)
1114                 status |= ClusterStatus.ImmutableMaskSet;
1115             else
1116                 status &= ClusterStatus.ImmutableMaskClear;
1117             resourceTable.setClusterStatus(status);
1118         }
1119         support.setImmutable(this, immutable);
1120     }
1121     
1122     @Override
1123     public String toString() {
1124         try {
1125             final TIntHashSet set = new TIntHashSet();
1126             TIntShortHashMap map = foreignTable.getResourceHashMap();
1127             map.forEachKey(new TIntProcedure() {
1128                 @Override
1129                 public boolean execute(int value) {
1130                     set.add(value & 0xfffff000);
1131                     return true;
1132                 }
1133             });
1134             return "ClusterSmall[" + getClusterUID() + " - " + getClusterId() + " - " + getNumberOfResources() + " - " + foreignTable.getResourceHashMap().size() + " - " + set.size() + "]";
1135         } catch (DatabaseException e) {
1136             try {
1137                 return "ClusterSmall[" + getNumberOfResources() + "]";
1138             } catch (IllegalAcornStateException e1) {
1139                 Logger.defaultLogError(e1);
1140                 e1.printStackTrace();
1141                 return "An exception occured!!";
1142             }
1143         }
1144     }
1145     
1146     // Memory map
1147     // bytes (b) | headers(i) | predicateTable (i) | objectTable (i) | completeTable (i) | resourceTable (l) | foreignTable (l)
1148
1149     @Override
1150     public byte[] storeBytes() throws IOException {
1151
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();
1155
1156         byte[] raw = new byte[12 + byteSize + 8*longSize + 4*intSize];
1157
1158                 int[] currentHeader = Arrays.copyOf(headerTable, INT_HEADER_SIZE);
1159                 
1160                 Bytes.writeLE(raw, 0, byteSize);
1161                 Bytes.writeLE(raw, 4, intSize);
1162                 Bytes.writeLE(raw, 8, longSize);
1163                 
1164                 int rawPos = valueTable.storeBytes(raw, 0, 12);
1165                 
1166                 int intBase = rawPos;
1167                 
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);
1172
1173                 int longBase = rawPos;
1174                 
1175                 rawPos += 8*LONG_HEADER_SIZE;
1176                 rawPos = resourceTable.storeBytes(raw, (rawPos-longBase)>>3, rawPos);
1177                 rawPos = foreignTable.storeBytes(raw, (rawPos-longBase)>>3, rawPos);
1178                 
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);
1183
1184         // write header
1185                 for(int i=0;i<INT_HEADER_SIZE;i++) {
1186                         int v = headerTable[i];
1187                         Bytes.writeLE(raw, intBase, v);
1188                         intBase+=4;
1189                 }
1190
1191                 for(int i=0;i<INT_HEADER_SIZE;i++)
1192                 headerTable[i] = currentHeader[i];
1193                 
1194         return raw;
1195         
1196     }
1197
1198     @Override
1199     public ClusterTables store() throws IOException {
1200
1201         ClusterTables result = new ClusterTables();
1202         
1203                 int[] currentHeader = Arrays.copyOf(headerTable, INT_HEADER_SIZE);
1204
1205         int byteSize = valueTable.getTableSize();
1206         byte[] byteBytes = new byte[byteSize];
1207         valueTable.store(byteBytes, 0);
1208         
1209         result.bytes = byteBytes;
1210
1211         int longSize = LONG_HEADER_SIZE + resourceTable.getTableSize() + foreignTable.getTableSize(); 
1212         long[] longBytes = new long[longSize];
1213
1214         longBytes[0] = -1;
1215         longBytes[1] = LONG_HEADER_VERSION;
1216         longBytes[2] = 0;
1217         longBytes[3] = clusterUID.second;
1218         
1219         int longPos = resourceTable.store(longBytes, LONG_HEADER_SIZE);
1220         foreignTable.store(longBytes, longPos);
1221         
1222         result.longs = longBytes;
1223         
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);
1230         // write header
1231                 for(int i=0;i<INT_HEADER_SIZE;i++) {
1232                         int v = headerTable[i];
1233                         intBytes[i] = v;
1234                         //Bytes.writeLE(intBytes, i<<2, v);
1235                 }
1236                 
1237                 result.ints = intBytes;
1238
1239         for(int i=0;i<INT_HEADER_SIZE;i++)
1240                 headerTable[i] = currentHeader[i];
1241         
1242         return result;
1243
1244     }
1245     
1246     @Override
1247     protected int getResourceTableCount() {
1248         return resourceTable.getTableCount();
1249     }
1250     
1251     @Override
1252     public boolean getDeleted() {
1253         if (deleted) return true;
1254         int status = resourceTable.getClusterStatus();
1255         return (status & ClusterStatus.DeletedMaskSet) == ClusterStatus.DeletedMaskSet;
1256     }
1257     @Override
1258     public void setDeleted(boolean set, ClusterSupport support) {
1259         deleted = set;
1260         if(resourceTable != null) {
1261             int status = resourceTable.getClusterStatus();
1262             if (set)
1263                 status |= ClusterStatus.DeletedMaskSet;
1264             else
1265                 status &= ClusterStatus.DeletedMaskClear;
1266             resourceTable.setClusterStatus(status);
1267         }
1268         if (null != support)
1269             support.setDeleted(this, set);
1270     }
1271
1272     @Override
1273     public Table<?> getPredicateTable() {
1274         return predicateTable;
1275     }
1276
1277     @Override
1278     public Table<?> getForeignTable() {
1279         return foreignTable;
1280     }
1281
1282     @Override
1283     public int makeResourceKey(int pRef) throws DatabaseException {
1284         throw new UnsupportedOperationException();
1285     }
1286
1287     @Override
1288     public Table<?> getCompleteTable() {
1289         return completeTable;
1290     }
1291
1292     @Override
1293     public Table<?> getValueTable() {
1294         return valueTable;
1295     }
1296
1297     @Override
1298     public Table<?> getObjectTable() {
1299         return objectTable;
1300     }
1301     
1302 }
1303
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;
1309 }