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 java.util.UUID;
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;
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;
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);
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]);
45 protected static Id getUniqueId(long[] longs) {
46 return new IdImpl(new UUID(longs[3], longs[4]));
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;
54 private boolean dirtySizeInBytes = true;
55 private long sizeInBytes = 0;
57 protected ClusterTable clusterTable;
59 public ClusterImpl(ClusterUID clusterUID, int clusterKey, ClusterSupport support) {
60 super(support, clusterUID, clusterKey);
61 this.clusterTable = ((SessionImplSocket)support.getSession()).clusterTable;
64 public static ClusterImpl make(ClusterUID clusterUID, int clusterKey, ClusterSupport support) {
65 return new ClusterSmall(clusterUID, clusterKey, support);
67 public static ClusterSmall proxy(ClusterUID clusterUID, int clusterKey, long clusterId, ClusterSupport support) {
69 new Exception("Cluster proxy for " + clusterUID).printStackTrace();
70 return new ClusterSmall(clusterUID, clusterKey, ((SessionImplSocket)support.getSession()).clusterTable, support);
72 public static ClusterImpl make(long[] longs, int[] ints, byte[] bytes, ClusterSupport support, int clusterKey)
73 throws DatabaseException {
75 return new ClusterBig(longs, ints, bytes, support, clusterKey);
77 return new ClusterSmall(longs, ints, bytes, support, clusterKey);
79 public abstract ClusterBig toBig(ClusterSupport support)
80 throws DatabaseException;
82 public abstract void checkDirectReference(int dr)
83 throws DatabaseException;
85 public abstract void checkForeingIndex(int fi)
86 throws DatabaseException;
88 public abstract void checkObjectSetReference(int or)
89 throws DatabaseException;
91 // public boolean virtual = false;
94 public boolean hasVirtual() {
95 return clusterTable.hasVirtual(clusterKey);
99 public void markVirtual() {
100 clusterTable.markVirtual(clusterKey);
105 public boolean isWriteOnly() {
109 public boolean isLoaded() {
114 public void resized() {
115 dirtySizeInBytes = true;
116 clusterTable.setDirtySizeInBytes(true);
119 public long getCachedSize() {
120 if(dirtySizeInBytes) {
121 long oldSize = sizeInBytes;
123 clusterTable.adjustCachedSize(-oldSize, this);
125 sizeInBytes = getUsedSpace();
126 //System.out.println("recomputed size of cluster " + getClusterId() + ": " + oldSize + " => " + sizeInBytes);
127 } catch (DatabaseException e) {
128 Logger.defaultLogError(e);
130 if (sizeInBytes != 0)
131 clusterTable.adjustCachedSize(sizeInBytes, this);
132 dirtySizeInBytes = false;
137 protected void calculateModifiedId() {
138 // setModifiedId(new IdImpl(UUID.randomUUID()));
142 public IClusterTable getClusterTable() {