openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r908322 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/conf/ openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ openjpa-project/src/doc/manual/
Date Wed, 10 Feb 2010 02:26:32 GMT
Author: ppoddar
Date: Wed Feb 10 02:26:31 2010
New Revision: 908322

URL: http://svn.apache.org/viewvc?rev=908322&view=rev
Log:
OPENJPA-1334: Separate distribution policy from DataCacheManager.

Added:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DefaultCacheDistributionPolicy.java
  (with props)
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/TypeBasedCacheDistributionPolicy.java
  (with props)
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/CacheDistributionPolicy.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManager.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManagerImpl.java
    openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java?rev=908322&r1=908321&r2=908322&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java
Wed Feb 10 02:26:31 2010
@@ -23,6 +23,7 @@
 import java.util.Map;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.openjpa.datacache.CacheDistributionPolicy;
 import org.apache.openjpa.datacache.ConcurrentDataCache;
 import org.apache.openjpa.datacache.ConcurrentQueryCache;
 import org.apache.openjpa.datacache.DataCacheManager;
@@ -93,6 +94,7 @@
     public BrokerValue brokerPlugin;
     public ObjectValue dataCachePlugin;
     public ObjectValue dataCacheManagerPlugin;
+    public ObjectValue cacheDistributionPolicyPlugin;
     public IntValue dataCacheTimeout;
     public ObjectValue queryCachePlugin;
     public BooleanValue dynamicDataStructs;
@@ -229,6 +231,15 @@
         dataCacheManagerPlugin.setString(aliases[0]);
         dataCacheManagerPlugin.setInstantiatingGetter("getDataCacheManager");
 
+        cacheDistributionPolicyPlugin = addPlugin("CacheDistributionPolicy", true);
+        aliases = new String[] {
+                "default",    "org.apache.openjpa.datacache.DefaultCacheDistributionPolicy",
+                "type-based", "org.apache.openjpa.datacache.TypeBasedCacheDistributionPolicy"};
+        cacheDistributionPolicyPlugin.setAliases(aliases);
+        cacheDistributionPolicyPlugin.setDefault(aliases[0]);
+        cacheDistributionPolicyPlugin.setString(aliases[0]);
+        cacheDistributionPolicyPlugin.setInstantiatingGetter("getCacheDistributionPolicy");
+        
         dataCachePlugin = addPlugin("DataCache", false);
         aliases = new String[] { 
             "false", null, 
@@ -1694,5 +1705,27 @@
     public String getDataCacheMode() {
         return dataCacheMode.getString();
     }
+    
+
+    public String getCacheDistributionPolicy() {
+        return cacheDistributionPolicyPlugin.getString();
+    }
+
+    public CacheDistributionPolicy getCacheDistributionPolicyInstance() {
+        CacheDistributionPolicy policy = (CacheDistributionPolicy) cacheDistributionPolicyPlugin.get();
+        if (policy == null) {
+            policy =  (CacheDistributionPolicy) 
+                cacheDistributionPolicyPlugin.instantiate(CacheDistributionPolicy.class,
this);
+        }
+        return policy;
+    }
+
+    public void setCacheDistributionPolicy(String policyPlugin) {
+        cacheDistributionPolicyPlugin.setString(policyPlugin);
+    }
+
+    public void setCacheDistributionPolicyInstance(CacheDistributionPolicy policy) {
+        cacheDistributionPolicyPlugin.set(policy);
+    }
 }
 

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/CacheDistributionPolicy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/CacheDistributionPolicy.java?rev=908322&r1=908321&r2=908322&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/CacheDistributionPolicy.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/CacheDistributionPolicy.java
Wed Feb 10 02:26:31 2010
@@ -19,38 +19,42 @@
 package org.apache.openjpa.datacache;
 
 import org.apache.openjpa.kernel.OpenJPAStateManager;
-import org.apache.openjpa.meta.ClassMetaData;
+import org.apache.openjpa.lib.conf.Configurable;
 
 /**
- * A policy determines the name of the cache where a given entity state will be cached.
+ * A policy determines whether a given entity should be cached and if true, in which named
partition of the
+ * cache.
+ * <br>
+ * This policy is activated for each instance if and only if the cache settings specified
in metadata 
+ * such as JPA specification defined {@link Cacheable @Cacheable} annotation or OpenJPA specific
+ * {@link org.apache.openjpa.persistence.DataCache @DataCache} annotation or configuration
property 
+ * such as <code>javax.persistence.sharedCache.mode</code> determined the type
of the instance being cachable.
+ * <br> 
+ * For example, a specific policy will never be active for when <code>javax.persistence.sharedCache.mode</code>

+ * is set to <code>NONE</code>. 
+ * <br>
+ * Distribution Policies are configurable. So a specific policy can be configured as
+ * <pre>
+ *  &lt;property name="openjpa.CacheDistributionPolicy" value="com.acme.FooPolicy(param1='xyz',param2=true)"/&gt;
+ * </pre>
+ * where <code>com.acme.FooPolicy</code> is an implementation of this interface
and defines bean style setter and
+ * getter methods for String property <code>param1</code> and boolean property
<code>param2</code>. 
  * 
  * @author Pinaki Poddar
  * 
  * @since 2.0.0
  *
  */
-public interface CacheDistributionPolicy {
+public interface CacheDistributionPolicy extends Configurable {
     /**
      * Selects the name of the cache where the given managed proxy object state be cached.
      * 
-     * @param sm the managed proxy object to be cached
-     * @param context the context of invocation. No specific semantics is 
-     * attributed currently. Can be null.
+     * @param sm the managed proxy object to be cached. The actual managed instance can be
accessed from the proxy
+     * instance simply as <code>sm.getManagedInstance()</code>.
+     * 
+     * @param context the context of invocation. No specific semantics is attributed currently.
Can be null.
      *  
-     * @return name of the cache or null if the managed instance need not be cached.
+     * @return name of the cache or null, implying that that the instance should not be cached.
      */
     String selectCache(OpenJPAStateManager sm, Object context);
-    
-    /**
-     * A default implementation that selects the cache by the type of the given
-     * managed instance.
-     * 
-     * @see ClassMetaData#getDataCacheName()
-     *
-     */
-    public static class Default implements CacheDistributionPolicy {
-        public String selectCache(OpenJPAStateManager sm, Object context) {
-            return sm.getMetaData().getDataCacheName();
-        }
-    }
 }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManager.java?rev=908322&r1=908321&r2=908322&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManager.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManager.java
Wed Feb 10 02:26:31 2010
@@ -20,12 +20,16 @@
 
 import org.apache.openjpa.conf.OpenJPAConfiguration;
 import org.apache.openjpa.kernel.OpenJPAStateManager;
+import org.apache.openjpa.lib.conf.Configurable;
 import org.apache.openjpa.lib.conf.ObjectValue;
 
 /**
- * Manages the system's data and query caches. You can
- * retrieve the data cache manager from the {@link OpenJPAConfiguration}.
+ * Manages the system's data and query caches. You can retrieve the data cache manager from
the 
+ * {@link OpenJPAConfiguration#getDataCacheManagerInstance()}.
  * <br>
+ * Manages zero or more individual {@link DataCache caches} or partitions. Each individual
partition
+ * is identified by a string-based identifier.
+ *  
  * Decides eligibility to cache for managed types.
  * 
  *
@@ -96,22 +100,6 @@
     public CacheDistributionPolicy getDistributionPolicy();
     
     /**
-     * Set the types that are explicitly excluded from being cached.
-     * 
-     * @param typeNames semicolon separated fully qualified class names.
-     * @since 2.0.0
-     */
-    public void setExcludedTypes(String typeNames);
-    
-    /**
-     * Set the types that are explicitly included to be cached.
-     * 
-     * @param typeNames semicolon separated fully qualified class names.
-     * @since 2.0.0
-     */
-    public void setIncludedTypes(String typeNames);
-
-    /**
      * Close all caches.
      */
     public void close();

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManagerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManagerImpl.java?rev=908322&r1=908321&r2=908322&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManagerImpl.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DataCacheManagerImpl.java
Wed Feb 10 02:26:31 2010
@@ -27,6 +27,7 @@
 import org.apache.openjpa.conf.OpenJPAConfiguration;
 import org.apache.openjpa.enhance.PCDataGenerator;
 import org.apache.openjpa.kernel.OpenJPAStateManager;
+import org.apache.openjpa.lib.conf.Configuration;
 import org.apache.openjpa.lib.conf.ObjectValue;
 import org.apache.openjpa.lib.util.Closeable;
 import org.apache.openjpa.meta.ClassMetaData;
@@ -51,9 +52,7 @@
     private QueryCache _queryCache = null;
     private DataCachePCDataGenerator _pcGenerator = null;
     private DataCacheScheduler _scheduler = null;
-    private CacheDistributionPolicy _policy = new CacheDistributionPolicy.Default();
-    private Set<String> _excludedTypes;
-    private Set<String> _includedTypes;
+    private CacheDistributionPolicy _policy = new DefaultCacheDistributionPolicy();
 
     public void initialize(OpenJPAConfiguration conf, ObjectValue dataCache, ObjectValue
queryCache) {
         _conf = conf;
@@ -65,6 +64,8 @@
         if (conf.getDynamicDataStructs())
             _pcGenerator = new DataCachePCDataGenerator(conf);
         _scheduler = new DataCacheScheduler(conf);
+        
+        _policy = conf.getCacheDistributionPolicyInstance();
 
         _cache.initialize(this);
         _queryCache = (QueryCache) queryCache.instantiate(QueryCache.class, conf);
@@ -133,22 +134,12 @@
     }
     
     /**
-     * Sets the instance-based cache distribution policy. 
-     */
-    public void setDistributionPolicy(CacheDistributionPolicy policy) {
-        _policy = policy;
-    }
-    
-    /**
      * Affirms if the given type is eligible for cache.
      */
     public boolean isCachable(ClassMetaData meta) {
-        Boolean isCachable = isCacheableByPlugin(meta);
+        Boolean isCachable = isCacheableByMode(meta);
         if (isCachable == null) {
-            isCachable = isCacheableByMode(meta);
-            if (isCachable == null) {
-                isCachable = isCacheableByType(meta);
-            }
+           isCachable = isCacheableByType(meta);
         }
         return isCachable;
     }
@@ -180,61 +171,4 @@
     private Boolean isCacheableByType(ClassMetaData meta) {
         return meta.getDataCacheName() != null;
     }
-    
-    /**
-     * Is the given type cacheable by excludeTypes/includeTypes plug-in properties.
-     *  
-     * @param meta the given type
-     * @return TRUE or FALSE if the type has appeared in the plug-in property.
-     * null otherwise.
-     */
-    private Boolean isCacheableByPlugin(ClassMetaData meta) {
-        String className = meta.getDescribedType().getName();
-        if (_excludedTypes != null && _excludedTypes.contains(className)) {  
-            return Boolean.FALSE;
-        } 
-        if (_includedTypes != null && _includedTypes.contains(className)) {
-            return Boolean.TRUE;
-        }
-        return null;
-    }
-
-    /**
-     * Gets the excluded types, if configured.
-     */
-    public Set<String> getExcludedTypes() {
-        return _excludedTypes;
-    }
-    
-    /**
-     * Sets excluded types from a semicolon separated list of type names.
-     */
-    public void setExcludedTypes(String types) {
-        _excludedTypes = parseNames(types);
-    }
-
-    /**
-     * Gets the included types, if configured.
-     */
-    public Set<String> getIncludedTypes() {
-        return _excludedTypes;
-    }
-    
-    /**
-     * Sets included types from a semicolon separated list of type names.
-     */
-    public void setIncludedTypes(String types) {
-        _includedTypes = parseNames(types);
-    }
-    
-    private Set<String> parseNames(String types) {
-        if (StringUtils.isEmpty(types))
-            return Collections.emptySet();
-        String[] names = Strings.split(types, ";", 0);
-        Set<String> set = new HashSet<String>();
-        set.addAll(Arrays.asList(names));
-        
-        return  Collections.unmodifiableSet(set);
-    }
-
 }

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DefaultCacheDistributionPolicy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DefaultCacheDistributionPolicy.java?rev=908322&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DefaultCacheDistributionPolicy.java
(added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DefaultCacheDistributionPolicy.java
Wed Feb 10 02:26:31 2010
@@ -0,0 +1,29 @@
+package org.apache.openjpa.datacache;
+
+import org.apache.openjpa.kernel.OpenJPAStateManager;
+import org.apache.openjpa.lib.conf.Configuration;
+import org.apache.openjpa.meta.ClassMetaData;
+
+/**
+ * A default implementation that selects the cache by the type of the given managed instance.
+ * The name of the cache is determined by {@link ClassMetaData#getDataCacheName() name as
specified} by
+ * the metadata. 
+ * 
+ * @see ClassMetaData#getDataCacheName()
+ *
+ */
+public class DefaultCacheDistributionPolicy implements CacheDistributionPolicy {
+    public String selectCache(OpenJPAStateManager sm, Object context) {
+        return sm.getMetaData().getDataCacheName();
+        
+    }
+
+    public void endConfiguration() {
+    }
+
+    public void setConfiguration(Configuration conf) {
+    }
+
+    public void startConfiguration() {
+    }
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/DefaultCacheDistributionPolicy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/TypeBasedCacheDistributionPolicy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/TypeBasedCacheDistributionPolicy.java?rev=908322&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/TypeBasedCacheDistributionPolicy.java
(added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/TypeBasedCacheDistributionPolicy.java
Wed Feb 10 02:26:31 2010
@@ -0,0 +1,104 @@
+package org.apache.openjpa.datacache;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.openjpa.kernel.OpenJPAStateManager;
+import org.apache.openjpa.meta.ClassMetaData;
+
+import serp.util.Strings;
+
+/**
+ * A cache distribution policy based on the type of the managed objects.
+ * <br>
+ * The policy is configured by specifying list of included or excluded types.
+ * The lists are specified as fully-qualified persistence class names separated by semicolon.
+ * <br>
+ * The policy checks for the given instance by its type whether the class name appears in
+ * exclusion or inclusion lists. If the class name appears in exclusion list then the
+ * instance is not cached. Otherwise If the class name appears in inclusion list but not
in
+ * exclusion list, then the instance is cached.
+ *  
+ * @author Pinaki Poddar
+ *
+ */
+public class TypeBasedCacheDistributionPolicy extends DefaultCacheDistributionPolicy 
+    implements CacheDistributionPolicy {
+    private Set<String> _excludedTypes;
+    private Set<String> _includedTypes;
+    
+    
+    /**
+     * Gets the excluded types, if configured.
+     */
+    public Set<String> getExcludedTypes() {
+        return _excludedTypes;
+    }
+    
+    /**
+     * Sets excluded types from a semicolon separated list of type names.
+     */
+    public void setExcludedTypes(String types) {
+        _excludedTypes = parseNames(types);
+    }
+
+    /**
+     * Gets the included types, if configured.
+     */
+    public Set<String> getIncludedTypes() {
+        return _includedTypes;
+    }
+    
+    /**
+     * Sets included types from a semicolon separated list of type names.
+     */
+    public void setIncludedTypes(String types) {
+        _includedTypes = parseNames(types);
+    }
+    
+    private Set<String> parseNames(String types) {
+        if (StringUtils.isEmpty(types))
+            return Collections.emptySet();
+        String[] names = Strings.split(types, ";", 0);
+        Set<String> set = new HashSet<String>();
+        set.addAll(Arrays.asList(names));
+        
+        return  Collections.unmodifiableSet(set);
+    }
+
+    /**
+     * Is the given type cacheable by excludeTypes/includeTypes plug-in properties.
+     *  
+     * @param meta the given type
+     * @return TRUE or FALSE if the type has appeared in the plug-in property.
+     * null otherwise.
+     */
+    private Boolean isCacheableByPlugin(ClassMetaData meta) {
+        String className = meta.getDescribedType().getName();
+        if (_excludedTypes != null && _excludedTypes.contains(className)) {  
+            return Boolean.FALSE;
+        } 
+        if (_includedTypes != null && _includedTypes.contains(className)) {
+            return Boolean.TRUE;
+        }
+        return null;
+    }
+    
+
+    
+    
+    @Override
+    public String selectCache(OpenJPAStateManager sm, Object context) {
+        Boolean result = isCacheableByPlugin(sm.getMetaData());
+        if (result == null) { // this policy does not know, ask the super class
+            return super.selectCache(sm, context);
+        } else if (Boolean.FALSE.equals(result)) { // must be excluded
+            return null;
+        } 
+        String name = sm.getMetaData().getDataCacheName();
+        return name == null ? DataCache.NAME_DEFAULT : name;
+    }
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/TypeBasedCacheDistributionPolicy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml?rev=908322&r1=908321&r2=908322&view=diff
==============================================================================
--- openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml (original)
+++ openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml Wed Feb 10 02:26:31
2010
@@ -150,10 +150,14 @@
 &lt;property name="openjpa.RemoteCommitProvider" value="sjvm"/&gt;
 </programlisting>
             </example>
+
+<section id="ref_guide_cache_distribution">
+   <title>"Distributing instanes across cache partitions"</title>
             <para>
+            
 OpenJPA also supports a partitioned cache configuration where the cached
 instances can be distributed across partitions by a application-defined
-policy. Each partition is a data cache by itself, identified by its name and can
+policy. Each partition behaves as a data cache by itself, identified by its name and can
 configured individually. The distribution policy 
 determines the specific partition that stores the state of a managed instance.
 The default distribution policy distributes the instances by their type
@@ -169,7 +173,7 @@
                     Partitioned Data Cache
                 </title>
 <programlisting>
-&lt;property name="openjpa.DataCacheManager" value="DistributionPolicy=org.acme.foo.DistributionPolicy"/&gt;
+&lt;property name="openjpa.CacheDistributionPolicy" value="org.acme.foo.DistributionPolicy"/&gt;
 &lt;property name="openjpa.DataCache" value="partitioned(PartitionType=concurrent,partitions=
                 '(name=a,cacheSize=100),(name=b,cacheSize=200)')"/&gt;
 </programlisting>



Mime
View raw message