1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.query;
14 import org.simantics.db.common.utils.Logger;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.impl.procedure.IntProcedureAdapter;
19 import gnu.trove.procedure.TIntProcedure;
20 import gnu.trove.set.hash.TIntHashSet;
22 public final class DirectSuperRelations extends UnaryQuery<IntProcedure> {
24 DirectSuperRelations(int resource) {
29 public final void removeEntry(QueryProcessor provider) {
30 provider.cache.remove(this);
33 private static class Ints {
35 private TIntHashSet set = null;
36 public int single = 0;
38 public boolean add(int val) {
39 if(single == val) return false;
44 if(set == null) set = new TIntHashSet(4);
50 if(single == 0) return 0;
51 if(set == null) return 1;
52 return set.size() + 1;
56 public void forEach(TIntProcedure proc) {
57 if(single > 0) proc.execute(single);
58 if(set != null) set.forEach(proc);
63 public Object compute(final ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
65 QueryProcessor processor = graph.processor;
67 processor.querySupport.ensureLoaded(graph, id);
69 int single = processor.querySupport.getSingleSuperrelation(id);
71 procedure.execute(graph, single);
72 procedure.finished(graph);
76 final int subrelationOf = processor.getSubrelationOf();
78 final IntSet result = new IntSet(processor.querySupport);
80 final class DirectProcedure extends Ints implements IntProcedure, TIntProcedure {
82 final public boolean execute(int r) {
87 final public void execute(ReadGraphImpl graph, int r) {
95 public void finished(ReadGraphImpl graph) {
98 public void exception(ReadGraphImpl graph, Throwable t) {
99 throw new Error("Errors are not supported.", t);
104 final DirectProcedure directProc = new DirectProcedure();
106 processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
108 int size = directProc.size();
112 procedure.finished(graph);
114 } else if (size == 1) {
116 procedure.execute(graph, directProc.single);
117 procedure.finished(graph);
121 directProc.forEach(new TIntProcedure() {
124 public boolean execute(int arg0) {
127 procedure.execute(graph, arg0);
128 } catch (DatabaseException e) {
129 Logger.defaultLogError(e);
137 procedure.finished(graph);
147 public String toString() {
148 return "DirectSuperRelations[" + id + "]";
152 public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
156 return compute(graph, procedure);
161 public void recompute(ReadGraphImpl graph) throws DatabaseException {
163 compute(graph, new IntProcedureAdapter() {
166 public void finished(ReadGraphImpl graph) {
170 public void exception(ReadGraphImpl graph, Throwable t) {
171 new Error("Error in recompute.", t).printStackTrace();