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 gnu.trove.map.hash.TObjectIntHashMap;
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;
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;
30 public void compact() {
36 * 13 bytes for each element. Approx capacity = 3 * size
38 public int getUsedSpace() {
41 return 3 * 13 * hashMap.size();
43 public int getForeignResourceIndex(int reference)
44 throws DatabaseException {
45 if (ClusterTraits.isFlat(reference))
46 throw new DatabaseException("Flat reference is not supported yet.");
48 return ClusterTraits.getResourceIndexFromForeignReference(reference);
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);
55 int foreignIndex = ClusterTraits.getForeignIndexFromReference(reference);
56 return foreignTable.getResourceUID(foreignIndex);
59 public int getForeignReferenceOrCreateByResourceKey(int resourceKey, ClusterUID clusterUID)
60 throws DatabaseException {
61 int resourceIndex = ClusterTraits.getResourceIndexFromResourceKey(resourceKey);
62 return getForeignReferenceOrCreateByResourceIndex(resourceIndex, clusterUID);
64 public int getForeignReferenceOrCreateByResourceIndex(int resourceIndex, ClusterUID clusterUID)
65 throws DatabaseException {
66 if (ClusterTraitsBase.isIllegalResourceIndex(resourceIndex))
67 throw new DatabaseException("Illegal resource index=" + resourceIndex + ".");
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);
75 return ClusterTraits.createForeignReference(foreignIndex, resourceIndex);
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();
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)
92 //System.err.println("foreignIndex for " + clusterId + " = " + foreignIndex);
93 return ClusterTraits.createForeignReference(foreignIndex, resourceIndex);