From: Tuukka Lehtonen Date: Wed, 10 Jun 2020 18:41:27 +0000 (+0300) Subject: Optimize ReadGraph.hasStatements(s,p) to not use getObjects(s,p) X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F08%2F4308%2F2;p=simantics%2Fplatform.git Optimize ReadGraph.hasStatements(s,p) to not use getObjects(s,p) 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 --- diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java index 614a96df3..d6a0aa9f4 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java @@ -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 objects = getObjects(subject, relation); - return !objects.isEmpty(); + SyncReadProcedure procedure = new SyncReadProcedure(); + processor.forRelationInfo(this, relation, procedure); + Collection 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) {