refactoring, cleanup

* cleanup cache key generation flow for SelectQuery
* do not generate cache key for null cache strategy


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/8ed6a07b
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/8ed6a07b
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/8ed6a07b

Branch: refs/heads/CAY-1946
Commit: 8ed6a07b2720fdba06d06bc862b936c6eec56b1c
Parents: e787265
Author: aadamchik <aadamc...@apache.org>
Authored: Sat Nov 15 12:58:07 2014 +0300
Committer: aadamchik <aadamc...@apache.org>
Committed: Sat Nov 15 12:58:07 2014 +0300

----------------------------------------------------------------------
 .../cayenne/query/SelectQueryMetadata.java      | 261 ++++++++++---------
 .../org/apache/cayenne/query/SelectByIdIT.java  |  25 +-
 2 files changed, 147 insertions(+), 139 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/8ed6a07b/cayenne-server/src/main/java/org/apache/cayenne/query/SelectQueryMetadata.java
----------------------------------------------------------------------
diff --git 
a/cayenne-server/src/main/java/org/apache/cayenne/query/SelectQueryMetadata.java
 
b/cayenne-server/src/main/java/org/apache/cayenne/query/SelectQueryMetadata.java
index 0367ac4..a05c987 100644
--- 
a/cayenne-server/src/main/java/org/apache/cayenne/query/SelectQueryMetadata.java
+++ 
b/cayenne-server/src/main/java/org/apache/cayenne/query/SelectQueryMetadata.java
@@ -33,131 +33,138 @@ import org.apache.cayenne.map.ObjEntity;
  */
 class SelectQueryMetadata extends BaseQueryMetadata {
 
-    Map<String, String> pathSplitAliases;
-
-    @Override
-    void copyFromInfo(QueryMetadata info) {
-        super.copyFromInfo(info);
-        this.pathSplitAliases = new HashMap<String, 
String>(info.getPathSplitAliases());
-    }
-
-    <T> boolean resolve(Object root, EntityResolver resolver, SelectQuery<T> 
query) {
-
-        if (super.resolve(root, resolver, null)) {
-
-            // generate unique cache key...
-            if (QueryCacheStrategy.NO_CACHE == getCacheStrategy()) {
-
-            } else {
-                // create a unique key based on entity, qualifier, ordering and
-                // fetch offset and limit
-
-                StringBuilder key = new StringBuilder();
-
-                ObjEntity entity = getObjEntity();
-                if (entity != null) {
-                    key.append(entity.getName());
-                } else if (dbEntity != null) {
-                    key.append("db:").append(dbEntity.getName());
-                }
-
-                if (query.getQualifier() != null) {
-                    key.append('/');
-                    try {
-                        query.getQualifier().appendAsString(key);
-                    } catch (IOException e) {
-                        throw new CayenneRuntimeException("Unexpected IO 
Exception appending to StringBuilder", e);
-                    }
-                }
-
-                if (!query.getOrderings().isEmpty()) {
-                    for (Ordering o : query.getOrderings()) {
-                        key.append('/').append(o.getSortSpecString());
-                        if (!o.isAscending()) {
-                            key.append(":d");
-                        }
-
-                        if (o.isCaseInsensitive()) {
-                            key.append(":i");
-                        }
-                    }
-                }
-
-                if (query.getFetchOffset() > 0 || query.getFetchLimit() > 0) {
-                    key.append('/');
-                    if (query.getFetchOffset() > 0) {
-                        key.append('o').append(query.getFetchOffset());
-                    }
-                    if (query.getFetchLimit() > 0) {
-                        key.append('l').append(query.getFetchLimit());
-                    }
-                }
-
-                this.cacheKey = key.toString();
-            }
-
-            resolveAutoAliases(query);
-
-            return true;
-        }
-
-        return false;
-    }
-
-    private <T> void resolveAutoAliases(SelectQuery<T> query) {
-        Expression qualifier = query.getQualifier();
-        if (qualifier != null) {
-            resolveAutoAliases(qualifier);
-        }
-
-        // TODO: include aliases in prefetches? flattened attributes?
-    }
-
-    private void resolveAutoAliases(Expression expression) {
-        Map<String, String> aliases = expression.getPathAliases();
-        if (!aliases.isEmpty()) {
-            if (pathSplitAliases == null) {
-                pathSplitAliases = new HashMap<String, String>();
-            }
-
-            pathSplitAliases.putAll(aliases);
-        }
-
-        int len = expression.getOperandCount();
-        for (int i = 0; i < len; i++) {
-            Object operand = expression.getOperand(i);
-            if (operand instanceof Expression) {
-                resolveAutoAliases((Expression) operand);
-            }
-        }
-    }
-
-    /**
-     * @since 3.0
-     */
-    @Override
-    public Map<String, String> getPathSplitAliases() {
-        return pathSplitAliases != null ? pathSplitAliases : 
Collections.<String, String> emptyMap();
-    }
-
-    /**
-     * @since 3.0
-     */
-    public void addPathSplitAliases(String path, String... aliases) {
-        if (aliases == null) {
-            throw new NullPointerException("Null aliases");
-        }
-
-        if (aliases.length == 0) {
-            throw new IllegalArgumentException("No aliases specified");
-        }
-
-        if (pathSplitAliases == null) {
-            pathSplitAliases = new HashMap<String, String>();
-        }
-
-        for (String alias : aliases) {
-            pathSplitAliases.put(alias, path);
-        }
-    }
+       private static final long serialVersionUID = 7465922769303943945L;
+       
+       Map<String, String> pathSplitAliases;
+
+       @Override
+       void copyFromInfo(QueryMetadata info) {
+               super.copyFromInfo(info);
+               this.pathSplitAliases = new HashMap<String, 
String>(info.getPathSplitAliases());
+       }
+
+       <T> boolean resolve(Object root, EntityResolver resolver, 
SelectQuery<T> query) {
+
+               if (super.resolve(root, resolver, null)) {
+
+                       // generate unique cache key, but only if we are 
caching..
+
+                       if (cacheStrategy != null && cacheStrategy != 
QueryCacheStrategy.NO_CACHE) {
+                               this.cacheKey = makeCacheKey(query);
+                       }
+
+                       resolveAutoAliases(query);
+
+                       return true;
+               }
+
+               return false;
+       }
+
+       private String makeCacheKey(SelectQuery<?> query) {
+
+               // create a unique key based on entity, qualifier, ordering and
+               // fetch offset and limit
+
+               StringBuilder key = new StringBuilder();
+
+               ObjEntity entity = getObjEntity();
+               if (entity != null) {
+                       key.append(entity.getName());
+               } else if (dbEntity != null) {
+                       key.append("db:").append(dbEntity.getName());
+               }
+
+               if (query.getQualifier() != null) {
+                       key.append('/');
+                       try {
+                               query.getQualifier().appendAsString(key);
+                       } catch (IOException e) {
+                               throw new CayenneRuntimeException("Unexpected 
IO Exception appending to StringBuilder", e);
+                       }
+               }
+
+               if (!query.getOrderings().isEmpty()) {
+                       for (Ordering o : query.getOrderings()) {
+                               key.append('/').append(o.getSortSpecString());
+                               if (!o.isAscending()) {
+                                       key.append(":d");
+                               }
+
+                               if (o.isCaseInsensitive()) {
+                                       key.append(":i");
+                               }
+                       }
+               }
+
+               if (query.getFetchOffset() > 0 || query.getFetchLimit() > 0) {
+                       key.append('/');
+                       if (query.getFetchOffset() > 0) {
+                               key.append('o').append(query.getFetchOffset());
+                       }
+                       if (query.getFetchLimit() > 0) {
+                               key.append('l').append(query.getFetchLimit());
+                       }
+               }
+
+               return key.toString();
+
+       }
+
+       private <T> void resolveAutoAliases(SelectQuery<T> query) {
+               Expression qualifier = query.getQualifier();
+               if (qualifier != null) {
+                       resolveAutoAliases(qualifier);
+               }
+
+               // TODO: include aliases in prefetches? flattened attributes?
+       }
+
+       private void resolveAutoAliases(Expression expression) {
+               Map<String, String> aliases = expression.getPathAliases();
+               if (!aliases.isEmpty()) {
+                       if (pathSplitAliases == null) {
+                               pathSplitAliases = new HashMap<String, 
String>();
+                       }
+
+                       pathSplitAliases.putAll(aliases);
+               }
+
+               int len = expression.getOperandCount();
+               for (int i = 0; i < len; i++) {
+                       Object operand = expression.getOperand(i);
+                       if (operand instanceof Expression) {
+                               resolveAutoAliases((Expression) operand);
+                       }
+               }
+       }
+
+       /**
+        * @since 3.0
+        */
+       @Override
+       public Map<String, String> getPathSplitAliases() {
+               return pathSplitAliases != null ? pathSplitAliases : 
Collections.<String, String> emptyMap();
+       }
+
+       /**
+        * @since 3.0
+        */
+       public void addPathSplitAliases(String path, String... aliases) {
+               if (aliases == null) {
+                       throw new NullPointerException("Null aliases");
+               }
+
+               if (aliases.length == 0) {
+                       throw new IllegalArgumentException("No aliases 
specified");
+               }
+
+               if (pathSplitAliases == null) {
+                       pathSplitAliases = new HashMap<String, String>();
+               }
+
+               for (String alias : aliases) {
+                       pathSplitAliases.put(alias, path);
+               }
+       }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/8ed6a07b/cayenne-server/src/test/java/org/apache/cayenne/query/SelectByIdIT.java
----------------------------------------------------------------------
diff --git 
a/cayenne-server/src/test/java/org/apache/cayenne/query/SelectByIdIT.java 
b/cayenne-server/src/test/java/org/apache/cayenne/query/SelectByIdIT.java
index f4e8ef8..bd5a623 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/query/SelectByIdIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/query/SelectByIdIT.java
@@ -72,7 +72,7 @@ public class SelectByIdIT extends ServerCase {
                tArtist.insert(3, "artist3");
        }
 
-    @Test
+       @Test
        public void testIntPk() throws Exception {
                createTwoArtists();
 
@@ -85,7 +85,7 @@ public class SelectByIdIT extends ServerCase {
                assertEquals("artist2", a2.getArtistName());
        }
 
-    @Test
+       @Test
        public void testMapPk() throws Exception {
                createTwoArtists();
 
@@ -98,7 +98,7 @@ public class SelectByIdIT extends ServerCase {
                assertEquals("artist2", a2.getArtistName());
        }
 
-    @Test
+       @Test
        public void testObjectIdPk() throws Exception {
                createTwoArtists();
 
@@ -113,7 +113,7 @@ public class SelectByIdIT extends ServerCase {
                assertEquals("artist2", a2.getArtistName());
        }
 
-    @Test
+       @Test
        public void testDataRowIntPk() throws Exception {
                createTwoArtists();
 
@@ -126,14 +126,15 @@ public class SelectByIdIT extends ServerCase {
                assertEquals("artist2", a2.get("ARTIST_NAME"));
        }
 
-    @Test
+       @Test
        public void testMetadataCacheKey() throws Exception {
-               SelectById<Painting> q1 = SelectById.query(Painting.class, 4);
+               SelectById<Painting> q1 = SelectById.query(Painting.class, 
4).useLocalCache();
                QueryMetadata md1 = q1.getMetaData(context.getEntityResolver());
                assertNotNull(md1);
                assertNotNull(md1.getCacheKey());
 
-               SelectById<Painting> q2 = SelectById.query(Painting.class, 
singletonMap(Painting.PAINTING_ID_PK_COLUMN, 4));
+               SelectById<Painting> q2 = SelectById.query(Painting.class, 
singletonMap(Painting.PAINTING_ID_PK_COLUMN, 4))
+                               .useLocalCache();
                QueryMetadata md2 = q2.getMetaData(context.getEntityResolver());
                assertNotNull(md2);
                assertNotNull(md2.getCacheKey());
@@ -142,20 +143,20 @@ public class SelectByIdIT extends ServerCase {
                // cache entry
                assertEquals(md1.getCacheKey(), md2.getCacheKey());
 
-               SelectById<Painting> q3 = SelectById.query(Painting.class, 5);
+               SelectById<Painting> q3 = SelectById.query(Painting.class, 
5).useLocalCache();
                QueryMetadata md3 = q3.getMetaData(context.getEntityResolver());
                assertNotNull(md3);
                assertNotNull(md3.getCacheKey());
                assertNotEquals(md1.getCacheKey(), md3.getCacheKey());
 
-               SelectById<Artist> q4 = SelectById.query(Artist.class, 4);
+               SelectById<Artist> q4 = SelectById.query(Artist.class, 
4).useLocalCache();
                QueryMetadata md4 = q4.getMetaData(context.getEntityResolver());
                assertNotNull(md4);
                assertNotNull(md4.getCacheKey());
                assertNotEquals(md1.getCacheKey(), md4.getCacheKey());
 
-               SelectById<Painting> q5 = SelectById.query(Painting.class, new 
ObjectId("Painting",
-                               Painting.PAINTING_ID_PK_COLUMN, 4));
+               SelectById<Painting> q5 = SelectById.query(Painting.class,
+                               new ObjectId("Painting", 
Painting.PAINTING_ID_PK_COLUMN, 4)).useLocalCache();
                QueryMetadata md5 = q5.getMetaData(context.getEntityResolver());
                assertNotNull(md5);
                assertNotNull(md5.getCacheKey());
@@ -165,7 +166,7 @@ public class SelectByIdIT extends ServerCase {
                assertEquals(md1.getCacheKey(), md5.getCacheKey());
        }
 
-    @Test
+       @Test
        public void testLocalCache() throws Exception {
                createTwoArtists();
 

Reply via email to