1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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 java.util.concurrent.Semaphore;
16 import org.simantics.db.common.utils.Logger;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.impl.procedure.IntProcedureAdapter;
21 import gnu.trove.procedure.TIntProcedure;
22 import gnu.trove.set.hash.TIntHashSet;
24 final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
26 DirectSuperRelations(final int resource) {
31 final public void removeEntry(QueryProcessor provider) {
32 provider.cache.remove(this);
37 private TIntHashSet set = null;
38 public int single = 0;
40 public boolean add(int val) {
41 if(single == val) return false;
46 if(set == null) set = new TIntHashSet(4);
52 if(single == 0) return 0;
53 if(set == null) return 1;
54 return set.size() + 1;
58 public void forEach(TIntProcedure proc) {
59 if(single > 0) proc.execute(single);
60 if(set != null) set.forEach(proc);
66 public Object compute(final ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
68 QueryProcessor processor = graph.processor;
70 processor.querySupport.ensureLoaded(graph, id);
72 int single = processor.querySupport.getSingleSuperrelation(id);
74 procedure.execute(graph, single);
75 procedure.finished(graph);
79 final int subrelationOf = processor.getSubrelationOf();
81 final IntSet result = new IntSet(processor.querySupport);
83 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
85 final public boolean execute(int r) {
90 final public void execute(ReadGraphImpl graph, int r) {
98 public void finished(ReadGraphImpl graph) {
101 public void exception(ReadGraphImpl graph, Throwable t) {
102 throw new Error("Errors are not supported.", t);
107 final DirectProcedure directProc = new DirectProcedure();
109 processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
111 int size = directProc.size();
115 procedure.finished(graph);
117 } else if (size == 1) {
119 procedure.execute(graph, directProc.single);
120 procedure.finished(graph);
124 directProc.forEach(new TIntProcedure() {
127 public boolean execute(int arg0) {
130 procedure.execute(graph, arg0);
131 } catch (DatabaseException e) {
132 Logger.defaultLogError(e);
140 procedure.finished(graph);
150 public String toString() {
151 return "DirectSuperRelations[" + id + "]";
155 public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
159 return compute(graph, procedure);
164 public void recompute(ReadGraphImpl graph) throws DatabaseException {
166 compute(graph, new IntProcedureAdapter() {
169 public void finished(ReadGraphImpl graph) {
173 public void exception(ReadGraphImpl graph, Throwable t) {
174 new Error("Error in recompute.", t).printStackTrace();