]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java
Optimize ReadGraph.hasStatements(s,p) to not use getObjects(s,p)
[simantics/platform.git] / 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) {