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