X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.procore%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fprocore%2Fcluster%2FClusterMapSmall.java;fp=bundles%2Forg.simantics.db.procore%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fprocore%2Fcluster%2FClusterMapSmall.java;h=c609560a7592ac1dea4631bb038af92c2f5c69d3;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ClusterMapSmall.java b/bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ClusterMapSmall.java new file mode 100644 index 000000000..c609560a7 --- /dev/null +++ b/bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ClusterMapSmall.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db.procore.cluster; + +import gnu.trove.map.hash.TIntShortHashMap; + +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.impl.TableSizeListener; +import org.simantics.db.service.ClusterUID; +import org.simantics.db.service.ResourceUID; + +public class ClusterMapSmall { + private TableSizeListener sizeListener; + private ForeignTableSmall foreignTable; + //private TObjectIntHashMap hashMap; + private TIntShortHashMap hashMap; + public ClusterMapSmall(TableSizeListener sizeListener, ForeignTableSmall foreignTable) { + this.sizeListener = sizeListener; + this.foreignTable = foreignTable; + hashMap = null; + } + public ResourceUID getForeignResourceUID(short reference) { + assert(reference >>> 15 > 0); + short foreignIndex = (short)(reference ^ 1<<15); + return foreignTable.getResourceUID(foreignIndex); + } + public void compact() { + if(hashMap == null) return; + hashMap.compact(); + sizeListener.resized(); + } + public int getUsedSpace() { + if(hashMap == null) return 0; + int size = hashMap.size(); + int cap = hashMap.capacity(); + // int[], Object[], actual objects + return cap*(4+ObjectSizes.POINTER_SIZE)+size*ObjectSizes.ResourceUIDSize; + } + + public short getForeignReferenceOrCreateByResourceKey(int resourceKey, ClusterUID uid) throws DatabaseException { + if (null == hashMap) + hashMap = foreignTable.getResourceHashMap(); + short foreignIndex = hashMap.get(resourceKey); + if (0 == foreignIndex) { + if (hashMap.size() >= ClusterTraitsSmall.FOREIGN_INDEX_MAX) + throw new OutOfSpaceException("Out of space for foreign resources=" + hashMap.size()); + foreignIndex = foreignTable.createForeign(resourceKey, uid); + hashMap.put(resourceKey, foreignIndex); + } + return foreignIndex; + } + + public short getForeignReferenceOrZero(int resourceKey) throws DatabaseException { + if (null == hashMap) + hashMap = foreignTable.getResourceHashMap(); + return hashMap.get(resourceKey); + } + + +}