FileDocCategorySizeDatePackage
VersionConsistencyCacheImpl.javaAPI DocGlassfish v2 API23605Fri May 04 22:35:06 BST 2007com.sun.jdo.spi.persistence.support.sqlstore.impl

VersionConsistencyCacheImpl

public class VersionConsistencyCacheImpl extends Object implements com.sun.jdo.spi.persistence.support.sqlstore.VersionConsistencyCache
A 2-level cache of StateManager instances (i.e., a map of maps). The inner map is a BucketizedHashtable or a LRU cache, depending on parameter given at construction.
author
Dave Bristor

Fields Summary
private final Map
pcTypeMap
The outermost map of the two-level cache.
private static CacheFactory
cacheFactory
Used to create different kinds of caches.
private static final ResourceBundle
messages
private static com.sun.jdo.spi.persistence.utility.logging.Logger
logger
Use the PersistenceManager's logger.
private static final String
LRU_CACHE_CLASSNAME
Name of implementation class of LRU cache.
private static final String
PROPERTY_PREFIX
Prefix of each property name of configuration item.
private static boolean
lruCache
Determines whether to use LruCache or the default.
private static final String
LRU_CACHE_PROPERTY
Name of property to choose LRU or basic cache.
private static float
loadFactor
For both LruCache and BucketizedHashtable.
private static final String
LOAD_FACTOR_PROPERTY
Name of property for specifying loadFactor.
private static int
bucketSize
For BucketizedHashtable only.
private static final String
BUCKET_SIZE_PROPERTY
Name of property for specifying bucketSize.
private static int
initialCapacity
For BucketizedHashtable only.
private static final String
INITIAL_CAPACITY_PROPERTY
Name of property for specifying initialCapacity.
private static int
maxEntries
For LruCache only.
private static final String
MAX_ENTRIES_PROPERTY
Name of property for specifying maxEntries.
private static long
timeout
LruCache only, 10 minute timeout
private static final String
TIMEOUT_PROPERTY
Name of property for specifying timeout.
Constructors Summary
private VersionConsistencyCacheImpl()
Empty default constructor.

 // NOI18N

    // Create the cache factory
     
        cacheFactory = createCacheFactory();
    
    
protected VersionConsistencyCacheImpl(boolean highPerf)
Creates a cache with desired performance. This constructor is expected to be used for unit testing ONLY.

param
highPerf If true, use LruCache, else use BucketizedHashtable.

        if (highPerf) {
            cacheFactory = new LruCacheFactory();
        } else {
            cacheFactory = new BasicCacheFactory();
        }
    
Methods Summary
public voidaddPCType(java.lang.Class pcType)
This implementation does nothing. Instead, we create buckets for each pcType as-needed; see {@link #put}

        if (logger.isLoggable(Logger.FINEST)) {
            logger.finest(
                    I18NHelper.getMessage(
                            messages,
                            "jdo.versionconsistencycacheimpl.addpctype", // NOI18N
                            pcType));
        }
        // Intentionally empty
    
static com.sun.jdo.spi.persistence.support.sqlstore.VersionConsistencyCachecreate()
Create a cache. The performance characteristics of the cache depends on the setting of the runtime properties. If the flag com.sun.jdo.spi.persistence.support.sqlstore.impl.VersionConsistency.LruCache is true, then the LruCache cache is used. If it has some other value, the BucketizedHashtable cache is used. If not set, but we can load the LruCache class, the LruCache cache is used. Otherwise, we use the BucketizedHashtable cache. Other properties control particulars of those two caches.

        return new VersionConsistencyCacheImpl();
    
static com.sun.jdo.spi.persistence.support.sqlstore.impl.VersionConsistencyCacheImpl$CacheFactorycreateCacheFactory()
Create a CacheFactory. Uses system properties to determine what kind of cache will be returned by the factory.

        CacheFactory rc = null;
        
        loadFactor = getFloatValue(LOAD_FACTOR_PROPERTY, loadFactor);

        bucketSize = getIntValue(BUCKET_SIZE_PROPERTY, bucketSize);

        initialCapacity = getIntValue(INITIAL_CAPACITY_PROPERTY, initialCapacity);

        maxEntries = getIntValue(MAX_ENTRIES_PROPERTY, maxEntries);

        timeout = getLongValue(TIMEOUT_PROPERTY, timeout);

        // Determine whether to use LRU cache or not.
        boolean lruCache = false;
        try {
            
            // Don't use Boolean.getBoolean, because we want to know if the
            // flag is given or not.
            String s = System.getProperty(LRU_CACHE_PROPERTY);
            if (s != null) {
                lruCache = Boolean.valueOf(s).booleanValue();
                if (lruCache) {
                    
                    // If user specifies lruCache, but it is not available,
                    // log a WARNING and use the basic cache.
                    try {
                        Class.forName(LRU_CACHE_CLASSNAME);
                    } catch (Exception ex) {
                        logger.warning(
                            I18NHelper.getMessage(
                                messages,
                                "jdo.versionconsistencycacheimpl.lrucachenotfound")); // NOI18N
                        lruCache = false;
                    }
                }
                
            } else {
                // No flag given: Try to load LRU cache
                try {
                    Class.forName(LRU_CACHE_CLASSNAME);
                    lruCache = true;
                } catch (Exception ex) {
                    // LRU cache not found, so use default
                }
            }
        } catch (Exception ex) {
            
            // This probably should not happen, but fallback to the
            // default cache just in case.
            lruCache = false;
            logger.warning(
                I18NHelper.getMessage(
                    messages,
                    "jdo.versionconsistencycacheimpl.unexpectedduringcreate", ex));// NOI18N
        }

        if (lruCache) {
            rc = new LruCacheFactory();
        } else {
            rc = new BasicCacheFactory();
        }

        if (logger.isLoggable(Logger.FINER)) {
            String values =
                "\nloadFactor= " + loadFactor // NOI18N
                + "\nbucketSize= " + bucketSize // NOI18N
                + "\ninitialCapacity=" + initialCapacity // NOI18N
                + "\nmaxEntries=" + maxEntries // NOI18N
                + "\ntimeout=" + timeout // NOI18N
                + "\nlruCache=" + lruCache; // NOI18N
            logger.finer(
                I18NHelper.getMessage(
                    messages,
                    "jdo.versionconsistencycacheimpl.created",
                    values)); // NOI18N
        }

        return rc;
    
public com.sun.jdo.spi.persistence.support.sqlstore.StateManagerget(java.lang.Class pcType, java.lang.Object oid)

see
VersionConsistencyCache#get

        boolean logAtFinest = logger.isLoggable(Logger.FINEST);

        if (logAtFinest) {
            logger.finest(
                    I18NHelper.getMessage(
                            messages,
                            "jdo.versionconsistencycacheimpl.get.entering", // NOI18N
                            new Object[] {pcType, oid}));
        }
        StateManager rc = null;

        VCCache oid2sm = null;
        synchronized (pcTypeMap) {
            oid2sm = (VCCache) pcTypeMap.get(pcType);
        }
    
        if (null != oid2sm) {
            rc = oid2sm.get(oid);
        }

        if (logAtFinest) {
            logger.finest(
                    I18NHelper.getMessage(
                            messages,
                            "jdo.versionconsistencycacheimpl.get.returning", // NOI18N
                            rc));
        }
        return rc;
    
private static floatgetFloatValue(java.lang.String propName, float defaultVal)
Returns the value for the given property name. If not available, returns the default value. If the property's value cannot be parsed as a float, logs a warning.

param
propName Name of property for value
param
defaultVal Default value used if property is not set.
return
value for the property.

        float rc = defaultVal;
        String valString = System.getProperty(propName);
        if (null != valString && valString.length() > 0) {
            try {
                rc = Float.parseFloat(valString);
            } catch (NumberFormatException ex) {
                logBadConfigValue(propName, valString);
            }
        }
        return rc;
    
private static intgetIntValue(java.lang.String propName, int defaultVal)
Returns the value for the given property name. If not available, returns the default value. If the property's value cannot be parsed as an integer, logs a warning.

param
propName Name of property for value
param
defaultVal Default value used if property is not set.
return
value for the property.

        int rc = defaultVal;
        String valString = System.getProperty(propName);
        if (null != valString && valString.length() > 0) {
            try {
                rc = Integer.parseInt(valString);
            } catch (NumberFormatException ex) {
                logBadConfigValue(propName, valString);
            }
        }
        return rc;
    
private static longgetLongValue(java.lang.String propName, long defaultVal)
Returns the value for the given property name. If not available, returns the default value. If the property's value cannot be parsed as a long, logs a warning.

param
propName Name of property for value
param
defaultVal Default value used if property is not set.
return
value for the property.

        long rc = defaultVal;
        String valString = System.getProperty(propName);
        if (null != valString && valString.length() > 0) {
            try {
                rc = Long.parseLong(valString);
            } catch (NumberFormatException ex) {
                logBadConfigValue(propName, valString);
            }
        }
        return rc;
    
public booleanisHighPerf()

return
true if this cache is based on LRU cache; false otherwise.

        return LruCacheFactory.class.equals(cacheFactory.getClass());
    
private static voidlogBadConfigValue(java.lang.String propName, java.lang.String valString)
Logs a warning that the property's value is invalid.

param
propName Name of property
param
valString Value of property as a String.

        logger.warning(
            I18NHelper.getMessage(
                messages,
                "jdo.versionconsistencycacheimpl.badconfigvalue", // NOI18N
                propName, valString));
    
public com.sun.jdo.spi.persistence.support.sqlstore.StateManagerput(java.lang.Class pcType, java.lang.Object oid, com.sun.jdo.spi.persistence.support.sqlstore.StateManager sm)

see
VersionConsistencyCache#put

        boolean logAtFinest = logger.isLoggable(Logger.FINEST);

        if (logAtFinest) {
            logger.finest(
                    I18NHelper.getMessage(
                            messages,
                            "jdo.versionconsistencycacheimpl.put.entering", // NOI18N
                            new Object[] {pcType, oid, sm}));
        }

        StateManager rc = null;
        VCCache oid2sm = null;
        synchronized (pcTypeMap) {
            oid2sm = (VCCache) pcTypeMap.get(pcType);
    
            if (null == oid2sm) {
                oid2sm = cacheFactory.create();
                pcTypeMap.put(pcType, oid2sm);
            }
        }

        rc = oid2sm.put(oid, sm);

        if (logAtFinest) {
            logger.finest(
                    I18NHelper.getMessage(
                            messages,
                            "jdo.versionconsistencycacheimpl.put.returning", // NOI18N
                            rc));
        }

        return rc;
    
public com.sun.jdo.spi.persistence.support.sqlstore.StateManagerremove(java.lang.Class pcType, java.lang.Object oid)

see
VersionConsistencyCache#remove

        boolean logAtFinest = logger.isLoggable(Logger.FINEST);

        if (logAtFinest) {
            logger.finest(
                    I18NHelper.getMessage(
                            messages,
                            "jdo.versionconsistencycacheimpl.remove.entering", // NOI18N
                            new Object[] {pcType, oid}));
        }

        StateManager rc = null;
        synchronized (pcTypeMap) {
            VCCache oid2sm = (VCCache) pcTypeMap.get(pcType);

            if (null != oid2sm) {
                rc = (StateManager) oid2sm.remove(oid);
                if (oid2sm.isEmpty()) {
                    pcTypeMap.remove(pcType);
                }
            }
        }

        if (logAtFinest) {
            logger.finest(
                    I18NHelper.getMessage(
                            messages,
                            "jdo.versionconsistencycacheimpl.remove.returning", // NOI18N
                            rc));
        }
        return rc;
    
public voidremovePCType(java.lang.Class pcType)

see
VersionConsistencyCache#removePCType

        if (logger.isLoggable(Logger.FINEST)) {
            logger.finest(
                    I18NHelper.getMessage(
                            messages,
                            "jdo.versionconsistencycacheimpl.removepctype", // NOI18N
                            pcType));
        }

        synchronized (pcTypeMap) {
            VCCache oid2sm = (VCCache) pcTypeMap.get(pcType);

            if (null != oid2sm) {
                oid2sm.clear();
            }
            pcTypeMap.remove(pcType);
        }
    
public intsize()

return
the number of elements in the cache.

        int rc = 0;
        synchronized (pcTypeMap) {
            for (Iterator i = pcTypeMap.keySet().iterator(); i.hasNext();) {
                VCCache oid2sm = (VCCache) pcTypeMap.get(i.next());
                rc += oid2sm.size();
            }
        }
        return rc;