]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/lru/ClusterUpdateOperation.java
Merge commit '876ede6'
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / lru / ClusterUpdateOperation.java
1 package org.simantics.acorn.lru;
2
3 import org.simantics.acorn.ClusterManager;
4 import org.simantics.acorn.cluster.ClusterImpl;
5 import org.simantics.acorn.exception.AcornAccessVerificationException;
6 import org.simantics.acorn.exception.IllegalAcornStateException;
7 import org.simantics.acorn.internal.ClusterChange;
8 import org.simantics.acorn.internal.ClusterChange2;
9 import org.simantics.acorn.internal.ClusterUpdateProcessor;
10 import org.simantics.acorn.internal.ClusterUpdateProcessor2;
11 import org.simantics.db.service.Bytes;
12 import org.simantics.db.service.ClusterUID;
13
14 final public class ClusterUpdateOperation {
15         
16         final public ClusterUID uid;
17         final protected ClusterManager manager;
18         final protected ClusterInfo info;
19
20         public byte[] data;
21         
22         public ClusterStreamChunk chunk;
23         public ClusterChangeSet ccs;
24         boolean finished = false;
25
26         public ClusterUpdateOperation(ClusterManager manager, byte[] data) throws IllegalAcornStateException, AcornAccessVerificationException {
27                 
28                 long cuid1 = Bytes.readLE8(data, 8);
29                 long cuid2 = Bytes.readLE8(data, 16);
30
31                 this.manager = manager;
32                 this.uid = ClusterUID.make(cuid1, cuid2);
33                 this.data = data;
34                 this.info = manager.clusterLRU.getOrCreate(uid, true);
35         }
36         
37         public void finish() {
38                 finished = true;
39         }
40         
41         public void scheduled(String ccsInfoId) throws AcornAccessVerificationException, IllegalAcornStateException {
42                 ccs = new ClusterChangeSet(ccsInfoId, uid);
43                 chunk = ccs.getChunk(manager);
44                 manager.addIntoCurrentChangeSet(ccsInfoId);
45         }
46         
47         public void run() throws AcornAccessVerificationException, IllegalAcornStateException {
48                 ClusterUpdateOperation op = null;
49                 byte[] data = null;
50                 chunk.acquireMutex();
51                 try {
52                         chunk.makeResident();
53                         op = chunk.operations.get(ccs.chunkOffset);
54                         data = op.data;
55                 } finally {
56                         chunk.releaseMutex();
57                 }
58                 op.runWithData(data);
59         }
60         
61         public void runWithData(byte[] data) throws IllegalAcornStateException, AcornAccessVerificationException {
62                 
63                 try {
64                         int version = Bytes.readLE4(data, 4);
65                         if(version == ClusterChange.VERSION) {
66                                 ClusterUpdateProcessor processor = new ClusterUpdateProcessor(manager, manager.support, data, this);
67                                 ClusterImpl cluster = info.getForUpdate();
68                                 cluster = processor.process(cluster);
69                                 manager.update(uid, cluster);
70                         } else if (version == ClusterChange2.VERSION) {
71                                 ClusterUpdateProcessor2 processor = new ClusterUpdateProcessor2(manager.support, data, this);
72                                 ClusterImpl cluster = info.getForUpdate();
73                                 processor.process(cluster);
74                                 manager.update(uid, cluster);
75                         } else {
76                                 throw new IllegalAcornStateException("unsupported clusterChange version " + version);
77                         }
78                 } catch (IllegalAcornStateException | AcornAccessVerificationException e) {
79                     throw e;
80                 } catch (Throwable t) {
81                         throw new IllegalAcornStateException(t);
82                 }
83         }
84         
85         @Override
86         public String toString() {
87             StringBuilder sb = new StringBuilder();
88             sb.append("ClusterUpdateOperation [uid=").append(uid).append("] [info=").append(info).append("] [ccs=").append(ccs).append("] [chunk=").append("]");
89             return sb.toString();
90         }
91 }