]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java
Query control functions into Simantics/DB
[simantics/platform.git] / bundles / org.simantics.scl.db / src / org / simantics / scl / db / SCLFunctions.java
index 00df313ad4e34eaed68253b4ccc93640f52d1c79..618d6a91fbe793e5d982656f19a063f49e297627 100644 (file)
@@ -8,10 +8,12 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.VirtualGraph;
 import org.simantics.db.WriteGraph;
+import org.simantics.db.common.procedure.adapter.SyncListenerAdapter;
 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
 import org.simantics.db.common.request.BinaryRead;
 import org.simantics.db.common.request.DelayedWriteRequest;
 import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.UnaryRead;
 import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.common.request.WriteResultRequest;
 import org.simantics.db.exception.DatabaseException;
@@ -19,6 +21,7 @@ import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.layer0.variable.Variables;
 import org.simantics.db.request.Read;
 import org.simantics.db.service.ClusterControl;
+import org.simantics.db.service.QueryControl;
 import org.simantics.db.service.SerialisationSupport;
 import org.simantics.db.service.VirtualGraphSupport;
 import org.simantics.layer0.utils.triggers.IActivationManager;
@@ -33,6 +36,7 @@ import org.simantics.scl.osgi.SCLOsgi;
 import org.simantics.scl.runtime.SCLContext;
 import org.simantics.scl.runtime.function.Function;
 import org.simantics.scl.runtime.function.Function1;
+import org.simantics.scl.runtime.tuple.Tuple;
 import org.simantics.scl.runtime.tuple.Tuple0;
 import org.simantics.utils.DataContainer;
 
@@ -51,25 +55,19 @@ public class SCLFunctions {
     }
     
     public static void asyncRead(final Function f) throws DatabaseException {
-        final SCLContext context = SCLContext.getCurrent();
-        Object graph = context.get(GRAPH);
-        if (graph != null) {
-            f.apply(Tuple0.INSTANCE);
-        } else {
-            Simantics.getSession().asyncRequest(new ReadRequest() {
-                @Override
-                public void run(ReadGraph graph) throws DatabaseException {
-                    SCLContext.push(context);
-                    ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
-                    try {
-                        f.apply(Tuple0.INSTANCE);
-                    } finally {
-                        context.put(GRAPH, oldGraph);
-                        SCLContext.pop();
-                    }
+        final SCLContext context = SCLContext.createDerivedContext();
+        Simantics.getSession().asyncRequest(new ReadRequest() {
+            @Override
+            public void run(ReadGraph graph) throws DatabaseException {
+                SCLContext.push(context);
+                context.put(GRAPH, graph);
+                try {
+                    f.apply(Tuple0.INSTANCE);
+                } finally {
+                    SCLContext.pop();
                 }
-            });
-        }
+            }
+        });
     }
     
     public static <T> T syncRead(final Function f) throws DatabaseException {
@@ -95,25 +93,19 @@ public class SCLFunctions {
     }
     
     public static void asyncWrite(final Function f) throws DatabaseException {
-        final SCLContext context = SCLContext.getCurrent();
-        Object graph = context.get(GRAPH);
-        if (graph != null) {
-            f.apply(Tuple0.INSTANCE);
-        } else {
-            Simantics.getSession().asyncRequest(new WriteRequest() {
-                @Override
-                public void perform(WriteGraph graph) throws DatabaseException {
-                    SCLContext.push(context);
-                    ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph);
-                    try {
-                        f.apply(Tuple0.INSTANCE);
-                    } finally {
-                        context.put(GRAPH, oldGraph);
-                        SCLContext.pop();
-                    }
+        SCLContext context = SCLContext.createDerivedContext();
+        Simantics.getSession().asyncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                SCLContext.push(context);
+                context.put(GRAPH, graph);
+                try {
+                    f.apply(Tuple0.INSTANCE);
+                } finally {
+                    SCLContext.pop();
                 }
-            });
-        }
+            }
+        });
     }
     
     public static <T> T syncWrite(final Function f) throws DatabaseException {
@@ -273,43 +265,17 @@ public class SCLFunctions {
     }
     
 
-    private static class Subquery implements Read<Object> {
-        Function q;
+    private static class Subquery extends UnaryRead<Function, Object> {
 
         public Subquery(Function q) {
-            this.q = q;
+            super(q);
         }
 
         @Override
         public Object perform(ReadGraph graph) throws DatabaseException {
-            SCLContext sclContext = SCLContext.getCurrent();
-            Object oldGraph = sclContext.put("graph", graph);
-            try {
-                return q.apply(Tuple0.INSTANCE);
-            } catch (Throwable e) {
-                if(e instanceof DatabaseException)
-                    throw (DatabaseException)e;
-                else
-                    throw new DatabaseException(e);
-            } finally {
-                sclContext.put("graph", oldGraph);
-            }
+            return Simantics.applySCLRead(graph, parameter, Tuple0.INSTANCE);
         }
 
-        @Override
-        public int hashCode() {
-            return q.hashCode();
-        }
-        
-        @Override
-        public boolean equals(Object obj) {
-            if(this == obj)
-                return true;
-            if(obj == null || obj.getClass() != getClass())
-                return false;
-            Subquery other = (Subquery)obj;
-            return q.equals(other.q);
-        }
     }
 
     public static Object subquery(ReadGraph graph, Function q) throws DatabaseException {
@@ -319,6 +285,25 @@ public class SCLFunctions {
     public static Object subqueryC(ReadGraph graph, Function q) throws DatabaseException {
         return graph.syncRequest(new Subquery(q), TransientCacheAsyncListener.<Object>instance());
     }
+    
+    public static void subqueryL(ReadGraph graph, Function query, Function executeCallback, Function1<Throwable, Tuple> exceptionCallback, Function1<Tuple0, Boolean> isDisposedCallback) throws DatabaseException {
+        graph.asyncRequest(new Subquery(query), new SyncListenerAdapter<Object>() {
+            @Override
+            public void execute(ReadGraph graph, Object result) throws DatabaseException {
+                Simantics.applySCLRead(graph, executeCallback, result);
+            }
+            
+            @Override
+            public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
+                Simantics.applySCLRead(graph, exceptionCallback, t);
+            }
+            
+            @Override
+            public boolean isDisposed() {
+                return isDisposedCallback.apply(Tuple0.INSTANCE);
+            }
+        });
+    }
 
     public static Object possibleFromDynamic(Type expectedType, String moduleName, Object value) {
        
@@ -343,4 +328,25 @@ public class SCLFunctions {
         return value;
     }
 
+    public static void restrictQueries(ReadGraph graph, int amount, int step, int maxTimeInMs) {
+
+               QueryControl qc = graph.getService(QueryControl.class);
+               long start = System.currentTimeMillis();
+               while(true) {
+                       int current = qc.count();
+                       if(current < amount) return;
+                       qc.gc(graph, step);
+                       long duration = System.currentTimeMillis() - start;
+                       if(duration > maxTimeInMs) return;
+               }
+
+    }
+
+    public static int countQueries(ReadGraph graph) {
+
+               QueryControl qc = graph.getService(QueryControl.class);
+               return qc.count();
+
+    }
+    
 }