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=638ce7e010a6292fedc677de18173676db305881;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 (cherry picked from commit ac990d10ff2d1d1042a68f0594489aed23a27453) --- 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 cc15c1eac..5d4852c0d 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 @@ -50,6 +50,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; @@ -1849,8 +1850,35 @@ public class ReadGraphImpl implements AsyncReadGraph { 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) {