Search in sources :

Example 1 with TransformationMetadataCacheKey

use of org.talend.dataprep.cache.TransformationMetadataCacheKey in project data-prep by Talend.

the class PrepMetadataCacheCondition method apply.

@Override
public boolean apply(Object... args) {
    // check pre-condition
    Validate.notNull(args);
    Validate.isTrue(args.length == 2);
    Validate.isInstanceOf(String.class, args[0]);
    Validate.isInstanceOf(String.class, args[1]);
    try {
        String preparationId = (String) args[0];
        String headId = (String) args[1];
        ExportParameters exportParameters = new ExportParameters();
        exportParameters.setPreparationId(preparationId);
        exportParameters.setStepId(headId);
        exportParameters = exportParametersUtil.populateFromPreparationExportParameter(exportParameters);
        final TransformationMetadataCacheKey cacheKey = cacheKeyGenerator.generateMetadataKey(exportParameters.getPreparationId(), exportParameters.getStepId(), HEAD);
        return cacheCondition.apply(cacheKey);
    } catch (IOException e) {
        LOGGER.error("Cannot get all information from export parameters", e);
    }
    return false;
}
Also used : ExportParameters(org.talend.dataprep.api.export.ExportParameters) TransformationMetadataCacheKey(org.talend.dataprep.cache.TransformationMetadataCacheKey) IOException(java.io.IOException)

Example 2 with TransformationMetadataCacheKey

use of org.talend.dataprep.cache.TransformationMetadataCacheKey in project data-prep by Talend.

the class TransformationMetadataCacheKeyTest method getKey_should_generate_serialized_key.

@Test
public void getKey_should_generate_serialized_key() throws Exception {
    // given
    final ContentCacheKey key = new TransformationMetadataCacheKey("prep1", "step1", HEAD, "user1");
    // when
    final String keyStr = key.getKey();
    // then
    assertThat(keyStr, is("transformation-metadata_prep1_step1_HEAD_user1"));
}
Also used : ContentCacheKey(org.talend.dataprep.cache.ContentCacheKey) TransformationMetadataCacheKey(org.talend.dataprep.cache.TransformationMetadataCacheKey) Test(org.junit.Test)

Example 3 with TransformationMetadataCacheKey

use of org.talend.dataprep.cache.TransformationMetadataCacheKey in project data-prep by Talend.

the class TransformationMetadataCacheKeyTest method getMatcher_should_return_matcher_for_partial_key.

@Test
public void getMatcher_should_return_matcher_for_partial_key() throws Exception {
    // given
    final ContentCacheKey prepKey = new TransformationMetadataCacheKey("prep1", null, null, null);
    final ContentCacheKey stepKey = new TransformationMetadataCacheKey(null, "step1", null, null);
    final ContentCacheKey sourceKey = new TransformationMetadataCacheKey(null, null, HEAD, null);
    final ContentCacheKey userKey = new TransformationMetadataCacheKey(null, null, null, "user1");
    final ContentCacheKey matchingKey = new TransformationMetadataCacheKey("prep1", "step1", HEAD, "user1");
    final ContentCacheKey nonMatchingKey = new TransformationMetadataCacheKey("prep2", "step2", FILTER, "user2");
    // when / then
    assertThat(prepKey.getMatcher().test(matchingKey.getKey()), is(true));
    assertThat(stepKey.getMatcher().test(matchingKey.getKey()), is(true));
    assertThat(sourceKey.getMatcher().test(matchingKey.getKey()), is(true));
    assertThat(userKey.getMatcher().test(matchingKey.getKey()), is(true));
    assertThat(prepKey.getMatcher().test(nonMatchingKey.getKey()), is(false));
    assertThat(stepKey.getMatcher().test(nonMatchingKey.getKey()), is(false));
    assertThat(sourceKey.getMatcher().test(nonMatchingKey.getKey()), is(false));
    assertThat(userKey.getMatcher().test(nonMatchingKey.getKey()), is(false));
}
Also used : ContentCacheKey(org.talend.dataprep.cache.ContentCacheKey) TransformationMetadataCacheKey(org.talend.dataprep.cache.TransformationMetadataCacheKey) Test(org.junit.Test)

Example 4 with TransformationMetadataCacheKey

use of org.talend.dataprep.cache.TransformationMetadataCacheKey in project data-prep by Talend.

the class TransformationService method executeMetadata.

@RequestMapping(value = "/apply/preparation/{preparationId}/{stepId}/metadata", method = GET)
@ApiOperation(value = "Run the transformation given the provided export parameters", notes = "This operation transforms the dataset or preparation using parameters in export parameters.")
@VolumeMetered
@AsyncOperation(// 
conditionalClass = GetPrepMetadataAsyncCondition.class, // 
resultUrlGenerator = PrepMetadataGetContentUrlGenerator.class, executionIdGeneratorClass = PrepMetadataExecutionIdGenerator.class)
public DataSetMetadata executeMetadata(@PathVariable("preparationId") @AsyncParameter String preparationId, @PathVariable("stepId") @AsyncParameter String stepId) {
    LOG.debug("getting preparation metadata for #{}, step {}", preparationId, stepId);
    final Preparation preparation = getPreparation(preparationId);
    if (preparation.getSteps().size() > 1) {
        String headId = "head".equalsIgnoreCase(stepId) ? preparation.getHeadId() : stepId;
        final TransformationMetadataCacheKey cacheKey = cacheKeyGenerator.generateMetadataKey(preparationId, headId, HEAD);
        // No metadata in cache, recompute it
        if (!contentCache.has(cacheKey)) {
            try {
                LOG.debug("Metadata not available for preparation '{}' at step '{}'", preparationId, headId);
                ExportParameters parameters = new ExportParameters();
                parameters.setPreparationId(preparationId);
                parameters.setExportType("JSON");
                parameters.setStepId(headId);
                parameters.setFrom(HEAD);
                // we regenerate cache
                parameters = exportParametersUtil.populateFromPreparationExportParameter(parameters);
                preparationExportStrategy.performPreparation(parameters, new NullOutputStream());
            } catch (Exception e) {
                throw new TDPException(TransformationErrorCodes.METADATA_NOT_FOUND, e);
            }
        }
        // }
        if (contentCache.has(cacheKey)) {
            try (InputStream stream = contentCache.get(cacheKey)) {
                return mapper.readerFor(DataSetMetadata.class).readValue(stream);
            } catch (IOException e) {
                throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
            }
        }
    } else {
        LOG.debug("No step in preparation '{}', falls back to get dataset metadata (id: {})", preparationId, preparation.getDataSetId());
        DataSetGetMetadata getMetadata = context.getBean(DataSetGetMetadata.class, preparation.getDataSetId());
        return getMetadata.execute();
    }
    return null;
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Preparation(org.talend.dataprep.api.preparation.Preparation) ExportParameters(org.talend.dataprep.api.export.ExportParameters) TransformationMetadataCacheKey(org.talend.dataprep.cache.TransformationMetadataCacheKey) TDPException(org.talend.dataprep.exception.TDPException) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) DataSetGetMetadata(org.talend.dataprep.command.dataset.DataSetGetMetadata) NullOutputStream(org.apache.commons.io.output.NullOutputStream) VolumeMetered(org.talend.dataprep.metrics.VolumeMetered) ApiOperation(io.swagger.annotations.ApiOperation)

Example 5 with TransformationMetadataCacheKey

use of org.talend.dataprep.cache.TransformationMetadataCacheKey in project data-prep by Talend.

the class TransformationService method shouldApplyDiffToSampleSource.

private boolean shouldApplyDiffToSampleSource(final PreviewParameters previewParameters) {
    if (previewParameters.getSourceType() != HEAD && previewParameters.getPreparationId() != null) {
        final TransformationMetadataCacheKey metadataKey = // 
        cacheKeyGenerator.generateMetadataKey(// 
        previewParameters.getPreparationId(), // 
        Step.ROOT_STEP.id(), // 
        previewParameters.getSourceType());
        final ContentCacheKey contentKey = // 
        cacheKeyGenerator.generateContentKey(// 
        previewParameters.getDataSetId(), // 
        previewParameters.getPreparationId(), // 
        Step.ROOT_STEP.id(), // 
        JSON, // 
        previewParameters.getSourceType(), // no filter for preview parameters
        "");
        return contentCache.has(metadataKey) && contentCache.has(contentKey);
    }
    return false;
}
Also used : ContentCacheKey(org.talend.dataprep.cache.ContentCacheKey) TransformationMetadataCacheKey(org.talend.dataprep.cache.TransformationMetadataCacheKey)

Aggregations

TransformationMetadataCacheKey (org.talend.dataprep.cache.TransformationMetadataCacheKey)7 ContentCacheKey (org.talend.dataprep.cache.ContentCacheKey)4 Test (org.junit.Test)2 DataSet (org.talend.dataprep.api.dataset.DataSet)2 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)2 RowMetadata (org.talend.dataprep.api.dataset.RowMetadata)2 ExportParameters (org.talend.dataprep.api.export.ExportParameters)2 TDPException (org.talend.dataprep.exception.TDPException)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 NullOutputStream (org.apache.commons.io.output.NullOutputStream)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Component (org.springframework.stereotype.Component)1 Preparation (org.talend.dataprep.api.preparation.Preparation)1 PreparationMessage (org.talend.dataprep.api.preparation.PreparationMessage)1