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