Sharing org.simantics.acorn for everyone to use
[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.db.service.Bytes;
8 import org.simantics.utils.datastructures.Pair;
9
10 import gnu.trove.list.array.TByteArrayList;
11
12 public class ChangeSetInfo extends LRUObject<Long, ChangeSetInfo> {
13
14         private byte[] metadataBytes;
15         private ArrayList<String> clusterChangeSetIds;
16         
17         // Stub
18         public ChangeSetInfo(LRU<Long, ChangeSetInfo> LRU, Path readDir, Long revision, int offset, int length) {
19                 super(LRU, revision, readDir, "clusterStream", offset, length, false, false);
20                 LRU.map(this);
21         }
22         
23         // New
24         public ChangeSetInfo(LRU<Long, ChangeSetInfo> LRU, Long revision, byte[] bytes, ArrayList<String> clusterChangeSetIds) {
25                 super(LRU, revision, LRU.getDirectory(), "clusterStream", true, true);
26                 this.metadataBytes = bytes;
27                 this.metadataBytes = bytes;
28                 this.clusterChangeSetIds = clusterChangeSetIds;
29                 LRU.insert(this, accessTime);
30         }
31         
32         public ArrayList<String> getCSSIds() {
33                 if(VERIFY) verifyAccess();
34                 return clusterChangeSetIds;
35         }
36         
37         public byte[] getMetadataBytes() {
38                 
39                 if(VERIFY) verifyAccess();
40                 
41                 makeResident();
42                 
43                 return metadataBytes;
44                 
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 }