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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.procore.cluster;
\r
14 import java.util.UUID;
\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
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
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
45 protected static Id getUniqueId(long[] longs) {
\r
46 return new IdImpl(new UUID(longs[3], longs[4]));
\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
54 private boolean dirtySizeInBytes = true;
\r
55 private long sizeInBytes = 0;
\r
57 protected ClusterTable clusterTable;
\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
64 public static ClusterImpl make(ClusterUID clusterUID, int clusterKey, ClusterSupport support) {
\r
65 return new ClusterSmall(clusterUID, clusterKey, support);
\r
67 public static ClusterSmall proxy(ClusterUID clusterUID, int clusterKey, long clusterId, ClusterSupport support) {
\r
69 new Exception("Cluster proxy for " + clusterUID).printStackTrace();
\r
70 return new ClusterSmall(clusterUID, clusterKey, ((SessionImplSocket)support.getSession()).clusterTable, support);
\r
72 public static ClusterImpl make(long[] longs, int[] ints, byte[] bytes, ClusterSupport support, int clusterKey)
\r
73 throws DatabaseException {
\r
75 return new ClusterBig(longs, ints, bytes, support, clusterKey);
\r
77 return new ClusterSmall(longs, ints, bytes, support, clusterKey);
\r
79 public abstract ClusterBig toBig(ClusterSupport support)
\r
80 throws DatabaseException;
\r
82 public abstract void checkDirectReference(int dr)
\r
83 throws DatabaseException;
\r
85 public abstract void checkForeingIndex(int fi)
\r
86 throws DatabaseException;
\r
88 public abstract void checkObjectSetReference(int or)
\r
89 throws DatabaseException;
\r
91 // public boolean virtual = false;
\r
94 public boolean hasVirtual() {
\r
95 return clusterTable.hasVirtual(clusterKey);
\r
99 public void markVirtual() {
\r
100 clusterTable.markVirtual(clusterKey);
\r
105 public boolean isWriteOnly() {
\r
109 public boolean isLoaded() {
\r
114 public void resized() {
\r
115 dirtySizeInBytes = true;
\r
116 clusterTable.setDirtySizeInBytes(true);
\r
119 public long getCachedSize() {
\r
120 if(dirtySizeInBytes) {
\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
127 dirtySizeInBytes = false;
\r
129 return sizeInBytes;
\r
132 protected void calculateModifiedId() {
\r
133 // setModifiedId(new IdImpl(UUID.randomUUID()));
\r
137 public IClusterTable getClusterTable() {
\r
138 return clusterTable;
\r