From ac990d10ff2d1d1042a68f0594489aed23a27453 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Wed, 10 Jun 2020 21:41:27 +0300 Subject: [PATCH] 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 --- .../db/impl/graph/ReadGraphImpl.java | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) 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) { -- 2.43.2