]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/OrderedSet.java
QueryListening sync is slow
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / OrderedSet.java
index e940a3bad037f5a9188a4a17c3e2dc5a298be5e1..e850e96f1bf6b94745b5946c5bcf578312715d42 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.ArrayList;\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.impl.procedure.IntProcedureAdapter;\r
-import org.simantics.db.procedure.ListenerBase;\r
-\r
-final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {\r
-       \r
-//     public ArrayList<IntProcedure> procs = null;\r
-\r
-       public OrderedSet(final int r) {\r
-        super(r);\r
-    }\r
-\r
-    final static OrderedSet entry(final QueryProcessor provider, final int r) {\r
-        \r
-        return (OrderedSet)provider.orderedSetMap.get(r);\r
-\r
-    }\r
-       \r
-    final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, OrderedSet cached, final CacheEntry parent, ListenerBase listener, final IntProcedure procedure) {\r
-        \r
-       OrderedSet entry = cached != null ? cached : (OrderedSet)provider.orderedSetMap.get(r); \r
-        if(entry == null) {\r
-               \r
-               entry = new OrderedSet(r);\r
-               entry.setPending();\r
-               entry.clearResult(provider.querySupport);\r
-               entry.putEntry(provider);\r
-               \r
-            provider.performForEach(graph, entry, parent, listener, procedure);\r
-            \r
-        } else {\r
-\r
-            if(entry.isPending()) {\r
-               synchronized(entry) {\r
-                    if(entry.isPending()) {\r
-                        throw new IllegalStateException();\r
-//                     if(entry.procs == null) entry.procs = new ArrayList<IntProcedure>();\r
-//                     entry.procs.add(procedure);\r
-//                        provider.registerDependencies(graph, entry, parent, listener, procedure, false);\r
-//                     return;\r
-                    }\r
-               }\r
-            }\r
-            provider.performForEach(graph, entry, parent, listener, procedure);\r
-        }\r
-\r
-    }\r
-    \r
-    final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {\r
-        \r
-       assert(r != 0);\r
-       \r
-       final OrderedSet entry = (OrderedSet)provider.orderedSetMap.get(r);\r
-       \r
-       if(parent == null && !(listener != null)) {\r
-               if(entry != null && entry.isReady()) { \r
-                       entry.performFromCache(graph, provider, procedure);\r
-                       return;\r
-               }\r
-        }\r
-\r
-        runner(graph, r, provider, entry, parent, listener, procedure);\r
-         \r
-    }\r
-\r
-    @Override\r
-    public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {\r
-        return provider.orderedSetMap.get(id);\r
-    }\r
-       \r
-       @Override\r
-       public void putEntry(QueryProcessor provider) {\r
-           provider.orderedSetMap.put(id, this);\r
-       }\r
-\r
-       @Override\r
-       final public void removeEntry(QueryProcessor provider) {\r
-        provider.orderedSetMap.remove(id);\r
-       }\r
-    \r
-       private int current = 0;\r
-\r
-       private boolean nextElement(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {\r
-           \r
-               provider.querySupport.ensureLoaded(graph, current);             \r
-               \r
-               boolean found = provider.querySupport.getObjects(graph, current, id, new IntProcedure() {\r
-\r
-                       @Override\r
-                       public void execute(ReadGraphImpl graph, int i) {\r
-                               if(i != id) {\r
-                                       addOrSet(i);\r
-                                       procedure.execute(graph, i);\r
-                               }\r
-                               current = i;\r
-                       }\r
-\r
-                       @Override\r
-                       public void exception(ReadGraphImpl graph, Throwable t) {\r
-                               if(DebugException.DEBUG) new DebugException(t).printStackTrace();\r
-                               procedure.exception(graph, t);\r
-                       }\r
-\r
-                       @Override\r
-                       public void finished(ReadGraphImpl graph) {\r
-                       }\r
-\r
-               });\r
-\r
-               if(!found) current = id;\r
-\r
-               if(current == id) {\r
-                       finish(graph, provider);\r
-                       procedure.finished(graph);\r
-                       return false;\r
-               } else {\r
-                       return true;\r
-               }\r
-\r
-       }\r
-       \r
-    @Override\r
-    public void clearResult(QuerySupport support) {\r
-        current = id;\r
-        setResult(new IntArray());\r
-    }\r
-       \r
-    @Override\r
-    public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {\r
-\r
-        while(nextElement(graph, provider, procedure, store));\r
-        \r
-        return getResult();\r
-        \r
-    }\r
-    \r
-    @Override\r
-    public String toString() {\r
-       return "OrderedSet[" + id + "]";\r
-    }\r
-\r
-    final private void finish(ReadGraphImpl graph, QueryProcessor provider) {\r
-        \r
-       assert(isPending());\r
-\r
-//        ArrayList<IntProcedure> p = null;\r
-\r
-        synchronized(this) {\r
-\r
-               setReady();\r
-//            p = procs;\r
-//            procs = null; \r
-        \r
-        }\r
-        \r
-//        if(p != null) {\r
-//             IntArray v = (IntArray)getResult();\r
-//             if(v.data == null) {\r
-//                 if(v.sizeOrData != IntArray.NO_DATA) {\r
-//                     for(IntProcedure proc : p) proc.execute(graph, v.sizeOrData);\r
-//                 }\r
-//             } else {\r
-//                 for(IntProcedure proc : p) {\r
-//                     for(int i = 0;i < v.sizeOrData ; i++) proc.execute(graph, v.data[i]);\r
-//                 }\r
-//             }\r
-//             for(IntProcedure proc : p) proc.finished(graph);\r
-//        }\r
-\r
-    }\r
-\r
-    final public void addOrSet(int add) {\r
-        \r
-        assert(isPending());\r
-        \r
-        IntArray value = (IntArray)getResult();\r
-        value.add(add);\r
-        \r
-    }\r
-\r
-    @Override\r
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {\r
-       \r
-       assert(isReady());\r
-\r
-       if(handleException(graph, procedure)) return EXCEPTED;\r
-       \r
-        final IntArray value = (IntArray)getResult();\r
-        if(value.data == null) {\r
-            if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);\r
-        } else {\r
-            for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);\r
-        }\r
-\r
-        procedure.finished(graph);\r
-        \r
-        return value;\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 IntProcedureAdapter() {\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
-        }, true);\r
-        \r
-        while(!s.tryAcquire()) {\r
-               provider.resume(graph);\r
-        }\r
-        \r
-    }\r
-    \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 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 java.util.concurrent.atomic.AtomicInteger;
+
+import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.graph.ReadGraphImpl;
+
+final public class OrderedSet extends CollectionUnaryQuery {
+       
+       public OrderedSet(final int r) {
+        super(r);
+    }
+
+       @Override
+       final public void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
+       }
+
+       private static int nextElement(ReadGraphImpl graph, int current, int orderedSet, OrderedSet parent, final IntProcedure procedure) throws DatabaseException {
+           
+               QueryProcessor processor = graph.processor;
+               
+               processor.querySupport.ensureLoaded(graph, current);
+               
+               AtomicInteger res = new AtomicInteger(0);
+               
+               processor.querySupport.getObjects(graph, current, orderedSet, new IntProcedure() {
+
+                       @Override
+                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                               if(i != orderedSet) {
+                                       procedure.execute(graph, i);
+                               }
+                               res.set(i);
+                       }
+
+                       @Override
+                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                               if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                               procedure.exception(graph, t);
+                       }
+
+                       @Override
+                       public void finished(ReadGraphImpl graph) {
+                       }
+
+               });
+
+               if(res.get() == orderedSet) {
+                       procedure.finished(graph);
+               }
+               
+               return res.get();
+
+       }
+       
+    @Override
+    public void compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
+        computeForEach(graph, id, this, procedure);
+    }
+
+    static void computeForEach(ReadGraphImpl graph, int orderedSet, final OrderedSet entry, final IntProcedure procedure_) throws DatabaseException {
+        
+        IntProcedure procedure = entry != null ? entry : procedure_;
+
+       int current = nextElement(graph, orderedSet, orderedSet, entry, procedure);
+       while(current != orderedSet) {
+               current = nextElement(graph, current, orderedSet, entry, procedure);
+       }
+
+       if(entry != null) entry.performFromCache(graph, procedure_);
+       
+    }
+    
+    @Override
+    public String toString() {
+       return "OrderedSet[" + id + "]";
+    }
+    
+}