]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/lru/ChangeSetInfo.java
Cluster sets are written to wrong directory until first snapshot is made
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / lru / ChangeSetInfo.java
1 package org.simantics.acorn.lru;
2
3 import java.nio.file.Path;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6
7 import org.simantics.acorn.exception.AcornAccessVerificationException;
8 import org.simantics.acorn.exception.IllegalAcornStateException;
9 import org.simantics.db.service.Bytes;
10 import org.simantics.utils.datastructures.Pair;
11
12 import gnu.trove.list.array.TByteArrayList;
13
14 public class ChangeSetInfo extends LRUObject<Long, ChangeSetInfo> {
15
16         private byte[] metadataBytes;
17         private ArrayList<String> clusterChangeSetIds;
18         
19         // Stub
20         public ChangeSetInfo(LRU<Long, ChangeSetInfo> LRU, Path readDir, Long revision, int offset, int length) throws AcornAccessVerificationException {
21                 super(LRU, revision, readDir, "clusterStream", offset, length, false, false);
22                 LRU.map(this);
23         }
24         
25         // New
26         public ChangeSetInfo(LRU<Long, ChangeSetInfo> LRU, Long revision, byte[] bytes, ArrayList<String> clusterChangeSetIds) throws AcornAccessVerificationException {
27                 super(LRU, revision, LRU.getDirectory(), "clusterStream", true, true);
28                 this.metadataBytes = bytes;
29                 this.metadataBytes = bytes;
30                 this.clusterChangeSetIds = clusterChangeSetIds;
31                 LRU.insert(this, accessTime);
32         }
33         
34         public ArrayList<String> getCCSIds() throws AcornAccessVerificationException {
35                 if(VERIFY) verifyAccess();
36                 return clusterChangeSetIds;
37         }
38         
39         public byte[] getMetadataBytes() throws AcornAccessVerificationException, IllegalAcornStateException {
40                 if(VERIFY)
41                     verifyAccess();
42                 
43                 makeResident();
44                 return metadataBytes;
45         }
46         
47         private static void writeLE(TByteArrayList bytes, int value) {
48                 
49                 bytes.add( (byte) (value & 0xFF));
50                 bytes.add((byte) ((value >>> 8) & 0xFF));
51                 bytes.add((byte) ((value >>> 16) & 0xFF));
52                 bytes.add((byte) ((value >>> 24) & 0xFF));
53
54         }
55
56         @Override
57         protected Pair<byte[], Integer> toBytes() {
58                 
59                 TByteArrayList result = new TByteArrayList();
60                 writeLE(result, metadataBytes.length);
61                 result.add(metadataBytes);
62                 writeLE(result, clusterChangeSetIds.size());
63                 for(String id : clusterChangeSetIds) {
64                         byte[] bb = id.getBytes();
65                         writeLE(result, bb.length);
66                         result.add(bb);
67                 }
68
69                 release();
70                 
71                 byte[] ret = result.toArray();
72                 
73                 return Pair.make(ret, ret.length);
74                 
75         }
76         
77         @Override
78         void release() {
79                 clusterChangeSetIds = null;
80                 metadataBytes = null;
81         }
82
83         @Override
84         public void fromFile(byte[] data) {
85                 
86                 clusterChangeSetIds = new ArrayList<String>();
87                 
88                 int metadataLength = Bytes.readLE4(data, 0);
89                 metadataBytes = Arrays.copyOfRange(data, 4, 4+metadataLength);
90                 int offset = 4+metadataLength;
91                 int numberOfChangeSets = Bytes.readLE4(data, offset);
92                 offset += 4;
93                 for(int i=0;i<numberOfChangeSets;i++) {
94                         int length = Bytes.readLE4(data, offset);
95                         offset += 4;
96                         String id = new String(Arrays.copyOfRange(data, offset, offset+length));
97                         clusterChangeSetIds.add(id);
98                         offset += length;
99                 }
100                 
101         }
102
103         @Override
104         String getExtension() {
105                 return "cs";
106         }
107         
108         @Override
109         protected boolean overwrite() {
110                 return false;
111         }
112
113 }