1 package fi.vtt.simantics.procore.internal;
\r
3 import java.util.Arrays;
\r
5 import org.simantics.db.procore.cluster.ClusterImpl;
\r
6 import org.simantics.db.service.Bytes;
\r
7 import org.simantics.db.service.ClusterUID;
\r
9 class ClusterChange2 {
\r
10 public static final int VERSION = 2;
\r
11 public static final byte SET_IMMUTABLE_OPERATION = 1; // <byte : 0 = false>
\r
12 public static final byte UNDO_VALUE_OPERATION = 2; // <int : resource index>
\r
13 public static final byte SET_DELETED_OPERATION = 3; // <byte : 0 = false>
\r
14 private static final int INCREMENT = 1<<10;
\r
15 private boolean dirty = false;
\r
16 private byte[] bytes;
\r
17 private int byteIndex;
\r
18 private ClusterUID clusterUID;
\r
19 ClusterChange2(ClusterUID clusterUID, ClusterImpl cluster) {
\r
20 this.clusterUID = clusterUID;
\r
24 // System.err.println("clusterChange2 dirty " + cluster.clusterId);
\r
26 bytes = new byte[INCREMENT];
\r
28 addInt(0); // Size of byte vector. Set by flush.
\r
30 byteIndex = clusterUID.toByte(bytes, 8);
\r
35 void flush(GraphSession graphSession) {
\r
36 // System.err.println("flush2 clusterChange2 " + dirty + this);
\r
39 Bytes.writeLE(bytes, 0, byteIndex - 4);
\r
40 byte[] ops = Arrays.copyOf(bytes, byteIndex);
\r
41 // System.err.println("flush2 clusterChange2 " + cluster.clusterId + " " + ops.length + " bytes.");
\r
42 graphSession.updateCluster(new UpdateClusterFunction(ops));
\r
45 void setImmutable(boolean immutable) {
\r
47 addByte(SET_IMMUTABLE_OPERATION);
\r
48 addByte((byte)(immutable ? -1 : 0));
\r
50 void setDeleted(boolean deleted) {
\r
52 addByte(SET_DELETED_OPERATION);
\r
53 addByte((byte)(deleted ? -1 : 0));
\r
55 void undoValueEx(int resourceIndex) {
\r
57 addByte(UNDO_VALUE_OPERATION);
\r
58 addInt(resourceIndex);
\r
60 private final void checkSpace(int len) {
\r
61 if (bytes.length - byteIndex > len)
\r
63 bytes = Arrays.copyOf(bytes, bytes.length + len + INCREMENT);
\r
65 private final void addByte(byte value) {
\r
67 bytes[byteIndex++] = value;
\r
69 private final void addInt(int value) {
\r
71 Bytes.writeLE(bytes, byteIndex, value);
\r