]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java
Some enhancements made by Antti for multiple readers
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / ReadGraphImpl.java
index e3e7d50ecbc04998d62a650a9695ce939b6667ac..c6e41f6189dec87cd4022f5712c08e9701d2b58c 100644 (file)
@@ -112,6 +112,7 @@ import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
 import org.simantics.db.common.request.AdaptValue;
 import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.utils.Functions;
 import org.simantics.db.common.utils.Logger;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.common.validation.L0Validations;
@@ -139,6 +140,7 @@ import org.simantics.db.impl.procedure.ResultCallWrappedQueryProcedure4;
 import org.simantics.db.impl.procedure.ResultCallWrappedSingleQueryProcedure4;
 import org.simantics.db.impl.query.CacheEntry;
 import org.simantics.db.impl.query.QueryCache;
+import org.simantics.db.impl.query.QueryCacheBase;
 import org.simantics.db.impl.query.QueryProcessor;
 import org.simantics.db.impl.query.QuerySupport;
 import org.simantics.db.impl.query.TripleIntProcedure;
@@ -179,6 +181,7 @@ import org.simantics.scl.compiler.types.Type;
 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
 import org.simantics.scl.reflection.ReflectionUtils;
 import org.simantics.scl.reflection.ValueNotFoundException;
+import org.simantics.scl.runtime.function.Function1;
 import org.simantics.scl.runtime.function.Function3;
 import org.simantics.utils.DataContainer;
 import org.simantics.utils.Development;
@@ -2031,7 +2034,7 @@ public class ReadGraphImpl implements ReadGraph {
                                        throw (DatabaseException) exception;
                                else
                                        throw new DatabaseException(
-                                                       "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
+                                                       "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
                                                        exception);
                        }
                }
@@ -2526,28 +2529,38 @@ public class ReadGraphImpl implements ReadGraph {
        final public <T> T syncRequest(final ExternalRead<T> request,
                        final Procedure<T> procedure) throws DatabaseException {
 
-               assert (request != null);
-
-               ListenerBase listener = getListenerBase(procedure);
-               
-               final DataContainer<Throwable> exception = new DataContainer<Throwable>();
-               final DataContainer<T> result = new DataContainer<T>();
+        assert (request != null);
 
-               processor.query(this, request, parent, new Procedure<T>() {
+        ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
+        return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
 
-                       @Override
-                       public void exception(Throwable throwable) {
-                               exception.set(throwable);
-                               procedure.exception(throwable);
-                       }
-
-                       @Override
-                       public void execute(T t) {
-                               result.set(t);
-                               procedure.execute(t);
-                       }
-
-               }, listener);
+//         
+//         ListenerBase listener = getListenerBase(procedure);
+//         
+//         return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
+           
+//             assert (request != null);
+//
+//             ListenerBase listener = getListenerBase(procedure);
+//             
+//             final DataContainer<Throwable> exception = new DataContainer<Throwable>();
+//             final DataContainer<T> result = new DataContainer<T>();
+//
+//             processor.query(this, request, parent, new Procedure<T>() {
+//
+//                     @Override
+//                     public void exception(Throwable throwable) {
+//                             exception.set(throwable);
+//                             procedure.exception(throwable);
+//                     }
+//
+//                     @Override
+//                     public void execute(T t) {
+//                             result.set(t);
+//                             procedure.execute(t);
+//                     }
+//
+//             }, listener);
                
 
 
@@ -2581,18 +2594,18 @@ public class ReadGraphImpl implements ReadGraph {
 //                     }
 //
 //             }
-
-               Throwable t = exception.get();
-               if (t != null) {
-                       if (t instanceof DatabaseException)
-                               throw (DatabaseException) t;
-                       else
-                               throw new DatabaseException(
-                                               "Unexpected exception in ReadGraph.syncRequest(Read)",
-                                               t);
-               }
-
-               return result.get();            
+//
+//             Throwable t = exception.get();
+//             if (t != null) {
+//                     if (t instanceof DatabaseException)
+//                             throw (DatabaseException) t;
+//                     else
+//                             throw new DatabaseException(
+//                                             "Unexpected exception in ReadGraph.syncRequest(Read)",
+//                                             t);
+//             }
+//
+//             return result.get();            
 
        }
 
@@ -5761,56 +5774,125 @@ public class ReadGraphImpl implements ReadGraph {
        final public <T> void asyncRequest(final ExternalRead<T> request,
                        final Procedure<T> procedure) {
 
-               assert (request != null);
-               assert (procedure != null);
-
-               ListenerBase listener = getListenerBase(procedure);
-
-               if (parent != null || listener != null) {
-
-                       processor.query(this, request, parent, procedure, listener);
-
-               } else {
-
-                       try {
+           
+        assert (request != null);
+        assert (procedure != null);
 
-                               request.register(this, new Listener<T>() {
+        final ListenerBase listener = getListenerBase(procedure);
 
-                                       @Override
-                                       public void execute(T result) {
-                                               procedure.execute(result);
-                                       }
+        if (parent != null || listener != null) {
 
-                                       @Override
-                                       public void exception(Throwable t) {
-                                               procedure.exception(t);
-                                       }
+            try {
+                QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
+                //processor.query(this, request, parent, procedure,listener);
+            } catch (DatabaseException e) {
+                Logger.defaultLogError(e);
+                // This throwable has already been transferred to procedure at this point - do nothing about it
+                //
+            }
+            
+        } else {
 
-                                       @Override
-                                       public String toString() {
-                                               return "asyncRequest(PrimitiveRead) -> " + request;
-                                       }
+            request.register(this, new Listener<T>() {
+                //
+                @Override
+                public void execute(T result) {
+                    procedure.execute(result);
+                }
 
-                                       @Override
-                                       public boolean isDisposed() {
-                                               return true;
-                                       }
+                @Override
+                public void exception(Throwable t) {
+                    procedure.exception(t);
+                }
 
-                               });
+                @Override
+                public String toString() {
+                    return "asyncRequest(PrimitiveRead) -> " + request;
+                }
 
-                       } catch (Throwable t) {
+                @Override
+                public boolean isDisposed() {
+                    return true;
+                }
 
-                               if (t instanceof DatabaseException)
-                                       procedure.exception(t);
-                               else
-                                       procedure
-                                                       .exception(new DatabaseException(
-                                                                       "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)",
-                                                                       t));
+            });            
+            
+//          final ReadGraphImpl newGraph = newSync();
 
-                       }
+//            try {
+//
+//                T result = request.perform(this);
+//                
+//                try {
+//                    procedure.execute(this, result);
+//                } catch (Throwable t) {
+//                    Logger.defaultLogError(t);
+//                }
+//
+//            } catch (Throwable t) {
+//
+//                try {
+//                    procedure.exception(this, t);
+//                } catch (Throwable t2) {
+//                    Logger.defaultLogError(t2);
+//                }
+//
+//            } finally {
+//
+//            }
 
-               }
+        }
+           
+//             assert (request != null);
+//             assert (procedure != null);
+//
+//             ListenerBase listener = getListenerBase(procedure);
+//
+//             if (parent != null || listener != null) {
+//
+//                     processor.query(this, request, parent, procedure, listener);
+//
+//             } else {
+//
+//                     try {
+//
+//                             request.register(this, new Listener<T>() {
+//
+//                                     @Override
+//                                     public void execute(T result) {
+//                                             procedure.execute(result);
+//                                     }
+//
+//                                     @Override
+//                                     public void exception(Throwable t) {
+//                                             procedure.exception(t);
+//                                     }
+//
+//                                     @Override
+//                                     public String toString() {
+//                                             return "asyncRequest(PrimitiveRead) -> " + request;
+//                                     }
+//
+//                                     @Override
+//                                     public boolean isDisposed() {
+//                                             return true;
+//                                     }
+//
+//                             });
+//
+//                     } catch (Throwable t) {
+//
+//                             if (t instanceof DatabaseException)
+//                                     procedure.exception(t);
+//                             else
+//                                     procedure
+//                                                     .exception(new DatabaseException(
+//                                                                     "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)",
+//                                                                     t));
+//
+//                     }
+//
+//             }
 
        }
 
@@ -6371,6 +6453,9 @@ public class ReadGraphImpl implements ReadGraph {
                        return getValue(r);
                }
        }
+        else if(types.contains(L0.Function)) {
+            return Functions.exec(this, r, this, r, context);
+        }
        else if(types.contains(L0.ExternalValue)) {
                return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
        }
@@ -6401,6 +6486,14 @@ public class ReadGraphImpl implements ReadGraph {
                 return getVariantValue(r);
             }
         }
+        else if(types.contains(L0.Function)) {
+            Object value = Functions.exec(this, r, this, r, context);
+            try {
+                return new Variant(Bindings.OBJECT.getContentBinding(value), value);
+            } catch ( org.simantics.databoard.binding.error.BindingException e ) {
+                throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
+            }
+        }
         else if(types.contains(L0.ExternalValue)) {
             Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
             try {
@@ -6511,6 +6604,21 @@ public class ReadGraphImpl implements ReadGraph {
                } else {
                        return getValue(r, binding);
                }
+       } else if(types.contains(L0.Function)) {
+            Object value = Functions.exec(this, r, this, r, context);
+            if(binding.isInstance(value)) return (T)value;
+            try {
+                Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
+                return (T)Bindings.adapt(value, srcBinding, binding);
+            } catch(RuntimeException e) {
+                DatabaseException dte = findPossibleRootException(e);
+                if(dte != null) throw dte;
+                else throw new DatabaseException(e);
+            } catch (AdaptException e) {
+                throw new DatabaseException(e);
+            } catch (org.simantics.databoard.binding.error.BindingException e) {
+                throw new DatabaseException(e);
+            }
        } else if(types.contains(L0.ExternalValue)) {
                try {
                        ExternalValue ev = adapt(r, ExternalValue.class);
@@ -6662,16 +6770,22 @@ public class ReadGraphImpl implements ReadGraph {
         }
     }
     
+    private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
+        protected Boolean initialValue() {
+            return true;
+        }
+    };
+
     @Override
     public boolean setSynchronous(boolean value) {
-        boolean old = processor.synch;
-        processor.synch = value;
+        boolean old = getSynchronous();
+        syncGraph.set(value);
         return old;
     }
     
     @Override
     public boolean getSynchronous() {
-        return processor.synch;
+        return syncGraph.get();
     }
     
     public void ensureLoaded(int resource) {