]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ClusterMap.java
Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / cluster / ClusterMap.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.ClusterTraitsBase;
18 import org.simantics.db.service.ClusterUID;
19 import org.simantics.db.service.ResourceUID;
20
21 public class ClusterMap {
22     private ForeignTable foreignTable;
23     //private FlatTable flatTable;
24     private TObjectIntHashMap<ClusterUID> hashMap;
25     public ClusterMap(ForeignTable foreignTable, FlatTable flatTable) {
26         this.foreignTable = foreignTable;
27         //this.flatTable = flatTable;
28         hashMap = null;
29     }
30     public void compact() {
31         if(hashMap == null)
32             return;
33         hashMap.compact();
34     }
35     /*
36      * 13 bytes for each element. Approx capacity = 3 * size
37      */
38     public int getUsedSpace() {
39         if(hashMap == null)
40             return 0;
41         return 3 * 13 * hashMap.size();
42     }
43     public int getForeignResourceIndex(int reference)
44     throws DatabaseException {
45         if (ClusterTraits.isFlat(reference))
46             throw new DatabaseException("Flat reference is not supported yet.");
47         else {
48             return ClusterTraits.getResourceIndexFromForeignReference(reference);
49         }
50     }
51     public final ResourceUID getResourceUID(int reference) throws DatabaseException {
52         if (ClusterTraits.isFlat(reference))
53             throw new DatabaseException("Flat reference is not supported yet. reference=" + reference);
54         else {
55             int foreignIndex = ClusterTraits.getForeignIndexFromReference(reference);
56             return foreignTable.getResourceUID(foreignIndex);
57         }
58     }
59     public int getForeignReferenceOrCreateByResourceKey(int resourceKey, ClusterUID clusterUID)
60     throws DatabaseException {
61         int resourceIndex = ClusterTraits.getResourceIndexFromResourceKey(resourceKey);
62         return getForeignReferenceOrCreateByResourceIndex(resourceIndex, clusterUID);
63     }
64     public int getForeignReferenceOrCreateByResourceIndex(int resourceIndex, ClusterUID clusterUID)
65     throws DatabaseException {
66         if (ClusterTraitsBase.isIllegalResourceIndex(resourceIndex))
67             throw new DatabaseException("Illegal resource index=" + resourceIndex + ".");
68         if (null == hashMap)
69             hashMap = foreignTable.getHashMap();
70         int foreignIndex = hashMap.get(clusterUID);
71         if (0 == foreignIndex) {
72             foreignIndex = foreignTable.createForeign(clusterUID.toRID(resourceIndex));
73             hashMap.put(clusterUID, foreignIndex);
74         }
75         return ClusterTraits.createForeignReference(foreignIndex, resourceIndex); 
76     }
77     public int getForeignReferenceOrZero(int resourceIndex, ClusterUID clusterUID)
78     throws DatabaseException {
79         // This is guaranteed by implementation
80 //        if(clusterId == 1) return (resourceIndex<<16) + 0x80000001;
81         //return ClusterTraits.createForeignReference(1, resourceIndex); 
82 //        new Exception("getForeignReferenceOrZero " + clusterId).printStackTrace();
83         if (null == hashMap)
84             hashMap = foreignTable.getHashMap();
85         if (ClusterUID.Null.equals(clusterUID))
86             throw new DatabaseException("Illegal cluster uid " + clusterUID);
87         if (0 == resourceIndex)
88             throw new DatabaseException("Illegal resource index " + resourceIndex);
89         int foreignIndex = hashMap.get(clusterUID);
90         if (0 == foreignIndex)
91             return 0;
92         //System.err.println("foreignIndex for " + clusterId + " = " + foreignIndex);
93         return ClusterTraits.createForeignReference(foreignIndex, resourceIndex); 
94     }
95 }