X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FDirectSuperRelations.java;h=47db3c8d8425acc839f86a8757d02db250110fe3;hp=ed56c879b3285bc5c7b089165c04c2a24f739a0d;hb=0d9b90834ce56b292c00b1a39850ed842c3e4d42;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectSuperRelations.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectSuperRelations.java index ed56c879b..47db3c8d8 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectSuperRelations.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectSuperRelations.java @@ -1,182 +1,178 @@ -/******************************************************************************* - * 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 gnu.trove.procedure.TIntProcedure; -import gnu.trove.set.hash.TIntHashSet; - -import java.util.ArrayList; - -import org.simantics.db.impl.graph.ReadGraphImpl; -import org.simantics.db.impl.procedure.InternalProcedure; -import org.simantics.db.procedure.ListenerBase; - -final public class DirectSuperRelations extends UnaryQuery { - - public ArrayList> procs = null; - - private DirectSuperRelations(final int resource) { - super(resource); - } - - final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) { - - new DirectSuperRelations(r).computeForEach(graph, provider, procedure, false); - - } - - @Override - public UnaryQuery getEntry(QueryProcessor provider) { - return null; - } - - @Override - public void putEntry(QueryProcessor provider) { - } - - @Override - final public void removeEntry(QueryProcessor provider) { - } - - 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 int[] toArray() { - // - // int[] result = Arrays.copyOf(set.toArray(), set.size() + 1); - // result[set.size()] = single; - // return result; - // - // } - // - - public void forEach(TIntProcedure proc) { - if(single > 0) proc.execute(single); - if(set != null) set.forEach(proc); - } - - } - - @Override - public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) { - - provider.querySupport.ensureLoaded(graph, id); - - int single = provider.querySupport.getSingleSuperrelation(id); - if(single > 0) { - procedure.execute(graph, single); - procedure.finished(graph); - return single; - } - - final int subrelationOf = provider.getSubrelationOf(); - - final IntSet result = new IntSet(provider.querySupport); - - final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure { - @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 - 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(); - - provider.querySupport.getObjects(graph, id, subrelationOf, directProc); - - int size = directProc.size(); - - if(size == 0) { - - procedure.finished(graph); - - } else if (size == 1) { - - procedure.execute(graph, directProc.single); - procedure.finished(graph); - - } else { - - directProc.forEach(new TIntProcedure() { - - @Override - public boolean execute(int arg0) { - - procedure.execute(graph, arg0); - return true; - - } - - }); - - procedure.finished(graph); - - } - - return getResult(); - - - } - - @Override - public String toString() { - return "SuperRelations2[" + id + "]"; - } - - @Override - public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) { - throw new UnsupportedOperationException(); - } - - @Override - public void recompute(ReadGraphImpl graph, QueryProcessor provider) { - - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2018 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.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.impl.graph.ReadGraphImpl; +import org.simantics.db.impl.procedure.IntProcedureAdapter; + +import gnu.trove.procedure.TIntProcedure; +import gnu.trove.set.hash.TIntHashSet; + +public final class DirectSuperRelations extends UnaryQuery { + + DirectSuperRelations(int resource) { + super(resource); + } + + @Override + public final void removeEntry(QueryProcessor provider) { + provider.cache.remove(this); + } + + private static class Ints { + + 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); + } + + } + + public Object compute(final ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { + + QueryProcessor processor = graph.processor; + + processor.querySupport.ensureLoaded(graph, id); + + int single = processor.querySupport.getSingleSuperrelation(id); + if(single > 0) { + procedure.execute(graph, single); + procedure.finished(graph); + return single; + } + + final int subrelationOf = processor.getSubrelationOf(); + + final IntSet result = new IntSet(processor.querySupport); + + final class DirectProcedure extends Ints implements IntProcedure, TIntProcedure { + @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 + 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) { + + procedure.finished(graph); + + } else if (size == 1) { + + procedure.execute(graph, directProc.single); + procedure.finished(graph); + + } else { + + directProc.forEach(new TIntProcedure() { + + @Override + public boolean execute(int arg0) { + + try { + procedure.execute(graph, arg0); + } catch (DatabaseException e) { + Logger.defaultLogError(e); + } + return true; + + } + + }); + + procedure.finished(graph); + + } + + return getResult(); + + + } + + @Override + public String toString() { + return "DirectSuperRelations[" + id + "]"; + } + + @Override + public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException { + + assert(isReady()); + + return compute(graph, procedure); + + } + + @Override + public void recompute(ReadGraphImpl graph) throws DatabaseException { + + compute(graph, new IntProcedureAdapter() { + + @Override + public void finished(ReadGraphImpl graph) { + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) { + new Error("Error in recompute.", t).printStackTrace(); + } + + }); + + } + +}