]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java
QueryListening sync is slow
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / DirectObjects.java
index f42a1ef31793788942db11bd041b940ee6f52ca6..9bcb6ddb8b4c61400194e2cec39aceb514b9c908 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.impl.query;\r
-\r
-import java.util.Collection;\r
-import java.util.concurrent.Semaphore;\r
-\r
-import org.simantics.db.common.exception.DebugException;\r
-import org.simantics.db.impl.graph.ReadGraphImpl;\r
-import org.simantics.db.procedure.ListenerBase;\r
-import org.simantics.db.request.RequestFlags;\r
-\r
-final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {\r
-\r
-       private DirectObjects(final int r1, final int r2) {\r
-               super(r1, r2);\r
-       }\r
-\r
-       @Override\r
-       public int type() {\r
-               return RequestFlags.INVALIDATE;\r
-       }\r
-       \r
-    @Override\r
-    public void clearResult(QuerySupport support) {\r
-       setResult(INVALID_RESULT);\r
-    }\r
-\r
-       final static DirectObjects entry(final QueryProcessor provider, final int r1, final int r2) {\r
-\r
-               return (DirectObjects)provider.directObjectsMap.get(id(r1,r2));\r
-\r
-       }\r
-       \r
-       final static Collection<DirectObjects> entries(final QueryProcessor processor, final int r1) {\r
-               DoubleKeyQueryHashMap<IntProcedure> hash = processor.directObjectsMap;\r
-               return hash.values(r1);\r
-       }\r
-\r
-       final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {\r
-\r
-       QueryProcessor processor = graph.processor;\r
-               \r
-               DirectObjects entry = (DirectObjects)processor.directObjectsMap.get(id(r1,r2));\r
-               if(entry == null) {\r
-                       \r
-               entry = new DirectObjects(r1, r2);\r
-               entry.setPending();\r
-               entry.clearResult(processor.querySupport);\r
-               entry.putEntry(processor);\r
-               \r
-                       processor.performForEach(graph, entry, parent, listener, procedure);\r
-                       \r
-               } else {\r
-                       \r
-            if(entry.isPending()) {\r
-                synchronized(entry) {\r
-                    if(entry.isPending()) {\r
-                        processor.registerDependencies(graph, entry, parent, listener, procedure, false);\r
-                                               entry.computeForEach(graph, processor, procedure, true);\r
-                        return;\r
-                    }\r
-                }\r
-            }\r
-               \r
-                       processor.performForEach(graph, entry, parent, listener, procedure);\r
-                       \r
-               }\r
-\r
-       }\r
-\r
-       final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {\r
-\r
-        assert(r1 != 0);\r
-        assert(r2 != 0);\r
-        \r
-        if(parent == null && listener == null) {\r
-               DirectObjects.computeForEach(graph, r1, r2, null, procedure);\r
-        } else {\r
-               runner(graph, r1, r2, parent, listener, procedure);\r
-        }\r
-\r
-       }\r
-\r
-       @Override\r
-       public BinaryQuery<IntProcedure> getEntry(QueryProcessor provider) {\r
-               return provider.directObjectsMap.get(id);\r
-       }\r
-\r
-       @Override\r
-       public void putEntry(QueryProcessor provider) {\r
-               provider.directObjectsMap.put(id, this);\r
-       }\r
-\r
-       @Override\r
-       final public void removeEntry(QueryProcessor provider) {\r
-               provider.directObjectsMap.remove(id);\r
-       }\r
-\r
-       @Override\r
-       public void computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure procedure, final boolean store) {\r
-               computeForEach(graph, r1(), r2(), this, procedure);\r
-       }\r
-\r
-       static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, final IntProcedure procedure) {\r
-\r
-               QueryProcessor processor = graph.processor;\r
-               \r
-               processor.querySupport.ensureLoaded(graph, r1, r2);\r
-\r
-               processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {\r
-\r
-                       @Override\r
-                       public void execute(ReadGraphImpl graph, int i) {\r
-                               procedure.execute(graph, i);\r
-                       }\r
-\r
-                       @Override\r
-                       public void finished(ReadGraphImpl graph) {\r
-                       }\r
-\r
-                       @Override\r
-                       public void exception(ReadGraphImpl graph, Throwable t) {\r
-                               if(DebugException.DEBUG) new DebugException(t).printStackTrace();\r
-                       }\r
-\r
-               });\r
-\r
-               if(entry != null) entry.finish(graph, processor);\r
-               procedure.finished(graph);\r
-\r
-       }\r
-\r
-       @Override\r
-       public String toString() {\r
-               return "DirectObjects[" + r1() + " - " + r2() + "]";\r
-       }\r
-\r
-       @Override\r
-       public void setReady() {\r
-               statusOrException = READY;\r
-       }\r
-       \r
-       final private void finish(ReadGraphImpl graph, QueryProcessor provider) {\r
-               setReady();\r
-       }\r
-\r
-       @Override\r
-       public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {\r
-\r
-               assert(isReady());\r
-               computeForEach(graph, provider, procedure, false);\r
-\r
-       }\r
-\r
-       @Override\r
-       public void recompute(ReadGraphImpl graph, QueryProcessor provider) {\r
-\r
-        final Semaphore s = new Semaphore(0);\r
-               \r
-               computeForEach(graph, provider, new IntProcedure() {\r
-\r
-                       @Override\r
-                       public void finished(ReadGraphImpl graph) {\r
-                               s.release();\r
-                       }\r
-\r
-                       @Override\r
-                       public void exception(ReadGraphImpl graph, Throwable t) {\r
-                               throw new Error("Error in recompute.", t);\r
-                       }\r
-\r
-                       @Override\r
-                       public void execute(ReadGraphImpl graphd, int i) {\r
-                       }\r
-\r
-               }, true);\r
-\r
-        while(!s.tryAcquire()) {\r
-               provider.resume(graph);\r
-        }\r
-\r
-       }\r
-\r
-    @Override\r
-    boolean isImmutable(ReadGraphImpl graph) {\r
-       return graph.processor.isImmutable(r1());\r
-    }\r
-       \r
-}\r
-\r
+/*******************************************************************************
+ * 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
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.impl.query;
+
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.request.RequestFlags;
+
+final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> implements IntProcedure {
+
+    DirectObjects(final int r1, final int r2) {
+        super(r1, r2);
+    }
+
+    @Override
+    public int type() {
+        return RequestFlags.INVALIDATE;
+    }
+
+    @Override
+    public final void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
+    }
+
+    // @Override
+    public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
+        computeForEach(graph, r1(), r2(), this, procedure);
+        return getResult();
+    }
+
+    public static void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry,
+            final IntProcedure procedure_) throws DatabaseException {
+
+        IntProcedure procedure = entry != null ? entry : procedure_;
+
+        QueryProcessor processor = graph.processor;
+
+        processor.querySupport.ensureLoaded(graph, r1, r2);
+
+        processor.querySupport.getObjects(graph, r1, r2, procedure);
+
+        procedure.finished(graph);
+
+        if (entry != null)
+            entry.performFromCache(graph, procedure_);
+
+    }
+
+    @Override
+    public String toString() {
+        return "DirectObjects[" + r1() + " - " + r2() + "]";
+    }
+
+    @Override
+    public void setReady() {
+        statusOrException = READY;
+    }
+
+    @Override
+    public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
+
+        assert (isReady());
+
+        if (handleException(graph, procedure))
+            return getResult();
+
+        final IntArray value = (IntArray) getResult();
+        if (value.data == null) {
+            if (value.sizeOrData != IntArray.NO_DATA)
+                procedure.execute(graph, value.sizeOrData);
+        } else {
+            for (int i = 0; i < value.sizeOrData; i++)
+                procedure.execute(graph, value.data[i]);
+        }
+
+        procedure.finished(graph);
+
+        return value;
+
+    }
+
+    @Override
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
+
+        compute(graph, new IntProcedure() {
+
+            @Override
+            public void finished(ReadGraphImpl graph) {
+            }
+
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) {
+                throw new Error("Error in recompute.", t);
+            }
+
+            @Override
+            public void execute(ReadGraphImpl graphd, int i) {
+            }
+
+        });
+
+    }
+
+    @Override
+    boolean isImmutable(ReadGraphImpl graph) {
+        return graph.processor.isImmutable(r1());
+    }
+
+    @Override
+    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+        IntArray value = (IntArray) getResult();
+        value.add(i);
+    }
+
+    @Override
+    public void finished(ReadGraphImpl graph) throws DatabaseException {
+        setReady();
+    }
+
+    @Override
+    public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
+    }
+
+}