]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOperations.java
Process changes in smaller chunks
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateOperations.java
index 74acf180cb455c952a14f1a7dcd21a866bed5ea6..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;
 
@@ -21,7 +23,8 @@ import org.simantics.interop.test.GraphChanges;
 public abstract  class UpdateOperations {
        
        private List<UpdateOp> operations = new ArrayList<UpdateOp>();
-       private Map<Resource, UpdateOp> opMap = new HashMap<Resource, UpdateOp>();
+       private Map<Resource, UpdateOp> resourceMap = new HashMap<Resource, UpdateOp>();
+       private Map<Statement, UpdateOp> statementMap = new HashMap<Statement, UpdateOp>();
        private GraphChanges changes;
        
        public UpdateOperations(GraphChanges changes) {
@@ -29,7 +32,11 @@ public abstract  class UpdateOperations {
        }
        
        public UpdateOp getUpdateOp(Resource r) {
-               return opMap.get(r);
+               return resourceMap.get(r);
+       }
+       
+       public UpdateOp getUpdateOp(Statement s) {
+               return statementMap.get(s);
        }
        
        public void applyAll(WriteGraph g) throws DatabaseException {
@@ -52,6 +59,14 @@ public abstract  class UpdateOperations {
        public GraphChanges getChanges() {
                return changes;
        }
+       
+       public Map<Resource, UpdateOp> getResourceMap() {
+               return resourceMap;
+       }
+       
+       public Map<Statement, UpdateOp> getStatementMap() {
+               return statementMap;
+       }
 
        private void apply(WriteGraph g, UpdateOp op) throws DatabaseException {
                Stack<UpdateOp> stack = new Stack<UpdateOp>();
@@ -61,19 +76,21 @@ public abstract  class UpdateOperations {
        private void _apply(WriteGraph g, Stack<UpdateOp> stack, UpdateOp op) throws DatabaseException {
                if (op.applied())
                        return;
-               if (stack.contains(op))
+               if (stack.contains(op)) {
+                       op.apply(g);
                        return;
-               stack.push(op);
-               if (op.requiresParentOps()) {
-                       for (UpdateOp pop : op.getParentOps())
-                               if (!pop.applied())
-                                       _apply(g, stack, pop);
-               } 
-               if (op.requiresSubOps()) {
-                       for (UpdateOp sop : op.getSubOps())
-                               if (!sop.applied())
-                                       _apply(g, stack, sop);
                }
+               stack.push(op);
+               for (UpdateOp pop : op.getParentOps())
+                   if (op.requiresOp(pop)) {
+                if (!pop.applied())
+                    _apply(g, stack, pop);
+        } 
+               for (UpdateOp sop : op.getSubOps())
+                   if (op.requiresOp(sop)) {
+                if (!sop.applied())
+                    _apply(g, stack, sop);
+        }
                stack.pop();
                op.apply(g);
        }
@@ -85,25 +102,49 @@ public abstract  class UpdateOperations {
        }
        
        protected void addOp(Resource r, UpdateOp op) {
-               opMap.put(r, op);
+               resourceMap.put(r, op);
+               operations.add(op);
+       }
+       
+       protected void addOp(Statement s, UpdateOp op) {
+               statementMap.put(s, op);
                operations.add(op);
        }
        
        protected void replaceOp(Resource r, UpdateOp op) {
-               UpdateOp oldOp = opMap.remove(r);
+               UpdateOp oldOp = resourceMap.remove(r);
                if (oldOp != null) {
-                       operations.remove(op);
+                       operations.remove(oldOp);
                }
-               opMap.put(r, op);
+               resourceMap.put(r, op);
                operations.add(op);
        }
        
        protected UpdateOp getOP(Resource r) {
-               return opMap.get(r);
+               return resourceMap.get(r);
+       }
+       
+       protected UpdateOp getOP(Statement r) {
+               return statementMap.get(r);
        }
 
        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;