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.TIntShortHashMap;
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;
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;
31 public ResourceUID getForeignResourceUID(short reference) {
32 assert(reference >>> 15 > 0);
33 short foreignIndex = (short)(reference ^ 1<<15);
34 return foreignTable.getResourceUID(foreignIndex);
36 public void compact() {
37 if(hashMap == null) return;
39 sizeListener.resized();
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;
49 public short getForeignReferenceOrCreateByResourceKey(int resourceKey, ClusterUID uid) throws DatabaseException {
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);
62 public short getForeignReferenceOrZero(int resourceKey) throws DatabaseException {
64 hashMap = foreignTable.getResourceHashMap();
65 return hashMap.get(resourceKey);