[jboss-cvs] JBossAS SVN: r75304 - in projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins: cache and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Jul 2 09:26:45 EDT 2008
Author: alesj
Date: 2008-07-02 09:26:45 -0400 (Wed, 02 Jul 2008)
New Revision: 75304
Added:
projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/
projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/Cache.java
projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/CacheFactory.java
projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/CacheItem.java
projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/ConcurrentHashMapCache.java
projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/DefaultCacheFactory.java
Modified:
projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/context/CachingMetaDataContext.java
Log:
[JBMDR-1]; initial preparation for true LRU cache.
Added: projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/Cache.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/Cache.java (rev 0)
+++ projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/Cache.java 2008-07-02 13:26:45 UTC (rev 75304)
@@ -0,0 +1,62 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.metadata.plugins.cache;
+
+/**
+ * Simple cache interface.
+ *
+ * @param <K> exact key type
+ * @param <V> value key type
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface Cache<K, V>
+{
+ /**
+ * Put value under key.
+ *
+ * @param key the key
+ * @param value the value
+ * @return old value if exists
+ */
+ V put(K key, V value);
+
+ /**
+ * Get value under key.
+ *
+ * @param key the key
+ * @return old value if exists
+ */
+ V get(K key);
+
+ /**
+ * Remove value under key.
+ *
+ * @param key the key
+ * @return removed value
+ */
+ V remove(K key);
+
+ /**
+ * Clear node.
+ */
+ void clear();
+}
Added: projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/CacheFactory.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/CacheFactory.java (rev 0)
+++ projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/CacheFactory.java 2008-07-02 13:26:45 UTC (rev 75304)
@@ -0,0 +1,57 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.metadata.plugins.cache;
+
+/**
+ * Simple cache factory.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface CacheFactory
+{
+ /**
+ * Create the owner's fqn.
+ *
+ * @param owner the factory owner instance
+ * @return owner's fqn
+ */
+ String createFqn(Object owner);
+
+ /**
+ * Create cache.
+ *
+ * @param keyClass the key class
+ * @param valueClass the value class
+ * @param rootFqn the root fqn
+ * @return new cache instance
+ */
+ <K, V> Cache<K, V> createCache(Class<K> keyClass, Class<V> valueClass, String rootFqn);
+
+ /**
+ * Create cache.
+ *
+ * @param valueClass the value class
+ * @param rootFqn the root fqn
+ * @return new cache instance
+ */
+ <V> CacheItem<V> createCacheItem(Class<V> valueClass, String rootFqn);
+}
\ No newline at end of file
Added: projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/CacheItem.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/CacheItem.java (rev 0)
+++ projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/CacheItem.java 2008-07-02 13:26:45 UTC (rev 75304)
@@ -0,0 +1,54 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.metadata.plugins.cache;
+
+/**
+ * Simple cache item.
+ *
+ * Candidate to be purged via
+ * similar cache mechanisms.
+ *
+ * @param <V> value key type
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface CacheItem<V>
+{
+ /**
+ * Put value.
+ *
+ * @param value the value
+ * @return old value if exists
+ */
+ V put(V value);
+
+ /**
+ * Get value.
+ *
+ * @return old value if exists
+ */
+ V get();
+
+ /**
+ * Clear node.
+ */
+ void clear();
+}
\ No newline at end of file
Added: projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/ConcurrentHashMapCache.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/ConcurrentHashMapCache.java (rev 0)
+++ projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/ConcurrentHashMapCache.java 2008-07-02 13:26:45 UTC (rev 75304)
@@ -0,0 +1,55 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.metadata.plugins.cache;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Simple map backed-up cache.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class ConcurrentHashMapCache<K, V> implements Cache<K, V>
+{
+ private Map<K, V> map = new ConcurrentHashMap<K,V>();
+
+ public V put(K key, V value)
+ {
+ return map.put(key, value);
+ }
+
+ public V get(K key)
+ {
+ return map.get(key);
+ }
+
+ public V remove(K key)
+ {
+ return map.remove(key);
+ }
+
+ public void clear()
+ {
+ map.clear();
+ }
+}
Added: projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/DefaultCacheFactory.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/DefaultCacheFactory.java (rev 0)
+++ projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/cache/DefaultCacheFactory.java 2008-07-02 13:26:45 UTC (rev 75304)
@@ -0,0 +1,71 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.metadata.plugins.cache;
+
+/**
+ * Default cache factory - legacy mock.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class DefaultCacheFactory implements CacheFactory
+{
+ public String createFqn(Object owner)
+ {
+ Class<?> clazz = owner.getClass();
+ return clazz.getSimpleName() + "_" + System.identityHashCode(owner);
+ }
+
+ public <K, V> Cache<K, V> createCache(Class<K> keyClass, Class<V> valueClass, String rootFqn)
+ {
+ return new ConcurrentHashMapCache<K,V>();
+ }
+
+ public <V> CacheItem<V> createCacheItem(Class<V> valueClass, String rootFqn)
+ {
+ return new InMemoryCacheItem<V>();
+ }
+
+ /**
+ * Simple in memory item.
+ */
+ private class InMemoryCacheItem<V> implements CacheItem<V>
+ {
+ private V value;
+
+ public V put(V value)
+ {
+ V oldValue = this.value;
+ this.value = value;
+ return oldValue;
+ }
+
+ public V get()
+ {
+ return value;
+ }
+
+ public void clear()
+ {
+ value = null;
+ }
+ }
+}
Modified: projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/context/CachingMetaDataContext.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/context/CachingMetaDataContext.java 2008-07-02 11:52:13 UTC (rev 75303)
+++ projects/jboss-mdr/trunk/src/main/org/jboss/metadata/plugins/context/CachingMetaDataContext.java 2008-07-02 13:26:45 UTC (rev 75304)
@@ -24,42 +24,43 @@
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.List;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.concurrent.ConcurrentHashMap;
+import org.jboss.metadata.plugins.cache.Cache;
+import org.jboss.metadata.plugins.cache.CacheFactory;
+import org.jboss.metadata.plugins.cache.CacheItem;
+import org.jboss.metadata.plugins.cache.DefaultCacheFactory;
import org.jboss.metadata.spi.context.MetaDataContext;
import org.jboss.metadata.spi.retrieval.AnnotationItem;
import org.jboss.metadata.spi.retrieval.AnnotationsItem;
import org.jboss.metadata.spi.retrieval.MetaDataItem;
import org.jboss.metadata.spi.retrieval.MetaDataRetrieval;
import org.jboss.metadata.spi.retrieval.MetaDatasItem;
+import org.jboss.metadata.spi.scope.ScopeLevel;
import org.jboss.metadata.spi.signature.Signature;
-import org.jboss.metadata.spi.scope.ScopeLevel;
/**
* CachingMetaDataContext.
*
- * TODO JBMICROCONT-120 LRU Cache
* @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
* @version $Revision$
*/
public class CachingMetaDataContext extends AbstractMetaDataContext
{
/** The annotations */
- private volatile Map<String, AnnotationItem<? extends Annotation>> annotations;
+ private volatile Cache<String, AnnotationItem> annotations;
/** MetaData by name */
- private volatile Map<String, MetaDataItem<?>> metaDataByName;
+ private volatile Cache<String, MetaDataItem> metaDataByName;
/** All annotations */
- private volatile AnnotationsItem cachedAnnotationsItem;
+ private volatile CacheItem<AnnotationsItem> cachedAnnotationsItem;
/** All meta data */
- private volatile MetaDatasItem cachedMetaDatasItem;
+ private volatile CacheItem<MetaDatasItem> cachedMetaDatasItem;
/** Cached components */
- private volatile Map<Signature, MetaDataRetrieval> cachedComponents;
+ private volatile Cache<Signature, MetaDataRetrieval> cachedComponents;
/** The valid time */
private volatile long validTime;
@@ -68,8 +69,14 @@
private volatile Boolean empty;
/** Scoped contexs */
- private volatile Map<ScopeLevel, MetaDataRetrieval> cachedScopedRetrievals;
+ private volatile Cache<ScopeLevel, MetaDataRetrieval> cachedScopedRetrievals;
+ /** The cache factory */
+ private CacheFactory factory;
+
+ /** The fqn */
+ private String fqn;
+
/**
* Create a new CachingMetaDataContext.
*
@@ -82,6 +89,17 @@
/**
* Create a new CachingMetaDataContext.
+ *
+ * @param retrieval the retrieval
+ * @param factory the cache factory
+ */
+ public CachingMetaDataContext(MetaDataRetrieval retrieval, CacheFactory factory)
+ {
+ this(null, retrieval, factory);
+ }
+
+ /**
+ * Create a new CachingMetaDataContext.
*
* @param parent the parent
* @param retrieval the retrieval
@@ -93,21 +111,67 @@
/**
* Create a new CachingMetaDataContext.
+ *
+ * @param parent the parent
+ * @param retrieval the retrieval
+ * @param factory the cache factory
+ */
+ public CachingMetaDataContext(MetaDataContext parent, MetaDataRetrieval retrieval, CacheFactory factory)
+ {
+ this(parent, Collections.singletonList(retrieval), factory);
+ }
+
+ /**
+ * Create a new CachingMetaDataContext.
*
* @param parent the parent
* @param retrievals the retrievals
*/
public CachingMetaDataContext(MetaDataContext parent, List<MetaDataRetrieval> retrievals)
{
+ this(parent, retrievals, null);
+ }
+
+ /**
+ * Create a new CachingMetaDataContext.
+ *
+ * @param parent the parent
+ * @param retrievals the retrievals
+ * @param factory the cache factory
+ */
+ public CachingMetaDataContext(MetaDataContext parent, List<MetaDataRetrieval> retrievals, CacheFactory factory)
+ {
super(parent, retrievals);
- validTime = super.getValidTime().getValidTime();
+ if (factory == null)
+ factory = new DefaultCacheFactory();
+ validTime = getValidTime().getValidTime();
+ this.factory = factory;
+ this.fqn = factory.createFqn(this);
}
+ /**
+ * Get fqn for this instance.
+ *
+ * @return the instance's fqn
+ */
+ protected String getFqn()
+ {
+ return fqn;
+ }
+
public AnnotationsItem retrieveAnnotations()
{
if (cachedAnnotationsItem == null)
- cachedAnnotationsItem = super.retrieveAnnotations();
- return cachedAnnotationsItem;
+ cachedAnnotationsItem = factory.createCacheItem(AnnotationsItem.class, getFqn());
+
+ AnnotationsItem item = cachedAnnotationsItem.get();
+ if (item == null)
+ {
+ item = super.retrieveAnnotations();
+ cachedAnnotationsItem.put(item);
+ }
+
+ return item;
}
@SuppressWarnings("unchecked")
@@ -118,13 +182,11 @@
String annotationName = annotationType.getName();
- long newValidTime = super.getValidTime().getValidTime();
+ long newValidTime = getValidTime().getValidTime();
if (validTime < newValidTime)
{
- if (annotations != null)
- annotations.clear();
- if (metaDataByName != null)
- metaDataByName.clear();
+ clearCache(annotations);
+ clearCache(metaDataByName);
validTime = newValidTime;
}
@@ -139,22 +201,29 @@
}
}
- AnnotationItem<T> result = super.retrieveAnnotation(annotationType);
+ AnnotationItem result = super.retrieveAnnotation(annotationType);
if (result != null && result.isCachable())
{
if (annotations == null)
- annotations = new ConcurrentHashMap<String, AnnotationItem<? extends Annotation>>();
+ annotations = factory.createCache(String.class, AnnotationItem.class, getFqn());
annotations.put(annotationName, result);
}
-
+
return result;
}
public MetaDatasItem retrieveMetaData()
{
if (cachedMetaDatasItem == null)
- cachedMetaDatasItem = super.retrieveMetaData();
- return cachedMetaDatasItem;
+ cachedMetaDatasItem = factory.createCacheItem(MetaDatasItem.class, getFqn());
+
+ MetaDatasItem item = cachedMetaDatasItem.get();
+ if (item == null)
+ {
+ item = super.retrieveMetaData();
+ cachedMetaDatasItem.put(item);
+ }
+ return item;
}
@SuppressWarnings("unchecked")
@@ -165,13 +234,11 @@
String name = type.getName();
- long newValidTime = super.getValidTime().getValidTime();
+ long newValidTime = getValidTime().getValidTime();
if (validTime < newValidTime)
{
- if (annotations != null)
- annotations.clear();
- if (metaDataByName != null)
- metaDataByName.clear();
+ clearCache(annotations);
+ clearCache(metaDataByName);
validTime = newValidTime;
}
@@ -190,10 +257,10 @@
if (result != null && result.isCachable())
{
if (metaDataByName == null)
- metaDataByName = new ConcurrentHashMap<String, MetaDataItem<?>>();
+ metaDataByName = factory.createCache(String.class, MetaDataItem.class, getFqn());
metaDataByName.put(name, result);
}
-
+
return result;
}
@@ -202,13 +269,11 @@
if (name == null)
throw new IllegalArgumentException("Null name");
- long newValidTime = super.getValidTime().getValidTime();
+ long newValidTime = getValidTime().getValidTime();
if (validTime < newValidTime)
{
- if (annotations != null)
- annotations.clear();
- if (metaDataByName != null)
- metaDataByName.clear();
+ clearCache(annotations);
+ clearCache(metaDataByName);
validTime = newValidTime;
}
@@ -227,35 +292,35 @@
if (result != null && result.isCachable())
{
if (metaDataByName == null)
- metaDataByName = new ConcurrentHashMap<String, MetaDataItem<?>>();
+ metaDataByName = factory.createCache(String.class, MetaDataItem.class, getFqn());
metaDataByName.put(name, result);
}
-
+
return result;
}
public void append(MetaDataRetrieval retrieval)
{
super.append(retrieval);
- cachedComponents = null;
+ clearCache(cachedComponents);
+ clearCache(cachedScopedRetrievals);
empty = null;
- cachedScopedRetrievals = null;
}
public void prepend(MetaDataRetrieval retrieval)
{
super.prepend(retrieval);
- cachedComponents = null;
+ clearCache(cachedComponents);
+ clearCache(cachedScopedRetrievals);
empty = null;
- cachedScopedRetrievals = null;
}
public void remove(MetaDataRetrieval retrieval)
{
super.remove(retrieval);
- cachedComponents = null;
+ clearCache(cachedComponents);
+ clearCache(cachedScopedRetrievals);
empty = null;
- cachedScopedRetrievals = null;
}
public MetaDataRetrieval getComponentMetaDataRetrieval(Signature signature)
@@ -269,16 +334,17 @@
if (retrieval != null)
return retrieval;
}
-
+
MetaDataRetrieval retrieval = super.getComponentMetaDataRetrieval(signature);
-
+
if (retrieval != null)
{
if (cachedComponents == null)
- cachedComponents = new ConcurrentHashMap<Signature, MetaDataRetrieval>();
+ cachedComponents = factory.createCache(Signature.class, MetaDataRetrieval.class, getFqn());
+
cachedComponents.put(signature, retrieval);
}
-
+
return retrieval;
}
@@ -291,29 +357,37 @@
public MetaDataRetrieval getScopedRetrieval(ScopeLevel level)
{
- boolean update = cachedScopedRetrievals == null || cachedScopedRetrievals.keySet().contains(level) == false;
- return getCachedScopedRetrieval(level, update);
- }
+ if (level == null)
+ return null;
- /**
- * Get the cached scoped retireval.
- *
- * @param level the scope level
- * @param update update current cache
- * @return cached retrieval or null
- */
- protected MetaDataRetrieval getCachedScopedRetrieval(ScopeLevel level, boolean update)
- {
- if (cachedScopedRetrievals == null)
+ if (cachedScopedRetrievals != null)
{
- cachedScopedRetrievals = new HashMap<ScopeLevel, MetaDataRetrieval>();
+ MetaDataRetrieval retrieval = cachedScopedRetrievals.get(level);
+ if (retrieval != null)
+ return retrieval;
}
- MetaDataRetrieval retrieval = cachedScopedRetrievals.get(level);
- if (update)
+
+ MetaDataRetrieval retrieval = super.getScopedRetrieval(level);
+
+ if (retrieval != null)
{
- retrieval = super.getScopedRetrieval(level);
+ if (cachedScopedRetrievals == null)
+ cachedScopedRetrievals = factory.createCache(ScopeLevel.class, MetaDataRetrieval.class, getFqn());
+
cachedScopedRetrievals.put(level, retrieval);
}
+
return retrieval;
}
+
+ /**
+ * Clear cache.
+ *
+ * @param cache the cache to clear
+ */
+ protected void clearCache(Cache cache)
+ {
+ if (cache != null)
+ cache.clear();
+ }
}
More information about the jboss-cvs-commits
mailing list