]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncMultiReadEntry.java
QueryListening sync is slow
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AsyncMultiReadEntry.java
index 9e4f49dbf9dc5ccff8aa1052c0d2eea47fef0896..ea88d179107c920f9f10193f727907a56d39356e 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
-\r
-import org.simantics.db.AsyncReadGraph;\r
-import org.simantics.db.common.exception.DebugException;\r
-import org.simantics.db.impl.graph.ReadGraphImpl;\r
-import org.simantics.db.procedure.AsyncMultiProcedure;\r
-import org.simantics.db.request.AsyncMultiRead;\r
-import org.simantics.db.request.RequestFlags;\r
-\r
-final public class AsyncMultiReadEntry<T> extends CacheEntryBase {\r
-\r
-//    public ArrayList<AsyncMultiProcedure<T>> procs = null;\r
-\r
-    protected AsyncMultiRead<T> request;\r
-    \r
-    public AsyncMultiReadEntry(AsyncMultiRead<T> request) {\r
-       this.request = request;\r
-    }\r
-    \r
-    @Override\r
-    int makeHash() {\r
-       return request.hashCode();\r
-    }\r
-    \r
-    @Override\r
-    public Object getOriginalRequest() {\r
-        return request;\r
-    }\r
-    \r
-    @Override\r
-    public void discard() {\r
-       super.discard();\r
-       request = null;\r
-       setResult(null);\r
-    }\r
-    \r
-    final synchronized public void finish(AsyncReadGraph graph) {\r
-       \r
-//     new Exception("finish " + this).printStackTrace();\r
-       \r
-       if(!isPending()) {\r
-               System.err.println("aff");\r
-       }\r
-       \r
-       assert(isPending());\r
-\r
-//        ArrayList<AsyncMultiProcedure<T>> p = null;\r
-\r
-        synchronized(this) {\r
-\r
-               setReady();\r
-//            p = procs;\r
-//            procs = null; \r
-        \r
-        }\r
-        \r
-//        if(p != null) {\r
-//             ArrayList<T> v = (ArrayList<T>)getResult();\r
-//             if(v != null) {\r
-//                 for(AsyncMultiProcedure<T> proc : p) {\r
-//                     for(T t : v) proc.execute(graph, t);\r
-//                 }\r
-//             }\r
-//             \r
-//             for(AsyncMultiProcedure<T>  proc : p) proc.finished(graph);\r
-//        }\r
-        \r
-    }\r
-\r
-    final synchronized public void except(AsyncReadGraph graph, Throwable t) {\r
-\r
-       assert(isPending());\r
-\r
-//        ArrayList<AsyncMultiProcedure<T>> p = null;\r
-\r
-        synchronized(this) {\r
-\r
-               except(t);\r
-//            p = procs;\r
-//            procs = null; \r
-        \r
-        }\r
-        \r
-//        if(p != null) {\r
-//             for(AsyncMultiProcedure<T>  proc : p) proc.exception(graph, t);\r
-//        }\r
-        \r
-    }\r
-\r
-    final synchronized public void addOrSet(Object item) {\r
-\r
-       assert(isPending());\r
-       \r
-        ArrayList<T> value = (ArrayList<T>)getResult(); \r
-        value.add((T)item);\r
-        \r
-    }\r
-    \r
-    @Override\r
-    public void clearResult(QuerySupport support) {\r
-       setResult(new ArrayList<T>());\r
-    }\r
-    \r
-    @Override\r
-    final public Query getQuery() {\r
-       \r
-        return new Query() {\r
-\r
-                       @Override\r
-                       public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {\r
-                               \r
-                               QueryProcessor qp = (QueryProcessor)provider;\r
-\r
-                               final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); \r
-\r
-                               try {\r
-\r
-                                   request.perform(parentGraph , new AsyncMultiProcedure<T>() {\r
-\r
-                        @Override\r
-                        public void execute(AsyncReadGraph graph, T result) {\r
-                            addOrSet(result);\r
-                        }\r
-                        \r
-                        public void finished(AsyncReadGraph graph) {\r
-                               finish(graph);\r
-                        };\r
-                                       \r
-                                       @Override\r
-                                       public void exception(AsyncReadGraph graph, Throwable t) {\r
-                            except(t);\r
-                           }\r
-\r
-                    });\r
-\r
-                               } catch (Throwable t) {\r
-                    except(t);\r
-                    if(DebugException.DEBUG) new DebugException(t).printStackTrace();\r
-                }\r
-                               \r
-                       }\r
-\r
-                       @Override\r
-                       public void removeEntry(QueryProcessor processor) {\r
-                       processor.asyncMultiReadMap.remove(request);\r
-                       }\r
-\r
-                       @Override\r
-                       public int type() {\r
-                               return RequestFlags.INVALIDATE;\r
-                       }\r
-                       \r
-                       @Override\r
-                       public String toString() {\r
-                               if(request == null) return "DISCARDED";\r
-                               else return request.toString() + statusOrException;\r
-                       }\r
-               \r
-        };\r
-        \r
-    }\r
-\r
-       @Override\r
-       public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {\r
-               \r
-               final AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;\r
-\r
-        if(isExcepted()) {\r
-\r
-            try {\r
-                proc.exception(graph, (Throwable)getResult());\r
-            } catch (Throwable t) {\r
-                t.printStackTrace();\r
-            }\r
-            \r
-            \r
-        } else {\r
-            \r
-            final ArrayList<T> values = (ArrayList<T>)getResult();\r
-            for(T value : values) {\r
-                try {\r
-                    proc.execute(graph, value);\r
-                } catch (Throwable t) {\r
-                    t.printStackTrace();\r
-                }\r
-            }\r
-\r
-            try {\r
-                proc.finished(graph);\r
-            } catch (Throwable t) {\r
-                t.printStackTrace();\r
-            }\r
-\r
-        }\r
-               \r
-               \r
-               \r
-       }\r
-\r
-//     @Override\r
-//     public void performFromCache(int callerThread, Object provider,\r
-//                     Object procedure) {\r
-//\r
-//         QueryProvider2 queryProvider = (QueryProvider2)provider;\r
-//         ReadGraphImpl graph = ReadGraphImpl.forFromCache(callerThread, null, new ReadGraphSupportImpl(null, queryProvider, null));\r
-//             performFromCache(graph, provider, procedure);\r
-//             \r
-//     }\r
-       \r
-       @Override\r
-       public String toString() {\r
-               if(request == null) return "DISCARDED";\r
-               else return request.toString() + statusOrException;\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 java.util.ArrayList;
+
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.BlockingAsyncMultiProcedure;
+import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.procedure.AsyncMultiProcedure;
+import org.simantics.db.request.AsyncMultiRead;
+import org.simantics.db.request.RequestFlags;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final public class AsyncMultiReadEntry<T> extends CacheEntryBase<AsyncMultiProcedure<T>> {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(AsyncMultiReadEntry.class);
+
+    protected AsyncMultiRead<T> id;
+    
+    AsyncMultiReadEntry(AsyncMultiRead<T> request) {
+       this.id = request;
+    }
+    
+    @Override
+    int makeHash() {
+       return id.hashCode();
+    }
+    
+    @Override
+    public Object getOriginalRequest() {
+        return id;
+    }
+    
+    @Override
+    public void discard() {
+       super.discard();
+       id = null;
+       setResult(null);
+    }
+    
+    final synchronized public void finish(AsyncReadGraph graph) {
+       
+       assert(isPending());
+
+        synchronized(this) {
+               setReady();
+        }
+        
+    }
+
+    final synchronized public void except(AsyncReadGraph graph, Throwable t) {
+
+       assert(isPending());
+
+        synchronized(this) {
+               except(t);
+        }
+        
+    }
+
+    @SuppressWarnings("unchecked")
+       final synchronized public void addOrSet(Object item) {
+
+       assert(isPending());
+       
+        ArrayList<T> value = (ArrayList<T>)getResult(); 
+        value.add((T)item);
+        
+    }
+    
+    @Override
+    public void clearResult(QuerySupport support) {
+       setResult(new ArrayList<T>());
+    }
+    
+    @Override
+    final public Query getQuery() {
+       
+        return new Query() {
+
+                       @Override
+                       public void recompute(ReadGraphImpl graph) {
+
+                               try {
+
+                                       BlockingAsyncMultiProcedure<T> proc = new BlockingAsyncMultiProcedure<>(graph.asyncBarrier, graph, new AsyncMultiProcedure<T>() {
+
+                                               @Override
+                                               public void execute(AsyncReadGraph graph, T result) {
+                                                       addOrSet(result);
+                                               }
+
+                                               public void finished(AsyncReadGraph graph) {
+                                                       finish(graph);
+                                               };
+
+                                               @Override
+                                               public void exception(AsyncReadGraph graph, Throwable t) {
+                                                       except(t);
+                                               }
+
+                                       }, id);
+
+                                       id.perform(graph , proc);
+
+                                       proc.get();
+
+                               } catch (Throwable t) {
+                                       
+                                       except(t);
+                                       
+                               }
+                               
+                       }
+
+                       @Override
+                       public void removeEntry(QueryProcessor processor) {
+                       processor.cache.remove(AsyncMultiReadEntry.this);
+                       }
+
+                       @Override
+                       public int type() {
+                               return RequestFlags.INVALIDATE;
+                       }
+                       
+                       @Override
+                       public String toString() {
+                               if(id == null) return "DISCARDED";
+                               else return id.toString() + statusOrException;
+                       }
+               
+        };
+        
+    }
+
+       @SuppressWarnings("unchecked")
+       @Override
+       public Object performFromCache(ReadGraphImpl graph, AsyncMultiProcedure<T> proc) {
+
+        if(isExcepted()) {
+
+            try {
+                proc.exception(graph, (Throwable)getResult());
+            } catch (Throwable t) {
+                LOGGER.error("performFromCache proc.exception failed", t);
+            }
+
+        } else {
+
+            final ArrayList<T> values = (ArrayList<T>)getResult();
+            for(T value : values) {
+                try {
+                    proc.execute(graph, value);
+                } catch (Throwable t) {
+                    LOGGER.error("performFromCache proc.execute failed", t);
+                }
+            }
+
+            try {
+                proc.finished(graph);
+            } catch (Throwable t) {
+                LOGGER.error("performFromCache proc.finished failed", t);
+            }
+
+        }
+               
+               return getResult();
+               
+       }
+       
+       @Override
+       public String toString() {
+               if(id == null) return "DISCARDED";
+               else return id.toString() + statusOrException;
+       }
+
+       public Object compute(ReadGraphImpl graph, AsyncMultiProcedure<T> procedure) throws DatabaseException {
+               return graph.processor.cache.performQuery(graph, id, this, procedure);
+       }
+
+}