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