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