All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.cq.social.srp.internal.ProviderCache Maven / Gradle / Ivy

There is a newer version: 6.5.21
Show newest version
/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2014 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.adobe.cq.social.srp.internal;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.social.srp.internal.CachingResourceProvider;

/**
 * Implementation of the cache appropriate for storing hash maps.
 */
public class ProviderCache extends AbstractCache> {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProviderCache.class);

    /**
     * serial version id.
     */
    private static final long serialVersionUID = 1L;

    /**
     * default constructor that will use default values for the cache.
     */
    public ProviderCache() {
        super();
    }

    /**
     * @param concurrencyLevel cache concurreny level
     * @param cacheStartSize starting cache size
     * @param cacheMaxSize the initial size of the cache.
     * @param ttl expiration time for cache entries in milliseconds
     */
    public ProviderCache(final int concurrencyLevel, final int cacheStartSize, final int cacheMaxSize, final long ttl) {
        super(concurrencyLevel, cacheStartSize, cacheMaxSize, ttl);
    }

    @SuppressWarnings("unchecked")
    @Override
    public synchronized void merge(final String key, final Map data,
        final Map returnedResult) {
        final CacheEntry> cacheval = get(key);
        if (cacheval != null) {
            // Need to create a copy of the cached map since the update map being passed in can be the cached
            // map itself (i.e. read from the cache), and the remove() in the for loop below will throw
            // a ConcurrentModificationException.
            final Map cachemap = new HashMap(cacheval.get());

            LOGGER.debug("Original entry {}: {}", key, cachemap);
            LOGGER.debug("Data entry {}: {}", key, data);
            LOGGER.debug("Returned result {}: {}", key, returnedResult);
            if (cachemap != null) {
                // If the update contains null values, it means the properties are removed.
                // For the cache, we want to actually remove the properties so that during a read, null values won't
                // be returned.
                for (final String k : data.keySet()) {
                    final Object v = data.get(k);
                    if (k.equals(CachingResourceProvider.INC)) {
                        replaceIncrementWithResult(cachemap, (Map) v, returnedResult);
                    } else {
                        if (v == null) {
                            cachemap.remove(k);
                        } else {
                            cachemap.put(k, v);
                        }
                    }
                }
                put(key, new CacheEntry>(cachemap));
                LOGGER.debug("Merged entry: {}", cachemap);
            }
        }
    }

    /**
     * Create a cache entry.
     * @param input the incoming map
     * @param result the result of the operation
     * @return the cache entry
     */
    CacheEntry> createEntry(final Map input, final Map result) {
        Map mapCopy = new HashMap(input);
        Object incObj = mapCopy.remove(UGCCResourceProvider.INC);
        if (incObj instanceof Map) {
            replaceIncrementWithResult(mapCopy, (Map) incObj, result);
        }
        return new CacheEntry>(mapCopy);
    }

    private void replaceIncrementWithResult(final Map cachemap, final Map increments,
        final Map returnedResult) {
        for (Map.Entry entry : increments.entrySet()) {
            Object val = returnedResult.get(entry.getKey());
            if (val != null) {
                cachemap.put(entry.getKey(), val);
            } else {
                LOGGER.warn("Increment key {} not found in returned result: {}", entry.getKey(), returnedResult);
            }

        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy