]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Optimize ReadGraph.hasStatements(s,p) to not use getObjects(s,p) 08/4308/2
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 10 Jun 2020 18:41:27 +0000 (21:41 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 10 Jun 2020 18:59:14 +0000 (18:59 +0000)
Depending on the predicate asked for, hasStatements(s,p) can be
optimized to not realize the entire [object] result list created by
getObjects(s,p).

gitlab #558

Change-Id: I7be1d2000396ce6c1e4595cabaafef5db4224de9

bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java

index 614a96df3c47d29ea45e249dfb10df9653aaedf4..d6a0aa9f45dea69a4cbb87efa7be87e77df59745 100644 (file)
@@ -48,6 +48,7 @@ import org.simantics.db.DevelopmentKeys;
 import org.simantics.db.ExternalValueSupport;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.RelationContext;
+import org.simantics.db.RelationInfo;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.Statement;
@@ -1817,8 +1818,35 @@ public class ReadGraphImpl implements ReadGraph {
 
                try {
 
-                       Collection<Resource> objects = getObjects(subject, relation);
-                       return !objects.isEmpty();
+                       SyncReadProcedure<RelationInfo> procedure = new SyncReadProcedure<RelationInfo>();
+                       processor.forRelationInfo(this, relation, procedure);
+                       Collection<Resource> predicates = getPredicates(subject);
+                       
+                       if(procedure.result.isFinal) {
+                               
+                               return predicates.contains(relation);
+                               
+                       } else if (procedure.result.isFunctional) {
+
+                               try {
+                                       int result = processor.getSingleObject(this, subject, relation);
+                                       return result != 0;
+                               } catch (ManyObjectsForFunctionalRelationException e) {
+                                       return true;
+                               } catch (DatabaseException e) {
+                                       throw new ServiceException(e);
+                               }
+                               
+                       } else {
+
+                               for(Resource predicate : getPredicates(subject)) {
+                                       if(isSubrelationOf(predicate, relation))
+                                               return true;
+                               }
+                               
+                       }
+                       
+                       return false;
 
                } catch (ServiceException e) {