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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.procore.cluster;
\r
14 import gnu.trove.map.hash.TObjectIntHashMap;
\r
16 import org.simantics.db.exception.DatabaseException;
\r
17 import org.simantics.db.impl.ClusterI.Procedure;
\r
18 import org.simantics.db.impl.ClusterSupport;
\r
19 import org.simantics.db.impl.Modifier;
\r
20 import org.simantics.db.impl.Table;
\r
21 import org.simantics.db.impl.TableFactory;
\r
22 import org.simantics.db.impl.TableSizeListener;
\r
23 import org.simantics.db.service.ClusterUID;
\r
24 import org.simantics.db.service.ResourceUID;
\r
26 final class ForeignElement {
\r
27 private static final int CLUSTER_OFFSET = 0; // resourceUID
\r
28 private static final int SIZE_OF = 3;
\r
30 static void constructForeign(long[] table, int index, ResourceUID resourceUID) {
\r
31 int i = CLUSTER_OFFSET + index;
\r
32 resourceUID.toLong(table, i);
\r
34 static void destructForeign(long[] table, int index) {
\r
35 int i = CLUSTER_OFFSET + index;
\r
40 static int getSizeOf() {
\r
43 static ResourceUID getResourceUID(long[] table, int index) {
\r
44 int i = CLUSTER_OFFSET + index;
\r
45 return new ResourceUID(table, i);
\r
47 static void fillResourceUID(long[] table, int index, ClusterSmall cluster) {
\r
48 int i = CLUSTER_OFFSET + index;
\r
49 cluster.clusterUID1 = table[i];
\r
50 cluster.clusterUID2 = table[i+1];
\r
51 cluster.executeIndex = (short)table[i+2];
\r
55 public final class ForeignTable extends Table<long[]>
\r
57 public ForeignTable(TableSizeListener sizeListener, int[] header, int headerBase) {
\r
58 super(TableFactory.getLongFactory(), sizeListener, header, headerBase);
\r
60 public ForeignTable(TableSizeListener sizeListener, int[] header, int headerBase, long[] longs) {
\r
61 super(TableFactory.getLongFactory(), sizeListener, header, headerBase, longs);
\r
63 public int getUsedSize() {
\r
64 return getTableCount();
\r
66 TObjectIntHashMap<ClusterUID> getHashMap()
\r
67 throws DatabaseException {
\r
68 int ELEMENT_SIZE = ForeignElement.getSizeOf();
\r
69 final int TABLE_SIZE = getTableCount();
\r
70 final int FOREIGN_COUNT = getForeignCount();
\r
71 final int BASE = this.getTableBase();
\r
72 final int TOP = BASE + TABLE_SIZE * ELEMENT_SIZE;
\r
73 long[] table = this.getTable();
\r
75 int index = ZERO_SHIFT;
\r
76 TObjectIntHashMap<ClusterUID> hm = new TObjectIntHashMap<ClusterUID>(FOREIGN_COUNT);
\r
77 for (int i=BASE; i<TOP && count<FOREIGN_COUNT;
\r
78 i+=ELEMENT_SIZE, ++index) {
\r
79 if (0 == table[i] && 0 == table[i+1] && 0 == table[i+2])
\r
80 continue; // element has been deleted
\r
81 hm.put(ClusterUID.make(table[i], table[i+1]), index);
\r
84 if (FOREIGN_COUNT != hm.size())
\r
85 throw new DatabaseException("Foreign table has been corrupted.");
\r
88 TObjectIntHashMap<ResourceUID> getResourceHashMap()
\r
89 throws DatabaseException {
\r
90 int ELEMENT_SIZE = ForeignElement.getSizeOf();
\r
91 assert(ELEMENT_SIZE == 3);
\r
92 final int TABLE_SIZE = getTableCount();
\r
93 final int FOREIGN_COUNT = getForeignCount();
\r
94 final int BASE = this.getTableBase();
\r
95 final int TOP = BASE + TABLE_SIZE * ELEMENT_SIZE;
\r
96 long[] table = this.getTable();
\r
98 int index = ZERO_SHIFT;
\r
99 TObjectIntHashMap<ResourceUID> hm = new TObjectIntHashMap<ResourceUID>(FOREIGN_COUNT);
\r
100 for (int i=BASE; i<TOP && count<FOREIGN_COUNT;
\r
101 i+=ELEMENT_SIZE, ++index) {
\r
102 if (0 == table[i] && 0 == table[i+1] && 0 == table[i+2])
\r
103 continue; // element has been deleted
\r
104 hm.put(new ResourceUID(table[i], table[i+1], table[i+2]), index);
\r
107 if (FOREIGN_COUNT != hm.size())
\r
108 throw new DatabaseException("Foreign table has been corrupted. count=" + FOREIGN_COUNT + "size=" + hm.size());
\r
111 int createForeign(ResourceUID uid) {
\r
112 int index = getTableCount();
\r
113 int size = ForeignElement.getSizeOf();
\r
114 int foreignIndex = createNewElement(size);
\r
115 int realIndex = checkIndexAndGetRealIndex(foreignIndex, size);
\r
116 ForeignElement.constructForeign(getTable(), realIndex, uid);
\r
118 return index + ZERO_SHIFT;
\r
120 void deleteForeign(int foreignIndex) {
\r
121 int realIndex = checkIndexAndGetRealIndex(foreignIndex);
\r
122 ForeignElement.destructForeign(getTable(), realIndex);
\r
125 public final ResourceUID getResourceUID(int foreignIndex) {
\r
126 return ForeignElement.getResourceUID(table, checkIndexAndGetRealIndex(foreignIndex));
\r
128 int getForeignCount() {
\r
129 return getExtra(FOREIGN_COUNT_INDEX);
\r
131 private static final int FOREIGN_COUNT_INDEX = 0;
\r
132 private int incForeignCount() {
\r
133 int count = getExtra(FOREIGN_COUNT_INDEX) + 1;
\r
134 setExtra(FOREIGN_COUNT_INDEX, count);
\r
137 private int decForeignCount() {
\r
138 int count = getExtra(FOREIGN_COUNT_INDEX) - 1;
\r
139 setExtra(FOREIGN_COUNT_INDEX, count);
\r
142 private int checkIndexAndGetRealIndex(int foreignIndex) {
\r
143 int index = (foreignIndex - ZERO_SHIFT) * ForeignElement.getSizeOf() + ZERO_SHIFT;
\r
144 int realIndex = checkIndexAndGetRealIndex(index, ForeignElement.getSizeOf());
\r
148 public <Context> boolean foreach(int setIndex, Procedure procedure, Context context,
\r
149 ClusterSupport support, Modifier modifier) throws DatabaseException {
\r
150 throw new UnsupportedOperationException();
\r