]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / DirectObjects.java
index 2c106476ef263cf36c1a23835d7bc5fbd24152e0..9bcb6ddb8b4c61400194e2cec39aceb514b9c908 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.Collection;
-import java.util.concurrent.Semaphore;
-
-import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
-import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
 
-final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
+final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> implements IntProcedure {
 
-       private DirectObjects(final int r1, final int r2) {
-               super(r1, r2);
-       }
+    DirectObjects(final int r1, final int r2) {
+        super(r1, r2);
+    }
 
-       @Override
-       public int type() {
-               return RequestFlags.INVALIDATE;
-       }
-       
     @Override
-    public void clearResult(QuerySupport support) {
-       setResult(INVALID_RESULT);
+    public int type() {
+        return RequestFlags.INVALIDATE;
     }
 
-       final static DirectObjects entry(final QueryProcessor provider, final int r1, final int r2) {
-
-               return (DirectObjects)provider.directObjectsMap.get(id(r1,r2));
-
-       }
-       
-       final static Collection<DirectObjects> entries(final QueryProcessor processor, final int r1) {
-               DoubleKeyQueryHashMap<IntProcedure> hash = processor.directObjectsMap;
-               return hash.values(r1);
-       }
-
-       final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-
-       QueryProcessor processor = graph.processor;
-               
-               DirectObjects entry = (DirectObjects)processor.directObjectsMap.get(id(r1,r2));
-               if(entry == null) {
-                       
-               entry = new DirectObjects(r1, r2);
-               entry.setPending();
-               entry.clearResult(processor.querySupport);
-               entry.putEntry(processor);
-               
-                       processor.performForEach(graph, entry, parent, listener, procedure);
-                       
-               } else {
-                       
-            if(entry.isPending()) {
-                synchronized(entry) {
-                    if(entry.isPending()) {
-                        processor.registerDependencies(graph, entry, parent, listener, procedure, false);
-                                               entry.computeForEach(graph, processor, procedure, true);
-                        return;
-                    }
-                }
-            }
-               
-                       processor.performForEach(graph, entry, parent, listener, procedure);
-                       
-               }
-
-       }
-
-       final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
+    @Override
+    public final void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
+    }
 
-        assert(r1 != 0);
-        assert(r2 != 0);
-        
-        if(parent == null && listener == null) {
-               DirectObjects.computeForEach(graph, r1, r2, null, procedure);
-        } else {
-               runner(graph, r1, r2, parent, listener, procedure);
-        }
+    // @Override
+    public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
+        computeForEach(graph, r1(), r2(), this, procedure);
+        return getResult();
+    }
 
-       }
+    public static void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry,
+            final IntProcedure procedure_) throws DatabaseException {
 
-       @Override
-       public BinaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return provider.directObjectsMap.get(id);
-       }
+        IntProcedure procedure = entry != null ? entry : procedure_;
 
-       @Override
-       public void putEntry(QueryProcessor provider) {
-               provider.directObjectsMap.put(id, this);
-       }
+        QueryProcessor processor = graph.processor;
 
-       @Override
-       final public void removeEntry(QueryProcessor provider) {
-               provider.directObjectsMap.remove(id);
-       }
+        processor.querySupport.ensureLoaded(graph, r1, r2);
 
-       @Override
-       public void computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure procedure, final boolean store) {
-               computeForEach(graph, r1(), r2(), this, procedure);
-       }
+        processor.querySupport.getObjects(graph, r1, r2, procedure);
 
-       static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, final IntProcedure procedure) {
+        procedure.finished(graph);
 
-               QueryProcessor processor = graph.processor;
-               
-               processor.querySupport.ensureLoaded(graph, r1, r2);
+        if (entry != null)
+            entry.performFromCache(graph, procedure_);
 
-               processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
+    }
 
-                       @Override
-                       public void execute(ReadGraphImpl graph, int i) {
-                               procedure.execute(graph, i);
-                       }
+    @Override
+    public String toString() {
+        return "DirectObjects[" + r1() + " - " + r2() + "]";
+    }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                       }
+    @Override
+    public void setReady() {
+        statusOrException = READY;
+    }
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                       }
+    @Override
+    public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
 
-               });
+        assert (isReady());
 
-               if(entry != null) entry.finish(graph, processor);
-               procedure.finished(graph);
+        if (handleException(graph, procedure))
+            return getResult();
 
-       }
+        final IntArray value = (IntArray) getResult();
+        if (value.data == null) {
+            if (value.sizeOrData != IntArray.NO_DATA)
+                procedure.execute(graph, value.sizeOrData);
+        } else {
+            for (int i = 0; i < value.sizeOrData; i++)
+                procedure.execute(graph, value.data[i]);
+        }
 
-       @Override
-       public String toString() {
-               return "DirectObjects[" + r1() + " - " + r2() + "]";
-       }
+        procedure.finished(graph);
 
-       @Override
-       public void setReady() {
-               statusOrException = READY;
-       }
-       
-       final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
-               setReady();
-       }
+        return value;
 
-       @Override
-       public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
+    }
 
-               assert(isReady());
-               computeForEach(graph, provider, procedure, false);
+    @Override
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-       }
+        compute(graph, new IntProcedure() {
 
-       @Override
-       public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+            @Override
+            public void finished(ReadGraphImpl graph) {
+            }
 
-        final Semaphore s = new Semaphore(0);
-               
-               computeForEach(graph, provider, new IntProcedure() {
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) {
+                throw new Error("Error in recompute.", t);
+            }
 
-                       @Override
-                       public void finished(ReadGraphImpl graph) {
-                               s.release();
-                       }
+            @Override
+            public void execute(ReadGraphImpl graphd, int i) {
+            }
 
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               throw new Error("Error in recompute.", t);
-                       }
+        });
 
-                       @Override
-                       public void execute(ReadGraphImpl graphd, int i) {
-                       }
+    }
 
-               }, true);
+    @Override
+    boolean isImmutable(ReadGraphImpl graph) {
+        return graph.processor.isImmutable(r1());
+    }
 
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+    @Override
+    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+        IntArray value = (IntArray) getResult();
+        value.add(i);
+    }
 
-       }
+    @Override
+    public void finished(ReadGraphImpl graph) throws DatabaseException {
+        setReady();
+    }
 
     @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(r1());
+    public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
     }
-       
-}
 
+}