]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ClusterMapSmall.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / cluster / ClusterMapSmall.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.procore.cluster;\r
13 \r
14 import gnu.trove.map.hash.TIntShortHashMap;\r
15 \r
16 import org.simantics.db.exception.DatabaseException;\r
17 import org.simantics.db.impl.TableSizeListener;\r
18 import org.simantics.db.service.ClusterUID;\r
19 import org.simantics.db.service.ResourceUID;\r
20 \r
21 public class ClusterMapSmall {\r
22     private TableSizeListener sizeListener;\r
23     private ForeignTableSmall foreignTable;\r
24     //private TObjectIntHashMap<ResourceUID> hashMap;\r
25     private TIntShortHashMap hashMap;\r
26     public ClusterMapSmall(TableSizeListener sizeListener, ForeignTableSmall foreignTable) {\r
27         this.sizeListener = sizeListener;\r
28         this.foreignTable = foreignTable;\r
29         hashMap = null;\r
30     }\r
31     public ResourceUID getForeignResourceUID(short reference) {\r
32         assert(reference >>> 15 > 0);\r
33         short foreignIndex = (short)(reference ^ 1<<15);\r
34         return foreignTable.getResourceUID(foreignIndex);\r
35     }\r
36     public void compact() {\r
37         if(hashMap == null) return;\r
38         hashMap.compact();\r
39         sizeListener.resized();\r
40     }\r
41     public int getUsedSpace() {\r
42         if(hashMap == null) return 0;\r
43         int size = hashMap.size();\r
44         int cap = hashMap.capacity();\r
45         // int[], Object[], actual objects\r
46         return cap*(4+ObjectSizes.POINTER_SIZE)+size*ObjectSizes.ResourceUIDSize;\r
47     }\r
48    \r
49     public short getForeignReferenceOrCreateByResourceKey(int resourceKey, ClusterUID uid) throws DatabaseException {\r
50         if (null == hashMap)\r
51             hashMap = foreignTable.getResourceHashMap();\r
52         short foreignIndex = hashMap.get(resourceKey);\r
53         if (0 == foreignIndex) {\r
54             if (hashMap.size() >= ClusterTraitsSmall.FOREIGN_INDEX_MAX)\r
55                 throw new OutOfSpaceException("Out of space for foreign resources=" + hashMap.size());\r
56             foreignIndex = foreignTable.createForeign(resourceKey, uid);\r
57             hashMap.put(resourceKey, foreignIndex);\r
58         }\r
59         return foreignIndex;\r
60     }\r
61 \r
62     public short getForeignReferenceOrZero(int resourceKey) throws DatabaseException {\r
63         if (null == hashMap)\r
64             hashMap = foreignTable.getResourceHashMap();\r
65         return hashMap.get(resourceKey);\r
66     }\r
67     \r
68     \r
69 }\r