]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
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 (file)
index 0000000..c609560
--- /dev/null
@@ -0,0 +1,69 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.procore.cluster;\r
+\r
+import gnu.trove.map.hash.TIntShortHashMap;\r
+\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.impl.TableSizeListener;\r
+import org.simantics.db.service.ClusterUID;\r
+import org.simantics.db.service.ResourceUID;\r
+\r
+public class ClusterMapSmall {\r
+    private TableSizeListener sizeListener;\r
+    private ForeignTableSmall foreignTable;\r
+    //private TObjectIntHashMap<ResourceUID> hashMap;\r
+    private TIntShortHashMap hashMap;\r
+    public ClusterMapSmall(TableSizeListener sizeListener, ForeignTableSmall foreignTable) {\r
+        this.sizeListener = sizeListener;\r
+        this.foreignTable = foreignTable;\r
+        hashMap = null;\r
+    }\r
+    public ResourceUID getForeignResourceUID(short reference) {\r
+        assert(reference >>> 15 > 0);\r
+        short foreignIndex = (short)(reference ^ 1<<15);\r
+        return foreignTable.getResourceUID(foreignIndex);\r
+    }\r
+    public void compact() {\r
+        if(hashMap == null) return;\r
+        hashMap.compact();\r
+        sizeListener.resized();\r
+    }\r
+    public int getUsedSpace() {\r
+        if(hashMap == null) return 0;\r
+        int size = hashMap.size();\r
+        int cap = hashMap.capacity();\r
+        // int[], Object[], actual objects\r
+        return cap*(4+ObjectSizes.POINTER_SIZE)+size*ObjectSizes.ResourceUIDSize;\r
+    }\r
+   \r
+    public short getForeignReferenceOrCreateByResourceKey(int resourceKey, ClusterUID uid) throws DatabaseException {\r
+        if (null == hashMap)\r
+            hashMap = foreignTable.getResourceHashMap();\r
+        short foreignIndex = hashMap.get(resourceKey);\r
+        if (0 == foreignIndex) {\r
+            if (hashMap.size() >= ClusterTraitsSmall.FOREIGN_INDEX_MAX)\r
+                throw new OutOfSpaceException("Out of space for foreign resources=" + hashMap.size());\r
+            foreignIndex = foreignTable.createForeign(resourceKey, uid);\r
+            hashMap.put(resourceKey, foreignIndex);\r
+        }\r
+        return foreignIndex;\r
+    }\r
+\r
+    public short getForeignReferenceOrZero(int resourceKey) throws DatabaseException {\r
+        if (null == hashMap)\r
+            hashMap = foreignTable.getResourceHashMap();\r
+        return hashMap.get(resourceKey);\r
+    }\r
+    \r
+    \r
+}\r