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