-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.procore.cluster;\r
-\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.impl.ClusterBase;\r
-import org.simantics.db.impl.ClusterI;\r
-import org.simantics.db.impl.ClusterI.Procedure;\r
-import org.simantics.db.impl.ClusterSupport;\r
-import org.simantics.db.impl.ClusterTraitsBase;\r
-import org.simantics.db.impl.IClusterTable;\r
-import org.simantics.db.impl.Modifier;\r
-import org.simantics.db.impl.Table;\r
-import org.simantics.db.impl.TableFactory;\r
-import org.simantics.db.service.ClusterUID;\r
-import org.simantics.db.service.ResourceUID;\r
-\r
-import gnu.trove.map.hash.TIntShortHashMap;\r
-\r
-final public class ForeignTableSmall extends Table<long[]>\r
-{\r
- private IClusterTable clusterTable;\r
- public ForeignTableSmall(ClusterBase sizeListener, int[] header, int headerBase) {\r
- super(TableFactory.getLongFactory(), sizeListener, header, headerBase);\r
- this.clusterTable = sizeListener.getClusterTable();\r
- }\r
- public ForeignTableSmall(ClusterBase sizeListener, int[] header, int headerBase, long[] longs) {\r
- super(TableFactory.getLongFactory(), sizeListener, header, headerBase, longs);\r
- this.clusterTable = sizeListener.getClusterTable();\r
- }\r
- public int getUsedSize() {\r
- return getTableCount();\r
- }\r
- static long rmTime = 0;\r
- public TIntShortHashMap getResourceHashMap()\r
- throws DatabaseException {\r
-// long start = System.nanoTime();\r
- int ELEMENT_SIZE = ForeignElement.getSizeOf();\r
- assert(ELEMENT_SIZE == 3);\r
- final int TABLE_SIZE = getTableCount();\r
- final int FOREIGN_COUNT = getForeignCount();\r
- final int BASE = this.getTableBase();\r
- final int TOP = BASE + TABLE_SIZE * ELEMENT_SIZE;\r
- long[] table = this.getTable();\r
- int count = 0;\r
- int index = ZERO_SHIFT;\r
- \r
- long firstCache = 0;\r
- long secondCache = 0;\r
- int clusterKeyCache = 0;\r
- \r
- TIntShortHashMap hm = new TIntShortHashMap(FOREIGN_COUNT);\r
- for (int i=BASE; i<TOP && count<FOREIGN_COUNT;\r
- i+=ELEMENT_SIZE, ++index) {\r
- \r
- if (0 == table[i] && 0 == table[i+1] && 0 == table[i+2])\r
- continue; // element has been deleted\r
- \r
- long first = table[i];\r
- long second = table[i+1];\r
- long third = table[i+2];\r
- \r
- int clusterKey = 0;\r
- if(firstCache == first && secondCache == second) {\r
- clusterKey = clusterKeyCache;\r
- }\r
- if(clusterKey == 0) {\r
- clusterKey = clusterTable.getClusterKeyByUID(first, second);\r
-// cluster = clusterTable.getClusterByClusterUIDOrMakeProxy(ClusterUID.make(first, second));\r
- clusterKeyCache = clusterKey;\r
- firstCache = first;\r
- secondCache =second;\r
- }\r
- \r
- int resourceKey = ClusterTraits.createResourceKey(clusterKey, (int)third);\r
- hm.put(resourceKey, ClusterTraitsSmall.makeForeignRef((short)index));\r
- ++count;\r
- \r
- }\r
- if (FOREIGN_COUNT != hm.size())\r
- throw new DatabaseException("Foreign table has been corrupted. count=" + FOREIGN_COUNT + "size=" + hm.size());\r
-// rmTime += System.nanoTime()-start;\r
-// System.err.println("rmTime: " + 1e-9*rmTime+"s.");\r
- return hm;\r
- }\r
- short createForeign(int resourceKey, ClusterUID cuid) {\r
- int index = getTableCount();\r
- int size = ForeignElement.getSizeOf();\r
- int foreignIndex = createNewElement(size);\r
- int realIndex = checkIndexAndGetRealIndex(foreignIndex, size);\r
- short resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);\r
- //ClusterUID cuid = clusterTable.getClusterUIDByResourceKey(resourceKey);\r
- ResourceUID uid = cuid.toRID(resourceIndex);\r
- ForeignElement.constructForeign(getTable(), realIndex, uid);\r
- incForeignCount();\r
- return ClusterTraitsSmall.makeForeignRef((short)(index + ZERO_SHIFT));\r
- }\r
- short createForeign(int resourceKey) {\r
- int index = getTableCount();\r
- int size = ForeignElement.getSizeOf();\r
- int foreignIndex = createNewElement(size);\r
- int realIndex = checkIndexAndGetRealIndex(foreignIndex, size);\r
- short resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);\r
- ClusterI cluster = clusterTable.getClusterProxyByResourceKey(resourceKey);\r
- //ClusterI cluster = clusterTable.getClusterByResourceKey(resourceKey);\r
- ClusterUID cuid = cluster.getClusterUID();\r
- ResourceUID uid = cuid.toRID(resourceIndex);\r
- ForeignElement.constructForeign(getTable(), realIndex, uid);\r
- incForeignCount();\r
- return ClusterTraitsSmall.makeForeignRef((short)(index + ZERO_SHIFT));\r
- }\r
- void deleteForeign(int foreignIndex) {\r
- int realIndex = checkIndexAndGetRealIndex(foreignIndex);\r
- ForeignElement.destructForeign(getTable(), realIndex);\r
- decForeignCount();\r
- }\r
- public final ResourceUID getResourceUID(int foreignIndex) {\r
- return ForeignElement.getResourceUID(table, checkIndexAndGetRealIndex(foreignIndex)); \r
- }\r
- final void fillResourceUID(int foreignIndex, ClusterSmall cluster) {\r
- ForeignElement.fillResourceUID(table, checkIndexAndGetRealIndex(foreignIndex), cluster); \r
- }\r
- int getForeignCount() {\r
- return getExtra(FOREIGN_COUNT_INDEX);\r
- }\r
- private static final int FOREIGN_COUNT_INDEX = 0;\r
- private int incForeignCount() {\r
- int count = getExtra(FOREIGN_COUNT_INDEX) + 1;\r
- setExtra(FOREIGN_COUNT_INDEX, count);\r
- return count;\r
- }\r
- private int decForeignCount() {\r
- int count = getExtra(FOREIGN_COUNT_INDEX) - 1;\r
- setExtra(FOREIGN_COUNT_INDEX, count);\r
- return count;\r
- }\r
- private int checkIndexAndGetRealIndex(int foreignIndex) {\r
- int index = (foreignIndex - ZERO_SHIFT) * ForeignElement.getSizeOf() + ZERO_SHIFT;\r
- int realIndex = checkIndexAndGetRealIndex(index, ForeignElement.getSizeOf());\r
- return realIndex;\r
- }\r
- @Override\r
- public <Context> boolean foreach(int setIndex, Procedure procedure, Context context,\r
- ClusterSupport support, Modifier modifier) throws DatabaseException {\r
- throw new UnsupportedOperationException();\r
- }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.procore.cluster;
+
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.ClusterBase;
+import org.simantics.db.impl.ClusterI;
+import org.simantics.db.impl.ClusterI.Procedure;
+import org.simantics.db.impl.ClusterSupport;
+import org.simantics.db.impl.ClusterTraitsBase;
+import org.simantics.db.impl.IClusterTable;
+import org.simantics.db.impl.Modifier;
+import org.simantics.db.impl.Table;
+import org.simantics.db.impl.TableFactory;
+import org.simantics.db.service.ClusterUID;
+import org.simantics.db.service.ResourceUID;
+
+import gnu.trove.map.hash.TIntShortHashMap;
+
+final public class ForeignTableSmall extends Table<long[]>
+{
+ private IClusterTable clusterTable;
+ public ForeignTableSmall(ClusterBase sizeListener, int[] header, int headerBase) {
+ super(TableFactory.getLongFactory(), sizeListener, header, headerBase);
+ this.clusterTable = sizeListener.getClusterTable();
+ }
+ public ForeignTableSmall(ClusterBase sizeListener, int[] header, int headerBase, long[] longs) {
+ super(TableFactory.getLongFactory(), sizeListener, header, headerBase, longs);
+ this.clusterTable = sizeListener.getClusterTable();
+ }
+ public int getUsedSize() {
+ return getTableCount();
+ }
+ static long rmTime = 0;
+ public TIntShortHashMap getResourceHashMap()
+ throws DatabaseException {
+// long start = System.nanoTime();
+ int ELEMENT_SIZE = ForeignElement.getSizeOf();
+ assert(ELEMENT_SIZE == 3);
+ final int TABLE_SIZE = getTableCount();
+ final int FOREIGN_COUNT = getForeignCount();
+ final int BASE = this.getTableBase();
+ final int TOP = BASE + TABLE_SIZE * ELEMENT_SIZE;
+ long[] table = this.getTable();
+ int count = 0;
+ int index = ZERO_SHIFT;
+
+ long firstCache = 0;
+ long secondCache = 0;
+ int clusterKeyCache = 0;
+
+ TIntShortHashMap hm = new TIntShortHashMap(FOREIGN_COUNT);
+ for (int i=BASE; i<TOP && count<FOREIGN_COUNT;
+ i+=ELEMENT_SIZE, ++index) {
+
+ if (0 == table[i] && 0 == table[i+1] && 0 == table[i+2])
+ continue; // element has been deleted
+
+ long first = table[i];
+ long second = table[i+1];
+ long third = table[i+2];
+
+ int clusterKey = 0;
+ if(firstCache == first && secondCache == second) {
+ clusterKey = clusterKeyCache;
+ }
+ if(clusterKey == 0) {
+ clusterKey = clusterTable.getClusterKeyByUID(first, second);
+// cluster = clusterTable.getClusterByClusterUIDOrMakeProxy(ClusterUID.make(first, second));
+ clusterKeyCache = clusterKey;
+ firstCache = first;
+ secondCache =second;
+ }
+
+ int resourceKey = ClusterTraits.createResourceKey(clusterKey, (int)third);
+ hm.put(resourceKey, ClusterTraitsSmall.makeForeignRef((short)index));
+ ++count;
+
+ }
+ if (FOREIGN_COUNT != hm.size())
+ throw new DatabaseException("Foreign table has been corrupted. count=" + FOREIGN_COUNT + "size=" + hm.size());
+// rmTime += System.nanoTime()-start;
+// System.err.println("rmTime: " + 1e-9*rmTime+"s.");
+ return hm;
+ }
+ short createForeign(int resourceKey, ClusterUID cuid) {
+ int index = getTableCount();
+ int size = ForeignElement.getSizeOf();
+ int foreignIndex = createNewElement(size);
+ int realIndex = checkIndexAndGetRealIndex(foreignIndex, size);
+ short resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);
+ //ClusterUID cuid = clusterTable.getClusterUIDByResourceKey(resourceKey);
+ ResourceUID uid = cuid.toRID(resourceIndex);
+ ForeignElement.constructForeign(getTable(), realIndex, uid);
+ incForeignCount();
+ return ClusterTraitsSmall.makeForeignRef((short)(index + ZERO_SHIFT));
+ }
+ short createForeign(int resourceKey) {
+ int index = getTableCount();
+ int size = ForeignElement.getSizeOf();
+ int foreignIndex = createNewElement(size);
+ int realIndex = checkIndexAndGetRealIndex(foreignIndex, size);
+ short resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(resourceKey);
+ ClusterI cluster = clusterTable.getClusterProxyByResourceKey(resourceKey);
+ //ClusterI cluster = clusterTable.getClusterByResourceKey(resourceKey);
+ ClusterUID cuid = cluster.getClusterUID();
+ ResourceUID uid = cuid.toRID(resourceIndex);
+ ForeignElement.constructForeign(getTable(), realIndex, uid);
+ incForeignCount();
+ return ClusterTraitsSmall.makeForeignRef((short)(index + ZERO_SHIFT));
+ }
+ void deleteForeign(int foreignIndex) {
+ int realIndex = checkIndexAndGetRealIndex(foreignIndex);
+ ForeignElement.destructForeign(getTable(), realIndex);
+ decForeignCount();
+ }
+ public final ResourceUID getResourceUID(int foreignIndex) {
+ return ForeignElement.getResourceUID(table, checkIndexAndGetRealIndex(foreignIndex));
+ }
+ final void fillResourceUID(int foreignIndex, ClusterSmall cluster) {
+ ForeignElement.fillResourceUID(table, checkIndexAndGetRealIndex(foreignIndex), cluster);
+ }
+ int getForeignCount() {
+ return getExtra(FOREIGN_COUNT_INDEX);
+ }
+ private static final int FOREIGN_COUNT_INDEX = 0;
+ private int incForeignCount() {
+ int count = getExtra(FOREIGN_COUNT_INDEX) + 1;
+ setExtra(FOREIGN_COUNT_INDEX, count);
+ return count;
+ }
+ private int decForeignCount() {
+ int count = getExtra(FOREIGN_COUNT_INDEX) - 1;
+ setExtra(FOREIGN_COUNT_INDEX, count);
+ return count;
+ }
+ private int checkIndexAndGetRealIndex(int foreignIndex) {
+ int index = (foreignIndex - ZERO_SHIFT) * ForeignElement.getSizeOf() + ZERO_SHIFT;
+ int realIndex = checkIndexAndGetRealIndex(index, ForeignElement.getSizeOf());
+ return realIndex;
+ }
+ @Override
+ public <Context> boolean foreach(int setIndex, Procedure procedure, Context context,
+ ClusterSupport support, Modifier modifier) throws DatabaseException {
+ throw new UnsupportedOperationException();
+ }
+}