Multiple reader thread support for db client 69/1969/7
authorAntti Villberg <antti.villberg@semantum.fi>
Fri, 27 Jul 2018 20:33:59 +0000 (23:33 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 3 Aug 2018 05:00:34 +0000 (08:00 +0300)
gitlab #5

Change-Id: Ia5c189525f2f8277bfc10234c41a1ae47082bb15

180 files changed:
bundles/org.simantics.acorn/src/org/simantics/acorn/cluster/ClusterBig.java
bundles/org.simantics.acorn/src/org/simantics/acorn/cluster/ClusterSmall.java
bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyGraphLabeler.java
bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyParametrizedViewpoint.java
bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyResourceQueryContainer.java
bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyViewpoint.java
bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/CallbackViewpointContributionImpl.java
bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/FinalCheckedStateContributionImpl.java
bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/FinalImageDecoratorContributionImpl.java
bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/FinalImagerContributionImpl.java
bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/FinalLabelDecoratorContributionImpl.java
bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/FinalLabelerContributionImpl.java
bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/FinalViewpointContributionImpl.java
bundles/org.simantics.db.common/src/org/simantics/db/common/primitiverequest/ForEachAssertedObject.java
bundles/org.simantics.db.common/src/org/simantics/db/common/primitiverequest/IsInstanceOf.java
bundles/org.simantics.db.common/src/org/simantics/db/common/primitiverequest/PossibleAdapter.java
bundles/org.simantics.db.common/src/org/simantics/db/common/primitiverequest/RelationInfoRequest.java
bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/SingleSetSyncListenerDelegate.java
bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/wrapper/NoneToSyncMultiListener.java [new file with mode: 0644]
bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/wrapper/NoneToSyncMultiProcedure.java [new file with mode: 0644]
bundles/org.simantics.db.common/src/org/simantics/db/common/processor/MergingGraphRequestProcessor.java
bundles/org.simantics.db.common/src/org/simantics/db/common/processor/ProcessorBase.java
bundles/org.simantics.db.common/src/org/simantics/db/common/request/PossibleTypedParent.java
bundles/org.simantics.db.common/src/org/simantics/db/common/request/ResourceAsyncMultiRead.java
bundles/org.simantics.db.common/src/org/simantics/db/common/request/ResourceAsyncRead2.java
bundles/org.simantics.db.common/src/org/simantics/db/common/uri/UnescapedChildMapOfResource.java
bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java
bundles/org.simantics.db.common/src/org/simantics/db/common/utils/OrderedSetUtils.java
bundles/org.simantics.db.common/src/org/simantics/db/common/utils/Transaction.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncMultiProcedure.java [new file with mode: 0644]
bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncProcedure.java [new file with mode: 0644]
bundles/org.simantics.db.impl/src/org/simantics/db/impl/ClusterI.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/ForEachObjectContextProcedure.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/ForEachObjectProcedure.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/ForPossibleRelatedValueContextProcedure.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/ForPossibleRelatedValueProcedure.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/TransientGraph.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/AsyncBarrierImpl.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/MultiIntProcedure.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphSupport.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/WriteGraphImpl.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/InternalProcedure.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/ResultCallWrappedSyncQueryProcedure.java [new file with mode: 0644]
bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/TripleIntProcedureAdapter.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedPredicates.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedStatements.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncMultiReadEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQuery.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQueryHash.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntryBase.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ChildMap.java [new file with mode: 0644]
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CodeGen.java [new file with mode: 0644]
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CollectionUnaryQuery.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectPredicates.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectSuperRelations.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DoubleKeyQueryHashMap.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/IntProcedure.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/IntSet.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/MultiReadEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java [deleted file]
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Objects.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/OrderedSet.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PossibleSuperRelation.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Predicates.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PrincipalTypes.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Query.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCache.java [new file with mode: 0644]
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCacheBase.java [new file with mode: 0644]
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryCollectorImpl2.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHash.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryProcessor.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QuerySupport.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryThread.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ReadEntry.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/RelationInfoQuery.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Statements.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/StringQuery.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperRelations.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SuperTypes.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/SyncIntProcedure.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ThreadRunnable.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TripleIntProcedure.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQuery.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQueryHash.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQueryP.java [new file with mode: 0644]
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ValueQuery.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/TGRemover.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependenciesRelation.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/ExternalRequest.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/NamespaceRequirements.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/OntologiesForModel.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/combinations/Combinators.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ConsistsOfProcess.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelTransferableGraphSourceRequest.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterTable.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterWriteOnly.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/CollectionSupportImpl.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/DirectQuerySupportImpl.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ObjectResourceMap.java [new file with mode: 0644]
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/QueryControlImpl.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/QueryDebugImpl.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/QuerySupportImpl.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplSocket.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionRequestManager.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/UndoRedoSupportImpl.java
bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ClusterBig.java
bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ClusterSmall.java
bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/IntHash.java
bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ObjectTable.java
bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ResourceElementSmall.java
bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ResourceTable.java
bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ResourceTableSmall.java
bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/TableIntArraySet.java
bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/TableIntSet.java
bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/AdaptionService2.java
bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/reflection/AbstractReflectionAdapter.java
bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/reflection/ReflectionAdapter2.java
bundles/org.simantics.db.testing/src/org/simantics/db/testing/common/TestBase.java
bundles/org.simantics.db/src/org/simantics/db/AsyncReadGraph.java
bundles/org.simantics.db/src/org/simantics/db/AsyncRequestProcessor.java
bundles/org.simantics.db/src/org/simantics/db/AsyncRequestProcessorSpecific.java
bundles/org.simantics.db/src/org/simantics/db/ObjectResourceIdMap.java [new file with mode: 0644]
bundles/org.simantics.db/src/org/simantics/db/ReadGraph.java
bundles/org.simantics.db/src/org/simantics/db/RequestProcessor.java
bundles/org.simantics.db/src/org/simantics/db/RequestProcessorSpecific.java
bundles/org.simantics.db/src/org/simantics/db/Session.java
bundles/org.simantics.db/src/org/simantics/db/procedure/SyncContextMultiProcedure.java [new file with mode: 0644]
bundles/org.simantics.db/src/org/simantics/db/procedure/SyncContextProcedure.java [new file with mode: 0644]
bundles/org.simantics.db/src/org/simantics/db/request/AsyncMultiRead.java
bundles/org.simantics.db/src/org/simantics/db/request/AsyncRead.java
bundles/org.simantics.db/src/org/simantics/db/request/MultiRead.java
bundles/org.simantics.db/src/org/simantics/db/request/Request.java [deleted file]
bundles/org.simantics.db/src/org/simantics/db/service/DirectQuerySupport.java
bundles/org.simantics.db/src/org/simantics/db/service/QueryControl.java
bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConstantStyle.java
bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DiagramContentRequest.java
bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GraphToDiagramSynchronizer.java
bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/MappedTypeGroup.java
bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/TypeGroup.java
bundles/org.simantics.diagram/src/org/simantics/diagram/profile/DiagramElementGroup.java
bundles/org.simantics.diagram/src/org/simantics/diagram/profile/StyleBase.java
bundles/org.simantics.diagram/src/org/simantics/diagram/runtime/RuntimeDiagramManager.java
bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java
bundles/org.simantics.issues.common/src/org/simantics/issues/common/IssueUtils.java
bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/queries/QueryExecutor2.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/monitor/MonitorClassFactory2.java
bundles/org.simantics.modeling/src/org/simantics/modeling/scl/issue/SCLExpressionIssueProvider.java
bundles/org.simantics.modeling/src/org/simantics/modeling/subscription/ModelHistoryCollector.java
bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/Group.java
bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/Style.java
bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java
bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/GraphUI.java
bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SheetFactory.java
bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceEditorInput2.java
bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/input/InputValidationCombinators.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/readGraph/forObjectSet/ForObjectSetTest1.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/AsyncTransactionTest.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestParentTest3.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestParentTest4.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestParentTest5.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestParentTest6.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestProcessorTest1.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/RequestQueuingTest.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncAsyncSyncTest.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncAsyncSyncTest2.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncAsyncSyncTest5.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncAsyncSyncTest6.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/SyncAsyncSyncTest9.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/HierarchicalNames.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/performance/read/ReadHierarchicalNames.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/SimanticsBug1659Test1.java
tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/SimanticsBug1659Test2.java

index 1de4006a72e630482a22dd5390c39b2e411fb15b..063bcf548fa27f687318a1e021fcae511f8bf668 100644 (file)
@@ -40,8 +40,8 @@ import org.simantics.db.impl.Table;
 import org.simantics.db.impl.TableHeader;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.query.QueryProcessor;
-import org.simantics.db.procedure.AsyncContextMultiProcedure;
-import org.simantics.db.procedure.AsyncMultiProcedure;
+import org.simantics.db.procedure.SyncContextMultiProcedure;
+import org.simantics.db.procedure.SyncMultiProcedure;
 import org.simantics.db.procore.cluster.ClusterMap;
 import org.simantics.db.procore.cluster.ClusterPrintDebugInfo;
 import org.simantics.db.procore.cluster.ClusterTraits;
@@ -54,6 +54,8 @@ import org.simantics.db.procore.cluster.ResourceTable;
 import org.simantics.db.procore.cluster.ValueTable;
 import org.simantics.db.service.ClusterUID;
 
+import fi.vtt.simantics.procore.internal.SessionImplSocket;
+
 final public class ClusterBig extends ClusterImpl {
     private static final int TABLE_HEADER_SIZE = TableHeader.HEADER_SIZE + TableHeader.EXTRA_SIZE;
     private static final int RESOURCE_TABLE_OFFSET = 0;
@@ -263,7 +265,7 @@ final public class ClusterBig extends ClusterImpl {
         return objectTable.getSingleObject(objectIndex, support, this);
     }
 
-    public void forObjects(int resourceKey, int predicateKey, int objectIndex, QueryProcessor processor, ReadGraphImpl graph, AsyncMultiProcedure<Resource> procedure,
+    public void forObjects(int resourceKey, int predicateKey, int objectIndex, QueryProcessor processor, ReadGraphImpl graph, SyncMultiProcedure<Resource> procedure,
             ClusterSupport support) throws DatabaseException {
         if (DEBUG)
             System.out.println("Cluster.forObjects1: rk=" + resourceKey + " pk=" + predicateKey);
@@ -276,7 +278,7 @@ final public class ClusterBig extends ClusterImpl {
         }
         objectTable.foreachObject(graph, objectIndex, procedure, this);
     }
-    public <C> void forObjects(int resourceKey, int predicateKey, int objectIndex, QueryProcessor processor, ReadGraphImpl graph, C context, AsyncContextMultiProcedure<C, Resource> procedure,
+    public <C> void forObjects(int resourceKey, int predicateKey, int objectIndex, QueryProcessor processor, ReadGraphImpl graph, C context, SyncContextMultiProcedure<C, Resource> procedure,
             ClusterSupport support) throws DatabaseException {
         if (DEBUG)
             System.out.println("Cluster.forObjects1: rk=" + resourceKey + " pk=" + predicateKey);
@@ -355,57 +357,53 @@ final public class ClusterBig extends ClusterImpl {
     
     @Override
     public void forObjects(ReadGraphImpl graph, int resourceKey,
-            int predicateKey, AsyncMultiProcedure<Resource> procedure)
+            int predicateKey, SyncMultiProcedure<Resource> procedure)
             throws DatabaseException {
         
-       throw new UnsupportedOperationException();
-       
-//        SessionImplSocket session = (SessionImplSocket)graph.getSession();
-//        ClusterSupport support = session.clusterTranslator;
-//        
-//        if (DEBUG)
-//            System.out.println("Cluster.forObjects3: rk=" + resourceKey + " pk=" + predicateKey);
-//        final int resourceIndex = getLocalReference(resourceKey);
-//        final int pRef = getInternalReferenceOrZero(predicateKey, support);
-//        final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
-//        if (ClusterI.CompleteTypeEnum.NotComplete != pCompleteType) {
-//            resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
-//            return;
-//        }
-//        final int predicateIndex = resourceTable.getPredicateIndex(resourceIndex);
-//        if (0 == predicateIndex) {
-//            resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
-//            return;
-//        }
-//        int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef);
-//        forObjects(resourceKey, predicateKey, objectIndex, graph.processor, graph, procedure, support);
+        SessionImplSocket session = (SessionImplSocket)graph.getSession();
+        ClusterSupport support = session.clusterTranslator;
+        
+        if (DEBUG)
+            System.out.println("Cluster.forObjects3: rk=" + resourceKey + " pk=" + predicateKey);
+        final int resourceIndex = getLocalReference(resourceKey);
+        final int pRef = getInternalReferenceOrZero(predicateKey, support);
+        final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
+        if (ClusterI.CompleteTypeEnum.NotComplete != pCompleteType) {
+            resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
+            return;
+        }
+        final int predicateIndex = resourceTable.getPredicateIndex(resourceIndex);
+        if (0 == predicateIndex) {
+            resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
+            return;
+        }
+        int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef);
+        forObjects(resourceKey, predicateKey, objectIndex, graph.processor, graph, procedure, support);
         
     }
     
     @Override
     public void forObjects(ReadGraphImpl graph, int resourceKey, ForEachObjectProcedure procedure) throws DatabaseException {
        
-       throw new UnsupportedOperationException();
-       
-//        SessionImplSocket session = (SessionImplSocket)graph.getSession();
-//        ClusterSupport support = session.clusterTranslator;
-//        final int predicateKey = procedure.predicateKey;
-//        if (DEBUG)
-//            System.out.println("Cluster.forObjects3: rk=" + resourceKey + " pk=" + predicateKey);
-//        final int resourceIndex = getLocalReference(resourceKey);
-//        final int pRef = getInternalReferenceOrZero(predicateKey, support);
-//        final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
-//        if (ClusterI.CompleteTypeEnum.NotComplete != pCompleteType) {
-//            resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
-//            return;
-//        }
-//        final int predicateIndex = resourceTable.getPredicateIndex(resourceIndex);
-//        if (0 == predicateIndex) {
-//            resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
-//            return;
-//        }
-//        int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef);
-//        forObjects(resourceKey, predicateKey, objectIndex, graph.processor, graph, procedure, support);
+        SessionImplSocket session = (SessionImplSocket)graph.getSession();
+        ClusterSupport support = session.clusterTranslator;
+        final int predicateKey = procedure.predicateKey;
+        if (DEBUG)
+            System.out.println("Cluster.forObjects3: rk=" + resourceKey + " pk=" + predicateKey);
+        final int resourceIndex = getLocalReference(resourceKey);
+        final int pRef = getInternalReferenceOrZero(predicateKey, support);
+        final ClusterI.CompleteTypeEnum pCompleteType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey);
+        if (ClusterI.CompleteTypeEnum.NotComplete != pCompleteType) {
+            resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
+            return;
+        }
+        final int predicateIndex = resourceTable.getPredicateIndex(resourceIndex);
+        if (0 == predicateIndex) {
+            resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
+            return;
+        }
+        int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef);
+        forObjects(resourceKey, predicateKey, objectIndex, graph.processor, graph, procedure, support);
         
     }
     @Override
index 117ab75a3fc723b66660cd48c6db6231b496a3d1..a964c90c98fbbb5bb64842d43563df7d54f76053 100644 (file)
@@ -38,8 +38,8 @@ import org.simantics.db.impl.IClusterTable;
 import org.simantics.db.impl.Table;
 import org.simantics.db.impl.TableHeader;
 import org.simantics.db.impl.graph.ReadGraphImpl;
-import org.simantics.db.procedure.AsyncContextMultiProcedure;
-import org.simantics.db.procedure.AsyncMultiProcedure;
+import org.simantics.db.procedure.SyncContextMultiProcedure;
+import org.simantics.db.procedure.SyncMultiProcedure;
 import org.simantics.db.procore.cluster.ClusterMapSmall;
 import org.simantics.db.procore.cluster.ClusterTraits;
 import org.simantics.db.procore.cluster.ClusterTraitsSmall;
@@ -54,6 +54,7 @@ import org.simantics.db.service.Bytes;
 import org.simantics.db.service.ClusterUID;
 import org.simantics.db.service.ResourceUID;
 
+import fi.vtt.simantics.procore.internal.SessionImplSocket;
 import gnu.trove.map.hash.TIntShortHashMap;
 import gnu.trove.procedure.TIntProcedure;
 import gnu.trove.set.hash.TIntHashSet;
@@ -300,7 +301,7 @@ final public class ClusterSmall extends ClusterImpl {
         return objectTable.getSingleObject(objectIndex, support, this);
     }
 
-    public void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, AsyncMultiProcedure<Resource> procedure,
+    public void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, SyncMultiProcedure<Resource> procedure,
             ClusterSupport support) throws DatabaseException {
         if (DEBUG)
             System.out.println("ClusterSmall.forObjects1: rk=" + resourceKey + " pk=" + predicateKey);
@@ -314,7 +315,7 @@ final public class ClusterSmall extends ClusterImpl {
         objectTable.foreachObject(graph, objectIndex, procedure, this);
     }
 
-    public <C> void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, C context, AsyncContextMultiProcedure<C, Resource> procedure,
+    public <C> void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, C context, SyncContextMultiProcedure<C, Resource> procedure,
             ClusterSupport support) throws DatabaseException {
         if (DEBUG)
             System.out.println("ClusterSmall.forObjects1: rk=" + resourceKey + " pk=" + predicateKey);
@@ -408,29 +409,28 @@ final public class ClusterSmall extends ClusterImpl {
 
     @Override
     public void forObjects(ReadGraphImpl graph, int resourceKey,
-            int predicateKey, AsyncMultiProcedure<Resource> procedure) throws DatabaseException {
+            int predicateKey, SyncMultiProcedure<Resource> procedure) throws DatabaseException {
        
-       throw new UnsupportedOperationException();
-       
-//        SessionImplSocket session = (SessionImplSocket)graph.getSession();
-//        ClusterSupport support = session.clusterTranslator;
-//        if (DEBUG)
-//            System.out.println("ClusterSmall.forObjects3: rk=" + resourceKey + " pk=" + predicateKey);
-//        final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
-//        final int pRef = getInternalReferenceOrZero2(predicateKey, support);
-//        final int completeType = ClusterTraitsBase.getCompleteTypeIntFromResourceKey(predicateKey);
-//        final ClusterI.CompleteTypeEnum pCompleteType = CompleteTypeEnum.make(completeType);
-//        if (completeType > 0) {
-//            resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
-//            return;
-//        }
-//        final int predicateIndex = (int)resourceTable.table[(resourceIndex<<1) - 1 + resourceTable.offset] & 0xFFFFFF;
-//        if (0 == predicateIndex) {
-//            resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
-//            return;
-//        }
-//        int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
-//        forObjects(graph, resourceKey, predicateKey, objectIndex, procedure, support);
+        SessionImplSocket session = (SessionImplSocket)graph.getSession();
+        ClusterSupport support = session.clusterTranslator;
+        if (DEBUG)
+            System.out.println("ClusterSmall.forObjects3: rk=" + resourceKey + " pk=" + predicateKey);
+        final int resourceIndex = ClusterTraitsBase.getResourceIndexFromResourceKey(resourceKey);
+        final int pRef = getInternalReferenceOrZero2(predicateKey, support);
+        final int completeType = ClusterTraitsBase.getCompleteTypeIntFromResourceKey(predicateKey);
+        final ClusterI.CompleteTypeEnum pCompleteType = CompleteTypeEnum.make(completeType);
+        if (completeType > 0) {
+            resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
+            return;
+        }
+        final int predicateIndex = (int)resourceTable.table[(resourceIndex<<1) - 1 + resourceTable.offset] & 0xFFFFFF;
+        if (0 == predicateIndex) {
+            resourceTable.foreachObject(resourceIndex, graph, procedure, support, pRef, pCompleteType, completeTable, this);
+            return;
+        }
+        int objectIndex = predicateTable.getObjectIndex(predicateIndex, pRef & 0xFFFF);
+        forObjects(graph, resourceKey, predicateKey, objectIndex, procedure, support);
+        
     }
 
     public void forObjects(ReadGraphImpl graph, int resourceKey, ForEachObjectProcedure procedure) throws DatabaseException {
index a7635bedfbb676c337870027bcb85550494766e5..639c90391f8c73351cb9c2e86211e7755d5953d0 100644 (file)
@@ -21,6 +21,7 @@ import org.simantics.browsing.ui.PrimitiveQueryUpdater;
 import org.simantics.browsing.ui.common.labelers.LabelerContent;
 import org.simantics.browsing.ui.common.labelers.LabelerStub;
 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
+import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.procedure.Listener;
@@ -92,7 +93,7 @@ public abstract class LazyGraphLabeler extends LabelerStub {
 
         if (content == LabelerContent.NO_CONTENT) {
 
-            final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+            final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
             assert(source != null);
             source.schedule(graph -> graph.asyncRequest(labelQuery, procedure));
 
index 91aeb9c5d4539b5e2e4e189fc703b5f2bdd6cb03..c5d96a90a81fac91b86f556acc421e61130eb153 100644 (file)
@@ -11,8 +11,6 @@
  *******************************************************************************/
 package org.simantics.browsing.ui.graph.impl;
 
-import java.util.function.Consumer;
-
 import org.simantics.browsing.ui.BuiltinKeys;
 import org.simantics.browsing.ui.DataSource;
 import org.simantics.browsing.ui.NodeContext;
@@ -20,6 +18,7 @@ import org.simantics.browsing.ui.PrimitiveQueryUpdater;
 import org.simantics.browsing.ui.common.viewpoints.ViewpointStub;
 import org.simantics.browsing.ui.content.Viewpoint;
 import org.simantics.browsing.ui.graph.impl.request.ParametrizedResourceQuery;
+import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.procedure.Listener;
@@ -135,7 +134,7 @@ public abstract class LazyParametrizedViewpoint extends ViewpointStub {
        @Override
        public NodeContext[] getChildren() {
                if (children == Viewpoint.PENDING_CHILDREN) {
-                       DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+                       DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
                        if (source != null) {
                                source.schedule(graph -> graph.asyncRequest(childQuery, childQueryProcedure));
                        }
@@ -147,17 +146,11 @@ public abstract class LazyParametrizedViewpoint extends ViewpointStub {
        @Override
        public Boolean getHasChildren() {
                if (hasChildren == Viewpoint.PENDING_HAS_CHILDREN) {
-                       DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+                       DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
                        if (source != null) {
-                               source.schedule(new Consumer<ReadGraph>() {
-                                       @Override
-                                       public void accept(ReadGraph source) {
-                                               source.asyncRequest(hasChildQuery, hasChildQueryProcedure);
-                                       }
-                               });
+                               source.schedule(graph -> graph.asyncRequest(hasChildQuery, hasChildQueryProcedure));
                        }
                }
-
                return hasChildren;
        }
 
index c694ba2e88291a52661a178438e60afc2ddc17be..808193a18982d11d672a26d7984dc34f20146e06 100644 (file)
  *******************************************************************************/
 package org.simantics.browsing.ui.graph.impl;
 
-import java.util.function.Consumer;
-
 import org.simantics.browsing.ui.DataSource;
 import org.simantics.browsing.ui.NodeContext;
 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;
 import org.simantics.browsing.ui.PrimitiveQueryProcessor;
 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
+import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.procedure.Listener;
@@ -113,19 +112,10 @@ public abstract class LazyResourceQueryContainer<Result> implements Container<Re
 
         if (!computed) {
 
-            final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+            final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
             assert(source != null);
 
-            source.schedule(new Consumer<ReadGraph>() {
-
-                @Override
-                public void accept(ReadGraph source) {
-
-                    source.asyncRequest(query, procedure);
-
-                }
-
-            });
+            source.schedule(graph -> graph.asyncRequest(query, procedure));
 
         }
 
index a760c848a793d0d87fed012a684087e33930d5cb..a86a172d8911b5f530755ab900941b20e9c8e150 100644 (file)
@@ -11,8 +11,6 @@
  *******************************************************************************/
 package org.simantics.browsing.ui.graph.impl;
 
-import java.util.function.Consumer;
-
 import org.simantics.browsing.ui.BuiltinKeys;
 import org.simantics.browsing.ui.DataSource;
 import org.simantics.browsing.ui.NodeContext;
@@ -20,6 +18,7 @@ import org.simantics.browsing.ui.PrimitiveQueryUpdater;
 import org.simantics.browsing.ui.common.viewpoints.ViewpointStub;
 import org.simantics.browsing.ui.content.Viewpoint;
 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
+import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.procedure.Listener;
@@ -149,14 +148,9 @@ public abstract class LazyViewpoint extends ViewpointStub {
     public NodeContext[] getChildren() {
 
         if (children == Viewpoint.PENDING_CHILDREN) {
-            DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+            DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
             final Listener<NodeContext[]> childProcedure = createListener();
-            source.schedule(new Consumer<ReadGraph>() {
-                @Override
-                public void accept(ReadGraph source) {
-                    source.asyncRequest(childQuery, childProcedure);
-                }
-            });
+            source.schedule(graph -> graph.asyncRequest(childQuery, childProcedure));
         }
 
         return children;
index 68bf689138841314bf6cf043ddfabc7ab63c7ea8..d6813edbbcabb6ee91878378bfa13794713ff07e 100644 (file)
@@ -69,7 +69,7 @@ abstract public class CallbackViewpointContributionImpl extends ContributionStub
         //System.out.println("LazyViewpoint2@" + System.identityHashCode(this) + " getChildren() = " + children.length);
 
         if (children == org.simantics.browsing.ui.content.ViewpointContribution.PENDING_CONTRIBUTION) {
-            DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+            DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
             if (source != null) {
                 source.schedule(graph -> {
                     ReadRequest childQuery = new ReadRequest() {
index 4c28fae2bb460f3b1a9aca3bd36c7be09ea2c8eb..9754fc8065ac7e4a0b547cf2ef829f74d3440d7f 100644 (file)
@@ -18,6 +18,7 @@ import org.simantics.browsing.ui.DataSource;
 import org.simantics.browsing.ui.NodeContext;
 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
+import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.procedure.Listener;
@@ -43,7 +44,7 @@ public abstract class FinalCheckedStateContributionImpl {
        
        if (state == null) {
 
-            final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+            final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
             assert(source != null);
 
             source.schedule(graph -> {
index 6acaffa43963f941dedb5d0d187e36807dc30a66..833db059c8fc79056ada3cd495ac266a5c486a2d 100644 (file)
@@ -17,6 +17,7 @@ import org.simantics.browsing.ui.NodeContext;
 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
 import org.simantics.browsing.ui.content.ImageDecorator;
 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
+import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.exception.PendingVariableException;
@@ -49,7 +50,7 @@ public abstract class FinalImageDecoratorContributionImpl implements ImageDecora
 
     private void request() {
 
-        final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+        final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
         assert(source != null);
 
         final Procedure<ImageDecorator> procedure = createProcedure();
index b4d233e0882289b59862157d11f872871376e52f..833e6ef19a76dede68f037d557d4792a4820f5aa 100644 (file)
@@ -22,6 +22,7 @@ import org.simantics.browsing.ui.NodeContext;
 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
 import org.simantics.browsing.ui.content.Imager;
 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
+import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.exception.PendingVariableException;
@@ -107,7 +108,7 @@ public abstract class FinalImagerContributionImpl implements Imager {
 
             content = PENDING;
 
-            final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+            final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
             assert(source != null);
 
             final Procedure<Map<String, ImageDescriptor>> procedure = createProcedure();
index 3459990009ede231ba7259c4d8535638dd363a5c..fa1945dfff2b41e235922f667bcce31225465058 100644 (file)
@@ -17,6 +17,7 @@ import org.simantics.browsing.ui.NodeContext;
 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
 import org.simantics.browsing.ui.content.LabelDecorator;
 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
+import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.procedure.Listener;
@@ -48,7 +49,7 @@ public abstract class FinalLabelDecoratorContributionImpl extends LabelDecorator
 
     private void request() {
 
-        final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+        final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
         assert(source != null);
         
         final Procedure<LabelDecorator> procedure = createProcedure();
index b283d662e76f92b3b50148ecf6e264585a9adf4b..ed56886944644b453c3d6f07d6c4a7cfc9a3337c 100644 (file)
@@ -25,6 +25,7 @@ import org.simantics.browsing.ui.common.labelers.LabelerContent;
 import org.simantics.browsing.ui.common.labelers.LabelerStub;
 import org.simantics.browsing.ui.common.node.IModifiableNode;
 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
+import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.UndoContext;
 import org.simantics.db.common.utils.Logger;
@@ -115,7 +116,7 @@ public abstract class FinalLabelerContributionImpl extends LabelerStub {
 
         if (content == LabelerContent.NO_CONTENT) {
 
-            final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+            final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
             assert(source != null);
 
             final Procedure<LabelerContent> procedure = createProcedure();
index f514fa48c9aeee4daeda1684e2c1e158f47a35cd..e1dbd4f5234b21a8e12415b86b1591ee9b58cdcf 100644 (file)
@@ -130,7 +130,7 @@ abstract public class FinalViewpointContributionImpl extends ContributionStub im
         //System.out.println("LazyViewpoint2@" + System.identityHashCode(this) + " getChildren() = " + children.length);
 
         if (children == org.simantics.browsing.ui.content.ViewpointContribution.PENDING_CONTRIBUTION) {
-            DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+            DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
             if (source != null) {
                 source.schedule(graph -> {
                     if(childProcedure instanceof Listener<?>)
index a71fd1ba6f91552ef78794dc84faac7a21f183da..6e0a3c0b5a6f126d8bde01fb739290adf844265f 100644 (file)
@@ -24,7 +24,7 @@ final public class ForEachAssertedObject extends ResourceAsyncMultiRead2<Resourc
 
        @Override
        public void perform(AsyncReadGraph graph, AsyncMultiProcedure<Resource> procedure) {
-       graph.forEachAssertedObject(resource, resource2, procedure);
+               graph.forEachAssertedObject(resource, resource2, procedure);
        }
 
 }
index 63a5ddcb3293f2fa34f72e118d123dce341e4cc0..84e6fdc34ea10402c68b27f4ac06f31f8c61c1b0 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  *******************************************************************************/
 package org.simantics.db.common.primitiverequest;
 
-import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
-import org.simantics.db.common.request.ResourceAsyncRead2;
-import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.db.common.request.ResourceRead2;
+import org.simantics.db.exception.DatabaseException;
 
-final public class IsInstanceOf extends ResourceAsyncRead2<Boolean> {
+public final class IsInstanceOf extends ResourceRead2<Boolean> {
 
     public IsInstanceOf(Resource resource, Resource resource2) {
         super(resource, resource2);
     }
 
     @Override
-    public void perform(AsyncReadGraph graph, AsyncProcedure<Boolean> procedure) {
-        graph.forIsInstanceOf(resource, resource2, procedure);
+    public Boolean perform(ReadGraph graph) throws DatabaseException {
+        return graph.isInstanceOf(resource, resource2);
     }
 
 }
index 61a47ecb12a96443620f5c546b29bc2fda50813d..81d0f38958e9217bfbbe4cd7efd6c367f75b8618 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  *******************************************************************************/
 package org.simantics.db.common.primitiverequest;
 
-import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
-import org.simantics.db.procedure.AsyncProcedure;
-import org.simantics.db.request.AsyncRead;
+import org.simantics.db.adaption.AdaptionService;
+import org.simantics.db.common.request.BinaryRead;
+import org.simantics.db.exception.DatabaseException;
 
-final public class PossibleAdapter<T> implements AsyncRead<T> {
+public final class PossibleAdapter<T> extends BinaryRead<Resource,Class<T>, T> {
 
-       final private Resource resource;
-    final private Class<T> clazz;
-    
-    @Override
-    public int hashCode() {
-        return resource.hashCode() + 31 * clazz.hashCode();
-    }
-    
-    @Override
-    public boolean equals(Object object) {
-        if (this == object)
-            return true;
-        else if (object == null)
-            return false;
-        else if (getClass() != object.getClass())
-            return false;
-        PossibleAdapter<?> r = (PossibleAdapter<?>)object;
-        return resource.equals(r.resource) && clazz.equals(r.clazz);
-    }
-
-    @Override
-    public int threadHash() {
-       return hashCode();
-    }
-
-    @Override
-    public int getFlags() {
-        return 0;
-    }
-    
     public PossibleAdapter(Resource resource, Class<T> clazz) {
-        this.resource = resource;
-        this.clazz = clazz;
+        super(resource, clazz);
     }
 
     @Override
-    public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
-        
-        graph.forPossibleAdapted(resource, clazz, procedure);
-        
+    public T perform(ReadGraph graph) throws DatabaseException {
+        final AdaptionService service = graph.peekService(AdaptionService.class);
+        return service.adapt(graph, parameter, parameter, Resource.class, parameter2, true);
     }
 
 }
index 59a66aeaa7fb8ba7f61e50663c2835c68cb3d4db..5fbe501137a108910f5b375534766644e6ab0079 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -15,20 +15,24 @@ import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.RelationInfo;
 import org.simantics.db.Resource;
 import org.simantics.db.common.request.ResourceAsyncRead;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.service.DirectQuerySupport;
 
-final public class RelationInfoRequest extends ResourceAsyncRead<RelationInfo> {
+public final class RelationInfoRequest extends ResourceAsyncRead<RelationInfo> {
 
     public RelationInfoRequest(Resource resource) {
         super(resource);
     }
 
-       @Override
-       public void perform(AsyncReadGraph graph, AsyncProcedure<RelationInfo> procedure) {
-               DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
-               dqs.forRelationInfo(graph, resource, procedure);
-       }
-       
-}
-       
\ No newline at end of file
+    @Override
+    public void perform(AsyncReadGraph graph, AsyncProcedure<RelationInfo> procedure) {
+        DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
+        try {
+            procedure.execute(graph, dqs.getRelationInfo(graph, resource));
+        } catch (DatabaseException e) {
+            procedure.exception(graph, e);
+        }
+    }
+
+}      
\ No newline at end of file
index 0628507d9e6fe7789b9b8cc5dc6cf12c36fad223..19f1ec42bf5597c2c654f786adb900cc47cd43d7 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
 package org.simantics.db.common.procedure.single;
 
 import org.simantics.db.ReadGraph;
-import org.simantics.db.common.procedure.adapter.AsyncListenerSupport;
+import org.simantics.db.common.procedure.adapter.SyncListenerSupport;
 
-abstract public class SingleSetSyncListenerDelegate<T> extends SingleSetSyncListener<T> {
+public abstract class SingleSetSyncListenerDelegate<T> extends SingleSetSyncListener<T> {
     
-    final private AsyncListenerSupport support;
+    private final SyncListenerSupport support;
     
-    public SingleSetSyncListenerDelegate(AsyncListenerSupport support) {
+    public SingleSetSyncListenerDelegate(SyncListenerSupport support) {
         this.support = support;
     }
 
diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/wrapper/NoneToSyncMultiListener.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/wrapper/NoneToSyncMultiListener.java
new file mode 100644 (file)
index 0000000..9e3178e
--- /dev/null
@@ -0,0 +1,51 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.common.procedure.wrapper;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.procedure.MultiListener;
+import org.simantics.db.procedure.SyncMultiListener;
+
+public class NoneToSyncMultiListener<T> implements SyncMultiListener<T> {
+
+    private final MultiListener<T> procedure;
+
+    public NoneToSyncMultiListener(MultiListener<T> procedure) {
+        this.procedure = procedure;
+    }
+
+    @Override
+    public void execute(ReadGraph graph, final T result) {
+        procedure.execute(result);
+    }
+
+    @Override
+    public void finished(ReadGraph graph) {
+        procedure.finished();
+    }
+
+    @Override
+    public void exception(ReadGraph graph, Throwable t) {
+        procedure.exception(t);
+    }
+
+    @Override
+    public boolean isDisposed() {
+        return procedure.isDisposed();
+    }
+
+    @Override
+    public String toString() {
+        return "NoneToAsyncMultiListener -> " + procedure;
+    }
+
+}
diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/wrapper/NoneToSyncMultiProcedure.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/wrapper/NoneToSyncMultiProcedure.java
new file mode 100644 (file)
index 0000000..7531f97
--- /dev/null
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.common.procedure.wrapper;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.procedure.MultiProcedure;
+import org.simantics.db.procedure.SyncMultiProcedure;
+
+public class NoneToSyncMultiProcedure<T> implements SyncMultiProcedure<T> {
+
+    private final MultiProcedure<T> procedure;
+
+    public NoneToSyncMultiProcedure(MultiProcedure<T> procedure) {
+        this.procedure = procedure;
+    }
+
+    @Override
+    public void execute(ReadGraph graph, final T result) {
+        procedure.execute(result);
+    }
+
+    @Override
+    public void finished(ReadGraph graph) {
+        procedure.finished();
+    }
+
+    @Override
+    public void exception(ReadGraph graph, Throwable t) {
+         procedure.exception(t);
+    }
+
+    @Override
+    public String toString() {
+        return "NoneToAsyncMultiProcedure -> " + procedure;
+    }
+
+}
index bf72e041648e4d11c4e0e4b1de8506b8e8397cf2..e9523c0c899628ee03fb29a4130067131044708a 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -24,12 +24,12 @@ import java.util.function.Consumer;
 import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.AsyncRequestProcessor;
 import org.simantics.db.ReadGraph;
-import org.simantics.db.RequestProcessor;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
+import org.simantics.db.common.procedure.adapter.SyncMultiProcedureAdapter;
 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
 import org.simantics.db.common.request.ReadRequest;
 import org.simantics.db.common.request.WriteRequest;
@@ -64,7 +64,7 @@ import org.simantics.db.request.WriteResult;
 import org.simantics.utils.DataContainer;
 import org.simantics.utils.datastructures.Pair;
 
-public class MergingGraphRequestProcessor implements RequestProcessor {
+public class MergingGraphRequestProcessor implements AsyncRequestProcessor {
 
     private static class SyncWriteRequestAdapter implements Write {
 
@@ -264,11 +264,11 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
 
                                Logger.defaultLogError(t);
 
-                        if(currentRequest.second instanceof AsyncProcedure<?>) {
-                            ((AsyncProcedure<?>)currentRequest.second).exception(graph, t);
-                        } else {
-                            ((AsyncMultiProcedure<?>)currentRequest.second).exception(graph, t);
-                        }
+//                        if(currentRequest.second instanceof AsyncProcedure<?>) {
+//                            ((AsyncProcedure<?>)currentRequest.second).exception(graph, t);
+//                        } else {
+//                            ((AsyncMultiProcedure<?>)currentRequest.second).exception(graph, t);
+//                        }
 
                     }
 
@@ -286,25 +286,25 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
                         if(currentRequest.second instanceof AsyncProcedure<?>) {
                             if(currentRequest.first instanceof AsyncRead) {
                                 AsyncRead req = (AsyncRead)currentRequest.first;
-                                graph.asyncRequest(req, (AsyncProcedure<?>)currentRequest.second);
+                                graph.syncRequest(req, (AsyncProcedure<?>)currentRequest.second);
                             } else {
                                 Read req = (Read)currentRequest.first;
-                                graph.asyncRequest(req, (AsyncProcedure<?>)currentRequest.second);
+                                graph.syncRequest(req, (AsyncProcedure<?>)currentRequest.second);
                             }
                         } else {
                             AsyncMultiRead req = (AsyncMultiRead)currentRequest.first;
-                            graph.asyncRequest(req, (AsyncMultiProcedure<?>)currentRequest.second);
+                            graph.syncRequest(req, (AsyncMultiProcedure<?>)currentRequest.second);
                         }
 
                     } catch(Throwable t) {
 
                                Logger.defaultLogError(t);
 
-                        if(currentRequest.second instanceof AsyncProcedure<?>) {
-                            ((AsyncProcedure<?>)currentRequest.second).exception(graph, t);
-                        } else {
-                            ((AsyncMultiProcedure<?>)currentRequest.second).exception(graph, t);
-                        }
+//                        if(currentRequest.second instanceof AsyncProcedure<?>) {
+//                            ((AsyncProcedure<?>)currentRequest.second).exception(graph, t);
+//                        } else {
+//                            ((AsyncMultiProcedure<?>)currentRequest.second).exception(graph, t);
+//                        }
 
                     }
                 }
@@ -443,10 +443,10 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
 
 //        System.out.println(this + " asyncRequest(ReadGraphRequest<QueryProcedure4<T>> request, QueryProcedure4<T> procedure)");
 
-        if (requestSet.contains(request))
+        Pair<Object, Object> pair = Pair.make(request, procedure);
+        if (requestSet.contains(pair))
             return;
 
-        Pair<Object, Object> pair = new Pair<Object, Object>(request, procedure);
         requestQueue.add(pair);
         requestSet.add(pair);
 
@@ -464,10 +464,10 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
 
 //        System.out.println(this + " asyncRequest(ReadGraphRequest<SingleQueryProcedure4<T>> request, SingleQueryProcedure4<T> procedure) " + this);
 
-        if (requestSet.contains(request))
+        Pair<Object, Object> pair = Pair.make(request, procedure);
+        if (requestSet.contains(pair))
             return;
 
-        Pair<Object, Object> pair = new Pair<Object, Object>(request, procedure);
         requestQueue.add(pair);
         requestSet.add(pair);
 
@@ -486,10 +486,10 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
 
 //        System.out.println(this + " asyncRequest(WriteGraphRequest request)");
 
-        if (requestSet.contains(request))
+        Pair<Object, Object> pair = Pair.make(request, callback);
+        if (requestSet.contains(pair))
             return;
 
-        Pair<Object, Object> pair = new Pair<Object, Object>(request, callback);
         requestQueue.add(pair);
         requestSet.add(pair);
 
@@ -509,10 +509,10 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
 
 //        System.out.println(this + " asyncRequest(WriteGraphRequest request)");
 
-        if (requestSet.contains(request))
+        Pair<Object, Object> pair = Pair.make(request, callback);
+        if (requestSet.contains(pair))
             return;
 
-        Pair<Object, Object> pair = new Pair<Object, Object>(request, callback);
         requestQueue.add(pair);
         requestSet.add(pair);
 
@@ -532,10 +532,10 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
 
 //        System.out.println(this + " asyncRequest(WriteGraphRequest request)");
 
-        if (requestSet.contains(request))
+        Pair<Object, Object> pair = Pair.make(request, callback);
+        if (requestSet.contains(pair))
             return;
 
-        Pair<Object, Object> pair = new Pair<Object, Object>(request, callback);
         requestQueue.add(pair);
         requestSet.add(pair);
 
@@ -889,10 +889,10 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
     public synchronized <T> void asyncRequest(Read<T> request,
             AsyncProcedure<T> procedure) {
 
-        if (requestSet.contains(request))
+        Pair<Object, Object> pair = Pair.make(request, procedure);
+        if (requestSet.contains(pair))
             return;
 
-        Pair<Object, Object> pair = new Pair<Object, Object>(request, procedure);
         requestQueue.add(pair);
         requestSet.add(pair);
 
@@ -917,17 +917,17 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
         final ArrayList<T> result = new ArrayList<T>();
         final DataContainer<Throwable> exception = new DataContainer<Throwable>();
 
-        syncRequest(request, new AsyncMultiProcedureAdapter<T>() {
+        syncRequest(request, new SyncMultiProcedureAdapter<T>() {
 
             @Override
-            public void execute(AsyncReadGraph graph, T t) {
+            public void execute(ReadGraph graph, T t) {
                 synchronized(result) {
                     result.add(t);
                 }
             }
 
             @Override
-            public void exception(AsyncReadGraph graph, Throwable t) {
+            public void exception(ReadGraph graph, Throwable t) {
                 exception.set(t);
             }
 
@@ -947,11 +947,6 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
         return result;
     }
 
-    @Override
-    public <T> Collection<T> syncRequest(MultiRead<T> request, AsyncMultiProcedure<T> procedure)  {
-        throw new UnsupportedOperationException("Not implemented");
-    }
-
     @Override
     public <T> Collection<T> syncRequest(MultiRead<T> request, MultiProcedure<T> procedure) {
         throw new UnsupportedOperationException("Not implemented");
@@ -967,11 +962,6 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
         throw new UnsupportedOperationException("Not implemented");
     }
 
-    @Override
-    public <T> void asyncRequest(MultiRead<T> request, AsyncMultiProcedure<T> procedure) {
-        throw new UnsupportedOperationException("Not implemented");
-    }
-
     @Override
     public <T> void asyncRequest(MultiRead<T> request, MultiProcedure<T> procedure) {
         throw new UnsupportedOperationException("Not implemented");
@@ -1103,12 +1093,6 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
 
     }
 
-    @Override
-    public <T> Collection<T> syncRequest(MultiRead<T> arg0, AsyncMultiListener<T> arg1) {
-        throw new UnsupportedOperationException("Not implemented.");
-
-    }
-
     @Override
     public <T> Collection<T> syncRequest(MultiRead<T> arg0, SyncMultiListener<T> arg1) {
         throw new UnsupportedOperationException("Not implemented.");
@@ -1153,12 +1137,6 @@ public class MergingGraphRequestProcessor implements RequestProcessor {
 
     }
 
-    @Override
-    public <T> void asyncRequest(MultiRead<T> arg0, AsyncMultiListener<T> arg1) {
-        throw new UnsupportedOperationException("Not implemented.");
-
-    }
-
     @Override
     public <T> void asyncRequest(MultiRead<T> arg0, SyncMultiListener<T> arg1) {
         throw new UnsupportedOperationException("Not implemented.");
index 6e1aeabaae4d3b47a8a3624e73c2e64a366c8854..1f00ff008674e73ada990c71e297e9f01afe2f19 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -14,7 +14,7 @@ package org.simantics.db.common.processor;
 import java.util.Collection;
 import java.util.function.Consumer;
 
-import org.simantics.db.RequestProcessor;
+import org.simantics.db.AsyncRequestProcessor;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.exception.DatabaseException;
@@ -44,7 +44,7 @@ import org.simantics.db.request.WriteOnly;
 import org.simantics.db.request.WriteOnlyResult;
 import org.simantics.db.request.WriteResult;
 
-public class ProcessorBase implements RequestProcessor {
+public class ProcessorBase implements AsyncRequestProcessor {
 
     @Override
     public <T> void asyncRequest(AsyncMultiRead<T> request, AsyncMultiProcedure<T> procedure) {
@@ -173,11 +173,6 @@ public class ProcessorBase implements RequestProcessor {
         throw new UnsupportedOperationException();
     }
 
-    @Override
-    public <T> Collection<T> syncRequest(MultiRead<T> request, AsyncMultiProcedure<T> procedure)  {
-        throw new UnsupportedOperationException();
-    }
-
     @Override
     public <T> Collection<T> syncRequest(MultiRead<T> request, MultiProcedure<T> procedure) {
         throw new UnsupportedOperationException();
@@ -193,11 +188,6 @@ public class ProcessorBase implements RequestProcessor {
         throw new UnsupportedOperationException();
     }
 
-    @Override
-    public <T> void asyncRequest(MultiRead<T> request, AsyncMultiProcedure<T> procedure) {
-        throw new UnsupportedOperationException();
-    }
-
     @Override
     public <T> void asyncRequest(MultiRead<T> request, MultiProcedure<T> procedure) {
         throw new UnsupportedOperationException();
@@ -313,11 +303,6 @@ public class ProcessorBase implements RequestProcessor {
         throw new UnsupportedOperationException();
     }
 
-    @Override
-    public <T> Collection<T> syncRequest(MultiRead<T> arg0, AsyncMultiListener<T> arg1) {
-        throw new UnsupportedOperationException();
-    }
-
     @Override
     public <T> Collection<T> syncRequest(MultiRead<T> arg0, SyncMultiListener<T> arg1) {
         throw new UnsupportedOperationException();
@@ -355,11 +340,6 @@ public class ProcessorBase implements RequestProcessor {
         throw new UnsupportedOperationException();
     }
 
-    @Override
-    public <T> void asyncRequest(MultiRead<T> arg0, AsyncMultiListener<T> arg1) {
-        throw new UnsupportedOperationException();
-    }
-
     @Override
     public <T> void asyncRequest(MultiRead<T> arg0, SyncMultiListener<T> arg1) {
         throw new UnsupportedOperationException();
index 668791821c7320927fb50c3e785537bf70422783..4a6d0b19169e86f8e384fd2e430a843acbd20f95 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  *******************************************************************************/
 package org.simantics.db.common.request;
 
-import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
-import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.layer0.Layer0;
 
-public class PossibleTypedParent extends ResourceAsyncRead2<Resource> {
+public class PossibleTypedParent extends ResourceRead2<Resource> {
 
        public PossibleTypedParent(Resource resource, Resource type) {
                super(resource, type);
        }
 
        @Override
-       public void perform(AsyncReadGraph graph, final AsyncProcedure<Resource> procedure) {
-
-           final Layer0 l0 = graph.getService(Layer0.class);
-
-               graph.forIsInstanceOf(resource, resource2, new AsyncProcedure<Boolean>() {
-
-                       @Override
-                       public void execute(AsyncReadGraph graph, Boolean isInstance) {
-                               if(isInstance) {
-                                       procedure.execute(graph, resource);
-                               } else {
-                                       
-                                       graph.forPossibleObject(resource, l0.PartOf, new AsyncProcedure<Resource>() {
-
-                                               @Override
-                                               public void execute(AsyncReadGraph graph, final Resource parent) {
-
-                                                       if(parent == null) {
-                                                               procedure.execute(graph, null);
-                                                       } else {
-                                                               graph.asyncRequest(new PossibleTypedParent(parent, resource2), procedure);
-                                                       }
-                                                       
-                                               }
-
-                                               @Override
-                                               public void exception(AsyncReadGraph graph, Throwable throwable) {
-                                                       procedure.exception(graph, throwable);
-                                               }
-                                               
-                                       });
-                                       
-                               }
-                       }
-
-                       @Override
-                       public void exception(AsyncReadGraph graph, Throwable throwable) {
-                               procedure.exception(graph, throwable);
+       public Resource perform(ReadGraph graph) throws DatabaseException {
+               if (graph.isInstanceOf(resource, resource2)) {
+                       return resource;
+               } else {
+                       Layer0 L0 = Layer0.getInstance(graph);
+                       Resource possibleParent = graph.getPossibleObject(resource, L0.PartOf);
+                       if (possibleParent != null) {
+                               return graph.syncRequest(new PossibleTypedParent(possibleParent, resource2));
+                       } else {
+                               return null;
                        }
-               });
-               
-               
+               }
        }
 
 }
index 4e51a09973c47a946e14ebc127fbb912c480bd30..4d15bd5f3398cbda58f2c82537c39ef9dae8081f 100644 (file)
@@ -22,11 +22,6 @@ public abstract class ResourceAsyncMultiRead<T> implements AsyncMultiRead<T> {
     public int hashCode() {
         return resource.hashCode();
     }
-    
-    @Override
-    final public int threadHash() {
-       return resource.getThreadHash();
-    }
 
     @Override
     public boolean equals(Object object) {
index ae40ad4acc90db9e8f6240ab5edc09d675a92f77..1b13285d86ef6fd3b7872ab7bdf1cec8cea2231d 100644 (file)
@@ -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<T> implements AsyncRead<T>, Request, ReadInterface<T> {
+public abstract class ResourceAsyncRead2<T> implements AsyncRead<T>, ReadInterface<T> {
 
     final protected Resource resource;
     final protected Resource resource2;
index f9517f7bbe0c495ba6e6ab9b32b5c2b1d6b3e8fe..7713535b50ddcf5b4366070ed644ddc5185875c6 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  *******************************************************************************/
 package org.simantics.db.common.uri;
 
-import java.util.Collection;
 import java.util.Map;
 
-import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.request.ResourceRead;
 import org.simantics.db.exception.DatabaseException;
-import org.simantics.db.service.CollectionSupport;
-import org.simantics.layer0.Layer0;
-import org.simantics.utils.Development;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public class UnescapedChildMapOfResource extends ResourceRead<Map<String, Resource>> {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(UnescapedChildMapOfResource.class);
-
        public UnescapedChildMapOfResource(Resource resource) {
-           super(resource);
+               super(resource);
        }
-       
+
        @Override
        public Map<String, Resource> perform(ReadGraph graph) throws DatabaseException {
-           Layer0 L0 = Layer0.getInstance(graph);
-           Collection<Resource> objects = graph.getObjects(resource, L0.ConsistsOf);
-           CollectionSupport cs = graph.getService(CollectionSupport.class);
-           Map<String,Resource> result = cs.createObjectResourceMap(String.class, objects.size());
-           for(Resource r : objects) {
-               String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING);
-               if(name != null) {
-                   Resource old = result.put(name, r);
-                   if (old != null)
-                       LOGGER.error("The database contains siblings with the same name " + name + " (resource=$" + resource.getResourceId() + ", child=$" + r.getResourceId() + ", previous child=$" + old.getResourceId() + ").");
-               } else {
-                       if(Development.DEVELOPMENT)
-                           LOGGER.error("The database contains a child with no unique name (resource=$" + resource.getResourceId() + ", child=$" + r.getResourceId() + ").");
-               }
-           }
-           return result;
-       }       
-       
+               return graph.getChildren(resource);
+       }
+
 }
index 2e16b284903997a204f1c229f3a1adc83df10a96..77eacf2055e9002c86aa99da5fed5c96a3f15906 100644 (file)
@@ -234,12 +234,10 @@ public class CommonDBUtils {
                DirectStatementProcedure proc = new DirectStatementProcedure();
 
                if (ignoreVirtual) {
-                       dqs.forEachDirectPersistentStatement(graph, resource, proc);
+                       return dqs.getDirectPersistentStatements(graph, resource);
                } else {
-                       dqs.forEachDirectStatement(graph, resource, proc);
+                       return dqs.getDirectStatements(graph, resource);
                }
-               
-               return proc.getOrThrow();
 
        }
        
index eb0ca24b6a5fa32e318936dfbfe54ce5a8a42f79..143d3e443099f31c703503dfea77b1da7ea115f1 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -25,7 +25,6 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.WriteOnlyGraph;
-import org.simantics.db.common.request.ReadRequest;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.RuntimeDatabaseException;
 import org.simantics.db.exception.ValidationException;
@@ -235,21 +234,6 @@ public class OrderedSetUtils {
         }
     }
 
-    /**
-     * Converts ordered set into a list.
-     */
-    public static void forEach(AsyncReadGraph g, final Resource l, final AsyncMultiProcedure<Resource> procedure) {
-        g.asyncRequest(new ReadRequest() {
-
-            @Override
-            public void run(ReadGraph graph) throws DatabaseException {
-                for(Resource r : toList(graph, l))
-                    procedure.execute(graph, r);
-            }
-
-        });
-    }
-
     /**
      * Creates an empty ordered set.
      */
index 9e5d563a51205fd2855400458fac64bf69111ff5..96d915482e53b91efb9f5e536d7d0e0d480a3868 100644 (file)
@@ -18,6 +18,7 @@ import java.util.function.Consumer;
 import org.simantics.databoard.accessor.Accessor;
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.type.Datatype;
+import org.simantics.db.AsyncRequestProcessor;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.RequestProcessor;
 import org.simantics.db.Resource;
@@ -150,11 +151,11 @@ public class Transaction {
                }
        }
        
-       public static void startTransaction(RequestProcessor processor, boolean write) throws DatabaseException {
+       public static void startTransaction(AsyncRequestProcessor processor, boolean write) throws DatabaseException {
                startTransaction(processor, write ? Type.WRITE : Type.READ);
        }
        
-       public static void startTransaction(RequestProcessor processor, Type type) throws DatabaseException {
+       public static void startTransaction(AsyncRequestProcessor processor, Type type) throws DatabaseException {
                switch (type) {
                        case READ:
                        {
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncMultiProcedure.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncMultiProcedure.java
new file mode 100644 (file)
index 0000000..cc50ea9
--- /dev/null
@@ -0,0 +1,95 @@
+/*******************************************************************************
+ * Copyright (c) 2018 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.impl;
+
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.procedure.AsyncMultiProcedure;
+
+public class BlockingAsyncMultiProcedure<Result> implements AsyncMultiProcedure<Result> {
+
+    private static final Object NO_RESULT = new Object();
+
+    private final Object key;
+    private final ReadGraphImpl graph;
+    private final AsyncMultiProcedure<Result> procedure;
+
+    private Object result = NO_RESULT;
+    private Throwable exception = null;
+
+    public BlockingAsyncMultiProcedure(ReadGraphImpl graph, AsyncMultiProcedure<Result> procedure, Object key) {
+        this.procedure = procedure;
+        this.key = key;
+        this.graph = ReadGraphImpl.newAsync(graph);
+        this.graph.asyncBarrier.inc();
+    }
+
+    @Override
+    public void execute(AsyncReadGraph graph, Result result) {
+        this.result = result;
+        try {
+            if(procedure != null) procedure.execute(graph, result);
+        } catch (Throwable throwable) {
+            Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
+        }
+    }
+
+    @Override
+    public void finished(AsyncReadGraph graph) {
+        this.graph.asyncBarrier.dec();
+        try {
+            if(procedure != null) procedure.finished(graph);
+        } catch (Throwable throwable) {
+            Logger.defaultLogError("AsyncProcedure.finish threw for " + procedure, throwable);
+        }
+    }
+
+    @Override
+    public void exception(AsyncReadGraph graph, Throwable t) {
+        this.exception = t;
+        try {
+            if (procedure != null) procedure.exception(graph, t);
+        } catch (Throwable throwable) {
+            Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
+        } finally {
+            this.graph.asyncBarrier.dec();
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    public Result get() throws DatabaseException {
+        graph.asyncBarrier.waitBarrier(key, graph);
+        if (exception != null) {
+            if (exception instanceof DatabaseException) throw (DatabaseException) exception;
+            throw new DatabaseException(exception);
+        } else {
+            return (Result) result;
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    public Result getResult() {
+        return (Result) result;
+    }
+
+    public Throwable getException() {
+        return exception;
+    }
+
+    @Override
+    public String toString() {
+        return "." + procedure; 
+    }
+
+}
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncProcedure.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncProcedure.java
new file mode 100644 (file)
index 0000000..4ae5303
--- /dev/null
@@ -0,0 +1,89 @@
+/*******************************************************************************
+ * Copyright (c) 2018 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.impl;
+
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.procedure.AsyncProcedure;
+
+public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
+
+    private static final Object NO_RESULT = new Object();
+
+    private final Object key;
+    private final ReadGraphImpl graph;
+    private final AsyncProcedure<Result> procedure;
+
+    private Object result = NO_RESULT;
+    private Throwable exception = null;
+
+    public BlockingAsyncProcedure(ReadGraphImpl graph, AsyncProcedure<Result> procedure, Object key) {
+        this.procedure = procedure;
+        this.key = key;
+        this.graph = ReadGraphImpl.newAsync(graph);
+        this.graph.asyncBarrier.inc();
+    }
+
+    @Override
+    public void execute(AsyncReadGraph graph, Result result) {
+        this.result = result;
+        this.graph.asyncBarrier.dec();
+        try {
+            if(procedure != null) procedure.execute(graph, result);
+        } catch (Throwable throwable) {
+            Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
+        }
+    }
+
+    @Override
+    public void exception(AsyncReadGraph graph, Throwable t) {
+        this.exception = t;
+        try {
+            if(procedure != null) procedure.exception(graph, t);
+        } catch (Throwable throwable) {
+            Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
+        } finally {
+            this.graph.asyncBarrier.dec();
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    public Result get() throws DatabaseException {
+
+        graph.asyncBarrier.waitBarrier(key, graph);
+
+        if(exception != null) {
+            if(exception instanceof DatabaseException) throw (DatabaseException)exception;
+            throw new DatabaseException(exception);
+        } else {
+            return (Result)result;
+        }
+
+    }
+
+    @SuppressWarnings("unchecked")
+    public Result getResult() {
+        return (Result)result;
+    }
+
+    public Throwable getException() {
+        return exception;
+    }
+
+    @Override
+    public String toString() {
+        return "." + procedure; 
+    }
+
+}
index 87acea43d4801c388154f5738b08f0b2428ed55d..a82a7490f3e1f506051b794c7cc17d3bf91fa5a1 100644 (file)
@@ -17,7 +17,7 @@ import java.util.function.Consumer;
 import org.simantics.db.Resource;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
-import org.simantics.db.procedure.AsyncMultiProcedure;
+import org.simantics.db.procedure.SyncMultiProcedure;
 import org.simantics.db.service.ClusterUID;
 
 public interface ClusterI {
@@ -114,7 +114,7 @@ public interface ClusterI {
     public <C, T> int getSingleObject(int resourceKey, ForPossibleRelatedValueContextProcedure<C, T> procedure, ClusterSupport support)
     throws DatabaseException;
 
-    public void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, AsyncMultiProcedure<Resource> procedure)
+    public void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, SyncMultiProcedure<Resource> procedure)
     throws DatabaseException;
 
     public void forObjects(ReadGraphImpl graph, int resourceKey, ForEachObjectProcedure procedure)
index 188917994a26881ccafe58a313ac6cb4559c2a67..6bec3b7074b540f34f5970cd20bd73cbc113892f 100644 (file)
@@ -1,12 +1,13 @@
 package org.simantics.db.impl;
 
-import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
 import org.simantics.db.RelationInfo;
 import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.query.QueryProcessor;
-import org.simantics.db.procedure.AsyncContextMultiProcedure;
+import org.simantics.db.procedure.SyncContextMultiProcedure;
 
-public final class ForEachObjectContextProcedure<C> implements AsyncContextMultiProcedure<C, Resource> {
+public final class ForEachObjectContextProcedure<C> implements SyncContextMultiProcedure<C, Resource> {
 
        public final int predicateKey;
        public final int[] clusterKey;
@@ -14,9 +15,9 @@ public final class ForEachObjectContextProcedure<C> implements AsyncContextMulti
        public final RelationInfo info;
        public final QueryProcessor processor;
        public final ClusterI.CompleteTypeEnum completeType;
-       private final AsyncContextMultiProcedure<C, Resource> user;
+       private final SyncContextMultiProcedure<C, Resource> user;
        
-       public ForEachObjectContextProcedure(int predicateKey, RelationInfo info, QueryProcessor processor, AsyncContextMultiProcedure<C, Resource> user) {
+       public ForEachObjectContextProcedure(int predicateKey, RelationInfo info, QueryProcessor processor, SyncContextMultiProcedure<C, Resource> user) {
                this.predicateKey = predicateKey;
                this.completeType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey); 
                this.info = info;
@@ -27,17 +28,17 @@ public final class ForEachObjectContextProcedure<C> implements AsyncContextMulti
        }
        
        @Override
-       public void execute(AsyncReadGraph graph, C context, Resource result) {
+       public void execute(ReadGraph graph, C context, Resource result) throws DatabaseException {
                user.execute(graph, context, result);
        }
 
        @Override
-       public void finished(AsyncReadGraph graph, C context) {
+       public void finished(ReadGraph graph, C context) throws DatabaseException {
                user.finished(graph, context);
        }
 
        @Override
-       public void exception(AsyncReadGraph graph, Throwable throwable) {
+       public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
                user.exception(graph, throwable);
        }
        
index 85790c4de02cbcce7b5b2d4b209a43ae6782625b..143d0a1868dc8b52be43ae91fff3b969b61a063b 100644 (file)
@@ -1,12 +1,13 @@
 package org.simantics.db.impl;
 
-import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
 import org.simantics.db.RelationInfo;
 import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.query.QueryProcessor;
-import org.simantics.db.procedure.AsyncMultiProcedure;
+import org.simantics.db.procedure.SyncMultiProcedure;
 
-public final class ForEachObjectProcedure implements AsyncMultiProcedure<Resource> {
+public final class ForEachObjectProcedure implements SyncMultiProcedure<Resource> {
 
        public final int predicateKey;
        public final int[] clusterKey;
@@ -14,9 +15,9 @@ public final class ForEachObjectProcedure implements AsyncMultiProcedure<Resourc
        public final RelationInfo info;
        public final QueryProcessor processor;
        public final ClusterI.CompleteTypeEnum completeType;
-       private final AsyncMultiProcedure<Resource> user;
+       private final SyncMultiProcedure<Resource> user;
        
-       public ForEachObjectProcedure(int predicateKey, RelationInfo info, QueryProcessor processor, AsyncMultiProcedure<Resource> user) {
+       public ForEachObjectProcedure(int predicateKey, RelationInfo info, QueryProcessor processor, SyncMultiProcedure<Resource> user) {
                this.predicateKey = predicateKey;
                this.completeType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey); 
                this.info = info;
@@ -27,17 +28,17 @@ public final class ForEachObjectProcedure implements AsyncMultiProcedure<Resourc
        }
        
        @Override
-       public void execute(AsyncReadGraph graph, Resource result) {
+       public void execute(ReadGraph graph, Resource result) throws DatabaseException {
                user.execute(graph, result);
        }
 
        @Override
-       public void finished(AsyncReadGraph graph) {
+       public void finished(ReadGraph graph) throws DatabaseException {
                user.finished(graph);
        }
 
        @Override
-       public void exception(AsyncReadGraph graph, Throwable throwable) {
+       public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
                user.exception(graph, throwable);
        }
        
index 0225656d5b9f3a7a584ea5811a65a8df6c4be0a9..d16853cdcbf1c380e7395201bef87a548eeee545 100644 (file)
@@ -1,19 +1,19 @@
 package org.simantics.db.impl;
 
-import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
 import org.simantics.db.RelationInfo;
-import org.simantics.db.procedure.AsyncContextProcedure;
+import org.simantics.db.procedure.SyncContextProcedure;
 
-public final class ForPossibleRelatedValueContextProcedure<C, T> implements AsyncContextProcedure<C, T> {
+public final class ForPossibleRelatedValueContextProcedure<C, T> implements SyncContextProcedure<C, T> {
 
        public final int predicateKey;
        public final int[] clusterKey;
        public final int[] predicateReference;
        public final RelationInfo info;
        public final ClusterI.CompleteTypeEnum completeType;
-       private final AsyncContextProcedure<C, T> user;
+       private final SyncContextProcedure<C, T> user;
        
-       public ForPossibleRelatedValueContextProcedure(int predicateKey, RelationInfo info, AsyncContextProcedure<C, T> user) {
+       public ForPossibleRelatedValueContextProcedure(int predicateKey, RelationInfo info, SyncContextProcedure<C, T> user) {
                this.predicateKey = predicateKey;
                this.completeType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey); 
                this.info = info;
@@ -23,12 +23,12 @@ public final class ForPossibleRelatedValueContextProcedure<C, T> implements Asyn
        }
        
        @Override
-       public void execute(AsyncReadGraph graph, C context, T result) {
+       public void execute(ReadGraph graph, C context, T result) {
                user.execute(graph, context, result);
        }
 
        @Override
-       public void exception(AsyncReadGraph graph, Throwable throwable) {
+       public void exception(ReadGraph graph, Throwable throwable) {
                user.exception(graph, throwable);
        }
        
index 6dcb0ea104b30da2fb7442b1ab31495f47029267..bd5e0ccaaac3f81716731f5035f2c8d6ebebbc45 100644 (file)
@@ -1,19 +1,20 @@
 package org.simantics.db.impl;
 
-import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
 import org.simantics.db.RelationInfo;
-import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.SyncProcedure;
 
-public final class ForPossibleRelatedValueProcedure<T> implements AsyncProcedure<T> {
+public final class ForPossibleRelatedValueProcedure<T> implements SyncProcedure<T> {
 
        public final int predicateKey;
        public final int[] clusterKey;
        public final int[] predicateReference;
        public final RelationInfo info;
        public final ClusterI.CompleteTypeEnum completeType;
-       private final AsyncProcedure<T> user;
+       private final SyncProcedure<T> user;
        
-       public ForPossibleRelatedValueProcedure(int predicateKey, RelationInfo info, AsyncProcedure<T> user) {
+       public ForPossibleRelatedValueProcedure(int predicateKey, RelationInfo info, SyncProcedure<T> user) {
                this.predicateKey = predicateKey;
                this.completeType = ClusterTraitsBase.getCompleteTypeFromResourceKey(predicateKey); 
                this.info = info;
@@ -23,12 +24,12 @@ public final class ForPossibleRelatedValueProcedure<T> implements AsyncProcedure
        }
        
        @Override
-       public void execute(AsyncReadGraph graph, T result) {
+       public void execute(ReadGraph graph, T result) throws DatabaseException {
                user.execute(graph, result);
        }
 
        @Override
-       public void exception(AsyncReadGraph graph, Throwable throwable) {
+       public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
                user.exception(graph, throwable);
        }
        
index f4e722fe5adea864496e4a7864197ae7fb6e877d..f48a64e58f754d004ea881cdc9eec8516d40f965 100644 (file)
@@ -28,7 +28,6 @@ import org.simantics.databoard.serialization.Serializer;
 import org.simantics.databoard.serialization.SerializerConstructionException;
 import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.AsyncRequestProcessor;
-import org.simantics.db.RequestProcessor;
 import org.simantics.db.Resource;
 import org.simantics.db.Statement;
 import org.simantics.db.VirtualGraphContext;
@@ -423,7 +422,7 @@ public class TransientGraph implements VirtualGraphImpl, VirtualGraphContext {
     final private SerialisationSupport serialization;
     final private ResourceSupport resourceSupport;
     final private VirtualGraphServerSupport virtualGraphServerSupport;
-    final private RequestProcessor sessionRequestProcessor;
+    final private AsyncRequestProcessor sessionRequestProcessor;
     
     /*
      * Cluster array by index.
@@ -446,17 +445,17 @@ public class TransientGraph implements VirtualGraphImpl, VirtualGraphContext {
 
     int[] EMPTY = new int[0];
 
-    public static TransientGraph workspacePersistent(SerialisationSupport ss, VirtualGraphServerSupport vgss, ResourceSupport rs, RequestProcessor srp, String databaseId, String identifier) throws DatabaseException {
+    public static TransientGraph workspacePersistent(SerialisationSupport ss, VirtualGraphServerSupport vgss, ResourceSupport rs, AsyncRequestProcessor srp, String databaseId, String identifier) throws DatabaseException {
         TransientGraph graph = new TransientGraph(ss, vgss, rs, srp, databaseId, identifier, Persistency.WORKSPACE);
         graph.load();
         return graph;
     }
     
-    public static TransientGraph memoryPersistent(SerialisationSupport ss, VirtualGraphServerSupport vgss, ResourceSupport rs, RequestProcessor srp, String databaseId, String identifier) {
+    public static TransientGraph memoryPersistent(SerialisationSupport ss, VirtualGraphServerSupport vgss, ResourceSupport rs, AsyncRequestProcessor srp, String databaseId, String identifier) {
         return new TransientGraph(ss, vgss, rs, srp, databaseId, identifier, Persistency.MEMORY);
     }
     
-    private TransientGraph(SerialisationSupport ss, VirtualGraphServerSupport vgss, ResourceSupport rs, RequestProcessor srp, String databaseId, String identifier, Persistency persistency) {
+    private TransientGraph(SerialisationSupport ss, VirtualGraphServerSupport vgss, ResourceSupport rs, AsyncRequestProcessor srp, String databaseId, String identifier, Persistency persistency) {
         this.serialization = ss;
         this.virtualGraphServerSupport = vgss;
         this.sessionRequestProcessor = srp;
index 7184acbc5242de4029abe6606eb806e37f424193..229c3d6a9e965b5b14167896334c2e83b1b2508a 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -20,8 +20,6 @@ import java.util.concurrent.atomic.AtomicInteger;
 import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.RuntimeDatabaseException;
 import org.simantics.db.impl.query.QueryProcessor.AsyncBarrier;
-import org.simantics.db.impl.query.QueryProcessor.SessionTask;
-import org.simantics.utils.Development;
 
 final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrier {
 
@@ -39,6 +37,8 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie
 
        final private AsyncBarrierImpl caller;
 
+       //private final Semaphore sema = new Semaphore(0);
+
        public AsyncBarrierImpl(AsyncBarrierImpl caller) {
                super(0);
                if (BOOKKEEPING) {
@@ -294,7 +294,7 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie
 
                        while (get() != 0) {
 
-                               boolean executed = impl.processor.resume(impl);
+                               boolean executed = impl.performPending();
                                if(executed) waitCount = 0;
                                
                                ++waitCount;
@@ -321,18 +321,16 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie
 
                                        }
 
-                                       if(Development.DEVELOPMENT) {
-
-                                               impl.processor.threadLocks[0].lock();
-                                               System.err.println("-queues=" + impl.processor.queues[0].size());
-                                               impl.processor.threadLocks[0].unlock();
-                                               System.err.println("-own=" + impl.processor.ownTasks[0].size());
-                                               System.err.println("-ownSync=" + impl.processor.ownSyncTasks[0].size());
-                                               for(SessionTask task : impl.processor.ownSyncTasks[0]) {
-                                                       System.err.println("--" + task);
-                                               }
-
-                                       }
+//                                     if(Development.DEVELOPMENT) {
+//                                             impl.processor.threadLocks[0].lock();
+//                                             System.err.println("-queues=" + impl.processor.queues[0].size());
+//                                             impl.processor.threadLocks[0].unlock();
+//                                             System.err.println("-own=" + impl.processor.ownTasks[0].size());
+//                                             System.err.println("-ownSync=" + impl.processor.ownSyncTasks[0].size());
+//                                             for(SessionTask task : impl.processor.ownSyncTasks[0]) {
+//                                                     System.err.println("--" + task);
+//                                             }
+//                                     }
 
                                        throw new RuntimeDatabaseException("Request timed out.");
                                        //waitCount = 0;
index 4695b8b338c02b7a87e85c8985f83a20e54c301a..0f0ad8319413c997e2f932d70ffb4d7eb925f98f 100644 (file)
@@ -4,15 +4,15 @@ import org.simantics.db.Resource;
 import org.simantics.db.common.utils.Logger;
 import org.simantics.db.impl.query.IntProcedure;
 import org.simantics.db.impl.query.QuerySupport;
-import org.simantics.db.procedure.AsyncMultiProcedure;
+import org.simantics.db.procedure.SyncMultiProcedure;
 
 public class MultiIntProcedure implements IntProcedure {
 
-       final private AsyncMultiProcedure<Resource> procedure;
+       final private SyncMultiProcedure<Resource> procedure;
        final private ReadGraphImpl impl;
        final private QuerySupport support;
        
-       public MultiIntProcedure(AsyncMultiProcedure<Resource> procedure, ReadGraphImpl impl, QuerySupport support) {
+       public MultiIntProcedure(SyncMultiProcedure<Resource> procedure, ReadGraphImpl impl, QuerySupport support) {
                this.procedure = procedure;
                this.impl = impl;
                this.support = support;
index 614a96df3c47d29ea45e249dfb10df9653aaedf4..b5da0b92f7bae6521487e2fa0907ca2b96a3c09b 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -26,6 +26,7 @@ import java.util.IdentityHashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
+import java.util.Map;
 import java.util.Set;
 import java.util.function.Consumer;
 
@@ -89,6 +90,7 @@ import org.simantics.db.common.primitiverequest.ValueImplied;
 import org.simantics.db.common.primitiverequest.VariantValueImplied;
 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
+import org.simantics.db.common.procedure.adapter.SyncMultiProcedureAdapter;
 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
 import org.simantics.db.common.procedure.single.SyncReadProcedure;
@@ -104,6 +106,8 @@ import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
+import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiListener;
+import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiProcedure;
 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
@@ -129,15 +133,17 @@ import org.simantics.db.exception.NoSingleResultException;
 import org.simantics.db.exception.ResourceNotFoundException;
 import org.simantics.db.exception.ServiceException;
 import org.simantics.db.exception.ValidationException;
+import org.simantics.db.impl.BlockingAsyncProcedure;
 import org.simantics.db.impl.RelationContextImpl;
 import org.simantics.db.impl.ResourceImpl;
 import org.simantics.db.impl.internal.RandomAccessValueSupport;
 import org.simantics.db.impl.internal.ResourceData;
-import org.simantics.db.impl.procedure.CallWrappedSingleQueryProcedure4;
-import org.simantics.db.impl.procedure.ResultCallWrappedQueryProcedure4;
-import org.simantics.db.impl.procedure.ResultCallWrappedSingleQueryProcedure4;
+import org.simantics.db.impl.procedure.ResultCallWrappedSyncQueryProcedure;
 import org.simantics.db.impl.query.CacheEntry;
+import org.simantics.db.impl.query.QueryCache;
+import org.simantics.db.impl.query.QueryCacheBase;
 import org.simantics.db.impl.query.QueryProcessor;
+import org.simantics.db.impl.query.QueryProcessor.SessionTask;
 import org.simantics.db.impl.query.QuerySupport;
 import org.simantics.db.impl.query.TripleIntProcedure;
 import org.simantics.db.impl.support.ResourceSupport;
@@ -166,7 +172,6 @@ import org.simantics.db.request.ExternalRead;
 import org.simantics.db.request.MultiRead;
 import org.simantics.db.request.Read;
 import org.simantics.db.request.ReadInterface;
-import org.simantics.db.request.RequestFlags;
 import org.simantics.db.request.Write;
 import org.simantics.db.request.WriteInterface;
 import org.simantics.db.request.WriteOnly;
@@ -182,16 +187,21 @@ import org.simantics.utils.DataContainer;
 import org.simantics.utils.Development;
 import org.simantics.utils.datastructures.Pair;
 import org.simantics.utils.datastructures.collections.CollectionUtils;
+import org.slf4j.LoggerFactory;
 
 import gnu.trove.map.hash.TObjectIntHashMap;
 
-public class ReadGraphImpl implements ReadGraph {
+public class ReadGraphImpl implements AsyncReadGraph {
+
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ReadGraphImpl.class);
 
     final static boolean EMPTY_RESOURCE_CHECK = false;
     
        final public CacheEntry parent;
        final public QueryProcessor processor;
        
+       public AsyncBarrierImpl asyncBarrier = null;
+       
        final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
        final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
 
@@ -278,8 +288,10 @@ public class ReadGraphImpl implements ReadGraph {
 
                try {
 
-                       return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
-                                       id));
+                       Integer rid = QueryCache.resultURIToResource(this, id, parent, null);
+                       // FIXME: stupid to throw this here and catch and wrap it right away
+                       if(rid == 0) throw new ResourceNotFoundException(id);
+                       return processor.querySupport.getResource(rid);
 
                } catch (ResourceNotFoundException e) {
 
@@ -309,8 +321,7 @@ public class ReadGraphImpl implements ReadGraph {
 
                try {
 
-                       return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
-                                       id));
+                       return getResource(id);
 
                } catch (ResourceNotFoundException e) {
                        
@@ -331,6 +342,32 @@ public class ReadGraphImpl implements ReadGraph {
                }
 
        }
+       
+       @Override
+       public Map<String, Resource> getChildren(Resource resource) throws ValidationException, ServiceException {
+               
+               assert (resource != null);
+
+               try {
+
+                       int rId = processor.querySupport.getId(resource);
+                       return QueryCache.resultChildMap(this, rId, parent, null);
+
+               } catch (ValidationException e) {
+
+                       throw new ValidationException(e);
+
+               } catch (ServiceException e) {
+
+                       throw new ServiceException(e);
+
+               } catch (DatabaseException e) {
+
+                       throw new ServiceException(INTERNAL_ERROR_STRING, e);
+
+               }
+               
+       }
 
        final public Resource getRootLibrary() {
                return processor.getRootLibraryResource();
@@ -1896,36 +1933,8 @@ public class ReadGraphImpl implements ReadGraph {
 
                assert (request != null);
 
-               if (parent != null) {
-
-                       try {
-                               return processor.queryRead(this, request, parent, null, null);
-                       } catch (Throwable e) {
-                               if(e instanceof DatabaseException) throw (DatabaseException)e;
-                               else throw new DatabaseException(e);
-                       }
-
-               } else {
-
-                       try {
-
-                               return processor.tryQuery(this, request);
+               return QueryCache.resultReadEntry(this, request, parent, null, null);
 
-                       } catch (Throwable throwable) {
-
-                               //Logger.defaultLogError("Internal read request failure", throwable);
-
-                               if (throwable instanceof DatabaseException)
-                                       throw (DatabaseException) throwable;
-                               else
-                                       throw new DatabaseException(
-                                                       "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
-                                                       throwable);
-
-                       }
-
-               }
-               
        }
 
        @Override
@@ -1947,42 +1956,7 @@ public class ReadGraphImpl implements ReadGraph {
 
                ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
 
-               if (parent != null || listener != null) {
-                       
-                       try {
-                return processor.queryRead(this, request, parent, procedure, listener);
-            } catch (Throwable e) {
-                if(e instanceof DatabaseException) throw (DatabaseException)e;
-                else throw new DatabaseException(e);
-            }
-
-               } else {
-
-                       try {
-
-                               T t = processor.tryQuery(this, request);
-                               if(procedure != null)
-                                       procedure.execute(this, t);
-
-                               return t;
-
-                       } catch (Throwable throwable) {
-
-                               Logger.defaultLogError("Internal read request failure", throwable);
-
-                               if(procedure != null)
-                                       procedure.exception(this, throwable);
-                               
-                               if (throwable instanceof DatabaseException)
-                                       throw (DatabaseException) throwable;
-                               else
-                                       throw new DatabaseException(
-                                                       "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
-                                                       throwable);
-
-                       }
-
-               }
+               return QueryCache.resultReadEntry(this, request, parent, listener, procedure);
 
        }
 
@@ -2022,7 +1996,7 @@ public class ReadGraphImpl implements ReadGraph {
                                        throw (DatabaseException) exception;
                                else
                                        throw new DatabaseException(
-                                                       "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
+                                                       "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
                                                        exception);
                        }
                }
@@ -2038,12 +2012,10 @@ public class ReadGraphImpl implements ReadGraph {
                        throws DatabaseException {
 
                assert (request != null);
-               AsyncReadProcedure<T> procedure = new AsyncReadProcedure<T>();
-               syncRequest(request, procedure);
-               procedure.checkAndThrow();
-               return procedure.result;
-               
-//             return syncRequest(request, new AsyncProcedureAdapter<T>());
+               asyncBarrier = new AsyncBarrierImpl(null);
+               BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, null, request);
+               syncRequest(request, ap);
+               return ap.get();
 
        }
 
@@ -2071,71 +2043,13 @@ public class ReadGraphImpl implements ReadGraph {
 
                assert (request != null);
 
-               // System.out.println("syncRequest " + request + " syncParent=" +
-               // syncParent);
-
                ListenerBase listener = getListenerBase(procedure);
 
-               if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
-
-//                     Object syncParent = request;
-
-//                     final ReadGraphImpl newGraph = newSync();
-
-                       final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
-                                       procedure, request);
-                       
-                       processor.query(this, request, parent, wrapper, listener);
-
-//                     newGraph.waitAsync(syncParent);
-                       
-                       Throwable e = wrapper.getException();
-                       if (e != null) {
-                               // The request was async - produce meaningful stack trace by
-                               // wrapping
-                               if (e instanceof DatabaseException)
-                                       throw (DatabaseException) e;
-                               else
-                                       throw new DatabaseException(e);
-                       }
-                       
-                       return wrapper.getResult();
-
-               } else {
-
-                       // System.out.println("direct call " + request );
-
-                       // Do not set the sync state.parent for external threads
-//                     Object syncParent = request;
-
-//                     final ReadGraphImpl newGraph = newSync();
-
-                       final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
-                                       procedure, request);
-
-                       try {
-
-                               processor.tryQuery(this, request, wrapper);
-
-                       } catch (Throwable t) {
-
-                               wrapper.exception(this, t);
-
-                       }
+               BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
 
-                       Throwable e = wrapper.getException();
-                       if (e != null) {
-                               // The request was async - produce meaningful stack trace by
-                               // wrapping
-                               if (e instanceof DatabaseException)
-                                       throw (DatabaseException) e;
-                               else
-                                       throw new DatabaseException(e);
-                       }
-                       
-                       return wrapper.getResult();
+               QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
 
-               }
+               return ap.get();
 
        }
 
@@ -2143,40 +2057,14 @@ public class ReadGraphImpl implements ReadGraph {
 
                assert (request != null);
 
-               // System.out.println("syncRequest " + request + " syncParent=" +
-               // syncParent);
-
                ListenerBase listener = getListenerBase(procedure);
+               assert(listener == null);
 
-               if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
+               BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
 
-//                     final ReadGraphImpl newGraph = newSync();
-
-                       final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
-                                       procedure, request);
+               QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
 
-                       processor.query(this, request, parent, wrapper, listener);
-
-               } else {
-
-                       try {
-
-//                             final ReadGraphImpl newGraph = newSync();
-                               processor.tryQuery(this, request, procedure);
-//                             newGraph.waitAsync(null);
-                               waitAsyncProcedure(procedure);
-
-                       } catch (Throwable t) {
-                               if(Development.DEVELOPMENT) {
-                                       if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
-                                       t.printStackTrace();
-                                       }
-                               }
-                               procedure.exception(this, t);
-                               waitAsyncProcedure(procedure);
-                       }
-
-               }
+               ap.get();
 
        }
 
@@ -2201,21 +2089,21 @@ public class ReadGraphImpl implements ReadGraph {
                final ArrayList<T> result = new ArrayList<T>();
                final DataContainer<Throwable> exception = new DataContainer<Throwable>();
 
-               syncRequest(request, new AsyncMultiProcedure<T>() {
+               syncRequest(request, new SyncMultiProcedure<T>() {
 
                        @Override
-                       public void execute(AsyncReadGraph graph, T t) {
+                       public void execute(ReadGraph graph, T t) {
                                synchronized (result) {
                                        result.add(t);
                                }
                        }
 
                        @Override
-                       public void finished(AsyncReadGraph graph) {
+                       public void finished(ReadGraph graph) {
                        }
 
                        @Override
-                       public void exception(AsyncReadGraph graph, Throwable t) {
+                       public void exception(ReadGraph graph, Throwable t) {
                                exception.set(t);
                        }
 
@@ -2240,39 +2128,35 @@ public class ReadGraphImpl implements ReadGraph {
 
        }
 
-       @Override
-       public <T> Collection<T> syncRequest(MultiRead<T> request,
-                       AsyncMultiListener<T> procedure) {
-               return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
-       }
-
        @Override
        public <T> Collection<T> syncRequest(MultiRead<T> request,
                        SyncMultiListener<T> procedure) {
-               return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
+               return syncRequest(request, (SyncMultiProcedure<T>)procedure);
        }
 
        @Override
        public <T> Collection<T> syncRequest(MultiRead<T> request,
                        MultiListener<T> procedure) {
-               return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
+               return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
        }
 
        @Override
        public <T> Collection<T> syncRequest(MultiRead<T> request,
-                       AsyncMultiProcedure<T> procedure) {
+                       SyncMultiProcedure<T> procedure) {
 
                assert (request != null);
 
                ListenerBase listener = getListenerBase(procedure);
 
+               final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
+
                if (parent != null || listener != null) {
 
 //                     Object syncParent = request;
 
 //                     final ReadGraphImpl newGraph = newSync();
 
-                       processor.query(this, request, parent, procedure, listener);
+                       processor.query(this, request, parent, wrapper, listener);
 
 //                     newGraph.waitAsync(syncParent);
 
@@ -2282,36 +2166,22 @@ public class ReadGraphImpl implements ReadGraph {
 
 //                     final ReadGraphImpl newGraph = newSync();
 
-                       final ResultCallWrappedQueryProcedure4<T> wrapper = new ResultCallWrappedQueryProcedure4<T>(procedure);
-
                        try {
-
                                request.perform(this, wrapper);
-
                        } catch (Throwable t) {
-
                                wrapper.exception(this, t);
-//                             newGraph.waitAsync(syncParent);
-
                        }
 
                }
 
-               // TODO
-               return null;
-
-       }
+               return wrapper.get();
 
-       @Override
-       public <T> Collection<T> syncRequest(MultiRead<T> request,
-                       SyncMultiProcedure<T> procedure) {
-               return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
        }
 
        @Override
        public <T> Collection<T> syncRequest(MultiRead<T> request,
                        MultiProcedure<T> procedure) {
-               return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
+               return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
        }
 
        static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
@@ -2542,70 +2412,10 @@ public class ReadGraphImpl implements ReadGraph {
        final public <T> T syncRequest(final ExternalRead<T> request,
                        final Procedure<T> procedure) throws DatabaseException {
 
-               assert (request != null);
-
-               ListenerBase listener = getListenerBase(procedure);
-
-               final DataContainer<Throwable> exception = new DataContainer<Throwable>();
-               final DataContainer<T> result = new DataContainer<T>();
-
-               if (parent != null || listener != null) {
-
-//                     final ReadGraphImpl newGraph = newSync();
-                       
-                       processor.query(this, request, parent, new Procedure<T>() {
-
-                               @Override
-                               public void exception(Throwable throwable) {
-                                       exception.set(throwable);
-                                       procedure.exception(throwable);
-                               }
-
-                               @Override
-                               public void execute(T t) {
-                                       result.set(t);
-                                       procedure.execute(t);
-                               }
-
-                       }, listener);
-
-//                     newGraph.waitAsync(request);
-
-               } else {
-
-                       try {
-
-                               T t = processor.tryQuery(this, request);
-                               result.set(t);
-                               procedure.execute(t);
-
-                       } catch (Throwable t) {
-
-                           if (t instanceof DatabaseException) {
-                               exception.set((DatabaseException)t);
-                               procedure.exception(exception.get());
-                           } else {
-                               exception.set(new DatabaseException(
-                                       "Unexpected exception in ReadGraph.syncRequest(Read)",
-                                       t));
-                               procedure.exception(exception.get());
-                           }
-
-                       }
-
-               }
-
-               Throwable t = exception.get();
-               if (t != null) {
-                       if (t instanceof DatabaseException)
-                               throw (DatabaseException) t;
-                       else
-                               throw new DatabaseException(
-                                               "Unexpected exception in ReadGraph.syncRequest(Read)",
-                                               t);
-               }
+        assert (request != null);
 
-               return result.get();            
+        ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
+        return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
 
        }
 
@@ -3252,42 +3062,19 @@ public class ReadGraphImpl implements ReadGraph {
 
        }
 
-//     @Override
-//     final public void forEachDirectObject(final Resource subject,
-//                     final Resource relation,
-//                     final AsyncMultiProcedure<Resource> procedure) {
-//
-//             processor.forEachDirectObject(this, subject, relation, procedure);
-//
-//     }
-//
-//     @Override
-//     public void forEachDirectObject(Resource subject, Resource relation,
-//                     SyncMultiProcedure<Resource> procedure) {
-//             forEachDirectObject(subject, relation,
-//                             new SyncToAsyncMultiProcedure<Resource>(procedure));
-//     }
-//
-//     @Override
-//     public void forEachDirectObject(Resource subject, Resource relation,
-//                     MultiProcedure<Resource> procedure) {
-//             forEachDirectObject(subject, relation,
-//                             new NoneToAsyncMultiProcedure<Resource>(procedure));
-//     }
-       
        @Override
-       final public void forEachDirectPredicate(final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
+       final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
                processor.forEachDirectPredicate(this, subject, procedure);
        }
 
        @Override
-       public void forEachDirectPredicate(Resource subject, SyncMultiProcedure<Resource> procedure) {
-               forEachDirectPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(procedure));
+       final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
+               forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
        }
 
        @Override
-       public void forEachDirectPredicate(Resource subject, MultiProcedure<Resource> procedure) {
-               forEachDirectPredicate(subject, new NoneToAsyncMultiProcedure<Resource>(procedure));
+       public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
+               forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
        }
 
        @Override
@@ -4655,14 +4442,6 @@ public class ReadGraphImpl implements ReadGraph {
                                listener);
        }
 
-//     @Override
-//     final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
-//                     final AsyncProcedure<T> procedure) {
-//             
-//             forPossibleRelatedValue(subject, relation, binding, procedure, false);
-//             
-//     }
-
        final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
                        final AsyncProcedure<T> procedure) {
 
@@ -5351,11 +5130,6 @@ public class ReadGraphImpl implements ReadGraph {
                return processor.getSession();
        }
 
-//     @Override
-//     final public Builtins getBuiltins() {
-//             return processor.getSession().getBuiltins();
-//     }
-
        @Override
        public <T> void asyncRequest(final Read<T> request) {
 
@@ -5400,46 +5174,20 @@ public class ReadGraphImpl implements ReadGraph {
 
                assert (request != null);
                assert (procedure != null);
+       
+               processor.schedule(new SessionTask(false) {
 
-               final ListenerBase listener = getListenerBase(procedure);
-
-               if (parent != null || listener != null) {
-
-                       try {
-                               processor.queryRead(this, request, parent, procedure,
-                                               listener);
-                       } catch (Throwable e) {
-                               // This throwable has already been transferred to procedure at this point - do nothing about it
-                               //Logger.defaultLogError("Internal error ", e);
-                       }
-                       
-               } else {
-
-//                     final ReadGraphImpl newGraph = newSync();
-
-                       try {
-
-                               T result = request.perform(this);
-                               
-                               try {
-                                       procedure.execute(this, result);
-                               } catch (Throwable t) {
-                                       Logger.defaultLogError(t);
-                               }
-
-                       } catch (Throwable t) {
-
+                       @Override
+                       public void run(int thread) {
                                try {
-                                       procedure.exception(this, t);
-                               } catch (Throwable t2) {
-                                       Logger.defaultLogError(t2);
+                                       final ListenerBase listener = getListenerBase(procedure);
+                                       QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
+                               } catch (DatabaseException e) {
+                                       Logger.defaultLogError(e);
                                }
-
-                       } finally {
-
                        }
-
-               }
+                       
+               });
 
        }
 
@@ -5447,10 +5195,6 @@ public class ReadGraphImpl implements ReadGraph {
         return new ReadGraphImpl(null, support);
     }
 
-    public static ReadGraphImpl forRecompute(CacheEntry entry, QueryProcessor support) {
-        return new ReadGraphImpl(entry, support);       
-    }
-
        @Override
        public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
                asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
@@ -5511,33 +5255,19 @@ public class ReadGraphImpl implements ReadGraph {
                assert (request != null);
                assert (procedure != null);
 
-               final ListenerBase listener = getListenerBase(procedure);
-
-               if (parent != null || listener != null) {
-
-                       processor.query(this, request, parent, procedure, listener);
-
-               } else {
-
-                       try {
-                               
-                               request.perform(this, new CallWrappedSingleQueryProcedure4<T>(procedure, request));
-
-                       } catch (Throwable t) {
-
-                               if (t instanceof DatabaseException)
-                                       procedure.exception(this, t);
-                               else
-                                       procedure
-                                                       .exception(
-                                                                       this,
-                                                                       new DatabaseException(
-                                                                                       "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)",
-                                                                                       t));
+               processor.schedule(new SessionTask(false) {
 
+                       @Override
+                       public void run(int thread) {
+                               try {
+                                       final ListenerBase listener = getListenerBase(procedure);
+                                       QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
+                               } catch (DatabaseException e) {
+                                       Logger.defaultLogError(e);
+                               }
                        }
-
-               }
+                       
+               });
 
        }
 
@@ -5558,9 +5288,9 @@ public class ReadGraphImpl implements ReadGraph {
 
                assert (request != null);
 
-               asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
+               asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
                        @Override
-                       public void exception(AsyncReadGraph graph, Throwable t) {
+                       public void exception(ReadGraph graph, Throwable t) {
                        Logger.defaultLogError(t);
                        }
 
@@ -5572,27 +5302,22 @@ public class ReadGraphImpl implements ReadGraph {
 
        }
 
-       @Override
-       public <T> void asyncRequest(MultiRead<T> request,
-                       AsyncMultiListener<T> procedure) {
-               asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
-       }
-
        @Override
        public <T> void asyncRequest(MultiRead<T> request,
                        SyncMultiListener<T> procedure) {
-               asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
+               asyncRequest(request, (SyncMultiProcedure<T>)procedure);
        }
 
        @Override
        public <T> void asyncRequest(MultiRead<T> request,
                        MultiListener<T> procedure) {
-               asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
+               asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
        }
 
+       
        @Override
        public <T> void asyncRequest(final MultiRead<T> request,
-                       final AsyncMultiProcedure<T> procedure) {
+                       final SyncMultiProcedure<T> procedure) {
 
                assert (request != null);
                assert (procedure != null);
@@ -5614,7 +5339,11 @@ public class ReadGraphImpl implements ReadGraph {
 
                    } catch (Throwable t) {
 
-                       procedure.exception(this, t);
+                       try {
+                                       procedure.exception(this, t);
+                               } catch (DatabaseException e) {
+                                       LOGGER.error("Unexpected exception while handling exception", e);
+                               }
 
                    }                           
 
@@ -5622,16 +5351,10 @@ public class ReadGraphImpl implements ReadGraph {
 
        }
 
-       @Override
-       public <T> void asyncRequest(MultiRead<T> request,
-                       SyncMultiProcedure<T> procedure) {
-               asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
-       }
-
        @Override
        public <T> void asyncRequest(MultiRead<T> request,
                        MultiProcedure<T> procedure) {
-               asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
+               asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
        }
 
        @Override
@@ -5771,51 +5494,41 @@ public class ReadGraphImpl implements ReadGraph {
                assert (request != null);
                assert (procedure != null);
 
-               ListenerBase listener = getListenerBase(procedure);
+               final ListenerBase listener = getListenerBase(procedure);
 
                if (parent != null || listener != null) {
 
-                       processor.query(this, request, parent, procedure, listener);
-
-               } else {
-
                        try {
+                               QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
+                       } catch (DatabaseException e) {
+                               Logger.defaultLogError(e);
+                               // This throwable has already been transferred to procedure at this point - do nothing about it
+                       }
 
-                               request.register(this, new Listener<T>() {
-
-                                       @Override
-                                       public void execute(T result) {
-                                               procedure.execute(result);
-                                       }
-
-                                       @Override
-                                       public void exception(Throwable t) {
-                                               procedure.exception(t);
-                                       }
-
-                                       @Override
-                                       public String toString() {
-                                               return "asyncRequest(PrimitiveRead) -> " + request;
-                                       }
+               } else {
 
-                                       @Override
-                                       public boolean isDisposed() {
-                                               return true;
-                                       }
+                       request.register(this, new Listener<T>() {
+                               @Override
+                               public void execute(T result) {
+                                       procedure.execute(result);
+                               }
 
-                               });
+                               @Override
+                               public void exception(Throwable t) {
+                                       procedure.exception(t);
+                               }
 
-                       } catch (Throwable t) {
+                               @Override
+                               public String toString() {
+                                       return "asyncRequest(PrimitiveRead) -> " + request;
+                               }
 
-                               if (t instanceof DatabaseException)
-                                       procedure.exception(t);
-                               else
-                                       procedure
-                                                       .exception(new DatabaseException(
-                                                                       "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)",
-                                                                       t));
+                               @Override
+                               public boolean isDisposed() {
+                                       return true;
+                               }
 
-                       }
+                       });
 
                }
 
@@ -5936,53 +5649,23 @@ public class ReadGraphImpl implements ReadGraph {
                return thread == Integer.MIN_VALUE;
        }
 
-//     final private boolean isSync(int thread) {
-//             return thread < -1 && thread > Integer.MIN_VALUE;
-//     }
-
        ReadGraphImpl(ReadGraphImpl graph) {
                this(graph.parent, graph.processor);
        }
 
        ReadGraphImpl(CacheEntry parent, QueryProcessor support) {
-//             this.state = new ReadGraphState(barrier, support);
                this.parent = parent;
                this.processor = support;
        }
-       
-       ReadGraphImpl(final QueryProcessor support) {
-               
-//             this.state = state;
-               this.processor = support;
-               this.parent = null;
-               
-       }
-
-//     public static ReadGraphImpl createSync(int syncThread, Object syncParent,
-//                     ReadGraphSupportImpl support) {
-//             return new ReadGraphImpl(syncThread, syncThread, syncParent, null,
-//                             support, new AsyncBarrierImpl(null));
-//     }
 
        public static ReadGraphImpl create(QueryProcessor support) {
-               return new ReadGraphImpl(support);
+               return new ReadGraphImpl(null, support);
        }
 
-//     public ReadGraphImpl newAsync() {
-//             return this;
-////           if(!state.synchronizedExecution) {
-////                   return this;
-////           } else {
-////                   return new ReadGraphImpl(false, parent, state.support, state.barrier);
-////           }
-//     }
-
-//     public ReadGraphImpl newSync() {
-//             return new ReadGraphImpl(parent, processor);
-//     }
-
-       public ReadGraphImpl newSync(CacheEntry parentEntry) {
-               return new ReadGraphImpl(parentEntry, processor);
+       public static ReadGraphImpl newAsync(ReadGraphImpl parent) {
+               ReadGraphImpl result = new ReadGraphImpl(parent);
+               result.asyncBarrier = new AsyncBarrierImpl(parent.asyncBarrier);
+               return result;
        }
 
        public ReadGraphImpl newRestart(ReadGraphImpl impl) {
@@ -5990,41 +5673,10 @@ public class ReadGraphImpl implements ReadGraph {
                WriteGraphImpl write = processor.getSession().getService(
                                WriteGraphImpl.class);
 
-//             if (write.callerThread != impl.callerThread)
-//                     return new WriteGraphImpl(impl.callerThread, parent, state.support, write.writeSupport, write.provider, write.state.barrier);
                return write;
 
        }
 
-//     public ReadGraphImpl newSync(Object parentRequest) {
-//             return new ReadGraphImpl(callerThread, state.parent, state.support, new AsyncBarrierImpl(state.barrier));
-//     }
-
-//     public ReadGraphImpl newSync(final int callerThread, Object parentRequest) {
-//             assert (state.syncThread == callerThread || (state.syncThread == Integer.MIN_VALUE && callerThread != Integer.MIN_VALUE));
-//             return new ReadGraphImpl(callerThread, callerThread, parentRequest,
-//                             state.parent, state.support, new AsyncBarrierImpl(state.barrier));
-//     }
-//
-//     public ReadGraphImpl newSyncAsync(Object parentRequest) {
-////           assert (callerThread < 0);
-//             return new ReadGraphImpl(callerThread, state.syncThread, parentRequest,
-//                             state.parent, state.support, new AsyncBarrierImpl(state.barrier));
-//     }
-//
-//     public ReadGraphImpl newSyncAsync(final int callerThread,
-//                     Object parentRequest) {
-////           assert (callerThread < 0);
-//             // assert(state.syncThread == callerThread || (state.syncThread == Integer.MIN_VALUE
-//             // && callerThread != Integer.MIN_VALUE) );
-//             return new ReadGraphImpl(callerThread, callerThread, parentRequest,
-//                             state.parent, state.support, new AsyncBarrierImpl(state.barrier));
-//     }
-
-       public ReadGraphImpl withAsyncParent(CacheEntry parent) {
-               return new ReadGraphImpl(parent, processor);
-       }
-
        public ReadGraphImpl withParent(CacheEntry parent) {
                if(parent == this.parent) return this;
                else return new ReadGraphImpl(parent, processor);
@@ -6041,56 +5693,13 @@ public class ReadGraphImpl implements ReadGraph {
                
                assert(procedure.done());
                
-//             while (!procedure.done()) {
-//
-//                     boolean executed = processor.resumeTasks(callerThread, null, null);
-//                     if (!executed) {
-//                             try {
-//                                     Thread.sleep(1);
-//                                     //                                                              sema.tryAcquire(1, TimeUnit.MILLISECONDS);
-//                             } catch (InterruptedException e) {
-//                                     e.printStackTrace();
-//                             }
-//                     }
-//
-//             }
-               
        }
 
        public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
                
                assert(procedure.done());
                
-//             while (!procedure.done()) {
-//
-//                     boolean executed = processor.processor.resume(this);
-//                     if (!executed) {
-//                             try {
-//                                     Thread.sleep(1);
-//                                     //                                                              sema.tryAcquire(1, TimeUnit.MILLISECONDS);
-//                             } catch (InterruptedException e) {
-//                                     e.printStackTrace();
-//                             }
-//                     }
-//
-//             }
-               
        }
-       
-//     public void waitAsync(Object request) {
-//             try {
-//                     state.barrier.waitBarrier(request, this);
-//             } catch (Throwable t) {
-//                     t.printStackTrace();
-//                     processor.scanPending();
-//                     processor.querySupport.checkTasks();
-//                     throw new RuntimeDatabaseException(t);
-//             }
-//     }
-
-//     public void restart() {
-//             state.barrier.restart();
-//     }
 
        public boolean resumeTasks() {
                return processor.resumeTasks(this);
@@ -6665,17 +6274,23 @@ public class ReadGraphImpl implements ReadGraph {
             throw new DatabaseException(e);
         }
     }
-    
+
+    private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
+        protected Boolean initialValue() {
+            return true;
+        }
+    };
+
     @Override
     public boolean setSynchronous(boolean value) {
-        boolean old = processor.synch;
-        processor.synch = value;
+        boolean old = getSynchronous();
+        syncGraph.set(value);
         return old;
     }
-    
+
     @Override
     public boolean getSynchronous() {
-        return processor.synch;
+        return syncGraph.get();
     }
     
     public void ensureLoaded(int resource) {
@@ -6706,5 +6321,10 @@ public class ReadGraphImpl implements ReadGraph {
     public Object getModificationCounter() {
        return processor.getSession().getModificationCounter();
     }
-    
+
+    @Override
+    public boolean performPending() {
+        return processor.performPending(processor.thread.get());
+    }
+
 }
index dea87d54ea35c01ccaf4fa482e285d01e4d33fca..f8d42fba5b72b568a8037db2b6eab9f51a2ce24b 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -13,7 +13,6 @@ package org.simantics.db.impl.graph;
 
 import java.util.Set;
 
-import org.simantics.db.DirectStatements;
 import org.simantics.db.RelationInfo;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
@@ -27,13 +26,10 @@ import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.procedure.AsyncSetListener;
 import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.procedure.MultiProcedure;
-import org.simantics.db.procedure.Procedure;
 import org.simantics.db.procedure.StatementProcedure;
+import org.simantics.db.procedure.SyncMultiProcedure;
 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;
 
 public interface ReadGraphSupport {
 
@@ -53,9 +49,7 @@ public interface ReadGraphSupport {
     void forEachAssertedStatement(ReadGraphImpl graph, Resource subject, Resource predicate, AsyncMultiProcedure<Statement> procedure);
     void forEachObject(ReadGraphImpl graph, Resource subject, Resource predicate, MultiProcedure<Resource> procedure);
     void forEachObject(ReadGraphImpl graph, Resource subject, Resource predicate, AsyncMultiProcedure<Resource> procedure);
-    void forEachDirectPredicate(ReadGraphImpl graph, Resource subject, AsyncMultiProcedure<Resource> procedure);
-    void forEachDirectStatement(ReadGraphImpl graph, Resource subject, Procedure<DirectStatements> procedure);
-    void forEachDirectStatement(ReadGraphImpl graph, Resource subject, AsyncProcedure<DirectStatements> procedure, boolean ignoreVirtual);
+    void forEachDirectPredicate(ReadGraphImpl graph, Resource subject, AsyncProcedure<Set<Resource>> procedure);
     void forObjectSet(ReadGraphImpl graph, Resource subject, Resource predicate, AsyncSetListener<Resource> procedure);
     void forPredicateSet(ReadGraphImpl graph, Resource subject, AsyncSetListener<Resource> procedure);
     void forPrincipalTypeSet(ReadGraphImpl graph, Resource subject, AsyncSetListener<Resource> procedure);
@@ -64,7 +58,8 @@ public interface ReadGraphSupport {
     void forEachPrincipalType(ReadGraphImpl graph, Resource subject, AsyncMultiProcedure<Resource> procedure);
     void forEachPrincipalType(ReadGraphImpl graph, Resource subject, MultiProcedure<Resource> procedure);
 
-    void forRelationInfo(ReadGraphImpl graph, Resource subject, AsyncProcedure<RelationInfo> procedure);
+    RelationInfo getRelationInfo(ReadGraphImpl graph, Resource subject) throws DatabaseException;
+    
     void forTypes(ReadGraphImpl graph, Resource subject, AsyncProcedure<Set<Resource>> procedure);
     IntSet getTypes(ReadGraphImpl graph, Resource subject) throws Throwable;
     void forSupertypes(ReadGraphImpl graph, Resource subject, AsyncProcedure<Set<Resource>> procedure);
@@ -72,7 +67,7 @@ public interface ReadGraphSupport {
     void forPossibleSuperrelation(ReadGraphImpl graph, Resource subject, AsyncProcedure<Resource> procedure);
     void forSuperrelations(ReadGraphImpl graph, Resource subject, AsyncProcedure<Set<Resource>> procedure);
     byte[] getValue(ReadGraphImpl graph, Resource subject) throws DatabaseException;
-    byte[] forValue(ReadGraphImpl graph, Resource subject, AsyncProcedure<byte[]> procedure);
+    void forValue(ReadGraphImpl graph, Resource subject, AsyncProcedure<byte[]> procedure);
     void forPossibleValue(ReadGraphImpl graph, Resource subject, AsyncProcedure<byte[]> procedure);
     void forInverse(ReadGraphImpl graph, Resource relation, AsyncProcedure<Resource> procedure);
     void forResource(ReadGraphImpl graph, String id, AsyncProcedure<Resource> procedure);
@@ -85,15 +80,9 @@ public interface ReadGraphSupport {
     void forHasValue(ReadGraphImpl graph, Resource subject, AsyncProcedure<Boolean> procedure);
     void forOrderedSet(ReadGraphImpl graph, Resource subject, AsyncMultiProcedure<Resource> procedure);
 
-    <T> T queryRead(ReadGraphImpl graph, Read<T> request, CacheEntry parent, AsyncProcedure<T> procedure, ListenerBase listener) throws Throwable;
-    <T> void query(ReadGraphImpl graph, MultiRead<T> request, CacheEntry parent, AsyncMultiProcedure<T> procedure, ListenerBase listener);
-    <T> void query(ReadGraphImpl graph, AsyncRead<T> request, CacheEntry parent, AsyncProcedure<T> procedure, ListenerBase listener);
+    <T> void query(ReadGraphImpl graph, MultiRead<T> request, CacheEntry parent, SyncMultiProcedure<T> procedure, ListenerBase listener);
     <T> void query(ReadGraphImpl graph, AsyncMultiRead<T> request, CacheEntry parent, AsyncMultiProcedure<T> procedure, ListenerBase listener);
-    <T> void query(ReadGraphImpl graph, ExternalRead<T> request, CacheEntry parent, Procedure<T> procedure, ListenerBase listener);
-    
-    <T> T tryQuery(final ReadGraphImpl graph, final Read<T> request) throws DatabaseException;
-    <T> void tryQuery(final ReadGraphImpl graph, final AsyncRead<T> request, final AsyncProcedure<T> procedure);
-    
+
     VirtualGraph getProvider(Resource subject, Resource predicate, Resource object);
     VirtualGraph getProvider(Resource subject, Resource predicate);
     VirtualGraph getValueProvider(Resource subject);
index ca485f9893957ff784075f686b87221e843fce50..56fb9ec72f977e967b6a3db6a9366d4d20433657 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -128,11 +128,6 @@ final public class WriteGraphImpl extends ReadGraphImpl implements WriteGraph {
         return new WriteGraphImpl(parent, processor, writeSupport, provider);
     }
 
-    @Override
-    final public ReadGraphImpl withAsyncParent(CacheEntry parent2) {
-        return new WriteGraphImpl(parent2, processor, writeSupport, provider);
-    }
-
     @Override
     public ReadGraphImpl newRestart(ReadGraphImpl impl) {
 
@@ -801,7 +796,7 @@ final public class WriteGraphImpl extends ReadGraphImpl implements WriteGraph {
         
         Layer0 b = getBuiltins();
         initBuiltinValues(b);       
-        
+
         Statement literalStatement = getPossibleStatement(resource, predicate);
 
         if(literalStatement != null && resource.equals(literalStatement.getSubject())) {
index aa6425a8c11210d1cea94f01b4f37402ec96cd53..18345d632b73ef4ee0b9b6da137d540dd033218c 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.procedure;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 
 
 
 public interface InternalProcedure<Result> {
     
-    void execute(ReadGraphImpl graph, Result result);
-    void exception(ReadGraphImpl graph, Throwable throwable);
+    void execute(ReadGraphImpl graph, Result result) throws DatabaseException;
+    void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException;
        
 }
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/ResultCallWrappedSyncQueryProcedure.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/ResultCallWrappedSyncQueryProcedure.java
new file mode 100644 (file)
index 0000000..d2034fb
--- /dev/null
@@ -0,0 +1,88 @@
+/*******************************************************************************
+ * Copyright (c) 2018 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.impl.procedure;
+
+import java.util.ArrayList;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.procedure.SyncMultiProcedure;
+
+public class ResultCallWrappedSyncQueryProcedure<Result> implements SyncMultiProcedure<Result> {
+
+    private final ArrayList<Result> result;
+    private Throwable exception = null;
+    private final SyncMultiProcedure<Result> procedure;
+    private final AtomicBoolean latch;
+
+    public ResultCallWrappedSyncQueryProcedure(SyncMultiProcedure<Result> procedure) {
+        this.procedure = procedure;
+        latch = new AtomicBoolean(false);
+        result = new ArrayList<Result>();
+    }
+
+    @Override
+    public void execute(ReadGraph graph, Result result) {
+        try {
+            synchronized(this.result) {
+                this.result.add(result);
+            }
+            procedure.execute(graph, result);
+        } catch (Throwable t) {
+            Logger.defaultLogError("AsyncMultiProcedure.execute failed for " + procedure, t);
+        }
+    }
+
+    @Override
+    public void finished(ReadGraph graph) {
+        if(latch.compareAndSet(false, true)) {
+            try {
+                procedure.finished(graph);
+            } catch (Throwable t) {
+                Logger.defaultLogError("AsyncMultiProcedure.exception failed for " + procedure, t);
+            } finally {
+            }
+        } else {
+            Logger.defaultLogError("Finished or exception was called many times (this time is finished)");
+        }
+    }
+
+    @Override
+    public void exception(ReadGraph graph, Throwable t) {
+        if(latch.compareAndSet(false, true)) {
+            try {
+                this.exception = t;
+                procedure.exception(graph, t);
+            } catch (Throwable throwable) {
+                Logger.defaultLogError("AsyncMultiProcedure.exception failed for " + procedure, throwable);
+            } finally {
+            }
+        } else {
+            Logger.defaultLogError("Finished or exception was called many times (this time is exception)");
+        }
+    }
+
+    public ArrayList<Result> get() {
+        return result;
+    }
+
+    public Throwable getException() {
+        return exception;
+    }
+
+    @Override
+    public String toString() {
+        return "ResultCallWrappedQueryProcedure4[" + procedure + "]"; 
+    }
+
+}
index 44e7aa38c2c692d38aa587a82a3e56f0929dcf66..9bb96a83617bcbe65539ff92c27be1ddb5f870d9 100644 (file)
@@ -11,6 +11,7 @@
  *******************************************************************************/
 package org.simantics.db.impl.procedure;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.query.TripleIntProcedure;
 
@@ -19,10 +20,10 @@ abstract public class TripleIntProcedureAdapter implements TripleIntProcedure {
     public TripleIntProcedureAdapter() {
     }
     @Override
-    public void execute(ReadGraphImpl graph, int s, int p, int o) {
+    public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
     }
     @Override
-    public void finished(ReadGraphImpl graph) {
+    public void finished(ReadGraphImpl graph) throws DatabaseException {
     }
     
 }
index af8858e5a9954ba59415d1f915c204ec653cdfbf..b4104e965fbc0b56be90fc40a8f0cd07eef5ab3a 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.concurrent.Semaphore;
-
 import org.simantics.db.RelationInfo;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.IntProcedureAdapter;
-import org.simantics.db.impl.procedure.InternalProcedure;
-import org.simantics.db.procedure.ListenerBase;
 
 
-final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure> {
-       
-//     public ArrayList<IntProcedure> procs = null;
+final public class AssertedPredicates extends UnaryQuery<IntProcedure> {
        
-    public AssertedPredicates(final int r) {
+    AssertedPredicates(final int r) {
         super(r);
     }
 
     public static AssertedPredicates newInstance(final int r) {
         return new AssertedPredicates(r);
     }
-    
-    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);
-        if(entry == null) {
-               
-               entry = new AssertedPredicates(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-               
-            if(!entry.isReady()) {
-               synchronized(entry) {
-                    if(!entry.isReady()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<IntProcedure>();
-//                     entry.procs.add(procedure);
-//                     return;
-                    }
-               }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
 
+    @Override
+    final public void clearResult(QuerySupport support) {
+        setResult(new IntArray());
     }
     
-    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);
-               if(entry != null && entry.isReady()) { 
-                       entry.performFromCache(graph, provider, procedure);
-                       return;
-               }
-        }
-        
-        runner(graph, r, provider, parent, listener, procedure);
-         
-    }
-
     @Override
-    public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
-        return provider.assertedPredicatesMap.get(id);
+    final public void setReady() {
+        super.setReady();
+        IntArray v = (IntArray)getResult();
+        int size = v.size();
+        if(size == 0) setResult(IntArray.EMPTY);
+        else v.trim();
     }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.assertedPredicatesMap.put(id, this);
-       }
-
+    
        @Override
        final public void removeEntry(QueryProcessor provider) {
-           provider.assertedPredicatesMap.remove(id);
+           provider.cache.remove(this);
        }
        
-       void computeInheritedAssertions(ReadGraphImpl graph, int type, final QueryProcessor queryProvider, final IntProcedure proc, final boolean store) {
+       void computeInheritedAssertions(ReadGraphImpl graph, int type, final IntProcedure proc) throws DatabaseException {
+               
+               QueryProcessor processor = graph.processor;
 
-           DirectObjects.queryEach(graph, type, queryProvider.getInherits(), queryProvider, this, null, new SyncIntProcedure() {
+               QueryCache.runnerDirectObjects(graph, type, processor.getInherits(), this, null, new SyncIntProcedure() {
 
             @Override
             public void run(ReadGraphImpl graph) {
-                
-//                finish(graph, queryProvider);
-//                     proc.finished(graph);
-                
             }
 
             @Override
-            public void execute(ReadGraphImpl graph,int inh) {
+            public void execute(ReadGraphImpl graph,int inh) throws DatabaseException {
 
                inc();
                 
-                AssertedPredicates.queryEach(graph, inh, queryProvider, AssertedPredicates.this, null, new IntProcedure() {
+                QueryCache.runnerAssertedPredicates(graph, inh, AssertedPredicates.this, null, new IntProcedure() {
 
                     @Override
                     public void execute(ReadGraphImpl graph, int ass) {
-                            
                        addOrSet(ass);
-//                     proc.execute(graph, ass);
-                        
                     }
 
                     @Override
-                    public void finished(ReadGraphImpl graph) {
+                    public void finished(ReadGraphImpl graph) throws DatabaseException {
                         dec(graph);
                     }
                                
                                @Override
                                public void exception(ReadGraphImpl graph, Throwable t) {
-//                                     proc.exception(graph, t);
                     }
                     
                 });
@@ -132,33 +82,32 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
-               
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
-                
             }
             
         });
 
        }
 
-    @Override
-    public Object computeForEach(ReadGraphImpl graph, final QueryProcessor queryProvider, final IntProcedure proc, final boolean store) {
+    //@Override
+    public Object compute(ReadGraphImpl graph, final IntProcedure proc) throws DatabaseException {
 
-        computeInheritedAssertions(graph, id, queryProvider, proc, store);
+       QueryProcessor processor = graph.processor;
+       
+        computeInheritedAssertions(graph, id, proc);
 
-       DirectObjects.queryEach(graph, id, queryProvider.getAsserts(), queryProvider, this, null, new IntProcedure() {
+       QueryCache.runnerDirectObjects(graph, id, processor.getAsserts(), this, null, new IntProcedure() {
 
             @Override
-            public void execute(ReadGraphImpl graph, final int ass) {
+            public void execute(ReadGraphImpl graph, final int ass) throws DatabaseException {
                 
-                DirectObjects.queryEach(graph, ass, queryProvider.getHasPredicate(), queryProvider, AssertedPredicates.this, null, new IntProcedure() {
+               QueryCache.runnerDirectObjects(graph, ass, processor.getHasPredicate(), AssertedPredicates.this, null, new IntProcedure() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, final int pred) {
+                    public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
 
                        addOrSetHiding(graph, pred, AssertedPredicates.this);
-//                     proc.execute(graph, pred);
                         return;
                         
                     }
@@ -187,9 +136,9 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
             
         });
        
-        finish(graph, queryProvider);
+        finish(graph, processor);
 
-       performFromCache(graph, queryProvider, proc);
+       performFromCache(graph, proc);
         
         return getResult();
         
@@ -204,31 +153,10 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
         
        assert(!isReady());
 
-//        ArrayList<IntProcedure> p = null;
-
         synchronized(this) {
-
                setReady();
-//            p = procs;
-//            procs = null; 
-        
         }
 
-//        if(p != null) {
-//             IntArray v = (IntArray)getResult();
-//             if(v.data == null) {
-//                 if(v.sizeOrData != IntArray.NO_DATA) {
-//                     for(IntProcedure proc : p) proc.execute(graph, v.sizeOrData);
-//                 }
-//             } else {
-//                 for(IntProcedure proc : p) {
-//                     for(int i = 0;i < v.sizeOrData ; i++) proc.execute(graph, v.data[i]);
-//                 }
-//             }
-//             
-//             for(IntProcedure proc : p) proc.finished(graph);
-//        }
-
     }
 
     synchronized private void addOrSet(int add) {
@@ -240,40 +168,16 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
         
     }
 
-       final static InternalProcedure<RelationInfo> ip = new InternalProcedure<RelationInfo>() {
-
-               @Override
-               public void execute(ReadGraphImpl graph, RelationInfo result) {
-               }
-
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable throwable) {
-               }
-
-       };
-
-       final static InternalProcedure<IntSet> ip2 = new InternalProcedure<IntSet>() {
-
-               @Override
-               public void execute(ReadGraphImpl graph, IntSet result) {
-               }
-
-               @Override
-               public void exception(ReadGraphImpl graph, Throwable throwable) {
-               }
-
-       };
-
-       synchronized private void addOrSetHiding(ReadGraphImpl graph, int add, CacheEntry parent) {
+       synchronized private void addOrSetHiding(ReadGraphImpl graph, int add, CacheEntry parent) throws DatabaseException {
 
        assert(isPending());
        
         IntArray value = (IntArray)getResult(); 
-               RelationInfo ri = RelationInfoQuery.queryEach(graph, add, graph.processor, parent, null, ip);
+               RelationInfo ri = QueryCacheBase.resultRelationInfoQuery(graph, add, parent, null);
                if(ri.isFunctional) {
                        // Replace existing functional predicate if found
                        try {
-                               IntSet supers = SuperRelations.queryEach2(graph, add, graph.processor, parent, null, ip2);
+                               IntSet supers = QueryCache.resultSuperRelations(graph, add, parent, null); 
                        if(value.data == null) {
                            if(value.sizeOrData != IntArray.NO_DATA) {
                                if(supers.contains(value.sizeOrData)) {
@@ -301,7 +205,7 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
     }
     
     @Override
-    public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, final IntProcedure procedure) {
+    public Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
 
        assert(isReady());
        
@@ -321,30 +225,21 @@ final public class AssertedPredicates extends CollectionUnaryQuery<IntProcedure>
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
 
-        computeForEach(graph, provider, new IntProcedureAdapter() {
+        compute(graph, new IntProcedureAdapter() {
 
             @Override
             public void finished(ReadGraphImpl graph) {
-                s.release();
             }
 
             @Override
             public void exception(ReadGraphImpl graph, Throwable t) {
-               s.release();
                 new Error("Error in recompute.", t).printStackTrace();
             }
 
-        }, true);
-
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
+        });
         
     }
     
-    
 }
index 41b8e08d59486690543fd79494195c76395a01f9..8649de32eeee907b1e551a4e681b165c1edd1768 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.simantics.db.RelationInfo;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.impl.procedure.TripleIntProcedureAdapter;
-import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
 
 
 final public class AssertedStatements extends CollectionBinaryQuery<TripleIntProcedure> {
     
-//    public ArrayList<TripleIntProcedure> procs;
-    
     public AssertedStatements(final int r1, final int r2) {
         super(r1, r2);
     }
@@ -34,82 +31,20 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
     public static AssertedStatements newInstance(final int r1, final int r2) {
         return new AssertedStatements(r1, r2);
     }
-    
-    final static AssertedStatements runner(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final AssertedStatements cached, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
-        
-        AssertedStatements entry = cached != null ? cached : (AssertedStatements)provider.assertedStatementsMap.get(id(r1,r2)); 
-        if(entry == null) {
-               
-               entry = new AssertedStatements(r1, r2);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-            return entry;
-            
-        } else {
-               
-            if(entry.isPending()) {
-                synchronized(entry) {
-                    if(entry.isPending()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<TripleIntProcedure>();
-//                        entry.procs.add(procedure);
-//                        provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                        return entry;
-                    }
-                }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-        
-        return entry;
-        
-    }
-    
-    final public static AssertedStatements queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
-        
-        assert(r1 != 0);
-        assert(r2 != 0);
-        
-        final AssertedStatements entry = (AssertedStatements)provider.assertedStatementsMap.get(id(r1,r2));
-        
-        if(parent == null && !(listener != null)) {
-            if(entry != null && entry.isReady()) { 
-                entry.performFromCache(graph, provider, procedure);
-                return entry;
-            }
-        }
-
-        return runner(graph, r1, r2, provider, entry, parent, listener, procedure);
-         
-    }
-
-    @Override
-    public BinaryQuery<TripleIntProcedure> getEntry(QueryProcessor provider) {
-        return provider.assertedStatementsMap.get(id);
-    }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.assertedStatementsMap.put(id, this);
-       }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-           provider.assertedStatementsMap.remove(id);
+           provider.cache.remove(this);
        }
        
-       void computeInheritedAssertions(ReadGraphImpl graph, int type, final int predicate, final RelationInfo ri, final QueryProcessor queryProvider, final TripleIntProcedure proc) {
+       static void computeInheritedAssertions(ReadGraphImpl graph, int type, final int predicate, final RelationInfo ri, final AssertedStatements entry, final TripleIntProcedure proc) throws DatabaseException {
            
-//        final AtomicBoolean found = new AtomicBoolean(0);
+        QueryProcessor processor = graph.processor;
 
-           DirectObjects.queryEach(graph, type, queryProvider.getInherits(), queryProvider, this, null, new SyncIntProcedure() {
+        QueryCache.runnerDirectObjects(graph, type, processor.getInherits(), entry, null, new SyncIntProcedure() {
 
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
 
 //                if(ri.isFunctional && found.get() == 1) {
 //
@@ -120,29 +55,29 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 //                    
 //                }
 
-                finish(graph, queryProvider);
+                finish(graph, entry);
                 proc.finished(graph);
                 
             }
 
             @Override
-            public void execute(ReadGraphImpl graph, int inh) {
+            public void execute(ReadGraphImpl graph, int inh) throws DatabaseException {
                 
 //                if(ri.isFunctional && found.get() == 1) return;
 
                 inc();
                 
-                AssertedStatements.queryEach(graph, inh, predicate, queryProvider, AssertedStatements.this, null, new TripleIntProcedureAdapter() {
+                QueryCache.runnerAssertedStatements(graph, inh, predicate, entry, null, new TripleIntProcedureAdapter() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, int s, int p, int o) {
+                    public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
 
 //                        if(ri.isFunctional) {
 //                            
 ////                            if(found.get() == 1) return;
 //
 //                            if(found.compareAndSet(0, o)) {
-                                if(addOrSet(s,p,o))
+                                if(addOrSet(s,p,o, entry))
                                        proc.execute(graph, s, p, o);
 //                            }
 //                            // If this was a duplicate, we can ignore this
@@ -164,12 +99,12 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                     }
 
                     @Override
-                    public void finished(ReadGraphImpl graph) {
+                    public void finished(ReadGraphImpl graph) throws DatabaseException {
                         dec(graph);
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        proc.exception(graph, t);
                                        dec(graph);
                     }
@@ -179,7 +114,7 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
             
@@ -187,47 +122,62 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
        }
 
-    void computeLocalAssertions(ReadGraphImpl graph, final int type, final int predicate, final RelationInfo ri, final QueryProcessor queryProvider, final TripleIntProcedure proc) {
+    static void computeLocalAssertions(ReadGraphImpl graph, final int type, final int predicate, final RelationInfo ri, final AssertedStatements entry, final TripleIntProcedure proc) throws DatabaseException {
+        
         
+    }
+
+       //@Override
+       public Object compute(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
+               computeForEach(graph, r1(), r2(), this, procedure);
+               return getResult();
+       }
+
+    public static void computeForEach(ReadGraphImpl graph, final int type, final int predicate, final AssertedStatements entry, final TripleIntProcedure procedure) throws DatabaseException {
+
+       RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, predicate, entry, null);
+       
         final AtomicInteger found = new AtomicInteger(0);
 
-        DirectObjects.queryEach(graph, type, queryProvider.getAsserts(), queryProvider, this, null, new SyncIntProcedure() {
+        QueryProcessor processor = graph.processor;
+        
+        QueryCache.runnerDirectObjects(graph, type, processor.getAsserts(), entry, null, new SyncIntProcedure() {
 
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
                 
                 if(ri.isFunctional && found.get() > 1) {
 
                     ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one asserted statement.");
-                    except(exception);
-                    proc.exception(graph, exception);
+                    except(exception, entry);
+                    procedure.exception(graph, exception);
                     return;
                     
                 }
 
                 if(ri.isFunctional && found.get() == 1) {
                                         
-                    finish(graph, queryProvider);
-                    proc.finished(graph);
+                    finish(graph, entry);
+                    procedure.finished(graph);
                     return;
                     
                 }
 
-                computeInheritedAssertions(graph, type, predicate, ri, queryProvider, proc);
+                computeInheritedAssertions(graph, type, predicate, ri, entry, procedure);
                 
             }
 
             @Override
-            public void execute(ReadGraphImpl graph, final int ass) {
+            public void execute(ReadGraphImpl graph, final int ass) throws DatabaseException {
                 
                 if(ri.isFunctional && found.get() > 1) return;
                 
                 inc();
                 
-                DirectObjects.queryEach(graph, ass, queryProvider.getHasPredicate(), queryProvider, AssertedStatements.this, null, new IntProcedure() {
+                QueryCache.runnerDirectObjects(graph, ass, processor.getHasPredicate(), entry, null, new IntProcedure() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, final int pred) {
+                    public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
                         
                         if(ri.isFunctional) {
                             
@@ -235,18 +185,18 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
                             inc();
 
-                            DirectObjects.queryEach(graph, ass, queryProvider.getHasObject(), queryProvider, AssertedStatements.this, null, new IntProcedure() {
+                            QueryCache.runnerDirectObjects(graph, ass, processor.getHasObject(), entry, null, new IntProcedure() {
 
                                 @Override
-                                public void execute(ReadGraphImpl graph, final int object) {
+                                public void execute(ReadGraphImpl graph, final int object) throws DatabaseException {
                                     
                                     if(found.get() > 1) return;
 
                                     if(pred == predicate) {
 
                                        if(found.getAndIncrement() == 0) {
-                                               if(addOrSet(type, pred, object))
-                                                       proc.execute(graph, type, pred, object);
+                                               if(addOrSet(type, pred, object, entry))
+                                                       procedure.execute(graph, type, pred, object);
                                        }
                                        
                                         return;
@@ -257,10 +207,10 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
                                         inc();
 
-                                        SuperRelations.queryEach(graph, pred, queryProvider, AssertedStatements.this, null, new InternalProcedure<IntSet>() {
+                                        QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
                                             
                                             @Override
-                                            public void execute(ReadGraphImpl graph, IntSet result) {
+                                            public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
                                                 
                                                 if(found.get() > 1) {
                                                     dec(graph);
@@ -270,8 +220,8 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                                 if(result.contains(predicate)) {
                     
                                                     if(found.getAndIncrement() == 0) {
-                                                               if(addOrSet(type, pred, object))
-                                                                       proc.execute(graph, type, pred, object);
+                                                               if(addOrSet(type, pred, object, entry))
+                                                                       procedure.execute(graph, type, pred, object);
                                                        }
                                                     
                                                 }
@@ -281,9 +231,9 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                             }
                                                        
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                            
-                                                               proc.exception(graph, t);
+                                                               procedure.exception(graph, t);
                                                 dec(graph);
                                                                
                                             }
@@ -295,13 +245,13 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                 }
                                 
                                 @Override
-                                public void finished(ReadGraphImpl graph) {
+                                public void finished(ReadGraphImpl graph) throws DatabaseException {
                                     dec(graph);
                                 }
                                        
                                        @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                               proc.exception(graph, t);
+                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                               procedure.exception(graph, t);
                                     dec(graph);
                                 }
 
@@ -311,15 +261,15 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
                             inc();
 
-                            DirectObjects.queryEach(graph, ass, queryProvider.getHasObject(), queryProvider, AssertedStatements.this, null, new IntProcedure() {
+                            QueryCache.runnerDirectObjects(graph, ass, processor.getHasObject(), entry, null, new IntProcedure() {
 
                                 @Override
-                                public void execute(ReadGraphImpl graph, final int object) {
+                                public void execute(ReadGraphImpl graph, final int object) throws DatabaseException {
                                     
                                     if(pred == predicate) {
 
-                                        addOrSet(type, pred, object);
-                                        proc.execute(graph, type, pred, object);
+                                        addOrSet(type, pred, object, entry);
+                                        procedure.execute(graph, type, pred, object);
                                         return;
                                         
                                     }
@@ -328,15 +278,15 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
                                         inc();
 
-                                        SuperRelations.queryEach(graph, pred, queryProvider, AssertedStatements.this, null, new InternalProcedure<IntSet>() {
+                                        QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
                                             
                                             @Override
-                                            public void execute(ReadGraphImpl graph, IntSet result) {
+                                            public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
                                                 
                                                 if(result.contains(predicate)) {
                     
-                                                    addOrSet(type, pred, object);
-                                                    proc.execute(graph, type, pred, object);
+                                                    addOrSet(type, pred, object, entry);
+                                                    procedure.execute(graph, type, pred, object);
 
                                                 }
                                                 
@@ -345,8 +295,8 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                             }
                                                        
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                                               proc.exception(graph, t);
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                                               procedure.exception(graph, t);
                                                 dec(graph);
                                             }
  
@@ -357,13 +307,13 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                 }
                                 
                                 @Override
-                                public void finished(ReadGraphImpl graph) {
+                                public void finished(ReadGraphImpl graph) throws DatabaseException {
                                     dec(graph);
                                 }
                                        
                                        @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                               proc.exception(graph, t);
+                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                               procedure.exception(graph, t);
                                     dec(graph);
                                 }
 
@@ -374,13 +324,13 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                     }
 
                     @Override
-                    public void finished(ReadGraphImpl graph) {
+                    public void finished(ReadGraphImpl graph) throws DatabaseException {
                         dec(graph);
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
-                                       proc.exception(graph, t);
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                       procedure.exception(graph, t);
                         dec(graph);
                     }
 
@@ -389,42 +339,21 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
             
         });
         
-    }
-
-       @Override
-    public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final TripleIntProcedure procedure, boolean store) {
-        
-        RelationInfoQuery.queryEach(graph, r2(), provider, this, null, new InternalProcedure<RelationInfo>() {
-
-            @Override
-            public void execute(ReadGraphImpl graph, RelationInfo ri) {
-                
-                computeLocalAssertions(graph, r1(), r2(), ri, provider, procedure);
-                
-            }
-                       
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               procedure.exception(graph, t);
-            }
-        });
-        
     }
     
     @Override
     public String toString() {
-       return "AssertedStatements2[" + r1() + " - " + r2() + "]";
+       return "AssertedStatements[" + r1() + " - " + r2() + "]";
     }
 
     private boolean addOrSet(int s, int p, int o) {
-        
+       
        assert(isPending());
         
         IntArray value = (IntArray)getResult();
@@ -449,39 +378,39 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
         
     }
     
-    final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
+    static boolean addOrSet(int s, int p, int o, AssertedStatements entry) {
+       if(entry != null) {
+               return entry.addOrSet(s, p, o);
+       } else {
+               return true;
+       }
+    }
+    
+    static void finish(ReadGraphImpl graph, AssertedStatements entry) {
         
-       assert(isPending());
-
-//        ArrayList<TripleIntProcedure> p = null;
-
-        synchronized(this) {
-               
-               setReady();
-//            p = procs;
-//            procs = null; 
-        
-        }
-        
-//        if(p != null) {
-//             final IntArray value = (IntArray)getResult();
-//             for(TripleIntProcedure proc : p) {
-//                     for(int i=0;i<value.size();i+=3) {
-//                             proc.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
-//                     }
-//             }
-//
-//             for(TripleIntProcedure proc : p) proc.finished(graph);
-//        }
+       assert(entry.isPending());
+       if(entry != null) {
+               synchronized(entry) {
+                       entry.setReady();
+               }
+       }
 
     }
+    
+    static void except(Throwable t, AssertedStatements entry) {
+       if(entry != null) {
+               synchronized(entry) {
+                       entry.except(t);
+               }
+       }
+    }
 
     @Override
-    public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {
+    public Object performFromCache(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
         
        assert(isReady());
         
-       if(handleException(graph, procedure)) return;
+       if(handleException(graph, procedure)) return getResult();
        
         final IntArray value = (IntArray)getResult();
         for(int i=0;i<value.size();i+=3) {
@@ -490,18 +419,17 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
         procedure.finished(graph);
         
+        return value;
+        
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
         
-        final Semaphore s = new Semaphore(0);
-
-        computeForEach(graph, provider, new TripleIntProcedureAdapter() {
+        compute(graph, new TripleIntProcedureAdapter() {
 
             @Override
             public void finished(ReadGraphImpl graph) {
-                s.release();
             }
                        
                        @Override
@@ -509,13 +437,7 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                throw new Error("Error in recompute", t);
             }
     
-        }, true);
-        
-        try {
-            s.acquire();
-        } catch (InterruptedException e) {
-            throw new Error(e);
-        }
+        });
         
     }
     
index fac6b011f23d2137e14755430efd88fa1365cc1e..24f8e5375f65660a41e08396e97a393b5ce4b5b1 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -14,19 +14,22 @@ package org.simantics.db.impl.query;
 import java.util.ArrayList;
 
 import org.simantics.db.AsyncReadGraph;
-import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.BlockingAsyncMultiProcedure;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.procedure.AsyncMultiProcedure;
 import org.simantics.db.request.AsyncMultiRead;
 import org.simantics.db.request.RequestFlags;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
+final public class AsyncMultiReadEntry<T> extends CacheEntryBase<AsyncMultiProcedure<T>> {
 
-//    public ArrayList<AsyncMultiProcedure<T>> procs = null;
+    private static final Logger LOGGER = LoggerFactory.getLogger(AsyncMultiReadEntry.class);
 
     protected AsyncMultiRead<T> request;
     
-    public AsyncMultiReadEntry(AsyncMultiRead<T> request) {
+    AsyncMultiReadEntry(AsyncMultiRead<T> request) {
        this.request = request;
     }
     
@@ -49,55 +52,22 @@ final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
     
     final synchronized public void finish(AsyncReadGraph graph) {
        
-//     new Exception("finish " + this).printStackTrace();
-       
-       if(!isPending()) {
-               System.err.println("aff");
-       }
-       
        assert(isPending());
 
-//        ArrayList<AsyncMultiProcedure<T>> p = null;
-
         synchronized(this) {
-
                setReady();
-//            p = procs;
-//            procs = null; 
-        
         }
         
-//        if(p != null) {
-//             ArrayList<T> v = (ArrayList<T>)getResult();
-//             if(v != null) {
-//                 for(AsyncMultiProcedure<T> proc : p) {
-//                     for(T t : v) proc.execute(graph, t);
-//                 }
-//             }
-//             
-//             for(AsyncMultiProcedure<T>  proc : p) proc.finished(graph);
-//        }
-        
     }
 
     final synchronized public void except(AsyncReadGraph graph, Throwable t) {
 
        assert(isPending());
 
-//        ArrayList<AsyncMultiProcedure<T>> p = null;
-
         synchronized(this) {
-
                except(t);
-//            p = procs;
-//            procs = null; 
-        
         }
         
-//        if(p != null) {
-//             for(AsyncMultiProcedure<T>  proc : p) proc.exception(graph, t);
-//        }
-        
     }
 
     @SuppressWarnings("unchecked")
@@ -121,42 +91,43 @@ final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
         return new Query() {
 
                        @Override
-                       public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-                               
-                               QueryProcessor qp = (QueryProcessor)provider;
-
-                               final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); 
+                       public void recompute(ReadGraphImpl graph) {
 
                                try {
 
-                                   request.perform(parentGraph , new AsyncMultiProcedure<T>() {
+                                       BlockingAsyncMultiProcedure<T> proc = new BlockingAsyncMultiProcedure<>(graph, new AsyncMultiProcedure<T>() {
 
-                        @Override
-                        public void execute(AsyncReadGraph graph, T result) {
-                            addOrSet(result);
-                        }
-                        
-                        public void finished(AsyncReadGraph graph) {
-                               finish(graph);
-                        };
-                                       
-                                       @Override
-                                       public void exception(AsyncReadGraph graph, Throwable t) {
-                            except(t);
-                           }
+                                               @Override
+                                               public void execute(AsyncReadGraph graph, T result) {
+                                                       addOrSet(result);
+                                               }
 
-                    });
+                                               public void finished(AsyncReadGraph graph) {
+                                                       finish(graph);
+                                               };
+
+                                               @Override
+                                               public void exception(AsyncReadGraph graph, Throwable t) {
+                                                       except(t);
+                                               }
+
+                                       }, request);
+
+                                       request.perform(graph , proc);
+
+                                       proc.get();
 
                                } catch (Throwable t) {
-                    except(t);
-                    if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                }
+                                       
+                                       except(t);
+                                       
+                               }
                                
                        }
 
                        @Override
                        public void removeEntry(QueryProcessor processor) {
-                       processor.asyncMultiReadMap.remove(request);
+                       processor.cache.remove(AsyncMultiReadEntry.this);
                        }
 
                        @Override
@@ -176,51 +147,38 @@ final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
 
        @SuppressWarnings("unchecked")
        @Override
-       public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-               
-               final AsyncMultiProcedure<T> proc = (AsyncMultiProcedure<T>)procedure;
+       public Object performFromCache(ReadGraphImpl graph, AsyncMultiProcedure<T> proc) {
 
         if(isExcepted()) {
 
             try {
                 proc.exception(graph, (Throwable)getResult());
             } catch (Throwable t) {
-                t.printStackTrace();
+                LOGGER.error("performFromCache proc.exception failed", t);
             }
-            
-            
+
         } else {
-            
+
             final ArrayList<T> values = (ArrayList<T>)getResult();
             for(T value : values) {
                 try {
                     proc.execute(graph, value);
                 } catch (Throwable t) {
-                    t.printStackTrace();
+                    LOGGER.error("performFromCache proc.execute failed", t);
                 }
             }
 
             try {
                 proc.finished(graph);
             } catch (Throwable t) {
-                t.printStackTrace();
+                LOGGER.error("performFromCache proc.finished failed", t);
             }
 
         }
                
-               
+               return getResult();
                
        }
-
-//     @Override
-//     public void performFromCache(int callerThread, Object provider,
-//                     Object procedure) {
-//
-//         QueryProvider2 queryProvider = (QueryProvider2)provider;
-//         ReadGraphImpl graph = ReadGraphImpl.forFromCache(callerThread, null, new ReadGraphSupportImpl(null, queryProvider, null));
-//             performFromCache(graph, provider, procedure);
-//             
-//     }
        
        @Override
        public String toString() {
@@ -228,4 +186,8 @@ final public class AsyncMultiReadEntry<T> extends CacheEntryBase {
                else return request.toString() + statusOrException;
        }
 
+       public Object compute(ReadGraphImpl graph, AsyncMultiProcedure<T> procedure) throws DatabaseException {
+               return graph.processor.cache.performQuery(graph, request, this, procedure);
+       }
+
 }
index a9726a75d6ac4eb5df323625ffd19da9369bcb9b..00abbebf65859e76d0e8dad481d01097cab04621 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
 package org.simantics.db.impl.query;
 
 import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.RuntimeDatabaseException;
+import org.simantics.db.impl.BlockingAsyncProcedure;
 import org.simantics.db.impl.DebugPolicy;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.request.AsyncRead;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-final public class AsyncReadEntry<T> extends CacheEntryBase {
+final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T> {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(AsyncReadEntry.class);
 
     protected AsyncRead<T> request;
 
-    public AsyncReadEntry(AsyncRead<T> request) {
-       this.request = request;
-       if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
+    AsyncReadEntry(AsyncRead<T> request) {
+        this.request = request;
+        if (DebugPolicy.QUERY_STATE)
+            System.out.println("[QUERY STATE]: created " + this);
     }
 
     @Override
     int makeHash() {
-       return request.hashCode();
+        return request.hashCode();
     }
-    
+
     @Override
     public Object getOriginalRequest() {
         return request;
     }
-    
+
     @Override
     public void discard() {
-       super.discard();
-       //request = null;
-       setResult(null);
+        super.discard();
+        setResult(null);
     }
-    
-    final public void addOrSet(AsyncReadGraph graph, Object item) {
-
-       assert(isPending());
-        
-//        ArrayList<AsyncProcedure<T>> p = null;
-
-        synchronized(this) {
-               
-            setResult(item);
-               setReady();
-//            p = procs;
-//            procs = null;
-            
-        }
 
-//        if(p != null)
-//            for(AsyncProcedure<T> proc : p) {
-//             proc.execute(graph, (T)item);
-////                proc.first.execute(graph, (T)item);
-////                proc.second.dec();
-//            }
-        
-    }
-    
-    
     public void except(AsyncReadGraph graph, Throwable t) {
-       
-       assert(isPending());
-        
-//        ArrayList<AsyncProcedure<T>> p = null;
 
-        synchronized(this) {
-               
+        assert (isPending());
+
+        synchronized (this) {
             except(t);
-////            p = procs;
-//            procs = null;
-            
         }
 
-//        if(p != null)
-//            for(AsyncProcedure<T> proc : p) {
-//             proc.exception(graph, t);
-//            }
-       
     }
-    
-    
+
     @Override
     final public Query getQuery() {
-       
-        return new Query() {
 
-                       @Override
-                       public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-                               
-                               QueryProcessor qp = (QueryProcessor)provider;
+        return new Query() {
 
-                               final ReadGraphImpl parentGraph = ReadGraphImpl.forRecompute(entry, qp); 
+            @Override
+            public void recompute(ReadGraphImpl graph) {
 
-                               try {
+                try {
 
-                                   request.perform(parentGraph , new AsyncProcedure<T>() {
+                    BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure<>(graph, new AsyncProcedure<T>() {
 
                         @Override
                         public void execute(AsyncReadGraph graph, T result) {
-                            addOrSet(graph, result);
+                            setResult(result);
+                            setReady();
                         }
-                               
-                               @Override
-                               public void exception(AsyncReadGraph graph, Throwable t) {
-                                   except(t);
+
+                        @Override
+                        public void exception(AsyncReadGraph graph, Throwable t) {
+                            except(t);
                         }
 
-                    });
+                    }, request);
+
+                    request.perform(graph, proc);
 
-                               } catch (Throwable t) {
-                                   except(t);
+                    proc.get();
+
+                } catch (Throwable t) {
+                    except(t);
                 }
-                               
-                       }
-
-                       @Override
-                       public void removeEntry(QueryProcessor qp) {
-                       qp.asyncReadMap.remove(request);
-                       }
-
-                       @Override
-                       public int type() {
-                               return request.getFlags();
-                       }
-                       
-                       @Override
-                       public String toString() {
-                               if(request == null) return "DISCARDED";
-                               else if(isExcepted()) return request.toString() + " " + getResult();
-                               else return request.toString() + " " + statusOrException;
-                       }
-               
+
+            }
+
+            @Override
+            public void removeEntry(QueryProcessor qp) {
+                qp.cache.remove(AsyncReadEntry.this);
+            }
+
+            @Override
+            public int type() {
+                return request.getFlags();
+            }
+
+            @Override
+            public String toString() {
+                if (request == null)
+                    return "DISCARDED";
+                else if (isExcepted())
+                    return request.toString() + " " + getResult();
+                else
+                    return request.toString() + " " + statusOrException;
+            }
+
         };
-        
+
     }
 
-       @SuppressWarnings("unchecked")
-       public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-               
-        AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
+    @Override
+    public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
+
+        if (isExcepted()) {
 
-           if(isExcepted()) {
-            
             try {
-                proc.exception(graph, (Throwable)getResult());
+                proc.exception(graph, (Throwable) getResult());
             } catch (Throwable t) {
-                t.printStackTrace();
+                LOGGER.error("performFromCache proc.exception failed", t);
             }
-            
+
         } else {
-            
+
             try {
-                proc.execute(graph, (T)getResult());
+                proc.execute(graph, (T) getResult());
             } catch (Throwable t) {
-                t.printStackTrace();
+                LOGGER.error("performFromCache proc.execute failed", t);
             }
-            
+
         }
-               
-       }
-
-       @Override
-       public String toString() {
-               if(isDiscarded()) return "DISCARDED " + request.toString();
-               else if(isExcepted()) return request.toString() + " " + getResult();
-               else return request.toString() + " " + statusOrException;
-       }
+
+        return getResult();
+
+    }
+
+    public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry,
+            AsyncProcedure<T> procedure_) throws DatabaseException {
+
+        AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
+
+        ReadGraphImpl queryGraph = parentGraph.withParent(entry);
+        
+        BlockingAsyncProcedure<T> proc = new BlockingAsyncProcedure<>(queryGraph, new AsyncProcedure<T>() {
+
+            @Override
+            public void execute(AsyncReadGraph returnGraph, T result) {
+                try {
+                    procedure.execute(parentGraph, result);
+                } catch (Throwable t) {
+                    LOGGER.error("computeForEach procedure.execute failed", t);
+                }
+            }
+
+            @Override
+            public void exception(AsyncReadGraph returnGraph, Throwable t) {
+                try {
+                    procedure.exception(parentGraph, t);
+                } catch (Throwable t2) {
+                    LOGGER.error("computeForEach procedure.exception failed", t2);
+                }
+            }
+
+            @Override
+            public String toString() {
+                return procedure.toString();
+            }
+
+        }, request);
+
+        request.perform(queryGraph, proc);
+        
+        proc.get();
+
+        if (entry != null)
+            entry.performFromCache(parentGraph, procedure_);
+
+    }
+
+    @Override
+    public String toString() {
+        if (isDiscarded())
+            return "DISCARDED " + request.toString();
+        else if (isExcepted())
+            return request.toString() + " " + getResult();
+        else
+            return request.toString() + " " + statusOrException;
+    }
+
+    @Override
+    public void execute(AsyncReadGraph graph, T result) {
+        setResult(result);
+        setReady();
+    }
+
+    @Override
+    public void exception(AsyncReadGraph graph, Throwable throwable) {
+        except(throwable);
+    }
 
 }
index cb2df4eea79b5f25d0e0ffb63912c5e63d7c07f7..d699ea825d9823cfb8c1f3454f13115ff4518455 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.request.RequestFlags;
 
-
-abstract public class BinaryQuery<Procedure> extends CacheEntryBase implements Query {
+public abstract class BinaryQuery<Procedure> extends CacheEntryBase<Procedure> implements Query {
 
        final public long id;
 
@@ -65,23 +63,7 @@ abstract public class BinaryQuery<Procedure> extends CacheEntryBase implements Q
     final public Query getQuery() {
         return this;
     }
-    
-    @Override
-    public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
-        recompute(graph, (QueryProcessor)provider);
-    }
-    
-    @SuppressWarnings("unchecked")
-    @Override
-    public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
-        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 void putEntry(QueryProcessor provider);
     abstract public void removeEntry(QueryProcessor provider);
-    abstract public BinaryQuery<Procedure> getEntry(QueryProcessor provider);
 
 }
index f09ccc082c4a425fc5ab2b5cc25169f9bcea7712..b460a22bd06762b5d4b253b470813b9a92bc7875 100644 (file)
@@ -15,6 +15,7 @@ import gnu.trove.impl.hash.THash;
 
 import java.lang.reflect.Array;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 
 
@@ -34,66 +35,23 @@ abstract public class BinaryQueryHash<Procedure> extends THash {
 
     protected final BinaryQuery<Procedure> REMOVED = new BinaryQuery<Procedure>(-1, -1) {
 
-        @Override
-        public void computeForEach(ReadGraphImpl graph, QueryProcessor provider, Object procedure, boolean store) {
-            throw new Error("Not possible!");
-        }
-
-        @Override
-        public void putEntry(QueryProcessor provider) {
-            throw new Error("Not possible!");
-        }
-
-        @Override
-        public BinaryQuery<Procedure> getEntry(QueryProcessor provider) {
-            throw new Error("Not possible!");
-        }
-
-        @Override
-        public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-            throw new Error("Not possible!");
-        }
-
         @Override
         public void removeEntry(QueryProcessor provider) {
             throw new Error("Not possible!");
         }
 
-//        @Override
-//        public ICacheEntry2 cachedEntry(Object provider) {
-//            throw new Error("Not possible!");
-//        }
-//
-//        @Override
-//        public void computeForEach(int callerThread, Object provider, ICacheEntry2 parent, Object procedure) {
-//            throw new Error("Not possible!");
-//        }
-
         @Override
         public int type() {
             throw new Error("Not possible!");
         }
 
-//        @Override
-//        public void reset() {
-//            throw new Error("Not possible!");
-//        }
-
                @Override
-               public void performFromCache(ReadGraphImpl graph, QueryProcessor provider,
-                               Procedure procedure) {
+               Object performFromCache(ReadGraphImpl graph, Procedure procedure) throws DatabaseException {
             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,
-                               Object procedure) {
+               public void recompute(ReadGraphImpl graph) throws DatabaseException {
             throw new Error("Not possible!");
                }
         
index d65622dadd0a18ecafb1c15b3c7c22b15b345cb0..8489256225da47bfa52c2aa29940ffbba386e24b 100644 (file)
@@ -16,7 +16,7 @@ import org.simantics.db.impl.graph.ReadGraphImpl;
 
 
 
-public abstract class CacheEntry {
+public abstract class CacheEntry<Procedure> {
        
     final public static int HAS_BEEN_BOUND = 1;
 
@@ -32,25 +32,26 @@ public abstract class CacheEntry {
     abstract void setPending();
     abstract void discard();
     abstract void except(Throwable t);
-    abstract void setResult(Object result);
     abstract void clearResult(QuerySupport support);
 
     abstract void prepareRecompute(QuerySupport querySupport);
     
     abstract public Object getOriginalRequest();
     abstract Query getQuery();
-    abstract <T> T getResult();
 
     abstract CacheEntry pruneFirstParents();
     abstract void removeParent(CacheEntry entry);
     abstract void addParent(CacheEntry entry);
     abstract boolean hasParents();
-    abstract Iterable<CacheEntry> getParents(QueryProcessor processor);
+    abstract Iterable<CacheEntry<?>> getParents(QueryProcessor processor);
     abstract CacheEntry getFirstParent(QueryProcessor processor);
     abstract boolean moreThanOneParent(QueryProcessor processor);
     abstract int parentCount(QueryProcessor processor);
     
-    abstract void performFromCache(ReadGraphImpl graph, Object provider, Object procedure);
+    abstract <T> T getResult();
+    abstract void setResult(Object result);
+    
+    abstract Object performFromCache(ReadGraphImpl graph, Procedure procedure) throws DatabaseException;
     
     abstract boolean isImmutable(ReadGraphImpl graph) throws DatabaseException;
     
index d30d59320b656b1178d9dab10730fa2d2ca61d0d..7c87b50e3acfa0e4630f3eb93ba8859c1eb13c90 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -21,7 +21,7 @@ import org.simantics.db.impl.procedure.InternalProcedure;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-abstract public class CacheEntryBase extends CacheEntry {
+public abstract class CacheEntryBase<Procedure> extends CacheEntry<Procedure> {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(CacheEntryBase.class);
     
@@ -34,11 +34,11 @@ abstract public class CacheEntryBase extends CacheEntry {
        
     final public static CacheEntryBase[] NONE = new CacheEntryBase[0];
 
-       static private Object NO_RESULT = new Object();
+       static Object NO_RESULT = new Object();
        static protected Object INVALID_RESULT = new Object();
        
-       // Just created
-    static protected Object FRESH = new Object() { public String toString() { return "CREATED"; }};
+//     // Just created
+//    static protected Object FRESH = new Object() { public String toString() { return "CREATED"; }};
     // Result is computed - no exception
     static protected Object READY = new Object() { public String toString() { return "READY"; }};
     // Computation is under way
@@ -46,12 +46,12 @@ abstract public class CacheEntryBase extends CacheEntry {
     // Entry is discarded and is waiting for garbage collect
     static protected Object DISCARDED = new Object() { public String toString() { return "DISCARDED"; }};
     // The result has been invalidated
-    static protected Object REFUTED = new Object() { public String toString() { return "REFUTED"; }};
+    static protected Object REQUIRES_COMPUTATION = new Object() { public String toString() { return "REFUTED"; }};
     // The computation has excepted - the exception is in the result
     static protected Object EXCEPTED = new Object() { public String toString() { return "EXCEPTED"; }};
 
     // This indicates the status of the entry
-    public Object statusOrException = FRESH;
+    public Object statusOrException = REQUIRES_COMPUTATION;
     
     private CacheEntry p1 = null;
     private Object p2OrParents = null;
@@ -67,10 +67,10 @@ abstract public class CacheEntryBase extends CacheEntry {
     abstract int makeHash();
     
     // This can be tested to see if the result is finished
-    private Object result = NO_RESULT;
+    Object result = NO_RESULT;
     
     final public boolean isFresh() {
-       return FRESH == statusOrException;
+       return REQUIRES_COMPUTATION == statusOrException;
     }
 
     public void setReady() {
@@ -96,23 +96,23 @@ abstract public class CacheEntryBase extends CacheEntry {
     @Override
     final public void refute() {
        if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: refuted " + this);
-       statusOrException = REFUTED;
+       statusOrException = REQUIRES_COMPUTATION;
     }
     
     @Override
     final public boolean isRefuted() {
-        return REFUTED == statusOrException;
+        return REQUIRES_COMPUTATION == statusOrException;
     }
 
     @Override
-    final public void except(Throwable t) {
+    public void except(Throwable throwable) {
        if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: excepted " + this);
        if(statusOrException != DISCARDED) {
                statusOrException = EXCEPTED;
-               result = t;
+               result = throwable;
        } else {
-               LOGGER.warn("Cache entry got excepted status after being discarded: " + getClass().getSimpleName(), t);
-               result = t;
+               LOGGER.warn("Cache entry got excepted status after being discarded: " + getClass().getSimpleName(), throwable);
+               result = throwable;
        }
     }
     
@@ -130,7 +130,7 @@ abstract public class CacheEntryBase extends CacheEntry {
     }
 
     @Override
-    final public void setPending() {
+    public void setPending() {
        statusOrException = PENDING;
     }
     
@@ -139,6 +139,10 @@ abstract public class CacheEntryBase extends CacheEntry {
         return PENDING == statusOrException;
     }
     
+    final public boolean requiresComputation() {
+       return REQUIRES_COMPUTATION == statusOrException;
+    }
+    
     final public boolean assertPending() {
        boolean result = isPending();
        if(!result) {
@@ -319,9 +323,9 @@ abstract public class CacheEntryBase extends CacheEntry {
     }
     
     @Override
-       final public Iterable<CacheEntry> getParents(QueryProcessor processor) {
+       final public Iterable<CacheEntry<?>> getParents(QueryProcessor processor) {
 
-               ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
+               ArrayList<CacheEntry<?>> result = new ArrayList<CacheEntry<?>>();
                if(p1 != null) result.add(p1);
                if(p2OrParents != null) {
                if(p2OrParents instanceof QueryIdentityHashSet) {
@@ -361,8 +365,7 @@ abstract public class CacheEntryBase extends CacheEntry {
        
     }
     
-    protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry> result) {
-       
+    protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {
     }
     
     protected String internalError() {
@@ -370,7 +373,7 @@ abstract public class CacheEntryBase extends CacheEntry {
     }
 
     
-    protected boolean handleException(ReadGraphImpl graph, IntProcedure procedure) {
+    protected boolean handleException(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
        if(isExcepted()) {
                procedure.exception(graph, (Throwable)getResult());
                return true;
@@ -379,7 +382,7 @@ abstract public class CacheEntryBase extends CacheEntry {
        }
     }
     
-    protected boolean handleException(ReadGraphImpl graph, TripleIntProcedure procedure) {
+    protected boolean handleException(ReadGraphImpl graph, TripleIntProcedure procedure) throws DatabaseException {
        if(isExcepted()) {
                procedure.exception(graph, (Throwable)getResult());
                return true;
@@ -388,7 +391,7 @@ abstract public class CacheEntryBase extends CacheEntry {
        }
     }
 
-    protected <T> boolean handleException(ReadGraphImpl graph, InternalProcedure<T> procedure) {
+    protected <T> boolean handleException(ReadGraphImpl graph, InternalProcedure<T> procedure) throws DatabaseException {
        if(isExcepted()) {
                procedure.exception(graph, (Throwable)getResult());
                return true;
@@ -425,10 +428,6 @@ abstract public class CacheEntryBase extends CacheEntry {
                clearResult(querySupport);
     }
     
-    /*
-     * 
-     * 
-     */
     @Override
     int getGCStatus() {
        return GCStatus;
@@ -454,5 +453,8 @@ abstract public class CacheEntryBase extends CacheEntry {
        // This is the original request for all built-in queries
        return getQuery();
     }
-    
+
+    public CacheEntryBase() {
+    }
+
 }
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ChildMap.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ChildMap.java
new file mode 100644 (file)
index 0000000..8effbb1
--- /dev/null
@@ -0,0 +1,150 @@
+/*******************************************************************************
+ * Copyright (c) 2018 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.impl.query;
+
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.serialization.Serializer;
+import org.simantics.db.ObjectResourceIdMap;
+import org.simantics.db.common.WriteBindings;
+import org.simantics.db.common.exception.DebugException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.impl.procedure.InternalProcedure;
+import org.simantics.db.service.CollectionSupport;
+
+public final class ChildMap extends UnaryQueryP<ObjectResourceIdMap<String>> {
+
+    ChildMap(final int r) {
+        super(r);
+    }
+
+    @Override
+    public final void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
+    }
+
+    @Override
+    public void compute(ReadGraphImpl graph, final InternalProcedure<ObjectResourceIdMap<String>> procedure)
+            throws DatabaseException {
+        computeForEach(graph, id, this, procedure);
+    }
+
+    public static void computeForEach(ReadGraphImpl graph, final int root, final ChildMap entry,
+            final InternalProcedure<ObjectResourceIdMap<String>> procedure_) throws DatabaseException {
+
+        InternalProcedure<ObjectResourceIdMap<String>> procedure = entry != null ? entry : procedure_;
+
+        computeForEach2(graph, root, entry, procedure);
+
+        if (entry != null)
+            entry.performFromCache(graph, procedure_);