]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Process changes in smaller chunks 99/4899/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 24 Feb 2022 09:39:27 +0000 (11:39 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 24 Feb 2022 09:39:27 +0000 (11:39 +0200)
gitlab #39

Change-Id: I82fab040ceb9c93246ca7db7ffa9f62dfb654ddb

org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOperations.java
org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java

index 0c1882c3fbcc38b6c7eadaeb5b88fa0f65ff146b..825ccf1b252ccf59c523e070cbb089d3d0120154 100644 (file)
@@ -8,8 +8,10 @@ import java.util.Stack;
 
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
+import org.simantics.db.Session;
 import org.simantics.db.Statement;
 import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ReadRequest;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.interop.test.GraphChanges;
 
@@ -128,6 +130,21 @@ public abstract  class UpdateOperations {
 
        public abstract void populate(ReadGraph g) throws DatabaseException;
        
+       /**
+        * Secondary populate method. Override this for chunked DB operations.  
+        * @param session
+        * @throws DatabaseException
+        */
+       public void populate(Session session) throws DatabaseException {
+               session.syncRequest(new ReadRequest() {
+                       
+                       @Override
+                       public void run(ReadGraph graph) throws DatabaseException {
+                               populate(graph);
+                       }
+               });
+       }
+       
        protected boolean compares(Resource r1, Resource r2) {
                if (r1.equals(r2))
                        return true;
index a971da4f41234690724961a9fc1137e13192ffe3..a558f84462cf5e8cfe6548f761afa0c85cdb00fe 100644 (file)
@@ -5,7 +5,10 @@ import java.util.Map;
 
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.common.request.ResourceRead;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Read;
 import org.simantics.interop.test.GraphChanges;
 
 
@@ -28,6 +31,29 @@ public class UpdateTree {
                populate(g);
        }
        
+       public UpdateTree(Session session, GraphChanges changes, UpdateOperations updateOps) throws DatabaseException {
+               this.changes = changes;
+               this.nodes = new HashMap<Resource, UpdateNode>();
+               this.rootNode = session.syncRequest(new NodeRequest(UpdateStatus.EXIST, changes.getResource1()));
+               nodes.put(changes.getResource1(), rootNode);
+               nodes.put(changes.getResource2(), rootNode);
+               this.updateOps = updateOps;
+               this.updateOps.populate(session);
+               populate(session);
+       }
+       
+       private class NodeRequest extends ResourceRead<UpdateNode> {
+               UpdateStatus status;
+               public NodeRequest(UpdateStatus status, Resource r) {
+                       super(r);
+                       this.status = status;
+               }
+               @Override
+               public UpdateNode perform(ReadGraph graph) throws DatabaseException {
+                       return createNode(graph, status, resource);
+               }
+       }
+       
        public UpdateOperations getUpdateOps() {
                return updateOps;
        }
@@ -127,23 +153,52 @@ public class UpdateTree {
                return false;
        }
        
-       private void populate(ReadGraph g) throws DatabaseException{
+       protected void populate(ReadGraph g) throws DatabaseException{
 
                for (UpdateOp op : updateOps.getOperations()) {
-                       if (!handleCustom(g, op)) {
-                               if (op.isAdd()) {
-                                       addNode(g, null,op.getResource());
-                               } else if (op.isDelete()){
-                                       addNode(g, op.getResource(), null);
-                               } else if (op.isChange()) {
-                                       Resource o = op.getResource();
-                                   Resource l = getChanges().getComparable().containsLeft(o) ? o :getChanges().getComparable().getLeft(o);
-                                   Resource r = getChanges().getComparable().containsRight(o) ? o :getChanges().getComparable().getRight(o);
-                                   addNode(g, l, r);
-                               }
+                       populate(g, op);
+               }
+               
+       }
+       
+       protected void populate(ReadGraph g, UpdateOp op) throws DatabaseException{
+               if (!handleCustom(g, op)) {
+                       if (op.isAdd()) {
+                               addNode(g, null,op.getResource());
+                       } else if (op.isDelete()){
+                               addNode(g, op.getResource(), null);
+                       } else if (op.isChange()) {
+                               Resource o = op.getResource();
+                           Resource l = getChanges().getComparable().containsLeft(o) ? o :getChanges().getComparable().getLeft(o);
+                           Resource r = getChanges().getComparable().containsRight(o) ? o :getChanges().getComparable().getRight(o);
+                           addNode(g, l, r);
                        }
                }
+       }
+       
+       protected void populate(Session session) throws DatabaseException{
+               int i = 0;
+               while (i < updateOps.getOperations().size()) {
+                       i = session.syncRequest(new PopulateRead(i));
+               }
+       }
+       
+       protected class PopulateRead implements Read<Integer> {
+               int s;
+               public PopulateRead(int s) {
+                       this.s = s;
+               }
                
+               public Integer perform(ReadGraph graph) throws DatabaseException {
+                       int l = s + 100;
+                       if (l > updateOps.getOperations().size())
+                               l = updateOps.getOperations().size();
+                       for (int i = s; i < l; i++) {
+                               UpdateOp op = updateOps.getOperations().get(i);
+                               populate(graph, op);
+                       }
+                       return l;
+               }
        }
 
 }