<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[10118] CalendarServer/branches/users/gaya/sharedgroups/txdav/carddav/datastore/sql.py</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/10118">10118</a></dd>
<dt>Author</dt> <dd>gaya@apple.com</dd>
<dt>Date</dt> <dd>2012-12-02 16:08:43 -0800 (Sun, 02 Dec 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>get rid of DELETE loops. cleaning.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServerbranchesusersgayasharedgroupstxdavcarddavdatastoresqlpy">CalendarServer/branches/users/gaya/sharedgroups/txdav/carddav/datastore/sql.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServerbranchesusersgayasharedgroupstxdavcarddavdatastoresqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/gaya/sharedgroups/txdav/carddav/datastore/sql.py (10117 => 10118)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/gaya/sharedgroups/txdav/carddav/datastore/sql.py        2012-12-02 21:13:11 UTC (rev 10117)
+++ CalendarServer/branches/users/gaya/sharedgroups/txdav/carddav/datastore/sql.py        2012-12-03 00:08:43 UTC (rev 10118)
</span><span class="lines">@@ -159,8 +159,9 @@
</span><span class="cx">                     abo.CREATED,
</span><span class="cx">                     abo.MODIFIED))
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx">     @classmethod
</span><del>-    def _columnsWithResourceIDsQuery(cls, columns, resourceIDs): #@NoSelf
</del><ins>+    def _abObjectColumnsWithResourceIDsQuery(cls, columns, resourceIDs): #@NoSelf
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         DAL statement to retrieve addressbook object rows with given columns.
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="lines">@@ -299,31 +300,30 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Get all addressbookobject resource IDs in this address book
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        # TODO: cache in attr
-        # TODO: optimize
</del><ins>+        # TODO: see if result can be cached in attr
</ins><span class="cx"> 
</span><del>-        allMemberIDs = set() if self.owned() else set([self._resourceID, ])
</del><ins>+        objectIDs = set() if self.owned() else set([self._resourceID, ])
</ins><span class="cx">         examinedIDs = set()
</span><span class="cx">         remainingIDs = set([self._resourceID, ])
</span><span class="cx">         while remainingIDs:
</span><span class="cx">             memberRows = yield self._memberIDsWithGroupIDsQuery(remainingIDs).on(self._txn, groupIDs=remainingIDs)
</span><del>-            allMemberIDs |= set([memberRow[0] for memberRow in memberRows])
</del><ins>+            objectIDs |= set([memberRow[0] for memberRow in memberRows])
</ins><span class="cx">             examinedIDs |= remainingIDs
</span><del>-            remainingIDs = allMemberIDs - examinedIDs
</del><ins>+            remainingIDs = objectIDs - examinedIDs
</ins><span class="cx"> 
</span><del>-        returnValue(tuple(allMemberIDs))
</del><ins>+        returnValue(tuple(objectIDs))
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def listObjectResources(self):
</span><span class="cx">         if self._objectNames is None:
</span><span class="cx">             abo = schema.ADDRESSBOOK_OBJECT
</span><del>-            memberIDs = yield self._allAddressBookObjectIDs()
-            rows = (yield self._columnsWithResourceIDsQuery(
</del><ins>+            objectIDs = yield self._allAddressBookObjectIDs()
+            rows = (yield self._abObjectColumnsWithResourceIDsQuery(
</ins><span class="cx">                     [abo.RESOURCE_NAME],
</span><del>-                    memberIDs).on(
-                        self._txn, resourceIDs=memberIDs)
-                    ) if memberIDs else []
</del><ins>+                    objectIDs).on(
+                        self._txn, resourceIDs=objectIDs)
+                    ) if objectIDs else []
</ins><span class="cx">             self._objectNames = sorted([row[0] for row in rows])
</span><span class="cx"> 
</span><span class="cx">         returnValue(self._objectNames)
</span><span class="lines">@@ -363,6 +363,14 @@
</span><span class="cx">         return self._kind
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @classmethod
+    def _deleteMembersWithMemberIDAndGroupIDsQuery(cls, memberID, groupIDs): #@NoSelf
+        aboMembers = schema.ABO_MEMBERS
+        return Delete(
+            aboMembers,
+            Where=(aboMembers.MEMBER_ID == memberID).And(
+                    aboMembers.GROUP_ID.In(Parameter(&quot;groupIDs&quot;, len(groupIDs)))))
+
</ins><span class="cx">     @inlineCallbacks
</span><span class="cx">     def remove(self):
</span><span class="cx"> 
</span><span class="lines">@@ -388,25 +396,14 @@
</span><span class="cx">             # convert delete in sharee shared group address book to remove of memberships
</span><span class="cx">             # that make this object visible to the sharee
</span><span class="cx"> 
</span><del>-            # FIX ME: Combine into one query
-            memberIDs = yield self._addressbook._allAddressBookObjectIDs()
</del><ins>+            ownerAddressBook = yield self.ownerAddressBook()
+            objectIDs = yield ownerAddressBook._allAddressBookObjectIDs()
+            assert self._ownerAddressBookResourceID not in objectIDs
</ins><span class="cx"> 
</span><del>-            groupIDRows = yield Select(
-                [aboMembers.GROUP_ID],
-                From=aboMembers,
-                Where=(aboMembers.MEMBER_ID == self._resourceID).And(
-                        aboMembers.GROUP_ID != self._ownerAddressBookResourceID),
-            ).on(self._txn)
-            groupIDs = [groupIDRow[0] for groupIDRow in groupIDRows]
</del><ins>+            if objectIDs:
+                yield self._deleteMembersWithMemberIDAndGroupIDsQuery(self._resourceID, objectIDs).on(
+                    self._txn, groupIDs=objectIDs)
</ins><span class="cx"> 
</span><del>-            for groupID in set(groupIDs) &amp; set(memberIDs):
-                yield Delete(
-                    aboMembers,
-                    Where=(aboMembers.MEMBER_ID == self._resourceID).And(
-                            aboMembers.GROUP_ID == groupID),
-                ).on(self._txn)
-
-            ownerAddressBook = yield self.ownerAddressBook()
</del><span class="cx">             yield self._changeAddressBookRevision(ownerAddressBook)
</span><span class="cx"> 
</span><span class="cx">         else:
</span><span class="lines">@@ -475,18 +472,18 @@
</span><span class="cx">         @return: L{self} if object exists in the DB, else C{None}
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-        memberIDs = yield self._addressbook._allAddressBookObjectIDs()
</del><ins>+        objectIDs = yield self._addressbook._allAddressBookObjectIDs()
</ins><span class="cx">         if self._name:
</span><del>-            rows = (yield self._allWithResourceIDAndName(memberIDs).on(
</del><ins>+            rows = (yield self._allWithResourceIDAndName(objectIDs).on(
</ins><span class="cx">                 self._txn, name=self._name,
</span><del>-                resourceIDs=memberIDs,)) if memberIDs else []
</del><ins>+                resourceIDs=objectIDs,)) if objectIDs else []
</ins><span class="cx">         elif self._uid:
</span><del>-            rows = (yield self._allWithResourceIDAndUID(memberIDs).on(
</del><ins>+            rows = (yield self._allWithResourceIDAndUID(objectIDs).on(
</ins><span class="cx">                 self._txn, uid=self._uid,
</span><del>-                resourceIDs=memberIDs,)) if memberIDs else []
</del><ins>+                resourceIDs=objectIDs,)) if objectIDs else []
</ins><span class="cx">         elif self._resourceID:
</span><span class="cx">             rows = (yield self._allWithResourceID.on(
</span><del>-                self._txn, resourceID=self._resourceID,)) if (self._resourceID in memberIDs) else []
</del><ins>+                self._txn, resourceID=self._resourceID,)) if (self._resourceID in objectIDs) else []
</ins><span class="cx"> 
</span><span class="cx">         if rows:
</span><span class="cx">             self._initFromRow(tuple(rows[0]))
</span><span class="lines">@@ -534,9 +531,9 @@
</span><span class="cx">     @classmethod
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def _allColumnsWithParent(cls, parent): #@NoSelf
</span><del>-        memberIDs = yield parent._allAddressBookObjectIDs()
-        rows = (yield cls._columnsWithResourceIDsQuery(cls._allColumns, memberIDs).on(
-            parent._txn, resourceIDs=memberIDs)) if memberIDs else []
</del><ins>+        objectIDs = yield parent._allAddressBookObjectIDs()
+        rows = (yield cls._abObjectColumnsWithResourceIDsQuery(cls._allColumns, objectIDs).on(
+            parent._txn, resourceIDs=objectIDs)) if objectIDs else []
</ins><span class="cx">         returnValue(rows)
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -551,9 +548,9 @@
</span><span class="cx">     @classmethod
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def _allColumnsWithParentAndNames(cls, parent, names): #@NoSelf
</span><del>-        memberIDs = yield parent._allAddressBookObjectIDs()
-        rows = (yield cls._allColumnsWithResourceIDsAndNamesQuery(memberIDs, names).on(
-            parent._txn, resourceIDs=memberIDs, names=names)) if memberIDs else []
</del><ins>+        objectIDs = yield parent._allAddressBookObjectIDs()
+        rows = (yield cls._allColumnsWithResourceIDsAndNamesQuery(objectIDs, names).on(
+            parent._txn, resourceIDs=objectIDs, names=names)) if objectIDs else []
</ins><span class="cx">         returnValue(rows)
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -635,9 +632,27 @@
</span><span class="cx">                       )
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @classmethod
+    def _deleteMembersWithGroupIDAndMemberIDsQuery(cls, groupID, memberIDs): #@NoSelf
+        aboMembers = schema.ABO_MEMBERS
+        return Delete(
+            aboMembers,
+            Where=(aboMembers.GROUP_ID == groupID).And(
+                    aboMembers.MEMBER_ID.In(Parameter(&quot;memberIDs&quot;, len(memberIDs)))))
+
+
+    @classmethod
+    def _deleteForeignMembersWithGroupIDAndMembeAddrsQuery(cls, groupID, memberAddrs): #@NoSelf
+        aboForeignMembers = schema.ABO_FOREIGN_MEMBERS
+        return Delete(
+            aboForeignMembers,
+            Where=(aboForeignMembers.GROUP_ID == groupID).And(
+                    aboForeignMembers.MEMBER_ADDRESS.In(Parameter(&quot;memberAddrs&quot;, len(memberAddrs)))))
+
+
</ins><span class="cx">     @inlineCallbacks
</span><span class="cx">     def updateDatabase(self, component, expand_until=None, reCreate=False,
</span><del>-                       inserting=False, removingObject=None):
</del><ins>+                       inserting=False):
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Update the database tables for the new data being written.
</span><span class="cx"> 
</span><span class="lines">@@ -778,17 +793,10 @@
</span><span class="cx">             memberIDsToDelete = set(currentMemberIDs) - set(memberIDs)
</span><span class="cx">             memberIDsToAdd = set(memberIDs) - set(currentMemberIDs)
</span><span class="cx"> 
</span><del>-            #correct for object that is about to be removed
-            if removingObject:
-                memberIDsToDelete |= set([removingObject._resourceID])
</del><ins>+            if memberIDsToDelete:
+                yield self._deleteMembersWithGroupIDAndMemberIDsQuery(self._resourceID, memberIDsToDelete).on(
+                    self._txn, memberIDs=memberIDsToDelete)
</ins><span class="cx"> 
</span><del>-            for memberIDToDelete in memberIDsToDelete:
-                yield Delete(
-                    aboMembers,
-                    Where=((aboMembers.GROUP_ID == self._resourceID).And(
-                            aboMembers.MEMBER_ID == memberIDToDelete))
-                ).on(self._txn)
-
</del><span class="cx">             for memberIDToAdd in memberIDsToAdd:
</span><span class="cx">                 yield Insert(
</span><span class="cx">                     {aboMembers.GROUP_ID: self._resourceID,
</span><span class="lines">@@ -808,17 +816,10 @@
</span><span class="cx">                 foreignMemberAddrsToDelete = set(currentForeignMemberAddrs) - set(foreignMemberAddrs)
</span><span class="cx">                 foreignMemberAddrsToAdd = set(foreignMemberAddrs) - set(currentForeignMemberAddrs)
</span><span class="cx"> 
</span><del>-                for foreignMemberAddrToDelete in foreignMemberAddrsToDelete:
-                    yield Delete(
-                        aboForeignMembers,
-                        Where=((aboForeignMembers.GROUP_ID == self._resourceID).And(
-                                aboForeignMembers.MEMBER_ADDRESS == foreignMemberAddrToDelete))
-                    ).on(self._txn)
</del><ins>+                if foreignMemberAddrsToDelete:
+                    yield self._deleteForeignMembersWithGroupIDAndMembeAddrsQuery(self._resourceID, foreignMemberAddrsToDelete).on(
+                        self._txn, memberAddrs=foreignMemberAddrsToDelete)
</ins><span class="cx"> 
</span><del>-                #correct for object that is about to be removed
-                if removingObject:
-                    foreignMemberAddrsToAdd |= set([&quot;urn:uuid:&quot; + removingObject.uid()])
-
</del><span class="cx">                 for foreignMemberAddrToAdd in foreignMemberAddrsToAdd:
</span><span class="cx">                     yield Insert(
</span><span class="cx">                         {aboForeignMembers.GROUP_ID: self._resourceID,
</span><span class="lines">@@ -869,7 +870,7 @@
</span><span class="cx"> 
</span><span class="cx">             # then get member UIDs
</span><span class="cx">             abo = schema.ADDRESSBOOK_OBJECT
</span><del>-            memberUIDRows = (yield self._columnsWithResourceIDsQuery(
</del><ins>+            memberUIDRows = (yield self._abObjectColumnsWithResourceIDsQuery(
</ins><span class="cx">                              [abo.VCARD_UID],
</span><span class="cx">                              memberIDs).on(
</span><span class="cx">                                 self._txn, resourceIDs=memberIDs)
</span></span></pre>
</div>
</div>

</body>
</html>