]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/OrderedSetUtils.java
Improved element reordering performance
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / OrderedSetUtils.java
index eb0ca24b6a5fa32e318936dfbfe54ce5a8a42f79..8e8171f85273e460bdf519df9eab706ba395b539 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -25,7 +25,6 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.WriteOnlyGraph;
-import org.simantics.db.common.request.ReadRequest;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.RuntimeDatabaseException;
 import org.simantics.db.exception.ValidationException;
@@ -221,6 +220,27 @@ public class OrderedSetUtils {
             }
     }
 
+    /**
+     * Reorders elements with a minimum number of writes. The set of elements must remain the same. 
+     */
+    public static void reorder(WriteGraph g, Resource l, Iterable<Resource> order) throws DatabaseException {
+        Resource newPrev = l;
+        for (Resource r : order) {
+            Resource prev = OrderedSetUtils.prev(g, l, r);
+            if (!prev.equals(newPrev)) {
+                g.deny(prev, l, r);
+                g.claim(newPrev, l, r);
+            }
+            newPrev = r;
+        }
+        Resource newLast = newPrev;
+        Resource last = OrderedSetUtils.prev(g, l, l);
+        if (!last.equals(newLast)) {
+            g.deny(last, l, l);
+            g.claim(newLast, l, l);
+        }
+    }
+
     /**
      * Converts ordered set into a list.
      */
@@ -235,21 +255,6 @@ public class OrderedSetUtils {
         }
     }
 
-    /**
-     * Converts ordered set into a list.
-     */
-    public static void forEach(AsyncReadGraph g, final Resource l, final AsyncMultiProcedure<Resource> procedure) {
-        g.asyncRequest(new ReadRequest() {
-
-            @Override
-            public void run(ReadGraph graph) throws DatabaseException {
-                for(Resource r : toList(graph, l))
-                    procedure.execute(graph, r);
-            }
-
-        });
-    }
-
     /**
      * Creates an empty ordered set.
      */