VersionConsistencyCacheImplpublic class VersionConsistencyCacheImpl extends Object implements com.sun.jdo.spi.persistence.support.sqlstore.VersionConsistencyCacheA 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. |
Fields Summary |
---|
private final Map | pcTypeMapThe outermost map of the two-level cache. | private static CacheFactory | cacheFactoryUsed to create different kinds of caches. | private static final ResourceBundle | messages | private static com.sun.jdo.spi.persistence.utility.logging.Logger | loggerUse the PersistenceManager's logger. | private static final String | LRU_CACHE_CLASSNAMEName of implementation class of LRU cache. | private static final String | PROPERTY_PREFIXPrefix of each property name of configuration item. | private static boolean | lruCacheDetermines whether to use LruCache or the default. | private static final String | LRU_CACHE_PROPERTYName of property to choose LRU or basic cache. | private static float | loadFactorFor both LruCache and BucketizedHashtable. | private static final String | LOAD_FACTOR_PROPERTYName of property for specifying loadFactor. | private static int | bucketSizeFor BucketizedHashtable only. | private static final String | BUCKET_SIZE_PROPERTYName of property for specifying bucketSize. | private static int | initialCapacityFor BucketizedHashtable only. | private static final String | INITIAL_CAPACITY_PROPERTYName of property for specifying initialCapacity. | private static int | maxEntriesFor LruCache only. | private static final String | MAX_ENTRIES_PROPERTYName of property for specifying maxEntries. | private static long | timeoutLruCache only, 10 minute timeout | private static final String | TIMEOUT_PROPERTYName 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.
if (highPerf) {
cacheFactory = new LruCacheFactory();
} else {
cacheFactory = new BasicCacheFactory();
}
|
Methods Summary |
---|
public void | addPCType(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.VersionConsistencyCache | create()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$CacheFactory | createCacheFactory()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.StateManager | get(java.lang.Class pcType, java.lang.Object oid)
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 float | getFloatValue(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.
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 int | getIntValue(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.
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 long | getLongValue(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.
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 boolean | isHighPerf()
return LruCacheFactory.class.equals(cacheFactory.getClass());
| private static void | logBadConfigValue(java.lang.String propName, java.lang.String valString)Logs a warning that the property's value is invalid.
logger.warning(
I18NHelper.getMessage(
messages,
"jdo.versionconsistencycacheimpl.badconfigvalue", // NOI18N
propName, valString));
| public com.sun.jdo.spi.persistence.support.sqlstore.StateManager | put(java.lang.Class pcType, java.lang.Object oid, com.sun.jdo.spi.persistence.support.sqlstore.StateManager sm)
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.StateManager | remove(java.lang.Class pcType, java.lang.Object oid)
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 void | removePCType(java.lang.Class pcType)
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 int | size()
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;
|
|