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 gnu.trove.procedure.TIntProcedure;
15 import gnu.trove.set.hash.TIntHashSet;
17 import java.util.concurrent.Semaphore;
18 import java.util.concurrent.atomic.AtomicInteger;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.impl.procedure.InternalProcedure;
22 import org.simantics.db.procedure.ListenerBase;
24 final public class SuperRelations extends UnaryQuery<InternalProcedure<IntSet>> {
26 private SuperRelations(final int resource) {
30 final static SuperRelations entry(final QueryProcessor provider, final int r) {
32 return (SuperRelations)provider.cache.superRelationsMap.get(r);
36 final static IntSet runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
38 SuperRelations entry = (SuperRelations)provider.cache.superRelationsMap.get(r);
41 entry = new SuperRelations(r);
43 entry.clearResult(provider.querySupport);
44 entry.putEntry(provider);
46 return (IntSet)provider.performForEach(graph, entry, parent, listener, procedure);
50 if(!entry.isReady()) {
51 throw new IllegalStateException();
53 return (IntSet)provider.performForEach(graph, entry, parent, listener, procedure);
59 final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws Throwable {
61 SuperRelations entry = (SuperRelations)provider.cache.superRelationsMap.get(r);
64 entry = new SuperRelations(r);
66 entry.clearResult(provider.querySupport);
67 entry.putEntry(provider);
69 return (IntSet)provider.performForEach2(graph, entry, parent, listener, procedure);
73 if(!entry.isReady()) {
74 throw new IllegalStateException();
76 return (IntSet)provider.performForEach2(graph, entry, parent, listener, procedure);
82 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
84 if(parent == null && listener == null) {
85 SuperRelations entry = (SuperRelations)provider.cache.superRelationsMap.get(r);
86 if(entry != null && entry.isReady()) {
87 entry.performFromCache(graph, provider, procedure);
92 runner(graph, r, provider, parent, listener, procedure);
96 final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws Throwable {
98 if(parent == null && listener == null) {
99 SuperRelations entry = (SuperRelations)provider.cache.superRelationsMap.get(r);
100 if(entry != null && entry.isReady()) {
101 return (IntSet)entry.get(graph, provider, procedure);
105 return runner2(graph, r, provider, parent, listener, procedure);
110 public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
111 return provider.cache.superRelationsMap.get(id);
115 public void putEntry(QueryProcessor provider) {
116 provider.cache.superRelationsMap.put(id, this);
120 final public void removeEntry(QueryProcessor provider) {
121 provider.cache.superRelationsMap.remove(id);
124 static int histoCounter = 0;
125 static IntSet EMPTY_SET = new IntSet();
126 static int counter = 0;
130 private TIntHashSet set = null;
131 public int single = 0;
133 public boolean add(int val) {
134 if(single == val) return false;
139 if(set == null) set = new TIntHashSet(4);
145 if(single == 0) return 0;
146 if(set == null) return 1;
147 return set.size() + 1;
151 public void forEach(TIntProcedure proc) {
152 if(single > 0) proc.execute(single);
153 if(set != null) set.forEach(proc);
159 public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, final boolean store) {
161 provider.querySupport.ensureLoaded(graph, id);
163 final InternalProcedure<IntSet> proc = (InternalProcedure<IntSet>)procedure;
165 final int subrelationOf = provider.getSubrelationOf();
167 final IntSet result = new IntSet(provider.querySupport);
169 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure, InternalProcedure<IntSet> {
171 final public boolean execute(int r) {
176 final public void execute(ReadGraphImpl graph, int r) {
184 final public void execute(ReadGraphImpl graph, IntSet set) {
186 addOrSet(graph, result, provider);
187 proc.execute(graph, result);
190 public void finished(ReadGraphImpl graph) {
193 public void exception(ReadGraphImpl graph, Throwable t) {
194 throw new Error("Errors are not supported.", t);
199 final DirectProcedure directProc = new DirectProcedure();
201 provider.querySupport.getObjects(graph, id, subrelationOf, directProc);
203 int size = directProc.size();
207 addOrSet(graph, EMPTY_SET, provider);
208 proc.execute(graph, EMPTY_SET);
210 } else if (size == 1) {
212 result.add(directProc.single);
213 SuperRelations.queryEach(graph, directProc.single, provider, SuperRelations.this, null, directProc);
217 // if((counter++ % 500) == 0) System.out.println("SR " + counter);
219 final TIntProcedure addToResult = new TIntProcedure() {
221 public boolean execute(int r) {
222 synchronized(result) {
229 final AtomicInteger finishes = new AtomicInteger(0);
231 directProc.forEach(new TIntProcedure() {
234 public boolean execute(int arg0) {
236 synchronized(result) {
240 SuperRelations.queryEach(graph, arg0, provider, SuperRelations.this, null, new InternalProcedure<IntSet>() {
243 public void execute(ReadGraphImpl graph, IntSet set) {
244 set.forEach(addToResult);
245 int current = finishes.addAndGet(1);
246 if(current == directProc.size()) {
247 addOrSet(graph, result, provider);
248 proc.execute(graph, result);
254 public void exception(ReadGraphImpl graph, Throwable t) {
255 proc.exception(graph, t);
273 public String toString() {
274 return "SuperRelations[" + id + "]";
277 private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
281 // ArrayList<InternalProcedure<IntSet>> p = null;
294 // IntSet v = (IntSet)getResult();
296 // for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);
303 public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
307 if(handleException(graph, procedure)) return null;
309 IntSet result = getResult();
311 procedure.execute(graph, result);
318 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
320 final Semaphore s = new Semaphore(0);
322 computeForEach(graph, provider, new InternalProcedure<IntSet>() {
325 public void execute(ReadGraphImpl graph, IntSet result) {
330 public void exception(ReadGraphImpl graph, Throwable t) {
332 new Error("Error in recompute.", t).printStackTrace();
337 while(!s.tryAcquire()) {
338 provider.resume(graph);
345 boolean isImmutable(ReadGraphImpl graph) {
346 return graph.processor.isImmutable(id);