]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterChange2.java
isImmutable can NPE
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ClusterChange2.java
1 package fi.vtt.simantics.procore.internal;
2
3 import java.util.Arrays;
4
5 import org.simantics.db.procore.cluster.ClusterImpl;
6 import org.simantics.db.service.Bytes;
7 import org.simantics.db.service.ClusterUID;
8
9 class ClusterChange2 {
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;
16     private byte[] bytes;
17     private int byteIndex;
18     private ClusterUID clusterUID;
19     ClusterChange2(ClusterUID clusterUID, ClusterImpl cluster) {
20         this.clusterUID = clusterUID;
21         init();
22     }
23     void init() {
24 //        System.err.println("clusterChange2 dirty " + cluster.clusterId);
25         dirty = false;
26         bytes = new byte[INCREMENT];
27         byteIndex = 0;
28         addInt(0); // Size of byte vector. Set by flush.
29         addInt(VERSION);
30         byteIndex = clusterUID.toByte(bytes, 8);
31     }
32     boolean isDirty() {
33         return dirty;
34     }
35     void flush(GraphSession graphSession) {
36 //        System.err.println("flush2 clusterChange2 " + dirty + this);
37         if (!dirty)
38             return;
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));
43         init();
44     }
45     void setImmutable(boolean immutable) {
46         dirty = true;
47         addByte(SET_IMMUTABLE_OPERATION);
48         addByte((byte)(immutable ? -1 : 0));
49     }
50     void setDeleted(boolean deleted) {
51         dirty = true;
52         addByte(SET_DELETED_OPERATION);
53         addByte((byte)(deleted ? -1 : 0));
54     }
55     void undoValueEx(int resourceIndex) {
56         dirty = true;
57         addByte(UNDO_VALUE_OPERATION);
58         addInt(resourceIndex);
59     }
60     private final void checkSpace(int len) {
61         if (bytes.length - byteIndex > len)
62             return;
63        bytes = Arrays.copyOf(bytes, bytes.length + len + INCREMENT);
64     }
65     private final void addByte(byte value) {
66         checkSpace(1);
67         bytes[byteIndex++] = value;
68     }
69     private final void addInt(int value) {
70         checkSpace(4);
71         Bytes.writeLE(bytes, byteIndex, value);
72         byteIndex += 4;
73     }
74 }