--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.procore.cluster;\r
+\r
+import java.util.UUID;\r
+\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.InvalidClusterException;\r
+import org.simantics.db.impl.ClusterBase;\r
+import org.simantics.db.impl.ClusterSupport;\r
+import org.simantics.db.impl.IClusterTable;\r
+import org.simantics.db.impl.Modifier;\r
+import org.simantics.db.service.ClusterCollectorPolicy.CollectorCluster;\r
+import org.simantics.db.service.ClusterUID;\r
+import org.simantics.db.service.ClusteringSupport.Id;\r
+\r
+import fi.vtt.simantics.procore.internal.Change;\r
+import fi.vtt.simantics.procore.internal.ClusterChange;\r
+import fi.vtt.simantics.procore.internal.ClusterTable;\r
+import fi.vtt.simantics.procore.internal.SessionImplSocket;\r
+\r
+public abstract class ClusterImpl extends ClusterBase implements Modifier, CollectorCluster {\r
+ private static final int LONG_HEADER_SIZE = 7;\r
+ private static final long LONG_HEADER_VERSION = 1;\r
+ protected static ClusterUID checkValidity(long type, long[] longs, int[] ints, byte[] bytes)\r
+ throws InvalidClusterException {\r
+ if (longs.length < LONG_HEADER_SIZE)\r
+ throw new InvalidClusterException("Header size mismatch. Expected=" + ClusterImpl.LONG_HEADER_SIZE + ", got=" + longs.length);\r
+ if (longs[0] != type)\r
+ throw new InvalidClusterException("Type mismatch. Expected=" + type + ", got=" + longs[0]);\r
+ if (longs[1] != ClusterImpl.LONG_HEADER_VERSION)\r
+ throw new InvalidClusterException("Header size mismatch. Expected=" + ClusterImpl.LONG_HEADER_VERSION + ", got=" + longs[1]);\r
+ return ClusterUID.make(longs[2], longs[3]);\r
+ }\r
+ protected static Id getUniqueId(long[] longs) {\r
+ return new IdImpl(new UUID(longs[3], longs[4]));\r
+ }\r
+ static final boolean DEBUG = false;\r
+ // This can be null iff the cluster has been converted to big\r
+ public Change change = new Change();\r
+ public ClusterChange cc;\r
+ public byte[] foreignLookup;\r
+ \r
+ private boolean dirtySizeInBytes = true;\r
+ private long sizeInBytes = 0;\r
+ \r
+ protected ClusterTable clusterTable;\r
+ \r
+ public ClusterImpl(ClusterUID clusterUID, int clusterKey, ClusterSupport support) {\r
+ super(support, clusterUID, clusterKey);\r
+ this.clusterTable = ((SessionImplSocket)support.getSession()).clusterTable;\r
+ }\r
+ \r
+ public static ClusterImpl make(ClusterUID clusterUID, int clusterKey, ClusterSupport support) {\r
+ return new ClusterSmall(clusterUID, clusterKey, support);\r
+ }\r
+ public static ClusterSmall proxy(ClusterUID clusterUID, int clusterKey, long clusterId, ClusterSupport support) {\r
+ if (DEBUG)\r
+ new Exception("Cluster proxy for " + clusterUID).printStackTrace();\r
+ return new ClusterSmall(clusterUID, clusterKey, ((SessionImplSocket)support.getSession()).clusterTable, support);\r
+ }\r
+ public static ClusterImpl make(long[] longs, int[] ints, byte[] bytes, ClusterSupport support, int clusterKey)\r
+ throws DatabaseException {\r
+ if (longs[0] == 0)\r
+ return new ClusterBig(longs, ints, bytes, support, clusterKey);\r
+ else\r
+ return new ClusterSmall(longs, ints, bytes, support, clusterKey);\r
+ }\r
+ public abstract ClusterBig toBig(ClusterSupport support)\r
+ throws DatabaseException;\r
+ \r
+ public abstract void checkDirectReference(int dr)\r
+ throws DatabaseException;\r
+\r
+ public abstract void checkForeingIndex(int fi)\r
+ throws DatabaseException;\r
+\r
+ public abstract void checkObjectSetReference(int or)\r
+ throws DatabaseException;\r
+\r
+// public boolean virtual = false;\r
+ \r
+ @Override\r
+ public boolean hasVirtual() {\r
+ return clusterTable.hasVirtual(clusterKey);\r
+ }\r
+\r
+ @Override\r
+ public void markVirtual() {\r
+ clusterTable.markVirtual(clusterKey);\r
+// virtual = true;\r
+ }\r
+ \r
+ @Override\r
+ public boolean isWriteOnly() {\r
+ return false;\r
+ }\r
+ @Override\r
+ public boolean isLoaded() {\r
+ return true;\r
+ }\r
+ \r
+ @Override\r
+ public void resized() {\r
+ dirtySizeInBytes = true;\r
+ clusterTable.setDirtySizeInBytes(true);\r
+ }\r
+ \r
+ public long getCachedSize() {\r
+ if(dirtySizeInBytes) {\r
+ try {\r
+ sizeInBytes = getUsedSpace();\r
+ //System.err.println("recomputed size of cluster " + getClusterId() + " => " + sizeInBytes);\r
+ } catch (DatabaseException e) {\r
+ Logger.defaultLogError(e);\r
+ }\r
+ dirtySizeInBytes = false;\r
+ }\r
+ return sizeInBytes;\r
+ }\r
+\r
+ protected void calculateModifiedId() {\r
+// setModifiedId(new IdImpl(UUID.randomUUID()));\r
+ }\r
+ \r
+ @Override\r
+ public IClusterTable getClusterTable() {\r
+ return clusterTable;\r
+ }\r
+}\r