]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java
Merge branch 'private/db-threads' into private/threads-testing
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / ReadGraphImpl.java
index c6e41f6189dec87cd4022f5712c08e9701d2b58c..6998ee55e127afa761b7aad2499264bc816588d8 100644 (file)
@@ -26,6 +26,7 @@ import java.util.IdentityHashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
+import java.util.Map;
 import java.util.Set;
 import java.util.function.Consumer;
 
@@ -45,7 +46,6 @@ import org.simantics.databoard.util.binary.BinaryFile;
 import org.simantics.databoard.util.binary.RandomAccessBinary;
 import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.DevelopmentKeys;
-import org.simantics.db.ExternalValue;
 import org.simantics.db.ExternalValueSupport;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.RelationContext;
@@ -88,6 +88,7 @@ import org.simantics.db.common.primitiverequest.UniqueAdapter;
 import org.simantics.db.common.primitiverequest.Value;
 import org.simantics.db.common.primitiverequest.ValueImplied;
 import org.simantics.db.common.primitiverequest.VariantValueImplied;
+import org.simantics.db.common.procedure.BlockingAsyncProcedure;
 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
@@ -136,6 +137,7 @@ import org.simantics.db.impl.ResourceImpl;
 import org.simantics.db.impl.internal.RandomAccessValueSupport;
 import org.simantics.db.impl.internal.ResourceData;
 import org.simantics.db.impl.procedure.CallWrappedSingleQueryProcedure4;
+import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.impl.procedure.ResultCallWrappedQueryProcedure4;
 import org.simantics.db.impl.procedure.ResultCallWrappedSingleQueryProcedure4;
 import org.simantics.db.impl.query.CacheEntry;
@@ -144,6 +146,7 @@ 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;
+import org.simantics.db.impl.query.QueryProcessor.SessionTask;
 import org.simantics.db.impl.support.ResourceSupport;
 import org.simantics.db.procedure.AsyncListener;
 import org.simantics.db.procedure.AsyncMultiListener;
@@ -283,8 +286,17 @@ public class ReadGraphImpl implements ReadGraph {
 
                try {
 
-                       return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
-                                       id));
+//                     assert (id != null);
+//                     assert (procedure != null);
+//
+//                     processor.forResource(this, id, procedure);
+//
+////                   return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
+////                                   id));
+                       
+                       Integer rid = QueryCache.resultURIToResource(this, id, parent, null);
+                       if(rid == 0) throw new ResourceNotFoundException(id);
+                       return processor.querySupport.getResource(rid);
 
                } catch (ResourceNotFoundException e) {
 
@@ -314,8 +326,10 @@ public class ReadGraphImpl implements ReadGraph {
 
                try {
 
-                       return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
-                                       id));
+                       return getResource(id);
+                       
+//                     return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
+//                                     id));
 
                } catch (ResourceNotFoundException e) {
                        
@@ -336,6 +350,32 @@ public class ReadGraphImpl implements ReadGraph {
                }
 
        }
+       
+       @Override
+       public Map<String, Resource> getChildren(Resource resource) throws ValidationException, ServiceException {
+               
+               assert (resource != null);
+
+               try {
+
+                       int rId = processor.querySupport.getId(resource);
+                       return QueryCache.resultChildMap(this, rId, parent, null);
+
+               } catch (ValidationException e) {
+
+                       throw new ValidationException(e);
+
+               } catch (ServiceException e) {
+
+                       throw new ServiceException(e);
+
+               } catch (DatabaseException e) {
+
+                       throw new ServiceException(INTERNAL_ERROR_STRING, e);
+
+               }
+               
+       }
 
        final public Resource getRootLibrary() {
                return processor.getRootLibraryResource();
@@ -2050,10 +2090,12 @@ public class ReadGraphImpl implements ReadGraph {
                        throws DatabaseException {
 
                assert (request != null);
-               AsyncReadProcedure<T> procedure = new AsyncReadProcedure<T>();
-               syncRequest(request, procedure);
-               procedure.checkAndThrow();
-               return procedure.result;
+//             AsyncReadProcedure<T> procedure = new AsyncReadProcedure<T>();
+               BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, null, request);
+               syncRequest(request, ap);
+               return ap.get();
+//             procedure.checkAndThrow();
+//             return procedure.result;
                
 //             return syncRequest(request, new AsyncProcedureAdapter<T>());
 
@@ -2085,14 +2127,18 @@ public class ReadGraphImpl implements ReadGraph {
 
                ListenerBase listener = getListenerBase(procedure);
 
-               final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
-                               procedure, request);
+               BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
                
-               QueryCache.runnerAsyncReadEntry(this, request, parent, listener, wrapper);
+//             final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
+//                             procedure, request);
+               
+               QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
                
                //processor.query(this, request, parent, wrapper, listener);
                
-               return wrapper.getResult();
+               return ap.get();
+               
+//             return wrapper.getResult();
 
 //             if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
 //
@@ -2160,10 +2206,14 @@ public class ReadGraphImpl implements ReadGraph {
                ListenerBase listener = getListenerBase(procedure);
                assert(listener == null);
 
+               BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
+               
 //             final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
 //                             procedure, request);
 
-               QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure);
+               QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
+               
+               ap.get();
 
        }
 
@@ -5400,47 +5450,20 @@ public class ReadGraphImpl implements ReadGraph {
 
                assert (request != null);
                assert (procedure != null);
+       
+               processor.schedule(new SessionTask(false) {
 
-               final ListenerBase listener = getListenerBase(procedure);
-
-               if (parent != null || listener != null) {
-
-                       try {
-                               QueryCache.runnerReadEntry(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 {
-
-//                     final ReadGraphImpl newGraph = newSync();
-
-                       try {
-
-                               T result = request.perform(this);
-                               
-                               try {
-                                       procedure.execute(this, result);
-                               } catch (Throwable t) {
-                                       Logger.defaultLogError(t);
-                               }
-
-                       } catch (Throwable t) {
-
+                       @Override
+                       public void run(int thread) {
                                try {
-                                       procedure.exception(this, t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
+                                       final ListenerBase listener = getListenerBase(procedure);
+                                       QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
+                               } catch (DatabaseException e) {
+                                       Logger.defaultLogError(e);
                                }
-
-                       } finally {
-
                        }
-
-               }
+                       
+               });
 
        }
 
@@ -5512,38 +5535,19 @@ public class ReadGraphImpl implements ReadGraph {
                assert (request != null);
                assert (procedure != null);
 
-               final ListenerBase listener = getListenerBase(procedure);
-
-               if (parent != null || listener != null) {
-
-                       try {
-                               QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure);
-                               //processor.query(this, request, parent, procedure, listener);
-                       } catch (DatabaseException e) {
-                               Logger.defaultLogError(e);
-                       }
-
-               } else {
-
-                       try {
-                               
-                               request.perform(this, new CallWrappedSingleQueryProcedure4<T>(procedure, request));
-
-                       } catch (Throwable t) {
-
-                               if (t instanceof DatabaseException)
-                                       procedure.exception(this, t);
-                               else
-                                       procedure
-                                                       .exception(
-                                                                       this,
-                                                                       new DatabaseException(
-                                                                                       "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)",
-                                                                                       t));
+               processor.schedule(new SessionTask(false) {
 
+                       @Override
+                       public void run(int thread) {
+                               try {
+                                       final ListenerBase listener = getListenerBase(procedure);
+                                       QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
+                               } catch (DatabaseException e) {
+                                       Logger.defaultLogError(e);
+                               }
                        }
-
-               }
+                       
+               });
 
        }
 
@@ -6453,9 +6457,6 @@ 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());
        }
@@ -6486,14 +6487,6 @@ 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 {
@@ -6547,8 +6540,7 @@ public class ReadGraphImpl implements ReadGraph {
             for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
                 try {
                        if(L0.Functions_functionApplication.equals(converter)) {
-                               return AdaptValue.functionApplication;
-                               //return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
+                               return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
                        } else {
                                return graph.getValue2(converter, resource);
                        }
@@ -6604,28 +6596,11 @@ 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);
-                       return ev.getValue(this, r);
-                       //return (T)ReflectionUtils.getValue(getURI(r)).getValue();
-//             } catch(ValueNotFoundException e) {
-//                     throw new DatabaseException(e);
+                       return (T)ReflectionUtils.getValue(getURI(r)).getValue();
+               } catch(ValueNotFoundException e) {
+                       throw new DatabaseException(e);
                } catch(ClassCastException e) {
                        throw new DatabaseException(e);
                }
@@ -6816,5 +6791,10 @@ public class ReadGraphImpl implements ReadGraph {
     public Object getModificationCounter() {
        return processor.getSession().getModificationCounter();
     }
+
+       @Override
+       public boolean performPending() {
+               return processor.performPending(processor.thread.get());
+       }
     
 }