Search in sources :

Example 41 with QueryImpl

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);
}
Also used : BatchingQueryResult(org.molgenis.data.support.BatchingQueryResult) PostgreSqlQueryUtils.getTableAttributes(org.molgenis.data.postgresql.PostgreSqlQueryUtils.getTableAttributes) RepositoryCapability(org.molgenis.data.RepositoryCapability) java.util(java.util) PostgreSqlQueryGenerator(org.molgenis.data.postgresql.PostgreSqlQueryGenerator) Stopwatch(com.google.common.base.Stopwatch) Operator(org.molgenis.data.QueryRule.Operator) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) LoggerFactory(org.slf4j.LoggerFactory) ONE_TO_MANY(org.molgenis.data.meta.AttributeType.ONE_TO_MANY) VALUE_TOO_LONG_MSG(org.molgenis.data.postgresql.PostgreSqlExceptionTranslator.VALUE_TOO_LONG_MSG) Fetch(org.molgenis.data.Fetch) QueryImpl(org.molgenis.data.support.QueryImpl) Attribute(org.molgenis.data.meta.model.Attribute) SQLException(java.sql.SQLException) AbstractRepository(org.molgenis.data.support.AbstractRepository) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResultSet(java.sql.ResultSet) Objects.requireNonNull(java.util.Objects.requireNonNull) DataSource(javax.sql.DataSource) org.springframework.jdbc.core(org.springframework.jdbc.core) com.google.common.collect(com.google.common.collect) AttributeType(org.molgenis.data.meta.AttributeType) PostgreSqlNameGenerator.getJunctionTableOrderColumnName(org.molgenis.data.postgresql.PostgreSqlNameGenerator.getJunctionTableOrderColumnName) Logger(org.slf4j.Logger) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Collections.emptyList(java.util.Collections.emptyList) PostgreSqlQueryUtils.getJunctionTableAttributes(org.molgenis.data.postgresql.PostgreSqlQueryUtils.getJunctionTableAttributes) PreparedStatement(java.sql.PreparedStatement) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) PostgreSqlUtils.getPostgreSqlValue(org.molgenis.data.postgresql.PostgreSqlUtils.getPostgreSqlValue) String.format(java.lang.String.format) EntityTypeUtils.isMultipleReferenceType(org.molgenis.data.support.EntityTypeUtils.isMultipleReferenceType) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Consumer(java.util.function.Consumer) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) Stopwatch.createStarted(com.google.common.base.Stopwatch.createStarted) StreamSupport.stream(java.util.stream.StreamSupport.stream) Query(org.molgenis.data.Query) Entity(org.molgenis.data.Entity) Entity(org.molgenis.data.Entity) QueryImpl(org.molgenis.data.support.QueryImpl) Stopwatch(com.google.common.base.Stopwatch)

Example 42 with QueryImpl

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\" = ?))");
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) QueryImpl(org.molgenis.data.support.QueryImpl) Attribute(org.molgenis.data.meta.model.Attribute) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Package(org.molgenis.data.meta.model.Package) Test(org.testng.annotations.Test)

Example 43 with QueryImpl

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);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) QueryImpl(org.molgenis.data.support.QueryImpl) Attribute(org.molgenis.data.meta.model.Attribute) IndexJobExecution(org.molgenis.data.index.job.IndexJobExecution) IndexAction(org.molgenis.data.index.meta.IndexAction) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 44 with QueryImpl

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);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) QueryImpl(org.molgenis.data.support.QueryImpl) Attribute(org.molgenis.data.meta.model.Attribute) IndexJobExecution(org.molgenis.data.index.job.IndexJobExecution) IndexAction(org.molgenis.data.index.meta.IndexAction) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 45 with QueryImpl

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);
    }
}
Also used : JobExecutor(org.molgenis.jobs.JobExecutor) STATUS(org.molgenis.jobs.model.JobExecutionMetaData.STATUS) LoggerFactory(org.slf4j.LoggerFactory) Collectors.counting(java.util.stream.Collectors.counting) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) RunAsSystemAspect.runAsSystem(org.molgenis.security.core.runas.RunAsSystemAspect.runAsSystem) QueryImpl(org.molgenis.data.support.QueryImpl) Scheduled(org.springframework.scheduling.annotation.Scheduled) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem) SUCCESS(org.molgenis.jobs.model.JobExecution.Status.SUCCESS) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ExecutorService(java.util.concurrent.ExecutorService) IndexActionGroup(org.molgenis.data.index.meta.IndexActionGroup) Logger(org.slf4j.Logger) INDEX_ACTION_GROUP(org.molgenis.data.index.meta.IndexActionGroupMetaData.INDEX_ACTION_GROUP) END_DATE(org.molgenis.jobs.model.JobExecutionMetaData.END_DATE) IndexActionMetaData(org.molgenis.data.index.meta.IndexActionMetaData) Instant(java.time.Instant) EntityType(org.molgenis.data.meta.model.EntityType) Executors(java.util.concurrent.Executors) ChronoUnit(java.time.temporal.ChronoUnit) Stream(java.util.stream.Stream) DataService(org.molgenis.data.DataService) Entity(org.molgenis.data.Entity) IndexActionGroup(org.molgenis.data.index.meta.IndexActionGroup) Entity(org.molgenis.data.Entity) QueryImpl(org.molgenis.data.support.QueryImpl) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Aggregations

QueryImpl (org.molgenis.data.support.QueryImpl)98 Test (org.testng.annotations.Test)70 DynamicEntity (org.molgenis.data.support.DynamicEntity)37 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)36 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)36 EntityType (org.molgenis.data.meta.model.EntityType)28 Attribute (org.molgenis.data.meta.model.Attribute)25 Entity (org.molgenis.data.Entity)15 WithMockUser (org.springframework.security.test.context.support.WithMockUser)8 Stream (java.util.stream.Stream)7 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)7 AggregateQueryImpl (org.molgenis.data.support.AggregateQueryImpl)7 Objects.requireNonNull (java.util.Objects.requireNonNull)6 QueryRule (org.molgenis.data.QueryRule)6 AggregateQuery (org.molgenis.data.aggregation.AggregateQuery)6 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 Instant (java.time.Instant)5 LocalDate (java.time.LocalDate)5 Operator (org.molgenis.data.QueryRule.Operator)5