From: Antti Villberg Date: Sun, 22 Jul 2018 22:49:21 +0000 (+0300) Subject: Trying to remove synchronization problems X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=de8b6e194da6d56c410101d9c72763078b0e8717;p=simantics%2Fplatform.git Trying to remove synchronization problems Change-Id: I52a00b8405aa1ca969fe339befa787f79c9ad10a --- diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedPredicates.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedPredicates.java index 184276412..b4104e965 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedPredicates.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedPredicates.java @@ -15,11 +15,9 @@ import org.simantics.db.RelationInfo; import org.simantics.db.exception.DatabaseException; 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.ListenerBase; -final public class AssertedPredicates extends CollectionUnaryQuery { +final public class AssertedPredicates extends UnaryQuery { AssertedPredicates(final int r) { super(r); @@ -28,11 +26,21 @@ final public class AssertedPredicates extends CollectionUnaryQuery public static AssertedPredicates newInstance(final int r) { return new AssertedPredicates(r); } - -// final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) throws DatabaseException { -// QueryCache.runnerAssertedPredicates(graph, r, parent, listener, procedure); -// } + @Override + final public void clearResult(QuerySupport support) { + setResult(new IntArray()); + } + + @Override + final public void setReady() { + super.setReady(); + IntArray v = (IntArray)getResult(); + int size = v.size(); + if(size == 0) setResult(IntArray.EMPTY); + else v.trim(); + } + @Override final public void removeEntry(QueryProcessor provider) { provider.cache.remove(this); diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java index 75169d02e..69a85220b 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java @@ -14,84 +14,72 @@ package org.simantics.db.impl.query; import org.simantics.db.AsyncReadGraph; import org.simantics.db.common.GraphSemaphore; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.RuntimeDatabaseException; import org.simantics.db.impl.DebugPolicy; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.procedure.AsyncProcedure; import org.simantics.db.request.AsyncRead; -final public class AsyncReadEntry extends CacheEntryBase> { +final public class AsyncReadEntry extends CacheEntryBase> implements AsyncProcedure { protected AsyncRead request; AsyncReadEntry(AsyncRead request) { - this.request = request; - if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this); + this.request = request; + if (DebugPolicy.QUERY_STATE) + System.out.println("[QUERY STATE]: created " + this); } @Override int makeHash() { - return request.hashCode(); + return request.hashCode(); } - + @Override public Object getOriginalRequest() { return request; } - + @Override public void discard() { - super.discard(); - setResult(null); + super.discard(); + setResult(null); } - - final public void addOrSet(AsyncReadGraph graph, Object item) { -// System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + item); - - assert(isPending()); - - synchronized(this) { - setResult(item); - setReady(); - } - - } - - public void except(AsyncReadGraph graph, Throwable t) { - - assert(isPending()); - synchronized(this) { + assert (isPending()); + + synchronized (this) { except(t); } - + } - - + @Override final public Query getQuery() { - + return new Query() { - @Override - public void recompute(ReadGraphImpl graph) { + @Override + public void recompute(ReadGraphImpl graph) { - try { + try { - GraphSemaphore s = new GraphSemaphore(graph, 0); - - request.perform(graph , new AsyncProcedure() { + GraphSemaphore s = new GraphSemaphore(graph, 0); + + request.perform(graph, new AsyncProcedure() { @Override public void execute(AsyncReadGraph graph, T result) { - addOrSet(graph, result); + setResult(result); + setReady(); s.release(); } - - @Override - public void exception(AsyncReadGraph graph, Throwable t) { - except(t); + + @Override + public void exception(AsyncReadGraph graph, Throwable t) { + except(t); s.release(); } @@ -99,144 +87,129 @@ final public class AsyncReadEntry extends CacheEntryBase> { s.waitFor(1); - } catch (Throwable t) { - except(t); + } catch (Throwable t) { + except(t); } - - } - - @Override - public void removeEntry(QueryProcessor qp) { - qp.cache.remove(AsyncReadEntry.this); - } - - @Override - public int type() { - return request.getFlags(); - } - - @Override - public String toString() { - if(request == null) return "DISCARDED"; - else if(isExcepted()) return request.toString() + " " + getResult(); - else return request.toString() + " " + statusOrException; - } - + + } + + @Override + public void removeEntry(QueryProcessor qp) { + qp.cache.remove(AsyncReadEntry.this); + } + + @Override + public int type() { + return request.getFlags(); + } + + @Override + public String toString() { + if (request == null) + return "DISCARDED"; + else if (isExcepted()) + return request.toString() + " " + getResult(); + else + return request.toString() + " " + statusOrException; + } + }; - + } @Override - public Object performFromCache(ReadGraphImpl graph, AsyncProcedure proc) { - - if(isExcepted()) { - + public Object performFromCache(ReadGraphImpl graph, AsyncProcedure proc) { + + if (isExcepted()) { + try { - proc.exception(graph, (Throwable)getResult()); + proc.exception(graph, (Throwable) getResult()); } catch (Throwable t) { t.printStackTrace(); } - + } else { - + try { - proc.execute(graph, (T)getResult()); + proc.execute(graph, (T) getResult()); } catch (Throwable t) { t.printStackTrace(); } - + + } + + return getResult(); + + } + + public static void computeForEach(ReadGraphImpl parentGraph, AsyncRead request, AsyncReadEntry entry, + AsyncProcedure procedure_) throws DatabaseException { + + AsyncProcedure procedure = entry != null ? entry : procedure_; + + GraphSemaphore s = new GraphSemaphore(parentGraph, 0); + + ReadGraphImpl queryGraph = parentGraph.withParent(entry); + + request.perform(queryGraph, new AsyncProcedure() { + + @Override + public void execute(AsyncReadGraph returnGraph, T result) { + try { + procedure.execute(parentGraph, result); + } catch (Throwable t) { + t.printStackTrace(); + } + s.release(); + } + + @Override + public void exception(AsyncReadGraph returnGraph, Throwable t) { + try { + procedure.exception(parentGraph, t); + } catch (Throwable t2) { + t2.printStackTrace(); + } + s.release(); + } + + @Override + public String toString() { + return procedure.toString(); + } + + }); + + try { + s.waitFor(1); + } catch (InterruptedException e) { + throw new RuntimeDatabaseException(e); } - - return getResult(); - - } - - //@Override - public Object compute(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { - - ReadGraphImpl queryGraph = graph.withParent(this); - - request.perform(queryGraph, new AsyncProcedure() { - - @Override - public void execute(AsyncReadGraph returnGraph, T result) { - ReadGraphImpl impl = (ReadGraphImpl)returnGraph; - AsyncReadEntry.this.addOrSet(graph, result); - try { - procedure.execute(graph, result); - } catch (Throwable t) { - t.printStackTrace(); - } - // parentBarrier.dec(query); - } - - @Override - public void exception(AsyncReadGraph returnGraph, Throwable t) { - ReadGraphImpl impl = (ReadGraphImpl)returnGraph; - // AsyncReadGraph resumeGraph = finalParentGraph.newAsync(); - AsyncReadEntry.this.except(graph, t); - try { - procedure.exception(graph, t); - } catch (Throwable t2) { - t2.printStackTrace(); - } - // parentBarrier.dec(query); - } - - @Override - public String toString() { - return procedure.toString(); - } - - }); - - return getResult(); - + + if (entry != null) + entry.performFromCache(parentGraph, procedure_); + + } + + @Override + public String toString() { + if (isDiscarded()) + return "DISCARDED " + request.toString(); + else if (isExcepted()) + return request.toString() + " " + getResult(); + else + return request.toString() + " " + statusOrException; } - - public static void computeForEach(ReadGraphImpl parentGraph, AsyncRead request, AsyncReadEntry entry, AsyncProcedure procedure) throws DatabaseException { - - ReadGraphImpl queryGraph = parentGraph.withParent(entry); - - request.perform(queryGraph, new AsyncProcedure() { - - @Override - public void execute(AsyncReadGraph returnGraph, T result) { - ReadGraphImpl impl = (ReadGraphImpl)returnGraph; - if(entry != null) entry.addOrSet(parentGraph, result); - try { - procedure.execute(parentGraph, result); - } catch (Throwable t) { - t.printStackTrace(); - } - } - - @Override - public void exception(AsyncReadGraph returnGraph, Throwable t) { - ReadGraphImpl impl = (ReadGraphImpl)returnGraph; - if(entry != null) entry.except(parentGraph, t); - try { - procedure.exception(parentGraph, t); - } catch (Throwable t2) { - t2.printStackTrace(); - } - } - - @Override - public String toString() { - return procedure.toString(); - } - - }); - + + @Override + public void execute(AsyncReadGraph graph, T result) { + setResult(result); + setReady(); + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + except(throwable); } - - - @Override - public String toString() { - if(isDiscarded()) return "DISCARDED " + request.toString(); - else if(isExcepted()) return request.toString() + " " + getResult(); - else return request.toString() + " " + statusOrException; - } } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntryBase.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntryBase.java index dea5de5a7..8c4313573 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntryBase.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntryBase.java @@ -108,14 +108,14 @@ abstract public class CacheEntryBase extends CacheEntry { } @Override - public void except(Throwable t) { + public void except(Throwable throwable) { if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: excepted " + this); if(statusOrException != DISCARDED) { statusOrException = EXCEPTED; - result = t; + result = throwable; } else { - LOGGER.warn("Cache entry got excepted status after being discarded: " + getClass().getSimpleName(), t); - result = t; + LOGGER.warn("Cache entry got excepted status after being discarded: " + getClass().getSimpleName(), throwable); + result = throwable; } } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ChildMap.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ChildMap.java index 7dd00d29b..19c36c77c 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ChildMap.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ChildMap.java @@ -21,170 +21,130 @@ import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.service.CollectionSupport; -final public class ChildMap extends UnaryQuery>> { - +final public class ChildMap extends UnaryQueryP> { + ChildMap(final int r) { super(r); } - - @Override - final public void removeEntry(QueryProcessor provider) { - provider.cache.remove(this); - } - - //@Override - public Object compute(ReadGraphImpl graph, final InternalProcedure> procedure) throws DatabaseException { - computeForEach(graph, id, this, procedure); - return getResult(); + + @Override + final public void removeEntry(QueryProcessor provider) { + provider.cache.remove(this); } - public static void computeForEach(ReadGraphImpl graph, final int root, final ChildMap entry, final InternalProcedure> procedure) throws DatabaseException { - - if(root == 0) { - if(entry != null) - entry.add2(graph, null); + @Override + public void compute(ReadGraphImpl graph, final InternalProcedure> procedure) + throws DatabaseException { + computeForEach(graph, id, this, procedure); + } + + public static void computeForEach(ReadGraphImpl graph, final int root, final ChildMap entry, + final InternalProcedure> procedure_) throws DatabaseException { + + InternalProcedure> procedure = entry != null ? entry : procedure_; + + computeForEach2(graph, root, entry, procedure); + + if (entry != null) + entry.performFromCache(graph, procedure_); + + } + + public static void computeForEach2(ReadGraphImpl graph, final int root, final ChildMap parent, + final InternalProcedure> procedure) throws DatabaseException { + + if (root == 0) { procedure.execute(graph, null); return; - } - - QueryProcessor processor = graph.processor; - - final int consistsOf = processor.getConsistsOf(); - final int hasName = processor.getHasName(); - - ObjectResourceIdMap result = graph.getService(CollectionSupport.class).createObjectResourceMap(String.class); - - QueryCache.runnerObjects(graph, root, consistsOf, entry, null, new SyncIntProcedure() { - - @Override - public void run(ReadGraphImpl graph) throws DatabaseException { - - if(entry != null) entry.add2(graph, result); - procedure.execute(graph, result); - - } - - @Override - public void finished(ReadGraphImpl graph) throws DatabaseException { - dec(graph); - } - - @Override - public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException { - - inc(); - - QueryCache.runnerObjects(graph, obj, hasName, entry, null, new IntProcedure() { - - @Override - public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - - inc(); - - QueryCache.runnerValueQuery(graph, i, entry, null, new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException { - - if(value != null) { - - try { - - Binding b = WriteBindings.STRING; - Serializer serializer = b.serializer(); - final String part = (String)serializer.deserialize(value); - result.putId(part, obj); - - } catch (Throwable e) { - if(DebugException.DEBUG) new DebugException(e).printStackTrace(); - } - - } - - dec(graph); - - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { - if(entry != null) entry.except(t); - dec(graph); + } + + QueryProcessor processor = graph.processor; + + final int consistsOf = processor.getConsistsOf(); + final int hasName = processor.getHasName(); + + ObjectResourceIdMap result = graph.getService(CollectionSupport.class) + .createObjectResourceMap(String.class); + + QueryCache.runnerObjects(graph, root, consistsOf, parent, null, new SyncIntProcedure() { + + @Override + public void run(ReadGraphImpl graph) throws DatabaseException { + procedure.execute(graph, result); + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + dec(graph); + } + + @Override + public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException { + + inc(); + + QueryCache.runnerObjects(graph, obj, hasName, parent, null, new IntProcedure() { + + @Override + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { + + inc(); + + QueryCache.runnerValueQuery(graph, i, parent, null, new InternalProcedure() { + + @Override + public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException { + + if (value != null) { + + try { + + Binding b = WriteBindings.STRING; + Serializer serializer = b.serializer(); + final String part = (String) serializer.deserialize(value); + result.putId(part, obj); + + } catch (Throwable e) { + if (DebugException.DEBUG) + new DebugException(e).printStackTrace(); + } + + } + + dec(graph); + + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + dec(graph); } - }); - - } - - @Override - public void finished(ReadGraphImpl graph) throws DatabaseException { - dec(graph); - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { - if(entry != null) entry.except(t); + }); + + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + dec(graph); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { dec(graph); } }); - } - + } + }); - - } - @Override - public String toString() { - return "ChildMap[" + id + "]"; } - private void add2(ReadGraphImpl graph, ObjectResourceIdMap result) { - - assert(isPending()); - - synchronized(this) { - - setResult(result); - setReady(); - - } - - } - @Override - public Object performFromCache(ReadGraphImpl graph, InternalProcedure> procedure) throws DatabaseException { - - assert(isReady()); - - if(handleException(graph, procedure)) return (Throwable)statusOrException; - - ObjectResourceIdMap result = (ObjectResourceIdMap)getResult(); - - procedure.execute(graph, result); - - return result; - + public String toString() { + return "ChildMap[" + id + "]"; } - - @Override - public void recompute(ReadGraphImpl graph) throws DatabaseException { - - compute(graph, new InternalProcedure>() { - - @Override - public void execute(ReadGraphImpl graph, ObjectResourceIdMap result) { - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - if(DebugException.DEBUG) new DebugException(t).printStackTrace(); - throw new Error("Error in recompute.", t); - } - }); - - } - } - diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CodeGen.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CodeGen.java index deecbabbc..3d5f14ac4 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CodeGen.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CodeGen.java @@ -14,8 +14,6 @@ public class CodeGen { int indent = 4; - - String[] signatureR1RelationInfo = { "int r", "r", "keyR", "long", "InternalProcedure", "entry.id" }; String[] signatureR1Bytes = { "int r", "r", "keyR", "long", "InternalProcedure", "entry.id" }; String[] signatureR1IntSet = { "int r", "r", "keyR", "long", "InternalProcedure", "entry.id" }; @@ -188,15 +186,14 @@ public class CodeGen { generateQuery(content, "ReadEntry", signatureRead, true, true); generateQuery(content, "AsyncReadEntry", signatureAsyncRead, true, true); generateQuery(content, "Types", signatureR1IntSet, true, false); - //generateQuery(content, "NamespaceIndex", signatureID2, true); generateQuery(content, "ChildMap", signatureChildMap, true, false); + generateQuery(content, "TypeHierarchy", signatureR1IntSet, true, false); + generateQuery(content, "SuperTypes", signatureR1IntSet, true, false); + generateQuery(content, "SuperRelations", signatureR1IntSet, true, false); - generateQuery(content, "AssertedStatements", signatureR2TIP, false, false); generateQuery(content, "AssertedPredicates", signatureR1IP, false, false); + generateQuery(content, "AssertedStatements", signatureR2TIP, false, false); generateQuery(content, "DirectSuperRelations", signatureR1IP, false, false); - generateQuery(content, "SuperTypes", signatureR1IntSet, false, false); - generateQuery(content, "TypeHierarchy", signatureR1IntSet, false, false); - generateQuery(content, "SuperRelations", signatureR1IntSet, false, false); generateQuery(content, "MultiReadEntry", signatureMultiRead, false, false); generateQuery(content, "AsyncMultiReadEntry", signatureAsyncMultiRead, false, false); generateQuery(content, "ExternalReadEntry", signatureExternalRead, false, false); diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CollectionUnaryQuery.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CollectionUnaryQuery.java index bde26c36d..f0389ccfe 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CollectionUnaryQuery.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CollectionUnaryQuery.java @@ -11,22 +11,25 @@ *******************************************************************************/ package org.simantics.db.impl.query; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.impl.graph.ReadGraphImpl; +import org.simantics.db.impl.procedure.IntProcedureAdapter; - - -abstract public class CollectionUnaryQuery extends UnaryQuery { +abstract public class CollectionUnaryQuery extends UnaryQuery implements IntProcedure { public CollectionUnaryQuery(final int id) { super(id); } + abstract public void compute(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException; + @Override - public void clearResult(QuerySupport support) { + final public void clearResult(QuerySupport support) { setResult(new IntArray()); } @Override - public void setReady() { + final public void setReady() { super.setReady(); IntArray v = (IntArray)getResult(); int size = v.size(); @@ -34,4 +37,63 @@ abstract public class CollectionUnaryQuery extends UnaryQuery { else v.trim(); } + @Override + final public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { + + assert(isReady()); + + if(handleException(graph, procedure)) return EXCEPTED; + + final IntArray value = (IntArray)getResult(); + if(value.data == null) { + if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData); + } else { + for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]); + } + + procedure.finished(graph); + + return getResult(); + + } + + @Override + final 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(); + } + + }); + + } + + @Override + final boolean isImmutable(ReadGraphImpl graph) { + return graph.processor.isImmutable(id); + } + + @Override + final public void execute(ReadGraphImpl graph, int i) throws DatabaseException { + IntArray v = (IntArray)getResult(); + v.add(i); + } + + @Override + final public void finished(ReadGraphImpl graph) throws DatabaseException { + setReady(); + } + + @Override + final public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException { + except(throwable); + } + } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java index 117056410..8f2c7e0d8 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java @@ -11,151 +11,133 @@ *******************************************************************************/ package org.simantics.db.impl.query; -import org.simantics.db.common.exception.DebugException; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; -import org.simantics.db.procedure.ListenerBase; import org.simantics.db.request.RequestFlags; -final public class DirectObjects extends CollectionBinaryQuery { +final public class DirectObjects extends CollectionBinaryQuery implements IntProcedure { - DirectObjects(final int r1, final int r2) { - super(r1, r2); - } + DirectObjects(final int r1, final int r2) { + super(r1, r2); + } - @Override - public int type() { - return RequestFlags.INVALIDATE; - } - -// @Override -// public void clearResult(QuerySupport support) { -// setResult(INVALID_RESULT); -// } + @Override + public int type() { + return RequestFlags.INVALIDATE; + } -// final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) throws DatabaseException { -// -// assert(r1 != 0); -// assert(r2 != 0); -// -// if(parent == null && listener == null) { -// DirectObjects.computeForEach(graph, r1, r2, null, procedure); -// } else { -// QueryCache.runnerDirectObjects(graph, r1, r2, parent, listener, procedure); -// } -// -// } + @Override + final public void removeEntry(QueryProcessor provider) { + provider.cache.remove(this); + } - @Override - final public void removeEntry(QueryProcessor provider) { - provider.cache.remove(this); - } + // @Override + public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { + computeForEach(graph, r1(), r2(), this, procedure); + return getResult(); + } - //@Override - public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { - computeForEach(graph, r1(), r2(), this, procedure); - return getResult(); - } + static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, + final IntProcedure procedure_) throws DatabaseException { - static public void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry, final IntProcedure procedure) throws DatabaseException { + IntProcedure procedure = entry != null ? entry : procedure_; - QueryProcessor processor = graph.processor; - - processor.querySupport.ensureLoaded(graph, r1, r2); + QueryProcessor processor = graph.processor; - processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() { + processor.querySupport.ensureLoaded(graph, r1, r2); - @Override - public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - procedure.execute(graph, i); - if(entry != null) entry.add(i); - } + processor.querySupport.getObjects(graph, r1, r2, procedure); - @Override - public void finished(ReadGraphImpl graph) { - } + procedure.finished(graph); - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - if(DebugException.DEBUG) new DebugException(t).printStackTrace(); - } + if (entry != null) + entry.performFromCache(graph, procedure_); - }); + } - if(entry != null) entry.finish(graph, processor); - procedure.finished(graph); + @Override + public String toString() { + return "DirectObjects[" + r1() + " - " + r2() + "]"; + } - } + @Override + public void setReady() { + statusOrException = READY; + } - @Override - public String toString() { - return "DirectObjects[" + r1() + " - " + r2() + "]"; - } +// final public void add(int add) { +// +// assert(isPending()); +// +// IntArray value = (IntArray)getResult(); +// value.add(add); +// +// } - @Override - public void setReady() { - statusOrException = READY; - } - - final public void add(int add) { + @Override + public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException { - assert(isPending()); + assert (isReady()); - IntArray value = (IntArray)getResult(); - value.add(add); + if (handleException(graph, procedure)) + return getResult(); - } + final IntArray value = (IntArray) getResult(); + if (value.data == null) { + if (value.sizeOrData != IntArray.NO_DATA) + procedure.execute(graph, value.sizeOrData); + } else { + for (int i = 0; i < value.sizeOrData; i++) + procedure.execute(graph, value.data[i]); + } - final private void finish(ReadGraphImpl graph, QueryProcessor provider) { - setReady(); - } + procedure.finished(graph); - @Override - public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException { + return value; - assert(isReady()); + } - if(handleException(graph, procedure)) return getResult(); + @Override + public void recompute(ReadGraphImpl graph) throws DatabaseException { - final IntArray value = (IntArray)getResult(); - if(value.data == null) { - if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData); - } else { - for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]); - } + compute(graph, new IntProcedure() { - procedure.finished(graph); - - return value; + @Override + public void finished(ReadGraphImpl graph) { + } - } + @Override + public void exception(ReadGraphImpl graph, Throwable t) { + throw new Error("Error in recompute.", t); + } - @Override - public void recompute(ReadGraphImpl graph) throws DatabaseException { - - compute(graph, new IntProcedure() { + @Override + public void execute(ReadGraphImpl graphd, int i) { + } - @Override - public void finished(ReadGraphImpl graph) { - } + }); - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - throw new Error("Error in recompute.", t); - } + } - @Override - public void execute(ReadGraphImpl graphd, int i) { - } + @Override + boolean isImmutable(ReadGraphImpl graph) { + return graph.processor.isImmutable(r1()); + } - }); + @Override + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { + IntArray value = (IntArray) getResult(); + value.add(i); + } - } + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + setReady(); + } @Override - boolean isImmutable(ReadGraphImpl graph) { - return graph.processor.isImmutable(r1()); + public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException { + except(throwable); } - -} +} diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectPredicates.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectPredicates.java index 63c54effe..bb2e1530e 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectPredicates.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectPredicates.java @@ -16,30 +16,26 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; -final public class DirectPredicates extends CollectionUnaryQuery> { +final public class DirectPredicates extends UnaryQueryP { DirectPredicates(final int resource) { super(resource); } - @Override - public void clearResult(QuerySupport support) { - // The cached result is never used - setResult(INVALID_RESULT); - } - @Override final public void removeEntry(QueryProcessor provider) { provider.cache.remove(this); } - //@Override - public Object compute(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { - return computeForEach(graph, id, this, procedure); + @Override + public void compute(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { + computeForEach(graph, id, this, procedure); } - public static Object computeForEach(ReadGraphImpl graph, int id, final DirectPredicates entry, final InternalProcedure procedure) throws DatabaseException { + public static Object computeForEach(ReadGraphImpl graph, int id, final DirectPredicates entry, final InternalProcedure procedure_) throws DatabaseException { + InternalProcedure procedure = entry != null ? entry : procedure_; + graph.processor.querySupport.ensureLoaded(graph, id); final IntSet list = new IntSet(graph.processor.querySupport); @@ -61,13 +57,10 @@ final public class DirectPredicates extends CollectionUnaryQuery procedure) throws DatabaseException { - - assert(isReady()); - - if(handleException(graph, procedure)) return EXCEPTED; - - 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 set) { - } - - @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); - } - } 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 621bdad96..3073b3461 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 @@ -11,8 +11,6 @@ *******************************************************************************/ package org.simantics.db.impl.query; -import java.util.concurrent.Semaphore; - import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; 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 1883d9c1f..4d5279c51 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 @@ -14,7 +14,6 @@ package org.simantics.db.impl.query; 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; @@ -23,8 +22,6 @@ import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.procedure.AsyncMultiProcedure; import org.simantics.db.request.RequestFlags; -import gnu.trove.procedure.TIntProcedure; - /* * Size analysis: * java 8 byte @@ -38,7 +35,7 @@ import gnu.trove.procedure.TIntProcedure; * */ -final public class Objects extends CollectionBinaryQuery { +final public class Objects extends CollectionBinaryQuery implements IntProcedure { public Objects(final int r1, final int r2) { super(r1, r2); @@ -117,33 +114,21 @@ final public class Objects extends CollectionBinaryQuery { } - final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException { + final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final IntProcedure procedure) throws DatabaseException { - IntArray map = getAssertionMap(graph, r1, r2, entry); + 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; } int size = map.size(); if(size == 3) { - - int value = map.data[2]; - - if(entry != null) { - entry.addOrSetFunctional(value); - entry.finish(graph, procedure); - } else { - procedure.execute(graph, value); - procedure.finished(graph); - } - + int value = map.data[2]; + procedure.execute(graph, value); + procedure.finished(graph); } else if(size == 0) { - - if(entry != null) entry.finish(graph, procedure); - else procedure.finished(graph); - + procedure.finished(graph); } else { int candidateS = map.data[0]; @@ -151,9 +136,8 @@ final public class Objects 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; } @@ -173,9 +157,8 @@ final public class Objects extends CollectionBinaryQuery { 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; } @@ -200,8 +183,6 @@ final public class Objects extends CollectionBinaryQuery { // candidate and next are unrelated => error ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions " + r1 + ", " + r2 + " " + map , r1); - - if(entry != null) entry.except(exception); procedure.exception(graph, exception); return; @@ -213,13 +194,8 @@ final public class Objects extends CollectionBinaryQuery { } - if(entry != null) { - entry.addOrSetFunctional(candidateO); - entry.finish(graph, procedure); - } else { - procedure.execute(graph, candidateO); - procedure.finished(graph); - } + procedure.execute(graph, candidateO); + procedure.finished(graph); } @@ -260,7 +236,7 @@ final public class Objects extends CollectionBinaryQuery { } // 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) throws DatabaseException { + final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final RelationInfo ri, final IntProcedure procedure) throws DatabaseException { if(ri.isFinal) { @@ -269,7 +245,7 @@ final public class Objects extends CollectionBinaryQuery { if(result == 0) { // Check for assertions - forSingleAssertion(graph, r1, r2, entry, procedure); + forSingleAssertion(graph, r1, r2, parent, procedure); } else if (result == -1) { @@ -277,8 +253,7 @@ final public class Objects extends CollectionBinaryQuery { @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - if(entry != null) entry.addOrSetFunctional(i); - else procedure.execute(graph, i); + procedure.execute(graph, i); } @Override @@ -293,18 +268,13 @@ final public class Objects 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(result); - entry.finish(graph, procedure); - } else { - procedure.execute(graph, result); - procedure.finished(graph); - } + procedure.execute(graph, result); + procedure.finished(graph); } @@ -321,12 +291,11 @@ final public class Objects extends CollectionBinaryQuery { public void run(ReadGraphImpl graph) throws DatabaseException { if(found) { - if(entry != null) entry.finish(graph, procedure); - else procedure.finished(graph); + procedure.finished(graph); } else { // Check for assertions - forSingleAssertion(graph, r1, r2, entry, procedure); + forSingleAssertion(graph, r1, r2, parent, procedure); } @@ -347,15 +316,12 @@ final public class Objects extends CollectionBinaryQuery { if(!found) { - if(entry != null) entry.addOrSetFunctional(i); - else procedure.execute(graph, i); - + procedure.execute(graph, i); found = true; } else { ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1); - if(entry != null) entry.except(exception); procedure.exception(graph, exception); } @@ -375,7 +341,7 @@ final public class Objects extends CollectionBinaryQuery { } else { - 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 { @@ -392,15 +358,12 @@ final public class Objects extends CollectionBinaryQuery { if(!found) { - if(entry != null) entry.addOrSetFunctional(i); - else procedure.execute(graph, i); - + procedure.execute(graph, i); found = true; } else { ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1); - if(entry != null) entry.except(exception); procedure.exception(graph, exception); } @@ -447,25 +410,21 @@ 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) throws DatabaseException { + final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, final IntProcedure procedure) throws DatabaseException { // Note! The dependency is intentionally cut! QueryCache.runnerPrincipalTypes(graph, r1, null, 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 TripleIntProcedure() { @Override public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException { - if(entry != null) entry.addOrSet(o); - else procedure.execute(graph, o); + procedure.execute(graph, o); } @Override @@ -486,7 +445,7 @@ final public class Objects extends CollectionBinaryQuery { 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); } @@ -501,16 +460,14 @@ final public class Objects extends CollectionBinaryQuery { } final public static void computeNotFunctionalFinalIndex(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, RelationInfo ri, AsyncMultiProcedure procedure) { - throw new Error(); - } 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) throws DatabaseException { + final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Objects parent, RelationInfo ri, final IntProcedure procedure) throws DatabaseException { if(ri.isFinal) { @@ -518,8 +475,7 @@ final public class Objects extends CollectionBinaryQuery { @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - if(entry != null) entry.addOrSet(i); - else procedure.execute(graph, i); + procedure.execute(graph, i); } @Override @@ -535,10 +491,9 @@ final public class Objects 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 { @@ -549,9 +504,7 @@ final public class Objects extends CollectionBinaryQuery { @Override public void run(ReadGraphImpl graph) throws DatabaseException { - - forAssertions(graph, r1, r2, entry, procedure); - + forAssertions(graph, r1, r2, parent, procedure); } @Override @@ -566,8 +519,7 @@ final public class Objects extends CollectionBinaryQuery { @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - if(entry != null) entry.addOrSet(i); - else procedure.execute(graph, i); + procedure.execute(graph, i); } @Override @@ -587,7 +539,7 @@ final public class Objects 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 { @@ -601,8 +553,7 @@ final public class Objects extends CollectionBinaryQuery { @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - if(entry != null) entry.addOrSet(i); - else procedure.execute(graph, i); + procedure.execute(graph, i); } @Override @@ -654,15 +605,19 @@ final public class Objects extends CollectionBinaryQuery { return getResult(); } - public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure) throws DatabaseException { - - 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); - } + public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Objects entry, final IntProcedure procedure_) throws DatabaseException { + + IntProcedure procedure = entry != null ? entry : procedure_; + + 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_); } @@ -671,49 +626,49 @@ final public class Objects extends CollectionBinaryQuery { return "Objects[" + r1() + " - " + r2() + "]"; } - final private void finish(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException { - - assert(assertPending()); - - synchronized(this) { - setReady(); - } - - IntArray v = (IntArray)getResult(); - - if(v.data == null) { - if(v.sizeOrData != IntArray.NO_DATA) { - procedure.execute(graph, v.sizeOrData); - } - } else { - for(int i = 0;i < v.sizeOrData ; i++) { - procedure.execute(graph, v.data[i]); - } - } - - procedure.finished(graph); - - } - - final public void addOrSet(int add) { - - assert(assertPending()); - - IntArray value = (IntArray)getResult(); - synchronized(value) { - value.add(add); - } - - } - - final public void addOrSetFunctional(int add) { - - assert(isPending()); - - IntArray value = (IntArray)getResult(); - value.add(add); - - } +// final private void finish(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException { +// +// assert(assertPending()); +// +// synchronized(this) { +// setReady(); +// } +// +// IntArray v = (IntArray)getResult(); +// +// if(v.data == null) { +// if(v.sizeOrData != IntArray.NO_DATA) { +// procedure.execute(graph, v.sizeOrData); +// } +// } else { +// for(int i = 0;i < v.sizeOrData ; i++) { +// procedure.execute(graph, v.data[i]); +// } +// } +// +// procedure.finished(graph); +// +// } + +// final public void addOrSet(int add) { +// +// assert(assertPending()); +// +// IntArray value = (IntArray)getResult(); +// synchronized(value) { +// value.add(add); +// } +// +// } + +// final public void addOrSetFunctional(int add) { +// +// assert(isPending()); +// +// IntArray value = (IntArray)getResult(); +// value.add(add); +// +// } @Override public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { @@ -763,4 +718,22 @@ final public class Objects extends CollectionBinaryQuery { return graph.processor.isImmutable(r1()); } + @Override + public void execute(ReadGraphImpl graph, int i) throws DatabaseException { + IntArray value = (IntArray)getResult(); + synchronized(value) { + value.add(i); + } + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + setReady(); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException { + except(throwable); + } + } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/OrderedSet.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/OrderedSet.java index 9e7bb5c92..69ac07edd 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/OrderedSet.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/OrderedSet.java @@ -17,10 +17,8 @@ import org.simantics.db.common.exception.DebugException; import org.simantics.db.exception.DatabaseException; 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.ListenerBase; -final public class OrderedSet extends CollectionUnaryQuery { +final public class OrderedSet extends CollectionUnaryQuery { public OrderedSet(final int r) { super(r); @@ -31,7 +29,7 @@ final public class OrderedSet extends CollectionUnaryQuery { provider.cache.remove(this); } - private static int nextElement(ReadGraphImpl graph, int current, int orderedSet, OrderedSet entry, final IntProcedure procedure) throws DatabaseException { + private static int nextElement(ReadGraphImpl graph, int current, int orderedSet, OrderedSet parent, final IntProcedure procedure) throws DatabaseException { QueryProcessor processor = graph.processor; @@ -39,12 +37,11 @@ final public class OrderedSet extends CollectionUnaryQuery { AtomicInteger res = new AtomicInteger(0); - boolean found = processor.querySupport.getObjects(graph, current, orderedSet, new IntProcedure() { + processor.querySupport.getObjects(graph, current, orderedSet, new IntProcedure() { @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { if(i != orderedSet) { - if(entry != null) entry.addOrSet(i); procedure.execute(graph, i); } res.set(i); @@ -63,7 +60,6 @@ final public class OrderedSet extends CollectionUnaryQuery { }); if(res.get() == orderedSet) { - if(entry != null) entry.finish(graph, processor); procedure.finished(graph); } @@ -72,85 +68,26 @@ final public class OrderedSet extends CollectionUnaryQuery { } @Override - public void clearResult(QuerySupport support) { - setResult(new IntArray()); - } - - //@Override - public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { - computeForEach(graph, id, this, procedure); - return getResult(); + public void compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { + computeForEach(graph, id, this, procedure); } - static void computeForEach(ReadGraphImpl graph, int orderedSet, final OrderedSet entry, final IntProcedure procedure) throws DatabaseException { + static void computeForEach(ReadGraphImpl graph, int orderedSet, final OrderedSet entry, final IntProcedure procedure_) throws DatabaseException { + + IntProcedure procedure = entry != null ? entry : procedure_; int current = nextElement(graph, orderedSet, orderedSet, entry, procedure); while(current != orderedSet) { current = nextElement(graph, current, orderedSet, entry, procedure); } + if(entry != null) entry.performFromCache(graph, procedure_); + } @Override public String toString() { return "OrderedSet[" + id + "]"; } - - final private void finish(ReadGraphImpl graph, QueryProcessor provider) { - - assert(isPending()); - - synchronized(this) { - setReady(); - } - - } - - final public void addOrSet(int add) { - - assert(isPending()); - - IntArray value = (IntArray)getResult(); - value.add(add); - - } - - @Override - public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { - - assert(isReady()); - - if(handleException(graph, procedure)) return EXCEPTED; - - final IntArray value = (IntArray)getResult(); - if(value.data == null) { - if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData); - } else { - for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]); - } - - procedure.finished(graph); - - return value; - - } - - @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) { - throw new Error("Error in recompute.", t); - } - - }); - - } } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Predicates.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Predicates.java index 03b17bba5..9445db03b 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Predicates.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Predicates.java @@ -12,16 +12,14 @@ package org.simantics.db.impl.query; import org.simantics.db.common.exception.DebugException; -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.InternalProcedure; -import org.simantics.db.procedure.ListenerBase; import org.simantics.db.request.RequestFlags; import gnu.trove.procedure.TIntProcedure; -final public class Predicates extends UnaryQuery> { +final public class Predicates extends UnaryQueryP { Predicates(final int r) { super(r); @@ -32,9 +30,9 @@ final public class Predicates extends UnaryQuery> { provider.cache.remove(this); } - final static private void forAssertions(ReadGraphImpl graph, int r, Predicates entry, final IntSet set) throws DatabaseException { + final static private void forAssertions(ReadGraphImpl graph, int r, Predicates parent, final IntSet set) throws DatabaseException { - QueryCache.runnerPrincipalTypes(graph, r, entry, null, new SyncIntProcedure() { + QueryCache.runnerPrincipalTypes(graph, r, parent, null, new SyncIntProcedure() { @Override public void run(ReadGraphImpl graph) throws DatabaseException { @@ -63,7 +61,7 @@ final public class Predicates extends UnaryQuery> { public void execute(ReadGraphImpl graph, int type) throws DatabaseException { inc(); - QueryCache.runnerAssertedPredicates(graph, type, entry, null, proc); + QueryCache.runnerAssertedPredicates(graph, type, parent, null, proc); } @@ -77,13 +75,14 @@ final public class Predicates extends UnaryQuery> { } - //@Override - public Object compute(ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { + @Override + public void compute(ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { computeForEach(graph, id, this, procedure); - return getResult(); } - public static void computeForEach(ReadGraphImpl graph, final int r, final Predicates entry, final InternalProcedure procedure) throws DatabaseException { + public static void computeForEach(ReadGraphImpl graph, final int r, final Predicates entry, final InternalProcedure procedure_) throws DatabaseException { + + InternalProcedure procedure = entry != null ? entry : procedure_; IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null); @@ -91,16 +90,8 @@ final public class Predicates extends UnaryQuery> { forAssertions(graph, r, entry, result); if(result.isEmpty()) { - - if(entry != null) { - entry.setResult(direct); - entry.setReady(); - } - procedure.execute(graph, direct); - } else { - direct.forEach(new TIntProcedure() { @Override public boolean execute(int value) { @@ -108,16 +99,10 @@ final public class Predicates extends UnaryQuery> { return true; } }); - - if(entry != null) { - entry.setResult(result); - entry.setReady(); - } - procedure.execute(graph, result); - } - + + if(entry != null) entry.performFromCache(graph, procedure_); } @@ -126,61 +111,14 @@ final public class Predicates extends UnaryQuery> { return "Predicates[" + id + "]"; } - final public void finish(final ReadGraphImpl graph, QueryProcessor provider) { - - synchronized(this) { - setReady(); - } - - } - @Override public void clearResult(QuerySupport support) { setResult(new IntSet(support)); } - @Override - public Object performFromCache(final ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { - - assert(isReady()); - - if(handleException(graph, procedure)) return EXCEPTED; - - IntSet result = getResult(); - - procedure.execute(graph, result); - - return result; - - } - - @Override - public void recompute(ReadGraphImpl graph) throws DatabaseException { - - compute(graph, new InternalProcedure() { - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - throw new Error("Error in recompute.", t); - } - - @Override - public void execute(ReadGraphImpl graph, IntSet i) { - } - - }); - - } - @Override public int type() { return RequestFlags.IMMEDIATE_UPDATE; } - - - @Override - boolean isImmutable(ReadGraphImpl graph) { - return graph.processor.isImmutable(id); - } } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PrincipalTypes.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PrincipalTypes.java index af4d20ebc..a5a4a8997 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PrincipalTypes.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PrincipalTypes.java @@ -13,19 +13,17 @@ package org.simantics.db.impl.query; import java.util.ArrayList; import java.util.Arrays; -import java.util.concurrent.Semaphore; 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 org.simantics.db.impl.procedure.InternalProcedure; -import org.simantics.db.procedure.ListenerBase; import gnu.trove.procedure.TIntProcedure; import gnu.trove.set.hash.TIntHashSet; -final public class PrincipalTypes extends CollectionUnaryQuery { +final public class PrincipalTypes extends CollectionUnaryQuery { PrincipalTypes(final int resource) { super(resource); @@ -75,26 +73,34 @@ final public class PrincipalTypes extends CollectionUnaryQuery { } - //@Override - public Object compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException { - return computeForEach(graph, id, this, proc); + @Override + public void compute(final ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException { + computeForEach(graph, id, this, proc); } - public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException { + public static Object computeForEach(final ReadGraphImpl graph, final int id, final PrincipalTypes entry, final IntProcedure procedure_) throws DatabaseException { + + IntProcedure procedure = entry != null ? entry : procedure_; + + Object result = computeForEach2(graph, id, entry, procedure); + + if(entry != null) entry.performFromCache(graph, procedure_); + + return result; + + } + + public static Object computeForEach2(final ReadGraphImpl graph, final int id, final PrincipalTypes parent, final IntProcedure procedure) throws DatabaseException { - QueryProcessor provider = graph.processor; + QueryProcessor provider = graph.processor; provider.querySupport.ensureLoaded(graph, id); assert(id != 0); int ret = provider.querySupport.getSingleInstance(id); if(ret > 0) { - if(entry != null) { - entry.add(ret); - entry.finish(graph, provider); - } - proc.execute(graph, ret); - proc.finished(graph); + procedure.execute(graph, ret); + procedure.finished(graph); return ret; } @@ -114,7 +120,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery { @Override public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { - proc.exception(graph, t); + procedure.exception(graph, t); } @Override @@ -132,7 +138,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery { @Override public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { - proc.exception(graph, t); + procedure.exception(graph, t); } @Override @@ -148,26 +154,19 @@ final public class PrincipalTypes extends CollectionUnaryQuery { if(indirect.size() == 0) { int size = material.size(); if(size == 0) { - if(entry != null) entry.finish(graph, provider); - proc.finished(graph); + procedure.finished(graph); return null; } else if(size == 1) { int single = material.single; - if(entry != null) { - entry.add(single); - entry.finish(graph, provider); - } - proc.execute(graph, single); - proc.finished(graph); + procedure.execute(graph, single); + procedure.finished(graph); return single; } else { - addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc); + addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure); return null; } } -// final AtomicInteger finishes = new AtomicInteger(0); - indirect.forEach(new TIntProcedure() { int finishes = 0; @@ -190,27 +189,22 @@ final public class PrincipalTypes extends CollectionUnaryQuery { if(current == indirect.size()) { int size = material.size(); if(size == 0) { - if(entry != null) entry.finish(graph, provider); - proc.finished(graph); + procedure.finished(graph); return true; } else if(size == 1) { int single = material.single; - if(entry != null) { - entry.add(single); - entry.finish(graph, provider); - } - proc.execute(graph, single); - proc.finished(graph); + procedure.execute(graph, single); + procedure.finished(graph); return true; } else { - addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc); + addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure); return true; } } return true; } - QueryCache.runnerPrincipalTypes(graph, arg0, entry, null, new IntProcedure() { + QueryCache.runnerPrincipalTypes(graph, arg0, parent, null, new IntProcedure() { @Override public void execute(ReadGraphImpl graph, int i) { @@ -226,20 +220,15 @@ final public class PrincipalTypes extends CollectionUnaryQuery { if(current == indirect.size()) { int size = material.size(); if(size == 0) { - if(entry != null) entry.finish(graph, provider); - proc.finished(graph); + procedure.finished(graph); return; } else if(size == 1) { int single = material.single; - if(entry != null) { - entry.add(single); - entry.finish(graph, provider); - } - proc.execute(graph, single); - proc.finished(graph); + procedure.execute(graph, single); + procedure.finished(graph); return; } else { - addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, entry, proc); + addPrincipalType(graph, new TIntHashSet(4), material.toArray(), 0, provider, parent, procedure); return; } } @@ -248,7 +237,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery { @Override public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { - proc.exception(graph, t); + procedure.exception(graph, t); } }); @@ -263,16 +252,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery { } - private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final PrincipalTypes entry, final QueryProcessor provider, final IntProcedure proc) throws DatabaseException { - - if(entry != null) { - for(int i : material) { - if(!rejects.contains(i)) { - entry.add(i); - } - } - entry.finish(graph, provider); - } + private static void finish(ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, final IntProcedure proc) throws DatabaseException { for(int i : material) { if(!rejects.contains(i)) { @@ -286,26 +266,25 @@ final public class PrincipalTypes extends CollectionUnaryQuery { } - private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes entry, final IntProcedure proc) throws DatabaseException { - - // if((counter++ % 500) == 0) System.out.println("PT " + counter + " mat = " + material.length); + private static void addPrincipalType(final ReadGraphImpl graph, final TIntHashSet rejects, final int[] material, int index, final QueryProcessor provider, final PrincipalTypes parent, final IntProcedure proc) throws DatabaseException { if(index == material.length) { - finish(graph, rejects, material, entry, provider, proc); + finish(graph, rejects, material, proc); return; } int type = material[index++]; while(rejects.contains(type)) { if(index == material.length) { - finish(graph, rejects, material, entry, provider, proc); + finish(graph, rejects, material, proc); return; } type = material[index++]; } + final int nextIndex = index; - QueryCache.runnerSuperTypes(graph, type, entry, null, new InternalProcedure() { + QueryCache.runnerSuperTypes(graph, type, parent, null, new InternalProcedure() { @Override public void execute(ReadGraphImpl graph, IntSet supers) throws DatabaseException { @@ -324,7 +303,7 @@ final public class PrincipalTypes extends CollectionUnaryQuery { } - addPrincipalType(graph, rejects, material, nextIndex, provider, entry, proc); + addPrincipalType(graph, rejects, material, nextIndex, provider, parent, proc); } @@ -341,65 +320,6 @@ final public class PrincipalTypes extends CollectionUnaryQuery { public String toString() { return "PrincipalTypes[" + id + "]"; } - - final private void add(int val) { - assert(isPending()); - IntArray v = (IntArray)getResult(); - v.add(val); - } - - final private void finish(ReadGraphImpl graph, QueryProcessor provider) { - - assert(isPending()); - synchronized(this) { - setReady(); - } - - } - - @Override - public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException { - - assert(isReady()); - - if(handleException(graph, procedure)) return EXCEPTED; - - final IntArray value = (IntArray)getResult(); - if(value.data == null) { - if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData); - } else { - for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]); - } - - procedure.finished(graph); - - return getResult(); - - } - - @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(); - } - - }); - - } - - - @Override - boolean isImmutable(ReadGraphImpl graph) { - return graph.processor.isImmutable(id); - } @Override protected void fillImpliedParents(QueryProcessor processor, ArrayList> result) { diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java index 38ed21026..146701551 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java @@ -16,8 +16,6 @@ import org.simantics.db.request.ExternalRead; import org.simantics.db.request.MultiRead; import org.simantics.db.request.Read; -import gnu.trove.map.hash.TObjectIntHashMap; - public class QueryCache extends QueryCacheBase { public QueryCache(QuerySupport querySupport, int threads) { @@ -674,15 +672,15 @@ public class QueryCache extends QueryCacheBase { } } - AssertedStatements getOrCreateAssertedStatements(QueryProcessor processor, int r1, int r2) throws DatabaseException { - AssertedStatements existing = null; - synchronized(assertedStatementsMap) { - existing = (AssertedStatements)assertedStatementsMap.get(r1,r2); + TypeHierarchy getOrCreateTypeHierarchy(QueryProcessor processor, int r) throws DatabaseException { + TypeHierarchy existing = null; + synchronized(typeHierarchyMap) { + existing = (TypeHierarchy)typeHierarchyMap.get(r); if(existing == null) { - existing = new AssertedStatements(r1,r2); + existing = new TypeHierarchy(r); existing.clearResult(querySupport); existing.setPending(); - assertedStatementsMap.put(keyR2(r1,r2), existing); + typeHierarchyMap.put(keyR(r), existing); size++; return existing; } @@ -695,34 +693,38 @@ public class QueryCache extends QueryCacheBase { return existing; } - void remove(AssertedStatements entry) { - synchronized(assertedStatementsMap) { - assertedStatementsMap.remove(entry.id); + void remove(TypeHierarchy entry) { + synchronized(typeHierarchyMap) { + typeHierarchyMap.remove(entry.id); } } - public static void runnerAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException { + public static void runnerTypeHierarchy(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; - AssertedStatements entry = (AssertedStatements)cache.getOrCreateAssertedStatements(graph.processor, r1,r2); - TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedStatements; + if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { + TypeHierarchy.computeForEach(graph, r, null, procedure); + return; + } + TypeHierarchy entry = (TypeHierarchy)cache.getOrCreateTypeHierarchy(graph.processor, r); + InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureTypeHierarchy; ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false); if(entry.isReady()) entry.performFromCache(graph, procedure_); else { assert(entry.isPending()); - entry.compute(graph, procedure_); + TypeHierarchy.computeForEach(graph, r, entry, procedure_); if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult()); } } - AssertedPredicates getOrCreateAssertedPredicates(QueryProcessor processor, int r) throws DatabaseException { - AssertedPredicates existing = null; - synchronized(assertedPredicatesMap) { - existing = (AssertedPredicates)assertedPredicatesMap.get(r); + SuperTypes getOrCreateSuperTypes(QueryProcessor processor, int r) throws DatabaseException { + SuperTypes existing = null; + synchronized(superTypesMap) { + existing = (SuperTypes)superTypesMap.get(r); if(existing == null) { - existing = new AssertedPredicates(r); + existing = new SuperTypes(r); existing.clearResult(querySupport); existing.setPending(); - assertedPredicatesMap.put(keyR(r), existing); + superTypesMap.put(keyR(r), existing); size++; return existing; } @@ -735,34 +737,38 @@ public class QueryCache extends QueryCacheBase { return existing; } - void remove(AssertedPredicates entry) { - synchronized(assertedPredicatesMap) { - assertedPredicatesMap.remove(entry.id); + void remove(SuperTypes entry) { + synchronized(superTypesMap) { + superTypesMap.remove(entry.id); } } - public static void runnerAssertedPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException { + public static void runnerSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; - AssertedPredicates entry = (AssertedPredicates)cache.getOrCreateAssertedPredicates(graph.processor, r); - IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedPredicates; + if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { + SuperTypes.computeForEach(graph, r, null, procedure); + return; + } + SuperTypes entry = (SuperTypes)cache.getOrCreateSuperTypes(graph.processor, r); + InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureSuperTypes; ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false); if(entry.isReady()) entry.performFromCache(graph, procedure_); else { assert(entry.isPending()); - entry.compute(graph, procedure_); + SuperTypes.computeForEach(graph, r, entry, procedure_); if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult()); } } - DirectSuperRelations getOrCreateDirectSuperRelations(QueryProcessor processor, int r) throws DatabaseException { - DirectSuperRelations existing = null; - synchronized(directSuperRelationsMap) { - existing = (DirectSuperRelations)directSuperRelationsMap.get(r); + SuperRelations getOrCreateSuperRelations(QueryProcessor processor, int r) throws DatabaseException { + SuperRelations existing = null; + synchronized(superRelationsMap) { + existing = (SuperRelations)superRelationsMap.get(r); if(existing == null) { - existing = new DirectSuperRelations(r); + existing = new SuperRelations(r); existing.clearResult(querySupport); existing.setPending(); - directSuperRelationsMap.put(keyR(r), existing); + superRelationsMap.put(keyR(r), existing); size++; return existing; } @@ -775,34 +781,38 @@ public class QueryCache extends QueryCacheBase { return existing; } - void remove(DirectSuperRelations entry) { - synchronized(directSuperRelationsMap) { - directSuperRelationsMap.remove(entry.id); + void remove(SuperRelations entry) { + synchronized(superRelationsMap) { + superRelationsMap.remove(entry.id); } } - public static void runnerDirectSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException { + public static void runnerSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; - DirectSuperRelations entry = (DirectSuperRelations)cache.getOrCreateDirectSuperRelations(graph.processor, r); - IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectSuperRelations; + if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { + SuperRelations.computeForEach(graph, r, null, procedure); + return; + } + SuperRelations entry = (SuperRelations)cache.getOrCreateSuperRelations(graph.processor, r); + InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureSuperRelations; ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false); if(entry.isReady()) entry.performFromCache(graph, procedure_); else { assert(entry.isPending()); - entry.compute(graph, procedure_); + SuperRelations.computeForEach(graph, r, entry, procedure_); if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult()); } } - SuperTypes getOrCreateSuperTypes(QueryProcessor processor, int r) throws DatabaseException { - SuperTypes existing = null; - synchronized(superTypesMap) { - existing = (SuperTypes)superTypesMap.get(r); + AssertedPredicates getOrCreateAssertedPredicates(QueryProcessor processor, int r) throws DatabaseException { + AssertedPredicates existing = null; + synchronized(assertedPredicatesMap) { + existing = (AssertedPredicates)assertedPredicatesMap.get(r); if(existing == null) { - existing = new SuperTypes(r); + existing = new AssertedPredicates(r); existing.clearResult(querySupport); existing.setPending(); - superTypesMap.put(keyR(r), existing); + assertedPredicatesMap.put(keyR(r), existing); size++; return existing; } @@ -815,16 +825,16 @@ public class QueryCache extends QueryCacheBase { return existing; } - void remove(SuperTypes entry) { - synchronized(superTypesMap) { - superTypesMap.remove(entry.id); + void remove(AssertedPredicates entry) { + synchronized(assertedPredicatesMap) { + assertedPredicatesMap.remove(entry.id); } } - public static void runnerSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { + public static void runnerAssertedPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; - SuperTypes entry = (SuperTypes)cache.getOrCreateSuperTypes(graph.processor, r); - InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureSuperTypes; + AssertedPredicates entry = (AssertedPredicates)cache.getOrCreateAssertedPredicates(graph.processor, r); + IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedPredicates; ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false); if(entry.isReady()) entry.performFromCache(graph, procedure_); else { @@ -834,15 +844,15 @@ public class QueryCache extends QueryCacheBase { } } - TypeHierarchy getOrCreateTypeHierarchy(QueryProcessor processor, int r) throws DatabaseException { - TypeHierarchy existing = null; - synchronized(typeHierarchyMap) { - existing = (TypeHierarchy)typeHierarchyMap.get(r); + AssertedStatements getOrCreateAssertedStatements(QueryProcessor processor, int r1, int r2) throws DatabaseException { + AssertedStatements existing = null; + synchronized(assertedStatementsMap) { + existing = (AssertedStatements)assertedStatementsMap.get(r1,r2); if(existing == null) { - existing = new TypeHierarchy(r); + existing = new AssertedStatements(r1,r2); existing.clearResult(querySupport); existing.setPending(); - typeHierarchyMap.put(keyR(r), existing); + assertedStatementsMap.put(keyR2(r1,r2), existing); size++; return existing; } @@ -855,16 +865,16 @@ public class QueryCache extends QueryCacheBase { return existing; } - void remove(TypeHierarchy entry) { - synchronized(typeHierarchyMap) { - typeHierarchyMap.remove(entry.id); + void remove(AssertedStatements entry) { + synchronized(assertedStatementsMap) { + assertedStatementsMap.remove(entry.id); } } - public static void runnerTypeHierarchy(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { + public static void runnerAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; - TypeHierarchy entry = (TypeHierarchy)cache.getOrCreateTypeHierarchy(graph.processor, r); - InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureTypeHierarchy; + AssertedStatements entry = (AssertedStatements)cache.getOrCreateAssertedStatements(graph.processor, r1,r2); + TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedStatements; ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false); if(entry.isReady()) entry.performFromCache(graph, procedure_); else { @@ -874,15 +884,15 @@ public class QueryCache extends QueryCacheBase { } } - SuperRelations getOrCreateSuperRelations(QueryProcessor processor, int r) throws DatabaseException { - SuperRelations existing = null; - synchronized(superRelationsMap) { - existing = (SuperRelations)superRelationsMap.get(r); + DirectSuperRelations getOrCreateDirectSuperRelations(QueryProcessor processor, int r) throws DatabaseException { + DirectSuperRelations existing = null; + synchronized(directSuperRelationsMap) { + existing = (DirectSuperRelations)directSuperRelationsMap.get(r); if(existing == null) { - existing = new SuperRelations(r); + existing = new DirectSuperRelations(r); existing.clearResult(querySupport); existing.setPending(); - superRelationsMap.put(keyR(r), existing); + directSuperRelationsMap.put(keyR(r), existing); size++; return existing; } @@ -895,16 +905,16 @@ public class QueryCache extends QueryCacheBase { return existing; } - void remove(SuperRelations entry) { - synchronized(superRelationsMap) { - superRelationsMap.remove(entry.id); + void remove(DirectSuperRelations entry) { + synchronized(directSuperRelationsMap) { + directSuperRelationsMap.remove(entry.id); } } - public static void runnerSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { + public static void runnerDirectSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; - SuperRelations entry = (SuperRelations)cache.getOrCreateSuperRelations(graph.processor, r); - InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureSuperRelations; + DirectSuperRelations entry = (DirectSuperRelations)cache.getOrCreateDirectSuperRelations(graph.processor, r); + IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectSuperRelations; ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false); if(entry.isReady()) entry.performFromCache(graph, procedure_); else { diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java index 322e820a2..2d3c92d82 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java @@ -640,7 +640,7 @@ public class QueryCacheBase { int counter = 0; while(entry.isPending()) { try { - SessionTask task = processor.getOwnTask(processor.thread.get()); + SessionTask task = null;//processor.getOwnTask(processor.thread.get()); if(task != null) { task.run(processor.thread.get()); } else { diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java index 115a4a5a1..56267cc12 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java @@ -14,185 +14,167 @@ package org.simantics.db.impl.query; import org.simantics.db.AsyncReadGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; -import org.simantics.db.impl.graph.WriteGraphImpl; import org.simantics.db.procedure.AsyncProcedure; import org.simantics.db.request.Read; import org.simantics.db.request.ReadExt; import org.simantics.db.request.RequestFlags; -final public class ReadEntry extends CacheEntryBase> { +final public class ReadEntry extends CacheEntryBase> implements AsyncProcedure { - protected Read request; + protected Read request; - public ReadEntry(Read request) { - this.request = request; + public ReadEntry(Read request) { + this.request = request; } - + @Override int makeHash() { - return request.hashCode(); + return request.hashCode(); } - + @Override public Object getOriginalRequest() { return request; } - + @Override public void discard() { - super.discard(); - setResult(null); - } - - final public Object addOrSet(AsyncReadGraph graph, Object result) { - -// System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + result); - - assert(assertPending()); - - setResult(result); - setReady(); - - return result; - + super.discard(); + setResult(null); } @Override final public Query getQuery() { - + return new Query() { - @Override - public void recompute(ReadGraphImpl graph) { - - try { + @Override + public void recompute(ReadGraphImpl graph) { + + try { + + T result = request.perform(graph); + setResult(result); + setReady(); + + } catch (Throwable t) { + + except(t); - T result = request.perform(graph); - addOrSet(graph, result); + } + + } - } catch (Throwable t) { + @Override + public void removeEntry(QueryProcessor processor) { + processor.cache.remove(ReadEntry.this); + } - except(t); - + @Override + public int type() { + if (request instanceof ReadExt) { + return ((ReadExt) request).getType(); + } else { + return RequestFlags.INVALIDATE; } - - } - - @Override - public void removeEntry(QueryProcessor processor) { - processor.cache.remove(ReadEntry.this); - } - - @Override - public int type() { - if(request instanceof ReadExt) { - return ((ReadExt)request).getType(); - } else { - return RequestFlags.INVALIDATE; - } - } - - @Override - public String toString() { - if(request == null) return "DISCARDED"; - else return request.toString() + statusOrException; - } - + } + + @Override + public String toString() { + if (request == null) + return "DISCARDED"; + else + return request.toString() + statusOrException; + } + }; - + } - - //@Override - public Object compute(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { - ReadGraphImpl queryGraph = graph.withParent(this); + public static void computeForEach(ReadGraphImpl graph, Read request, ReadEntry entry, + AsyncProcedure procedure_) throws DatabaseException { - try { + AsyncProcedure procedure = entry != null ? entry : procedure_; - addOrSet(queryGraph, request.perform(queryGraph)); - return get(queryGraph, procedure); + ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph; - } catch (DatabaseException e) { + try { - except(e); - return get(graph, procedure); + T result = request.perform(queryGraph); + procedure.execute(graph, result); - } catch (Throwable t) { + } catch (DatabaseException e) { - except(new DatabaseException(t)); - return get(graph, procedure); + procedure.exception(graph, e); - } - - } + } catch (Throwable t) { - public static void computeForEach(ReadGraphImpl graph, Read request, ReadEntry entry, AsyncProcedure procedure) throws DatabaseException { - - ReadGraphImpl queryGraph = entry != null ? graph.withParent(entry) : graph; + DatabaseException dbe = new DatabaseException(t); + procedure.exception(graph, dbe); - try { + } - T result = request.perform(queryGraph); - if(entry != null) entry.addOrSet(queryGraph, result); - procedure.execute(graph, result); + if (entry != null) + entry.performFromCache(queryGraph, procedure_); - } catch (DatabaseException e) { + } - if(entry != null) entry.except(e); - procedure.exception(graph, e); + public Object performFromCache(ReadGraphImpl graph, AsyncProcedure procedure) { - } catch (Throwable t) { + AsyncProcedure proc = (AsyncProcedure) procedure; - DatabaseException dbe = new DatabaseException(t); - if(entry != null) entry.except(dbe); - procedure.exception(graph, dbe); + if (proc != null) { + if (isExcepted()) { + try { + proc.exception(graph, (Throwable) getResult()); + } catch (Throwable t) { + t.printStackTrace(); + } + } else { + try { + proc.execute(graph, (T) getResult()); + } catch (Throwable t) { + t.printStackTrace(); + } + } + } + + return (T) getResult(); - } - } - - public Object performFromCache(ReadGraphImpl graph, AsyncProcedure procedure) { - - AsyncProcedure proc = (AsyncProcedure)procedure; - - if(proc != null) { - if(isExcepted()) { - try { - proc.exception(graph, (Throwable)getResult()); - } catch (Throwable t) { - t.printStackTrace(); - } - } else { - try { - proc.execute(graph, (T)getResult()); - } catch (Throwable t) { - t.printStackTrace(); - } - } + + @Override + public String toString() { + if (request == null) + return "DISCARDED"; + else + return request.toString() + " - " + statusOrException; + } + + public Object get(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { + if (procedure != null) + performFromCache(graph, procedure); + checkAndThrow(); + return getResult(); + } + + @Override + boolean isImmutable(ReadGraphImpl graph) throws DatabaseException { + if (request instanceof ReadExt) { + return ((ReadExt) request).isImmutable(graph); } - - return (T)getResult(); - - } - - @Override - public String toString() { - if(request == null) return "DISCARDED"; - else return request.toString() + " - " + statusOrException; - } - - public Object get(ReadGraphImpl graph, AsyncProcedure procedure) throws DatabaseException { - if(procedure != null) performFromCache(graph, procedure); - checkAndThrow(); - return getResult(); - } - - @Override - boolean isImmutable(ReadGraphImpl graph) throws DatabaseException { - if(request instanceof ReadExt) { - return ((ReadExt)request).isImmutable(graph); - } - return false; - } + return false; + } + + @Override + public void execute(AsyncReadGraph graph, T result) { + setResult(result); + setReady(); + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + except(throwable); + } } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/RelationInfoQuery.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/RelationInfoQuery.java index 204a0005a..b0a2a1198 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/RelationInfoQuery.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/RelationInfoQuery.java @@ -17,7 +17,7 @@ import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.request.RequestFlags; -final public class RelationInfoQuery extends UnaryQuery> { +final public class RelationInfoQuery extends UnaryQueryP { RelationInfoQuery(final int resource) { super(resource); @@ -28,14 +28,14 @@ final public class RelationInfoQuery extends UnaryQuery proc) throws DatabaseException { + private static void computeAssertions(ReadGraphImpl graph, int r, final boolean isFinal, final boolean isFunctional, RelationInfoQuery parent, final InternalProcedure procedure) throws DatabaseException { QueryProcessor processor = graph.processor; final int isUsedInAssertion = processor.getHasPredicateInverse(); assert(isUsedInAssertion != 0); - QueryCache.runnerDirectObjects(graph, r, isUsedInAssertion, entry, null, new IntProcedure() { + QueryCache.runnerDirectObjects(graph, r, isUsedInAssertion, parent, null, new IntProcedure() { boolean done = false; @@ -44,8 +44,7 @@ final public class RelationInfoQuery extends UnaryQuery procedure) throws DatabaseException { + public static void computeForEach(ReadGraphImpl graph, int r, RelationInfoQuery entry, InternalProcedure procedure_) throws DatabaseException { + + InternalProcedure procedure = entry != null ? entry : procedure_; QueryProcessor provider = graph.processor; @@ -81,77 +80,30 @@ final public class RelationInfoQuery extends UnaryQuery procedure) throws DatabaseException { + @Override + public void compute(ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { computeForEach(graph, id, this, procedure); - return getResult(); } @Override public String toString() { return "RelationInfoQuery[" + id + "]"; } - -// public void addOrSet(ReadGraphImpl graph, final RelationInfo result, final QueryProcessor provider) { -// -// assert(isPending()); -// -// synchronized(this) { -// -// setResult(result); -// setReady(); -// -// } -// -// } @Override public void setResult(Object result) { super.setResult(result); - if(!(result instanceof RelationInfo) && !(result == NO_RESULT)) - System.err.println("foo"); setReady(); } - - @Override - public Object performFromCache(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { - - assert(isReady()); - - if(handleException(graph, procedure)) return EXCEPTED; - - RelationInfo result = getResult(); - - procedure.execute(graph, result); - - return result; - - } @Override public int type() { return RequestFlags.IMMEDIATE_UPDATE; } - - @Override - public void recompute(ReadGraphImpl graph) throws DatabaseException { - - compute(graph, new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, RelationInfo result) { - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - throw new Error("Error in recompute.", t); - } - - }); - - } } 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 ad4700aa8..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,16 +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 { - - if(entry != null) { - assert(entry.isPending()); - } + 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, entry); + 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; } @@ -153,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 { @@ -174,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)) { @@ -231,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; - } } @@ -247,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); } @@ -272,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) { @@ -281,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) { @@ -289,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 @@ -305,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); } @@ -333,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); } } @@ -360,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); - } } @@ -391,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 { @@ -412,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); - } } @@ -468,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 @@ -503,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 @@ -524,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) { @@ -532,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 @@ -549,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 { @@ -563,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 @@ -580,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 @@ -603,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(); @@ -613,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); } @@ -652,32 +580,19 @@ final public class Statements extends CollectionBinaryQuery } - public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) throws DatabaseException { - - if(entry != null) { - assert(entry.isPending()); - } + 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_); } @@ -789,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); + } } 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); - } } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java index a01c84699..c7efade25 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java @@ -14,38 +14,29 @@ package org.simantics.db.impl.query; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; -import org.simantics.db.procedure.ListenerBase; import gnu.trove.procedure.TIntProcedure; -final public class SuperTypes extends UnaryQuery> { +final public class SuperTypes extends UnaryQueryP { SuperTypes(final int resource) { super(resource); } - -// final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { -// return QueryCache.runnerSuperTypes(graph, r, parent, listener, procedure); -// } @Override final public void removeEntry(QueryProcessor provider) { provider.cache.remove(this); } - //@Override - public Object compute(ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { - return computeForEach(graph, id, this, procedure); - } - - private static void addOrSet(ReadGraphImpl graph, SuperTypes entry, IntSet value) { - if(entry != null) { - entry.addOrSet(graph, value, graph.processor); - } + @Override + public void compute(ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { + computeForEach(graph, id, this, procedure); } - public static Object computeForEach(ReadGraphImpl graph, int r, SuperTypes entry, final InternalProcedure procedure) throws DatabaseException { + public static Object computeForEach(ReadGraphImpl graph, int r, SuperTypes entry, final InternalProcedure procedure_) throws DatabaseException { + InternalProcedure procedure = entry != null ? entry : procedure_; + QueryProcessor provider = graph.processor; final int inherits = provider.getInherits(); @@ -66,17 +57,12 @@ final public class SuperTypes extends UnaryQuery> { @Override public void run(ReadGraphImpl graph) throws DatabaseException { - - addOrSet(graph, entry, result); procedure.execute(graph, result); - } @Override public void execute(ReadGraphImpl graph, final int i) throws DatabaseException { -// assert(graph.parent == parent); - synchronized(result) { result.add(i); } @@ -109,65 +95,16 @@ final public class SuperTypes extends UnaryQuery> { }); - return result; - - } - - @Override - public String toString() { - return "SuperTypes2[" + 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); + if(entry != null) entry.performFromCache(graph, procedure_); 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(); - } - - }); - + public String toString() { + return "SuperTypes[" + id + "]"; } - @Override - boolean isImmutable(ReadGraphImpl graph) { - return graph.processor.isImmutable(id); - } } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java index ad4581bb6..cc8ee07de 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java @@ -18,7 +18,7 @@ import org.simantics.db.procedure.ListenerBase; import gnu.trove.procedure.TIntProcedure; -final public class TypeHierarchy extends UnaryQuery> { +final public class TypeHierarchy extends UnaryQueryP { TypeHierarchy(final int resource) { super(resource); @@ -33,8 +33,14 @@ final public class TypeHierarchy extends UnaryQuery> { provider.cache.remove(this); } - //@Override - public IntSet compute(ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { + @Override + public void compute(final ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { + computeForEach(graph, id, this, procedure); + } + + public static IntSet computeForEach(ReadGraphImpl graph, int id, TypeHierarchy entry, final InternalProcedure procedure_) throws DatabaseException { + + InternalProcedure procedure = entry != null ? entry : procedure_; QueryProcessor processor = graph.processor; @@ -48,15 +54,12 @@ final public class TypeHierarchy extends UnaryQuery> { } }; - QueryCache.runnerSuperTypes(graph, id, TypeHierarchy.this, null, new InternalProcedure() { + QueryCache.runnerSuperTypes(graph, id, entry, null, new InternalProcedure() { @Override public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException { - types.forEach(addToResult); - addOrSet(graph, result, processor); procedure.execute(graph, result); - } @Override @@ -66,65 +69,15 @@ final public class TypeHierarchy extends UnaryQuery> { }); - return result; - - } - - @Override - public String toString() { - return "TypeHierarchy[" + 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); + if(entry != null) entry.performFromCache(graph, procedure_); 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); + public String toString() { + return "TypeHierarchy[" + id + "]"; } } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java index 512045e17..35c43885a 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java @@ -19,154 +19,156 @@ import org.simantics.db.impl.procedure.InternalProcedure; import gnu.trove.procedure.TIntProcedure; -final public class Types extends UnaryQuery> { - +final public class Types extends UnaryQueryP { + Types(final int resource) { super(resource); } - @Override - final public void removeEntry(QueryProcessor provider) { - provider.cache.remove(this); - } + @Override + final public void removeEntry(QueryProcessor provider) { + provider.cache.remove(this); + } - //@Override - public Object compute(final ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { - computeForEach(graph, id, this, procedure); - return getResult(); - } + @Override + public void compute(final ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { + computeForEach(graph, id, this, procedure); + } - public static void computeForEach(final ReadGraphImpl graph, int id, Types entry, final InternalProcedure procedure) throws DatabaseException { + public static void computeForEach(final ReadGraphImpl graph, int id, Types entry, + final InternalProcedure procedure_) throws DatabaseException { - assert(procedure != null); + InternalProcedure procedure = entry != null ? entry : procedure_; + + computeForEach2(graph, id, entry, procedure); + + if (entry != null) + entry.performFromCache(graph, procedure_); + + } - QueryProcessor processor = graph.processor; + public static void computeForEach2(final ReadGraphImpl graph, int id, Types parent, + final InternalProcedure procedure) throws DatabaseException { - processor.querySupport.ensureLoaded(graph, id); - - int ret = processor.querySupport.getSingleInstance(id); - if(ret > 0) { + QueryProcessor processor = graph.processor; - TypeHierarchy.queryEach(graph, ret, processor, entry, null, new InternalProcedure() { + processor.querySupport.ensureLoaded(graph, id); - @Override - public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException { + int ret = processor.querySupport.getSingleInstance(id); + if (ret > 0) { - if(entry != null) { - entry.addOrSet(graph, types, processor); - entry.finish(); - } - procedure.execute(graph, types); + TypeHierarchy.queryEach(graph, ret, processor, parent, null, new InternalProcedure() { - } + @Override + public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException { + procedure.execute(graph, types); + } - @Override - public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { - procedure.exception(graph, t); - } + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); + } - }); - - return; + }); - } - - final int instanceOf = processor.getInstanceOf(); + return; + + } + + final int instanceOf = processor.getInstanceOf(); final int inherits = processor.getInherits(); final int subrelationOf = processor.getSubrelationOf(); final IntSet result = new IntSet(processor.querySupport); - + final TIntProcedure addToResult = new TIntProcedure() { @Override public boolean execute(int r) { - synchronized(result) { - result.add(r); - } + synchronized (result) { + result.add(r); + } return true; } }; - + final AtomicInteger finishes = new AtomicInteger(0); - + SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() { - + @Override public void run(ReadGraphImpl graph) throws DatabaseException { - - if(finishes.addAndGet(1) == 3) { - if(entry != null) entry.addOrSet(graph, result, processor); + + if (finishes.addAndGet(1) == 3) { procedure.execute(graph, result); - } - + } + } - + @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - - result.add(i); - + + result.add(i); + inc(); - QueryCache.runnerSuperTypes(graph, i, entry, null, new InternalProcedure() { + QueryCache.runnerSuperTypes(graph, i, parent, null, new InternalProcedure() { @Override public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException { types.forEach(addToResult); dec(graph); } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { - procedure.exception(graph, t); + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); dec(graph); - } + } }); - + } @Override public void finished(ReadGraphImpl graph) throws DatabaseException { dec(graph); } - - }; - + + }; + SyncIntProcedure inheritsProcedure = new SyncIntProcedure() { - + @Override public void run(ReadGraphImpl graph) throws DatabaseException { - int current = finishes.addAndGet(1); - if(current == 3) { - if(entry != null) entry.addOrSet(graph, result, processor); + int current = finishes.addAndGet(1); + if (current == 3) { procedure.execute(graph, result); - } - + } + } - + @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - + inc(); - QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure() { + QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure() { @Override public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException { types.forEach(addToResult); dec(graph); } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { - procedure.exception(graph, t); + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); dec(graph); - } + } }); - + } @Override @@ -175,124 +177,67 @@ final public class Types extends UnaryQuery> { dec(graph); } - - }; + + }; SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() { @Override public void run(ReadGraphImpl graph) throws DatabaseException { - int current = finishes.addAndGet(1); - if(current == 3) { - if(entry != null) entry.addOrSet(graph, result, processor); + int current = finishes.addAndGet(1); + if (current == 3) { procedure.execute(graph, result); - } - + } + } - + @Override public void execute(ReadGraphImpl graph, int i) throws DatabaseException { - + inc(); - - QueryCache.runnerTypes(graph, i, entry, null, new InternalProcedure() { + + QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure() { @Override public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException { types.forEach(addToResult); dec(graph); - + } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { - procedure.exception(graph, t); + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); dec(graph); - } + } }); - + } @Override public void finished(ReadGraphImpl graph) throws DatabaseException { - + dec(graph); } - - }; - + + }; + processor.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure); instanceOfProcedure.finished(graph); processor.querySupport.getObjects(graph, id, inherits, inheritsProcedure); inheritsProcedure.finished(graph); processor.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure); subrelationOfProcedure.finished(graph); - - if(entry != null) entry.finish(); - - } - - @Override - public String toString() { - return "Types[" + id + "]"; - } - - private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) { - - assert(!isReady()); - setResult(value); - } - - void finish() { - IntSet result = getResult(); - result.trim(); - setReady(); - - } - - @Override - final public Object performFromCache(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { - - assert(isReady()); - - if(handleException(graph, procedure)) return EXCEPTED; - - 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(); - } - - }); - + public String toString() { + return "Types[" + id + "]"; } - @Override - boolean isImmutable(ReadGraphImpl graph) { - return graph.processor.isImmutable(id); - } - } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java index 918f882f1..547264f4b 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java @@ -19,7 +19,7 @@ import org.simantics.db.exception.ResourceNotFoundException; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; -public class URIToResource extends StringQuery> { +public class URIToResource extends StringQuery> implements InternalProcedure { URIToResource(final String id) { super(id); @@ -36,12 +36,13 @@ public class URIToResource extends StringQuery> { return getResult(); } - static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure procedure) throws DatabaseException { + static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure procedure_) throws DatabaseException { + + InternalProcedure procedure = entry != null ? entry : procedure_; if("http://".equals(id) || "http:/".equals(id)) { QueryProcessor processor = graph.processor; - if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary()); procedure.execute(graph, processor.getRootLibrary()); } else { @@ -49,8 +50,6 @@ public class URIToResource extends StringQuery> { final String[] parts = URIStringUtils.splitURI(id); if (parts != null) { - //Integer parentId = QueryCache.resultURIToResource(graph, parts[0], entry, null); - QueryCache.runnerURIToResource(graph, parts[0], entry, null, new InternalProcedure() { @Override @@ -58,31 +57,17 @@ public class URIToResource extends StringQuery> { ObjectResourceIdMap map = QueryCache.resultChildMap(graph, parentId, entry, null); assert(map != null); -// if(map == null) { -// throw new DatabaseException("Internal Error, contact application support."); -// if(entry != null) entry.except(e); -// procedure.exception(graph, e); -//// procedure.execute(graph, 0); -//// if(entry != null) entry.addOrSet(graph, graph.processor, 0); -// } else { - int result = map.getId(URIStringUtils.unescape(parts[1])); - if(result == 0) { - ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id); - if(entry != null) entry.except(e); - procedure.exception(graph, e); - } else { - if(entry != null) entry.addOrSet(graph, graph.processor, result); - procedure.execute(graph, result); - } -// } - - // TODO Auto-generated method stub - + int result = map.getId(URIStringUtils.unescape(parts[1])); + if (result == 0) { + ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id); + procedure.exception(graph, e); + } else { + procedure.execute(graph, result); + } } @Override public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException { - if(entry != null) entry.except(throwable); procedure.exception(graph, throwable); } @@ -90,18 +75,17 @@ public class URIToResource extends StringQuery> { } else { - ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id); - if(entry != null) entry.except(e); - procedure.exception(graph, e); - + procedure.exception(graph, e); } } + if(entry != null) entry.performFromCache(graph, procedure_); + } - public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) { + public void addOrSet(Integer result) { assert(isPending()); @@ -148,5 +132,18 @@ public class URIToResource extends StringQuery> { }); } + + @Override + public void execute(ReadGraphImpl graph, Integer result) throws DatabaseException { + synchronized(this) { + setResult(result); + setReady(); + } + } + + @Override + public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException { + except(throwable); + } } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQueryP.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQueryP.java new file mode 100644 index 000000000..08ebff9e4 --- /dev/null +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQueryP.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * 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 org.simantics.db.common.exception.DebugException; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.impl.graph.ReadGraphImpl; +import org.simantics.db.impl.procedure.InternalProcedure; + +abstract public class UnaryQueryP extends UnaryQuery> implements InternalProcedure { + + public UnaryQueryP(int r) { + super(r); + } + + abstract public void compute(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException; + + @Override + final public void recompute(ReadGraphImpl graph) throws DatabaseException { + + compute(graph, new InternalProcedure() { + + @Override + public void execute(ReadGraphImpl graph, R result) { + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) { + if (DebugException.DEBUG) + new DebugException(t).printStackTrace(); + throw new Error("Error in recompute.", t); + } + + }); + + } + + @Override + final public Object performFromCache(ReadGraphImpl graph, InternalProcedure procedure) + throws DatabaseException { + + if (handleException(graph, procedure)) + return (Throwable) statusOrException; + + procedure.execute(graph, (R)getResult()); + + return result; + + } + + @Override + final public void execute(ReadGraphImpl graph, R result) throws DatabaseException { + setResult(result); + setReady(); + } + + @Override + final public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException { + except(throwable); + } + + @Override + final boolean isImmutable(ReadGraphImpl graph) { + return graph.processor.isImmutable(id); + } + +} diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java index b96e13d42..e533ee30f 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java @@ -15,7 +15,7 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; -final public class ValueQuery extends UnaryQuery> { +final public class ValueQuery extends UnaryQueryP { ValueQuery(final int resource) { super(resource); @@ -25,59 +25,30 @@ final public class ValueQuery extends UnaryQuery> { final public void removeEntry(QueryProcessor provider) { provider.cache.remove(this); } + + @Override + public void compute(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { + computeForEach(graph, id, this, procedure); + } - public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure procedure) throws DatabaseException { + public static byte[] computeForEach(ReadGraphImpl graph, final int r, final ValueQuery entry, final InternalProcedure procedure_) throws DatabaseException { + + InternalProcedure procedure = entry != null ? entry : procedure_; graph.ensureLoaded(r); byte[] value = graph.getValue(r); - if(entry != null) { - entry.setResult(value); - entry.setReady(); - } - if(procedure != null) { - procedure.execute(graph, value); - } + if(procedure != null) procedure.execute(graph, value); + + if(entry != null && procedure_ != null) entry.performFromCache(graph, procedure_); return value; } - -// public Object compute(ReadGraphImpl graph, final InternalProcedure procedure) throws DatabaseException { -// return computeForEach(graph, id, this, procedure); -// } @Override public String toString() { return "Value[" + id + "]"; } - - @Override - public Object performFromCache(ReadGraphImpl graph, InternalProcedure procedure) throws DatabaseException { - return computeForEach(graph, id, this, procedure); - } - - @Override - public void recompute(ReadGraphImpl graph) throws DatabaseException { - - computeForEach(graph, id, this, new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, byte[] result) { - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - throw new Error("Error in recompute.", t); - } - - }); - - } - - @Override - boolean isImmutable(ReadGraphImpl graph) { - return graph.processor.isImmutable(id); - } } diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java index f30e6e156..df3b6904f 100644 --- a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java +++ b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java @@ -826,7 +826,7 @@ public final class ClusterTable implements IClusterTable { System.err.println("value " + cc.getValueIndex()[i] + " changed."); } } - final void refreshImportance(ClusterImpl c) { + final synchronized void refreshImportance(ClusterImpl c) { if (c.isWriteOnly()) return;