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