[CalendarServer-changes] [6986] CalendarServer/branches/users/glyph/dalify/txdav

source_changes at macosforge.org source_changes at macosforge.org
Wed Feb 16 06:36:22 PST 2011


Revision: 6986
          http://trac.macosforge.org/projects/calendarserver/changeset/6986
Author:   glyph at apple.com
Date:     2011-02-16 06:36:22 -0800 (Wed, 16 Feb 2011)
Log Message:
-----------
PropertyStore.loadAll -> PropertyStore.forMultipleResources.  Add a docstring, change to use DAL, remove now-redundant parameter from the signature.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/dalify/txdav/base/propertystore/sql.py
    CalendarServer/branches/users/glyph/dalify/txdav/common/datastore/sql.py

Modified: CalendarServer/branches/users/glyph/dalify/txdav/base/propertystore/sql.py
===================================================================
--- CalendarServer/branches/users/glyph/dalify/txdav/base/propertystore/sql.py	2011-02-16 14:36:11 UTC (rev 6985)
+++ CalendarServer/branches/users/glyph/dalify/txdav/base/propertystore/sql.py	2011-02-16 14:36:22 UTC (rev 6986)
@@ -29,6 +29,7 @@
 from twext.enterprise.dal.syntax import Parameter
 from twext.enterprise.dal.syntax import Update
 from twext.enterprise.dal.syntax import Insert
+from twext.enterprise.dal.syntax import TableSyntax
 
 from txdav.common.datastore.sql_tables import schema
 
@@ -81,31 +82,49 @@
 
     @classmethod
     @inlineCallbacks
-    def loadAll(cls, defaultuser, txn, joinTable, joinColumn, parentIDColumn, parentID):
+    def forMultipleResources(cls, defaultUser, txn,
+                             childColumn, parentColumn, parentID):
         """
-        Return a list of property stores for all objects in a parent collection
+        Return a list of property stores for all objects in a collection.  This
+        is used to optimize Depth:1 operations on that collection, by loading
+        all relevant properties in a single query.
+
+        @param defaultUser: the UID of the user who owns / is requesting the
+            property stores; the ones whose per-user properties will be exposed.
+
+        @type defaultUser: C{str}
+
+        @param txn: the transaction within which to fetch the rows.
+
+        @type txn: L{IAsyncTransaction}
+
+        @param childColumn: The resource ID column for the child resources, i.e.
+            the resources of the type for which this method will loading the
+            property stores.
+
+        @param parentColumn: The resource ID column for the parent resources.
+            e.g. if childColumn is addressbook object's resource ID, then this
+            should be addressbook's resource ID.
+
         """
-        rows = yield txn.execSQL(
-            """
-            select
-              %s,
-              RESOURCE_PROPERTY.RESOURCE_ID,
-              RESOURCE_PROPERTY.NAME,
-              RESOURCE_PROPERTY.VIEWER_UID,
-              RESOURCE_PROPERTY.VALUE
-            from RESOURCE_PROPERTY
-            right join %s on (RESOURCE_PROPERTY.RESOURCE_ID = %s) 
-            where %s = %%s
-            """ % (joinColumn, joinTable, joinColumn, parentIDColumn),
-            [parentID]
+        childTable = TableSyntax(childColumn.model.table)
+        query = Select([
+            childColumn,
+            # XXX is that column necessary?  as per the 'on' clause it has to be
+            # the same as prop.RESOURCE_ID anyway.
+            prop.RESOURCE_ID, prop.NAME, prop.VIEWER_UID, prop.VALUE],
+            From=prop.join(childTable, prop.RESOURCE_ID == childColumn,
+                           'right'),
+            Where=parentColumn == parentID
         )
-        
+        rows = yield query.on(txn)
+
         createdStores = {}
         for object_resource_id, resource_id, name, view_uid, value in rows:
             if resource_id:
                 if resource_id not in createdStores:
                     store = cls.__new__(cls)
-                    super(PropertyStore, store).__init__(defaultuser)
+                    super(PropertyStore, store).__init__(defaultUser)
                     store._txn = txn
                     store._resourceID = resource_id
                     store._cached = {}
@@ -113,12 +132,12 @@
                 createdStores[resource_id]._cached[(name, view_uid)] = value
             else:
                 store = cls.__new__(cls)
-                super(PropertyStore, store).__init__(defaultuser)
+                super(PropertyStore, store).__init__(defaultUser)
                 store._txn = txn
                 store._resourceID = object_resource_id
                 store._cached = {}
                 createdStores[object_resource_id] = store
-                
+
         returnValue(createdStores)
 
 

Modified: CalendarServer/branches/users/glyph/dalify/txdav/common/datastore/sql.py
===================================================================
--- CalendarServer/branches/users/glyph/dalify/txdav/common/datastore/sql.py	2011-02-16 14:36:11 UTC (rev 6985)
+++ CalendarServer/branches/users/glyph/dalify/txdav/common/datastore/sql.py	2011-02-16 14:36:22 UTC (rev 6986)
@@ -1055,13 +1055,10 @@
 
         if dataRows:
             # Get property stores for all these child resources (if any found)
-            propertyStores =(yield PropertyStore.loadAll(
-                home.uid(),
-                home._txn,
-                cls._bindTable["name"],
-                cls._bindTable["column_RESOURCE_ID"],
-                cls._bindTable["column_HOME_RESOURCE_ID"],
-                home._resourceID,
+            propertyStores = (yield PropertyStore.forMultipleResources(
+                home.uid(), home._txn,
+                cls._bindSchema.RESOURCE_ID, cls._bindSchema.HOME_RESOURCE_ID,
+                home._resourceID
             ))
 
             bind = cls._bindSchema
@@ -1905,6 +1902,8 @@
 
     _objectTable = None
 
+    _objectSchema = None
+
     def __init__(self, parent, name, uid, resourceID=None, metadata=None):
         self._parentCollection = parent
         self._resourceID = resourceID
@@ -1921,11 +1920,12 @@
     @inlineCallbacks
     def loadAllObjects(cls, parent):
         """
-        Load all child objects and return a list of them. This must create the child classes
-        and initialize them using "batched" SQL operations to keep this constant wrt the number of
-        children. This is an optimization for Depth:1 operations on the collection.
+        Load all child objects and return a list of them. This must create the
+        child classes and initialize them using "batched" SQL operations to keep
+        this constant wrt the number of children. This is an optimization for
+        Depth:1 operations on the collection.
         """
-        
+
         results = []
 
         # Load from the main table first
@@ -1935,30 +1935,32 @@
             """ % cls._objectTable,
             [parent._resourceID,]
         )
-        
+
         if dataRows:
             # Get property stores for all these child resources (if any found)
             if parent.objectResourcesHaveProperties():
-                propertyStores =(yield PropertyStore.loadAll(
+                propertyStores =(yield PropertyStore.forMultipleResources(
                     parent._home.uid(),
                     parent._txn,
-                    cls._objectTable["name"],
-                    "%s.%s" % (cls._objectTable["name"], cls._objectTable["column_RESOURCE_ID"],),
-                    "%s.%s" % (cls._objectTable["name"], cls._objectTable["column_PARENT_RESOURCE_ID"]),
-                    parent._resourceID,
+                    cls._objectSchema.RESOURCE_ID,
+                    cls._objectSchema.PARENT_RESOURCE_ID,
+                    parent._resourceID
                 ))
             else:
                 propertyStores = {}
-        
+
         # Create the actual objects merging in properties
         for row in dataRows:
             child = cls(parent, "", None)
             child._initFromRow(tuple(row))
-            yield child._loadPropertyStore(props=propertyStores.get(child._resourceID, None))
+            yield child._loadPropertyStore(
+                props=propertyStores.get(child._resourceID, None)
+            )
             results.append(child)
-        
+
         returnValue(results)
 
+
     @classmethod
     def objectWithName(cls, parent, name, uid):
         objectResource = cls(parent, name, uid, None)
@@ -2498,15 +2500,17 @@
     def __repr__(self):
         return "<%s: %s>" % (self.__class__.__name__, self._resourceID)
 
+
     @classmethod
     @inlineCallbacks
     def loadAllObjects(cls, parent):
         """
-        Load all child objects and return a list of them. This must create the child classes
-        and initialize them using "batched" SQL operations to keep this constant wrt the number of
-        children. This is an optimization for Depth:1 operations on the collection.
+        Load all child objects and return a list of them. This must create the
+        child classes and initialize them using "batched" SQL operations to keep
+        this constant wrt the number of children. This is an optimization for
+        Depth:1 operations on the collection.
         """
-        
+
         results = []
 
         # Load from the main table first
@@ -2524,18 +2528,17 @@
             """,
             [parent._resourceID]
         ))
-        
+
         if dataRows:
             # Get property stores for all these child resources (if any found)
-            propertyStores =(yield PropertyStore.loadAll(
+            propertyStores =(yield PropertyStore.forMultipleResources(
                 parent.uid(),
                 parent._txn,
-                "NOTIFICATION",
-                "NOTIFICATION.RESOURCE_ID",
-                "NOTIFICATION.NOTIFICATION_HOME_RESOURCE_ID",
+                schema.NOTIFICATION.RESOURCE_ID,
+                schema.NOTIFICATION.NOTIFICATION_HOME_RESOURCE_ID,
                 parent._resourceID,
             ))
-        
+
         # Create the actual objects merging in properties
         for row in dataRows:
             child = cls(parent, None)
@@ -2546,11 +2549,14 @@
              child._xmlType,
              child._created,
              child._modified,) = tuple(row)
-            child._loadPropertyStore(props=propertyStores.get(child._resourceID, None))
+            child._loadPropertyStore(
+                props=propertyStores.get(child._resourceID, None)
+            )
             results.append(child)
-        
+
         returnValue(results)
 
+
     @inlineCallbacks
     def initFromStore(self):
         """
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110216/86f07f2b/attachment-0001.html>


More information about the calendarserver-changes mailing list