]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java
Multiple readers in db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / ReadGraphImpl.java
index a693b29228e026c1e1f6a5c7f3652c8d4320eb4c..2eced2b7c046ebef86a64760cee27106667c706c 100644 (file)
@@ -1926,9 +1926,7 @@ public class ReadGraphImpl implements AsyncReadGraph {
        public <T> T syncRequest(final Read<T> request) throws DatabaseException {
                assert (request != null);
 
-               ITask task = ThreadLogger.task(request);
                T result = (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
-               task.finish();
                return result;
                
        }
@@ -1950,10 +1948,8 @@ public class ReadGraphImpl implements AsyncReadGraph {
 
                assert (request != null);
 
-               ITask task = ThreadLogger.task(request);
                ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
-               T result = QueryCache.resultReadEntry(this, request, parent, listener, procedure);
-               task.finish();
+               T result = (T)QueryCache.runnerReadEntry(this, request, parent, listener, procedure, true);
                return result;
 
        }
@@ -2038,10 +2034,8 @@ public class ReadGraphImpl implements AsyncReadGraph {
 
                assert (request != null);
 
-               ITask task = ThreadLogger.task(request);
                ListenerBase listener = getListenerBase(procedure);
                T result = (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true); 
-               task.finish();
                return result;
 
        }
@@ -5160,7 +5154,11 @@ public class ReadGraphImpl implements AsyncReadGraph {
 
                assert (request != null);
                assert (procedure != null);
-       
+               
+               AsyncBarrierImpl barrier = asyncBarrier;
+               if(barrier != null)
+                   barrier.inc();
+               
                processor.scheduleNow(new SessionTask(this) {
 
                        @Override
@@ -5170,6 +5168,9 @@ public class ReadGraphImpl implements AsyncReadGraph {
                                        QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
                                } catch (DatabaseException e) {
                                        Logger.defaultLogError(e);
+                               } finally {
+                               if(barrier != null)
+                                   barrier.dec();
                                }
                        }
                        
@@ -5241,29 +5242,39 @@ public class ReadGraphImpl implements AsyncReadGraph {
                assert (request != null);
                assert (procedure != null);
 
-               ITask task = ThreadLogger.task(request);
+        AsyncBarrierImpl barrier = asyncBarrier;
+        if(barrier != null)
+            barrier.inc();
 
                processor.scheduleNow(new SessionTask(this) {
 
                        @Override
                        public void run0(int thread) {
-                               try {
+
+                           if(barrier != null)
+                           barrier.inc();
+
+                           try {
                                        final ListenerBase listener = getListenerBase(procedure);
                                        QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, new AsyncProcedure<T>() {
 
                                                @Override
                                                public void execute(AsyncReadGraph graph, T result) {
-                                                       task.finish();
                                                        procedure.execute(graph, result);
+                                               if(barrier != null)
+                                                   barrier.dec();
                                                }
 
                                                @Override
                                                public void exception(AsyncReadGraph graph, Throwable throwable) {
-                                                       task.finish();
                                                        procedure.exception(graph, throwable);
+                            if(barrier != null)
+                                barrier.dec();
                                                }
                                                
                                        }, false);
+                               if(barrier != null)
+                                   barrier.dec();
                                } catch (DatabaseException e) {
                                        Logger.defaultLogError(e);
                                }
@@ -5651,27 +5662,47 @@ public class ReadGraphImpl implements AsyncReadGraph {
                this.parentGraph = parentGraph;
                this.parent = parent;
                this.processor = support;
-               this.asyncBarrier = new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent);
+               this.asyncBarrier = new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, null);
        }
 
+    ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support, AsyncBarrierImpl asyncBarrier) {
+        this.parentGraph = parentGraph;
+        this.parent = parent;
+        this.processor = support;
+        this.asyncBarrier = asyncBarrier;
+    }
+
        ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
                this(graph, parent, graph.processor);
        }
 
+    ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback) {
+        this(parentGraph, parent, parentGraph.processor, new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, callback));
+    }
+
        ReadGraphImpl(ReadGraphImpl graph) {
                this(graph, graph.parent);
        }
 
-       public ReadGraphImpl withParent(CacheEntry parent) {
-               return new ReadGraphImpl(this, parent);
+       public ReadGraphImpl withParent(CacheEntry parent, Runnable callback) {
+               return new ReadGraphImpl(this, parent, callback);
        }
 
+    public ReadGraphImpl withParent(CacheEntry parent) {
+        return withParent(parent, null);
+    }
+
+       public ReadGraphImpl syncWithParent(CacheEntry parent) {
+        return new ReadGraphImpl(this, parent, processor, null);
+    }
+
        public ReadGraphImpl forRecompute(CacheEntry parent) {
                return new ReadGraphImpl(null, parent, processor);
        }
 
        public static ReadGraphImpl create(QueryProcessor support) {
-               return new ReadGraphImpl(null, null, support);
+           ReadGraphImpl result = new ReadGraphImpl(null, null, support);
+           return result;
        }
 
        public ReadGraphImpl newRestart(ReadGraphImpl impl) {
@@ -6357,6 +6388,12 @@ public class ReadGraphImpl implements AsyncReadGraph {
         else return 1 + getLevelStatic(impl.parentGraph);
     }
     
+    public boolean isParent(ReadGraphImpl impl) {
+        if(impl == null) return false;
+        if(this == impl) return true;
+        return isParent(impl.parentGraph);
+    }
+    
     public ReadGraphImpl getTopLevelGraph() {
         return getTopLevelGraphStatic(this);
     }