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.Collection;
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.procedure.ListenerBase;
20 import org.simantics.db.request.RequestFlags;
22 final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
24 private DirectObjects(final int r1, final int r2) {
30 return RequestFlags.INVALIDATE;
34 public void clearResult(QuerySupport support) {
35 setResult(INVALID_RESULT);
38 final static DirectObjects entry(final QueryProcessor provider, final int r1, final int r2) {
40 return (DirectObjects)provider.directObjectsMap.get(id(r1,r2));
44 final static Collection<DirectObjects> entries(final QueryProcessor processor, final int r1) {
45 DoubleKeyQueryHashMap<IntProcedure> hash = processor.directObjectsMap;
46 return hash.values(r1);
49 final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
51 QueryProcessor processor = graph.processor;
53 DirectObjects entry = (DirectObjects)processor.directObjectsMap.get(id(r1,r2));
56 entry = new DirectObjects(r1, r2);
58 entry.clearResult(processor.querySupport);
59 entry.putEntry(processor);
61 processor.performForEach(graph, entry, parent, listener, procedure);
65 if(entry.isPending()) {
67 if(entry.isPending()) {
68 processor.registerDependencies(graph, entry, parent, listener, procedure, false);
69 entry.computeForEach(graph, processor, procedure, true);
75 processor.performForEach(graph, entry, parent, listener, procedure);
81 final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
86 if(parent == null && listener == null) {
87 DirectObjects.computeForEach(graph, r1, r2, null, procedure);
89 runner(graph, r1, r2, parent, listener, procedure);
95 public BinaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
96 return provider.directObjectsMap.get(id);
100 public void putEntry(QueryProcessor provider) {
101 provider.directObjectsMap.put(id, this);
105 final public void removeEntry(QueryProcessor provider) {
106 provider.directObjectsMap.remove(id);
110 public void computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure procedure, final boolean store) {
111 computeForEach(graph, r1(), r2(), this, procedure);
114 static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, final IntProcedure procedure) {
116 QueryProcessor processor = graph.processor;
118 processor.querySupport.ensureLoaded(graph, r1, r2);
120 processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
123 public void execute(ReadGraphImpl graph, int i) {
124 procedure.execute(graph, i);
128 public void finished(ReadGraphImpl graph) {
132 public void exception(ReadGraphImpl graph, Throwable t) {
133 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
138 if(entry != null) entry.finish(graph, processor);
139 procedure.finished(graph);
144 public String toString() {
145 return "DirectObjects[" + r1() + " - " + r2() + "]";
149 public void setReady() {
150 statusOrException = READY;
153 final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
158 public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
161 computeForEach(graph, provider, procedure, false);
166 public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
168 final Semaphore s = new Semaphore(0);
170 computeForEach(graph, provider, new IntProcedure() {
173 public void finished(ReadGraphImpl graph) {
178 public void exception(ReadGraphImpl graph, Throwable t) {
179 throw new Error("Error in recompute.", t);
183 public void execute(ReadGraphImpl graphd, int i) {
188 while(!s.tryAcquire()) {
189 provider.resume(graph);
195 boolean isImmutable(ReadGraphImpl graph) {
196 return graph.processor.isImmutable(r1());