X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FObjects.java;h=62fb3bdb8c73c3449b091501d7d275264ab6a889;hb=ded784594eb0e1fb318fbb931135288152691cf2;hp=0b3e1be34d3e8f16a135df845e95ebdbe4fc7ccb;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java index 0b3e1be34..62fb3bdb8 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java @@ -11,23 +11,19 @@ *******************************************************************************/ package org.simantics.db.impl.query; -import java.util.Collection; -import java.util.concurrent.Semaphore; - -import org.simantics.databoard.Bindings; -import org.simantics.db.DevelopmentKeys; import org.simantics.db.RelationInfo; import org.simantics.db.Resource; import org.simantics.db.common.exception.DebugException; +import org.simantics.db.common.utils.Logger; 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.IntProcedureAdapter; import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.procedure.AsyncMultiProcedure; -import org.simantics.db.procedure.ListenerBase; import org.simantics.db.request.RequestFlags; -import org.simantics.utils.Development; + +import gnu.trove.procedure.TIntProcedure; /* * Size analysis: @@ -48,144 +44,12 @@ final public class Objects extends CollectionBinaryQuery { super(r1, r2); } - final static Objects entry(final QueryProcessor provider, final int r1, final int r2) { - return (Objects)provider.objectsMap.get(r1,r2); - } - - final static Collection entries(final QueryProcessor processor, final int r1) { - return processor.objectsMap.values(r1); - } - - public final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) { - - if(parent == null && listener == null) { - Objects.computeForEach(graph, r1, r2, null, procedure); - return; - } - - QueryProcessor processor = graph.processor; - - Objects entry = (Objects)processor.objectsMap.get(r1,r2); - if(entry == null) { - - entry = new Objects(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()) { - processor.registerDependencies(graph, entry, parent, listener, procedure, false); - computeForEach(graph, r1, r2, null, procedure); - return; - } - } - } - - processor.performForEach(graph, entry, parent, listener, procedure); - - } - - } - - static class Runner2Procedure implements IntProcedure { - - public int single = 0; - public Throwable t = null; - - public void clear() { - single = 0; - t = null; - } - - @Override - public void execute(ReadGraphImpl graph, int i) { - if(single == 0) single = i; - else single = -1; - } - - @Override - public void finished(ReadGraphImpl graph) { - if(single == -1) single = 0; - } - - @Override - public void exception(ReadGraphImpl graph, Throwable throwable) { - single = 0; - this.t = throwable; - } - - public int get() throws DatabaseException { - if(t != null) { - if(t instanceof DatabaseException) throw (DatabaseException)t; - else throw new DatabaseException(t); - } - return single; - } - - } - - static final Runner2Procedure runner2Procedure = new Runner2Procedure(); - - public final static int runner2(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent) throws DatabaseException { - - runner2Procedure.clear(); - - if(parent == null) { - Objects.computeForEach(graph, r1, r2, null, runner2Procedure); - return runner2Procedure.get(); - } - - QueryProcessor processor = graph.processor; - - Objects entry = (Objects)processor.objectsMap.get(r1,r2); - if(entry == null) { - - entry = new Objects(r1, r2); - entry.setPending(); - entry.clearResult(processor.querySupport); - entry.putEntry(processor); - - processor.performForEach(graph, entry, parent, null, runner2Procedure); - return runner2Procedure.get(); - - } else { - - if(entry.isPending()) throw new IllegalStateException(); - - processor.performForEach(graph, entry, parent, null, runner2Procedure); - return runner2Procedure.get(); - - } - - } - - @Override - public BinaryQuery getEntry(QueryProcessor provider) { - return provider.objectsMap.get(id); - } - - @Override - public void putEntry(QueryProcessor provider) { - if(Development.DEVELOPMENT) { - if(Development.getProperty(DevelopmentKeys.QUERYPROCESSOR_PUT, Bindings.BOOLEAN)) { - System.err.println("put " + this); - } - } - provider.objectsMap.put(id, this); - } - @Override final public void removeEntry(QueryProcessor provider) { - provider.objectsMap.remove(id); + provider.cache.remove(this); } - final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Objects entry) { + final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Objects entry) throws DatabaseException { class AssertionMapProc implements IntProcedure { @@ -215,10 +79,9 @@ final public class Objects 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 = QueryCacheBase.resultAssertedStatements(graph, type, r2, entry, null); } else { if (first) { IntArray ia = result; @@ -228,7 +91,7 @@ final public class Objects extends CollectionBinaryQuery { } first = false; } - IntArray ia = stms.getResult(); + IntArray ia = QueryCacheBase.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]); } @@ -248,13 +111,13 @@ final public class Objects 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 Objects entry, final IntProcedure procedure) { + final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException { IntArray map = getAssertionMap(graph, r1, r2, entry); if(map == null) { @@ -286,13 +149,14 @@ final public class Objects extends CollectionBinaryQuery { int candidateS = map.data[0]; 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(); 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()); + 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; } - IntSet nextIs = next.getResult(); + +// 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)) { @@ -382,12 +255,12 @@ final public class Objects extends CollectionBinaryQuery { }; // Search for one statement - final public void computeFunctionalIndex(ReadGraphImpl graph, final QueryProcessor provider, final RelationInfo ri, final IntProcedure procedure) { + final public void computeFunctionalIndex(ReadGraphImpl graph, final QueryProcessor provider, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException { computeFunctionalIndex(graph, r1(), r2(), this, ri, procedure); } // Search for one statement - final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final RelationInfo ri, final IntProcedure procedure) { + final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException { if(ri.isFinal) { @@ -403,7 +276,7 @@ final public class Objects 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(i); else procedure.execute(graph, i); } @@ -439,12 +312,13 @@ final public class Objects 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() { boolean found = false; @Override - public void run(ReadGraphImpl graph) { + public void run(ReadGraphImpl graph) throws DatabaseException { if(found) { if(entry != null) entry.finish(graph, procedure); @@ -459,17 +333,17 @@ final public class Objects extends CollectionBinaryQuery { } @Override - public void execute(ReadGraphImpl graph, final int pred) { + public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException { if(found) return; if(pred == r2) { // 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) { @@ -493,7 +367,7 @@ final public class Objects extends CollectionBinaryQuery { } @Override - public void exception(ReadGraphImpl graph, Throwable t) { + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { procedure.exception(graph, t); } @@ -501,20 +375,20 @@ final public class Objects extends CollectionBinaryQuery { } else { - 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) return; if(result.contains(r2)) { // 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) { @@ -538,7 +412,7 @@ final public class Objects extends CollectionBinaryQuery { } @Override - public void exception(ReadGraphImpl graph, Throwable t) { + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { procedure.exception(graph, t); } @@ -549,7 +423,7 @@ final public class Objects extends CollectionBinaryQuery { } @Override - public void exception(ReadGraphImpl graph, Throwable t) { + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { procedure.exception(graph, t); } @@ -560,7 +434,7 @@ final public class Objects extends CollectionBinaryQuery { } @Override - public void finished(ReadGraphImpl graph) { + public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); @@ -573,13 +447,13 @@ final public class Objects extends CollectionBinaryQuery { } - final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) { + final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException { // Note! The dependency is intentionally cut! - PrincipalTypes.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() { + QueryCache.runnerPrincipalTypes(graph, r1, null, 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); @@ -589,18 +463,18 @@ final public class Objects extends CollectionBinaryQuery { TripleIntProcedure proc = new TripleIntProcedure() { @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(o); else procedure.execute(graph, 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 { if(DebugException.DEBUG) new DebugException(t).printStackTrace(); procedure.exception(graph, t); dec(graph); @@ -609,16 +483,15 @@ final public class Objects 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); } @@ -633,24 +506,24 @@ final public class Objects extends CollectionBinaryQuery { } - final public void computeNotFunctionalIndex(ReadGraphImpl graph, RelationInfo ri, final IntProcedure procedure) { + final public void computeNotFunctionalIndex(ReadGraphImpl graph, RelationInfo ri, final IntProcedure procedure) throws DatabaseException { computeNotFunctionalIndex(graph, r1(), r2(), this, ri, procedure); } - final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, RelationInfo ri, final IntProcedure procedure) { + final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, RelationInfo ri, final IntProcedure 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(i); else procedure.execute(graph, 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); } @@ -671,38 +544,39 @@ final public class Objects 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 pred) { + public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException { if(pred == r2) { 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(entry != null) entry.addOrSet(i); else procedure.execute(graph, 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); } @@ -713,31 +587,31 @@ final public class Objects 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(result.contains(r2)) { 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(entry != null) entry.addOrSet(i); else procedure.execute(graph, 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 { if(DebugException.DEBUG) new DebugException(t).printStackTrace(); procedure.exception(graph, t); dec(graph); @@ -752,7 +626,7 @@ final public class Objects 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); } @@ -764,7 +638,7 @@ final public class Objects extends CollectionBinaryQuery { } @Override - public void finished(ReadGraphImpl graph) { + public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); } @@ -775,13 +649,14 @@ final public class Objects extends CollectionBinaryQuery { } @Override - public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) { + public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { computeForEach(graph, r1(), r2(), this, procedure); + return getResult(); } - public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) { + public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException { - RelationInfo ri = RelationInfoQuery.queryEach(graph, r2, graph.processor, entry, null, ip); + RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, r2, entry, null); graph.ensureLoaded(r1, r2); if(ri.isFunctional) { computeFunctionalIndex(graph, r1, r2, entry, ri, procedure); @@ -791,24 +666,12 @@ final public class Objects extends CollectionBinaryQuery { } - final static InternalProcedure ip = new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, RelationInfo result) { - } - - @Override - public void exception(ReadGraphImpl graph, Throwable throwable) { - } - - }; - @Override public String toString() { return "Objects[" + r1() + " - " + r2() + "]"; } - final private void finish(ReadGraphImpl graph, IntProcedure procedure) { + final private void finish(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException { assert(assertPending()); @@ -853,11 +716,11 @@ final public class Objects extends CollectionBinaryQuery { } @Override - public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) { + public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { assert(isReady()); - if(handleException(graph, procedure)) return; + if(handleException(graph, procedure)) return getResult(); final IntArray value = (IntArray)getResult(); if(value.data == null) { @@ -867,32 +730,26 @@ final public class Objects extends CollectionBinaryQuery { } procedure.finished(graph); + + return value; } @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 IntProcedureAdapter() { + compute(graph, new IntProcedureAdapter() { @Override public void finished(ReadGraphImpl graph) { - s.release(); } @Override public void exception(ReadGraphImpl graph, Throwable t) { - s.release(); new Error("Error in recompute.", t).printStackTrace(); } - }, true); - - while(!s.tryAcquire()) { - provider.resume(graph); - } + }); }