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