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