]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/ClusterTraitsBase.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / ClusterTraitsBase.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.impl;\r
13 \r
14 import org.simantics.db.exception.DatabaseException;\r
15 \r
16 public class ClusterTraitsBase {\r
17     private static final int RK_TYPE_BITS = 1; // bits 31-31, 0=database 1=virtual\r
18     private static final int RK_CLUSTER_BITS = 19; // bits 30-12\r
19     private static final int RK_CLUSTER_MAX = (1<<RK_CLUSTER_BITS)-1;\r
20     private static final int RK_INDEX_BITS = 12; // bits 11-0, index zero reserved (not a legal resource index)\r
21     private static final int RK_INDEX_MAX = (1<<RK_INDEX_BITS)-1;\r
22     private static final int RID_TYPE_BITS = RK_TYPE_BITS; // bits 63-63, 0=database 1= virtual\r
23     private static final int RID_CLUSTER_BITS = 64-RID_TYPE_BITS-RK_INDEX_BITS; // bits 62-12\r
24     private static final int RID_CLUSTER_MAX = (1<<RID_CLUSTER_BITS)-1;\r
25     private static final int RID_INDEX_BITS = RK_INDEX_BITS;\r
26     private static final int RID_INDEX_MAX = (1<<RID_INDEX_BITS)-1;\r
27 //    private static final int RESOURCE_INDEX_BITS = 14; // resource index bits in cluster\r
28 //    private static final int RESOURCE_INDEX_MAX = (1<<RESOURCE_INDEX_BITS)-1;\r
29     public static final boolean isIllegalResourceIndex(int resourceIndex) {\r
30         return resourceIndex < 1 || resourceIndex > RK_INDEX_MAX;\r
31     }\r
32     public static final short getResourceIndexFromResourceKey(int resourceKey) throws DatabaseException {\r
33         short resourceIndex = (short)(resourceKey & RK_INDEX_MAX);\r
34         if (isIllegalResourceIndex(resourceIndex))\r
35             throw new DatabaseException("Illegal resource key " + resourceKey);\r
36         return resourceIndex;\r
37     }\r
38     public static final short getResourceIndexFromResourceKeyNoThrow(int resourceKey) {\r
39         return (short)(resourceKey & RK_INDEX_MAX);\r
40     }\r
41     public static final int getClusterKeyFromResourceKey(int resourceKey)\r
42     throws DatabaseException {\r
43         int clusterKey = resourceKey >>> RK_INDEX_BITS;\r
44         if (clusterKey < 1 || clusterKey >= getClusterArraySize())\r
45             throw new DatabaseException("Illegal cluster key for resource key=" + resourceKey);\r
46         return clusterKey;\r
47     }\r
48     public static final int getClusterKeyFromResourceKeyNoThrow(int resourceKey) {\r
49         return resourceKey >>> RK_INDEX_BITS;\r
50     }\r
51     public static final boolean isVirtualClusterKey(int clusterKey) {\r
52         return (clusterKey & ~RK_CLUSTER_MAX) != 0; \r
53     }\r
54     // This requires that builtin cluster has cluster index 1 and that IIS entities\r
55     // are given the first three indexes within that cluster.\r
56     public static final int getCompleteTypeIntFromResourceKey(int resourceKey) {\r
57         if ((resourceKey & 0xFFFFEFFC) != 0)\r
58             return 0; // not complete\r
59         return resourceKey & 3;\r
60     }\r
61     public static final ClusterI.CompleteTypeEnum getCompleteTypeFromResourceKey(int resourceKey) {\r
62         return ClusterI.CompleteTypeEnum.make(getCompleteTypeIntFromResourceKey(resourceKey));\r
63     }\r
64     public static final int getCompleteTypeResourceKeyFromEnum(ClusterI.CompleteTypeEnum completeType)\r
65     throws DatabaseException {\r
66         switch (completeType) {\r
67             default: throw new DatabaseException("Illegal compete type for getCompletePredicateKey: " + completeType);\r
68             case InstanceOf: return 4097;\r
69             case Inherits: return 4098; \r
70             case SubrelationOf: return 4099;\r
71         }\r
72     }\r
73     public static final int createResourceKey(int clusterIndex, int resourceIndex)\r
74     throws DatabaseException {\r
75 // Do not use assert because this method is called with the assumption that it\r
76 // will throw exception if arguments are not correct. If you really can't afford\r
77 // the few if tests then use the NoThrow version of this method.\r
78         if (clusterIndex < 0 || clusterIndex > RK_CLUSTER_MAX)\r
79             throw new DatabaseException("Illegal cluster index " + clusterIndex);\r
80         if (isIllegalResourceIndex(resourceIndex))\r
81             throw new DatabaseException("Illegal resource index " + resourceIndex);\r
82         return createResourceKeyNoThrow(clusterIndex, resourceIndex);\r
83     }\r
84     public static final int createResourceKeyNoThrow(int clusterIndex, int resourceIndex){\r
85         return (clusterIndex << RK_INDEX_BITS | resourceIndex);\r
86     }\r
87     public static final int getClusterBits(int clusterKey) {\r
88         return clusterKey << RK_INDEX_BITS;\r
89     }\r
90     public static final boolean isCluster(int clusterKeyBits, int resourceKey) {\r
91 //        return ((resourceKey ^ clusterKeyBits) & RK_INDEX_MAX) == 0;\r
92         return clusterKeyBits == (resourceKey & ~RK_INDEX_MAX);\r
93 \r
94     }\r
95     public static final int getClusterMaskFromResourceKey(int resourceKey) {\r
96         return resourceKey & ~RK_INDEX_MAX;\r
97     }\r
98     public static final int getMaxNumberOfResources() {\r
99         return RK_INDEX_MAX;\r
100     }\r
101     public static final long createResourceId(long clusterId, int resourceIndex)\r
102     throws DatabaseException {\r
103         if (clusterId < 1 || (clusterId > RID_CLUSTER_MAX))\r
104             throw new DatabaseException("Illegal cluster id " + clusterId);\r
105         if (isIllegalResourceIndex(resourceIndex))\r
106             throw new DatabaseException("Illegal resource index " + resourceIndex);\r
107         return createResourceIdNoThrow(clusterId, resourceIndex);\r
108     }\r
109     public static final long createResourceIdNoThrow(long clusterId, int resourceIndex) {\r
110         return (clusterId<<RID_INDEX_BITS | resourceIndex);\r
111     }\r
112     public static final long getClusterIdFromResourceId(long resourceId) {\r
113         return resourceId >>> RID_INDEX_BITS;\r
114     }\r
115     public static final int getResourceIndexFromResourceId(long resourceId) {\r
116         return (int)(resourceId & RID_INDEX_MAX);\r
117     }\r
118     // Max number of resident clusters per session.\r
119     public static final int getClusterArraySize() {\r
120         return Math.min(RK_CLUSTER_MAX, 1<<15);\r
121     }\r
122 }