X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FSuperRelations.java;fp=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FSuperRelations.java;h=52781fb61f63f344e0f73bf445fe916b2003d41b;hb=de8b6e194da6d56c410101d9c72763078b0e8717;hp=b8d29b7e6ea995299e9baa11cc6c6c1be4a78c11;hpb=880a8a4927805afbbf47005a807cef3f39406866;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperRelations.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperRelations.java index b8d29b7e6..52781fb61 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperRelations.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperRelations.java @@ -21,7 +21,7 @@ import org.simantics.db.impl.procedure.InternalProcedure; import gnu.trove.procedure.TIntProcedure; import gnu.trove.set.hash.TIntHashSet; -final public class SuperRelations extends UnaryQuery> { +final public class SuperRelations extends UnaryQueryP { SuperRelations(final int resource) { super(resource); @@ -36,10 +36,7 @@ final public class SuperRelations extends UnaryQuery> provider.cache.remove(this); } - static int histoCounter = 0; - static int counter = 0; - - class Koss { + static class Koss { private TIntHashSet set = null; public int single = 0; @@ -69,10 +66,53 @@ final public class SuperRelations extends UnaryQuery> } - //@Override - public Object compute(final ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { + public void compute(final ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { + computeForEach(graph, id, this, procedure); + } + + static class DirectProcedure extends Koss implements IntProcedure, TIntProcedure, InternalProcedure { + + IntSet result; + InternalProcedure proc; + + public DirectProcedure(IntSet result, InternalProcedure proc) { + this.result = result; + this.proc = proc; + } + + @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); + 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); + } - QueryProcessor processor = graph.processor; + } + + public static Object computeForEach(final ReadGraphImpl graph, int id, SuperRelations entry, final InternalProcedure procedure_) throws DatabaseException { + + InternalProcedure procedure = entry != null ? entry : procedure_; + + QueryProcessor processor = graph.processor; processor.querySupport.ensureLoaded(graph, id); @@ -82,56 +122,19 @@ final public class SuperRelations extends UnaryQuery> 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(); + final DirectProcedure directProc = new DirectProcedure(result, proc); 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); - + QueryCache.runnerSuperRelations(graph, directProc.single, entry, null, directProc); } else { - // if((counter++ % 500) == 0) System.out.println("SR " + counter); - final TIntProcedure addToResult = new TIntProcedure() { @Override public boolean execute(int r) { @@ -162,14 +165,13 @@ final public class SuperRelations extends UnaryQuery> result.add(arg0); } - QueryCache.runnerSuperRelations(graph, arg0, SuperRelations.this, null, new InternalProcedure() { + QueryCache.runnerSuperRelations(graph, arg0, entry, 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; } @@ -190,6 +192,8 @@ final public class SuperRelations extends UnaryQuery> } + if(entry != null) entry.performFromCache(graph, procedure_); + return result; } @@ -198,57 +202,5 @@ final public class SuperRelations extends UnaryQuery> 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); - } }