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