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