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;
}
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;
}
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;
});
}
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();
}
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);
}
Aggregations