]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java
Some enhancements made by Antti for multiple readers
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryCacheBase.java
index f75cd9dcd80b790727383a5d078f84f9c374c994..d6924c708a9738fa1cd517fc686f1cb461369cc7 100644 (file)
@@ -16,6 +16,7 @@ import org.simantics.db.procedure.AsyncMultiProcedure;
 import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.procedure.Listener;
 import org.simantics.db.procedure.ListenerBase;
+import org.simantics.db.procedure.Procedure;
 import org.simantics.db.request.AsyncMultiRead;
 import org.simantics.db.request.AsyncRead;
 import org.simantics.db.request.ExternalRead;
@@ -200,9 +201,9 @@ public class QueryCacheBase {
                                        
                                        // Just for safety
                                        if(entry.isDiscarded()) return;
-                                       if(entry.isExcepted()) entry.setPending();
                                        
                                        if(used.compareAndSet(false, true)) {
+                           //entry.setPending();
                                                entry.addOrSet(parentGraph.processor, result);
                                                procedure.execute(parentGraph, result);
                                        } else {
@@ -945,6 +946,39 @@ public class QueryCacheBase {
                
     }
 
+    static class ExternalProcedureWrapper<T> implements AsyncProcedure<T> {
+        
+        private Procedure<T> procedure;
+        private T result = null;
+        private Throwable throwable = null;
+        
+        ExternalProcedureWrapper(Procedure<T> procedure) {
+            this.procedure = procedure;
+        }
+
+        @Override
+        public void execute(AsyncReadGraph graph, T result) {
+            if(procedure != null) procedure.execute(result);
+            this.result = result;
+        }
+
+        @Override
+        public void exception(AsyncReadGraph graph, Throwable throwable) {
+            if(procedure != null) procedure.exception(throwable);
+            this.throwable = throwable;
+        }
+        
+        public T get() throws DatabaseException {
+            if(throwable != null) {
+                if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
+                else throw new DatabaseException(throwable);
+            } else {
+                return result;
+            }
+        }
+        
+    }
+
     
     static class InternalProcedureWrapper<T> implements InternalProcedure<T> {
        
@@ -1058,6 +1092,12 @@ public class QueryCacheBase {
 
     }
 
+    public static <T> T resultExternalReadEntry(ReadGraphImpl graph, ExternalRead r, CacheEntry parent, ListenerBase listener, Procedure<T> procedure) throws DatabaseException {
+        ExternalProcedureWrapper<T> wrap = new ExternalProcedureWrapper<>(procedure);
+        QueryCache.runnerExternalReadEntry(graph, r, parent, listener, wrap);
+        return wrap.get();
+    }
+
     public static <T> T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
        AsyncProcedureWrapper<T> wrap = new AsyncProcedureWrapper<>(procedure);
         QueryCache.runnerReadEntry(graph, r, parent, listener, wrap);