From 0a007b4917ceeba0208fbcf5df1818908b7eeb0e Mon Sep 17 00:00:00 2001 From: brinn <brinn> Date: Thu, 30 Jul 2009 17:01:24 +0000 Subject: [PATCH] change: set an upper limit for property loading also for material listing (generalized from sample listing) SVN: 11950 --- .../server/dataaccess/db/DAOUtils.java | 52 +++++++++++++++++++ .../server/dataaccess/db/MaterialDAO.java | 10 +++- .../server/dataaccess/db/SampleDAO.java | 19 +------ 3 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DAOUtils.java diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DAOUtils.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DAOUtils.java new file mode 100644 index 00000000000..45f98cfc977 --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DAOUtils.java @@ -0,0 +1,52 @@ +/* + * Copyright 2009 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.server.dataaccess.db; + +import org.hibernate.Criteria; +import org.hibernate.criterion.Projections; + +/** + * Utility routines for DAOs. + * + * @author Bernd Rinn + */ +final class DAOUtils +{ + + private DAOUtils() + { + // Cannot be instantiated + } + + /** + * Don't try to get properties for more than 1000 entities. + */ + final static int MAX_COUNT_FOR_PROPERTIES = 1000; + + /** + * Returns the number of entities that the given <var>critera</var> will return. + */ + static int getCount(final Criteria criteria) + { + int count = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult(); + // Undo the rowCount projection + criteria.setProjection(null); + criteria.setResultTransformer(Criteria.ROOT_ENTITY); + return count; + } + +} diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/MaterialDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/MaterialDAO.java index 96161791a62..7ca4f5785a7 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/MaterialDAO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/MaterialDAO.java @@ -61,7 +61,15 @@ public class MaterialDAO extends AbstractGenericEntityDAO<MaterialPE> implements final Criteria criteria = getSession().createCriteria(ENTITY_CLASS); criteria.add(Restrictions.eq("materialType", materialType)); - criteria.setFetchMode("materialProperties", FetchMode.JOIN); + final int count = DAOUtils.getCount(criteria); + if (count <= DAOUtils.MAX_COUNT_FOR_PROPERTIES) + { + criteria.setFetchMode("materialProperties", FetchMode.JOIN); + } else + { + operationLog.info(String.format("Found %d materials, disable properties loading.", + count)); + } criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); final List<MaterialPE> list = cast(criteria.list()); if (operationLog.isDebugEnabled()) diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java index 47e4276f2a1..9f3c90fa5b0 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java @@ -24,7 +24,6 @@ import org.hibernate.Criteria; import org.hibernate.FetchMode; import org.hibernate.SessionFactory; import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.hibernate.validator.ClassValidator; import org.springframework.dao.DataAccessException; @@ -52,11 +51,6 @@ public class SampleDAO extends AbstractGenericEntityDAO<SamplePE> implements ISa { private final static Class<SamplePE> ENTITY_CLASS = SamplePE.class; - /** - * Don't try to get properties for more than 1000 samples. - */ - private final static int MAX_COUNT_FOR_PROPERTIES = 1000; - /** * This logger does not output any SQL statement. If you want to do so, you had better set an * appropriate debugging level for class {@link JdbcAccessor}. </p> @@ -105,11 +99,11 @@ public class SampleDAO extends AbstractGenericEntityDAO<SamplePE> implements ISa { basicCriteria.add(criterion); } - final int count = getCount(basicCriteria); + final int count = DAOUtils.getCount(basicCriteria); if (withExperimentAndProperties) { basicCriteria.setFetchMode("experimentInternal", FetchMode.JOIN); - if (count < MAX_COUNT_FOR_PROPERTIES) + if (count <= DAOUtils.MAX_COUNT_FOR_PROPERTIES) { basicCriteria.setFetchMode("sampleProperties", FetchMode.JOIN); } else @@ -122,15 +116,6 @@ public class SampleDAO extends AbstractGenericEntityDAO<SamplePE> implements ISa return cast(basicCriteria.list()); } - private int getCount(final Criteria basicCriteria) - { - int count = (Integer) basicCriteria.setProjection(Projections.rowCount()).uniqueResult(); - // Undo the rowCount projection - basicCriteria.setProjection(null); - basicCriteria.setResultTransformer(Criteria.ROOT_ENTITY); - return count; - } - private List<SamplePE> listSamplesByCriteria(boolean withExperimentAndProperties, Criterion... criterions) throws DataAccessException { -- GitLab