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.atomic.AtomicInteger;
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.InternalProcedure;
21 import gnu.trove.procedure.TIntProcedure;
22 import gnu.trove.set.hash.TIntHashSet;
24 final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>> {
26 SuperRelations(final int resource) {
30 final static SuperRelations entry(final QueryProcessor provider, final int r) {
31 return (SuperRelations)provider.cache.superRelationsMap.get(r);
35 final public void removeEntry(QueryProcessor provider) {
36 provider.cache.remove(this);
39 static int histoCounter = 0;
40 static IntSet EMPTY_SET = new IntSet();
41 static int counter = 0;
45 private TIntHashSet set = null;
46 public int single = 0;
48 public boolean add(int val) {
49 if(single == val) return false;
54 if(set == null) set = new TIntHashSet(4);
60 if(single == 0) return 0;
61 if(set == null) return 1;
62 return set.size() + 1;
66 public void forEach(TIntProcedure proc) {
67 if(single > 0) proc.execute(single);
68 if(set != null) set.forEach(proc);
74 public Object compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
76 QueryProcessor processor = graph.processor;
78 processor.querySupport.ensureLoaded(graph, id);
80 final InternalProcedure<IntSet> proc = (InternalProcedure<IntSet>)procedure;
82 final int subrelationOf = processor.getSubrelationOf();
84 final IntSet result = new IntSet(processor.querySupport);
86 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure, InternalProcedure<IntSet> {
88 final public boolean execute(int r) {
93 final public void execute(ReadGraphImpl graph, int r) {
101 final public void execute(ReadGraphImpl graph, IntSet set) throws DatabaseException {
103 addOrSet(graph, result, processor);
104 proc.execute(graph, result);
107 public void finished(ReadGraphImpl graph) {
110 public void exception(ReadGraphImpl graph, Throwable t) {
111 throw new Error("Errors are not supported.", t);
116 final DirectProcedure directProc = new DirectProcedure();
118 processor.querySupport.getObjects(graph, id, subrelationOf, directProc);
120 int size = directProc.size();
124 addOrSet(graph, EMPTY_SET, processor);
125 proc.execute(graph, EMPTY_SET);
127 } else if (size == 1) {
129 result.add(directProc.single);
130 QueryCache.runnerSuperRelations(graph, directProc.single, SuperRelations.this, null, directProc);
134 // if((counter++ % 500) == 0) System.out.println("SR " + counter);
136 final TIntProcedure addToResult = new TIntProcedure() {
138 public boolean execute(int r) {
139 synchronized(result) {
146 final AtomicInteger finishes = new AtomicInteger(0);
148 directProc.forEach(new TIntProcedure() {
151 public boolean execute(int arg0) {
153 return execute0(arg0);
154 } catch (DatabaseException e) {
155 Logger.defaultLogError(e);
160 public boolean execute0(int arg0) throws DatabaseException {
162 synchronized(result) {
166 QueryCache.runnerSuperRelations(graph, arg0, SuperRelations.this, null, new InternalProcedure<IntSet>() {
169 public void execute(ReadGraphImpl graph, IntSet set) throws DatabaseException {
170 set.forEach(addToResult);
171 int current = finishes.addAndGet(1);
172 if(current == directProc.size()) {
173 addOrSet(graph, result, processor);
174 proc.execute(graph, result);
180 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
181 proc.exception(graph, t);
199 public String toString() {
200 return "SuperRelations[" + id + "]";
203 private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
218 public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
222 if(handleException(graph, procedure)) return null;
224 IntSet result = getResult();
226 procedure.execute(graph, result);
233 public void recompute(ReadGraphImpl graph) throws DatabaseException {
235 compute(graph, new InternalProcedure<IntSet>() {
238 public void execute(ReadGraphImpl graph, IntSet result) {
242 public void exception(ReadGraphImpl graph, Throwable t) {
243 new Error("Error in recompute.", t).printStackTrace();
251 boolean isImmutable(ReadGraphImpl graph) {
252 return graph.processor.isImmutable(id);