]> 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 825ccf1b252ccf59c523e070cbb089d3d0120154..c8f4f1d6727fcc2e46f3a9ea6c97a52245d9cb8a 100644 (file)
@@ -6,12 +6,15 @@ import java.util.List;
 import java.util.Map;
 import java.util.Stack;
 
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.Statement;
+import org.simantics.db.VirtualGraph;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.interop.test.GraphChanges;
 
@@ -26,6 +29,7 @@ public abstract  class UpdateOperations {
        private Map<Resource, UpdateOp> resourceMap = new HashMap<Resource, UpdateOp>();
        private Map<Statement, UpdateOp> statementMap = new HashMap<Statement, UpdateOp>();
        private GraphChanges changes;
+       private int chunkSize = -1;
        
        public UpdateOperations(GraphChanges changes) {
                this.changes = changes; 
@@ -39,16 +43,101 @@ public abstract  class UpdateOperations {
                return statementMap.get(s);
        }
        
-       public void applyAll(WriteGraph g) throws DatabaseException {
+       public int getChunkSize() {
+               return chunkSize;
+       }
+       
+       public void setChunkSize(int chunkSize) {
+               this.chunkSize = chunkSize;
+       }
+       
+       /**
+        * Applies all changes.
+        * 
+        * @param graph
+        * @throws DatabaseException
+        */
+       public void applyAll(WriteGraph graph) throws DatabaseException {
+               List<UpdateOp> list = operations;
+               apply(graph, list);
+       }
+
+       /**
+        * Applies selected changes.
+        * @param graph
+        * @throws DatabaseException
+        */
+       public void applySelected(WriteGraph graph) throws DatabaseException {
+               List<UpdateOp> list = new ArrayList<UpdateOp>();
                for (UpdateOp op : operations) {
-                       apply(g, op);
+                       if (op.selected())
+                               list.add(op);
                }
+               apply(graph, list);
+       }
+       
+       /**
+        * Applies all changes with chunked DB writes.
+        * 
+        * @param session
+        * @throws DatabaseException
+        */
+       public void applyAll(Session session, VirtualGraph vg) throws DatabaseException {
+               List<UpdateOp> list = operations;
+               apply(session, list, vg);
        }
 
-       public void applySelected(WriteGraph g) throws DatabaseException {
+       /**
+        * Applies selected changes with chunked DB writes.
+        * 
+        * @param session
+        * @throws DatabaseException
+        */
+       public void applySelected(Session session, VirtualGraph vg) throws DatabaseException {
+               List<UpdateOp> list = new ArrayList<UpdateOp>();
                for (UpdateOp op : operations) {
                        if (op.selected())
-                               apply(g, op);
+                               list.add(op);
+               }
+               apply(session, list, vg);
+       }
+       
+       protected void apply(WriteGraph graph, List<UpdateOp> list) throws DatabaseException {
+               for (UpdateOp op : list) {
+                       apply(graph, op);
+               }
+       }
+       
+       protected void apply(Session session, List<UpdateOp> list, VirtualGraph vg) throws DatabaseException {
+               if (getChunkSize() > 0) {
+                       for (int s = 0; s < list.size(); ) {
+                               int e = s + getChunkSize();
+                               if (e > list.size())
+                                       e = list.size();
+                               List<UpdateOp> subList = list.subList(s, e);
+                               session.syncRequest(new WriteRequest(vg) {
+                                       
+                                       @Override
+                                       public void perform(WriteGraph graph) throws DatabaseException {
+                                               
+                                               for (UpdateOp op : subList) {
+                                                       apply(graph, op);
+                                               }
+                                       }
+                               });
+                               s = e;
+                       }
+               } else {
+                       session.syncRequest(new WriteRequest(vg) {
+                               
+                               @Override
+                               public void perform(WriteGraph graph) throws DatabaseException {
+                                       
+                                       for (UpdateOp op : list) {
+                                               apply(graph, op);
+                                       }
+                               }
+                       });
                }
        }
        
@@ -135,7 +224,7 @@ public abstract  class UpdateOperations {
         * @param session
         * @throws DatabaseException
         */
-       public void populate(Session session) throws DatabaseException {
+       public void populate(Session session, IProgressMonitor monitor) throws DatabaseException {
                session.syncRequest(new ReadRequest() {
                        
                        @Override