]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ReadEntry.java
index edc32454929542ab76a0f4a51155559a52b22324..8abbb86c8745499246b311b5648a7dcc499eecf9 100644 (file)
@@ -20,7 +20,7 @@ import org.simantics.db.request.Read;
 import org.simantics.db.request.ReadExt;
 import org.simantics.db.request.RequestFlags;
 
-final public class ReadEntry<T> extends CacheEntryBase {
+final public class ReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
 
        protected Read<T> request;
 
@@ -44,26 +44,14 @@ final public class ReadEntry<T> extends CacheEntryBase {
        setResult(null);
     }
        
-    final public void addOrSet(AsyncReadGraph graph, Object item) {
+    final public Object addOrSet(AsyncReadGraph graph, Object result) {
 
        assert(assertPending());
        
-//        ArrayList<Pair<AsyncProcedure<T>, AsyncBarrier>> p = null;
-
-       synchronized(this) {
-           
-            setResult(item);
-               setReady();
-//             p = procs;
-//             procs = null;
-               
-       }
-
-//        if(p != null)
-//             for(Pair<AsyncProcedure<T>, AsyncBarrier> proc : p) {
-//                     proc.first.execute(graph, (T)item);
-//                     proc.second.dec();
-//             }
+       setResult(result);
+       setReady();
+       
+       return result;
         
     }
 
@@ -73,17 +61,10 @@ final public class ReadEntry<T> extends CacheEntryBase {
         return new Query() {
 
                        @Override
-                       public void recompute(ReadGraphImpl graph_, Object provider, CacheEntry entry) {
-                               
-                               QueryProcessor qp = (QueryProcessor)provider;
+                       public void recompute(ReadGraphImpl graph) {
                                
-                       WriteGraphImpl write = qp.getCore().getSession().getService(WriteGraphImpl.class);
-                               
-                       ReadGraphImpl graph = write.newSync(entry);
-
                                try {
 
-                                       entry.setPending();
                                    T result = request.perform(graph);
                                    addOrSet(graph, result);
 
@@ -97,7 +78,7 @@ final public class ReadEntry<T> extends CacheEntryBase {
 
                        @Override
                        public void removeEntry(QueryProcessor processor) {
-                               processor.cache.readMap.remove(request);
+                               processor.cache.remove(ReadEntry.this);
                        }
 
                        @Override
@@ -119,7 +100,56 @@ final public class ReadEntry<T> extends CacheEntryBase {
         
     }
     
-       public Object performFromCache(ReadGraphImpl graph, Object provider,    Object procedure) {
+    @Override
+    public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+
+               ReadGraphImpl queryGraph = graph.withParent(this);
+
+               try {
+
+                       addOrSet(queryGraph, request.perform(queryGraph));
+                       return get(queryGraph, procedure);
+
+               }  catch (DatabaseException e) {
+
+                       except(e);
+                       return get(graph, procedure);
+
+               }  catch (Throwable t) {
+
+                       except(new DatabaseException(t));
+                       return get(graph, procedure);
+
+               }
+       
+    }
+
+    public static <T> void computeForEach(ReadGraphImpl graph, Read<T> request, ReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
+       
+               ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph;
+
+               try {
+
+                       T result = request.perform(queryGraph);
+                       if(entry != null) entry.addOrSet(queryGraph, result);
+                       procedure.execute(graph, result);
+
+               }  catch (DatabaseException e) {
+
+                       if(entry != null) entry.except(e);
+                       procedure.exception(graph, e);
+
+               }  catch (Throwable t) {
+
+                       DatabaseException dbe = new DatabaseException(t);
+                       if(entry != null) entry.except(dbe);
+                       procedure.exception(graph, dbe);
+
+               }
+       
+    }
+    
+       public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
            
         AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
 
@@ -149,8 +179,8 @@ final public class ReadEntry<T> extends CacheEntryBase {
                else return request.toString() + " - " + statusOrException;
        }
        
-       public Object get(ReadGraphImpl graph, QueryProcessor processor, Object procedure) throws DatabaseException {
-               if(procedure != null) performFromCache(graph, processor, procedure);
+       public Object get(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+               if(procedure != null) performFromCache(graph, procedure);
                checkAndThrow();
                return getResult();
        }