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=a71df27fd38a06d8fbecf0cd6b08b63bec68895d;hb=de8b6e194da6d56c410101d9c72763078b0e8717;hp=e1a106aaac540cc019262d03f1688194bbd23fd2;hpb=3850fec72035293b9a4ede780d01aedc5fbc9056;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 e1a106aaa..a71df27fd 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 @@ -23,7 +23,7 @@ import org.simantics.db.impl.procedure.TripleIntProcedureAdapter; import org.simantics.db.procedure.ListenerBase; import org.simantics.db.request.RequestFlags; -final public class Statements extends CollectionBinaryQuery { +final public class Statements extends CollectionBinaryQuery implements TripleIntProcedure { public Statements(final int r1, final int r2) { super(r1, r2); @@ -133,12 +133,11 @@ final public class Statements extends CollectionBinaryQuery } - 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); + final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final TripleIntProcedure procedure) throws DatabaseException { + + IntArray map = getAssertionMap(graph, r1, r2, parent); if(map == null) { - if(entry != null) entry.finish(graph, procedure); - else procedure.finished(graph); + procedure.finished(graph); return; } @@ -149,18 +148,12 @@ final public class Statements extends CollectionBinaryQuery int p = map.data[1]; int o = map.data[2]; - if(entry != null) { - entry.addOrSetFunctional(s,p,o); - entry.finish(graph, procedure); - } else { procedure.execute(graph, s,p,o); procedure.finished(graph); - } } else if(size == 0) { - if(entry != null) entry.finish(graph, procedure); - else procedure.finished(graph); + procedure.finished(graph); } else { @@ -170,22 +163,12 @@ final public class Statements extends CollectionBinaryQuery IntSet candidateIs = null; try { - candidateIs = QueryCache.resultSuperTypes(graph, candidateS, entry, null); + candidateIs = QueryCache.resultSuperTypes(graph, candidateS, parent, null); } catch (DatabaseException e) { - if(entry != null) entry.except(e); procedure.exception(graph, e); return; } - -// 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 IntSet nextIs = null; try { - nextIs = QueryCache.resultSuperTypes(graph, nextS, entry, null); + nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, 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)) { @@ -227,14 +201,10 @@ final public class Statements extends CollectionBinaryQuery candidateIs = nextIs; } else { - // candidate and next are unrelated => error ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions.", r1); - - if(entry != null) entry.except(exception); procedure.exception(graph, exception); return; - } } @@ -243,13 +213,8 @@ final public class Statements extends CollectionBinaryQuery } - if(entry != null) { - entry.addOrSetFunctional(candidateS, candidateP, candidateO); - entry.finish(graph, procedure); - } else { - procedure.execute(graph, candidateS, candidateP, candidateO); - procedure.finished(graph); - } + procedure.execute(graph, candidateS, candidateP, candidateO); + procedure.finished(graph); } @@ -268,7 +233,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) throws DatabaseException { + final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException { if(ri.isFinal) { @@ -277,7 +242,7 @@ final public class Statements extends CollectionBinaryQuery if(result == 0) { // Check for assertions - forSingleAssertion(graph, r1, r2, entry, procedure); + forSingleAssertion(graph, r1, r2, parent, procedure); } else if(result == -1) { @@ -285,8 +250,7 @@ final public class Statements extends CollectionBinaryQuery @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - if(entry != null) entry.addOrSetFunctional(r1, r2, i); - else procedure.execute(graph, r1, r2, i); + procedure.execute(graph, r1, r2, i); } @Override @@ -301,18 +265,13 @@ final public class Statements extends CollectionBinaryQuery }); // Check for assertions - forSingleAssertion(graph, r1, r2, entry, procedure); + forSingleAssertion(graph, r1, r2, parent, procedure); } else { // If functional relation was found there is no need to check assertions - if(entry != null) { - entry.addOrSetFunctional(r1, r2, result); - entry.finish(graph, procedure); - } else { - procedure.execute(graph, r1, r2, result); - procedure.finished(graph); - } + procedure.execute(graph, r1, r2, result); + procedure.finished(graph); } @@ -329,13 +288,10 @@ final public class Statements extends CollectionBinaryQuery public void run(ReadGraphImpl graph) throws DatabaseException { if(found.get()) { - if(entry != null) entry.finish(graph, procedure); - else procedure.finished(graph); + procedure.finished(graph); } else { - - // Check for assertions - forSingleAssertion(graph, r1, r2, entry, procedure); - + // Check for assertions + forSingleAssertion(graph, r1, r2, parent, procedure); } } @@ -356,16 +312,10 @@ final public class Statements extends CollectionBinaryQuery public void execute(ReadGraphImpl graph, int i) throws DatabaseException { if(found.compareAndSet(false, true)) { - - if(entry != null) entry.addOrSetFunctional(r1, pred, i); - else procedure.execute(graph, r1, pred, i); - + procedure.execute(graph, r1, pred, i); } else { - ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1); - if(entry != null) entry.except(exception); procedure.exception(graph, exception); - } } @@ -387,7 +337,7 @@ final public class Statements extends CollectionBinaryQuery inc(); - QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure() { + QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure() { @Override public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException { @@ -408,16 +358,10 @@ final public class Statements extends CollectionBinaryQuery public void execute(ReadGraphImpl graph, int i) throws DatabaseException { if(found.compareAndSet(false, true)) { - - if(entry != null) entry.addOrSetFunctional(r1, pred, i); - else procedure.execute(graph, r1, pred, i); - + procedure.execute(graph, r1, pred, i); } else { - ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1); - if(entry != null) entry.except(exception); procedure.exception(graph, exception); - } } @@ -464,24 +408,20 @@ 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) throws DatabaseException { + final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final TripleIntProcedure procedure) throws DatabaseException { - QueryCache.runnerPrincipalTypes(graph, r1, entry, null, new SyncIntProcedure() { + QueryCache.runnerPrincipalTypes(graph, r1, parent, null, new SyncIntProcedure() { @Override public void run(ReadGraphImpl graph) throws DatabaseException { - - if(entry != null) entry.finish(graph, procedure); - else procedure.finished(graph); - + procedure.finished(graph); } TripleIntProcedure proc = new TripleIntProcedureAdapter() { @Override 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); + procedure.execute(graph, s, p, o); } @Override @@ -499,10 +439,8 @@ final public class Statements extends CollectionBinaryQuery @Override public void execute(ReadGraphImpl graph, int type) throws DatabaseException { - inc(); - QueryCache.runnerAssertedStatements(graph, type, r2, entry, null, proc); - + QueryCache.runnerAssertedStatements(graph, type, r2, parent, null, proc); } @Override @@ -520,7 +458,7 @@ 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) throws DatabaseException { + final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException { if(ri.isFinal) { @@ -528,8 +466,7 @@ final public class Statements extends CollectionBinaryQuery @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - if(entry != null) entry.addOrSet(r1, r2, i); - else procedure.execute(graph, r1, r2, i); + procedure.execute(graph, r1, r2, i); } @Override @@ -545,10 +482,9 @@ final public class Statements extends CollectionBinaryQuery }); if(ri.isAsserted) { - forAssertions(graph, r1, r2, entry, procedure); + forAssertions(graph, r1, r2, parent, procedure); } else { - if(entry != null) entry.finish(graph, procedure); - else procedure.finished(graph); + procedure.finished(graph); } } else { @@ -559,9 +495,7 @@ final public class Statements extends CollectionBinaryQuery @Override public void run(ReadGraphImpl graph) throws DatabaseException { - - forAssertions(graph, r1, r2, entry, procedure); - + forAssertions(graph, r1, r2, parent, procedure); } @Override @@ -576,8 +510,7 @@ final public class Statements extends CollectionBinaryQuery @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - if(entry != null) entry.addOrSet(r1, pred2, i); - else procedure.execute(graph, r1, pred2, i); + procedure.execute(graph, r1, pred2, i); } @Override @@ -599,7 +532,7 @@ final public class Statements extends CollectionBinaryQuery try { - IntSet result = QueryCache.resultSuperRelations(graph, pred2, entry, null); + IntSet result = QueryCache.resultSuperRelations(graph, pred2, parent, null); if(result.contains(r2)) { inc(); @@ -609,8 +542,7 @@ final public class Statements extends CollectionBinaryQuery @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - if(entry != null) entry.addOrSet(r1, pred2, i); - else procedure.execute(graph, r1, pred2, i); + procedure.execute(graph, r1, pred2, i); } @@ -647,35 +579,20 @@ final public class Statements extends CollectionBinaryQuery } } - - @Override - 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) throws DatabaseException { + public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure_) throws DatabaseException { + + TripleIntProcedure procedure = entry != null ? entry : procedure_; - QueryCache.runnerRelationInfoQuery(graph, r2, entry, null, new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, final RelationInfo ri) throws DatabaseException { - - graph.ensureLoaded(r1, r2); - if(ri.isFunctional) { - computeFunctionalIndex(graph, r1, r2, entry, ri, procedure); - } else { - computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure); - } - - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { - procedure.exception(graph, t); - } + RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, r2, entry, null); + graph.ensureLoaded(r1, r2); + if(ri.isFunctional) { + computeFunctionalIndex(graph, r1, r2, entry, ri, procedure); + } else { + computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure); + } - }); + if(entry != null) entry.performFromCache(graph, procedure_); } @@ -690,6 +607,7 @@ final public class Statements extends CollectionBinaryQuery synchronized(this) { setReady(); + //new Exception(toString() + " is READY").printStackTrace(); } IntArray v = (IntArray)getResult(); @@ -761,7 +679,7 @@ final public class Statements extends CollectionBinaryQuery @Override public void recompute(ReadGraphImpl graph) throws DatabaseException { - compute(graph, new TripleIntProcedureAdapter() { + computeForEach(graph, r1(), r2(), this, new TripleIntProcedureAdapter() { @Override public void finished(ReadGraphImpl graph) { @@ -786,5 +704,20 @@ final public class Statements extends CollectionBinaryQuery boolean isImmutable(ReadGraphImpl graph) { return graph.processor.isImmutable(r1()); } + + @Override + public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException { + addOrSet(s, p, o); + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + setReady(); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException { + except(throwable); + } }