Search in sources :

Example 11 with ContentCacheKey

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

the class TransformationService method execute.

@RequestMapping(value = "/apply", method = POST)
@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 = GetPrepContentAsyncCondition.class, // 
resultUrlGenerator = PreparationGetContentUrlGenerator.class, // 
executionIdGeneratorClass = ExportParametersExecutionIdGenerator.class)
public StreamingResponseBody execute(@ApiParam(value = "Preparation id to apply.") @RequestBody @Valid @AsyncParameter @AsyncExecutionId final ExportParameters parameters) throws IOException {
    ExportParameters completeParameters = parameters;
    if (StringUtils.isNotEmpty(completeParameters.getPreparationId())) {
        // we deal with preparation transformation (not dataset)
        completeParameters = exportParametersUtil.populateFromPreparationExportParameter(parameters);
        ContentCacheKey cacheKey = cacheKeyGenerator.generateContentKey(completeParameters);
        if (!contentCache.has(cacheKey)) {
            preparationExportStrategy.performPreparation(completeParameters, new NullOutputStream());
        }
    }
    return executeSampleExportStrategy(completeParameters);
}
Also used : ExportParameters(org.talend.dataprep.api.export.ExportParameters) ContentCacheKey(org.talend.dataprep.cache.ContentCacheKey) NullOutputStream(org.apache.commons.io.output.NullOutputStream) VolumeMetered(org.talend.dataprep.metrics.VolumeMetered) ApiOperation(io.swagger.annotations.ApiOperation)

Example 12 with ContentCacheKey

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

the class TransformationService method evictCache.

private void evictCache(final String preparationId, final ExportParameters.SourceType sourceType) {
    final ContentCacheKey metadataKey = cacheKeyGenerator.metadataBuilder().preparationId(preparationId).sourceType(sourceType).build();
    final ContentCacheKey contentKey = cacheKeyGenerator.contentBuilder().preparationId(preparationId).sourceType(sourceType).build();
    contentCache.evictMatch(metadataKey);
    contentCache.evictMatch(contentKey);
}
Also used : ContentCacheKey(org.talend.dataprep.cache.ContentCacheKey)

Example 13 with ContentCacheKey

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

the class DatasetUpdateListener method onUpdate.

@EventListener
public void onUpdate(DatasetUpdatedEvent event) {
    // when we update a dataset we need to clean cache
    final DataSetMetadata dataSetMetadata = event.getSource();
    final ContentCacheKey sampleKey = () -> "dataset-sample_" + dataSetMetadata.getId();
    LOGGER.debug("Evicting sample cache entry for #{}", dataSetMetadata.getId());
    publisher.publishEvent(new CleanCacheEvent(sampleKey));
    LOGGER.debug("Evicting sample cache entry for #{} done.", dataSetMetadata.getId());
    LOGGER.debug("Evicting transformation cache entry for dataset #{}", dataSetMetadata.getId());
    publisher.publishEvent(new CleanCacheEvent(new ContentCacheKey() {

        @Override
        public String getKey() {
            return dataSetMetadata.getId();
        }

        @Override
        public Predicate<String> getMatcher() {
            String regex = ".*_" + getKey() + "_.*";
            // Build regular expression matcher
            final Pattern pattern = Pattern.compile(regex);
            return str -> pattern.matcher(str).matches();
        }
    }, Boolean.TRUE));
    LOGGER.debug("Evicting transformation cache entry for dataset  #{} done.", dataSetMetadata.getId());
}
Also used : Component(org.springframework.stereotype.Component) Logger(org.slf4j.Logger) ContentCacheKey(org.talend.dataprep.cache.ContentCacheKey) Predicate(java.util.function.Predicate) LoggerFactory(org.slf4j.LoggerFactory) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Autowired(org.springframework.beans.factory.annotation.Autowired) EventListener(org.springframework.context.event.EventListener) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) Pattern(java.util.regex.Pattern) DatasetUpdatedEvent(org.talend.dataprep.dataset.event.DatasetUpdatedEvent) Pattern(java.util.regex.Pattern) ContentCacheKey(org.talend.dataprep.cache.ContentCacheKey) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) EventListener(org.springframework.context.event.EventListener)

Example 14 with ContentCacheKey

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

the class TransformationServiceTest method testEvictPreparationCache.

@Test
public void testEvictPreparationCache() throws Exception {
    // given
    final String preparationId = "prepId";
    final ContentCacheKey metadataKey = cacheKeyGenerator.metadataBuilder().preparationId(preparationId).stepId("step1").sourceType(FILTER).build();
    final ContentCacheKey contentKey = cacheKeyGenerator.contentBuilder().datasetId("datasetId").preparationId(preparationId).stepId("step1").format(JSON).parameters(emptyMap()).sourceType(FILTER).build();
    try (final OutputStream entry = contentCache.put(metadataKey, PERMANENT)) {
        entry.write("metadata".getBytes());
        entry.flush();
    }
    try (final OutputStream entry = contentCache.put(contentKey, PERMANENT)) {
        entry.write("content".getBytes());
        entry.flush();
    }
    assertThat(contentCache.has(metadataKey), is(true));
    assertThat(contentCache.has(contentKey), is(true));
    // when
    // 
    given().expect().statusCode(200).log().ifError().when().delete("/preparation/{preparationId}/cache", // 
    preparationId).asString();
    // then
    assertThat(contentCache.has(metadataKey), is(false));
    assertThat(contentCache.has(contentKey), is(false));
}
Also used : ContentCacheKey(org.talend.dataprep.cache.ContentCacheKey) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 15 with ContentCacheKey

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

the class CacheCondition method apply.

@Override
public boolean apply(Object... args) {
    // check pre-condition
    Validate.notNull(args);
    Validate.isTrue(args.length == 1);
    Validate.isInstanceOf(ContentCacheKey.class, args[0]);
    ContentCacheKey cacheKey = (ContentCacheKey) args[0];
    return contentCache.has(cacheKey);
}
Also used : ContentCacheKey(org.talend.dataprep.cache.ContentCacheKey)

Aggregations

ContentCacheKey (org.talend.dataprep.cache.ContentCacheKey)28 Test (org.junit.Test)20 ServiceBaseTest (org.talend.ServiceBaseTest)14 TransformationMetadataCacheKey (org.talend.dataprep.cache.TransformationMetadataCacheKey)4 OutputStream (java.io.OutputStream)3 ArrayList (java.util.ArrayList)3 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)3 TDPException (org.talend.dataprep.exception.TDPException)3 ApiOperation (io.swagger.annotations.ApiOperation)2 InputStream (java.io.InputStream)2 Predicate (java.util.function.Predicate)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Component (org.springframework.stereotype.Component)2 TransformationCacheKey (org.talend.dataprep.cache.TransformationCacheKey)2 Timed (org.talend.dataprep.metrics.Timed)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 IOException (java.io.IOException)1 Long.parseLong (java.lang.Long.parseLong)1