]> 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 ed56c879b3285bc5c7b089165c04c2a24f739a0d..47db3c8d8425acc839f86a8757d02db250110fe3 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.impl.query;\r
-\r
-import gnu.trove.procedure.TIntProcedure;\r
-import gnu.trove.set.hash.TIntHashSet;\r
-\r
-import java.util.ArrayList;\r
-\r
-import org.simantics.db.impl.graph.ReadGraphImpl;\r
-import org.simantics.db.impl.procedure.InternalProcedure;\r
-import org.simantics.db.procedure.ListenerBase;\r
-\r
-final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {\r
-\r
-       public ArrayList<InternalProcedure<IntSet>> procs = null;\r
-\r
-       private DirectSuperRelations(final int resource) {\r
-               super(resource);\r
-       }\r
-\r
-       final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {\r
-\r
-               new DirectSuperRelations(r).computeForEach(graph, provider, procedure, false);\r
-\r
-       }\r
-\r
-       @Override\r
-       public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {\r
-               return null;\r
-       }\r
-\r
-       @Override\r
-       public void putEntry(QueryProcessor provider) {\r
-       }\r
-\r
-       @Override\r
-       final public void removeEntry(QueryProcessor provider) {\r
-       }\r
-\r
-       class Koss {\r
-\r
-               private TIntHashSet set = null;\r
-               public int single = 0;\r
-\r
-               public boolean add(int val) {\r
-                       if(single == val) return false;\r
-                       if(single == 0) {\r
-                               single = val;\r
-                               return true;\r
-                       }\r
-                       if(set == null) set = new TIntHashSet(4);\r
-                       return set.add(val);\r
-               }\r
-\r
-               public int size() {\r
-\r
-                       if(single == 0) return 0;\r
-                       if(set == null) return 1;\r
-                       return set.size() + 1;\r
-\r
-               }\r
-\r
-               //        public int[] toArray() {\r
-                       //            \r
-                       //            int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);\r
-                       //            result[set.size()] = single;\r
-                       //            return result;\r
-                       //            \r
-                       //        }\r
-               //        \r
-\r
-               public void forEach(TIntProcedure proc) {\r
-                       if(single > 0) proc.execute(single);\r
-                       if(set != null) set.forEach(proc);\r
-               }\r
-\r
-       }\r
-\r
-       @Override\r
-       public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {\r
-\r
-               provider.querySupport.ensureLoaded(graph, id);\r
-               \r
-               int single = provider.querySupport.getSingleSuperrelation(id);\r
-               if(single > 0) {\r
-                       procedure.execute(graph, single);\r
-                       procedure.finished(graph);\r
-                       return single;\r
-               }\r
-\r
-               final int subrelationOf = provider.getSubrelationOf();\r
-\r
-               final IntSet result = new IntSet(provider.querySupport);\r
-\r
-               final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {\r
-                       @Override\r
-                       final public boolean execute(int r) {\r
-                               result.add(r);\r
-                               return true;\r
-                       }\r
-                       @Override\r
-                       final public void execute(ReadGraphImpl graph, int r) {\r
-                               if(single == 0) {\r
-                                       single = r;\r
-                                       return;\r
-                               }\r
-                               add(r);\r
-                       }\r
-                       @Override\r
-                       public void finished(ReadGraphImpl graph) {\r
-                       }\r
-                       @Override\r
-                       public void exception(ReadGraphImpl graph, Throwable t) {\r
-                               throw new Error("Errors are not supported.", t);\r
-                       }\r
-\r
-               }\r
-\r
-               final DirectProcedure directProc = new DirectProcedure();\r
-\r
-               provider.querySupport.getObjects(graph, id, subrelationOf, directProc);\r
-\r
-               int size = directProc.size();\r
-\r
-               if(size == 0) {\r
-\r
-                       procedure.finished(graph);\r
-\r
-               } else if (size == 1) {\r
-\r
-                       procedure.execute(graph, directProc.single);\r
-                       procedure.finished(graph);\r
-\r
-               } else {\r
-\r
-                       directProc.forEach(new TIntProcedure() {\r
-\r
-                               @Override\r
-                               public boolean execute(int arg0) {\r
-\r
-                                       procedure.execute(graph, arg0);\r
-                                       return true;\r
-\r
-                               }\r
-\r
-                       });\r
-\r
-                       procedure.finished(graph);\r
-\r
-               }\r
-               \r
-               return getResult();\r
-\r
-\r
-       }\r
-\r
-       @Override\r
-       public String toString() {\r
-               return "SuperRelations2[" + id + "]";\r
-       }\r
-\r
-       @Override\r
-       public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {\r
-               throw new UnsupportedOperationException();\r
-       }\r
-\r
-       @Override\r
-       public void recompute(ReadGraphImpl graph, QueryProcessor provider) {\r
-\r
-       }\r
-\r
-}\r
+/*******************************************************************************
+ * 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
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.impl.query;
+
+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.IntProcedureAdapter;
+
+import gnu.trove.procedure.TIntProcedure;
+import gnu.trove.set.hash.TIntHashSet;
+
+public final class DirectSuperRelations extends UnaryQuery<IntProcedure> {
+
+       DirectSuperRelations(int resource) {
+               super(resource);
+       }
+
+       @Override
+       public final void removeEntry(QueryProcessor provider) {
+               provider.cache.remove(this);
+       }
+
+       private static class Ints {
+
+               private TIntHashSet set = null;
+               public int single = 0;
+
+               public boolean add(int val) {
+                       if(single == val) return false;
+                       if(single == 0) {
+                               single = val;
+                               return true;
+                       }
+                       if(set == null) set = new TIntHashSet(4);
+                       return set.add(val);
+               }
+
+               public int size() {
+
+                       if(single == 0) return 0;
+                       if(set == null) return 1;
+                       return set.size() + 1;
+
+               }
+
+               public void forEach(TIntProcedure proc) {
+                       if(single > 0) proc.execute(single);
+                       if(set != null) set.forEach(proc);
+               }
+
+       }
+
+       public Object compute(final ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
+
+               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 = processor.getSubrelationOf();
+
+               final IntSet result = new IntSet(processor.querySupport);
+
+               final class DirectProcedure extends Ints implements IntProcedure, TIntProcedure {
+                       @Override
+                       final public boolean execute(int r) {
+                               result.add(r);
+                               return true;
+                       }
+                       @Override
+                       final public void execute(ReadGraphImpl graph, int r) {
+                               if(single == 0) {
+                                       single = r;
+                                       return;
+                               }
+                               add(r);
+                       }
+                       @Override
+                       public void finished(ReadGraphImpl graph) {
+                       }
+                       @Override
+                       public void exception(ReadGraphImpl graph, Throwable t) {
+                               throw new Error("Errors are not supported.", t);
+                       }
+
+               }
+
+               final DirectProcedure directProc = new DirectProcedure();
+
+               processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
+
+               int size = directProc.size();
+
+               if(size == 0) {
+
+                       procedure.finished(graph);
+
+               } else if (size == 1) {
+
+                       procedure.execute(graph, directProc.single);
+                       procedure.finished(graph);
+
+               } else {
+
+                       directProc.forEach(new TIntProcedure() {
+
+                               @Override
+                               public boolean execute(int arg0) {
+
+                                       try {
+                                               procedure.execute(graph, arg0);
+                                       } catch (DatabaseException e) {
+                                               Logger.defaultLogError(e);
+                                       }
+                                       return true;
+
+                               }
+
+                       });
+
+                       procedure.finished(graph);
+
+               }
+               
+               return getResult();
+
+
+       }
+
+       @Override
+       public String toString() {
+               return "DirectSuperRelations[" + id + "]";
+       }
+
+       @Override
+       public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
+
+               assert(isReady());
+
+               return compute(graph, procedure);
+
+       }
+
+       @Override
+       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();
+                       }
+
+               });
+
+       }
+
+}