You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2011/01/03 22:26:20 UTC

svn commit: r1054758 - in /cayenne/sandbox/cayenne-mixin/trunk: ./ src/main/java/org/apache/cayenne/mixin/cache/ src/main/java/org/apache/cayenne/mixin/relationship/

Author: aadamchik
Date: Mon Jan  3 21:26:20 2011
New Revision: 1054758

URL: http://svn.apache.org/viewvc?rev=1054758&view=rev
Log:
@CacheGroup annotation

Added:
    cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/cache/
    cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/cache/CacheGroups.java
      - copied, changed from r1053048, cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationship.java
    cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/cache/CacheInvalidationFilter.java
Modified:
    cayenne/sandbox/cayenne-mixin/trunk/pom.xml
    cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationship.java
    cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationshipFilter.java

Modified: cayenne/sandbox/cayenne-mixin/trunk/pom.xml
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-mixin/trunk/pom.xml?rev=1054758&r1=1054757&r2=1054758&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-mixin/trunk/pom.xml (original)
+++ cayenne/sandbox/cayenne-mixin/trunk/pom.xml Mon Jan  3 21:26:20 2011
@@ -8,7 +8,7 @@
 		<version>3.1M1</version>
 	</parent>
 	<artifactId>cayenne-mixin</artifactId>
-	<version>3.1.0.12</version>
+	<version>3.1.0.13</version>
 	<name>Library: cayenne-mixin</name>
 	<packaging>jar</packaging>
 	<properties>

Copied: cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/cache/CacheGroups.java (from r1053048, cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationship.java)
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/cache/CacheGroups.java?p2=cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/cache/CacheGroups.java&p1=cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationship.java&r1=1053048&r2=1054758&rev=1054758&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationship.java (original)
+++ cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/cache/CacheGroups.java Mon Jan  3 21:26:20 2011
@@ -16,7 +16,7 @@
  *  specific language governing permissions and limitations
  *  under the License.
  ****************************************************************/
-package org.apache.cayenne.mixin.relationship;
+package org.apache.cayenne.mixin.cache;
 
 import java.lang.annotation.Documented;
 import java.lang.annotation.ElementType;
@@ -26,21 +26,17 @@ import java.lang.annotation.RetentionPol
 import java.lang.annotation.Target;
 
 /**
- * Allows to tag a relationship between a mixin entity and another entity that
- * has a mixin attached. When a mixin is modeled via its own set of entities,
- * this annotation is placed on a "mixin" entity class, telling Cayenne to
- * inject related entity object in a mixin entity, resolved based on the object
- * UUID "FK".
+ * A built-in annotation that provides declarative cache management for persistent
+ * objects.
  */
-@Target( { ElementType.TYPE })
+@Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Inherited
-public @interface MixinRelationship {
+public @interface CacheGroups {
 
-	/**
-	 * Returns the name of the property of the mixin entity that is a UUID "FK"
-	 * to any of the entities implementing the mixin.
-	 */
-	String value() default "";
-}
+    /**
+     * Defines one or more cache group names associated with the tagged entity.
+     */
+    String[] value();
+}
\ No newline at end of file

Added: cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/cache/CacheInvalidationFilter.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/cache/CacheInvalidationFilter.java?rev=1054758&view=auto
==============================================================================
--- cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/cache/CacheInvalidationFilter.java (added)
+++ cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/cache/CacheInvalidationFilter.java Mon Jan  3 21:26:20 2011
@@ -0,0 +1,119 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.mixin.cache;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.cayenne.DataChannel;
+import org.apache.cayenne.DataChannelFilter;
+import org.apache.cayenne.DataChannelFilterChain;
+import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.QueryResponse;
+import org.apache.cayenne.access.DataContext;
+import org.apache.cayenne.annotation.PrePersist;
+import org.apache.cayenne.annotation.PreRemove;
+import org.apache.cayenne.annotation.PreUpdate;
+import org.apache.cayenne.cache.QueryCache;
+import org.apache.cayenne.graph.GraphDiff;
+import org.apache.cayenne.query.Query;
+
+/**
+ * A {@link DataChannelFilter} that invalidates cache groups defined for mapped entities
+ * via {@link CacheGroups} annotations.
+ */
+public class CacheInvalidationFilter implements DataChannelFilter {
+
+    private final ThreadLocal<Set<String>> groups = new ThreadLocal<Set<String>>();
+
+    @Override
+    public void init(DataChannel channel) {
+        // noop
+    }
+
+    @Override
+    public QueryResponse onQuery(
+            ObjectContext originatingContext,
+            Query query,
+            DataChannelFilterChain filterChain) {
+        return filterChain.onQuery(originatingContext, query);
+    }
+
+    @Override
+    public GraphDiff onSync(
+            ObjectContext originatingContext,
+            GraphDiff changes,
+            int syncType,
+            DataChannelFilterChain filterChain) {
+
+        try {
+            GraphDiff result = filterChain.onSync(originatingContext, changes, syncType);
+
+            // no exceptions, flush...
+
+            Collection<String> groupSet = groups.get();
+            if (groupSet != null && !groupSet.isEmpty()) {
+
+                // TODO: replace this with QueryCache injection once CAY-1445 is done
+                QueryCache cache = ((DataContext) originatingContext).getQueryCache();
+
+                for (String group : groupSet) {
+                    cache.removeGroup(group);
+                }
+            }
+
+            return result;
+        }
+        finally {
+            groups.set(null);
+        }
+    }
+
+    /**
+     * A callback method that records cache group to flush at the end of the commit.
+     */
+    @PrePersist(entityAnnotations = CacheGroups.class)
+    @PreRemove(entityAnnotations = CacheGroups.class)
+    @PreUpdate(entityAnnotations = CacheGroups.class)
+    protected void preCommit(Object object) {
+
+        Set<String> groupSet = groups.get();
+        if (groupSet == null) {
+            groupSet = new HashSet<String>();
+            groups.set(groupSet);
+        }
+
+        addCacheGroups(groupSet, object);
+    }
+
+    /**
+     * A method that builds a list of cache groups for a given object and adds them to the
+     * invalidation group set. This implementation adds all groups defined via
+     * {@link CacheGroups} annotation for a given class. Subclasses may override this
+     * method to provide more fine-grained filtering of cache groups to invalidate, based
+     * on the state of the object.
+     */
+    protected void addCacheGroups(Set<String> groupSet, Object object) {
+        CacheGroups a = object.getClass().getAnnotation(CacheGroups.class);
+        for (String group : a.value()) {
+            groupSet.add(group);
+        }
+    }
+}

Modified: cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationship.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationship.java?rev=1054758&r1=1054757&r2=1054758&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationship.java (original)
+++ cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationship.java Mon Jan  3 21:26:20 2011
@@ -26,21 +26,22 @@ import java.lang.annotation.RetentionPol
 import java.lang.annotation.Target;
 
 /**
- * Allows to tag a relationship between a mixin entity and another entity that
- * has a mixin attached. When a mixin is modeled via its own set of entities,
- * this annotation is placed on a "mixin" entity class, telling Cayenne to
- * inject related entity object in a mixin entity, resolved based on the object
- * UUID "FK".
+ * Allows to tag a relationship from a mixin entity to another entity that has this mixin
+ * attached. When a mixin is modeled via its own set of entities, this annotation is
+ * placed on a "mixin" entity class, telling Cayenne to inject related entity object in a
+ * mixin entity, resolved based on the object UUID "FK".
  */
-@Target( { ElementType.TYPE })
+@Target( {
+    ElementType.TYPE
+})
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Inherited
 public @interface MixinRelationship {
 
-	/**
-	 * Returns the name of the property of the mixin entity that is a UUID "FK"
-	 * to any of the entities implementing the mixin.
-	 */
-	String value() default "";
+    /**
+     * Returns the name of the property of the mixin entity that is a UUID "FK" to any of
+     * the entities implementing the mixin.
+     */
+    String value() default "";
 }

Modified: cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationshipFilter.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationshipFilter.java?rev=1054758&r1=1054757&r2=1054758&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationshipFilter.java (original)
+++ cayenne/sandbox/cayenne-mixin/trunk/src/main/java/org/apache/cayenne/mixin/relationship/MixinRelationshipFilter.java Mon Jan  3 21:26:20 2011
@@ -70,7 +70,7 @@ public class MixinRelationshipFilter imp
 	 */
 	@PostLoad(entityAnnotations = MixinRelationship.class)
 	@PostPersist(entityAnnotations = MixinRelationship.class)
-	void postLoadObject(DataObject object) {
+	void postLoad(DataObject object) {
 		faultingStrategy.afterObjectLoaded(object);
 	}
 }