1 package fi.vtt.simantics.procore.internal;
3 import java.util.Arrays;
5 import org.simantics.db.service.Bytes;
6 import org.simantics.db.service.ClusterUID;
8 public class ClusterChange2 {
9 public static final int VERSION = 2;
10 public static final byte SET_IMMUTABLE_OPERATION = 1; // <byte : 0 = false>
11 public static final byte UNDO_VALUE_OPERATION = 2; // <int : resource index>
12 public static final byte SET_DELETED_OPERATION = 3; // <byte : 0 = false>
13 private static final int INCREMENT = 1<<10;
14 private boolean dirty = false;
16 private int byteIndex;
17 private ClusterUID clusterUID;
18 ClusterChange2(ClusterUID clusterUID) {
19 this.clusterUID = clusterUID;
23 // System.err.println("clusterChange2 dirty " + cluster.clusterId);
25 bytes = new byte[INCREMENT];
27 addInt(0); // Size of byte vector. Set by flush.
29 byteIndex = clusterUID.toByte(bytes, 8);
34 void flush(GraphSession graphSession) {
35 // System.err.println("flush2 clusterChange2 " + dirty + this);
38 Bytes.writeLE(bytes, 0, byteIndex - 4);
39 byte[] ops = Arrays.copyOf(bytes, byteIndex);
40 // System.err.println("flush2 clusterChange2 " + cluster.clusterId + " " + ops.length + " bytes.");
41 graphSession.updateCluster(new UpdateClusterFunction(ops));
44 void setImmutable(boolean immutable) {
46 addByte(SET_IMMUTABLE_OPERATION);
47 addByte((byte)(immutable ? -1 : 0));
49 void setDeleted(boolean deleted) {
51 addByte(SET_DELETED_OPERATION);
52 addByte((byte)(deleted ? -1 : 0));
54 void undoValueEx(int resourceIndex) {
56 addByte(UNDO_VALUE_OPERATION);
57 addInt(resourceIndex);
59 private final void checkSpace(int len) {
60 if (bytes.length - byteIndex > len)
62 bytes = Arrays.copyOf(bytes, bytes.length + len + INCREMENT);
64 private final void addByte(byte value) {
66 bytes[byteIndex++] = value;
68 private final void addInt(int value) {
70 Bytes.writeLE(bytes, byteIndex, value);