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 org.simantics.db.common.exception.DebugException;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.procedure.ListenerBase;
18 import org.simantics.db.request.RequestFlags;
20 final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> {
22 DirectObjects(final int r1, final int r2) {
28 return RequestFlags.INVALIDATE;
32 // public void clearResult(QuerySupport support) {
33 // setResult(INVALID_RESULT);
36 // 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) throws DatabaseException {
41 // if(parent == null && listener == null) {
42 // DirectObjects.computeForEach(graph, r1, r2, null, procedure);
44 // QueryCache.runnerDirectObjects(graph, r1, r2, parent, listener, procedure);
50 final public void removeEntry(QueryProcessor provider) {
51 provider.cache.remove(this);
55 public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
56 computeForEach(graph, r1(), r2(), this, procedure);
60 static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, final IntProcedure procedure) throws DatabaseException {
62 QueryProcessor processor = graph.processor;
64 processor.querySupport.ensureLoaded(graph, r1, r2);
66 processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
69 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
70 procedure.execute(graph, i);
71 if(entry != null) entry.add(i);
75 public void finished(ReadGraphImpl graph) {
79 public void exception(ReadGraphImpl graph, Throwable t) {
80 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
85 if(entry != null) entry.finish(graph, processor);
86 procedure.finished(graph);
91 public String toString() {
92 return "DirectObjects[" + r1() + " - " + r2() + "]";
96 public void setReady() {
97 statusOrException = READY;
100 final public void add(int add) {
104 IntArray value = (IntArray)getResult();
109 final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
114 public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
118 if(handleException(graph, procedure)) return getResult();
120 final IntArray value = (IntArray)getResult();
121 if(value.data == null) {
122 if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
124 for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
127 procedure.finished(graph);
134 public void recompute(ReadGraphImpl graph) throws DatabaseException {
136 compute(graph, new IntProcedure() {
139 public void finished(ReadGraphImpl graph) {
143 public void exception(ReadGraphImpl graph, Throwable t) {
144 throw new Error("Error in recompute.", t);
148 public void execute(ReadGraphImpl graphd, int i) {
156 boolean isImmutable(ReadGraphImpl graph) {
157 return graph.processor.isImmutable(r1());