Search in sources :

Example 21 with ReliableResource

use of ddf.catalog.resource.data.ReliableResource in project ddf by codice.

the class ProductCacheDirListener method entryAdded.

@Override
public synchronized void entryAdded(EntryEvent<K, V> event) {
    V value = event.getValue();
    if (value.getClass().isAssignableFrom(ReliableResource.class)) {
        ReliableResource resource = (ReliableResource) value;
        LOGGER.debug("entry added event triggered: {}", resource.getKey());
        long currentCacheDirSize = cacheDirSize.addAndGet(resource.getSize());
        if (maxDirSizeBytes > 0 && maxDirSizeBytes < currentCacheDirSize) {
            PagingPredicate pp = new PagingPredicate(new ReliableResourceComparator(), DEFAULT_PAGE_SIZE);
            Collection<ReliableResource> lruResourceEntries = map.values(pp);
            Iterator<ReliableResource> itr = lruResourceEntries.iterator();
            while (maxDirSizeBytes < currentCacheDirSize) {
                if (itr.hasNext()) {
                    ReliableResource rr = itr.next();
                    deleteFromCache(map, rr);
                    currentCacheDirSize -= rr.getSize();
                } else {
                    pp.nextPage();
                    lruResourceEntries = map.values(pp);
                    itr = lruResourceEntries.iterator();
                }
            }
        }
    }
}
Also used : PagingPredicate(com.hazelcast.query.PagingPredicate) ReliableResourceComparator(ddf.catalog.resource.data.ReliableResourceComparator) ReliableResource(ddf.catalog.resource.data.ReliableResource)

Example 22 with ReliableResource

use of ddf.catalog.resource.data.ReliableResource in project ddf by codice.

the class ProductCacheDirListener method entryEvicted.

@Override
public void entryEvicted(EntryEvent<K, V> event) {
    V value = event.getValue();
    if (value.getClass().isAssignableFrom(ReliableResource.class)) {
        ReliableResource resource = (ReliableResource) value;
        LOGGER.debug("entry evicted event triggered: {}", resource.getKey());
        cacheDirSize.addAndGet(-resource.getSize());
    }
}
Also used : ReliableResource(ddf.catalog.resource.data.ReliableResource)

Example 23 with ReliableResource

use of ddf.catalog.resource.data.ReliableResource in project ddf by codice.

the class TestMetacardResourceSizePlugin method testMetacardResourceSizePopulatedButNoProduct.

/**
     * Verifies case where product has been cached previously but has since
     * been deleted from the product-cache directory, so there is still an
     * entry in the cache map but no cache file on disk.
     *
     * @throws Exception
     */
@Test
public void testMetacardResourceSizePopulatedButNoProduct() throws Exception {
    ResourceCacheInterface cache = mock(ResourceCacheInterface.class);
    ReliableResource cachedResource = mock(ReliableResource.class);
    when(cachedResource.getSize()).thenReturn(999L);
    when(cachedResource.hasProduct()).thenReturn(false);
    when(cache.getValid(anyString(), (Metacard) anyObject())).thenReturn(cachedResource);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setId("abc123");
    metacard.setSourceId("ddf-1");
    metacard.setResourceSize("N/A");
    Result result = new ResultImpl(metacard);
    List<Result> results = new ArrayList<Result>();
    results.add(result);
    QueryResponse input = mock(QueryResponse.class);
    when(input.getResults()).thenReturn(results);
    MetacardResourceSizePlugin plugin = new MetacardResourceSizePlugin(cache);
    QueryResponse queryResponse = plugin.process(input);
    assertThat(queryResponse.getResults().size(), is(1));
    Metacard resultMetacard = queryResponse.getResults().get(0).getMetacard();
    assertThat(metacard, is(notNullValue()));
    // Since using Metacard vs. MetacardImpl have to get resource-size as an
    // Attribute vs. String
    Attribute resourceSizeAttr = resultMetacard.getAttribute(Metacard.RESOURCE_SIZE);
    assertThat((String) resourceSizeAttr.getValue(), equalTo("N/A"));
}
Also used : Metacard(ddf.catalog.data.Metacard) Attribute(ddf.catalog.data.Attribute) QueryResponse(ddf.catalog.operation.QueryResponse) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) ResourceCacheInterface(ddf.catalog.cache.ResourceCacheInterface) ReliableResource(ddf.catalog.resource.data.ReliableResource) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 24 with ReliableResource

use of ddf.catalog.resource.data.ReliableResource in project ddf by codice.

the class TestMetacardResourceSizePlugin method testWhenCachedResourceSizeIsZero.

@Test
public void testWhenCachedResourceSizeIsZero() throws Exception {
    ResourceCacheInterface cache = mock(ResourceCacheInterface.class);
    ReliableResource cachedResource = mock(ReliableResource.class);
    when(cachedResource.getSize()).thenReturn(0L);
    when(cache.getValid(anyString(), (Metacard) anyObject())).thenReturn(cachedResource);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setId("abc123");
    metacard.setSourceId("ddf-1");
    metacard.setResourceSize("N/A");
    Result result = new ResultImpl(metacard);
    List<Result> results = new ArrayList<Result>();
    results.add(result);
    QueryResponse input = mock(QueryResponse.class);
    when(input.getResults()).thenReturn(results);
    MetacardResourceSizePlugin plugin = new MetacardResourceSizePlugin(cache);
    QueryResponse queryResponse = plugin.process(input);
    assertThat(queryResponse.getResults().size(), is(1));
    Metacard resultMetacard = queryResponse.getResults().get(0).getMetacard();
    assertThat(metacard, is(notNullValue()));
    // Since using Metacard vs. MetacardImpl have to get resource-size as an
    // Attribute vs. Long
    Attribute resourceSizeAttr = resultMetacard.getAttribute(Metacard.RESOURCE_SIZE);
    assertThat((String) resourceSizeAttr.getValue(), equalTo("N/A"));
}
Also used : Metacard(ddf.catalog.data.Metacard) Attribute(ddf.catalog.data.Attribute) QueryResponse(ddf.catalog.operation.QueryResponse) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) ResourceCacheInterface(ddf.catalog.cache.ResourceCacheInterface) ReliableResource(ddf.catalog.resource.data.ReliableResource) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 25 with ReliableResource

use of ddf.catalog.resource.data.ReliableResource in project ddf by codice.

the class TestMetacardResourceSizePlugin method testMetacardResourceSizePopulatedAndHasProduct.

@Test
public void testMetacardResourceSizePopulatedAndHasProduct() throws Exception {
    ResourceCacheInterface cache = mock(ResourceCacheInterface.class);
    ReliableResource cachedResource = mock(ReliableResource.class);
    when(cachedResource.getSize()).thenReturn(999L);
    when(cachedResource.hasProduct()).thenReturn(true);
    when(cache.getValid(anyString(), (Metacard) anyObject())).thenReturn(cachedResource);
    MetacardImpl metacard = new MetacardImpl();
    metacard.setId("abc123");
    metacard.setSourceId("ddf-1");
    metacard.setResourceSize("N/A");
    Result result = new ResultImpl(metacard);
    List<Result> results = new ArrayList<Result>();
    results.add(result);
    QueryResponse input = mock(QueryResponse.class);
    when(input.getResults()).thenReturn(results);
    MetacardResourceSizePlugin plugin = new MetacardResourceSizePlugin(cache);
    QueryResponse queryResponse = plugin.process(input);
    assertThat(queryResponse.getResults().size(), is(1));
    Metacard resultMetacard = queryResponse.getResults().get(0).getMetacard();
    assertThat(metacard, is(notNullValue()));
    // Since using Metacard vs. MetacardImpl have to get resource-size as an
    // Attribute vs. Long
    Attribute resourceSizeAttr = resultMetacard.getAttribute(Metacard.RESOURCE_SIZE);
    assertThat((String) resourceSizeAttr.getValue(), is("999"));
}
Also used : Metacard(ddf.catalog.data.Metacard) Attribute(ddf.catalog.data.Attribute) QueryResponse(ddf.catalog.operation.QueryResponse) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) ResourceCacheInterface(ddf.catalog.cache.ResourceCacheInterface) ReliableResource(ddf.catalog.resource.data.ReliableResource) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Aggregations

ReliableResource (ddf.catalog.resource.data.ReliableResource)44 Test (org.junit.Test)32 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)15 Metacard (ddf.catalog.data.Metacard)14 HazelcastInstance (com.hazelcast.core.HazelcastInstance)7 Attribute (ddf.catalog.data.Attribute)7 Result (ddf.catalog.data.Result)7 File (java.io.File)7 MockInputStream (ddf.catalog.cache.MockInputStream)6 ResourceCacheInterface (ddf.catalog.cache.ResourceCacheInterface)6 ResultImpl (ddf.catalog.data.impl.ResultImpl)6 QueryResponse (ddf.catalog.operation.QueryResponse)6 ResourceRetriever (ddf.catalog.resourceretriever.ResourceRetriever)6 ArrayList (java.util.ArrayList)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 CacheKey (ddf.catalog.cache.impl.CacheKey)4 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)3 Resource (ddf.catalog.resource.Resource)3 MimeType (javax.activation.MimeType)3 Ignore (org.junit.Ignore)3