]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/MultiReadEntry.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / MultiReadEntry.java
index d0bb436d74e88e994a66fa9e27474f5a31139bfd..6b7415c9780f7a45c715068c83c4f02f1fe1fb29 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * 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
@@ -14,21 +14,24 @@ package org.simantics.db.impl.query;
 import java.util.ArrayList;
 
 import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
 import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
-import org.simantics.db.impl.query.QueryProcessor.AsyncBarrier;
 import org.simantics.db.procedure.AsyncMultiProcedure;
+import org.simantics.db.procedure.SyncMultiProcedure;
 import org.simantics.db.request.MultiRead;
 import org.simantics.db.request.RequestFlags;
-import org.simantics.utils.datastructures.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-final public class MultiReadEntry<T> extends CacheEntryBase {
+public final class MultiReadEntry<T> extends CacheEntryBase<SyncMultiProcedure<T>> {
 
-//    public ArrayList<Pair<AsyncMultiProcedure<T>, AsyncBarrier>> procs;
+    private static final Logger LOGGER = LoggerFactory.getLogger(MultiReadEntry.class);
 
     protected MultiRead<T> request;
     
-    public MultiReadEntry(MultiRead<T> request) {
+    MultiReadEntry(MultiRead<T> request) {
        this.request = request;
     }
 
@@ -49,34 +52,14 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
        setResult(null);
     }
     
-    synchronized public void finish(AsyncReadGraph graph) {
+    synchronized public void finish(ReadGraph graph) {
        
        assert(isPending());
 
-        ArrayList<Pair<AsyncMultiProcedure<T>, AsyncBarrier>> p = null;
-
-        synchronized(this) {
-
+       synchronized(this) {
                setReady();
-               
-//            p = procs;
-//            procs = null; 
-        
         }
         
-//        if(p != null) {
-//             ArrayList<T> v = (ArrayList<T>)getResult();
-//             if(v != null) {
-//                     for(Pair<AsyncMultiProcedure<T>, AsyncBarrier> pair : p) {
-//                             for(T t : v) pair.first.execute(graph, t);
-//                     }
-//             }
-//             for(Pair<AsyncMultiProcedure<T>, AsyncBarrier> pair : p) {
-//                     pair.first.finished(graph);
-//                     pair.second.dec();
-//             }
-//        }
-        
     }
 
     @Override
@@ -99,41 +82,30 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
         return new Query() {
 
                        @Override
-                       public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
+                       public void recompute(ReadGraphImpl graph) {
                                
-                               QueryProcessor qp = (QueryProcessor)provider;
-
-                               final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); 
-//                parentGraph.state.barrier.inc();
-
                                try {
 
-                                   request.perform(parentGraph , new AsyncMultiProcedure<T>() {
+                                   request.perform(graph , new SyncMultiProcedure<T>() {
 
                         @Override
-                        public void execute(AsyncReadGraph graph, T result) {
+                        public void execute(ReadGraph graph, T result) {
                             addOrSet(result);
-//                            parentGraph.state.barrier.dec();
                         }
                         
-                        public void finished(AsyncReadGraph graph) {
+                        public void finished(ReadGraph graph) {
                                finish(graph);
-//                            parentGraph.state.barrier.dec();
                         };
                                        
                                        @Override
-                                       public void exception(AsyncReadGraph graph, Throwable t) {
+                                       public void exception(ReadGraph graph, Throwable t) {
                             except(t);
-//                            parentGraph.state.barrier.dec();
                            }
 
                     });
 
-//                                     parentGraph.waitAsync(request);
-
                                } catch (Throwable t) {
                     except(t);
-//                    parentGraph.state.barrier.dec();
                     if(DebugException.DEBUG) new DebugException(t).printStackTrace();
                 }
                                
@@ -141,7 +113,7 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
 
                        @Override
                        public void removeEntry(QueryProcessor processor) {
-                       processor.multiReadMap.remove(request);
+                       processor.cache.remove(MultiReadEntry.this);
                        }
 
                        @Override
@@ -168,7 +140,7 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
             try {
                 proc.exception(graph, (Throwable)getResult());
             } catch (Throwable t) {
-                t.printStackTrace();
+                LOGGER.error("performFromCache proc.exception failed", t);
             }
 //            parentBarrier.dec();
             
@@ -179,33 +151,30 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
                 try {
                     proc.execute(graph, value);
                 } catch (Throwable t) {
-                    t.printStackTrace();
+                    LOGGER.error("performFromCache proc.execute failed", t);
                 }
             }
 
             try {
                 proc.finished(graph);
             } catch (Throwable t) {
-                t.printStackTrace();
+                LOGGER.error("performFromCache proc.finished failed", t);
             }
 //            parentBarrier.dec();
 
         }
         
     }
-    
-       @Override
-       public void performFromCache(ReadGraphImpl graph, Object provider,
-                       Object procedure) {
-               
-               final AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
+
+    @Override
+    public Object performFromCache(ReadGraphImpl graph, SyncMultiProcedure<T> proc) {
 
         if(isExcepted()) {
             
             try {
                 proc.exception(graph, (Throwable)getResult());
             } catch (Throwable t) {
-                t.printStackTrace();
+                LOGGER.error("performFromCache(Sync) proc.exception failed", t);
             }
             
         } else {
@@ -215,26 +184,30 @@ final public class MultiReadEntry<T> extends CacheEntryBase {
                 try {
                     proc.execute(graph, value);
                 } catch (Throwable t) {
-                    t.printStackTrace();
+                    LOGGER.error("performFromCache(Sync) proc.execute failed", t);
                 }
             }
 
             try {
                 proc.finished(graph);
             } catch (Throwable t) {
-                t.printStackTrace();
+                LOGGER.error("performFromCache(Sync) proc.finished failed", t);
             }
 
         }
-               
-               
-               
+
+        return null;
+
        }
-       
+
        @Override
        public String toString() {
                if(request == null) return "DISCARDED";
                else return request.toString() + statusOrException;
        }
 
+       public Object compute(ReadGraphImpl graph, SyncMultiProcedure<T> procedure) throws DatabaseException {
+               return graph.processor.cache.performQuery(graph, request, this, procedure);
+       }
+
 }