]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java
QueryListening sync is slow
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ValueQuery.java
index 7e4b2e18aae5e27bbd04e744cdd18f0e0ecc8e9e..ad4017f9cff863c0a2bbe42a4fb9458a655e57f2 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.concurrent.Semaphore;\r
-\r
-import org.simantics.db.impl.graph.ReadGraphImpl;\r
-import org.simantics.db.impl.procedure.InternalProcedure;\r
-import org.simantics.db.procedure.ListenerBase;\r
-\r
-final public class ValueQuery extends UnaryQuery<InternalProcedure<byte[]>> {\r
-    \r
-    private ValueQuery(final int resource) {\r
-        super(resource);\r
-    }\r
-    \r
-    final static ValueQuery entry(final QueryProcessor provider, final int r) {\r
-        return (ValueQuery)provider.valueMap.get(r);\r
-    }\r
-\r
-    final static byte[] runner(final ReadGraphImpl graph, final int r, CacheEntry parent, final ListenerBase listener, final InternalProcedure<byte[]> procedure) {\r
-\r
-       QueryProcessor processor = graph.processor;\r
-       \r
-       ValueQuery entry = (ValueQuery)processor.valueMap.get(r); \r
-        if(entry == null) {\r
-               \r
-               entry = new ValueQuery(r);\r
-               entry.setPending();\r
-               entry.clearResult(processor.querySupport);\r
-               entry.putEntry(processor);\r
-               \r
-               return (byte[])processor.performForEach(graph, entry, parent, listener, procedure);\r
-            \r
-        } else {\r
-               \r
-               return (byte[])processor.performForEach(graph, entry, parent, listener, procedure);\r
-            \r
-        }\r
-\r
-    }\r
-    \r
-    final public static byte[] queryEach(ReadGraphImpl graph, final int r, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<byte[]> procedure) {\r
-        \r
-       assert(r != 0);\r
-       \r
-       if(graph.parent == null && listener == null) {\r
-               return ValueQuery.computeForEach(graph, r, null, procedure);\r
-        } else {\r
-               return runner(graph, r, parent, listener, procedure);\r
-        }\r
-         \r
-    }\r
-\r
-    final public static byte[] queryEach(ReadGraphImpl graph, final int r, final CacheEntry parent) {\r
-        \r
-        assert(r != 0);\r
-        \r
-        if(graph.parent == null) {\r
-            return ValueQuery.computeForEach(graph, r);\r
-        } else {\r
-            return runner(graph, r, parent, null, null);\r
-        }\r
-         \r
-    }\r
-    \r
-       @Override\r
-       public UnaryQuery<InternalProcedure<byte[]>> getEntry(QueryProcessor provider) {\r
-        return provider.valueMap.get(id);\r
-       }\r
-       \r
-       @Override\r
-       public void putEntry(QueryProcessor provider) {\r
-        provider.valueMap.put(id, this);\r
-       }\r
-\r
-       @Override\r
-       final public void removeEntry(QueryProcessor provider) {\r
-               provider.valueMap.remove(id);\r
-       }\r
-       \r
-               \r
-       public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure<byte[]> procedure) {\r
-\r
-               graph.ensureLoaded(r);\r
-               \r
-               byte[] value = graph.getValue(r);\r
-               if(entry != null) {\r
-                       entry.setResult(value);\r
-                       entry.setReady();\r
-               }\r
-               if(procedure != null) {\r
-                       procedure.execute(graph, value);\r
-               }\r
-               \r
-               return value;\r
-               \r
-       }\r
-\r
-    public static byte[] computeForEach(ReadGraphImpl graph, final int r) {\r
-\r
-        graph.ensureLoaded(r);\r
-        \r
-        return graph.getValue(r);\r
-        \r
-    }\r
-       \r
-       @Override\r
-       public Object computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final InternalProcedure<byte[]> procedure, final boolean store) {\r
-               return computeForEach(graph, id, this, procedure);\r
-    }\r
-    \r
-    @Override\r
-    public String toString() {\r
-       return "Value[" + id + "]";\r
-    }\r
-\r
-    @Override\r
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor queryProvider, InternalProcedure<byte[]> procedure) {\r
-       return computeForEach(graph, queryProvider, procedure, false);\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 InternalProcedure<byte[]>() {\r
-\r
-            @Override\r
-            public void execute(ReadGraphImpl graph, byte[] result) {\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
-        }, 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(id);\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.impl.procedure.InternalProcedure;
+
+public final class ValueQuery extends UnaryQueryP<byte[]> {
+
+    ValueQuery(int resource) {
+        super(resource);
+    }
+
+    @Override
+    public final void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
+    }
+
+    @Override
+    public void compute(ReadGraphImpl graph, InternalProcedure<byte[]> procedure) throws DatabaseException {
+        computeForEach(graph, id, this, procedure);
+    }
+
+    public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure<byte[]> procedure_) throws DatabaseException {
+
+        InternalProcedure<byte[]> procedure = entry != null ? entry : procedure_;
+
+        graph.ensureLoaded(r);
+
+        byte[] value = graph.getValue(r);
+        if(procedure != null) procedure.execute(graph, value);
+
+        if(entry != null && procedure_ != null) entry.performFromCache(graph, procedure_);
+
+        return value;
+
+    }
+
+    @Override
+    public String toString() {
+        return "Value[" + id + "]";
+    }
+
+}