use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class PostgreSqlRepository method forEachBatched.
@Override
public void forEachBatched(Fetch fetch, Consumer<List<Entity>> consumer, int batchSize) {
final Stopwatch stopwatch = createStarted();
final JdbcTemplate template = new JdbcTemplate(dataSource);
template.setFetchSize(batchSize);
final Query<Entity> query = new QueryImpl<>();
if (fetch != null) {
query.fetch(fetch);
}
final String allRowsSelect = getSqlSelect(entityType, query, emptyList(), false);
LOG.debug("Fetching [{}] data...", getName());
LOG.trace("SQL: {}", allRowsSelect);
RowMapper<Entity> rowMapper = postgreSqlEntityFactory.createRowMapper(entityType, fetch);
template.query(allRowsSelect, (ResultSetExtractor<Object>) resultSet -> processResultSet(consumer, batchSize, entityType, rowMapper, resultSet));
LOG.debug("Streamed entire repository in batches of size {} in {}.", batchSize, stopwatch);
}
use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class PostgreSqlQueryGeneratorTest method getSqlWhere.
@Test
public void getSqlWhere() {
Package eric = createPackage("eu_bbmri_eric");
Attribute collectionsIdAttribute = createIdAttribute("collectionsId");
EntityType collectionsEntity = createMockEntityWithIdAttribute("eu_bbmri_eric_collections", collectionsIdAttribute, "collectionsId");
Attribute typeIdAttribute = createIdAttribute("typeId");
EntityType typeEntity = createMockEntityWithIdAttribute("eu_bbmri_eric_type", typeIdAttribute, "typeId");
Attribute categoryIdAttribute = createIdAttribute("categoryId");
EntityType categoryEntity = createMockEntityWithIdAttribute("eu_bbmri_eric_category", categoryIdAttribute, "categoryId");
Attribute typeAttribute = createMrefAttribute("type", typeEntity);
Attribute categoryAttribute = createMrefAttribute("data_categories", categoryEntity);
when(collectionsEntity.getPackage()).thenReturn(eric);
when(collectionsEntity.getAttribute("type")).thenReturn(typeAttribute);
when(collectionsEntity.getAttribute("data_categories")).thenReturn(categoryAttribute);
when(collectionsEntity.getAtomicAttributes()).thenReturn(asList(collectionsIdAttribute, typeAttribute, categoryAttribute));
QueryRule typeRule = new QueryRule(NESTED, newArrayList(new QueryRule("type", EQUALS, "POPULATION_BASED"), new QueryRule(OR), new QueryRule("type", EQUALS, "QUALITY_CONTROL")));
QueryRule categoryRule = new QueryRule(NESTED, newArrayList(new QueryRule("data_categories", EQUALS, "MEDICAL_RECORDS"), new QueryRule(OR), new QueryRule("data_categories", EQUALS, "NATIONAL_REGISTRIES")));
QueryImpl<Entity> q = new QueryImpl<>(new QueryRule(NESTED, newArrayList(newArrayList(typeRule, new QueryRule(AND), categoryRule))));
List<Object> parameters = Lists.newArrayList();
String sqlWhere = PostgreSqlQueryGenerator.getSqlWhere(collectionsEntity, q, parameters, new AtomicInteger());
assertEquals(sqlWhere, "((\"type_filter1\".\"type\" = ? OR \"type_filter2\".\"type\" = ?)" + " AND " + "(\"data_categories_filter3\".\"data_categories\" = ? OR \"data_categories_filter4\".\"data_categories\" = ?))");
}
use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class IndexBootstrapperTest method testStartupFailedIndexJobsUnknownEntityType.
@Test
public void testStartupFailedIndexJobsUnknownEntityType() {
when(indexService.hasIndex(attributeMetadata)).thenReturn(true);
IndexJobExecution indexJobExecution = mock(IndexJobExecution.class);
when(indexJobExecution.getIndexActionJobID()).thenReturn("id");
IndexAction action = mock(IndexAction.class);
when(action.getEntityTypeId()).thenReturn("myEntityTypeName");
when(action.getEntityId()).thenReturn("1");
EntityType entityType = mock(EntityType.class);
when(dataService.findOneById(EntityTypeMetadata.ENTITY_TYPE_META_DATA, "myEntityTypeName", EntityType.class)).thenReturn(null);
Attribute idAttribute = mock(Attribute.class);
when(idAttribute.getDataType()).thenReturn(AttributeType.INT);
when(entityType.getIdAttribute()).thenReturn(idAttribute);
when(dataService.findAll(IndexJobExecutionMeta.INDEX_JOB_EXECUTION, new QueryImpl<IndexJobExecution>().eq(JobExecutionMetaData.STATUS, FAILED), IndexJobExecution.class)).thenReturn(Stream.of(indexJobExecution));
when(dataService.findAll(IndexActionMetaData.INDEX_ACTION, new QueryImpl<IndexAction>().eq(IndexActionMetaData.INDEX_ACTION_GROUP_ATTR, "id"), IndexAction.class)).thenReturn(Stream.of(action));
indexBootstrapper.bootstrap();
// verify that we are not passing through the "missing index" code
verify(metaDataService, never()).getRepositories();
// verify that a new job is registered for the failed one
verify(indexActionRegisterService, times(0)).register(entityType, 1);
}
use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class IndexBootstrapperTest method testStartupFailedIndexJobs.
@Test
public void testStartupFailedIndexJobs() {
when(indexService.hasIndex(attributeMetadata)).thenReturn(true);
IndexJobExecution indexJobExecution = mock(IndexJobExecution.class);
when(indexJobExecution.getIndexActionJobID()).thenReturn("id");
IndexAction action = mock(IndexAction.class);
when(action.getEntityTypeId()).thenReturn("myEntityTypeName");
when(action.getEntityId()).thenReturn("1");
EntityType entityType = mock(EntityType.class);
when(dataService.findOneById(EntityTypeMetadata.ENTITY_TYPE_META_DATA, "myEntityTypeName", EntityType.class)).thenReturn(entityType);
Attribute idAttribute = mock(Attribute.class);
when(idAttribute.getDataType()).thenReturn(AttributeType.INT);
when(entityType.getIdAttribute()).thenReturn(idAttribute);
when(dataService.findAll(IndexJobExecutionMeta.INDEX_JOB_EXECUTION, new QueryImpl<IndexJobExecution>().eq(JobExecutionMetaData.STATUS, FAILED), IndexJobExecution.class)).thenReturn(Stream.of(indexJobExecution));
when(dataService.findAll(IndexActionMetaData.INDEX_ACTION, new QueryImpl<IndexAction>().eq(IndexActionMetaData.INDEX_ACTION_GROUP_ATTR, "id"), IndexAction.class)).thenReturn(Stream.of(action));
indexBootstrapper.bootstrap();
// verify that we are not passing through the "missing index" code
verify(metaDataService, never()).getRepositories();
// verify that a new job is registered for the failed one
verify(indexActionRegisterService).register(entityType, 1);
}
use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class IndexJobSchedulerImpl method scheduleIndexJob.
@Override
@RunAsSystem
public void scheduleIndexJob(String transactionId) {
LOG.trace("Index transaction with id {}...", transactionId);
IndexActionGroup indexActionGroup = dataService.findOneById(INDEX_ACTION_GROUP, transactionId, IndexActionGroup.class);
if (indexActionGroup != null) {
Stream<Entity> indexActions = dataService.findAll(INDEX_ACTION, new QueryImpl<>().eq(INDEX_ACTION_GROUP_ATTR, indexActionGroup));
Map<String, Long> numberOfActionsPerEntity = indexActions.collect(groupingBy(indexAction -> indexAction.getString(ENTITY_TYPE_ID), counting()));
indexStatus.addActionCounts(numberOfActionsPerEntity);
IndexJobExecution indexJobExecution = indexJobExecutionFactory.create();
indexJobExecution.setUser("admin");
indexJobExecution.setIndexActionJobID(transactionId);
jobExecutor.submit(indexJobExecution, executorService).whenComplete((a, b) -> indexStatus.removeActionCounts(numberOfActionsPerEntity));
} else {
LOG.debug("No index job found for id [{}].", transactionId);
}
}
Aggregations