]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.acorn/src/org/simantics/acorn/lru/ClusterUpdateOperation.java
Sharing org.simantics.acorn for everyone to use
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / lru / ClusterUpdateOperation.java
diff --git a/bundles/org.simantics.acorn/src/org/simantics/acorn/lru/ClusterUpdateOperation.java b/bundles/org.simantics.acorn/src/org/simantics/acorn/lru/ClusterUpdateOperation.java
new file mode 100644 (file)
index 0000000..40a44bc
--- /dev/null
@@ -0,0 +1,91 @@
+package org.simantics.acorn.lru;
+
+import org.simantics.acorn.ClusterManager;
+import org.simantics.acorn.cluster.ClusterImpl;
+import org.simantics.acorn.internal.ClusterChange;
+import org.simantics.acorn.internal.ClusterChange2;
+import org.simantics.acorn.internal.ClusterUpdateProcessor;
+import org.simantics.acorn.internal.ClusterUpdateProcessor2;
+import org.simantics.db.service.Bytes;
+import org.simantics.db.service.ClusterUID;
+
+final public class ClusterUpdateOperation {
+       
+       final public ClusterUID uid;
+       final protected ClusterManager manager;
+       final protected ClusterInfo info;
+
+       public byte[] data;
+       
+       public ClusterStreamChunk chunk;
+       public ClusterChangeSet ccs;
+       boolean finished = false;
+
+       public ClusterUpdateOperation(ClusterManager manager, byte[] data) {
+               
+               long cuid1 = Bytes.readLE8(data, 8);
+               long cuid2 = Bytes.readLE8(data, 16);
+
+               this.manager = manager;
+               this.uid = ClusterUID.make(cuid1, cuid2);
+               this.data = data;
+               this.info = manager.clusterLRU.getOrCreate(uid, true);
+                               
+       }
+       
+       public void finish() {
+               finished = true;
+       }
+       
+       public void scheduled(String ccsInfoId) {
+               ccs = new ClusterChangeSet(ccsInfoId, uid);
+               chunk = ccs.getChunk(manager);
+               manager.addIntoCurrentChangeSet(ccsInfoId);
+       }
+       
+       public void run() {
+               ClusterUpdateOperation op = null;
+               byte[] data = null;
+               chunk.acquireMutex();
+               try {
+                       chunk.makeResident();
+                       op = chunk.operations.get(ccs.chunkOffset);
+                       data = op.data;
+               } catch (Throwable t) {
+                       throw new IllegalStateException(t);
+               } finally {
+                       chunk.releaseMutex();
+               }
+               op.runWithData(data);
+       }
+       
+       public void runWithData(byte[] data) {
+               
+               try {
+                       int version = Bytes.readLE4(data, 4);
+                       if(version == ClusterChange.VERSION) {
+                               ClusterUpdateProcessor processor = new ClusterUpdateProcessor(manager, manager.support, data, this);
+                               ClusterImpl cluster = info.getForUpdate();
+                               cluster = processor.process(cluster);
+                               manager.update(uid, cluster);
+                       } else if (version == ClusterChange2.VERSION) {
+                               ClusterUpdateProcessor2 processor = new ClusterUpdateProcessor2(manager.support, data, this);
+                               ClusterImpl cluster = info.getForUpdate();
+                               processor.process(cluster);
+                               manager.update(uid, cluster);
+                       } else {
+                               throw new IllegalStateException();
+                       }
+               } catch (Throwable t) {
+                       t.printStackTrace();
+               }
+               
+       }
+       
+       @Override
+       public String toString() {
+           StringBuilder sb = new StringBuilder();
+           sb.append("ClusterUpdateOperation [uid=").append(uid).append("] [info=").append(info).append("] [ccs=").append(ccs).append("] [chunk=").append("]");
+           return sb.toString();
+       }
+}
\ No newline at end of file