From 67b615f53cb96c89900131f642648b5219ae8efc Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Wed, 21 Mar 2018 09:39:58 +0200 Subject: [PATCH] First changes refs #6861 Change-Id: Ia28f96305e977927b14098f79dc3975a67bfd676 --- .../ForEachAssertedObject.java | 38 +- .../request/ResourceAsyncMultiRead.java | 5 - .../db/common/request/ResourceAsyncRead2.java | 3 +- .../db/impl/query/AssertedPredicates.java | 12 +- .../db/impl/query/AssertedStatements.java | 18 +- .../db/impl/query/AsyncMultiReadEntry.java | 6 +- .../db/impl/query/AsyncReadEntry.java | 6 +- .../simantics/db/impl/query/BinaryQuery.java | 6 +- .../db/impl/query/BinaryQueryHash.java | 15 +- .../simantics/db/impl/query/CacheEntry.java | 2 +- .../db/impl/query/CacheEntryBase.java | 6 + .../db/impl/query/DirectObjects.java | 15 +- .../db/impl/query/DirectPredicates.java | 12 +- .../db/impl/query/ExternalReadEntry.java | 12 +- .../db/impl/query/MultiReadEntry.java | 6 +- .../db/impl/query/NamespaceIndex.java | 12 +- .../org/simantics/db/impl/query/Objects.java | 20 +- .../simantics/db/impl/query/OrderedSet.java | 14 +- .../simantics/db/impl/query/Predicates.java | 18 +- .../db/impl/query/PrincipalTypes.java | 18 +- .../simantics/db/impl/query/QueryCache.java | 746 +++++++ .../db/impl/query/QueryIdentityHash.java | 5 +- .../db/impl/query/QueryProcessor.java | 1743 ++++++++--------- .../simantics/db/impl/query/ReadEntry.java | 38 +- .../db/impl/query/RelationInfoQuery.java | 15 +- .../simantics/db/impl/query/Statements.java | 23 +- .../simantics/db/impl/query/StringQuery.java | 2 +- .../db/impl/query/SuperRelations.java | 18 +- .../simantics/db/impl/query/SuperTypes.java | 12 +- .../db/impl/query/TypeHierarchy.java | 10 +- .../org/simantics/db/impl/query/Types.java | 16 +- .../db/impl/query/URIToResource.java | 10 +- .../simantics/db/impl/query/UnaryQuery.java | 4 +- .../simantics/db/impl/query/ValueQuery.java | 10 +- .../procore/internal/QueryDebugImpl.java | 8 +- .../simantics/db/request/AsyncMultiRead.java | 2 +- .../org/simantics/db/request/AsyncRead.java | 3 +- .../src/org/simantics/db/request/Request.java | 14 - .../layer0/utils/queries/QueryExecutor2.java | 5 - .../api/request/misc/RequestParentTest3.java | 5 - 40 files changed, 1747 insertions(+), 1186 deletions(-) create mode 100644 bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java delete mode 100644 bundles/org.simantics.db/src/org/simantics/db/request/Request.java diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/primitiverequest/ForEachAssertedObject.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/primitiverequest/ForEachAssertedObject.java index a71fd1ba6..541aee94e 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/primitiverequest/ForEachAssertedObject.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/primitiverequest/ForEachAssertedObject.java @@ -11,20 +11,50 @@ *******************************************************************************/ package org.simantics.db.common.primitiverequest; +import java.util.Collection; + import org.simantics.db.AsyncReadGraph; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.common.request.ResourceAsyncMultiRead2; +import org.simantics.db.common.request.ResourceRead2; +import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.AsyncMultiProcedure; +import org.simantics.db.service.CollectionSupport; +import org.simantics.utils.DataContainer; -final public class ForEachAssertedObject extends ResourceAsyncMultiRead2 { +final public class ForEachAssertedObject extends ResourceRead2> { public ForEachAssertedObject(Resource subject, Resource relation) { super(subject, relation); } @Override - public void perform(AsyncReadGraph graph, AsyncMultiProcedure procedure) { - graph.forEachAssertedObject(resource, resource2, procedure); + public Collection perform(ReadGraph graph) throws DatabaseException { + CollectionSupport cs = graph.getService(CollectionSupport.class); + Collection result = cs.createSet(); + DataContainer throwable = new DataContainer(null); + graph.forEachAssertedObject(resource, resource2, new AsyncMultiProcedure() { + + @Override + public void finished(AsyncReadGraph graph) { + } + + @Override + public void execute(AsyncReadGraph graph, Resource r) { + result.add(r); + } + + @Override + public void exception(AsyncReadGraph graph, Throwable t) { + throwable.set(t); + } + }); + Throwable t = throwable.get(); + if(t != null) + if(t instanceof DatabaseException) + throw (DatabaseException)t; + else throw new DatabaseException(t); + return result; } } diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/request/ResourceAsyncMultiRead.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/request/ResourceAsyncMultiRead.java index 4e51a0997..4d15bd5f3 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/request/ResourceAsyncMultiRead.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/request/ResourceAsyncMultiRead.java @@ -22,11 +22,6 @@ public abstract class ResourceAsyncMultiRead implements AsyncMultiRead { public int hashCode() { return resource.hashCode(); } - - @Override - final public int threadHash() { - return resource.getThreadHash(); - } @Override public boolean equals(Object object) { diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/request/ResourceAsyncRead2.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/request/ResourceAsyncRead2.java index ae40ad4ac..1b13285d8 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/request/ResourceAsyncRead2.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/request/ResourceAsyncRead2.java @@ -23,9 +23,8 @@ import org.simantics.db.procedure.SyncListener; import org.simantics.db.procedure.SyncProcedure; import org.simantics.db.request.AsyncRead; import org.simantics.db.request.ReadInterface; -import org.simantics.db.request.Request; -public abstract class ResourceAsyncRead2 implements AsyncRead, Request, ReadInterface { +public abstract class ResourceAsyncRead2 implements AsyncRead, ReadInterface { final protected Resource resource; final protected Resource resource2; 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 31b0f3e90..5ac4886c8 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 @@ -22,8 +22,6 @@ import org.simantics.db.procedure.ListenerBase; final public class AssertedPredicates extends CollectionUnaryQuery { -// public ArrayList procs = null; - public AssertedPredicates(final int r) { super(r); } @@ -34,7 +32,7 @@ final public class AssertedPredicates extends CollectionUnaryQuery final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) { - AssertedPredicates entry = (AssertedPredicates)provider.assertedPredicatesMap.get(r); + AssertedPredicates entry = (AssertedPredicates)provider.cache.assertedPredicatesMap.get(r); if(entry == null) { entry = new AssertedPredicates(r); @@ -64,7 +62,7 @@ final public class AssertedPredicates extends CollectionUnaryQuery final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) { if(parent == null && listener == null) { - AssertedPredicates entry = (AssertedPredicates)provider.assertedPredicatesMap.get(r); + AssertedPredicates entry = (AssertedPredicates)provider.cache.assertedPredicatesMap.get(r); if(entry != null && entry.isReady()) { entry.performFromCache(graph, provider, procedure); return; @@ -77,17 +75,17 @@ final public class AssertedPredicates extends CollectionUnaryQuery @Override public UnaryQuery getEntry(QueryProcessor provider) { - return provider.assertedPredicatesMap.get(id); + return provider.cache.assertedPredicatesMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.assertedPredicatesMap.put(id, this); + provider.cache.assertedPredicatesMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.assertedPredicatesMap.remove(id); + provider.cache.assertedPredicatesMap.remove(id); } void computeInheritedAssertions(ReadGraphImpl graph, int type, final QueryProcessor queryProvider, final IntProcedure proc, final boolean store) { diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedStatements.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedStatements.java index 6e7ca62e5..24634292c 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedStatements.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedStatements.java @@ -26,8 +26,6 @@ import org.simantics.db.request.RequestFlags; final public class AssertedStatements extends CollectionBinaryQuery { -// public ArrayList procs; - public AssertedStatements(final int r1, final int r2) { super(r1, r2); } @@ -38,7 +36,7 @@ final public class AssertedStatements extends CollectionBinaryQuery getEntry(QueryProcessor provider) { - return provider.assertedStatementsMap.get(id); + return provider.cache.assertedStatementsMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.assertedStatementsMap.put(id, this); + provider.cache.assertedStatementsMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.assertedStatementsMap.remove(id); + provider.cache.assertedStatementsMap.remove(id); } void computeInheritedAssertions(ReadGraphImpl graph, int type, final int predicate, final RelationInfo ri, final QueryProcessor queryProvider, final TripleIntProcedure proc) { @@ -478,11 +476,11 @@ final public class AssertedStatements extends CollectionBinaryQuery extends CacheEntryBase { @Override public void removeEntry(QueryProcessor processor) { - processor.asyncMultiReadMap.remove(request); + processor.cache.asyncMultiReadMap.remove(request); } @Override @@ -174,7 +174,7 @@ final public class AsyncMultiReadEntry extends CacheEntryBase { } @Override - public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { + public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { final AsyncMultiProcedure proc = (AsyncMultiProcedure)procedure; @@ -206,7 +206,7 @@ final public class AsyncMultiReadEntry extends CacheEntryBase { } - + return getResult(); } 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 010f55446..c6091eb26 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 @@ -128,7 +128,7 @@ final public class AsyncReadEntry extends CacheEntryBase { @Override public void removeEntry(QueryProcessor qp) { - qp.asyncReadMap.remove(request); + qp.cache.asyncReadMap.remove(request); } @Override @@ -147,7 +147,7 @@ final public class AsyncReadEntry extends CacheEntryBase { } - public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { + public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { AsyncProcedure proc = (AsyncProcedure)procedure; @@ -169,6 +169,8 @@ final public class AsyncReadEntry extends CacheEntryBase { } + return getResult(); + } @Override diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQuery.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQuery.java index e7af8d04d..357d3d1d4 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQuery.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQuery.java @@ -72,13 +72,13 @@ abstract public class BinaryQuery extends CacheEntryBase implements Q } @Override - public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { - performFromCache(graph, (QueryProcessor)provider, (Procedure)procedure); + public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { + return performFromCache(graph, (QueryProcessor)provider, (Procedure)procedure); } abstract public void recompute(ReadGraphImpl graph, QueryProcessor provider); abstract public void computeForEach(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure, boolean store); - abstract public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure); + abstract public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure); abstract public void putEntry(QueryProcessor provider); abstract public void removeEntry(QueryProcessor provider); abstract public BinaryQuery getEntry(QueryProcessor provider); diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQueryHash.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQueryHash.java index f09ccc082..3ac46276f 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQueryHash.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQueryHash.java @@ -74,25 +74,14 @@ abstract public class BinaryQueryHash extends THash { throw new Error("Not possible!"); } -// @Override -// public void reset() { -// throw new Error("Not possible!"); -// } - @Override - public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, + public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure) { throw new Error("Not possible!"); } -// @Override -// public void performFromCache(int callerThread, Object provider, -// Object procedure) { -// throw new Error("Not possible!"); -// } - @Override - public void performFromCache(ReadGraphImpl graph, Object provider, + public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { throw new Error("Not possible!"); } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntry.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntry.java index d65622dad..34eba4eb1 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntry.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntry.java @@ -50,7 +50,7 @@ public abstract class CacheEntry { abstract boolean moreThanOneParent(QueryProcessor processor); abstract int parentCount(QueryProcessor processor); - abstract void performFromCache(ReadGraphImpl graph, Object provider, Object procedure); + abstract Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure); abstract boolean isImmutable(ReadGraphImpl graph) throws DatabaseException; 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 5776857d3..800d19bdd 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 @@ -14,6 +14,7 @@ package org.simantics.db.impl.query; import java.util.ArrayList; import java.util.Iterator; +import org.simantics.db.AsyncReadGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.DebugPolicy; import org.simantics.db.impl.graph.ReadGraphImpl; @@ -454,4 +455,9 @@ abstract public class CacheEntryBase extends CacheEntry { return getQuery(); } +// abstract public void addOrSet(AsyncReadGraph graph, Object item); +// abstract public void except(AsyncReadGraph graph, Throwable t); + + //abstract public void perform(AsyncReadGraph graph, Object query, Object procedure); + } 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 2c106476e..360e067fa 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 @@ -37,12 +37,12 @@ final public class DirectObjects extends CollectionBinaryQuery { final static DirectObjects entry(final QueryProcessor provider, final int r1, final int r2) { - return (DirectObjects)provider.directObjectsMap.get(id(r1,r2)); + return (DirectObjects)provider.cache.directObjectsMap.get(id(r1,r2)); } final static Collection entries(final QueryProcessor processor, final int r1) { - DoubleKeyQueryHashMap hash = processor.directObjectsMap; + DoubleKeyQueryHashMap hash = processor.cache.directObjectsMap; return hash.values(r1); } @@ -50,7 +50,7 @@ final public class DirectObjects extends CollectionBinaryQuery { QueryProcessor processor = graph.processor; - DirectObjects entry = (DirectObjects)processor.directObjectsMap.get(id(r1,r2)); + DirectObjects entry = (DirectObjects)processor.cache.directObjectsMap.get(id(r1,r2)); if(entry == null) { entry = new DirectObjects(r1, r2); @@ -93,17 +93,17 @@ final public class DirectObjects extends CollectionBinaryQuery { @Override public BinaryQuery getEntry(QueryProcessor provider) { - return provider.directObjectsMap.get(id); + return provider.cache.directObjectsMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.directObjectsMap.put(id, this); + provider.cache.directObjectsMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.directObjectsMap.remove(id); + provider.cache.directObjectsMap.remove(id); } @Override @@ -155,10 +155,11 @@ final public class DirectObjects extends CollectionBinaryQuery { } @Override - public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) { + public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) { assert(isReady()); computeForEach(graph, provider, procedure, false); + return null; } 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 606afa430..117448b5b 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 @@ -26,13 +26,13 @@ final public class DirectPredicates extends CollectionUnaryQuery { final static DirectPredicates entry(final QueryProcessor provider, final int r) { - return (DirectPredicates)provider.directPredicatesMap.get(r); + return (DirectPredicates)provider.cache.directPredicatesMap.get(r); } final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) { - DirectPredicates entry = (DirectPredicates)provider.directPredicatesMap.get(r); + DirectPredicates entry = (DirectPredicates)provider.cache.directPredicatesMap.get(r); if(entry == null) { entry = new DirectPredicates(r); @@ -63,7 +63,7 @@ final public class DirectPredicates extends CollectionUnaryQuery { final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) { if(parent == null && listener == null) { - DirectPredicates entry = (DirectPredicates)provider.directPredicatesMap.get(r); + DirectPredicates entry = (DirectPredicates)provider.cache.directPredicatesMap.get(r); if(entry != null && entry.isReady()) { entry.performFromCache(graph, provider, procedure); return; @@ -86,17 +86,17 @@ final public class DirectPredicates extends CollectionUnaryQuery { @Override public UnaryQuery getEntry(QueryProcessor provider) { - return provider.directPredicatesMap.get(id); + return provider.cache.directPredicatesMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.directPredicatesMap.put(id, this); + provider.cache.directPredicatesMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.directPredicatesMap.remove(id); + provider.cache.directPredicatesMap.remove(id); } @Override diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java index 18d91dc06..31fe0103b 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java @@ -22,8 +22,6 @@ import org.simantics.db.request.RequestFlags; final public class ExternalReadEntry extends CacheEntryBase { final LinkedList items = new LinkedList(); - -// public ArrayList> procs; protected ExternalRead request; @@ -125,7 +123,7 @@ final public class ExternalReadEntry extends CacheEntryBase { @Override public void removeEntry(QueryProcessor processor) { - processor.externalReadMap.remove(request); + processor.cache.externalReadMap.remove(request); } @Override @@ -143,7 +141,7 @@ final public class ExternalReadEntry extends CacheEntryBase { } - public void performFromCache(Object procedure) { + public Object performFromCache(Object procedure) { Procedure proc = (Procedure)procedure; @@ -156,6 +154,8 @@ final public class ExternalReadEntry extends CacheEntryBase { proc.execute((T)getResult()); } + + return getResult(); } @@ -166,8 +166,8 @@ final public class ExternalReadEntry extends CacheEntryBase { } @Override - public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { - performFromCache(procedure); + public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { + return performFromCache(procedure); } @Override diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/MultiReadEntry.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/MultiReadEntry.java index d0bb436d7..1a1f88de6 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/MultiReadEntry.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/MultiReadEntry.java @@ -141,7 +141,7 @@ final public class MultiReadEntry extends CacheEntryBase { @Override public void removeEntry(QueryProcessor processor) { - processor.multiReadMap.remove(request); + processor.cache.multiReadMap.remove(request); } @Override @@ -195,7 +195,7 @@ final public class MultiReadEntry extends CacheEntryBase { } @Override - public void performFromCache(ReadGraphImpl graph, Object provider, + public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { final AsyncMultiProcedure proc = (AsyncMultiProcedure)procedure; @@ -227,7 +227,7 @@ final public class MultiReadEntry extends CacheEntryBase { } - + return null; } diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java index 526728003..a0a8c19cb 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java @@ -25,8 +25,6 @@ import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.procedure.ListenerBase; final public class NamespaceIndex extends StringQuery>> { - -// public ArrayList>> procs = null; private NamespaceIndex(final String id) { super(id); @@ -34,7 +32,7 @@ final public class NamespaceIndex extends StringQuery> procedure) { - NamespaceIndex entry = cached != null ? cached : (NamespaceIndex)provider.namespaceIndexMap22.get(id); + NamespaceIndex entry = cached != null ? cached : (NamespaceIndex)provider.cache.namespaceIndexMap22.get(id); if(entry == null) { entry = new NamespaceIndex(id); @@ -64,7 +62,7 @@ final public class NamespaceIndex extends StringQuery> procedure) { - final NamespaceIndex entry = (NamespaceIndex)provider.namespaceIndexMap22.get(id); + final NamespaceIndex entry = (NamespaceIndex)provider.cache.namespaceIndexMap22.get(id); if(parent == null && listener == null && entry != null && entry.isReady()) { entry.performFromCache(graph, provider, procedure); @@ -77,17 +75,17 @@ final public class NamespaceIndex extends StringQuery> procedure) { diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java index 0b3e1be34..a35826341 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 @@ -49,11 +49,11 @@ final public class Objects extends CollectionBinaryQuery { } final static Objects entry(final QueryProcessor provider, final int r1, final int r2) { - return (Objects)provider.objectsMap.get(r1,r2); + return (Objects)provider.cache.objectsMap.get(r1,r2); } final static Collection entries(final QueryProcessor processor, final int r1) { - return processor.objectsMap.values(r1); + return processor.cache.objectsMap.values(r1); } public final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) { @@ -65,7 +65,7 @@ final public class Objects extends CollectionBinaryQuery { QueryProcessor processor = graph.processor; - Objects entry = (Objects)processor.objectsMap.get(r1,r2); + Objects entry = (Objects)processor.cache.objectsMap.get(r1,r2); if(entry == null) { entry = new Objects(r1, r2); @@ -143,7 +143,7 @@ final public class Objects extends CollectionBinaryQuery { QueryProcessor processor = graph.processor; - Objects entry = (Objects)processor.objectsMap.get(r1,r2); + Objects entry = (Objects)processor.cache.objectsMap.get(r1,r2); if(entry == null) { entry = new Objects(r1, r2); @@ -167,7 +167,7 @@ final public class Objects extends CollectionBinaryQuery { @Override public BinaryQuery getEntry(QueryProcessor provider) { - return provider.objectsMap.get(id); + return provider.cache.objectsMap.get(id); } @Override @@ -177,12 +177,12 @@ final public class Objects extends CollectionBinaryQuery { System.err.println("put " + this); } } - provider.objectsMap.put(id, this); + provider.cache.objectsMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.objectsMap.remove(id); + provider.cache.objectsMap.remove(id); } final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Objects entry) { @@ -853,11 +853,11 @@ final public class Objects extends CollectionBinaryQuery { } @Override - public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) { + public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) { assert(isReady()); - if(handleException(graph, procedure)) return; + if(handleException(graph, procedure)) return getResult(); final IntArray value = (IntArray)getResult(); if(value.data == null) { @@ -867,6 +867,8 @@ final public class Objects extends CollectionBinaryQuery { } procedure.finished(graph); + + return value; } 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 65cfb007c..c693d2a91 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 @@ -21,21 +21,19 @@ import org.simantics.db.procedure.ListenerBase; final public class OrderedSet extends CollectionUnaryQuery { -// public ArrayList procs = null; - public OrderedSet(final int r) { super(r); } final static OrderedSet entry(final QueryProcessor provider, final int r) { - return (OrderedSet)provider.orderedSetMap.get(r); + return (OrderedSet)provider.cache.orderedSetMap.get(r); } final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, OrderedSet cached, final CacheEntry parent, ListenerBase listener, final IntProcedure procedure) { - OrderedSet entry = cached != null ? cached : (OrderedSet)provider.orderedSetMap.get(r); + OrderedSet entry = cached != null ? cached : (OrderedSet)provider.cache.orderedSetMap.get(r); if(entry == null) { entry = new OrderedSet(r); @@ -67,7 +65,7 @@ final public class OrderedSet extends CollectionUnaryQuery { assert(r != 0); - final OrderedSet entry = (OrderedSet)provider.orderedSetMap.get(r); + final OrderedSet entry = (OrderedSet)provider.cache.orderedSetMap.get(r); if(parent == null && !(listener != null)) { if(entry != null && entry.isReady()) { @@ -82,17 +80,17 @@ final public class OrderedSet extends CollectionUnaryQuery { @Override public UnaryQuery getEntry(QueryProcessor provider) { - return provider.orderedSetMap.get(id); + return provider.cache.orderedSetMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.orderedSetMap.put(id, this); + provider.cache.orderedSetMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.orderedSetMap.remove(id); + provider.cache.orderedSetMap.remove(id); } private int current = 0; 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 960cdb6c0..312522007 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 @@ -21,8 +21,6 @@ import org.simantics.db.procedure.ListenerBase; import org.simantics.db.request.RequestFlags; final public class Predicates extends UnaryQuery { - -// public ArrayList procs; public Predicates(final int r) { super(r); @@ -34,13 +32,13 @@ final public class Predicates extends UnaryQuery { final static Predicates entry(final QueryProcessor provider, final int r) { - return (Predicates)provider.predicatesMap.get(r); + return (Predicates)provider.cache.predicatesMap.get(r); } final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, Predicates cached, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) { - Predicates entry = cached != null ? cached : (Predicates)provider.predicatesMap.get(r); + Predicates entry = cached != null ? cached : (Predicates)provider.cache.predicatesMap.get(r); if(entry == null) { entry = new Predicates(r); @@ -66,7 +64,7 @@ final public class Predicates extends UnaryQuery { final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable { - Predicates entry = (Predicates)provider.predicatesMap.get(r); + Predicates entry = (Predicates)provider.cache.predicatesMap.get(r); if(entry == null) { entry = new Predicates(r); @@ -95,7 +93,7 @@ final public class Predicates extends UnaryQuery { assert(r != 0); - final Predicates entry = (Predicates)provider.predicatesMap.get(r); + final Predicates entry = (Predicates)provider.cache.predicatesMap.get(r); if(parent == null && listener == null) { if(entry != null && entry.isReady()) { @@ -111,7 +109,7 @@ final public class Predicates extends UnaryQuery { final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable { if(parent == null) { - final Predicates entry = (Predicates)provider.predicatesMap.get(r); + final Predicates entry = (Predicates)provider.cache.predicatesMap.get(r); if(entry != null && entry.isReady()) { return (IntSet)entry.get(graph, provider, null); } @@ -123,17 +121,17 @@ final public class Predicates extends UnaryQuery { @Override public UnaryQuery getEntry(QueryProcessor provider) { - return provider.predicatesMap.get(id); + return provider.cache.predicatesMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.predicatesMap.put(id, this); + provider.cache.predicatesMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.predicatesMap.remove(id); + provider.cache.predicatesMap.remove(id); } final private void forAssertions(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure procedure, final boolean store) { 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 25e2b6fe3..60fe07411 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 @@ -11,9 +11,6 @@ *******************************************************************************/ package org.simantics.db.impl.query; -import gnu.trove.procedure.TIntProcedure; -import gnu.trove.set.hash.TIntHashSet; - import java.util.ArrayList; import java.util.Arrays; import java.util.concurrent.Semaphore; @@ -23,23 +20,24 @@ import org.simantics.db.impl.procedure.IntProcedureAdapter; import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.procedure.ListenerBase; -final public class PrincipalTypes extends CollectionUnaryQuery { +import gnu.trove.procedure.TIntProcedure; +import gnu.trove.set.hash.TIntHashSet; -// public ArrayList procs = null; +final public class PrincipalTypes extends CollectionUnaryQuery { private PrincipalTypes(final int resource) { super(resource); } final static PrincipalTypes entry(final QueryProcessor provider, final int r) { - return (PrincipalTypes)provider.principalTypesMap.get(r); + return (PrincipalTypes)provider.cache.principalTypesMap.get(r); } final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) { QueryProcessor processor = graph.processor; - PrincipalTypes entry = (PrincipalTypes)processor.principalTypesMap.get(r); + PrincipalTypes entry = (PrincipalTypes)processor.cache.principalTypesMap.get(r); if(entry == null) { entry = new PrincipalTypes(r); @@ -81,17 +79,17 @@ final public class PrincipalTypes extends CollectionUnaryQuery { @Override public UnaryQuery getEntry(QueryProcessor provider) { - return provider.principalTypesMap.get(id); + return provider.cache.principalTypesMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.principalTypesMap.put(id, this); + provider.cache.principalTypesMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.principalTypesMap.remove(id); + provider.cache.principalTypesMap.remove(id); } static class Koss { 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 new file mode 100644 index 000000000..2adc7c78b --- /dev/null +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java @@ -0,0 +1,746 @@ +package org.simantics.db.impl.query; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.RelationInfo; +import org.simantics.db.common.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.impl.DebugPolicy; +import org.simantics.db.impl.graph.ReadGraphImpl; +import org.simantics.db.impl.procedure.InternalProcedure; +import org.simantics.db.procedure.AsyncMultiProcedure; +import org.simantics.db.procedure.AsyncProcedure; +import org.simantics.db.procedure.Listener; +import org.simantics.db.procedure.ListenerBase; +import org.simantics.db.procedure.Procedure; +import org.simantics.db.request.AsyncMultiRead; +import org.simantics.db.request.AsyncRead; +import org.simantics.db.request.ExternalRead; +import org.simantics.db.request.MultiRead; +import org.simantics.db.request.Read; + +import gnu.trove.map.hash.THashMap; + +public class QueryCache { + + final public UnaryQueryHashMap directPredicatesMap; + final public UnaryQueryHashMap principalTypesMap; + final public THashMap uriToResourceMap; + final public THashMap namespaceIndexMap22; + final public UnaryQueryHashMap projectsMap; + final public UnaryQueryHashMap> relationInfoMap; + final public UnaryQueryHashMap> superTypesMap; + final public UnaryQueryHashMap> typeHierarchyMap; + final public UnaryQueryHashMap> superRelationsMap; + final public UnaryQueryHashMap> typesMap; + final public UnaryQueryHashMap> valueMap; + final public DoubleKeyQueryHashMap directObjectsMap; + final public DoubleKeyQueryHashMap objectsMap; + final public UnaryQueryHashMap orderedSetMap; + final public UnaryQueryHashMap predicatesMap; + final public DoubleKeyQueryHashMap statementsMap; + final public UnaryQueryHashMap assertedPredicatesMap; + final public BinaryQueryHashMap assertedStatementsMap; + final public StableHashMap externalReadMap; + final public StableHashMap asyncReadMap; + final public StableHashMap readMap; + final public StableHashMap asyncMultiReadMap; + final public StableHashMap multiReadMap; + + final THashMap> listeners; + + public QueryCache() { + directPredicatesMap = new UnaryQueryHashMap(); + valueMap = new UnaryQueryHashMap(); + principalTypesMap = new UnaryQueryHashMap(); + uriToResourceMap = new THashMap(); + namespaceIndexMap22 = new THashMap(); + projectsMap = new UnaryQueryHashMap(); + relationInfoMap = new UnaryQueryHashMap(); + typeHierarchyMap = new UnaryQueryHashMap(); + superTypesMap = new UnaryQueryHashMap(); + superRelationsMap = new UnaryQueryHashMap(); + typesMap = new UnaryQueryHashMap(); + objectsMap = new DoubleKeyQueryHashMap(); + orderedSetMap = new UnaryQueryHashMap(); + predicatesMap = new UnaryQueryHashMap(); + statementsMap = new DoubleKeyQueryHashMap(); + directObjectsMap = new DoubleKeyQueryHashMap(); + assertedPredicatesMap = new UnaryQueryHashMap(); + assertedStatementsMap = new BinaryQueryHashMap(); + asyncReadMap = new StableHashMap(); + readMap = new StableHashMap(); + asyncMultiReadMap = new StableHashMap(); + multiReadMap = new StableHashMap(); + externalReadMap = new StableHashMap(); + listeners = new THashMap>(10, 0.75f); + } + + public int requestHash(Object object) { + try { + return object.hashCode(); + } catch (Throwable t) { + Logger.defaultLogError(t); + return 0; + } + } + + private CacheEntryBase getCached(Object query, int hash) { + if (query instanceof AsyncRead) + return asyncReadMap.get(query, hash); + else if (query instanceof Read) + return readMap.get(query, hash); + else if (query instanceof ExternalRead) + return externalReadMap.get(query, hash); + else if (query instanceof AsyncMultiRead) + return asyncMultiReadMap.get(query, hash); + throw new IllegalStateException(); + } + + private CacheEntryBase createEntry(QuerySupport support, Object query, int hash) { + + CacheEntryBase result; + if (query instanceof AsyncRead) { + AsyncReadEntry entry = new AsyncReadEntry((AsyncRead)query); + asyncReadMap.put((AsyncRead)query, entry, hash); + result = entry; + } else if (query instanceof Read) { + ReadEntry entry = new ReadEntry((Read)query); + readMap.put((Read)query, entry, hash); + result = entry; + } else if (query instanceof ExternalRead) { + ExternalReadEntry entry = new ExternalReadEntry((ExternalRead)query); + externalReadMap.put((ExternalRead)query, entry, hash); + result = entry; + } else if (query instanceof AsyncMultiRead) { + AsyncMultiReadEntry entry = new AsyncMultiReadEntry((AsyncMultiRead)query); + asyncMultiReadMap.put((AsyncMultiRead)query, entry, hash); + result = entry; + } else { + throw new IllegalStateException(); + } + + result.setPending(); + result.clearResult(support); + + return result; + + } + + public final Object runQuery(final ReadGraphImpl graph, final Object query, final CacheEntry parent, final ListenerBase listener, final Object procedure) throws DatabaseException { + + int hash = requestHash(query); + + CacheEntryBase entry = getCached(query, hash); + + if(parent == null && listener == null) { + if(entry != null && (entry.isReady() || entry.isExcepted())) { + return entry.performFromCache(graph, this, procedure); + } else { + return performQuery(graph, query, entry, null, procedure); + } + } + + if(entry == null) { + entry = createEntry(graph.processor.querySupport, query, hash); + return performForEach(graph, query, entry, parent, listener, procedure, false); + + } else { + + if(entry.isPending()) { + synchronized(entry) { + if(entry.isPending()) { + throw new IllegalStateException(); + // final AsyncBarrierImpl parentBarrier = graph.state.barrier; + // if(entry.procs == null) entry.procs = new ArrayList>(); + // entry.procs.add(new AsyncProcedure() { + // + // @Override + // public void execute(AsyncReadGraph graph, T result) { + // procedure.execute(graph, result); + // parentBarrier.dec(query); + // } + // + // @Override + // public void exception(AsyncReadGraph graph, Throwable throwable) { + // procedure.exception(graph, throwable); + // parentBarrier.dec(query); + // } + // + // }); +// if(graph.parent != null || listener != null) { +// registerDependencies(graph, entry, parent, listener, procedure, false); +// } +// +// query.perform(graph, procedure); +// +// return; + + } + } + } + + if(entry.isReady()) { + Object result = entry.performFromCache(graph, this, procedure); + registerDependencies(graph, entry, parent, listener, procedure, false); + return result; + } else { + return performForEach(graph, query, entry, parent, listener, procedure, false); + } + + } + + } + + public Object performQuery(ReadGraphImpl parentGraph, final Object query_, final CacheEntryBase entry_, ListenerEntry listenerEntry, Object procedure_) throws DatabaseException { + + ReadGraphImpl queryGraph = parentGraph.withParent(entry_); + + if(query_ instanceof AsyncRead) { + + AsyncRead query = (AsyncRead)query_; + AsyncReadEntry entry = (AsyncReadEntry)entry_; + AsyncProcedure procedure = (AsyncProcedure)procedure_; + + try { + + query.perform(queryGraph, new AsyncProcedure() { + + @Override + public void execute(AsyncReadGraph returnGraph, T result) { + ReadGraphImpl impl = (ReadGraphImpl)returnGraph; + entry.addOrSet(parentGraph, result); + if(listenerEntry != null) { + primeListenerEntry(listenerEntry, result); + } + try { + procedure.execute(parentGraph, 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(); + entry.except(parentGraph, t); + try { + procedure.exception(parentGraph, t); + } catch (Throwable t2) { + t2.printStackTrace(); + } + // parentBarrier.dec(query); + } + + @Override + public String toString() { + return procedure.toString(); + } + + }); + + } catch (Throwable t) { + + entry.except(t); + try { + procedure.exception(parentGraph, t); + } catch (Throwable t2) { + t2.printStackTrace(); + } + // parentBarrier.dec(query); + + } + + return null; + + } else if (query_ instanceof Read) { + + Read query = (Read)query_; + ReadEntry entry = (ReadEntry)entry_; + + entry.setPending(); + + try { + + T result = (T)query.perform(queryGraph); + entry.addOrSet(queryGraph, result); + + if(listenerEntry != null) primeListenerEntry(listenerEntry, result); + + return (T)entry.get(parentGraph, parentGraph.processor, procedure_); + + } catch (Throwable t) { + + entry.except(t); + return (T)entry.get(parentGraph, parentGraph.processor, procedure_); + + } + + } else if (query_ instanceof ExternalRead) { + + ExternalRead query = (ExternalRead)query_; + ExternalReadEntry entry = (ExternalReadEntry)entry_; + Procedure procedure = (Procedure)procedure_; + + try { + + query.register(parentGraph, new Listener() { + + AtomicBoolean used = new AtomicBoolean(false); + + @Override + public void execute(T result) { + + // Just for safety + if(entry.isDiscarded()) return; + if(entry.isExcepted()) entry.setPending(); + + if(used.compareAndSet(false, true)) { + entry.addOrSet(parentGraph.processor, result); + procedure.execute(result); + } else { + entry.queue(result); + parentGraph.processor.updatePrimitive(query); + } + + } + + @Override + public void exception(Throwable t) { + + entry.except(t); + + if(used.compareAndSet(false, true)) { + procedure.exception(t); + } else { +// entry.queue(result); + parentGraph.processor.updatePrimitive(query); + } + + } + + @Override + public String toString() { + return procedure.toString(); + } + + @Override + public boolean isDisposed() { + return entry.isDiscarded() || !parentGraph.processor.isBound(entry); + } + + }); + + return entry.getResult(); + + } catch (Throwable t) { + + entry.except(t); + procedure.exception(t); + return entry.getResult(); + + } + + } else if (query_ instanceof AsyncMultiRead) { + + AsyncMultiRead query = (AsyncMultiRead)query_; + AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_; + AsyncMultiProcedure procedure = (AsyncMultiProcedure)procedure_; + + try { + + query.perform(queryGraph, new AsyncMultiProcedure() { + + @Override + public void execute(AsyncReadGraph graph, T result) { + ReadGraphImpl impl = (ReadGraphImpl)graph; + entry.addOrSet(result); + try { + procedure.execute(parentGraph, result); + } catch (Throwable t) { + t.printStackTrace(); + } + } + + @Override + public void finished(AsyncReadGraph graph) { + ReadGraphImpl impl = (ReadGraphImpl)graph; + entry.finish(parentGraph); + try { + procedure.finished(parentGraph); + } catch (Throwable t) { + t.printStackTrace(); + } + } + + @Override + public void exception(AsyncReadGraph graph, Throwable t) { + ReadGraphImpl impl = (ReadGraphImpl)graph; + entry.except(parentGraph, t); + try { + procedure.exception(parentGraph, t); + } catch (Throwable t2) { + t2.printStackTrace(); + } + } + + }); + + return entry.getResult(); + + } catch (Throwable t) { + + entry.except(t); + try { + procedure.exception(parentGraph, t); + } catch (Throwable t2) { + t2.printStackTrace(); + } + + return entry.getResult(); + + } + + } else { + + throw new IllegalStateException(); + + } + + } + + public Object performFromCache(ReadGraphImpl parentGraph, Object query, CacheEntryBase entry_, ListenerEntry listenerEntry, Object procedure_) throws DatabaseException { + + Object result = entry_.performFromCache(parentGraph, this, procedure_); + if(listenerEntry != null) { + primeListenerEntry(listenerEntry, result); + } + return result; + +// if(query instanceof AsyncRead) { +// +// AsyncProcedure procedure = (AsyncProcedure)procedure_; +// +// +// return null; +// +// } else if(query instanceof Read) { +// +// ReadEntry entry = (ReadEntry)entry_; +// +// T result = (T)entry.get(parentGraph, parentGraph.processor, procedure_); +// +// if(listenerEntry != null) primeListenerEntry(listenerEntry, result); +// +// return result; +// +// } else if(query instanceof ExternalRead) { +// +// ExternalReadEntry entry = (ExternalReadEntry)entry_; +// Procedure procedure = (Procedure)procedure_; +// +// return entry.performFromCache(procedure); +// +// } else if (query instanceof AsyncMultiRead) { +// +// AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_; +// AsyncMultiProcedure procedure = (AsyncMultiProcedure)procedure_; +// +// return entry.performFromCache(parentGraph, this, procedure); +// +// } else { +// +// throw new IllegalStateException(); +// +// } + + } + + public Object performForEach(ReadGraphImpl parentGraph, final Object query, final CacheEntryBase entry, final CacheEntry parent, final ListenerBase base, final Object procedure, + boolean inferredDependency) throws DatabaseException { + +// if (DebugPolicy.PERFORM) +// System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query); + +// assert (!dirty); +// assert (!collecting); + + assert(!entry.isDiscarded()); + + final ListenerEntry listenerEntry = registerDependencies(parentGraph, entry, parent, base, procedure, inferredDependency); + + // FRESH, REFUTED, EXCEPTED go here + if (!entry.isReady()) { + + entry.setPending(); + +// size++; + + return performQuery(parentGraph, query, entry, listenerEntry, procedure); + +// misses++; + + } else { + + return performFromCache(parentGraph, query, entry, listenerEntry, procedure); + +// parentBarrier.dec(query); + +// hits++; + + } + + } + + synchronized public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) { + + if (parent != null && !inferred) { + try { + if(!child.isImmutable(graph)) + child.addParent(parent); + } catch (DatabaseException e) { + Logger.defaultLogError(e); + } + if(DebugPolicy.DEPENDENCIES) System.out.println(child + " -> " + parent); + } + + if (listener != null) { + return registerListener(child, listener, procedure); + } else { + return null; + } + + } + + public ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) { + + assert (entry != null); + + if (base.isDisposed()) + return null; + + return addListener(entry, base, procedure); + + } + + private void primeListenerEntry(final ListenerEntry entry, final Object result) { + entry.setLastKnown(result); + } + + private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) { + + assert (entry != null); + assert (procedure != null); + + ArrayList list = listeners.get(entry); + if (list == null) { + list = new ArrayList(1); + listeners.put(entry, list); + } + + ListenerEntry result = new ListenerEntry(entry, base, procedure); + int currentIndex = list.indexOf(result); + // There was already a listener + if(currentIndex > -1) { + ListenerEntry current = list.get(currentIndex); + if(!current.base.isDisposed()) return null; + list.set(currentIndex, result); + } else { + list.add(result); + } + + if(DebugPolicy.LISTENER) { + new Exception().printStackTrace(); + System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure); + } + + return result; + + } + + + public Collection getRootList() { + + ArrayList result = new ArrayList(); + + for (Object e : valueMap.values()) { + result.add((CacheEntry) e); + } + for (Object e : directPredicatesMap.values()) { + result.add((CacheEntry) e); + } + for (Object e : objectsMap.values()) { + result.add((CacheEntry) e); + } + for (Object e : directObjectsMap.values()) { + result.add((CacheEntry) e); + } + for (Object e : principalTypesMap.values()) { + result.add((CacheEntry) e); + } + for (Object e : superRelationsMap.values()) { + result.add((CacheEntry) e); + } + for (Object e : superTypesMap.values()) { + result.add((CacheEntry) e); + } + for (Object e : typesMap.values()) { + result.add((CacheEntry) e); + } + for (Object e : objectsMap.values()) { + result.add((CacheEntry) e); + } + for (Object e : assertedStatementsMap.values()) { + result.add((CacheEntry) e); + } + for (Object e : readMap.values()) { + if(e instanceof CacheEntry) { + result.add((CacheEntry) e); + } else { + System.err.println("e=" + e); + } + } + for (Object e : asyncReadMap.values()) { + if(e instanceof CacheEntry) { + result.add((CacheEntry) e); + } else { + System.err.println("e=" + e); + } + } + for (Object e : externalReadMap.values()) { + result.add((CacheEntry) e); + } + for (Object e : orderedSetMap.values()) { + result.add((CacheEntry) e); + } + + return result; + + } + + public int calculateCurrentSize() { + + int realSize = 0; + + realSize += directPredicatesMap.size(); + realSize += principalTypesMap.size(); + realSize += uriToResourceMap.size(); + realSize += namespaceIndexMap22.size(); + realSize += projectsMap.size(); + + realSize += relationInfoMap.size(); + realSize += superTypesMap.size(); + realSize += typeHierarchyMap.size(); + realSize += superRelationsMap.size(); + realSize += typesMap.size(); + + realSize += valueMap.size(); + realSize += directObjectsMap.size(); + realSize += objectsMap.size(); + realSize += orderedSetMap.size(); + realSize += predicatesMap.size(); + + realSize += statementsMap.size(); + realSize += assertedPredicatesMap.size(); + realSize += assertedStatementsMap.size(); + realSize += externalReadMap.size(); + realSize += asyncReadMap.size(); + + realSize += readMap.size(); + realSize += asyncMultiReadMap.size(); + realSize += multiReadMap.size(); + + return realSize; + + } + + CacheCollectionResult allCaches(CacheCollectionResult result) { + + int level = Integer.MAX_VALUE; + directPredicatesMap.values(level, result); + principalTypesMap.values(level, result); + for(CacheEntryBase e : uriToResourceMap.values()) + if(e.getLevel() <= level) + result.add(e); + for(CacheEntryBase e : namespaceIndexMap22.values()) + if(e.getLevel() <= level) + result.add(e); + projectsMap.values(level, result); + + relationInfoMap.values(level, result); + superTypesMap.values(level, result); + typeHierarchyMap.values(level, result); + superRelationsMap.values(level, result); + typesMap.values(level, result); + + valueMap.values(level, result); + directObjectsMap.values(level, result); + objectsMap.values(level, result); + orderedSetMap.values(level, result); + predicatesMap.values(level, result); + + statementsMap.values(level, result); + assertedPredicatesMap.values(level, result); + assertedStatementsMap.values(level, result); + externalReadMap.values(level, result); + asyncReadMap.values(level, result); + + readMap.values(level, result); + asyncMultiReadMap.values(level, result); + multiReadMap.values(level, result); + + return result; + + } + + public void scanPending() { + + ArrayList entries = new ArrayList(); + + entries.addAll(directPredicatesMap.values()); + entries.addAll(principalTypesMap.values()); + entries.addAll(uriToResourceMap.values()); + entries.addAll(namespaceIndexMap22.values()); + entries.addAll(projectsMap.values()); + entries.addAll(relationInfoMap.values()); + entries.addAll(superTypesMap.values()); + entries.addAll(superRelationsMap.values()); + entries.addAll(typesMap.values()); + entries.addAll(valueMap.values()); + entries.addAll(directObjectsMap.values()); + entries.addAll(objectsMap.values()); + entries.addAll(orderedSetMap.values()); + entries.addAll(predicatesMap.values()); + entries.addAll(orderedSetMap.values()); + entries.addAll(statementsMap.values()); + // entries.addAll(assertedObjectsMap.values()); + entries.addAll(assertedPredicatesMap.values()); + entries.addAll(assertedStatementsMap.values()); + entries.addAll(externalReadMap.values()); + entries.addAll(asyncReadMap.values()); + entries.addAll(externalReadMap.values()); + entries.addAll(readMap.values()); + entries.addAll(asyncMultiReadMap.values()); + entries.addAll(multiReadMap.values()); + entries.addAll(readMap.values()); + System.out.println(entries.size() + " entries."); + for(Object e : entries) { + if(e instanceof CacheEntry) { + CacheEntry en = (CacheEntry)e; + if(en.isPending()) System.out.println("pending " + e); + if(en.isExcepted()) System.out.println("excepted " + e); + if(en.isDiscarded()) System.out.println("discarded " + e); + if(en.isRefuted()) System.out.println("refuted " + e); + if(en.isFresh()) System.out.println("fresh " + e); + } else { + //System.out.println("Unknown object " + e); + } + } + } + +} diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHash.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHash.java index 4842cf5a6..c9190193f 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHash.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHash.java @@ -118,9 +118,8 @@ abstract public class QueryIdentityHash extends THash { } @Override - public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { - // TODO Auto-generated method stub - + public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { + return null; } @Override diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryProcessor.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryProcessor.java index ff15d301b..cca9d681e 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryProcessor.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryProcessor.java @@ -94,31 +94,7 @@ import gnu.trove.set.hash.TIntHashSet; @SuppressWarnings({"rawtypes", "unchecked"}) final public class QueryProcessor extends AbstractDisposable implements ReadGraphSupport { - final public UnaryQueryHashMap directPredicatesMap; - final public UnaryQueryHashMap principalTypesMap; - final public THashMap uriToResourceMap; - final public THashMap namespaceIndexMap22; - final public UnaryQueryHashMap projectsMap; - final public UnaryQueryHashMap> relationInfoMap; - final public UnaryQueryHashMap> superTypesMap; - final public UnaryQueryHashMap> typeHierarchyMap; - final public UnaryQueryHashMap> superRelationsMap; - final public UnaryQueryHashMap> typesMap; - final public UnaryQueryHashMap> valueMap; - final public DoubleKeyQueryHashMap directObjectsMap; - final public DoubleKeyQueryHashMap objectsMap; - final public UnaryQueryHashMap orderedSetMap; - final public UnaryQueryHashMap predicatesMap; - final public DoubleKeyQueryHashMap statementsMap; - final public UnaryQueryHashMap assertedPredicatesMap; - final public BinaryQueryHashMap assertedStatementsMap; - final public StableHashMap externalReadMap; - final public StableHashMap asyncReadMap; - final public StableHashMap readMap; - final public StableHashMap asyncMultiReadMap; - final public StableHashMap multiReadMap; - - final private THashMap> listeners; + final public QueryCache cache = new QueryCache(); public static int indent = 0; @@ -365,31 +341,6 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap } - directPredicatesMap = new UnaryQueryHashMap(); - valueMap = new UnaryQueryHashMap(); - principalTypesMap = new UnaryQueryHashMap(); - uriToResourceMap = new THashMap(); - namespaceIndexMap22 = new THashMap(); - projectsMap = new UnaryQueryHashMap(); - relationInfoMap = new UnaryQueryHashMap(); - typeHierarchyMap = new UnaryQueryHashMap(); - superTypesMap = new UnaryQueryHashMap(); - superRelationsMap = new UnaryQueryHashMap(); - typesMap = new UnaryQueryHashMap(); - objectsMap = new DoubleKeyQueryHashMap(); - orderedSetMap = new UnaryQueryHashMap(); - predicatesMap = new UnaryQueryHashMap(); - statementsMap = new DoubleKeyQueryHashMap(); - directObjectsMap = new DoubleKeyQueryHashMap(); - assertedPredicatesMap = new UnaryQueryHashMap(); - assertedStatementsMap = new BinaryQueryHashMap(); - asyncReadMap = new StableHashMap(); - readMap = new StableHashMap(); - asyncMultiReadMap = new StableHashMap(); - multiReadMap = new StableHashMap(); - externalReadMap = new StableHashMap(); - listeners = new THashMap>(10, 0.75f); - // Now start threads for (int i = 0; i < THREADS; i++) { executors[i].start(); @@ -625,201 +576,223 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap public final void runAsyncRead(final ReadGraphImpl graph, final AsyncRead query, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure procedure) { - int hash = requestHash(query); - - AsyncReadEntry entry = asyncReadMap.get(query, hash); - - if(parent == null && listener == null) { - if(entry != null && (entry.isReady() || entry.isExcepted())) { - System.out.println("ready " + query); - entry.performFromCache(graph, this, procedure); -// graph.state.barrier.dec(query); - return; - } else { - query.perform(graph, procedure); -// graph.state.barrier.dec(query); - return; - } + try { + cache.runQuery(graph, query, parent, listener, procedure); + } catch (DatabaseException e) { + throw new IllegalStateException(e); } - - if(entry == null) { - - entry = new AsyncReadEntry(query); - entry.setPending(); - entry.clearResult(querySupport); - asyncReadMap.put(query, entry, hash); - - performForEach(graph, query, entry, parent, listener, procedure, false); - - } else { - - if(entry.isPending()) { - synchronized(entry) { - if(entry.isPending()) { - throw new IllegalStateException(); - // final AsyncBarrierImpl parentBarrier = graph.state.barrier; - // if(entry.procs == null) entry.procs = new ArrayList>(); - // entry.procs.add(new AsyncProcedure() { - // - // @Override - // public void execute(AsyncReadGraph graph, T result) { - // procedure.execute(graph, result); - // parentBarrier.dec(query); - // } - // - // @Override - // public void exception(AsyncReadGraph graph, Throwable throwable) { - // procedure.exception(graph, throwable); - // parentBarrier.dec(query); - // } - // - // }); -// if(graph.parent != null || listener != null) { -// registerDependencies(graph, entry, parent, listener, procedure, false); -// } + +// int hash = requestHash(query); // -// query.perform(graph, procedure); +// AsyncReadEntry entry = asyncReadMap.get(query, hash); // -// return; - - } - } - } - - if(entry.isReady()) { - entry.performFromCache(graph, this, procedure); - registerDependencies(graph, entry, parent, listener, procedure, false); - } else { - performForEach(graph, query, entry, parent, listener, procedure, false); - } - - } +// if(parent == null && listener == null) { +// if(entry != null && (entry.isReady() || entry.isExcepted())) { +// System.out.println("ready " + query); +// entry.performFromCache(graph, this, procedure); +//// graph.state.barrier.dec(query); +// return; +// } else { +// query.perform(graph, procedure); +//// graph.state.barrier.dec(query); +// return; +// } +// } +// +// if(entry == null) { +// +// entry = new AsyncReadEntry(query); +// entry.setPending(); +// entry.clearResult(querySupport); +// asyncReadMap.put(query, entry, hash); +// +// performForEach(graph, query, entry, parent, listener, procedure, false); +// +// } else { +// +// if(entry.isPending()) { +// synchronized(entry) { +// if(entry.isPending()) { +// throw new IllegalStateException(); +// // final AsyncBarrierImpl parentBarrier = graph.state.barrier; +// // if(entry.procs == null) entry.procs = new ArrayList>(); +// // entry.procs.add(new AsyncProcedure() { +// // +// // @Override +// // public void execute(AsyncReadGraph graph, T result) { +// // procedure.execute(graph, result); +// // parentBarrier.dec(query); +// // } +// // +// // @Override +// // public void exception(AsyncReadGraph graph, Throwable throwable) { +// // procedure.exception(graph, throwable); +// // parentBarrier.dec(query); +// // } +// // +// // }); +//// if(graph.parent != null || listener != null) { +//// registerDependencies(graph, entry, parent, listener, procedure, false); +//// } +//// +//// query.perform(graph, procedure); +//// +//// return; +// +// } +// } +// } +// +// if(entry.isReady()) { +// entry.performFromCache(graph, this, procedure); +// registerDependencies(graph, entry, parent, listener, procedure, false); +// } else { +// performForEach(graph, query, entry, parent, listener, procedure, false); +// } +// +// } } - final static void runMultiRead(final ReadGraphImpl graph, MultiReadEntry cached, final MultiRead query, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final AsyncMultiProcedure procedure) { - - MultiReadEntry entry = cached != null ? cached : provider.multiReadMap.get(query); - if(entry == null) { - - entry = new MultiReadEntry(query); - entry.setPending(); - entry.clearResult(provider.querySupport); - - provider.multiReadMap.put(query, entry); - - provider.performForEach(graph, query, entry, parent, listener, procedure, false); - - } else { - - if(entry.isPending()) { + final void runMultiRead(final ReadGraphImpl graph, MultiReadEntry cached, final MultiRead query, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final AsyncMultiProcedure procedure) { - synchronized(entry) { - - if(entry.isPending()) { - throw new IllegalStateException(); - -// if(entry.procs == null) entry.procs = new ArrayList, AsyncBarrier>>(); -// entry.procs.add(new Pair(procedure, parentBarrier)); -// if(graph.parent != null || listener != null) { -// provider.registerDependencies(graph, entry, parent, listener, procedure, false); -// } + try { + cache.runQuery(graph, query, parent, listener, procedure); + } catch (DatabaseException e) { + throw new IllegalStateException(e); + } - // If this was synchronized we must wait here until completion - // if(graph.state.synchronizedExecution) { - // while(entry.isPending()) { - // graph.resumeTasks(graph.callerThread, null, null); - // } - // } +// MultiReadEntry entry = cached != null ? cached : provider.multiReadMap.get(query); +// if(entry == null) { // -// return; - - } - } - - entry.performFromCache(graph, provider, procedure); -// graph.state.barrier.dec(query); - return; - - } else { - - provider.performForEach(graph, query, entry, parent, listener, procedure, false); - - } - - } +// entry = new MultiReadEntry(query); +// entry.setPending(); +// entry.clearResult(provider.querySupport); +// +// provider.multiReadMap.put(query, entry); +// +// provider.performForEach(graph, query, entry, parent, listener, procedure, false); +// +// } else { +// +// if(entry.isPending()) { +// +// synchronized(entry) { +// +// if(entry.isPending()) { +// throw new IllegalStateException(); +// +//// if(entry.procs == null) entry.procs = new ArrayList, AsyncBarrier>>(); +//// entry.procs.add(new Pair(procedure, parentBarrier)); +//// if(graph.parent != null || listener != null) { +//// provider.registerDependencies(graph, entry, parent, listener, procedure, false); +//// } +// +// // If this was synchronized we must wait here until completion +// // if(graph.state.synchronizedExecution) { +// // while(entry.isPending()) { +// // graph.resumeTasks(graph.callerThread, null, null); +// // } +// // } +//// +//// return; +// +// } +// } +// +// entry.performFromCache(graph, provider, procedure); +//// graph.state.barrier.dec(query); +// return; +// +// } else { +// +// provider.performForEach(graph, query, entry, parent, listener, procedure, false); +// +// } +// +// } } public final void runAsyncMultiRead(final ReadGraphImpl graph, final AsyncMultiRead query, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure procedure) { - int hash = requestHash(query); - - AsyncMultiReadEntry entry = asyncMultiReadMap.get(query, hash); - - if(parent == null && listener == null) { - if(entry != null && (entry.isReady() || entry.isExcepted())) { - System.out.println("ready " + query); - entry.performFromCache(graph, this, procedure); - return; - } else { - query.perform(graph, procedure); - return; - } + + try { + cache.runQuery(graph, query, parent, listener, procedure); + } catch (DatabaseException e) { + throw new IllegalStateException(e); } - if(entry == null) { - - entry = new AsyncMultiReadEntry(query); - entry.setPending(); - entry.clearResult(querySupport); - - asyncMultiReadMap.put(query, entry, hash); - performForEach(graph, query, entry, parent, listener, procedure, false); - - } else { - - if(entry.isPending()) { - - synchronized(entry) { - if(entry.isPending()) { - throw new IllegalStateException(); -// if(entry.procs == null) entry.procs = new ArrayList>(); -// entry.procs.add(procedure); -// if(graph.parent != null || listener != null) { -// registerDependencies(graph, entry, parent, listener, procedure, false); -// } -// return; - } - } - } - - performForEach(graph, query, entry, parent, listener, procedure, false); - - } +// int hash = requestHash(query); +// +// AsyncMultiReadEntry entry = asyncMultiReadMap.get(query, hash); +// +// if(parent == null && listener == null) { +// if(entry != null && (entry.isReady() || entry.isExcepted())) { +// System.out.println("ready " + query); +// entry.performFromCache(graph, this, procedure); +// return; +// } else { +// query.perform(graph, procedure); +// return; +// } +// } +// +// if(entry == null) { +// +// entry = new AsyncMultiReadEntry(query); +// entry.setPending(); +// entry.clearResult(querySupport); +// +// asyncMultiReadMap.put(query, entry, hash); +// +// performForEach(graph, query, entry, parent, listener, procedure, false); +// +// } else { +// +// if(entry.isPending()) { +// +// synchronized(entry) { +// if(entry.isPending()) { +// throw new IllegalStateException(); +//// if(entry.procs == null) entry.procs = new ArrayList>(); +//// entry.procs.add(procedure); +//// if(graph.parent != null || listener != null) { +//// registerDependencies(graph, entry, parent, listener, procedure, false); +//// } +//// return; +// } +// } +// } +// +// performForEach(graph, query, entry, parent, listener, procedure, false); +// +// } } - final static void runPrimitiveRead(ReadGraphImpl graph, ExternalReadEntry cached, final ExternalRead query, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final Procedure procedure) { + final void runPrimitiveRead(ReadGraphImpl graph, ExternalReadEntry cached, final ExternalRead query, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final Procedure procedure) throws DatabaseException { - final ExternalReadEntry entry = cached != null ? cached : provider.externalReadMap.get(query); - if(entry == null) { - provider.performForEach(graph, query, new ExternalReadEntry(query), parent, listener, procedure, false); - } else { - if(entry.isPending()) { - synchronized(entry) { - if(entry.isPending()) { - throw new IllegalStateException(); -// if(entry.procs == null) entry.procs = new ArrayList>(); -// entry.procs.add(procedure); -// return; - } - } - } - provider.performForEach(graph, query, entry, parent, listener, procedure, false); - } + cache.runQuery(graph, query, parent, listener, procedure); + +// final ExternalReadEntry entry = cached != null ? cached : provider.externalReadMap.get(query); +// if(entry == null) { +// provider.performForEach(graph, query, new ExternalReadEntry(query), parent, listener, procedure, false); +// } else { +// if(entry.isPending()) { +// synchronized(entry) { +// if(entry.isPending()) { +// throw new IllegalStateException(); +//// if(entry.procs == null) entry.procs = new ArrayList>(); +//// entry.procs.add(procedure); +//// return; +// } +// } +// } +// provider.performForEach(graph, query, entry, parent, listener, procedure, false); +// } } @@ -835,543 +808,549 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap @Override public T queryRead(final ReadGraphImpl graph, final Read query, final CacheEntry parent, final AsyncProcedure procedure, final ListenerBase listener) throws Throwable { - assert(query != null); - - ReadEntry entry = readMap.get(query); - - if(entry != null) { - if(parent == null && (listener == null || listener.isDisposed()) && entry.isReady()) { - return (T)entry.get(graph, this, procedure); - } else if (entry.isPending()) { - throw new IllegalStateException(); - } - } - - if(entry == null) { - - entry = new ReadEntry(query); - entry.setPending(); - entry.clearResult(querySupport); - - readMap.put(query, entry); - - return (T)performForEach(graph, query, entry, parent, listener, procedure, false); - - } else { - - if(entry.isPending()) { - throw new IllegalStateException(); - } else { - return (T)performForEach(graph, query, entry, parent, listener, procedure, false); - } - - } - - } - - public void queryMultiRead(final ReadGraphImpl graph, final MultiRead query, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure procedure) { - - assert(query != null); - assert(procedure != null); - - final MultiReadEntry entry = multiReadMap.get(query); - - if(parent == null && !(listener != null)) { - if(entry != null && entry.isReady()) { - entry.performFromCache(graph, this, procedure); - return; - } - } - - runMultiRead(graph, entry, query, parent, this, listener, procedure); - - } - - public void queryPrimitiveRead(final ReadGraphImpl graph, final ExternalRead query, final CacheEntry parent, final ListenerBase listener, final Procedure procedure) { - - assert(query != null); - assert(procedure != null); - - final ExternalReadEntry entry = externalReadMap.get(query); - - if(parent == null && !(listener != null)) { - if(entry != null && entry.isReady()) { - entry.performFromCache(procedure); - return; - } - } - - runPrimitiveRead(graph, entry, query, parent, this, listener, procedure); - - } - - public void performForEach(ReadGraphImpl parentGraph, final AsyncRead query, final AsyncReadEntry entry, final CacheEntry parent, final ListenerBase base, final AsyncProcedure procedure, - boolean inferredDependency) { - - if (DebugPolicy.PERFORM) - System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query); - - assert (!dirty); - assert (!collecting); - - assert(!entry.isDiscarded()); - - final ListenerEntry listenerEntry = registerDependencies(parentGraph, entry, parent, base, procedure, inferredDependency); - - // FRESH, REFUTED, EXCEPTED go here - if (!entry.isReady()) { - - entry.setPending(); + return (T)cache.runQuery(graph, query, parent, listener, procedure); - size++; - - try { - - final ReadGraphImpl finalParentGraph = parentGraph; - - query.perform(parentGraph.withParent(entry), new AsyncProcedure() { - - @Override - public void execute(AsyncReadGraph returnGraph, T result) { - ReadGraphImpl impl = (ReadGraphImpl)returnGraph; - //AsyncReadGraph resumeGraph = finalParentGraph.newAsync(); - entry.addOrSet(finalParentGraph, result); - if(listenerEntry != null) { - primeListenerEntry(listenerEntry, result); - } - try { - procedure.execute(finalParentGraph, 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(); - entry.except(finalParentGraph, t); - try { - procedure.exception(finalParentGraph, t); - } catch (Throwable t2) { - t2.printStackTrace(); - } -// parentBarrier.dec(query); - } - - @Override - public String toString() { - return procedure.toString(); - } - - }); - - } catch (Throwable t) { - - entry.except(t); - try { - procedure.exception(parentGraph, t); - } catch (Throwable t2) { - t2.printStackTrace(); - } -// parentBarrier.dec(query); - - } - - misses++; - - } else { - - entry.performFromCache(parentGraph, this, new AsyncProcedure() { - - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - procedure.exception(graph, throwable); - } - - @Override - public void execute(AsyncReadGraph graph, T result) { - procedure.execute(graph, result); - if(listenerEntry != null) { - primeListenerEntry(listenerEntry, result); - } - } - - }); - -// parentBarrier.dec(query); - - hits++; - - } - - assert (!entry.isDiscarded()); - - } - - public T performForEach(final ReadGraphImpl graph, final Read query, final ReadEntry entry, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure procedure, - boolean inferredDependency) throws Throwable { - - if (DebugPolicy.PERFORM) - System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query); - - assert (!dirty); - assert (!collecting); - - entry.assertNotDiscarded(); - - if(entry.isReady()) { - - // EXCEPTED goes here - -// if(procedure != null) entry.performFromCache(graph, this, procedure); -// parentBarrier.dec(query); - hits++; - - ListenerEntry listenerEntry = registerDependencies(graph, entry, parent, listener, procedure, inferredDependency); - - T result = (T)entry.get(graph, this, procedure); - - if(listenerEntry != null) primeListenerEntry(listenerEntry, result); - - return result; - - } else { - - // FRESH, REFUTED, PENDING go here - - entry.setPending(); - - size++; - misses++; - - ListenerEntry listenerEntry = registerDependencies(graph, entry, parent, listener, procedure, inferredDependency); - - final ReadGraphImpl performGraph = graph.newSync(entry); - - try { - - if(Development.DEVELOPMENT) - Development.recordHistogram("run " + query); - - T result = query.perform(performGraph); - entry.addOrSet(performGraph, result); - - if(listenerEntry != null) primeListenerEntry(listenerEntry, result); - - return (T)entry.get(graph, this, procedure); - - } catch (Throwable t) { - - entry.except(t); - return (T)entry.get(graph, this, procedure); - - } - - } +// assert(query != null); +// +// ReadEntry entry = readMap.get(query); +// +// if(entry != null) { +// if(parent == null && (listener == null || listener.isDisposed()) && entry.isReady()) { +// return (T)entry.get(graph, this, procedure); +// } else if (entry.isPending()) { +// throw new IllegalStateException(); +// } +// } +// +// if(entry == null) { +// +// entry = new ReadEntry(query); +// entry.setPending(); +// entry.clearResult(querySupport); +// +// readMap.put(query, entry); +// +// return (T)performForEach(graph, query, entry, parent, listener, procedure, false); +// +// } else { +// +// if(entry.isPending()) { +// throw new IllegalStateException(); +// } else { +// return (T)performForEach(graph, query, entry, parent, listener, procedure, false); +// } +// +// } } - public void performForEach(final ReadGraphImpl graph, final MultiRead query, final MultiReadEntry entry, CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure procedure, - boolean inferredDependency) { - - if (DebugPolicy.PERFORM) - System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query); - - assert (!dirty); - assert (!collecting); - - assert(!entry.isPending()); - assert(!entry.isDiscarded()); - - // FRESH, REFUTED, EXCEPTED go here - if (!entry.isReady()) { - - entry.setPending(); - entry.clearResult(querySupport); - - multiReadMap.put(query, entry); - size++; - - final ReadGraphImpl newGraph = graph.newSync(entry); -// newGraph.state.barrier.inc(); - - try { - - query.perform(newGraph, new AsyncMultiProcedure() { + public void queryMultiRead(final ReadGraphImpl graph, final MultiRead query, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure procedure) throws DatabaseException { - @Override - public void execute(AsyncReadGraph graph, T result) { - entry.addOrSet(result); - try { - procedure.execute(graph, result); - } catch (Throwable t) { - t.printStackTrace(); - } - } - - @Override - public void finished(AsyncReadGraph graph) { - entry.finish(graph); - try { - procedure.finished(graph); - } catch (Throwable t) { - t.printStackTrace(); - } -// newGraph.state.barrier.dec(); -// parentBarrier.dec(); - } + cache.runQuery(graph, query, parent, listener, procedure); - @Override - public void exception(AsyncReadGraph graph, Throwable t) { - entry.except(t); - try { - procedure.exception(graph, t); - } catch (Throwable t2) { - t2.printStackTrace(); - } -// newGraph.state.barrier.dec(); -// parentBarrier.dec(); - } - - }); - - } catch (DatabaseException e) { - - entry.except(e); - try { - procedure.exception(graph, e); - } catch (Throwable t2) { - t2.printStackTrace(); - } -// newGraph.state.barrier.dec(); -// parentBarrier.dec(); - - } catch (Throwable t) { - - DatabaseException e = new DatabaseException(t); - - entry.except(e); - try { - procedure.exception(graph, e); - } catch (Throwable t2) { - t2.printStackTrace(); - } -// newGraph.state.barrier.dec(); -// parentBarrier.dec(); - - } - - misses++; - - } else { - - entry.performFromCache(graph, this, procedure); - hits++; - - - } - - assert (!entry.isDiscarded()); - - registerDependencies(graph, entry, parent, listener, procedure, inferredDependency); +// assert(query != null); +// assert(procedure != null); +// +// final MultiReadEntry entry = multiReadMap.get(query); +// +// if(parent == null && !(listener != null)) { +// if(entry != null && entry.isReady()) { +// entry.performFromCache(graph, this, procedure); +// return; +// } +// } +// +// runMultiRead(graph, entry, query, parent, this, listener, procedure); } + public void queryPrimitiveRead(final ReadGraphImpl graph, final ExternalRead query, final CacheEntry parent, final ListenerBase listener, final Procedure procedure) throws DatabaseException { - public void performForEach(final ReadGraphImpl callerGraph, AsyncMultiRead query, final AsyncMultiReadEntry entry, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure procedure, - boolean inferredDependency) { - - if (DebugPolicy.PERFORM) - System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query); - - assert (!dirty); - assert (!collecting); - - try { - - assert(!entry.isDiscarded()); - - // FRESH, REFUTED, EXCEPTED go here - if (!entry.isReady()) { - - size++; - - try { - - ReadGraphImpl performGraph = callerGraph.withAsyncParent(entry); - - query.perform(performGraph, new AsyncMultiProcedure() { - - @Override - public void execute(AsyncReadGraph graph, T result) { - ReadGraphImpl impl = (ReadGraphImpl)graph; -// ReadGraphImpl executeGraph = callerGraph.newAsync(); - entry.addOrSet(result); - try { - procedure.execute(callerGraph, result); - } catch (Throwable t) { - t.printStackTrace(); - } - } - - @Override - public void finished(AsyncReadGraph graph) { - ReadGraphImpl impl = (ReadGraphImpl)graph; -// ReadGraphImpl executeGraph = callerGraph.newAsync(); - entry.finish(callerGraph); - try { - procedure.finished(callerGraph); - } catch (Throwable t) { - t.printStackTrace(); - } - } - - @Override - public void exception(AsyncReadGraph graph, Throwable t) { - ReadGraphImpl impl = (ReadGraphImpl)graph; -// ReadGraphImpl executeGraph = callerGraph.newAsync(); - entry.except(callerGraph, t); - try { - procedure.exception(callerGraph, t); - } catch (Throwable t2) { - t2.printStackTrace(); - } - } - - }); - - } catch (Throwable t) { - - entry.except(t); - try { - procedure.exception(callerGraph, t); - } catch (Throwable t2) { - t2.printStackTrace(); - } - - } - - - misses++; + cache.runQuery(graph, query, parent, listener, procedure); - } else { - - entry.performFromCache(callerGraph, this, procedure); - - hits++; - - } - - assert (!entry.isDiscarded()); - - registerDependencies(callerGraph, entry, parent, listener, procedure, inferredDependency); - - } catch (Throwable t) { - - Logger.defaultLogError(t); - - } finally { - - } +// assert(query != null); +// assert(procedure != null); +// +// final ExternalReadEntry entry = externalReadMap.get(query); +// +// if(parent == null && !(listener != null)) { +// if(entry != null && entry.isReady()) { +// entry.performFromCache(procedure); +// return; +// } +// } +// +// runPrimitiveRead(graph, entry, query, parent, this, listener, procedure); } - public void performForEach(ReadGraphImpl graph, final ExternalRead query, final ExternalReadEntry entry, final CacheEntry parent, final ListenerBase base, final Procedure procedure, - boolean inferredDependency) { - - if (DebugPolicy.PERFORM) - System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query); - - assert (!dirty); - assert (!collecting); - - assert(!entry.isPending()); - assert(!entry.isDiscarded()); - - registerDependencies(graph, entry, parent, base, procedure, inferredDependency); - - // FRESH, REFUTED, EXCEPTED go here - if (!entry.isReady()) { - - entry.setPending(); - entry.clearResult(querySupport); - - externalReadMap.put(query, entry); - size++; - - try { - - query.register(graph, new Listener() { - - AtomicBoolean used = new AtomicBoolean(false); - - @Override - public void execute(T result) { - - // Just for safety - if(entry.isDiscarded()) return; - if(entry.isExcepted()) entry.setPending(); - - if(used.compareAndSet(false, true)) { - entry.addOrSet(QueryProcessor.this, result); - procedure.execute(result); - } else { - entry.queue(result); - updatePrimitive(query); - } - - } +// public void performForEach(ReadGraphImpl parentGraph, final AsyncRead query, final AsyncReadEntry entry, final CacheEntry parent, final ListenerBase base, final AsyncProcedure procedure, +// boolean inferredDependency) { +// +// if (DebugPolicy.PERFORM) +// System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query); +// +// assert (!dirty); +// assert (!collecting); +// +// assert(!entry.isDiscarded()); +// +// final ListenerEntry listenerEntry = registerDependencies(parentGraph, entry, parent, base, procedure, inferredDependency); +// +// // FRESH, REFUTED, EXCEPTED go here +// if (!entry.isReady()) { +// +// entry.setPending(); +// +// size++; +// +// try { +// +// final ReadGraphImpl finalParentGraph = parentGraph; +// +// query.perform(parentGraph.withParent(entry), new AsyncProcedure() { +// +// @Override +// public void execute(AsyncReadGraph returnGraph, T result) { +// ReadGraphImpl impl = (ReadGraphImpl)returnGraph; +// //AsyncReadGraph resumeGraph = finalParentGraph.newAsync(); +// entry.addOrSet(finalParentGraph, result); +// if(listenerEntry != null) { +// primeListenerEntry(listenerEntry, result); +// } +// try { +// procedure.execute(finalParentGraph, 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(); +// entry.except(finalParentGraph, t); +// try { +// procedure.exception(finalParentGraph, t); +// } catch (Throwable t2) { +// t2.printStackTrace(); +// } +//// parentBarrier.dec(query); +// } +// +// @Override +// public String toString() { +// return procedure.toString(); +// } +// +// }); +// +// } catch (Throwable t) { +// +// entry.except(t); +// try { +// procedure.exception(parentGraph, t); +// } catch (Throwable t2) { +// t2.printStackTrace(); +// } +//// parentBarrier.dec(query); +// +// } +// +// misses++; +// +// } else { +// +// entry.performFromCache(parentGraph, this, new AsyncProcedure() { +// +// @Override +// public void exception(AsyncReadGraph graph, Throwable throwable) { +// procedure.exception(graph, throwable); +// } +// +// @Override +// public void execute(AsyncReadGraph graph, T result) { +// procedure.execute(graph, result); +// if(listenerEntry != null) { +// primeListenerEntry(listenerEntry, result); +// } +// } +// +// }); +// +//// parentBarrier.dec(query); +// +// hits++; +// +// } +// +// assert (!entry.isDiscarded()); +// +// } - @Override - public void exception(Throwable t) { - - entry.except(t); +// public T performForEach(final ReadGraphImpl graph, final Read query, final ReadEntry entry, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure procedure, +// boolean inferredDependency) throws Throwable { +// +// if (DebugPolicy.PERFORM) +// System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query); +// +// assert (!dirty); +// assert (!collecting); +// +// entry.assertNotDiscarded(); +// +// if(entry.isReady()) { +// +// // EXCEPTED goes here +// +//// if(procedure != null) entry.performFromCache(graph, this, procedure); +//// parentBarrier.dec(query); +// hits++; +// +// ListenerEntry listenerEntry = registerDependencies(graph, entry, parent, listener, procedure, inferredDependency); +// +// T result = (T)entry.get(graph, this, procedure); +// +// if(listenerEntry != null) primeListenerEntry(listenerEntry, result); +// +// return result; +// +// } else { +// +// // FRESH, REFUTED, PENDING go here +// +// entry.setPending(); +// +// size++; +// misses++; +// +// ListenerEntry listenerEntry = registerDependencies(graph, entry, parent, listener, procedure, inferredDependency); +// +// final ReadGraphImpl performGraph = graph.newSync(entry); +// +// try { +// +// if(Development.DEVELOPMENT) +// Development.recordHistogram("run " + query); +// +// T result = query.perform(performGraph); +// entry.addOrSet(performGraph, result); +// +// if(listenerEntry != null) primeListenerEntry(listenerEntry, result); +// +// return (T)entry.get(graph, this, procedure); +// +// } catch (Throwable t) { +// +// entry.except(t); +// return (T)entry.get(graph, this, procedure); +// +// } +// +// } +// +// } - if(used.compareAndSet(false, true)) { - procedure.exception(t); - } else { +// public void performForEach(final ReadGraphImpl graph, final MultiRead query, final MultiReadEntry entry, CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure procedure, +// boolean inferredDependency) { +// +// if (DebugPolicy.PERFORM) +// System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query); +// +// assert (!dirty); +// assert (!collecting); +// +// assert(!entry.isPending()); +// assert(!entry.isDiscarded()); +// +// // FRESH, REFUTED, EXCEPTED go here +// if (!entry.isReady()) { +// +// entry.setPending(); +// entry.clearResult(); +// +// multiReadMap.put(query, entry); +// size++; +// +// final ReadGraphImpl newGraph = graph.newSync(entry); +//// newGraph.state.barrier.inc(); +// +// try { +// +// query.perform(newGraph, new AsyncMultiProcedure() { +// +// @Override +// public void execute(AsyncReadGraph graph, T result) { +// entry.addOrSet(result); +// try { +// procedure.execute(graph, result); +// } catch (Throwable t) { +// t.printStackTrace(); +// } +// } +// +// @Override +// public void finished(AsyncReadGraph graph) { +// entry.finish(graph); +// try { +// procedure.finished(graph); +// } catch (Throwable t) { +// t.printStackTrace(); +// } +//// newGraph.state.barrier.dec(); +//// parentBarrier.dec(); +// } +// +// @Override +// public void exception(AsyncReadGraph graph, Throwable t) { +// entry.except(t); +// try { +// procedure.exception(graph, t); +// } catch (Throwable t2) { +// t2.printStackTrace(); +// } +//// newGraph.state.barrier.dec(); +//// parentBarrier.dec(); +// } +// +// }); +// +// } catch (DatabaseException e) { +// +// entry.except(e); +// try { +// procedure.exception(graph, e); +// } catch (Throwable t2) { +// t2.printStackTrace(); +// } +//// newGraph.state.barrier.dec(); +//// parentBarrier.dec(); +// +// } catch (Throwable t) { +// +// DatabaseException e = new DatabaseException(t); +// +// entry.except(e); +// try { +// procedure.exception(graph, e); +// } catch (Throwable t2) { +// t2.printStackTrace(); +// } +//// newGraph.state.barrier.dec(); +//// parentBarrier.dec(); +// +// } +// +// misses++; +// +// } else { +// +// entry.performFromCache(graph, this, procedure); +// hits++; +// +// +// } +// +// assert (!entry.isDiscarded()); +// +// registerDependencies(graph, entry, parent, listener, procedure, inferredDependency); +// +// } +// +// +// public void performForEach(final ReadGraphImpl callerGraph, AsyncMultiRead query, final AsyncMultiReadEntry entry, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure procedure, +// boolean inferredDependency) { +// +// if (DebugPolicy.PERFORM) +// System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query); +// +// assert (!dirty); +// assert (!collecting); +// +// try { +// +// assert(!entry.isDiscarded()); +// +// // FRESH, REFUTED, EXCEPTED go here +// if (!entry.isReady()) { +// +// size++; +// +// try { +// +// ReadGraphImpl performGraph = callerGraph.withAsyncParent(entry); +// +// query.perform(performGraph, new AsyncMultiProcedure() { +// +// @Override +// public void execute(AsyncReadGraph graph, T result) { +// ReadGraphImpl impl = (ReadGraphImpl)graph; +//// ReadGraphImpl executeGraph = callerGraph.newAsync(); +// entry.addOrSet(result); +// try { +// procedure.execute(callerGraph, result); +// } catch (Throwable t) { +// t.printStackTrace(); +// } +// } +// +// @Override +// public void finished(AsyncReadGraph graph) { +// ReadGraphImpl impl = (ReadGraphImpl)graph; +//// ReadGraphImpl executeGraph = callerGraph.newAsync(); +// entry.finish(callerGraph); +// try { +// procedure.finished(callerGraph); +// } catch (Throwable t) { +// t.printStackTrace(); +// } +// } +// +// @Override +// public void exception(AsyncReadGraph graph, Throwable t) { +// ReadGraphImpl impl = (ReadGraphImpl)graph; +//// ReadGraphImpl executeGraph = callerGraph.newAsync(); +// entry.except(callerGraph, t); +// try { +// procedure.exception(callerGraph, t); +// } catch (Throwable t2) { +// t2.printStackTrace(); +// } +// } +// +// }); +// +// } catch (Throwable t) { +// +// entry.except(t); +// try { +// procedure.exception(callerGraph, t); +// } catch (Throwable t2) { +// t2.printStackTrace(); +// } +// +// } +// +// +// misses++; +// +// } else { +// +// entry.performFromCache(callerGraph, this, procedure); +// +// hits++; +// +// } +// +// assert (!entry.isDiscarded()); +// +// registerDependencies(callerGraph, entry, parent, listener, procedure, inferredDependency); +// +// } catch (Throwable t) { +// +// Logger.defaultLogError(t); +// +// } finally { +// +// } +// +// } +// +// public void performForEach(ReadGraphImpl graph, final ExternalRead query, final ExternalReadEntry entry, final CacheEntry parent, final ListenerBase base, final Procedure procedure, +// boolean inferredDependency) { +// +// if (DebugPolicy.PERFORM) +// System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query); +// +// assert (!dirty); +// assert (!collecting); +// +// assert(!entry.isPending()); +// assert(!entry.isDiscarded()); +// +// registerDependencies(graph, entry, parent, base, procedure, inferredDependency); +// +// // FRESH, REFUTED, EXCEPTED go here +// if (!entry.isReady()) { +// +// entry.setPending(); +// entry.clearResult(querySupport); +// +// externalReadMap.put(query, entry); +// size++; +// +// try { +// +// query.register(graph, new Listener() { +// +// AtomicBoolean used = new AtomicBoolean(false); +// +// @Override +// public void execute(T result) { +// +// // Just for safety +// if(entry.isDiscarded()) return; +// if(entry.isExcepted()) entry.setPending(); +// +// if(used.compareAndSet(false, true)) { +// entry.addOrSet(QueryProcessor.this, result); +// procedure.execute(result); +// } else { // entry.queue(result); - updatePrimitive(query); - } - - } - - @Override - public String toString() { - return procedure.toString(); - } - - @Override - public boolean isDisposed() { - return entry.isDiscarded() || !isBound(entry); - } - - }); - - } catch (Throwable t) { - - entry.except(t); - procedure.exception(t); - - } - - misses++; - - } else { - - entry.performFromCache(procedure); - - hits++; - - } - - assert (!entry.isDiscarded()); - - } +// updatePrimitive(query); +// } +// +// } +// +// @Override +// public void exception(Throwable t) { +// +// entry.except(t); +// +// if(used.compareAndSet(false, true)) { +// procedure.exception(t); +// } else { +//// entry.queue(result); +// updatePrimitive(query); +// } +// +// } +// +// @Override +// public String toString() { +// return procedure.toString(); +// } +// +// @Override +// public boolean isDisposed() { +// return entry.isDiscarded() || !isBound(entry); +// } +// +// }); +// +// } catch (Throwable t) { +// +// entry.except(t); +// procedure.exception(t); +// +// } +// +// misses++; +// +// } else { +// +// entry.performFromCache(procedure); +// +// hits++; +// +// } +// +// assert (!entry.isDiscarded()); +// +// } - private boolean isBound(ExternalReadEntry entry) { + boolean isBound(ExternalReadEntry entry) { if(entry.hasParents()) return true; else if(hasListener(entry)) return true; else return false; @@ -1690,101 +1669,12 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap } public Collection getRootList() { - - ArrayList result = new ArrayList(); - - for (Object e : valueMap.values()) { - result.add((CacheEntry) e); - } - for (Object e : directPredicatesMap.values()) { - result.add((CacheEntry) e); - } - for (Object e : objectsMap.values()) { - result.add((CacheEntry) e); - } - for (Object e : directObjectsMap.values()) { - result.add((CacheEntry) e); - } - for (Object e : principalTypesMap.values()) { - result.add((CacheEntry) e); - } - for (Object e : superRelationsMap.values()) { - result.add((CacheEntry) e); - } - for (Object e : superTypesMap.values()) { - result.add((CacheEntry) e); - } - for (Object e : typesMap.values()) { - result.add((CacheEntry) e); - } - for (Object e : objectsMap.values()) { - result.add((CacheEntry) e); - } - for (Object e : assertedStatementsMap.values()) { - result.add((CacheEntry) e); - } - for (Object e : readMap.values()) { - if(e instanceof CacheEntry) { - result.add((CacheEntry) e); - } else { - System.err.println("e=" + e); - } - } - for (Object e : asyncReadMap.values()) { - if(e instanceof CacheEntry) { - result.add((CacheEntry) e); - } else { - System.err.println("e=" + e); - } - } - for (Object e : externalReadMap.values()) { - result.add((CacheEntry) e); - } - for (Object e : orderedSetMap.values()) { - result.add((CacheEntry) e); - } - - return result; - + return cache.getRootList(); } @Override public int calculateCurrentSize() { - - int realSize = 0; - - realSize += directPredicatesMap.size(); - realSize += principalTypesMap.size(); - realSize += uriToResourceMap.size(); - realSize += namespaceIndexMap22.size(); - realSize += projectsMap.size(); - - realSize += relationInfoMap.size(); - realSize += superTypesMap.size(); - realSize += typeHierarchyMap.size(); - realSize += superRelationsMap.size(); - realSize += typesMap.size(); - - realSize += valueMap.size(); - realSize += directObjectsMap.size(); - realSize += objectsMap.size(); - realSize += orderedSetMap.size(); - realSize += predicatesMap.size(); - - realSize += statementsMap.size(); - realSize += assertedPredicatesMap.size(); - realSize += assertedStatementsMap.size(); - realSize += externalReadMap.size(); - realSize += asyncReadMap.size(); - - realSize += readMap.size(); - realSize += asyncMultiReadMap.size(); - realSize += multiReadMap.size(); - - size = realSize; - - return realSize; - + return cache.calculateCurrentSize(); } @Override @@ -1828,10 +1718,10 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap assert (entry != null); assert (procedure != null); - ArrayList list = listeners.get(entry); + ArrayList list = cache.listeners.get(entry); if (list == null) { list = new ArrayList(1); - listeners.put(entry, list); + cache.listeners.put(entry, list); } ListenerEntry result = new ListenerEntry(entry, base, procedure); @@ -1862,22 +1752,22 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap private void removeListener(ListenerEntry entry) { assert (entry != null); - ArrayList list = listeners.get(entry.entry); + ArrayList list = cache.listeners.get(entry.entry); if(list == null) return; boolean success = list.remove(entry); assert (success); if (list.isEmpty()) - listeners.remove(entry.entry); + cache.listeners.remove(entry.entry); } private boolean hasListener(CacheEntry entry) { - if(listeners.get(entry) != null) return true; + if(cache.listeners.get(entry) != null) return true; return false; } boolean hasListenerAfterDisposing(CacheEntry entry) { - if(listeners.get(entry) != null) { - ArrayList entries = listeners.get(entry); + if(cache.listeners.get(entry) != null) { + ArrayList entries = cache.listeners.get(entry); ArrayList list = null; for (ListenerEntry e : entries) { if (e.base.isDisposed()) { @@ -1891,7 +1781,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap } } if (entries.isEmpty()) { - listeners.remove(entry); + cache.listeners.remove(entry); return false; } return true; @@ -1901,8 +1791,8 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap List getListenerEntries(CacheEntry entry) { hasListenerAfterDisposing(entry); - if(listeners.get(entry) != null) - return listeners.get(entry); + if(cache.listeners.get(entry) != null) + return cache.listeners.get(entry); else return Collections.emptyList(); } @@ -2420,7 +2310,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap // System.err.println(" => FOO " + type); if (hasListener) { - ArrayList entries = listeners.get(entry); + ArrayList entries = cache.listeners.get(entry); if(entries != null) { for (ListenerEntry le : entries) { scheduleListener(le); @@ -2776,11 +2666,11 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap @Override public boolean execute(Object arg0) { - ExternalReadEntry query = (ExternalReadEntry)externalReadMap.get(arg0); + ExternalReadEntry query = (ExternalReadEntry)cache.externalReadMap.get(arg0); if (query != null) { boolean listening = update(graph, query); if (!listening && !query.hasParents()) { - externalReadMap.remove(arg0); + cache.externalReadMap.remove(arg0); query.discard(); } } @@ -3003,15 +2893,15 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap public Set getReferencedClusters() { HashSet result = new HashSet(); - for (CacheEntry entry : objectsMap.values()) { + for (CacheEntry entry : cache.objectsMap.values()) { Objects query = (Objects) entry.getQuery(); result.add(querySupport.getClusterId(query.r1())); } - for (CacheEntry entry : directPredicatesMap.values()) { + for (CacheEntry entry : cache.directPredicatesMap.values()) { DirectPredicates query = (DirectPredicates) entry.getQuery(); result.add(querySupport.getClusterId(query.id)); } - for (CacheEntry entry : valueMap.values()) { + for (CacheEntry entry : cache.valueMap.values()) { ValueQuery query = (ValueQuery) entry.getQuery(); result.add(querySupport.getClusterId(query.id)); } @@ -3022,41 +2912,8 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap } CacheCollectionResult allCaches(CacheCollectionResult result) { - - int level = Integer.MAX_VALUE; - directPredicatesMap.values(level, result); - principalTypesMap.values(level, result); - for(CacheEntryBase e : uriToResourceMap.values()) - if(e.getLevel() <= level) - result.add(e); - for(CacheEntryBase e : namespaceIndexMap22.values()) - if(e.getLevel() <= level) - result.add(e); - projectsMap.values(level, result); - - relationInfoMap.values(level, result); - superTypesMap.values(level, result); - typeHierarchyMap.values(level, result); - superRelationsMap.values(level, result); - typesMap.values(level, result); - - valueMap.values(level, result); - directObjectsMap.values(level, result); - objectsMap.values(level, result); - orderedSetMap.values(level, result); - predicatesMap.values(level, result); - - statementsMap.values(level, result); - assertedPredicatesMap.values(level, result); - assertedStatementsMap.values(level, result); - externalReadMap.values(level, result); - asyncReadMap.values(level, result); - readMap.values(level, result); - asyncMultiReadMap.values(level, result); - multiReadMap.values(level, result); - - return result; + return cache.allCaches(result); } @@ -3083,7 +2940,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap public CacheEntryBase iterate(int level) { if(iterator.hasNext()) { ExternalRead request = iterator.next(); - ExternalReadEntry entry = externalReadMap.get(request); + ExternalReadEntry entry = cache.externalReadMap.get(request); if (entry != null) return entry; else return iterate(level); } else { @@ -3103,7 +2960,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap public Collection getRootList() { ArrayList result = new ArrayList(requests.size()); for (ExternalRead request : requests) { - ExternalReadEntry entry = externalReadMap.get(request); + ExternalReadEntry entry = cache.externalReadMap.get(request); if (entry != null) result.add(entry); } @@ -3127,48 +2984,8 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap } public void scanPending() { - - ArrayList entries = new ArrayList(); - - entries.addAll(directPredicatesMap.values()); - entries.addAll(principalTypesMap.values()); - entries.addAll(uriToResourceMap.values()); - entries.addAll(namespaceIndexMap22.values()); - entries.addAll(projectsMap.values()); - entries.addAll(relationInfoMap.values()); - entries.addAll(superTypesMap.values()); - entries.addAll(superRelationsMap.values()); - entries.addAll(typesMap.values()); - entries.addAll(valueMap.values()); - entries.addAll(directObjectsMap.values()); - entries.addAll(objectsMap.values()); - entries.addAll(orderedSetMap.values()); - entries.addAll(predicatesMap.values()); - entries.addAll(orderedSetMap.values()); - entries.addAll(statementsMap.values()); - // entries.addAll(assertedObjectsMap.values()); - entries.addAll(assertedPredicatesMap.values()); - entries.addAll(assertedStatementsMap.values()); - entries.addAll(externalReadMap.values()); - entries.addAll(asyncReadMap.values()); - entries.addAll(externalReadMap.values()); - entries.addAll(readMap.values()); - entries.addAll(asyncMultiReadMap.values()); - entries.addAll(multiReadMap.values()); - entries.addAll(readMap.values()); - System.out.println(entries.size() + " entries."); - for(Object e : entries) { - if(e instanceof CacheEntry) { - CacheEntry en = (CacheEntry)e; - if(en.isPending()) System.out.println("pending " + e); - if(en.isExcepted()) System.out.println("excepted " + e); - if(en.isDiscarded()) System.out.println("discarded " + e); - if(en.isRefuted()) System.out.println("refuted " + e); - if(en.isFresh()) System.out.println("fresh " + e); - } else { - //System.out.println("Unknown object " + e); - } - } + + cache.scanPending(); } @@ -5185,7 +5002,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap assert(graph != null); assert(request != null); - final ReadEntry entry = readMap.get(request); + final ReadEntry entry = cache.readMap.get(request); if(entry != null && entry.isReady()) { return (T)entry.get(graph, this, null); } else { @@ -5199,7 +5016,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap assert(graph != null); assert(request != null); - final ExternalReadEntry entry = externalReadMap.get(request); + final ExternalReadEntry entry = cache.externalReadMap.get(request); if(entry != null && entry.isReady()) { if(entry.isExcepted()) { Throwable t = (Throwable)entry.getResult(); @@ -5250,7 +5067,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap assert(graph != null); assert(request != null); - final AsyncReadEntry entry = asyncReadMap.get(request); + final AsyncReadEntry entry = cache.asyncReadMap.get(request); if(entry != null && entry.isReady()) { if(entry.isExcepted()) { procedure.exception(graph, (Throwable)entry.getResult()); @@ -5269,9 +5086,15 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap assert(request != null); assert(procedure != null); -// impl.state.barrier.inc(null, null); + try { - queryMultiRead(impl, request, parent, listener, procedure); + queryMultiRead(impl, request, parent, listener, procedure); + + } catch (DatabaseException e) { + + throw new IllegalStateException(e); + + } } @@ -5335,32 +5158,40 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap assert(request != null); assert(procedure != null); - queryPrimitiveRead(impl, request, parent, listener, new Procedure() { - - @Override - public void execute(T result) { - try { - procedure.execute(result); - } catch (Throwable t2) { - Logger.defaultLogError(t2); + try { + + queryPrimitiveRead(impl, request, parent, listener, new Procedure() { + + @Override + public void execute(T result) { + try { + procedure.execute(result); + } catch (Throwable t2) { + Logger.defaultLogError(t2); + } } - } - - @Override - public String toString() { - return procedure.toString(); - } - - @Override - public void exception(Throwable t) { - try { - procedure.exception(t); - } catch (Throwable t2) { - Logger.defaultLogError(t2); + + @Override + public String toString() { + return procedure.toString(); } - } - - }); + + @Override + public void exception(Throwable t) { + try { + procedure.exception(t); + } catch (Throwable t2) { + Logger.defaultLogError(t2); + } + } + + }); + + } catch (DatabaseException e) { + + throw new IllegalStateException(e); + + } } 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 e4ff1ea87..edc324549 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 @@ -97,7 +97,7 @@ final public class ReadEntry extends CacheEntryBase { @Override public void removeEntry(QueryProcessor processor) { - processor.readMap.remove(request); + processor.cache.readMap.remove(request); } @Override @@ -119,28 +119,28 @@ final public class ReadEntry extends CacheEntryBase { } - public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { + public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { AsyncProcedure proc = (AsyncProcedure)procedure; - 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(); - } - - } + 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(); + } @Override 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 d32da5be5..f2081d35f 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 @@ -11,7 +11,6 @@ *******************************************************************************/ package org.simantics.db.impl.query; -import java.util.ArrayList; import java.util.concurrent.Semaphore; import java.util.concurrent.atomic.AtomicBoolean; @@ -23,8 +22,6 @@ import org.simantics.db.procedure.ListenerBase; import org.simantics.db.request.RequestFlags; final public class RelationInfoQuery extends UnaryQuery> { - -// public ArrayList> procs = null; private RelationInfoQuery(final int resource) { super(resource); @@ -32,7 +29,7 @@ final public class RelationInfoQuery extends UnaryQuery procedure) { - RelationInfoQuery entry = (RelationInfoQuery)provider.relationInfoMap.get(r); + RelationInfoQuery entry = (RelationInfoQuery)provider.cache.relationInfoMap.get(r); if(entry == null) { entry = new RelationInfoQuery(r); @@ -67,7 +64,7 @@ final public class RelationInfoQuery extends UnaryQuery procedure) { - RelationInfoQuery entry = (RelationInfoQuery)provider.relationInfoMap.get(r); + RelationInfoQuery entry = (RelationInfoQuery)provider.cache.relationInfoMap.get(r); if(entry != null && entry.isReady()) { entry.performFromCache(graph, provider, procedure); return entry.getResult(); @@ -80,7 +77,7 @@ final public class RelationInfoQuery extends UnaryQuery> getEntry(QueryProcessor provider) { - return provider.relationInfoMap.get(id); + return provider.cache.relationInfoMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.relationInfoMap.put(id, this); + provider.cache.relationInfoMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.relationInfoMap.remove(id); + provider.cache.relationInfoMap.remove(id); } private void computeAssertions(ReadGraphImpl graph, final boolean isFinal, final boolean isFunctional, final QueryProcessor queryProvider, final InternalProcedure proc) { 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 935b81159..d251a65c1 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 @@ -27,27 +27,25 @@ import org.simantics.db.request.RequestFlags; final public class Statements extends CollectionBinaryQuery { -// public ArrayList procs = null; - public Statements(final int r1, final int r2) { super(r1, r2); } final static Statements entry(final QueryProcessor processor, final int r1, final int r2) { - return (Statements)processor.statementsMap.get(id(r1,r2)); + return (Statements)processor.cache.statementsMap.get(id(r1,r2)); } final static Collection entries(final QueryProcessor processor, final int r1) { - return processor.statementsMap.values(r1); + return processor.cache.statementsMap.values(r1); } final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) { QueryProcessor processor = graph.processor; - Statements entry = (Statements)processor.statementsMap.get(id(r1,r2)); + Statements entry = (Statements)processor.cache.statementsMap.get(id(r1,r2)); if(entry == null) { entry = new Statements(r1, r2); @@ -93,17 +91,17 @@ final public class Statements extends CollectionBinaryQuery @Override public BinaryQuery getEntry(QueryProcessor provider) { - return provider.statementsMap.get(id); + return provider.cache.statementsMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.statementsMap.put(id, this); + provider.cache.statementsMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.statementsMap.remove(id); + provider.cache.statementsMap.remove(id); } final static TripleIntProcedure NOPT = new TripleIntProcedure() { @@ -842,19 +840,22 @@ final public class Statements extends CollectionBinaryQuery } @Override - public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) { + public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) { assert(isReady()); - if(handleException(graph, procedure)) return; - final IntArray value = (IntArray)getResult(); + + if(handleException(graph, procedure)) return value; + for(int i=0;i extends CacheEntryBase implements Q } @Override - public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { + public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { throw new Error("Not possible."); } 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 e76f702e1..fc3cbebab 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 @@ -22,8 +22,6 @@ import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.procedure.ListenerBase; final public class SuperRelations extends UnaryQuery> { - -// public ArrayList> procs = null; private SuperRelations(final int resource) { super(resource); @@ -31,13 +29,13 @@ final public class SuperRelations extends UnaryQuery> final static SuperRelations entry(final QueryProcessor provider, final int r) { - return (SuperRelations)provider.superRelationsMap.get(r); + return (SuperRelations)provider.cache.superRelationsMap.get(r); } final static IntSet runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) { - SuperRelations entry = (SuperRelations)provider.superRelationsMap.get(r); + SuperRelations entry = (SuperRelations)provider.cache.superRelationsMap.get(r); if(entry == null) { entry = new SuperRelations(r); @@ -60,7 +58,7 @@ final public class SuperRelations extends UnaryQuery> final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) throws Throwable { - SuperRelations entry = (SuperRelations)provider.superRelationsMap.get(r); + SuperRelations entry = (SuperRelations)provider.cache.superRelationsMap.get(r); if(entry == null) { entry = new SuperRelations(r); @@ -84,7 +82,7 @@ final public class SuperRelations extends UnaryQuery> final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) { if(parent == null && listener == null) { - SuperRelations entry = (SuperRelations)provider.superRelationsMap.get(r); + SuperRelations entry = (SuperRelations)provider.cache.superRelationsMap.get(r); if(entry != null && entry.isReady()) { entry.performFromCache(graph, provider, procedure); return; @@ -98,7 +96,7 @@ final public class SuperRelations extends UnaryQuery> final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) throws Throwable { if(parent == null && listener == null) { - SuperRelations entry = (SuperRelations)provider.superRelationsMap.get(r); + SuperRelations entry = (SuperRelations)provider.cache.superRelationsMap.get(r); if(entry != null && entry.isReady()) { return (IntSet)entry.get(graph, provider, procedure); } @@ -110,17 +108,17 @@ final public class SuperRelations extends UnaryQuery> @Override public UnaryQuery> getEntry(QueryProcessor provider) { - return provider.superRelationsMap.get(id); + return provider.cache.superRelationsMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.superRelationsMap.put(id, this); + provider.cache.superRelationsMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.superRelationsMap.remove(id); + provider.cache.superRelationsMap.remove(id); } static int histoCounter = 0; 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 0489546c5..1fce56870 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 @@ -20,8 +20,6 @@ import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.procedure.ListenerBase; final public class SuperTypes extends UnaryQuery> { - -// public ArrayList> procs = null; private SuperTypes(final int resource) { super(resource); @@ -29,7 +27,7 @@ final public class SuperTypes extends UnaryQuery> { final static SuperTypes runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure procedure) { - SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r); + SuperTypes entry = (SuperTypes)provider.cache.superTypesMap.get(r); if(entry == null) { entry = new SuperTypes(r); @@ -64,7 +62,7 @@ final public class SuperTypes extends UnaryQuery> { final public static SuperTypes queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) { if(parent == null && listener == null) { - SuperTypes entry = (SuperTypes)provider.superTypesMap.get(r); + SuperTypes entry = (SuperTypes)provider.cache.superTypesMap.get(r); if(entry != null && entry.isReady()) { entry.performFromCache(graph, provider, procedure); return entry; @@ -77,17 +75,17 @@ final public class SuperTypes extends UnaryQuery> { @Override public UnaryQuery> getEntry(QueryProcessor provider) { - return provider.superTypesMap.get(id); + return provider.cache.superTypesMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.superTypesMap.put(id, this); + provider.cache.superTypesMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.superTypesMap.remove(id); + provider.cache.superTypesMap.remove(id); } @Override 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 5c5d6cb09..6a96e11ea 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 @@ -29,7 +29,7 @@ final public class TypeHierarchy extends UnaryQuery> { final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure procedure) { - TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r); + TypeHierarchy entry = (TypeHierarchy)provider.cache.typeHierarchyMap.get(r); if(entry == null) { entry = new TypeHierarchy(r); @@ -60,7 +60,7 @@ final public class TypeHierarchy extends UnaryQuery> { final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) { if(parent == null && listener == null) { - TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r); + TypeHierarchy entry = (TypeHierarchy)provider.cache.typeHierarchyMap.get(r); if(entry != null && entry.isReady()) { entry.performFromCache(graph, provider, procedure); return; @@ -73,17 +73,17 @@ final public class TypeHierarchy extends UnaryQuery> { @Override public UnaryQuery> getEntry(QueryProcessor provider) { - return provider.typeHierarchyMap.get(id); + return provider.cache.typeHierarchyMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.typeHierarchyMap.put(id, this); + provider.cache.typeHierarchyMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.typeHierarchyMap.remove(id); + provider.cache.typeHierarchyMap.remove(id); } @Override 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 49cb5c4c6..0f4918850 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 @@ -29,12 +29,12 @@ final public class Types extends UnaryQuery> { } final static Types entry(final QueryProcessor provider, final int r) { - return (Types)provider.typesMap.get(r); + return (Types)provider.cache.typesMap.get(r); } final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, Types cached, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) { - Types entry = cached != null ? cached : (Types)provider.typesMap.get(r); + Types entry = cached != null ? cached : (Types)provider.cache.typesMap.get(r); if(entry == null) { entry = new Types(r); @@ -56,7 +56,7 @@ final public class Types extends UnaryQuery> { final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable { - Types entry = (Types)provider.typesMap.get(r); + Types entry = (Types)provider.cache.typesMap.get(r); if(entry == null) { entry = new Types(r); @@ -79,7 +79,7 @@ final public class Types extends UnaryQuery> { final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) { - final Types entry = (Types)provider.typesMap.get(r); + final Types entry = (Types)provider.cache.typesMap.get(r); if(parent == null && listener == null) { if(entry != null && entry.isReady()) { @@ -95,7 +95,7 @@ final public class Types extends UnaryQuery> { final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable { if(parent == null) { - Types entry = (Types)provider.typesMap.get(r); + Types entry = (Types)provider.cache.typesMap.get(r); if(entry != null && entry.isReady()) { return (IntSet)entry.get(graph, provider, null); } @@ -107,17 +107,17 @@ final public class Types extends UnaryQuery> { @Override public UnaryQuery> getEntry(QueryProcessor provider) { - return provider.typesMap.get(id); + return provider.cache.typesMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.typesMap.put(id, this); + provider.cache.typesMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.typesMap.remove(id); + provider.cache.typesMap.remove(id); } @Override 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 c466cb261..1098ed313 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 @@ -30,14 +30,14 @@ public class URIToResource extends StringQuery> { } final static URIToResource entry(final QueryProcessor provider, final String id) { - return (URIToResource)provider.uriToResourceMap.get(id); + return (URIToResource)provider.cache.uriToResourceMap.get(id); } final static void runner(ReadGraphImpl graph, final String id, CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) { QueryProcessor processor = graph.processor; - URIToResource entry = (URIToResource)processor.uriToResourceMap.get(id); + URIToResource entry = (URIToResource)processor.cache.uriToResourceMap.get(id); if(entry == null) { entry = new URIToResource(id); @@ -81,17 +81,17 @@ public class URIToResource extends StringQuery> { @Override public URIToResource getEntry(QueryProcessor provider) { - return provider.uriToResourceMap.get(id); + return provider.cache.uriToResourceMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.uriToResourceMap.put(id, this); + provider.cache.uriToResourceMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.uriToResourceMap.remove(id); + provider.cache.uriToResourceMap.remove(id); } private void lookup(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure procedure, final String namespace, final String name) { diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQuery.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQuery.java index ee89bedf7..ef6015070 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQuery.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQuery.java @@ -58,8 +58,8 @@ abstract public class UnaryQuery extends CacheEntryBase implements Qu } @Override - public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { - performFromCache(graph, (QueryProcessor)provider, (Procedure)procedure); + public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) { + return performFromCache(graph, (QueryProcessor)provider, (Procedure)procedure); } abstract public void recompute(ReadGraphImpl graph, QueryProcessor provider); 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 d85595831..770a25cb9 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 @@ -24,14 +24,14 @@ final public class ValueQuery extends UnaryQuery> { } final static ValueQuery entry(final QueryProcessor provider, final int r) { - return (ValueQuery)provider.valueMap.get(r); + return (ValueQuery)provider.cache.valueMap.get(r); } final static byte[] runner(final ReadGraphImpl graph, final int r, CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) { QueryProcessor processor = graph.processor; - ValueQuery entry = (ValueQuery)processor.valueMap.get(r); + ValueQuery entry = (ValueQuery)processor.cache.valueMap.get(r); if(entry == null) { entry = new ValueQuery(r); @@ -75,17 +75,17 @@ final public class ValueQuery extends UnaryQuery> { @Override public UnaryQuery> getEntry(QueryProcessor provider) { - return provider.valueMap.get(id); + return provider.cache.valueMap.get(id); } @Override public void putEntry(QueryProcessor provider) { - provider.valueMap.put(id, this); + provider.cache.valueMap.put(id, this); } @Override final public void removeEntry(QueryProcessor provider) { - provider.valueMap.remove(id); + provider.cache.valueMap.remove(id); } diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/QueryDebugImpl.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/QueryDebugImpl.java index 7a6421768..93b3e363d 100644 --- a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/QueryDebugImpl.java +++ b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/QueryDebugImpl.java @@ -22,7 +22,7 @@ public class QueryDebugImpl implements QueryDebug { @Override public Set getParents(AsyncRead request) { HashSet result = new HashSet(); - CacheEntryBase entry = session.queryProvider2.asyncReadMap.get(request); + CacheEntryBase entry = session.queryProvider2.cache.asyncReadMap.get(request); if(entry != null) { for(CacheEntry parent : entry.getParents(session.queryProvider2)) result.add(parent); } @@ -32,7 +32,7 @@ public class QueryDebugImpl implements QueryDebug { @Override public Set getParents(AsyncMultiRead request) { HashSet result = new HashSet(); - CacheEntryBase entry = session.queryProvider2.asyncMultiReadMap.get(request); + CacheEntryBase entry = session.queryProvider2.cache.asyncMultiReadMap.get(request); if(entry != null) { for(CacheEntry parent : entry.getParents(session.queryProvider2)) result.add(parent); } @@ -42,7 +42,7 @@ public class QueryDebugImpl implements QueryDebug { @Override public Set getParents(Read request) { HashSet result = new HashSet(); - CacheEntryBase entry = session.queryProvider2.readMap.get(request); + CacheEntryBase entry = session.queryProvider2.cache.readMap.get(request); if(entry != null) { for(CacheEntry parent : entry.getParents(session.queryProvider2)) result.add(parent); } @@ -52,7 +52,7 @@ public class QueryDebugImpl implements QueryDebug { @Override public Set getParents(MultiRead request) { HashSet result = new HashSet(); - CacheEntryBase entry = session.queryProvider2.multiReadMap.get(request); + CacheEntryBase entry = session.queryProvider2.cache.multiReadMap.get(request); if(entry != null) { for(CacheEntry parent : entry.getParents(session.queryProvider2)) result.add(parent); } diff --git a/bundles/org.simantics.db/src/org/simantics/db/request/AsyncMultiRead.java b/bundles/org.simantics.db/src/org/simantics/db/request/AsyncMultiRead.java index cb191f647..a8e2f2b61 100644 --- a/bundles/org.simantics.db/src/org/simantics/db/request/AsyncMultiRead.java +++ b/bundles/org.simantics.db/src/org/simantics/db/request/AsyncMultiRead.java @@ -50,7 +50,7 @@ import org.simantics.db.procedure.AsyncMultiProcedure; * @see AsyncMultiProcedure * @see Session */ -public interface AsyncMultiRead extends Request { +public interface AsyncMultiRead { /** * When a GraphRequest is serviced by the database session diff --git a/bundles/org.simantics.db/src/org/simantics/db/request/AsyncRead.java b/bundles/org.simantics.db/src/org/simantics/db/request/AsyncRead.java index a197a5f21..42a87600a 100644 --- a/bundles/org.simantics.db/src/org/simantics/db/request/AsyncRead.java +++ b/bundles/org.simantics.db/src/org/simantics/db/request/AsyncRead.java @@ -50,7 +50,7 @@ import org.simantics.db.procedure.AsyncProcedure; * @see AsyncProcedure * @see Session */ -public interface AsyncRead extends Request { +public interface AsyncRead { /** * When a GraphRequest is serviced by the database session @@ -77,5 +77,6 @@ public interface AsyncRead extends Request { */ void perform(AsyncReadGraph graph, AsyncProcedure procedure); int getFlags(); + int threadHash(); } diff --git a/bundles/org.simantics.db/src/org/simantics/db/request/Request.java b/bundles/org.simantics.db/src/org/simantics/db/request/Request.java deleted file mode 100644 index ef605a53b..000000000 --- a/bundles/org.simantics.db/src/org/simantics/db/request/Request.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.simantics.db.request; - -public interface Request { - - /* - * The integer value obtained from this method can be used to determine - * the evaluation thread of the request. - * - * @return A non-negative integer value - * - */ - int threadHash(); - -} diff --git a/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/queries/QueryExecutor2.java b/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/queries/QueryExecutor2.java index 9814925e3..4753c0b3b 100644 --- a/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/queries/QueryExecutor2.java +++ b/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/queries/QueryExecutor2.java @@ -47,11 +47,6 @@ public abstract class QueryExecutor2 extends ReadRequest implements AsyncListene public void execute(RequestProcessor processor) throws DatabaseException { AsyncMultiRead request = new AsyncMultiRead() { - - @Override - public int threadHash() { - return hashCode(); - } @Override public void perform(AsyncReadGraph graph, diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestParentTest3.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestParentTest3.java index 2bec7c21e..8181e08fb 100644 --- a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestParentTest3.java +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestParentTest3.java @@ -33,11 +33,6 @@ public class RequestParentTest3 extends ExistingDatabaseTest { QueryDebug debug = session.getService(QueryDebug.class); class Request implements AsyncMultiRead { - - @Override - public int threadHash() { - return hashCode(); - } @Override public void perform(AsyncReadGraph graph, AsyncMultiProcedure callback) { -- 2.47.1