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 // public ArrayList<InternalProcedure<IntSet>> procs = null;
28 private SuperRelations(final int resource) {
32 final static SuperRelations entry(final QueryProcessor provider, final int r) {
34 return (SuperRelations)provider.superRelationsMap.get(r);
38 final static IntSet runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
40 SuperRelations entry = (SuperRelations)provider.superRelationsMap.get(r);
43 entry = new SuperRelations(r);
45 entry.clearResult(provider.querySupport);
46 entry.putEntry(provider);
48 return (IntSet)provider.performForEach(graph, entry, parent, listener, procedure);
52 if(!entry.isReady()) {
53 throw new IllegalStateException();
55 return (IntSet)provider.performForEach(graph, entry, parent, listener, procedure);
61 final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws Throwable {
63 SuperRelations entry = (SuperRelations)provider.superRelationsMap.get(r);
66 entry = new SuperRelations(r);
68 entry.clearResult(provider.querySupport);
69 entry.putEntry(provider);
71 return (IntSet)provider.performForEach2(graph, entry, parent, listener, procedure);
75 if(!entry.isReady()) {
76 throw new IllegalStateException();
78 return (IntSet)provider.performForEach2(graph, entry, parent, listener, procedure);
84 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
86 if(parent == null && listener == null) {
87 SuperRelations entry = (SuperRelations)provider.superRelationsMap.get(r);
88 if(entry != null && entry.isReady()) {
89 entry.performFromCache(graph, provider, procedure);
94 runner(graph, r, provider, parent, listener, procedure);
98 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 {
100 if(parent == null && listener == null) {
101 SuperRelations entry = (SuperRelations)provider.superRelationsMap.get(r);
102 if(entry != null && entry.isReady()) {
103 return (IntSet)entry.get(graph, provider, procedure);
107 return runner2(graph, r, provider, parent, listener, procedure);
112 public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
113 return provider.superRelationsMap.get(id);
117 public void putEntry(QueryProcessor provider) {
118 provider.superRelationsMap.put(id, this);
122 final public void removeEntry(QueryProcessor provider) {
123 provider.superRelationsMap.remove(id);
126 static int histoCounter = 0;
127 static IntSet EMPTY_SET = new IntSet();
128 static int counter = 0;
132 private TIntHashSet set = null;
133 public int single = 0;
135 public boolean add(int val) {
136 if(single == val) return false;
141 if(set == null) set = new TIntHashSet(4);
147 if(single == 0) return 0;
148 if(set == null) return 1;
149 return set.size() + 1;
153 public void forEach(TIntProcedure proc) {
154 if(single > 0) proc.execute(single);
155 if(set != null) set.forEach(proc);
161 public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure<IntSet> procedure, final boolean store) {
163 provider.querySupport.ensureLoaded(graph, id);
165 final InternalProcedure<IntSet> proc = (InternalProcedure<IntSet>)procedure;
167 final int subrelationOf = provider.getSubrelationOf();
169 final IntSet result = new IntSet(provider.querySupport);
171 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure, InternalProcedure<IntSet> {
173 final public boolean execute(int r) {
178 final public void execute(ReadGraphImpl graph, int r) {
186 final public void execute(ReadGraphImpl graph, IntSet set) {
188 addOrSet(graph, result, provider);
189 proc.execute(graph, result);
192 public void finished(ReadGraphImpl graph) {
195 public void exception(ReadGraphImpl graph, Throwable t) {
196 throw new Error("Errors are not supported.", t);
201 final DirectProcedure directProc = new DirectProcedure();
203 provider.querySupport.getObjects(graph, id, subrelationOf, directProc);
205 int size = directProc.size();
209 addOrSet(graph, EMPTY_SET, provider);
210 proc.execute(graph, EMPTY_SET);
212 } else if (size == 1) {
214 result.add(directProc.single);
215 SuperRelations.queryEach(graph, directProc.single, provider, SuperRelations.this, null, directProc);
219 // if((counter++ % 500) == 0) System.out.println("SR " + counter);
221 final TIntProcedure addToResult = new TIntProcedure() {
223 public boolean execute(int r) {
224 synchronized(result) {
231 final AtomicInteger finishes = new AtomicInteger(0);
233 directProc.forEach(new TIntProcedure() {
236 public boolean execute(int arg0) {
238 synchronized(result) {
242 SuperRelations.queryEach(graph, arg0, provider, SuperRelations.this, null, new InternalProcedure<IntSet>() {
245 public void execute(ReadGraphImpl graph, IntSet set) {
246 set.forEach(addToResult);
247 int current = finishes.addAndGet(1);
248 if(current == directProc.size()) {
249 addOrSet(graph, result, provider);
250 proc.execute(graph, result);
256 public void exception(ReadGraphImpl graph, Throwable t) {
257 proc.exception(graph, t);
275 public String toString() {
276 return "SuperRelations[" + id + "]";
279 private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
283 // ArrayList<InternalProcedure<IntSet>> p = null;
296 // IntSet v = (IntSet)getResult();
298 // for(InternalProcedure<IntSet> proc : p) proc.execute(graph, v);
305 public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
309 if(handleException(graph, procedure)) return null;
311 IntSet result = getResult();
313 procedure.execute(graph, result);
320 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
322 final Semaphore s = new Semaphore(0);
324 computeForEach(graph, provider, new InternalProcedure<IntSet>() {
327 public void execute(ReadGraphImpl graph, IntSet result) {
332 public void exception(ReadGraphImpl graph, Throwable t) {
334 new Error("Error in recompute.", t).printStackTrace();
339 while(!s.tryAcquire()) {
340 provider.resume(graph);
347 boolean isImmutable(ReadGraphImpl graph) {
348 return graph.processor.isImmutable(id);