1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.procore.cluster;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.impl.ClusterI;
16 import org.simantics.db.impl.ClusterTraitsBase;
19 public final class ClusterTraits extends ClusterTraitsBase {
20 private static final int FOREIGN_INDEX_BITS = 16;
21 private static final int FOREIGN_INDEX_MAX = (1<<FOREIGN_INDEX_BITS)-1;
22 private static final int CLUSTER_INDEX_BITS = 16;
23 private static final int CLUSTER_INDEX_MAX = (1<<CLUSTER_INDEX_BITS)-1;
24 private static final int TYPE_BITS = 2;
25 private static final int NOTYPE_BITS = 32 - TYPE_BITS;
26 // private static final int CLUSTER_RESERVED_BITS = 15;
27 // private static final long CLUSTER_RESERVED_BITS2ZERO = ((long)1 << CLUSTER_RESERVED_BITS) - 1;
28 public static final int createForeignReference(int foreignIndex, int resourceIndex)
29 throws DatabaseException {
30 if (foreignIndex < 1 || foreignIndex > FOREIGN_INDEX_MAX)
31 throw new DatabaseException("Illegal foreign index " + foreignIndex);
32 if (isIllegalResourceIndex(resourceIndex))
33 throw new DatabaseException("Illegal resource index " + resourceIndex);
34 return (resourceIndex<<FOREIGN_INDEX_BITS | foreignIndex)| 0x80000000;
36 public static int referenceGetType(int ref) {
37 return ref >>> NOTYPE_BITS;
39 public static final boolean isFlat(int reference) {
40 return reference < 0 && (reference & 0x40000000) != 0;
42 public static final boolean isLocal(int reference) {
43 return (reference & 0xC0000000) == 0;
45 public static final int getForeignIndexFromReference(int reference)
46 throws DatabaseException {
47 int foreignIndex = reference & 0x0000FFFF;
48 if (0 == foreignIndex)
49 throw new DatabaseException("Illegal foreign index " + foreignIndex);
52 public static final int getResourceIndexFromForeignReference(int reference)
53 throws DatabaseException {
54 int resourceIndex = (reference & 0x7FFFFFFF) >> FOREIGN_INDEX_BITS;
55 if (isIllegalResourceIndex(resourceIndex))
56 throw new DatabaseException("Illegal foreign reference " + reference);
59 public static final boolean statementIndexIsDirect(int index) {
60 return (index & 0xC0000000) != 0x40000000;
62 public static final int statementIndexMake(int index)
63 throws DatabaseException {
65 if ((index & 0xC0000000) != 0)
66 throw new DatabaseException("Illegal statement index " + index);
67 return index | 0x40000000;
69 public static final int statementIndexGet(int index)
70 throws DatabaseException {
71 if ((index & 0xC0000000) == 0x40000000)
72 return index & 0x3FFFFFFF;
76 public static boolean completeReferenceIsLocal(int completeRef) {
77 boolean complete = (completeRef & 0xC0000000) != 0;
78 return complete && ((completeRef & 0x0000FFFF) == 0);
80 public static boolean completeReferenceIsMultiple(int completeRef) {
81 boolean complete = (completeRef & 0xC0000000) != 0;
82 return !complete && (completeRef != 0);
84 public static int completeReferenceGetResourceIndex(int completeRef) {
85 int low = (completeRef & 0x3FFFFFFF) >>> CLUSTER_INDEX_BITS;
86 return low & 0x00003FFFF;
88 public static int completeReferenceGetForeignIndex(int completeRef) {
89 return completeRef & 0x0000FFFF;
91 public static ClusterI.CompleteTypeEnum completeReferenceGetType(int completeRef) {
92 return ClusterI.CompleteTypeEnum.make(completeRef >>> NOTYPE_BITS);
94 public static int completeReferenceMake(int type, int resourceIndex, int clusterIndex) {
95 assert(type < (1<<TYPE_BITS));
96 assert(!isIllegalResourceIndex(resourceIndex));
97 assert(clusterIndex <= CLUSTER_INDEX_MAX);
98 int ref = (type << NOTYPE_BITS) | (resourceIndex << CLUSTER_INDEX_BITS) | clusterIndex;
101 public static int completeReferenceMake(int type, int setIndex) {
102 assert(type < (1<<TYPE_BITS));
103 assert(setIndex < (1<<NOTYPE_BITS));
104 int ref = (type << NOTYPE_BITS) | setIndex;