Search in sources :

Example 6 with LongType

use of org.hibernate.type.LongType in project Gemma by PavlidisLab.

the class CompositeSequenceDaoImpl method getGenes.

@Override
public Map<CompositeSequence, Collection<Gene>> getGenes(Collection<CompositeSequence> compositeSequences) {
    Map<CompositeSequence, Collection<Gene>> returnVal = new HashMap<>();
    int BATCH_SIZE = 2000;
    if (compositeSequences.size() == 0)
        return returnVal;
    /*
         * Get the cs->gene mapping
         */
    final String nativeQuery = "SELECT CS, GENE FROM GENE2CS WHERE CS IN (:csids) ";
    for (CompositeSequence cs : compositeSequences) {
        returnVal.put(cs, new HashSet<Gene>());
    }
    List<Object> csGene = new ArrayList<>();
    Session session = this.getSessionFactory().getCurrentSession();
    org.hibernate.SQLQuery queryObject = session.createSQLQuery(nativeQuery);
    queryObject.addScalar("cs", new LongType());
    queryObject.addScalar("gene", new LongType());
    Collection<Long> csIdBatch = new HashSet<>();
    for (CompositeSequence cs : compositeSequences) {
        csIdBatch.add(cs.getId());
        if (csIdBatch.size() == BATCH_SIZE) {
            queryObject.setParameterList("csids", csIdBatch);
            csGene.addAll(queryObject.list());
            session.clear();
            csIdBatch.clear();
        }
    }
    if (csIdBatch.size() > 0) {
        queryObject.setParameterList("csids", csIdBatch);
        csGene.addAll(queryObject.list());
        session.clear();
    }
    StopWatch watch = new StopWatch();
    watch.start();
    int count = 0;
    Collection<Long> genesToFetch = new HashSet<>();
    Map<Long, Collection<Long>> cs2geneIds = new HashMap<>();
    for (Object object : csGene) {
        Object[] ar = (Object[]) object;
        Long cs = (Long) ar[0];
        Long gene = (Long) ar[1];
        if (!cs2geneIds.containsKey(cs)) {
            cs2geneIds.put(cs, new HashSet<Long>());
        }
        cs2geneIds.get(cs).add(gene);
        genesToFetch.add(gene);
    }
    // nothing found?
    if (genesToFetch.size() == 0) {
        returnVal.clear();
        return returnVal;
    }
    if (AbstractDao.log.isDebugEnabled())
        AbstractDao.log.debug("Built cs -> gene map in " + watch.getTime() + " ms; fetching " + genesToFetch.size() + " genes.");
    // fetch the genes
    Collection<Long> batch = new HashSet<>();
    Collection<Gene> genes = new HashSet<>();
    String geneQuery = "from Gene g where g.id in ( :gs )";
    org.hibernate.Query geneQueryObject = this.getSessionFactory().getCurrentSession().createQuery(geneQuery).setFetchSize(1000);
    for (Long gene : genesToFetch) {
        batch.add(gene);
        if (batch.size() == BATCH_SIZE) {
            AbstractDao.log.debug("Processing batch ... ");
            geneQueryObject.setParameterList("gs", batch);
            // noinspection unchecked
            genes.addAll(geneQueryObject.list());
            batch.clear();
        }
    }
    if (batch.size() > 0) {
        geneQueryObject.setParameterList("gs", batch);
        // noinspection unchecked
        genes.addAll(geneQueryObject.list());
    }
    if (AbstractDao.log.isDebugEnabled())
        AbstractDao.log.debug("Got information on " + genes.size() + " genes in " + watch.getTime() + " ms");
    Map<Long, Gene> geneIdMap = new HashMap<>();
    for (Gene g : genes) {
        Hibernate.initialize(g);
        Long id = g.getId();
        geneIdMap.put(id, g);
    }
    // fill in the return value.
    for (CompositeSequence cs : compositeSequences) {
        Long csId = cs.getId();
        assert csId != null;
        Collection<Long> genesToAttach = cs2geneIds.get(csId);
        if (genesToAttach == null) {
            // this means there was no gene for that cs; we should remove it from the result
            returnVal.remove(cs);
            continue;
        }
        for (Long geneId : genesToAttach) {
            returnVal.get(cs).add(geneIdMap.get(geneId));
        }
        ++count;
    }
    if (AbstractDao.log.isDebugEnabled())
        AbstractDao.log.debug("Done, " + count + " result rows processed, " + returnVal.size() + "/" + compositeSequences.size() + " probes are associated with genes");
    return returnVal;
}
Also used : LongType(org.hibernate.type.LongType) org.hibernate(org.hibernate) Gene(ubic.gemma.model.genome.Gene) CompositeSequence(ubic.gemma.model.expression.designElement.CompositeSequence) StopWatch(org.apache.commons.lang3.time.StopWatch) CompositeSequenceValueObject(ubic.gemma.model.expression.designElement.CompositeSequenceValueObject)

Example 7 with LongType

use of org.hibernate.type.LongType in project gocd by gocd.

the class MaterialRepository method latestModificationRunByPipeline.

public Long latestModificationRunByPipeline(final CaseInsensitiveString pipelineName, final Material material) {
    final long materialId = findMaterialInstance(material).getId();
    String key = cacheKeyForLatestPmrForPipelineKey(materialId, pipelineName.toLower());
    Long modificationId = (Long) goCache.get(key);
    if (modificationId == null) {
        synchronized (key) {
            modificationId = (Long) goCache.get(key);
            if (modificationId == null) {
                modificationId = (Long) getHibernateTemplate().execute((HibernateCallback) session -> {
                    SQLQuery sqlQuery = session.createSQLQuery("SELECT  MAX(pmr.toRevisionId) toRevisionId " + "FROM (SELECT torevisionid, pipelineid FROM pipelineMaterialRevisions WHERE materialid = :material_id)  AS pmr\n" + "INNER JOIN pipelines p ON ( p.name = :pipeline_name AND p.id = pmr.pipelineId)");
                    sqlQuery.setParameter("material_id", materialId);
                    sqlQuery.setParameter("pipeline_name", pipelineName.toString());
                    sqlQuery.addScalar("toRevisionId", new LongType());
                    return sqlQuery.uniqueResult();
                });
                if (modificationId == null) {
                    modificationId = -1L;
                }
                goCache.put(key, modificationId);
            }
        }
    }
    return modificationId;
}
Also used : java.util(java.util) MaterialConfigConverter(com.thoughtworks.go.server.service.MaterialConfigConverter) org.hibernate(org.hibernate) LoggerFactory(org.slf4j.LoggerFactory) ExceptionUtils.bomb(com.thoughtworks.go.util.ExceptionUtils.bomb) Autowired(org.springframework.beans.factory.annotation.Autowired) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Value(org.springframework.beans.factory.annotation.Value) HibernateDaoSupport(org.springframework.orm.hibernate3.support.HibernateDaoSupport) GoCache(com.thoughtworks.go.server.cache.GoCache) Database(com.thoughtworks.go.server.database.Database) DependencyMaterialInstance(com.thoughtworks.go.domain.materials.dependency.DependencyMaterialInstance) com.thoughtworks.go.domain.materials(com.thoughtworks.go.domain.materials) CollectionUtil(com.thoughtworks.go.server.util.CollectionUtil) ModificationForPipeline(com.thoughtworks.go.server.ui.ModificationForPipeline) BigInteger(java.math.BigInteger) MaterialQueries.loadModificationQuery(com.thoughtworks.go.server.persistence.MaterialQueries.loadModificationQuery) PipelineId(com.thoughtworks.go.server.ui.PipelineId) StringType(org.hibernate.type.StringType) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) TransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TransactionSynchronizationManager) QueryExtensions(com.thoughtworks.go.server.database.QueryExtensions) LongType(org.hibernate.type.LongType) com.thoughtworks.go.domain(com.thoughtworks.go.domain) Logger(org.slf4j.Logger) Pagination(com.thoughtworks.go.server.util.Pagination) FeedModifier(com.thoughtworks.go.server.dao.FeedModifier) Restrictions.isNull(org.hibernate.criterion.Restrictions.isNull) Collectors(java.util.stream.Collectors) File(java.io.File) TestOnly(org.jetbrains.annotations.TestOnly) Restrictions.eq(org.hibernate.criterion.Restrictions.eq) Component(org.springframework.stereotype.Component) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) AbstractMaterial(com.thoughtworks.go.config.materials.AbstractMaterial) MaterialExpansionService(com.thoughtworks.go.server.service.MaterialExpansionService) SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) CacheKeyGenerator(com.thoughtworks.go.server.cache.CacheKeyGenerator) MaterialConfigs(com.thoughtworks.go.config.materials.MaterialConfigs) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Materials(com.thoughtworks.go.config.materials.Materials) org.hibernate.criterion(org.hibernate.criterion) LongType(org.hibernate.type.LongType) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString)

Example 8 with LongType

use of org.hibernate.type.LongType in project gocd by gocd.

the class MaterialRepository method findModificationsForPipelineIds.

public Map<Long, List<ModificationForPipeline>> findModificationsForPipelineIds(final List<Long> pipelineIds) {
    final int MODIFICATION = 0;
    final int RELEVANT_PIPELINE_ID = 1;
    final int RELEVANT_PIPELINE_NAME = 2;
    final int MATERIAL_TYPE = 3;
    final int MATERIAL_FINGERPRINT = 4;
    // noinspection unchecked
    return (Map<Long, List<ModificationForPipeline>>) getHibernateTemplate().execute((HibernateCallback) session -> {
        if (pipelineIds.isEmpty()) {
            return new HashMap<Long, List<ModificationForPipeline>>();
        }
        Map<PipelineId, Set<Long>> relevantToLookedUpMap = relevantToLookedUpDependencyMap(session, pipelineIds);
        SQLQuery query = session.createSQLQuery("SELECT mods.*, pmr.pipelineId as pmrPipelineId, p.name as pmrPipelineName, m.type as materialType, m.fingerprint as fingerprint" + " FROM modifications mods " + "     INNER JOIN pipelineMaterialRevisions pmr ON (mods.id >= pmr.fromRevisionId AND mods.id <= pmr.toRevisionId) AND mods.materialId = pmr.materialId " + "     INNER JOIN pipelines p ON pmr.pipelineId = p.id" + "     INNER JOIN materials m ON mods.materialId = m.id" + " WHERE pmr.pipelineId IN (:ids)");
        List<Object[]> allModifications = query.addEntity("mods", Modification.class).addScalar("pmrPipelineId", new LongType()).addScalar("pmrPipelineName", new StringType()).addScalar("materialType", new StringType()).addScalar("fingerprint", new StringType()).setParameterList("ids", relevantToLookedUpMap.keySet().stream().map(PipelineId::getPipelineId).collect(Collectors.toList())).list();
        Map<Long, List<ModificationForPipeline>> modificationsForPipeline = new HashMap<>();
        CollectionUtil.CollectionValueMap<Long, ModificationForPipeline> modsForPipeline = CollectionUtil.collectionValMap(modificationsForPipeline, new CollectionUtil.ArrayList<>());
        for (Object[] modAndPmr : allModifications) {
            Modification mod = (Modification) modAndPmr[MODIFICATION];
            Long relevantPipelineId = (Long) modAndPmr[RELEVANT_PIPELINE_ID];
            String relevantPipelineName = (String) modAndPmr[RELEVANT_PIPELINE_NAME];
            String materialType = (String) modAndPmr[MATERIAL_TYPE];
            String materialFingerprint = (String) modAndPmr[MATERIAL_FINGERPRINT];
            PipelineId relevantPipeline = new PipelineId(relevantPipelineName, relevantPipelineId);
            Set<Long> longs = relevantToLookedUpMap.get(relevantPipeline);
            for (Long lookedUpPipeline : longs) {
                modsForPipeline.put(lookedUpPipeline, new ModificationForPipeline(relevantPipeline, mod, materialType, materialFingerprint));
            }
        }
        return modificationsForPipeline;
    });
}
Also used : LongType(org.hibernate.type.LongType) StringType(org.hibernate.type.StringType) ModificationForPipeline(com.thoughtworks.go.server.ui.ModificationForPipeline) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) CollectionUtil(com.thoughtworks.go.server.util.CollectionUtil) PipelineId(com.thoughtworks.go.server.ui.PipelineId) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback)

Example 9 with LongType

use of org.hibernate.type.LongType in project gocd by gocd.

the class MaterialRepository method fromInclusiveModificationsForPipelineRange.

private List<Long> fromInclusiveModificationsForPipelineRange(Session session, String pipelineName, Integer fromCounter, Integer toCounter) {
    String pipelineIdsSql = queryExtensions.queryFromInclusiveModificationsForPipelineRange(pipelineName, fromCounter, toCounter);
    SQLQuery pipelineIdsQuery = session.createSQLQuery(pipelineIdsSql);
    final List ids = pipelineIdsQuery.list();
    if (ids.isEmpty()) {
        return new ArrayList<>();
    }
    String minMaxQuery = " SELECT mods1.materialId as materialId, min(mods1.id) as min, max(mods1.id) as max" + " FROM modifications mods1 " + "     INNER JOIN pipelineMaterialRevisions pmr ON (mods1.id >= pmr.actualFromRevisionId AND mods1.id <= pmr.toRevisionId) AND mods1.materialId = pmr.materialId " + " WHERE pmr.pipelineId IN (:ids) " + " GROUP BY mods1.materialId";
    SQLQuery query = session.createSQLQuery("SELECT mods.id " + " FROM modifications mods" + "     INNER JOIN (" + minMaxQuery + ") as edges on edges.materialId = mods.materialId and mods.id >= min and mods.id <= max" + " ORDER BY mods.materialId ASC, mods.id DESC");
    query.addScalar("id", new LongType());
    query.setParameterList("ids", ids);
    return query.list();
}
Also used : LongType(org.hibernate.type.LongType) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString)

Example 10 with LongType

use of org.hibernate.type.LongType in project gocd by gocd.

the class MaterialRepository method relevantToLookedUpDependencyMap.

private Map<PipelineId, Set<Long>> relevantToLookedUpDependencyMap(Session session, List<Long> pipelineIds) {
    final int LOOKED_UP_PIPELINE_ID = 2;
    final int RELEVANT_PIPELINE_ID = 0;
    final int RELEVANT_PIPELINE_NAME = 1;
    String pipelineIdsSql = queryExtensions.queryRelevantToLookedUpDependencyMap(pipelineIds);
    SQLQuery pipelineIdsQuery = session.createSQLQuery(pipelineIdsSql);
    pipelineIdsQuery.addScalar("id", new LongType());
    pipelineIdsQuery.addScalar("name", new StringType());
    pipelineIdsQuery.addScalar("lookedUpId", new LongType());
    final List<Object[]> ids = pipelineIdsQuery.list();
    Map<Long, List<PipelineId>> lookedUpToParentMap = new HashMap<>();
    CollectionUtil.CollectionValueMap<Long, PipelineId> lookedUpToRelevantMap = CollectionUtil.collectionValMap(lookedUpToParentMap, new CollectionUtil.ArrayList<>());
    for (Object[] relevantAndLookedUpId : ids) {
        lookedUpToRelevantMap.put((Long) relevantAndLookedUpId[LOOKED_UP_PIPELINE_ID], new PipelineId((String) relevantAndLookedUpId[RELEVANT_PIPELINE_NAME], (Long) relevantAndLookedUpId[RELEVANT_PIPELINE_ID]));
    }
    return CollectionUtil.reverse(lookedUpToParentMap);
}
Also used : LongType(org.hibernate.type.LongType) StringType(org.hibernate.type.StringType) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) CollectionUtil(com.thoughtworks.go.server.util.CollectionUtil) PipelineId(com.thoughtworks.go.server.ui.PipelineId)

Aggregations

LongType (org.hibernate.type.LongType)12 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)5 StringType (org.hibernate.type.StringType)5 org.hibernate (org.hibernate)4 PipelineId (com.thoughtworks.go.server.ui.PipelineId)3 CollectionUtil (com.thoughtworks.go.server.util.CollectionUtil)3 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)3 ModificationForPipeline (com.thoughtworks.go.server.ui.ModificationForPipeline)2 ResultType (org.broadleafcommerce.common.extension.ResultType)2 Type (org.hibernate.type.Type)2 AbstractMaterial (com.thoughtworks.go.config.materials.AbstractMaterial)1 MaterialConfigs (com.thoughtworks.go.config.materials.MaterialConfigs)1 Materials (com.thoughtworks.go.config.materials.Materials)1 com.thoughtworks.go.domain (com.thoughtworks.go.domain)1 com.thoughtworks.go.domain.materials (com.thoughtworks.go.domain.materials)1 DependencyMaterialInstance (com.thoughtworks.go.domain.materials.dependency.DependencyMaterialInstance)1 CacheKeyGenerator (com.thoughtworks.go.server.cache.CacheKeyGenerator)1 GoCache (com.thoughtworks.go.server.cache.GoCache)1 FeedModifier (com.thoughtworks.go.server.dao.FeedModifier)1 Database (com.thoughtworks.go.server.database.Database)1