From 939dda12d6b0fc305c2deb0447284df9944ffa1c Mon Sep 17 00:00:00 2001 From: juanf <juanf@ethz.ch> Date: Tue, 17 Jan 2023 15:10:50 +0100 Subject: [PATCH] SSDM-13195 : Remove unnecesary getParents and getChildren calls --- .../sample/VerifySampleParentsExecutor.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleParentsExecutor.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleParentsExecutor.java index a5ee40ae03d..5d0b454eb5f 100644 --- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleParentsExecutor.java +++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleParentsExecutor.java @@ -48,14 +48,19 @@ public class VerifySampleParentsExecutor extends AbstractVerifyEntityCyclesExecu public void verify(IOperationContext context, CollectionBatch<SamplePE> batch) { super.verify(context, batch); - new CollectionBatchProcessor<SamplePE>(context, batch) { @Override public void process(SamplePE sample) { - SampleGenericBusinessRules.assertValidParents(sample); - SampleGenericBusinessRules.assertValidChildren(sample); + // DAO methods to fetch a list of sample parents or children are way faster than the PE + // counterparts. These checks here save unnecessary and slow database trips on sample imports. + if (getParents(context, sample).isEmpty() == false) { + SampleGenericBusinessRules.assertValidParents(sample); + } + if (getChildren(context, sample).isEmpty() == false) { + SampleGenericBusinessRules.assertValidChildren(sample); + } } @Override @@ -82,8 +87,21 @@ public class VerifySampleParentsExecutor extends AbstractVerifyEntityCyclesExecu @Override protected Map<Long, Set<Long>> getRelatedIdsMap(IOperationContext context, Set<Long> entityIds) { - Long relationshipId = getRelationshipIdExecutor.get(context, RelationshipType.PARENT_CHILD); + Long relationshipId = getRelatedIdForParentChild(context); return daoFactory.getSampleDAO().mapSampleIdsByChildrenIds(entityIds, relationshipId); } + private Long getRelatedIdForParentChild(IOperationContext context) + { + return getRelationshipIdExecutor.get(context, RelationshipType.PARENT_CHILD); + } + + private Set<TechId> getParents(IOperationContext context, SamplePE samplePE) { + Long relationshipId = getRelatedIdForParentChild(context); + return daoFactory.getSampleDAO().listSampleIdsByChildrenIds(TechId.createList(samplePE.getId()), new TechId(relationshipId)); + } + + private Set<TechId> getChildren(IOperationContext context, SamplePE samplePE) { + return daoFactory.getSampleDAO().listSampleIdsByParentIds(TechId.createList(samplePE.getId())); + } } -- GitLab