Author: aadamchik
Date: Sun Apr  1 11:32:09 2012
New Revision: 1308081

URL: http://svn.apache.org/viewvc?rev=1308081&view=rev
Log:
CAY-1681 Third prefetch kind - DISJOINT_BY_ID

* performance optimization - loop independent code is run in the loop
* a possibly negligible performance optimization - reversing individual Db rels 
instead of flattened obj rel
* missing dot when creating prefetch path (not visible in our tests as 
flattened will have to be 3 steps long at least)

Modified:
    
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/HierarchicalObjectResolver.java

Modified: 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/HierarchicalObjectResolver.java
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/HierarchicalObjectResolver.java?rev=1308081&r1=1308080&r2=1308081&view=diff
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/HierarchicalObjectResolver.java
 (original)
+++ 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/HierarchicalObjectResolver.java
 Sun Apr  1 11:32:09 2012
@@ -59,7 +59,7 @@ class HierarchicalObjectResolver {
     }
 
     HierarchicalObjectResolver(DataContext context, QueryMetadata metadata,
-                               ClassDescriptor descriptor, boolean 
needToSaveDuplicates) {
+            ClassDescriptor descriptor, boolean needToSaveDuplicates) {
         this(context, metadata);
         this.descriptor = descriptor;
         this.needToSaveDuplicates = needToSaveDuplicates;
@@ -132,36 +132,48 @@ class HierarchicalObjectResolver {
                 return true;
             }
 
-            PrefetchProcessorNode parentProcessorNode = 
(PrefetchProcessorNode) processorNode.getParent();
+            PrefetchProcessorNode parentProcessorNode = 
(PrefetchProcessorNode) processorNode
+                    .getParent();
             ObjRelationship relationship = 
processorNode.getIncoming().getRelationship();
 
-            PrefetchSelectQuery query = new 
PrefetchSelectQuery(node.getPath(), relationship);
-
-            for (Object dataRow : parentProcessorNode.getDataRows()) {
-                List<DbRelationship> dbRelationships = 
relationship.getReverseRelationship().getDbRelationships();
-                DbRelationship lastDbRelationship = 
dbRelationships.get(dbRelationships.size() - 1);
-                
-                String pathPrefix = "";
-                if (dbRelationships.size() > 1) {
-                    // we need path prefix for flattened relationships
-                    List<DbRelationship> headingDbRelationships
-                            = dbRelationships.subList(0, 
dbRelationships.size() - 1);
-                    StringBuilder pathPrefixBuilder = new StringBuilder();
-                    for (DbRelationship dbRelationship : 
headingDbRelationships) {
-                        pathPrefixBuilder.append(dbRelationship.getName());
+            PrefetchSelectQuery query = new PrefetchSelectQuery(
+                    node.getPath(),
+                    relationship);
+
+            List<DbRelationship> dbRelationships = 
relationship.getDbRelationships();
+            DbRelationship lastDbRelationship = dbRelationships.get(0);
+
+            String pathPrefix = "";
+            if (dbRelationships.size() > 1) {
+
+                // we need path prefix for flattened relationships
+                StringBuilder buffer = new StringBuilder();
+                for (int i = dbRelationships.size() - 1; i >= 1; i--) {
+                    if (buffer.length() > 0) {
+                        buffer.append(".");
                     }
-                    pathPrefix = pathPrefixBuilder.toString() + ".";
+
+                    buffer.append(dbRelationships
+                            .get(i)
+                            .getReverseRelationship()
+                            .getName());
                 }
 
+                pathPrefix = buffer.append(".").toString();
+            }
+
+            for (Object dataRow : parentProcessorNode.getDataRows()) {
+
                 Expression allJoinsQualifier = null;
                 for (DbJoin join : lastDbRelationship.getJoins()) {
-                    // we have reversed db relationship here, so target and 
source are interchanged
-                    Object targetValue = ((DataRow) 
dataRow).get(join.getTargetName());
-                    Expression joinQualifier
-                            = ExpressionFactory.matchDbExp(pathPrefix + 
join.getSourceName(), targetValue);
+
+                    Object targetValue = ((DataRow) 
dataRow).get(join.getSourceName());
+                    Expression joinQualifier = 
ExpressionFactory.matchDbExp(pathPrefix
+                            + join.getTargetName(), targetValue);
                     if (allJoinsQualifier == null) {
                         allJoinsQualifier = joinQualifier;
-                    } else {
+                    }
+                    else {
                         allJoinsQualifier = 
allJoinsQualifier.andExp(joinQualifier);
                     }
                 }
@@ -171,10 +183,9 @@ class HierarchicalObjectResolver {
 
             query.setFetchingDataRows(true);
             if (relationship.isSourceIndependentFromTargetChange()) {
-                // setup extra result columns to be able to relate result rows 
to the parent
-                // result objects.
-                query.addResultPath("db:"
-                        + relationship.getReverseDbRelationshipPath());
+                // setup extra result columns to be able to relate result rows 
to the
+                // parent result objects.
+                query.addResultPath("db:" + 
relationship.getReverseDbRelationshipPath());
             }
 
             List dataRows = context.performQuery(query);


Reply via email to