--- /dev/null
+/*******************************************************************************\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.impl;\r
+\r
+import org.simantics.db.exception.DatabaseException;\r
+\r
+public class ClusterTraitsBase {\r
+ private static final int RK_TYPE_BITS = 1; // bits 31-31, 0=database 1=virtual\r
+ private static final int RK_CLUSTER_BITS = 19; // bits 30-12\r
+ private static final int RK_CLUSTER_MAX = (1<<RK_CLUSTER_BITS)-1;\r
+ private static final int RK_INDEX_BITS = 12; // bits 11-0, index zero reserved (not a legal resource index)\r
+ private static final int RK_INDEX_MAX = (1<<RK_INDEX_BITS)-1;\r
+ private static final int RID_TYPE_BITS = RK_TYPE_BITS; // bits 63-63, 0=database 1= virtual\r
+ private static final int RID_CLUSTER_BITS = 64-RID_TYPE_BITS-RK_INDEX_BITS; // bits 62-12\r
+ private static final int RID_CLUSTER_MAX = (1<<RID_CLUSTER_BITS)-1;\r
+ private static final int RID_INDEX_BITS = RK_INDEX_BITS;\r
+ private static final int RID_INDEX_MAX = (1<<RID_INDEX_BITS)-1;\r
+// private static final int RESOURCE_INDEX_BITS = 14; // resource index bits in cluster\r
+// private static final int RESOURCE_INDEX_MAX = (1<<RESOURCE_INDEX_BITS)-1;\r
+ public static final boolean isIllegalResourceIndex(int resourceIndex) {\r
+ return resourceIndex < 1 || resourceIndex > RK_INDEX_MAX;\r
+ }\r
+ public static final short getResourceIndexFromResourceKey(int resourceKey) throws DatabaseException {\r
+ short resourceIndex = (short)(resourceKey & RK_INDEX_MAX);\r
+ if (isIllegalResourceIndex(resourceIndex))\r
+ throw new DatabaseException("Illegal resource key " + resourceKey);\r
+ return resourceIndex;\r
+ }\r
+ public static final short getResourceIndexFromResourceKeyNoThrow(int resourceKey) {\r
+ return (short)(resourceKey & RK_INDEX_MAX);\r
+ }\r
+ public static final int getClusterKeyFromResourceKey(int resourceKey)\r
+ throws DatabaseException {\r
+ int clusterKey = resourceKey >>> RK_INDEX_BITS;\r
+ if (clusterKey < 1 || clusterKey >= getClusterArraySize())\r
+ throw new DatabaseException("Illegal cluster key for resource key=" + resourceKey);\r
+ return clusterKey;\r
+ }\r
+ public static final int getClusterKeyFromResourceKeyNoThrow(int resourceKey) {\r
+ return resourceKey >>> RK_INDEX_BITS;\r
+ }\r
+ public static final boolean isVirtualClusterKey(int clusterKey) {\r
+ return (clusterKey & ~RK_CLUSTER_MAX) != 0; \r
+ }\r
+ // This requires that builtin cluster has cluster index 1 and that IIS entities\r
+ // are given the first three indexes within that cluster.\r
+ public static final int getCompleteTypeIntFromResourceKey(int resourceKey) {\r
+ if ((resourceKey & 0xFFFFEFFC) != 0)\r
+ return 0; // not complete\r
+ return resourceKey & 3;\r
+ }\r
+ public static final ClusterI.CompleteTypeEnum getCompleteTypeFromResourceKey(int resourceKey) {\r
+ return ClusterI.CompleteTypeEnum.make(getCompleteTypeIntFromResourceKey(resourceKey));\r
+ }\r
+ public static final int getCompleteTypeResourceKeyFromEnum(ClusterI.CompleteTypeEnum completeType)\r
+ throws DatabaseException {\r
+ switch (completeType) {\r
+ default: throw new DatabaseException("Illegal compete type for getCompletePredicateKey: " + completeType);\r
+ case InstanceOf: return 4097;\r
+ case Inherits: return 4098; \r
+ case SubrelationOf: return 4099;\r
+ }\r
+ }\r
+ public static final int createResourceKey(int clusterIndex, int resourceIndex)\r
+ throws DatabaseException {\r
+// Do not use assert because this method is called with the assumption that it\r
+// will throw exception if arguments are not correct. If you really can't afford\r
+// the few if tests then use the NoThrow version of this method.\r
+ if (clusterIndex < 0 || clusterIndex > RK_CLUSTER_MAX)\r
+ throw new DatabaseException("Illegal cluster index " + clusterIndex);\r
+ if (isIllegalResourceIndex(resourceIndex))\r
+ throw new DatabaseException("Illegal resource index " + resourceIndex);\r
+ return createResourceKeyNoThrow(clusterIndex, resourceIndex);\r
+ }\r
+ public static final int createResourceKeyNoThrow(int clusterIndex, int resourceIndex){\r
+ return (clusterIndex << RK_INDEX_BITS | resourceIndex);\r
+ }\r
+ public static final int getClusterBits(int clusterKey) {\r
+ return clusterKey << RK_INDEX_BITS;\r
+ }\r
+ public static final boolean isCluster(int clusterKeyBits, int resourceKey) {\r
+// return ((resourceKey ^ clusterKeyBits) & RK_INDEX_MAX) == 0;\r
+ return clusterKeyBits == (resourceKey & ~RK_INDEX_MAX);\r
+\r
+ }\r
+ public static final int getClusterMaskFromResourceKey(int resourceKey) {\r
+ return resourceKey & ~RK_INDEX_MAX;\r
+ }\r
+ public static final int getMaxNumberOfResources() {\r
+ return RK_INDEX_MAX;\r
+ }\r
+ public static final long createResourceId(long clusterId, int resourceIndex)\r
+ throws DatabaseException {\r
+ if (clusterId < 1 || (clusterId > RID_CLUSTER_MAX))\r
+ throw new DatabaseException("Illegal cluster id " + clusterId);\r
+ if (isIllegalResourceIndex(resourceIndex))\r
+ throw new DatabaseException("Illegal resource index " + resourceIndex);\r
+ return createResourceIdNoThrow(clusterId, resourceIndex);\r
+ }\r
+ public static final long createResourceIdNoThrow(long clusterId, int resourceIndex) {\r
+ return (clusterId<<RID_INDEX_BITS | resourceIndex);\r
+ }\r
+ public static final long getClusterIdFromResourceId(long resourceId) {\r
+ return resourceId >>> RID_INDEX_BITS;\r
+ }\r
+ public static final int getResourceIndexFromResourceId(long resourceId) {\r
+ return (int)(resourceId & RID_INDEX_MAX);\r
+ }\r
+ // Max number of resident clusters per session.\r
+ public static final int getClusterArraySize() {\r
+ return Math.min(RK_CLUSTER_MAX, 1<<15);\r
+ }\r
+}
\ No newline at end of file