]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectSuperRelations.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / DirectSuperRelations.java
index c11e9f262118ceca8fdb0f2f4c7070f23c293c38..47db3c8d8425acc839f86a8757d02db250110fe3 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 gnu.trove.procedure.TIntProcedure;
-import gnu.trove.set.hash.TIntHashSet;
-
-import java.util.ArrayList;
-
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
-import org.simantics.db.impl.procedure.InternalProcedure;
-import org.simantics.db.procedure.ListenerBase;
+import org.simantics.db.impl.procedure.IntProcedureAdapter;
 
-final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
+import gnu.trove.procedure.TIntProcedure;
+import gnu.trove.set.hash.TIntHashSet;
 
-       public ArrayList<InternalProcedure<IntSet>> procs = null;
+public final class DirectSuperRelations extends UnaryQuery<IntProcedure> {
 
-       private DirectSuperRelations(final int resource) {
+       DirectSuperRelations(int resource) {
                super(resource);
        }
 
-       final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
-
-               new DirectSuperRelations(r).computeForEach(graph, provider, procedure, false);
-
-       }
-
-       @Override
-       public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-               return null;
-       }
-
        @Override
-       public void putEntry(QueryProcessor provider) {
+       public final void removeEntry(QueryProcessor provider) {
+               provider.cache.remove(this);
        }
 
-       @Override
-       final public void removeEntry(QueryProcessor provider) {
-       }
-
-       class Koss {
+       private static class Ints {
 
                private TIntHashSet set = null;
                public int single = 0;
@@ -70,15 +53,6 @@ final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
 
                }
 
-               //        public int[] toArray() {
-                       //            
-                       //            int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
-                       //            result[set.size()] = single;
-                       //            return result;
-                       //            
-                       //        }
-               //        
-
                public void forEach(TIntProcedure proc) {
                        if(single > 0) proc.execute(single);
                        if(set != null) set.forEach(proc);
@@ -86,23 +60,24 @@ final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
 
        }
 
-       @Override
-       public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
+       public Object compute(final ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
 
-               provider.querySupport.ensureLoaded(graph, id);
-               
-               int single = provider.querySupport.getSingleSuperrelation(id);
+               QueryProcessor processor = graph.processor;
+
+               processor.querySupport.ensureLoaded(graph, id);
+
+               int single = processor.querySupport.getSingleSuperrelation(id);
                if(single > 0) {
                        procedure.execute(graph, single);
                        procedure.finished(graph);
                        return single;
                }
 
-               final int subrelationOf = provider.getSubrelationOf();
+               final int subrelationOf = processor.getSubrelationOf();
 
-               final IntSet result = new IntSet(provider.querySupport);
+               final IntSet result = new IntSet(processor.querySupport);
 
-               final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
+               final class DirectProcedure extends Ints implements IntProcedure, TIntProcedure {
                        @Override
                        final public boolean execute(int r) {
                                result.add(r);
@@ -128,7 +103,7 @@ final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
 
                final DirectProcedure directProc = new DirectProcedure();
 
-               provider.querySupport.getObjects(graph, id, subrelationOf, directProc);
+               processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
 
                int size = directProc.size();
 
@@ -148,7 +123,11 @@ final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
                                @Override
                                public boolean execute(int arg0) {
 
-                                       procedure.execute(graph, arg0);
+                                       try {
+                                               procedure.execute(graph, arg0);
+                                       } catch (DatabaseException e) {
+                                               Logger.defaultLogError(e);
+                                       }
                                        return true;
 
                                }
@@ -166,16 +145,33 @@ final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
 
        @Override
        public String toString() {
-               return "SuperRelations2[" + id + "]";
+               return "DirectSuperRelations[" + id + "]";
        }
 
        @Override
-       public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
-               throw new UnsupportedOperationException();
+       public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
+
+               assert(isReady());
+
+               return compute(graph, procedure);
+
        }
 
        @Override
-       public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+       public void recompute(ReadGraphImpl graph) throws DatabaseException {
+
+               compute(graph, new IntProcedureAdapter() {
+
+                       @Override
+                       public void finished(ReadGraphImpl graph) {
+                       }
+
+                       @Override
+                       public void exception(ReadGraphImpl graph, Throwable t) {
+                               new Error("Error in recompute.", t).printStackTrace();
+                       }
+
+               });
 
        }