]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ClusterImpl.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / cluster / ClusterImpl.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.procore.cluster;\r
13 \r
14 import java.util.UUID;\r
15 \r
16 import org.simantics.db.common.utils.Logger;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 import org.simantics.db.exception.InvalidClusterException;\r
19 import org.simantics.db.impl.ClusterBase;\r
20 import org.simantics.db.impl.ClusterSupport;\r
21 import org.simantics.db.impl.IClusterTable;\r
22 import org.simantics.db.impl.Modifier;\r
23 import org.simantics.db.service.ClusterCollectorPolicy.CollectorCluster;\r
24 import org.simantics.db.service.ClusterUID;\r
25 import org.simantics.db.service.ClusteringSupport.Id;\r
26 \r
27 import fi.vtt.simantics.procore.internal.Change;\r
28 import fi.vtt.simantics.procore.internal.ClusterChange;\r
29 import fi.vtt.simantics.procore.internal.ClusterTable;\r
30 import fi.vtt.simantics.procore.internal.SessionImplSocket;\r
31 \r
32 public abstract class ClusterImpl extends ClusterBase implements Modifier, CollectorCluster {\r
33     private static final int LONG_HEADER_SIZE = 7;\r
34     private static final long LONG_HEADER_VERSION = 1;\r
35     protected static ClusterUID checkValidity(long type, long[] longs, int[] ints, byte[] bytes)\r
36     throws InvalidClusterException {\r
37         if (longs.length < LONG_HEADER_SIZE)\r
38             throw new InvalidClusterException("Header size mismatch. Expected=" + ClusterImpl.LONG_HEADER_SIZE + ", got=" + longs.length);\r
39         if (longs[0] != type)\r
40             throw new InvalidClusterException("Type mismatch. Expected=" + type + ", got=" + longs[0]);\r
41         if (longs[1] != ClusterImpl.LONG_HEADER_VERSION)\r
42             throw new InvalidClusterException("Header size mismatch. Expected=" + ClusterImpl.LONG_HEADER_VERSION + ", got=" + longs[1]);\r
43         return ClusterUID.make(longs[2], longs[3]);\r
44     }\r
45     protected static Id getUniqueId(long[] longs) {\r
46         return new IdImpl(new UUID(longs[3], longs[4]));\r
47     }\r
48     static final boolean DEBUG = false;\r
49     // This can be null iff the cluster has been converted to big\r
50     public Change change = new Change();\r
51     public ClusterChange cc;\r
52     public byte[] foreignLookup;\r
53     \r
54     private boolean dirtySizeInBytes = true;\r
55     private long sizeInBytes = 0;\r
56     \r
57     protected ClusterTable clusterTable;\r
58     \r
59     public ClusterImpl(ClusterUID clusterUID, int clusterKey, ClusterSupport support) {\r
60         super(support, clusterUID, clusterKey);\r
61         this.clusterTable = ((SessionImplSocket)support.getSession()).clusterTable;\r
62     }\r
63     \r
64     public static ClusterImpl make(ClusterUID clusterUID, int clusterKey, ClusterSupport support) {\r
65         return new ClusterSmall(clusterUID, clusterKey, support);\r
66     }\r
67     public static ClusterSmall proxy(ClusterUID clusterUID, int clusterKey, long clusterId, ClusterSupport support) {\r
68         if (DEBUG)\r
69             new Exception("Cluster proxy for " + clusterUID).printStackTrace();\r
70         return new ClusterSmall(clusterUID, clusterKey, ((SessionImplSocket)support.getSession()).clusterTable, support);\r
71     }\r
72     public static ClusterImpl make(long[] longs, int[] ints, byte[] bytes, ClusterSupport support, int clusterKey)\r
73     throws DatabaseException {\r
74         if (longs[0] == 0)\r
75             return new ClusterBig(longs, ints, bytes, support, clusterKey);\r
76         else\r
77             return new ClusterSmall(longs, ints, bytes, support, clusterKey);\r
78     }\r
79     public abstract ClusterBig toBig(ClusterSupport support)\r
80     throws DatabaseException;\r
81     \r
82     public abstract void checkDirectReference(int dr)\r
83     throws DatabaseException;\r
84 \r
85     public abstract void checkForeingIndex(int fi)\r
86     throws DatabaseException;\r
87 \r
88     public abstract void checkObjectSetReference(int or)\r
89     throws DatabaseException;\r
90 \r
91 //    public boolean virtual = false;\r
92     \r
93     @Override\r
94     public boolean hasVirtual() {\r
95         return clusterTable.hasVirtual(clusterKey);\r
96     }\r
97 \r
98     @Override\r
99     public void markVirtual() {\r
100         clusterTable.markVirtual(clusterKey);\r
101 //        virtual = true;\r
102     }\r
103     \r
104     @Override\r
105     public boolean isWriteOnly() {\r
106         return false;\r
107     }\r
108     @Override\r
109     public boolean isLoaded() {\r
110         return true;\r
111     }\r
112     \r
113     @Override\r
114     public void resized() {\r
115         dirtySizeInBytes = true;\r
116         clusterTable.setDirtySizeInBytes(true);\r
117     }\r
118     \r
119     public long getCachedSize() {\r
120         if(dirtySizeInBytes) {\r
121             try {\r
122                 sizeInBytes = getUsedSpace();\r
123                 //System.err.println("recomputed size of cluster " + getClusterId() + " => " + sizeInBytes);\r
124             } catch (DatabaseException e) {\r
125                 Logger.defaultLogError(e);\r
126             }\r
127             dirtySizeInBytes = false;\r
128         }\r
129         return sizeInBytes;\r
130     }\r
131 \r
132     protected void calculateModifiedId() {\r
133 //        setModifiedId(new IdImpl(UUID.randomUUID()));\r
134     }\r
135     \r
136     @Override\r
137     public IClusterTable getClusterTable() {\r
138         return clusterTable;\r
139     }\r
140 }\r