]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.issues.common/src/org/simantics/issues/common/ChildMaxIssueSeverity.java
Multiple readers in db client
[simantics/platform.git] / bundles / org.simantics.issues.common / src / org / simantics / issues / common / ChildMaxIssueSeverity.java
index 1103b5f262af788774072ef3d400ada1800fea51..80f22b7852e85f1ad968f10aec0ebc1666b51cc6 100644 (file)
  *******************************************************************************/
 package org.simantics.issues.common;
 
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
 
 import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.request.TernaryAsyncRead;
-import org.simantics.db.procedure.AsyncMultiProcedure;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.issues.Severity;
 
 /**
  * @author Tuukka Lehtonen
  */
-public class ChildMaxIssueSeverity extends TernaryAsyncRead<Resource, Resource, Set<Resource>, Severity>{
+public class ChildMaxIssueSeverity extends TernaryAsyncRead<Resource, Resource, Set<Resource>, Severity> {
 
+    static class AsyncReadResult<T> {
+        private AtomicReference<T> resultRef;
+        private Throwable throwable;
+        private AtomicInteger counter = new AtomicInteger(1);
+        private AsyncProcedure<T> procedure;
+        AsyncReadResult(AsyncProcedure<T> procedure, AtomicReference<T> resultRef) {
+            this.procedure = procedure;
+            this.resultRef = resultRef;
+        }
+        void except(AsyncReadGraph graph, Throwable throwable) {
+            this.throwable = throwable;
+            dec(graph);
+        }
+        void set(AsyncReadGraph graph, T result) {
+            resultRef.set(result);
+            dec(graph);
+        }
+        void inc() {
+            counter.incrementAndGet();
+        }
+        void dec(AsyncReadGraph graph) {
+            if(counter.decrementAndGet() == 0) {
+                if(throwable != null)
+                    procedure.exception(graph, throwable);
+                else
+                    procedure.execute(graph, resultRef.get());
+            }
+        }
+        
+    }
+    
     public ChildMaxIssueSeverity(Resource resource, Resource childRelation, Set<Resource> typesToRecurse) {
         super(resource, childRelation, typesToRecurse);
     }
 
-//    @Override
-//    public Severity perform(ReadGraph graph) throws DatabaseException {
-//        Severity maxSeverity = null;
-//        //System.out.println("severityForChildren: " + NameUtils.getSafeName(graph, resource));
-//        for (Resource child : graph.getObjects(resource, resource2)) {
-//            Severity s = graph.syncRequest(new MaxIssueSeverityRecursive(child));
-//            maxSeverity = Severity.moreSevere(maxSeverity, s);
-//        }
-//        //System.out.println("severityForChildren: " + NameUtils.getSafeName(graph, resource) + " : " + maxSeverity);
-//        return maxSeverity;
-//    }
-
     @Override
     public void perform(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {
-        //System.out.println(getClass().getSimpleName() + ": " + parameter);
         
-        graph.forTypes(parameter, new AsyncProcedure<Set<Resource>>() {
-            @Override
-            public void execute(AsyncReadGraph graph, Set<Resource> result) {
-                if (!Collections.disjoint(parameter3, result)) {
-                    checkChildren(graph, procedure);
-                } else {
-                    procedure.execute(graph, null);
-                }
-            }
-            @Override
-            public void exception(AsyncReadGraph graph, Throwable throwable) {
-                procedure.exception(graph, throwable);
+        try {
+            Set<Resource> types = graph.getTypes(parameter);
+            if (!Collections.disjoint(parameter3, types)) {
+                checkChildren(graph, procedure);
+            } else {
+                procedure.execute(graph, null);
             }
-        });
+        } catch (DatabaseException e) {
+            procedure.exception(graph, e);
+        }
+        
     }
 
     protected void checkChildren(AsyncReadGraph graph, final AsyncProcedure<Severity> procedure) {
-        graph.forEachObject(parameter, parameter2, new AsyncMultiProcedure<Resource>() {
-            AtomicReference<Severity> maxSeverity = new AtomicReference<Severity>();
-            @Override
-            public void execute(AsyncReadGraph graph, Resource child) {
+        
+        AsyncReadResult<Severity> maxSeverity = new AsyncReadResult<Severity>(procedure, new AtomicReference<Severity>());
+        
+        try {
+            Collection<Resource> children = graph.getObjects(parameter, parameter2);
+            for(Resource child : children) {
+                maxSeverity.inc();
                 graph.asyncRequest(new MaxIssueSeverityRecursive(child, parameter2, parameter3), new AsyncProcedure<Severity>() {
                     @Override
                     public void execute(AsyncReadGraph graph, Severity severity) {
                         if (severity != null) {
                             synchronized (maxSeverity) {
-                                maxSeverity.set(Severity.moreSevere(maxSeverity.get(), severity));
+                                maxSeverity.set(graph, Severity.moreSevere(maxSeverity.resultRef.get(), severity));
                             }
+                        } else {
+                            maxSeverity.dec(graph);
                         }
                     }
                     @Override
                     public void exception(AsyncReadGraph graph, Throwable throwable) {
-                        procedure.exception(graph, throwable);
+                        maxSeverity.except(graph, throwable);
                     }
                 });
             }
-            @Override
-            public void finished(AsyncReadGraph graph) {
-                procedure.execute(graph, maxSeverity.get());
-            }
-            @Override
-            public void exception(AsyncReadGraph graph, Throwable throwable) {
-                procedure.exception(graph, throwable);
-            }
-        });
+            maxSeverity.dec(graph);
+        } catch (DatabaseException e) {
+            maxSeverity.except(graph, e);
+            return;
+        }
+        
     }
 
 }