X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FStatements.java;h=e1a106aaac540cc019262d03f1688194bbd23fd2;hb=ded784594eb0e1fb318fbb931135288152691cf2;hp=d251a65c11b93dc4c70b3673d4b50518e912c5e5;hpb=e3290cc52342e783a2617f3009fff8e6f3e54560;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Statements.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Statements.java index d251a65c1..e1a106aaa 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Statements.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Statements.java @@ -11,13 +11,11 @@ *******************************************************************************/ package org.simantics.db.impl.query; -import java.util.ArrayList; -import java.util.Collection; -import java.util.concurrent.Semaphore; import java.util.concurrent.atomic.AtomicBoolean; import org.simantics.db.RelationInfo; import org.simantics.db.common.exception.DebugException; +import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; @@ -30,52 +28,8 @@ final public class Statements extends CollectionBinaryQuery public Statements(final int r1, final int r2) { super(r1, r2); } - - final static Statements entry(final QueryProcessor processor, final int r1, final int r2) { - - return (Statements)processor.cache.statementsMap.get(id(r1,r2)); - - } - - final static Collection entries(final QueryProcessor processor, final int r1) { - return processor.cache.statementsMap.values(r1); - } - - final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) { - - QueryProcessor processor = graph.processor; - - Statements entry = (Statements)processor.cache.statementsMap.get(id(r1,r2)); - if(entry == null) { - - entry = new Statements(r1, r2); - entry.setPending(); - entry.clearResult(processor.querySupport); - entry.putEntry(processor); - - processor.performForEach(graph, entry, parent, listener, procedure); - - } else { - - if(entry.isPending()) { - synchronized(entry) { - if(entry.isPending()) { - throw new IllegalStateException(); -// if(entry.procs == null) entry.procs = new ArrayList(); -// entry.procs.add(procedure); -// processor.registerDependencies(graph, entry, parent, listener, procedure, false); -// return; - } - } - } - - processor.performForEach(graph, entry, parent, listener, procedure); - - } - - } - final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) { + final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException { assert(r1 != 0); assert(r2 != 0); @@ -85,23 +39,13 @@ final public class Statements extends CollectionBinaryQuery return; } - runner(graph, r1, r2, parent, listener, procedure); + QueryCache.runnerStatements(graph, r1, r2, parent, listener, procedure); } - @Override - public BinaryQuery getEntry(QueryProcessor provider) { - return provider.cache.statementsMap.get(id); - } - - @Override - public void putEntry(QueryProcessor provider) { - provider.cache.statementsMap.put(id, this); - } - @Override final public void removeEntry(QueryProcessor provider) { - provider.cache.statementsMap.remove(id); + provider.cache.remove(this); } final static TripleIntProcedure NOPT = new TripleIntProcedure() { @@ -121,7 +65,7 @@ final public class Statements extends CollectionBinaryQuery }; - final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Statements entry) { + final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Statements entry) throws DatabaseException { class AssertionMapProc implements IntProcedure { @@ -151,10 +95,9 @@ final public class Statements extends CollectionBinaryQuery } @Override - public void execute(ReadGraphImpl graph, int type) { - AssertedStatements stms = AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, NOPT); + public void execute(ReadGraphImpl graph, int type) throws DatabaseException { if(result == null) { - result = stms.getResult(); + result = QueryCache.resultAssertedStatements(graph, type, r2, entry, null); } else { if (first) { IntArray ia = result; @@ -164,7 +107,7 @@ final public class Statements extends CollectionBinaryQuery } first = false; } - IntArray ia = stms.getResult(); + IntArray ia = QueryCache.resultAssertedStatements(graph, type, r2, entry, null); if(ia.data != null) { for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]); } @@ -184,13 +127,13 @@ final public class Statements extends CollectionBinaryQuery AssertionMapProc amp = new AssertionMapProc(); // This dependency could be cut - PrincipalTypes.queryEach(graph, r1, graph.processor, entry, null, amp); + QueryCache.runnerPrincipalTypes(graph, r1, entry, null, amp); return amp.result; } - final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) { + final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException { IntArray map = getAssertionMap(graph, r1, r2, entry); if(map == null) { @@ -224,14 +167,24 @@ final public class Statements extends CollectionBinaryQuery int candidateS = map.data[0]; int candidateP = map.data[1]; int candidateO = map.data[2]; - - SuperTypes candidate = SuperTypes.queryEach(graph, candidateS, graph.processor, entry, null, NOP); - if(candidate.isExcepted()) { - if(entry != null) entry.except((Throwable)candidate.getResult()); - procedure.exception(graph, (Throwable)candidate.getResult()); + + IntSet candidateIs = null; + try { + candidateIs = QueryCache.resultSuperTypes(graph, candidateS, entry, null); + } catch (DatabaseException e) { + if(entry != null) entry.except(e); + procedure.exception(graph, e); return; } - IntSet candidateIs = candidate.getResult(); + + +// SuperTypes candidate = SuperTypes.queryEach(graph, candidateS, graph.processor, entry, null, NOP); +// if(candidate.isExcepted()) { +// if(entry != null) entry.except((Throwable)candidate.getResult()); +// procedure.exception(graph, (Throwable)candidate.getResult()); +// return; +// } +// IntSet candidateIs = candidate.getResult(); for(int i=3;i } else { - SuperTypes next = SuperTypes.queryEach(graph, nextS, graph.processor, entry, null, NOP); - if(next.isExcepted()) { - if(entry != null) entry.except((Throwable)next.getResult()); - procedure.exception(graph, (Throwable)next.getResult()); - return; - } - IntSet nextIs = next.getResult(); + IntSet nextIs = null; + try { + nextIs = QueryCache.resultSuperTypes(graph, nextS, entry, null); + } catch (DatabaseException e) { + if(entry != null) entry.except(e); + procedure.exception(graph, e); + return; + } + +// SuperTypes next = SuperTypes.queryEach(graph, nextS, graph.processor, entry, null, NOP); +// if(next.isExcepted()) { +// if(entry != null) entry.except((Throwable)next.getResult()); +// procedure.exception(graph, (Throwable)next.getResult()); +// return; +// } +// IntSet nextIs = next.getResult(); if(nextIs.contains(candidateS)) { @@ -306,7 +268,7 @@ final public class Statements extends CollectionBinaryQuery }; // Search for one statement - final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final RelationInfo ri, final TripleIntProcedure procedure) { + final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException { if(ri.isFinal) { @@ -322,7 +284,7 @@ final public class Statements extends CollectionBinaryQuery graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() { @Override - public void execute(ReadGraphImpl graph, int i) { + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { if(entry != null) entry.addOrSetFunctional(r1, r2, i); else procedure.execute(graph, r1, r2, i); } @@ -360,10 +322,11 @@ final public class Statements extends CollectionBinaryQuery final AtomicBoolean found = new AtomicBoolean(false); // Note! The dependency is intentionally cut! - DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() { + IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null); + direct.forEach(graph, new SyncIntProcedure() { @Override - public void run(ReadGraphImpl graph) { + public void run(ReadGraphImpl graph) throws DatabaseException { if(found.get()) { if(entry != null) entry.finish(graph, procedure); @@ -371,14 +334,14 @@ final public class Statements extends CollectionBinaryQuery } else { // Check for assertions - forSingleAssertion(graph, r1, r2, entry, procedure); + forSingleAssertion(graph, r1, r2, entry, procedure); } } @Override - public void execute(ReadGraphImpl graph, final int pred) { + public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException { if(found.get()) return; @@ -387,10 +350,10 @@ final public class Statements extends CollectionBinaryQuery inc(); // Note! The dependency is intentionally cut! - DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() { + QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() { @Override - public void execute(ReadGraphImpl graph, int i) { + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { if(found.compareAndSet(false, true)) { @@ -408,12 +371,12 @@ final public class Statements extends CollectionBinaryQuery } @Override - public void finished(ReadGraphImpl graph) { + public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); } @Override - public void exception(ReadGraphImpl graph, Throwable t) { + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { procedure.exception(graph, t); dec(graph); } @@ -424,10 +387,10 @@ final public class Statements extends CollectionBinaryQuery inc(); - SuperRelations.queryEach(graph, pred, graph.processor, entry, null, new InternalProcedure() { + QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure() { @Override - public void execute(ReadGraphImpl graph, IntSet result) { + public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException { if(found.get()) { dec(graph); @@ -439,10 +402,10 @@ final public class Statements extends CollectionBinaryQuery inc(); // Note! The dependency is intentionally cut! - DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() { + QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() { @Override - public void execute(ReadGraphImpl graph, int i) { + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { if(found.compareAndSet(false, true)) { @@ -460,12 +423,12 @@ final public class Statements extends CollectionBinaryQuery } @Override - public void finished(ReadGraphImpl graph) { + public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); } @Override - public void exception(ReadGraphImpl graph, Throwable t) { + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { procedure.exception(graph, t); dec(graph); } @@ -479,7 +442,7 @@ final public class Statements extends CollectionBinaryQuery } @Override - public void exception(ReadGraphImpl graph, Throwable t) { + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { procedure.exception(graph, t); dec(graph); } @@ -491,10 +454,8 @@ final public class Statements extends CollectionBinaryQuery } @Override - public void finished(ReadGraphImpl graph) { - + public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); - } }); @@ -503,12 +464,12 @@ final public class Statements extends CollectionBinaryQuery } - final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) { + final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException { - PrincipalTypes.queryEach(graph, r1, graph.processor, entry, null, new SyncIntProcedure() { + QueryCache.runnerPrincipalTypes(graph, r1, entry, null, new SyncIntProcedure() { @Override - public void run(ReadGraphImpl graph) { + public void run(ReadGraphImpl graph) throws DatabaseException { if(entry != null) entry.finish(graph, procedure); else procedure.finished(graph); @@ -518,18 +479,18 @@ final public class Statements extends CollectionBinaryQuery TripleIntProcedure proc = new TripleIntProcedureAdapter() { @Override - public void execute(ReadGraphImpl graph, int s, int p, int o) { + public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException { if(entry != null) entry.addOrSet(s, p, o); else procedure.execute(graph, s, p, o); } @Override - public void finished(ReadGraphImpl graph) { + public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); } @Override - public void exception(ReadGraphImpl graph, Throwable t) { + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { dec(graph); procedure.exception(graph, t); } @@ -537,21 +498,20 @@ final public class Statements extends CollectionBinaryQuery }; @Override - public void execute(ReadGraphImpl graph, int type) { + public void execute(ReadGraphImpl graph, int type) throws DatabaseException { inc(); - - AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, proc); + QueryCache.runnerAssertedStatements(graph, type, r2, entry, null, proc); } @Override - public void finished(ReadGraphImpl graph) { + public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); } @Override - public void exception(ReadGraphImpl graph, Throwable t) { + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { dec(graph); } @@ -560,20 +520,20 @@ final public class Statements extends CollectionBinaryQuery } - final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final RelationInfo ri, final TripleIntProcedure procedure) { + final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException { if(ri.isFinal) { graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() { @Override - public void execute(ReadGraphImpl graph, int i) { + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { if(entry != null) entry.addOrSet(r1, r2, i); else procedure.execute(graph, r1, r2, i); } @Override - public void exception(ReadGraphImpl graph, Throwable t) { + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { if(DebugException.DEBUG) new DebugException(t).printStackTrace(); procedure.exception(graph, t); } @@ -594,38 +554,39 @@ final public class Statements extends CollectionBinaryQuery } else { // Note! The dependency is intentionally cut! - DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() { + IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null); + direct.forEach(graph, new SyncIntProcedure() { @Override - public void run(ReadGraphImpl graph) { + public void run(ReadGraphImpl graph) throws DatabaseException { forAssertions(graph, r1, r2, entry, procedure); } @Override - public void execute(ReadGraphImpl graph, final int pred2) { + public void execute(ReadGraphImpl graph, final int pred2) throws DatabaseException { if(pred2 == r2) { inc(); // Note! The dependency is intentionally cut! - DirectObjects.queryEach(graph, r1, pred2, graph.processor, null, null, new IntProcedure() { + QueryCache.runnerDirectObjects(graph, r1, pred2, null, null, new IntProcedure() { @Override - public void execute(ReadGraphImpl graph, int i) { + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { if(entry != null) entry.addOrSet(r1, pred2, i); else procedure.execute(graph, r1, pred2, i); } @Override - public void finished(ReadGraphImpl graph) { + public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); } @Override - public void exception(ReadGraphImpl graph, Throwable t) { + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { procedure.exception(graph, t); dec(graph); } @@ -638,28 +599,28 @@ final public class Statements extends CollectionBinaryQuery try { - IntSet result = SuperRelations.queryEach2(graph, pred2, graph.processor, entry, null, null); + IntSet result = QueryCache.resultSuperRelations(graph, pred2, entry, null); if(result.contains(r2)) { inc(); // Note! The dependency is intentionally cut! - DirectObjects.queryEach(graph, r1, pred2, graph.processor, null, null, new IntProcedure() { + QueryCache.runnerDirectObjects(graph, r1, pred2, null, null, new IntProcedure() { @Override - public void execute(ReadGraphImpl graph, int i) { + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { if(entry != null) entry.addOrSet(r1, pred2, i); else procedure.execute(graph, r1, pred2, i); } @Override - public void finished(ReadGraphImpl graph) { + public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); } @Override - public void exception(ReadGraphImpl graph, Throwable t) { + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { procedure.exception(graph, t); dec(graph); } @@ -677,7 +638,7 @@ final public class Statements extends CollectionBinaryQuery } @Override - public void finished(ReadGraphImpl graph) { + public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); } @@ -688,28 +649,17 @@ final public class Statements extends CollectionBinaryQuery } @Override - public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final TripleIntProcedure procedure, final boolean store) { + public Object compute(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException { computeForEach(graph, r1(), r2(), this, procedure); + return getResult(); } - public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) { - - RelationInfoQuery riEntry = RelationInfoQuery.probe(graph, r2); - if(riEntry != null) { - RelationInfo ri = riEntry.getResult(); - graph.ensureLoaded(r1, r2); - if(ri.isFunctional) { - computeFunctionalIndex(graph, r1, r2, entry, ri, procedure); - } else { - computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure); - } - return; - } + public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException { - RelationInfoQuery.queryEach(graph, r2, graph.processor, entry, null, new InternalProcedure() { + QueryCache.runnerRelationInfoQuery(graph, r2, entry, null, new InternalProcedure() { @Override - public void execute(ReadGraphImpl graph, final RelationInfo ri) { + public void execute(ReadGraphImpl graph, final RelationInfo ri) throws DatabaseException { graph.ensureLoaded(r1, r2); if(ri.isFunctional) { @@ -721,7 +671,7 @@ final public class Statements extends CollectionBinaryQuery } @Override - public void exception(ReadGraphImpl graph, Throwable t) { + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { procedure.exception(graph, t); } @@ -734,38 +684,17 @@ final public class Statements extends CollectionBinaryQuery return "Statements[" + r1() + " - " + r2() + "]"; } - final private void finish(ReadGraphImpl graph, TripleIntProcedure procedure) { + final private void finish(ReadGraphImpl graph, TripleIntProcedure procedure) throws DatabaseException { assert(assertPending()); -// ArrayList p = null; - synchronized(this) { - setReady(); -// p = procs; -// procs = null; - } IntArray v = (IntArray)getResult(); final IntArray value = (IntArray)getResult(); -// if(p != null) { -// -// for(TripleIntProcedure proc : p) { -// for(int i=0;i } -// final private void finish(ReadGraphImpl graph, QueryProcessor provider) { -// -// assert(isPending()); -// -// ArrayList p = null; -// -// synchronized(this) { -// -// setReady(); -// p = procs; -// procs = null; -// -// } -// -// if(p != null) { -// -// final IntArray value = (IntArray)getResult(); -// for(TripleIntProcedure proc : p) { -// for(int i=0;i } @Override - public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) { + public Object performFromCache(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException { assert(isReady()); @@ -859,34 +759,20 @@ final public class Statements extends CollectionBinaryQuery } @Override - public void recompute(ReadGraphImpl graph, QueryProcessor provider) { + public void recompute(ReadGraphImpl graph) throws DatabaseException { - final Semaphore s = new Semaphore(0); - - computeForEach(graph, provider, new TripleIntProcedureAdapter() { + compute(graph, new TripleIntProcedureAdapter() { @Override public void finished(ReadGraphImpl graph) { - s.release(); } @Override public void exception(ReadGraphImpl graph, Throwable t) { new Error("Error in recompute.", t).printStackTrace(); - s.release(); } - }, true); - - while(!s.tryAcquire()) { - provider.resume(graph); - } - -// try { -// s.acquire(); -// } catch (InterruptedException e) { -// throw new Error(e); -// } + }); }