/******************************************************************************* * 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 java.util.concurrent.atomic.AtomicInteger; 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 gnu.trove.procedure.TIntProcedure; import gnu.trove.set.hash.TIntHashSet; final public class SuperRelations extends UnaryQuery> { SuperRelations(final int resource) { super(resource); } final static SuperRelations entry(final QueryProcessor provider, final int r) { return (SuperRelations)provider.cache.superRelationsMap.get(r); } @Override final public void removeEntry(QueryProcessor provider) { provider.cache.remove(this); } static int histoCounter = 0; static int counter = 0; class Koss { private TIntHashSet set = null; public int single = 0; public boolean add(int val) { if(single == val) return false; if(single == 0) { single = val; return true; } if(set == null) set = new TIntHashSet(4); return set.add(val); } public int size() { if(single == 0) return 0; if(set == null) return 1; return set.size() + 1; } public void forEach(TIntProcedure proc) { if(single > 0) proc.execute(single); if(set != null) set.forEach(proc); } } //@Override public Object compute(final ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { QueryProcessor processor = graph.processor; processor.querySupport.ensureLoaded(graph, id); final InternalProcedure proc = (InternalProcedure)procedure; final int subrelationOf = processor.getSubrelationOf(); final IntSet result = new IntSet(processor.querySupport); final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure, InternalProcedure { @Override final public boolean execute(int r) { result.add(r); return true; } @Override final public void execute(ReadGraphImpl graph, int r) { if(single == 0) { single = r; return; } add(r); } @Override final public void execute(ReadGraphImpl graph, IntSet set) throws DatabaseException { set.forEach(this); addOrSet(graph, result, processor); proc.execute(graph, result); } @Override public void finished(ReadGraphImpl graph) { } @Override public void exception(ReadGraphImpl graph, Throwable t) { throw new Error("Errors are not supported.", t); } } final DirectProcedure directProc = new DirectProcedure(); processor.querySupport.getObjects(graph, id, subrelationOf, directProc); int size = directProc.size(); if(size == 0) { addOrSet(graph, IntSet.EMPTY, processor); proc.execute(graph, IntSet.EMPTY); } else if (size == 1) { result.add(directProc.single); QueryCache.runnerSuperRelations(graph, directProc.single, SuperRelations.this, null, directProc); } else { // if((counter++ % 500) == 0) System.out.println("SR " + counter); final TIntProcedure addToResult = new TIntProcedure() { @Override public boolean execute(int r) { synchronized(result) { result.add(r); } return true; } }; final AtomicInteger finishes = new AtomicInteger(0); directProc.forEach(new TIntProcedure() { @Override public boolean execute(int arg0) { try { return execute0(arg0); } catch (DatabaseException e) { Logger.defaultLogError(e); } return false; } public boolean execute0(int arg0) throws DatabaseException { synchronized(result) { result.add(arg0); } QueryCache.runnerSuperRelations(graph, arg0, SuperRelations.this, null, new InternalProcedure() { @Override public void execute(ReadGraphImpl graph, IntSet set) throws DatabaseException { set.forEach(addToResult); int current = finishes.addAndGet(1); if(current == directProc.size()) { addOrSet(graph, result, processor); proc.execute(graph, result); return; } } @Override public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { proc.exception(graph, t); } }); return true; } }); } return result; } @Override public String toString() { return "SuperRelations[" + id + "]"; } private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) { assert(!isReady()); synchronized(this) { value.trim(); setResult(value); setReady(); } } @Override public Object performFromCache(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { assert(isReady()); if(handleException(graph, procedure)) return null; IntSet result = getResult(); procedure.execute(graph, result); return result; } @Override public void recompute(ReadGraphImpl graph) throws DatabaseException { compute(graph, new InternalProcedure() { @Override public void execute(ReadGraphImpl graph, IntSet result) { } @Override public void exception(ReadGraphImpl graph, Throwable t) { new Error("Error in recompute.", t).printStackTrace(); } }); } @Override boolean isImmutable(ReadGraphImpl graph) { return graph.processor.isImmutable(id); } }