]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ExternalReadEntry.java
index 18d91dc0618643e092aa4d242f6a50545cbf6c6e..d61049744a3cbdbdb09c85040833ffc0e8329d91 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.ArrayList;
 import java.util.LinkedList;
 
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.DebugPolicy;
 import org.simantics.db.impl.graph.ReadGraphImpl;
-import org.simantics.db.procedure.Procedure;
+import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.request.ExternalRead;
 import org.simantics.db.request.RequestFlags;
 
-final public class ExternalReadEntry<T> extends CacheEntryBase {
+final public class ExternalReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
 
     final LinkedList<T> items = new LinkedList<T>();
-    
-//    public ArrayList<Procedure<T>> procs;
 
     protected ExternalRead<T> request;
 
@@ -47,6 +46,15 @@ final public class ExternalReadEntry<T> extends CacheEntryBase {
         request = null;
         super.discard();
     }
+
+    @Override
+    public void setPending() {
+        //if(result != NO_RESULT) {
+            //new Exception("result = " + result).printStackTrace();
+        //}
+        statusOrException = PENDING;
+        result = REQUIRES_COMPUTATION;
+    }
     
     public ExternalReadEntry(ExternalRead<T> request) {
         assert request != null;
@@ -67,7 +75,7 @@ final public class ExternalReadEntry<T> extends CacheEntryBase {
         
             assert(isPending());
 
-            ArrayList<Procedure<T>> p = null;
+            //ArrayList<Procedure<T>> p = null;
 
             synchronized(this) {
 
@@ -89,43 +97,53 @@ final public class ExternalReadEntry<T> extends CacheEntryBase {
         
     }
     
+    @Override
+    public void except(Throwable t) {
+        if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: excepted " + this);
+        if(statusOrException != DISCARDED) {
+            statusOrException = EXCEPTED;
+            result = t;
+        } else {
+            result = t;
+        }
+        assert(isExcepted());
+    }
+    
+    @Override
+    public void setResult(Object result) {
+        super.setResult(result);
+        assert(!(result instanceof Throwable));
+        assert(!isExcepted());
+    }
+
     @Override
     final public Query getQuery() {
        
         return new Query() {
 
                        @Override
-                       public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-
-                           final QueryProcessor qp = (QueryProcessor)provider;
-                           synchronized(items) {
-
-                               if(entry.isExcepted()) {
-                                       
-                                       // Exception persists
-                                       
-                               } else {
-                               
-                                       // Update
-                                       if(!items.isEmpty()) {
-                                               setResult(items.removeFirst());
-                                       }
-                                       // Reschedule
-                                           if(!items.isEmpty()) {
-                                               qp.updatePrimitive(request);
-                                           }
-                                           
+                       public void recompute(ReadGraphImpl graph) {
+
+                               synchronized(items) {
+
+
+                                       // Update
+                                       if(!items.isEmpty()) {
                                            setReady();
-                                           
-                               }
-                               
-                           }
+                                               setResult(items.removeFirst());
+                                       }
+                                       // Reschedule
+                                       if(!items.isEmpty()) {
+                                               graph.processor.updatePrimitive(request);
+                                       }
+
+                               }
                                
                        }
 
                        @Override
                        public void removeEntry(QueryProcessor processor) {
-                               processor.externalReadMap.remove(request);
+                               processor.cache.remove(ExternalReadEntry.this);
                        }
 
                        @Override
@@ -143,36 +161,29 @@ final public class ExternalReadEntry<T> extends CacheEntryBase {
         
     }
 
-       public void performFromCache(Object procedure) {
-               
-        Procedure<T> proc = (Procedure<T>)procedure;
+       @Override
+       public String toString() {
+               if(request == null) return "DISCARDED ExternalRead " + System.identityHashCode(this);
+               else return request.toString() + " " + + System.identityHashCode(this);
+       }
+
+    @Override
+    public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
+       
+        AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
 
            if(isExcepted()) {
             
-            proc.exception((Throwable)getResult());
+            proc.exception(graph, (Throwable)getResult());
             
         } else {
             
-            proc.execute((T)getResult());
+            proc.execute(graph, (T)getResult());
 
         }
-               
-       }
-
-       @Override
-       public String toString() {
-               if(request == null) return "DISCARDED ExternalRead " + System.identityHashCode(this);
-               else return request.toString() + " " + + System.identityHashCode(this);
-       }
-
-    @Override
-    public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-        performFromCache(procedure);
-    }
-    
-    @Override
-    public void setReady() {
-       super.setReady();
+           
+           return getResult();
+       
     }
     
     @Override
@@ -180,4 +191,8 @@ final public class ExternalReadEntry<T> extends CacheEntryBase {
        // Do nothing - the state is already set and cannot be recomputed on demand
     }
 
+    public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
+        return graph.processor.cache.performQuery(graph, request, this, procedure);
+    }
+
 }