Author: aadamchik
Date: Tue Mar 13 19:17:28 2012
New Revision: 1300294

URL: http://svn.apache.org/viewvc?rev=1300294&view=rev
Log:
CAY-1686 StringIdQuery - a query providing an optimized fetch for objects based 
on 1..N String IDs

in progress

Added:
    
cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/id/StringIdQuery.java
    
cayenne/main/trunk/framework/cayenne-lifecycle/src/test/java/org/apache/cayenne/lifecycle/id/StringIdQueryTest.java
Modified:
    cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
    
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/IndirectQuery.java
    cayenne/main/trunk/framework/cayenne-lifecycle/pom.xml

Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt?rev=1300294&r1=1300293&r2=1300294&view=diff
==============================================================================
--- cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt (original)
+++ cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt Tue Mar 13 
19:17:28 2012
@@ -49,6 +49,7 @@ CAY-1675 [PATCH] add .first(List<T>) met
 CAY-1679 A notion of default node
 CAY-1680 Get rid of shared locks in DataDomain metadata lookups
 CAY-1684 Upgrade commons-collections dependency to 3.2.1
+CAY-1686 StringIdQuery - a query providing an optimized fetch for objects 
based on 1..N String IDs. 
 
 Bug Fixes Since 3.1M3:
 

Modified: 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/IndirectQuery.java
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/IndirectQuery.java?rev=1300294&r1=1300293&r2=1300294&view=diff
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/IndirectQuery.java
 (original)
+++ 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/IndirectQuery.java
 Tue Mar 13 19:17:28 2012
@@ -33,16 +33,15 @@ import org.apache.cayenne.map.EntityReso
 public abstract class IndirectQuery implements Query {
 
     protected String name;
-    
+
     /**
      * @since 3.1
      */
     protected DataMap dataMap;
 
-
     protected transient Query replacementQuery;
-    
-     protected transient EntityResolver lastResolver;
+
+    protected transient EntityResolver lastResolver;
 
     /**
      * @since 3.1
@@ -50,15 +49,14 @@ public abstract class IndirectQuery impl
     public <T> T acceptVisitor(ConfigurationNodeVisitor<T> visitor) {
         return visitor.visitQuery(this);
     }
-    
+
     /**
      * @since 3.1
      */
     public DataMap getDataMap() {
         return dataMap;
     }
-    
-    
+
     /**
      * @since 3.1
      */

Modified: cayenne/main/trunk/framework/cayenne-lifecycle/pom.xml
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-lifecycle/pom.xml?rev=1300294&r1=1300293&r2=1300294&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-lifecycle/pom.xml (original)
+++ cayenne/main/trunk/framework/cayenne-lifecycle/pom.xml Tue Mar 13 19:17:28 
2012
@@ -16,6 +16,12 @@
                        <artifactId>junit</artifactId>
                </dependency>
                <dependency>
+                       <groupId>org.apache.cayenne.build-tools</groupId>
+                       <artifactId>cayenne-test-utilities</artifactId>
+                       <version>${project.version}</version>
+                       <scope>test</scope>
+               </dependency>
+               <dependency>
                        <groupId>org.mockito</groupId>
                        <artifactId>mockito-all</artifactId>
                </dependency>

Added: 
cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/id/StringIdQuery.java
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/id/StringIdQuery.java?rev=1300294&view=auto
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/id/StringIdQuery.java
 (added)
+++ 
cayenne/main/trunk/framework/cayenne-lifecycle/src/main/java/org/apache/cayenne/lifecycle/id/StringIdQuery.java
 Tue Mar 13 19:17:28 2012
@@ -0,0 +1,260 @@
+/*****************************************************************
+ *   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.lifecycle.id;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.configuration.ConfigurationNodeVisitor;
+import org.apache.cayenne.exp.Expression;
+import org.apache.cayenne.exp.ExpressionFactory;
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.Procedure;
+import org.apache.cayenne.query.PrefetchTreeNode;
+import org.apache.cayenne.query.Query;
+import org.apache.cayenne.query.QueryCacheStrategy;
+import org.apache.cayenne.query.QueryChain;
+import org.apache.cayenne.query.QueryMetadata;
+import org.apache.cayenne.query.QueryRouter;
+import org.apache.cayenne.query.SQLAction;
+import org.apache.cayenne.query.SQLActionVisitor;
+import org.apache.cayenne.query.SelectQuery;
+import org.apache.cayenne.reflect.ClassDescriptor;
+
+/**
+ * A query that allows to fetch objects based on one or more String IDs. The 
returned
+ * objects do not have to be of the same type, be related via inheritance, or 
come from
+ * the same DB table. Note that if you expect multiple types of objects, use
+ * {@link ObjectContext#performGenericQuery(Query)}. The returned 
QueryResponse will
+ * contain separate lists of DataRows for each type in no particular order.
+ * <p>
+ * As of this writing, a limitation of this query is that it returns DataRows 
that need to
+ * be manually converted to objects if needed. In that it is similar to {@link 
QueryChain}.
+ * 
+ * @since 3.1
+ */
+public class StringIdQuery implements Query {
+
+    private static Collection<String> toCollection(String... stringIds) {
+
+        if (stringIds == null) {
+            throw new NullPointerException("Null stringIds");
+        }
+
+        return Arrays.asList(stringIds);
+    }
+
+    protected String name;
+    protected DataMap dataMap;
+    protected Collection<String> stringIds;
+
+    protected transient Map<String, SelectQuery> idQueriesByEntity;
+
+    public StringIdQuery(String... stringIds) {
+        this(toCollection(stringIds));
+    }
+
+    public StringIdQuery(Collection<String> stringIds) {
+        // using a Set to ensure that duplicates do not result in a longer 
invariant
+        // qualifier
+        this.stringIds = new HashSet<String>(stringIds);
+    }
+
+    public Collection<String> getStringIds() {
+        return stringIds;
+    }
+
+    public void addStringIds(String... ids) {
+        if (ids == null) {
+            throw new NullPointerException("Null ids");
+        }
+
+        boolean changed = false;
+        for (String id : ids) {
+            if (stringIds.add(id)) {
+                changed = true;
+            }
+        }
+
+        if (changed) {
+            idQueriesByEntity = null;
+        }
+    }
+
+    protected Map<String, SelectQuery> getIdQueriesByEntity(EntityResolver 
resolver) {
+        if (this.idQueriesByEntity == null) {
+
+            Map<String, SelectQuery> idQueriesByEntity = new HashMap<String, 
SelectQuery>();
+            Map<String, EntityIdCoder> codersByEntity = new HashMap<String, 
EntityIdCoder>();
+
+            for (String id : stringIds) {
+                String entityName = EntityIdCoder.getEntityName(id);
+                EntityIdCoder coder = codersByEntity.get(entityName);
+                SelectQuery query;
+
+                if (coder == null) {
+                    coder = new 
EntityIdCoder(resolver.getObjEntity(entityName));
+
+                    query = new SelectQuery(entityName);
+
+                    codersByEntity.put(entityName, coder);
+                    idQueriesByEntity.put(entityName, query);
+                }
+                else {
+                    query = idQueriesByEntity.get(entityName);
+                }
+
+                Expression idExp = ExpressionFactory.matchAllDbExp(coder
+                        .toObjectId(id)
+                        .getIdSnapshot(), Expression.EQUAL_TO);
+                query.orQualifier(idExp);
+            }
+
+            this.idQueriesByEntity = idQueriesByEntity;
+        }
+
+        return this.idQueriesByEntity;
+    }
+
+    public <T> T acceptVisitor(ConfigurationNodeVisitor<T> visitor) {
+        return visitor.visitQuery(this);
+    }
+
+    public QueryMetadata getMetaData(EntityResolver resolver) {
+
+        // Cayenne doesn't know how to handle multiple root entities, so this
+        // QueryMetadata, just like QueryChain's metadata is not very precise 
and won't
+        // result in correct PersistentObjects...
+        return new QueryMetadata() {
+
+            public DataMap getDataMap() {
+                return null;
+            }
+
+            public List<Object> getResultSetMapping() {
+                return null;
+            }
+
+            public Query getOrginatingQuery() {
+                return null;
+            }
+
+            public QueryCacheStrategy getCacheStrategy() {
+                return QueryCacheStrategy.getDefaultStrategy();
+            }
+
+            public DbEntity getDbEntity() {
+                return null;
+            }
+
+            public ObjEntity getObjEntity() {
+                return null;
+            }
+
+            public ClassDescriptor getClassDescriptor() {
+                return null;
+            }
+
+            public Procedure getProcedure() {
+                return null;
+            }
+
+            public String getCacheKey() {
+                return null;
+            }
+
+            public String[] getCacheGroups() {
+                return null;
+            }
+
+            public boolean isFetchingDataRows() {
+                // overriding this... Can't fetch objects until 
DataDomainQueryAction
+                // starts converting multiple ResultSets to object... Same as 
QueryChain
+                // essentially.
+                return true;
+            }
+
+            public boolean isRefreshingObjects() {
+                return true;
+            }
+
+            public int getPageSize() {
+                return QueryMetadata.PAGE_SIZE_DEFAULT;
+            }
+
+            public int getFetchOffset() {
+                return -1;
+            }
+
+            public int getFetchLimit() {
+                return QueryMetadata.FETCH_LIMIT_DEFAULT;
+            }
+
+            public PrefetchTreeNode getPrefetchTree() {
+                return null;
+            }
+
+            public Map<String, String> getPathSplitAliases() {
+                return Collections.emptyMap();
+            }
+
+            public int getStatementFetchSize() {
+                return QueryMetadata.STATEMENT_FETCH_SIZE_DEFAULT;
+            }
+        };
+    }
+
+    public void route(QueryRouter router, EntityResolver resolver, Query 
substitutedQuery) {
+
+        Map<String, SelectQuery> queries = getIdQueriesByEntity(resolver);
+        for (SelectQuery query : queries.values()) {
+            query.route(router, resolver, this);
+        }
+    }
+
+    public SQLAction createSQLAction(SQLActionVisitor visitor) {
+        throw new UnsupportedOperationException(
+                "This query was supposed to be replace with a set of 
SelectQueries during the route phase");
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public DataMap getDataMap() {
+        return dataMap;
+    }
+
+    public void setDataMap(DataMap dataMap) {
+        this.dataMap = dataMap;
+    }
+}

Added: 
cayenne/main/trunk/framework/cayenne-lifecycle/src/test/java/org/apache/cayenne/lifecycle/id/StringIdQueryTest.java
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-lifecycle/src/test/java/org/apache/cayenne/lifecycle/id/StringIdQueryTest.java?rev=1300294&view=auto
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-lifecycle/src/test/java/org/apache/cayenne/lifecycle/id/StringIdQueryTest.java
 (added)
+++ 
cayenne/main/trunk/framework/cayenne-lifecycle/src/test/java/org/apache/cayenne/lifecycle/id/StringIdQueryTest.java
 Tue Mar 13 19:17:28 2012
@@ -0,0 +1,117 @@
+/*****************************************************************
+ *   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.lifecycle.id;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.apache.cayenne.DataRow;
+import org.apache.cayenne.QueryResponse;
+import org.apache.cayenne.configuration.server.ServerRuntime;
+import org.apache.cayenne.test.jdbc.DBHelper;
+import org.apache.cayenne.test.jdbc.TableHelper;
+
+public class StringIdQueryTest extends TestCase {
+
+    private ServerRuntime runtime;
+    private DBHelper dbHelper;
+    private TableHelper e1Helper;
+    private TableHelper e2Helper;
+
+    @Override
+    protected void setUp() throws Exception {
+        runtime = new ServerRuntime("cayenne-lifecycle.xml");
+        dbHelper = new DBHelper(runtime.getDataSource("lifecycle-db"));
+        e1Helper = new TableHelper(dbHelper, "E1", "ID");
+        e2Helper = new TableHelper(dbHelper, "E2", "ID");
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        runtime.shutdown();
+    }
+
+    public void testConstructor() {
+        StringIdQuery q1 = new StringIdQuery();
+        assertEquals(0, q1.getStringIds().size());
+
+        StringIdQuery q2 = new StringIdQuery("a", "b", "c", "c");
+        assertEquals(3, q2.getStringIds().size());
+        assertTrue(q2.getStringIds().contains("a"));
+        assertTrue(q2.getStringIds().contains("b"));
+        assertTrue(q2.getStringIds().contains("c"));
+
+        StringIdQuery q3 = new StringIdQuery(Arrays.asList("a", "b", "b", 
"c"));
+        assertEquals(3, q3.getStringIds().size());
+        assertTrue(q3.getStringIds().contains("a"));
+        assertTrue(q3.getStringIds().contains("b"));
+        assertTrue(q3.getStringIds().contains("c"));
+    }
+
+    public void testPerformQuery_SingleEntity() throws Exception {
+        e1Helper.deleteAll();
+        e1Helper.insert(3).insert(4);
+
+        StringIdQuery query = new StringIdQuery("E1:3", "E1:4", "E1:5");
+        QueryResponse response = 
runtime.getContext().performGenericQuery(query);
+        assertEquals(1, response.size());
+        assertEquals(2, response.firstList().size());
+
+        Set<Number> ids = new HashSet<Number>();
+
+        DataRow r1 = (DataRow) response.firstList().get(0);
+        ids.add((Number) r1.get("ID"));
+
+        DataRow r2 = (DataRow) response.firstList().get(1);
+        ids.add((Number) r2.get("ID"));
+
+        assertTrue(ids.contains(3l));
+        assertTrue(ids.contains(4l));
+    }
+
+    public void testPerformQuery_MultipleEntities() throws Exception {
+        e1Helper.deleteAll();
+        e1Helper.insert(3).insert(4);
+
+        e2Helper.deleteAll();
+        e2Helper.insert(5).insert(6).insert(7);
+
+        StringIdQuery query = new StringIdQuery("E1:3", "E1:4", "E2:6", 
"E1:5");
+        QueryResponse response = 
runtime.getContext().performGenericQuery(query);
+        assertEquals(2, response.size());
+
+        Set<String> ids = new HashSet<String>();
+
+        while (response.next()) {
+            List<DataRow> list = (List<DataRow>) response.currentList();
+            for (DataRow row : list) {
+                ids.add(row.getEntityName() + ":" + row.get("ID"));
+            }
+        }
+
+        assertEquals(3, ids.size());
+        assertTrue(ids.contains("E1:3"));
+        assertTrue(ids.contains("E1:4"));
+        assertTrue(ids.contains("E2:6"));
+    }
+}


Reply via email to