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.ArrayList;
15 import java.util.concurrent.Semaphore;
17 import org.simantics.db.common.exception.DebugException;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.impl.procedure.IntProcedureAdapter;
20 import org.simantics.db.procedure.ListenerBase;
22 final public class OrderedSet extends CollectionUnaryQuery<IntProcedure> {
24 // public ArrayList<IntProcedure> procs = null;
26 public OrderedSet(final int r) {
30 final static OrderedSet entry(final QueryProcessor provider, final int r) {
32 return (OrderedSet)provider.orderedSetMap.get(r);
36 final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, OrderedSet cached, final CacheEntry parent, ListenerBase listener, final IntProcedure procedure) {
38 OrderedSet entry = cached != null ? cached : (OrderedSet)provider.orderedSetMap.get(r);
41 entry = new OrderedSet(r);
43 entry.clearResult(provider.querySupport);
44 entry.putEntry(provider);
46 provider.performForEach(graph, entry, parent, listener, procedure);
50 if(entry.isPending()) {
52 if(entry.isPending()) {
53 throw new IllegalStateException();
54 // if(entry.procs == null) entry.procs = new ArrayList<IntProcedure>();
55 // entry.procs.add(procedure);
56 // provider.registerDependencies(graph, entry, parent, listener, procedure, false);
61 provider.performForEach(graph, entry, parent, listener, procedure);
66 final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
70 final OrderedSet entry = (OrderedSet)provider.orderedSetMap.get(r);
72 if(parent == null && !(listener != null)) {
73 if(entry != null && entry.isReady()) {
74 entry.performFromCache(graph, provider, procedure);
79 runner(graph, r, provider, entry, parent, listener, procedure);
84 public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
85 return provider.orderedSetMap.get(id);
89 public void putEntry(QueryProcessor provider) {
90 provider.orderedSetMap.put(id, this);
94 final public void removeEntry(QueryProcessor provider) {
95 provider.orderedSetMap.remove(id);
98 private int current = 0;
100 private boolean nextElement(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
102 provider.querySupport.ensureLoaded(graph, current);
104 boolean found = provider.querySupport.getObjects(graph, current, id, new IntProcedure() {
107 public void execute(ReadGraphImpl graph, int i) {
110 procedure.execute(graph, i);
116 public void exception(ReadGraphImpl graph, Throwable t) {
117 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
118 procedure.exception(graph, t);
122 public void finished(ReadGraphImpl graph) {
127 if(!found) current = id;
130 finish(graph, provider);
131 procedure.finished(graph);
140 public void clearResult(QuerySupport support) {
142 setResult(new IntArray());
146 public Object computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
148 while(nextElement(graph, provider, procedure, store));
155 public String toString() {
156 return "OrderedSet[" + id + "]";
159 final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
163 // ArrayList<IntProcedure> p = null;
174 // IntArray v = (IntArray)getResult();
175 // if(v.data == null) {
176 // if(v.sizeOrData != IntArray.NO_DATA) {
177 // for(IntProcedure proc : p) proc.execute(graph, v.sizeOrData);
180 // for(IntProcedure proc : p) {
181 // for(int i = 0;i < v.sizeOrData ; i++) proc.execute(graph, v.data[i]);
184 // for(IntProcedure proc : p) proc.finished(graph);
189 final public void addOrSet(int add) {
193 IntArray value = (IntArray)getResult();
199 public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
203 if(handleException(graph, procedure)) return EXCEPTED;
205 final IntArray value = (IntArray)getResult();
206 if(value.data == null) {
207 if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
209 for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
212 procedure.finished(graph);
219 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
221 final Semaphore s = new Semaphore(0);
223 computeForEach(graph, provider, new IntProcedureAdapter() {
226 public void finished(ReadGraphImpl graph) {
231 public void exception(ReadGraphImpl graph, Throwable t) {
232 throw new Error("Error in recompute.", t);
237 while(!s.tryAcquire()) {
238 provider.resume(graph);