/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.db.impl.query; import org.simantics.db.common.exception.DebugException; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.procedure.ListenerBase; import org.simantics.db.request.RequestFlags; final public class DirectObjects extends CollectionBinaryQuery { DirectObjects(final int r1, final int r2) { super(r1, r2); } @Override public int type() { return RequestFlags.INVALIDATE; } // @Override // public void clearResult(QuerySupport support) { // setResult(INVALID_RESULT); // } // 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 { // // assert(r1 != 0); // assert(r2 != 0); // // if(parent == null && listener == null) { // DirectObjects.computeForEach(graph, r1, r2, null, procedure); // } else { // QueryCache.runnerDirectObjects(graph, r1, r2, parent, listener, procedure); // } // // } @Override final public void removeEntry(QueryProcessor provider) { provider.cache.remove(this); } //@Override public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { computeForEach(graph, r1(), r2(), this, procedure); return getResult(); } static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, final IntProcedure procedure) throws DatabaseException { QueryProcessor processor = graph.processor; processor.querySupport.ensureLoaded(graph, r1, r2); processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() { @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { procedure.execute(graph, i); if(entry != null) entry.add(i); } @Override public void finished(ReadGraphImpl graph) { } @Override public void exception(ReadGraphImpl graph, Throwable t) { if(DebugException.DEBUG) new DebugException(t).printStackTrace(); } }); if(entry != null) entry.finish(graph, processor); procedure.finished(graph); } @Override public String toString() { return "DirectObjects[" + r1() + " - " + r2() + "]"; } @Override public void setReady() { statusOrException = READY; } final public void add(int add) { assert(isPending()); IntArray value = (IntArray)getResult(); value.add(add); } final private void finish(ReadGraphImpl graph, QueryProcessor provider) { setReady(); } @Override public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException { assert(isReady()); if(handleException(graph, procedure)) return getResult(); final IntArray value = (IntArray)getResult(); if(value.data == null) { if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData); } else { for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]); } procedure.finished(graph); return value; } @Override public void recompute(ReadGraphImpl graph) throws DatabaseException { compute(graph, new IntProcedure() { @Override public void finished(ReadGraphImpl graph) { } @Override public void exception(ReadGraphImpl graph, Throwable t) { throw new Error("Error in recompute.", t); } @Override public void execute(ReadGraphImpl graphd, int i) { } }); } @Override boolean isImmutable(ReadGraphImpl graph) { return graph.processor.isImmutable(r1()); } }