]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ForeignTableSmall.java
Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / cluster / ForeignTableSmall.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.db.procore.cluster;
13
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.impl.ClusterBase;
16 import org.simantics.db.impl.ClusterI;
17 import org.simantics.db.impl.ClusterI.Procedure;
18 import org.simantics.db.impl.ClusterSupport;
19 import org.simantics.db.impl.ClusterTraitsBase;
20 import org.simantics.db.impl.IClusterTable;
21 import org.simantics.db.impl.Modifier;
22 import org.simantics.db.impl.Table;
23 import org.simantics.db.impl.TableFactory;
24 import org.simantics.db.service.ClusterUID;
25 import org.simantics.db.service.ResourceUID;
26
27 import gnu.trove.map.hash.TIntShortHashMap;
28
29 final public class ForeignTableSmall extends Table<long[]>
30 {
31     private IClusterTable clusterTable;
32     public ForeignTableSmall(ClusterBase sizeListener, int[] header, int headerBase) {
33         super(TableFactory.getLongFactory(), sizeListener, header, headerBase);
34         this.clusterTable = sizeListener.getClusterTable();
35     }
36     public ForeignTableSmall(ClusterBase sizeListener, int[] header, int headerBase, long[] longs) {
37         super(TableFactory.getLongFactory(), sizeListener, header, headerBase, longs);
38         this.clusterTable = sizeListener.getClusterTable();
39     }
40     public int getUsedSize() {
41         return getTableCount();
42     }
43     static long rmTime = 0;
44     public TIntShortHashMap getResourceHashMap()
45     throws DatabaseException {
46 //        long start = System.nanoTime();
47         int ELEMENT_SIZE = ForeignElement.getSizeOf();
48         assert(ELEMENT_SIZE == 3);
49         final int TABLE_SIZE = getTableCount();
50         final int FOREIGN_COUNT = getForeignCount();
51         final int BASE = this.getTableBase();
52         final int TOP = BASE + TABLE_SIZE * ELEMENT_SIZE;
53         long[] table = this.getTable();
54         int count = 0;
55         int index = ZERO_SHIFT;
56         
57         long firstCache = 0;
58         long secondCache = 0;
59         int clusterKeyCache = 0;
60         
61         TIntShortHashMap hm = new TIntShortHashMap(FOREIGN_COUNT);
62         for (int i=BASE; i<TOP && count<FOREIGN_COUNT;
63         i+=ELEMENT_SIZE, ++index) {
64             
65             if (0 == table[i] && 0 == table[i+1] && 0 == table[i+2])
66                 continue; // element has been deleted
67             
68             long first = table[i];
69             long second = table[i+1];
70             long third = table[i+2];
71             
72             int clusterKey = 0;
73             if(firstCache == first && secondCache == second) {
74                 clusterKey = clusterKeyCache;
75             }
76             if(clusterKey == 0) {
77                 clusterKey = clusterTable.getClusterKeyByUID(first, second);
78 //                cluster = clusterTable.getClusterByClusterUIDOrMakeProxy(ClusterUID.make(first, second));
79                 clusterKeyCache = clusterKey;
80                 firstCache = first;
81                 secondCache =second;
82             }
83             
84             int resourceKey = ClusterTraits.createResourceKey(clusterKey, (int)third);
85             hm.put(resourceKey, ClusterTraitsSmall.makeForeignRef((short)index));
86             ++count;
87             
88         }
89         if (FOREIGN_COUNT != hm.size())
90             throw new DatabaseException("Foreign table has been corrupted. count=" + FOREIGN_COUNT + "size=" + hm.size());
91 //        rmTime += System.nanoTime()-start;
92 //        System.err.println("rmTime: " + 1e-9*rmTime+"s.");
93         return hm;
94     }
95     short createForeign(int resourceKey, ClusterUID cuid) {
96         int index = getTableCount();
97         int size = ForeignElement.getSizeOf();
98         int foreignIndex = createNewElement(size);
99         int realIndex = checkIndexAndGetRealIndex(foreignIndex, size);
100         short resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);
101         //ClusterUID cuid = clusterTable.getClusterUIDByResourceKey(resourceKey);
102         ResourceUID uid = cuid.toRID(resourceIndex);
103         ForeignElement.constructForeign(getTable(), realIndex, uid);
104         incForeignCount();
105         return ClusterTraitsSmall.makeForeignRef((short)(index + ZERO_SHIFT));
106     }
107     short createForeign(int resourceKey) {
108         int index = getTableCount();
109         int size = ForeignElement.getSizeOf();
110         int foreignIndex = createNewElement(size);
111         int realIndex = checkIndexAndGetRealIndex(foreignIndex, size);
112         short resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);
113         ClusterI cluster = clusterTable.getClusterProxyByResourceKey(resourceKey);
114         //ClusterI cluster = clusterTable.getClusterByResourceKey(resourceKey);
115         ClusterUID cuid = cluster.getClusterUID();
116         ResourceUID uid = cuid.toRID(resourceIndex);
117         ForeignElement.constructForeign(getTable(), realIndex, uid);
118         incForeignCount();
119         return ClusterTraitsSmall.makeForeignRef((short)(index + ZERO_SHIFT));
120     }
121     void deleteForeign(int foreignIndex) {
122         int realIndex = checkIndexAndGetRealIndex(foreignIndex);
123         ForeignElement.destructForeign(getTable(), realIndex);
124         decForeignCount();
125     }
126     public final ResourceUID getResourceUID(int foreignIndex) {
127         return ForeignElement.getResourceUID(table, checkIndexAndGetRealIndex(foreignIndex)); 
128     }
129     final void fillResourceUID(int foreignIndex, ClusterSmall cluster) {
130         ForeignElement.fillResourceUID(table, checkIndexAndGetRealIndex(foreignIndex), cluster); 
131     }
132     int getForeignCount() {
133         return getExtra(FOREIGN_COUNT_INDEX);
134     }
135     private static final int FOREIGN_COUNT_INDEX = 0;
136     private int incForeignCount() {
137         int count = getExtra(FOREIGN_COUNT_INDEX) + 1;
138         setExtra(FOREIGN_COUNT_INDEX, count);
139         return count;
140     }
141     private int decForeignCount() {
142         int count = getExtra(FOREIGN_COUNT_INDEX) - 1;
143         setExtra(FOREIGN_COUNT_INDEX, count);
144         return count;
145     }
146     private int checkIndexAndGetRealIndex(int foreignIndex) {
147         int index = (foreignIndex - ZERO_SHIFT) * ForeignElement.getSizeOf() + ZERO_SHIFT;
148         int realIndex = checkIndexAndGetRealIndex(index, ForeignElement.getSizeOf());
149         return realIndex;
150     }
151     @Override
152     public <Context> boolean foreach(int setIndex, Procedure procedure, Context context,
153             ClusterSupport support, Modifier modifier) throws DatabaseException {
154         throw new UnsupportedOperationException();
155     }
156 }