Search in sources :

Example 11 with CacheKey

use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.

the class ReliableResourceDownloadManagerTest method testDownloadWithCaching.

@Test
public //@Ignore
void testDownloadWithCaching() throws Exception {
    mis = new MockInputStream(productInputFilename);
    Metacard metacard = getMockMetacard(EXPECTED_METACARD_ID, EXPECTED_METACARD_SOURCE_ID);
    resourceResponse = getMockResourceResponse();
    ResourceRetriever retriever = mock(ResourceRetriever.class);
    when(retriever.retrieveResource()).thenReturn(resourceResponse);
    CacheKey cacheKey = new CacheKey(metacard, resourceResponse.getRequest());
    String key = cacheKey.generateKey();
    when(resourceCache.isPending(key)).thenReturn(false);
    int chunkSize = 50;
    startDownload(true, chunkSize, false, metacard, retriever);
    ByteArrayOutputStream clientBytesRead = clientRead(chunkSize, productInputStream);
    // Captures the ReliableResource object that should have been put in the ResourceCacheImpl's map
    ArgumentCaptor<ReliableResource> argument = ArgumentCaptor.forClass(ReliableResource.class);
    verify(resourceCache).put(argument.capture());
    verifyCaching(argument.getValue(), EXPECTED_CACHE_KEY);
    verifyClientBytesRead(clientBytesRead);
    cleanup();
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) Metacard(ddf.catalog.data.Metacard) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheKey(ddf.catalog.cache.impl.CacheKey) ReliableResource(ddf.catalog.resource.data.ReliableResource) Test(org.junit.Test)

Example 12 with CacheKey

use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.

the class ReliableResourceDownloader method setupDownload.

public ResourceResponse setupDownload(Metacard metacard, DownloadStatusInfo downloadStatusInfo) {
    Resource resource = resourceResponse.getResource();
    MimeType mimeType = resource.getMimeType();
    String resourceName = resource.getName();
    fbos = new FileBackedOutputStream(DEFAULT_FILE_BACKED_OUTPUT_STREAM_THRESHOLD);
    countingFbos = new CountingOutputStream(fbos);
    streamReadByClient = new ReliableResourceInputStream(fbos, countingFbos, downloadState, downloadIdentifier, resourceResponse);
    this.metacard = metacard;
    // Create new ResourceResponse to return that will encapsulate the
    // ReliableResourceInputStream that will be read by the client simultaneously as the product
    // is cached to disk (if caching is enabled)
    ResourceImpl newResource = new ResourceImpl(streamReadByClient, mimeType, resourceName);
    resourceResponse = new ResourceResponseImpl(resourceResponse.getRequest(), resourceResponse.getProperties(), newResource);
    // Get handle to retrieved product's InputStream
    resourceInputStream = resource.getInputStream();
    eventListener.setDownloadMap(downloadIdentifier, resourceResponse);
    downloadStatusInfo.addDownloadInfo(downloadIdentifier, this, resourceResponse);
    if (downloaderConfig.isCacheEnabled()) {
        CacheKey keyMaker = null;
        String key = null;
        try {
            keyMaker = new CacheKey(metacard, resourceResponse.getRequest());
            key = keyMaker.generateKey();
        } catch (IllegalArgumentException e) {
            LOGGER.info("Cannot create cache key for resource with metacard ID = {}", metacard.getId());
            return resourceResponse;
        }
        if (!resourceCache.isPending(key)) {
            // Fully qualified path to cache file that will be written to.
            // Example:
            // <INSTALL-DIR>/data/product-cache/<source-id>-<metacard-id>
            // <INSTALL-DIR>/data/product-cache/ddf.distribution-abc123
            filePath = FilenameUtils.concat(resourceCache.getProductCacheDirectory(), key);
            if (filePath == null) {
                LOGGER.info("Unable to create cache for cache directory {} and key {} - no caching will be done.", resourceCache.getProductCacheDirectory(), key);
                return resourceResponse;
            }
            reliableResource = new ReliableResource(key, filePath, mimeType, resourceName, metacard);
            resourceCache.addPendingCacheEntry(reliableResource);
            try {
                fos = FileUtils.openOutputStream(new File(filePath));
                doCaching = true;
                this.downloadState.setCacheEnabled(true);
            } catch (IOException e) {
                LOGGER.info("Unable to open cache file {} - no caching will be done.", filePath);
            }
        } else {
            LOGGER.debug("Cache key {} is already pending caching", key);
        }
    }
    return resourceResponse;
}
Also used : ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) IOException(java.io.IOException) MimeType(javax.activation.MimeType) ReliableResource(ddf.catalog.resource.data.ReliableResource) CountingOutputStream(com.google.common.io.CountingOutputStream) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) File(java.io.File) CacheKey(ddf.catalog.cache.impl.CacheKey)

Example 13 with CacheKey

use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.

the class MetacardResourceSizePlugin method process.

@Override
public QueryResponse process(QueryResponse input) throws PluginExecutionException, StopProcessingException {
    List<Result> results = input.getResults();
    for (Result result : results) {
        Metacard metacard = result.getMetacard();
        if (metacard != null) {
            // Can only search cache based on Metacard - no way to generate ResourceRequest with
            // any properties for use in generating the CacheKey
            final ResourceRequest resourceRequest = new ResourceRequestById(metacard.getId());
            CacheKey cacheKey;
            String key = null;
            ReliableResource cachedResource = null;
            try {
                cacheKey = new CacheKey(metacard, resourceRequest);
                ClassLoader tccl = Thread.currentThread().getContextClassLoader();
                key = cacheKey.generateKey();
                try {
                    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
                    cachedResource = (ReliableResource) cache.getValid(key, metacard);
                } finally {
                    Thread.currentThread().setContextClassLoader(tccl);
                }
            } catch (IllegalArgumentException e) {
                LOGGER.debug("Unable to retrieve cached resource for metacard id = {}", metacard.getId());
            }
            if (cachedResource != null) {
                long resourceSize = cachedResource.getSize();
                if (resourceSize > 0 && cachedResource.hasProduct()) {
                    LOGGER.debug("Setting resourceSize = {} for metacard ID = {}", resourceSize, metacard.getId());
                    Attribute resourceSizeAttribute = new AttributeImpl(Metacard.RESOURCE_SIZE, String.valueOf(resourceSize));
                    metacard.setAttribute(resourceSizeAttribute);
                } else {
                    LOGGER.debug("resourceSize <= 0 for metacard ID = {}", metacard.getId());
                }
            } else {
                LOGGER.debug("No cached resource for cache key = {}", key);
            }
        }
    }
    return input;
}
Also used : Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ReliableResource(ddf.catalog.resource.data.ReliableResource) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ResourceRequest(ddf.catalog.operation.ResourceRequest) CacheKey(ddf.catalog.cache.impl.CacheKey)

Example 14 with CacheKey

use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.

the class CacheKeyTest method testKeyUniquenessFromSources.

@Test
public void testKeyUniquenessFromSources() {
    CacheKey cacheKey1 = new CacheKey(getMetacardStub("sampleId", "source1"), getResourceRequestStub());
    CacheKey cacheKey2 = new CacheKey(getMetacardStub("sampleId", "source2"), getResourceRequestStub());
    String key1 = cacheKey1.generateKey();
    String key2 = cacheKey2.generateKey();
    assertThat("Keys must be different.", key1, not(equalTo(key2)));
}
Also used : CacheKey(ddf.catalog.cache.impl.CacheKey) Test(org.junit.Test)

Example 15 with CacheKey

use of ddf.catalog.cache.impl.CacheKey in project ddf by codice.

the class CacheKeyTest method testKeyUniquenessMetacardId.

@Test
public void testKeyUniquenessMetacardId() {
    CacheKey cacheKey1 = new CacheKey(getMetacardStub("sampleId"), getResourceRequestStub());
    CacheKey cacheKey2 = new CacheKey(getMetacardStub("sampledI"), getResourceRequestStub());
    String key1 = cacheKey1.generateKey();
    String key2 = cacheKey2.generateKey();
    assertThat("Keys must be different.", key1, not(equalTo(key2)));
}
Also used : CacheKey(ddf.catalog.cache.impl.CacheKey) Test(org.junit.Test)

Aggregations

CacheKey (ddf.catalog.cache.impl.CacheKey)20 Test (org.junit.Test)17 Metacard (ddf.catalog.data.Metacard)7 MockInputStream (ddf.catalog.cache.MockInputStream)6 ResourceRetriever (ddf.catalog.resourceretriever.ResourceRetriever)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ReliableResource (ddf.catalog.resource.data.ReliableResource)4 Ignore (org.junit.Ignore)4 Serializable (java.io.Serializable)3 HashMap (java.util.HashMap)3 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)2 Resource (ddf.catalog.resource.Resource)2 IOException (java.io.IOException)2 CountingOutputStream (com.google.common.io.CountingOutputStream)1 FileBackedOutputStream (com.google.common.io.FileBackedOutputStream)1 Attribute (ddf.catalog.data.Attribute)1 Result (ddf.catalog.data.Result)1 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)1 ResourceRequest (ddf.catalog.operation.ResourceRequest)1 ResourceResponse (ddf.catalog.operation.ResourceResponse)1