/******************************************************************************* * 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.common.utils.Logger; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.procedure.ListenerBase; import org.simantics.db.request.RequestFlags; import gnu.trove.procedure.TIntProcedure; final public class Predicates extends UnaryQuery> { Predicates(final int r) { super(r); } @Override final public void removeEntry(QueryProcessor provider) { provider.cache.remove(this); } final static private void forAssertions(ReadGraphImpl graph, int r, Predicates entry, final IntSet set) throws DatabaseException { QueryCache.runnerPrincipalTypes(graph, r, entry, null, new SyncIntProcedure() { @Override public void run(ReadGraphImpl graph) throws DatabaseException { } IntProcedure proc = new IntProcedure() { @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { set.add(i); } @Override public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); } @Override public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { if(DebugException.DEBUG) new DebugException(t).printStackTrace(); } }; @Override public void execute(ReadGraphImpl graph, int type) throws DatabaseException { inc(); QueryCache.runnerAssertedPredicates(graph, type, entry, null, proc); } @Override public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); } }); } //@Override public Object compute(ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { computeForEach(graph, id, this, procedure); return getResult(); } public static void computeForEach(ReadGraphImpl graph, final int r, final Predicates entry, final InternalProcedure procedure) throws DatabaseException { IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null); IntSet result = new IntSet(graph.processor.querySupport); forAssertions(graph, r, entry, result); if(result.isEmpty()) { if(entry != null) { entry.setResult(direct); entry.setReady(); } procedure.execute(graph, direct); } else { direct.forEach(new TIntProcedure() { @Override public boolean execute(int value) { result.add(value); return true; } }); if(entry != null) { entry.setResult(result); entry.setReady(); } procedure.execute(graph, result); } } @Override public String toString() { return "Predicates[" + id + "]"; } final public void finish(final ReadGraphImpl graph, QueryProcessor provider) { synchronized(this) { setReady(); } } @Override public void clearResult(QuerySupport support) { setResult(new IntSet(support)); } @Override public Object performFromCache(final ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { assert(isReady()); if(handleException(graph, procedure)) return EXCEPTED; IntSet result = getResult(); procedure.execute(graph, result); return result; } @Override public void recompute(ReadGraphImpl graph) throws DatabaseException { compute(graph, new InternalProcedure() { @Override public void exception(ReadGraphImpl graph, Throwable t) { throw new Error("Error in recompute.", t); } @Override public void execute(ReadGraphImpl graph, IntSet i) { } }); } @Override public int type() { return RequestFlags.IMMEDIATE_UPDATE; } @Override boolean isImmutable(ReadGraphImpl graph) { return graph.processor.isImmutable(id); } }