1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.procore.cluster;
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;
27 import gnu.trove.map.hash.TIntShortHashMap;
29 final public class ForeignTableSmall extends Table<long[]>
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();
36 public ForeignTableSmall(ClusterBase sizeListener, int[] header, int headerBase, long[] longs) {
37 super(TableFactory.getLongFactory(), sizeListener, header, headerBase, longs);
38 this.clusterTable = sizeListener.getClusterTable();
40 public int getUsedSize() {
41 return getTableCount();
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();
55 int index = ZERO_SHIFT;
59 int clusterKeyCache = 0;
61 TIntShortHashMap hm = new TIntShortHashMap(FOREIGN_COUNT);
62 for (int i=BASE; i<TOP && count<FOREIGN_COUNT;
63 i+=ELEMENT_SIZE, ++index) {
65 if (0 == table[i] && 0 == table[i+1] && 0 == table[i+2])
66 continue; // element has been deleted
68 long first = table[i];
69 long second = table[i+1];
70 long third = table[i+2];
73 if(firstCache == first && secondCache == second) {
74 clusterKey = clusterKeyCache;
77 clusterKey = clusterTable.getClusterKeyByUID(first, second);
78 // cluster = clusterTable.getClusterByClusterUIDOrMakeProxy(ClusterUID.make(first, second));
79 clusterKeyCache = clusterKey;
84 int resourceKey = ClusterTraits.createResourceKey(clusterKey, (int)third);
85 hm.put(resourceKey, ClusterTraitsSmall.makeForeignRef((short)index));
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.");
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);
105 return ClusterTraitsSmall.makeForeignRef((short)(index + ZERO_SHIFT));
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);
119 return ClusterTraitsSmall.makeForeignRef((short)(index + ZERO_SHIFT));
121 void deleteForeign(int foreignIndex) {
122 int realIndex = checkIndexAndGetRealIndex(foreignIndex);
123 ForeignElement.destructForeign(getTable(), realIndex);
126 public final ResourceUID getResourceUID(int foreignIndex) {
127 return ForeignElement.getResourceUID(table, checkIndexAndGetRealIndex(foreignIndex));
129 final void fillResourceUID(int foreignIndex, ClusterSmall cluster) {
130 ForeignElement.fillResourceUID(table, checkIndexAndGetRealIndex(foreignIndex), cluster);
132 int getForeignCount() {
133 return getExtra(FOREIGN_COUNT_INDEX);
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);
141 private int decForeignCount() {
142 int count = getExtra(FOREIGN_COUNT_INDEX) - 1;
143 setExtra(FOREIGN_COUNT_INDEX, count);
146 private int checkIndexAndGetRealIndex(int foreignIndex) {
147 int index = (foreignIndex - ZERO_SHIFT) * ForeignElement.getSizeOf() + ZERO_SHIFT;
148 int realIndex = checkIndexAndGetRealIndex(index, ForeignElement.getSizeOf());
152 public <Context> boolean foreach(int setIndex, Procedure procedure, Context context,
153 ClusterSupport support, Modifier modifier) throws DatabaseException {
154 throw new UnsupportedOperationException();