RE: Patch for CAY-360

From: Gentry, Michael \(Contractor\) ("Gentry,)
Date: Mon Sep 12 2005 - 13:49:51 EDT

  • Next message: Kevin Menard: "Re: Patch for CAY-360"

    The hash is computed based on the parameters and values supplied to the
    query. So, if your expression was:
     
    "firstName = $firstName and lastName = $lastName"
     
    So, assuming your query was supplied $firstName and $lastName, it would
    compute the name as:
     
    queryName + hash("firstName") + hash($firstName) + hash("lastName") +
    hash($lastName)
     
    When $firstName and $lastName have the same values, you'll get the same
    overall query string, which will allow Cayenne to do caching. If any of
    the parameter variables change (or are omitted), a different hash is
    computed which will generate a different name and Cayenne can pull that
    out of the cache or do a new query and cache the results.
     
    My test case looked like:
     
    Expression (set to be cached):
     
    (isFilled = 0) or (orderID = $orderID)
     
    Code:
     
    1: // Call SelectQuery defined in Cayenne Modeler
    2: Map tmap = new HashMap();
    3: tmap.put("orderID", "foo");
    4: List result = dataContext.performQuery("UnfilledOrders", tmap,
    false);
    5: logger.info("Unfilled Orders = " + result);
    6: result = dataContext.performQuery("UnfilledOrders", tmap, false);
    7: tmap.put("orderID", "bar");
    8: result = dataContext.performQuery("UnfilledOrders", tmap, false);
    9: tmap.put("orderID", "foo");
    10: result = dataContext.performQuery("UnfilledOrders", tmap, false);
     
    Line 4 resulted in 5 rows being fetched (I hadn't done this fetch
    previously, and 5 rows is the correct number). Line 6 resulted in 0
    rows being fetched, but 5 rows returned from the cache. Line 8 resulted
    in 5 rows being fetched (parameter value changed) and returned. Line 10
    resulted in 0 rows being fetched, but 5 rows returned from the cache.
     
    I played around with other variants, but this was my final test case.
    Seems to be working. I sent an updated JAR to Jeff and it seems to be
    working for him, too.
     
    /dev/mrg
     
     
     
    -----Original Message-----
    From: Kevin Menard [mailto:kmenar..ervprise.com]
    Sent: Monday, September 12, 2005 12:48 PM
    To: cayenne-deve..bjectstyle.org
    Subject: Re: Patch for CAY-360

            I finally got a chance to look this over. I really don't know
    how most of the code is working, so I can't comment super intelligently.

            However, why is the query name being set with a name + some
    calculated hash code? Will the query itself calculate the same hash
    code? If so, why calculate here? If not, why does the name have a
    hashCode value that the query itself does not?

            --
            Kevin

            On Sep 6, 2005, at 12:12 PM, Michael Gentry (Yes, I'm a
    Contractor) wrote:

                    Could a few eyeballs look over this and give me
    feedback? It seems to work with my test case.
                    
                    In org.objectstyle.cayenne.query.SelectQuery.java:354
    (at least in my 1.2 checkout):
                    
                    
                       public SelectQuery queryWithParameters(Map
    parameters, boolean pruneMissing) {
                            // create a query replica
                            SelectQuery query = new SelectQuery();
                            query.setDistinct(distinct);
                    
            
    this.selectProperties.copyToProperties(query.selectProperties);
                    
                            query.setLoggingLevel(logLevel);
            
    query.setParentObjEntityName(parentObjEntityName);
                            query.setParentQualifier(parentQualifier);
                            query.setRoot(root);
                    
                           // TODO: implement algorithm for building the
    name based on the original name and
                            // the hashcode of the map of parameters. This
    way query clone can take advantage
                            // of caching.
                    
                            Iterator keyValuePairs =
    parameters.entrySet().iterator();
                            HashCodeBuilder parametersHash = new
    HashCodeBuilder();
                    
                            while (keyValuePairs.hasNext()) {
                              Map.Entry entry = (Map.Entry)
    keyValuePairs.next();
                    
                              parametersHash.append(entry.getKey());
                              parametersHash.append(entry.getValue());
                            }
                    
                            query.setName(name +
    parametersHash.toHashCode());
                    
                            if (prefetches != null) {
                                query.addPrefetches(prefetches);
                            }
                    
                            if (orderings != null) {
                                query.addOrderings(orderings);
                            }
                    
                            if (customDbAttributes != null) {
            
    query.addCustomDbAttributes(customDbAttributes);
                            }
                    
                            // substitute qualifier parameters
                            if (qualifier != null) {
            
    query.setQualifier(qualifier.expWithParameters(parameters,
    pruneMissing));
                            }
                    
                            return query;
                        }
                    
                    
                    I bolded the portions of interest. Of course, the TODO
    can be removed if everything thinks this is valid.
                    
                    Thanks,
                    
                    /dev/mrg
                    --
                    I'm Victor. I'm the cleaner.
                    
                    



    This archive was generated by hypermail 2.0.0 : Mon Sep 12 2005 - 13:50:19 EDT