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