<!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>[13236] CalendarServer/trunk</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/13236">13236</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-04-09 18:55:12 -0700 (Wed, 09 Apr 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Adds inTransaction( ) method to the store, and now delegates.py uses that.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktwistedcaldavdirectoryprincipalpy">CalendarServer/trunk/twistedcaldav/directory/principal.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastorefilepy">CalendarServer/trunk/txdav/common/datastore/file.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresqlpy">CalendarServer/trunk/txdav/common/datastore/sql.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoretesttest_sqlpy">CalendarServer/trunk/txdav/common/datastore/test/test_sql.py</a></li>
<li><a href="#CalendarServertrunktxdavwhodelegatespy">CalendarServer/trunk/txdav/who/delegates.py</a></li>
<li><a href="#CalendarServertrunktxdavwhoutilpy">CalendarServer/trunk/txdav/who/util.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktwistedcaldavdirectoryprincipalpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/directory/principal.py (13235 => 13236)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/directory/principal.py        2014-04-10 01:43:23 UTC (rev 13235)
+++ CalendarServer/trunk/twistedcaldav/directory/principal.py        2014-04-10 01:55:12 UTC (rev 13236)
</span><span class="lines">@@ -484,7 +484,7 @@
</span><span class="cx">                 for record in (
</span><span class="cx">                     yield self.directory.recordsWithRecordType(self.recordType)
</span><span class="cx">                 ):
</span><del>-                    for shortName in record.shortNames:
</del><ins>+                    for shortName in getattr(record, &quot;shortNames&quot;, []):
</ins><span class="cx">                         children.append(shortName)
</span><span class="cx">             except AttributeError:
</span><span class="cx">                 log.warn(&quot;Cannot list children of record type {rt}&quot;,
</span><span class="lines">@@ -638,19 +638,17 @@
</span><span class="cx">             emailAddresses = record.emailAddresses
</span><span class="cx">         except AttributeError:
</span><span class="cx">             emailAddresses = []
</span><ins>+        try:
+            shortNames = record.shortNames
+        except AttributeError:
+            shortNames = []
</ins><span class="cx">         return tag.fillSlots(
</span><span class="cx">             directoryGUID=str(record.service.guid),
</span><span class="cx">             realm=record.service.realmName.encode(&quot;utf-8&quot;),
</span><span class="cx">             principalGUID=guid,
</span><span class="cx">             recordType=record.service.recordTypeToOldName(record.recordType),
</span><del>-            shortNames=&quot;,&quot;.join([n.encode(&quot;utf-8&quot;) for n in record.shortNames]),
-            # MOVE2WHO: need this?
-            # securityIDs=&quot;,&quot;.join(record.authIDs),
</del><ins>+            shortNames=&quot;,&quot;.join([n.encode(&quot;utf-8&quot;) for n in shortNames]),
</ins><span class="cx">             fullName=record.displayName.encode(&quot;utf-8&quot;),
</span><del>-            # MOVE2WHO: need this?
-            # firstName=str(record.firstName),
-            # MOVE2WHO: need this?
-            # lastName=str(record.lastName),
</del><span class="cx">             emailAddresses=formatList(emailAddresses),
</span><span class="cx">             principalUID=str(self.resource.principalUID()),
</span><span class="cx">             principalURL=formatLink(self.resource.principalURL()),
</span><span class="lines">@@ -815,12 +813,12 @@
</span><span class="cx">                 record.service.recordTypeToOldName(record.recordType),
</span><span class="cx">                 quote(shortName.encode(&quot;utf-8&quot;))
</span><span class="cx">             ) + slash
</span><del>-            for shortName in record.shortNames
</del><ins>+            for shortName in getattr(record, &quot;shortNames&quot;, [])
</ins><span class="cx">         ])
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def __str__(self):
</span><del>-        return &quot;(%s)%s&quot; % (self.record.recordType, self.record.shortNames[0])
</del><ins>+        return &quot;(%s)%s&quot; % (self.record.recordType, self.record.uid)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def __eq__(self, other):
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastorefilepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/file.py (13235 => 13236)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/file.py        2014-04-10 01:43:23 UTC (rev 13235)
+++ CalendarServer/trunk/txdav/common/datastore/file.py        2014-04-10 01:55:12 UTC (rev 13236)
</span><span class="lines">@@ -31,6 +31,7 @@
</span><span class="cx"> from twisted.internet.defer import succeed, inlineCallbacks, returnValue
</span><span class="cx"> from twisted.python.util import FancyEqMixin
</span><span class="cx"> from twisted.python import hashlib
</span><ins>+from twisted.python.failure import Failure
</ins><span class="cx"> 
</span><span class="cx"> from twistedcaldav import customxml
</span><span class="cx"> from twistedcaldav.customxml import NotificationType
</span><span class="lines">@@ -182,6 +183,41 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><ins>+    def inTransaction(self, label, operation, transactionCreator=None):
+        &quot;&quot;&quot;
+        Perform the given operation in a transaction, committing or aborting as
+        required.
+
+        @param label: the label to pass to the transaction creator
+
+        @param operation: a 1-arg callable that takes an L{IAsyncTransaction} and
+            returns a value.
+
+        @param transactionCreator: a 1-arg callable that takes a &quot;label&quot; arg and
+            returns a transaction
+
+        @return: a L{Deferred} that fires with C{operation}'s result or fails with
+            its error, unless there is an error creating, aborting or committing
+            the transaction.
+        &quot;&quot;&quot;
+
+        if transactionCreator is None:
+            txn = self.newTransaction()
+        else:
+            txn = transactionCreator(label=label)
+
+        try:
+            result = yield operation(txn)
+        except:
+            f = Failure()
+            yield txn.abort()
+            returnValue(f)
+        else:
+            yield txn.commit()
+            returnValue(result)
+
+
+    @inlineCallbacks
</ins><span class="cx">     def _withEachHomeDo(self, enumerator, action, batchSize):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Implementation of L{ICalendarStore.withEachCalendarHomeDo} and
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql.py (13235 => 13236)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql.py        2014-04-10 01:43:23 UTC (rev 13235)
+++ CalendarServer/trunk/txdav/common/datastore/sql.py        2014-04-10 01:55:12 UTC (rev 13236)
</span><span class="lines">@@ -293,6 +293,41 @@
</span><span class="cx">         return txn
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @inlineCallbacks
+    def inTransaction(self, label, operation, transactionCreator=None):
+        &quot;&quot;&quot;
+        Perform the given operation in a transaction, committing or aborting as
+        required.
+
+        @param label: the label to pass to the transaction creator
+
+        @param operation: a 1-arg callable that takes an L{IAsyncTransaction} and
+            returns a value.
+
+        @param transactionCreator: a 1-arg callable that takes a &quot;label&quot; arg and
+            returns a transaction
+
+        @return: a L{Deferred} that fires with C{operation}'s result or fails with
+            its error, unless there is an error creating, aborting or committing
+            the transaction.
+        &quot;&quot;&quot;
+
+        if transactionCreator is None:
+            txn = self.newTransaction()
+        else:
+            txn = transactionCreator(label=label)
+
+        try:
+            result = yield operation(txn)
+        except:
+            f = Failure()
+            yield txn.abort()
+            returnValue(f)
+        else:
+            yield txn.commit()
+            returnValue(result)
+
+
</ins><span class="cx">     def setMigrating(self, state):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Set the &quot;migrating&quot; state
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoretesttest_sqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/test/test_sql.py (13235 => 13236)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/test/test_sql.py        2014-04-10 01:43:23 UTC (rev 13235)
+++ CalendarServer/trunk/txdav/common/datastore/test/test_sql.py        2014-04-10 01:55:12 UTC (rev 13236)
</span><span class="lines">@@ -21,7 +21,7 @@
</span><span class="cx"> from twext.enterprise.dal.syntax import Select
</span><span class="cx"> from twext.enterprise.dal.syntax import Insert
</span><span class="cx"> 
</span><del>-from twisted.internet.defer import inlineCallbacks, returnValue
</del><ins>+from twisted.internet.defer import inlineCallbacks, returnValue, succeed
</ins><span class="cx"> from twisted.internet.task import Clock
</span><span class="cx"> from twisted.trial.unittest import TestCase
</span><span class="cx"> from twisted.internet.defer import Deferred
</span><span class="lines">@@ -433,3 +433,50 @@
</span><span class="cx">         yield fixUUIDNormalization(self.storeUnderTest())
</span><span class="cx">         self.assertEqual((yield self.allHomeUIDs(schema.ADDRESSBOOK_HOME)),
</span><span class="cx">                          [[normalizedUID]])
</span><ins>+
+
+    @inlineCallbacks
+    def test_inTransaction(self):
+        &quot;&quot;&quot;
+        Make sure a successful operation commits the transaction while an
+        unsuccessful operation (raised an exception) aborts the transaction.
+        &quot;&quot;&quot;
+
+        store = self.storeUnderTest()
+
+        def txnCreator(label):
+            self.txn = StubTransaction(label)
+            return self.txn
+
+        def goodOperation(txn):
+            return succeed(None)
+
+        def badOperation(txn):
+            1 / 0
+            return succeed(None)
+
+        yield store.inTransaction(&quot;good&quot;, goodOperation, txnCreator)
+        self.assertEquals(self.txn.action, &quot;committed&quot;)
+
+        try:
+            yield store.inTransaction(&quot;bad&quot;, badOperation, txnCreator)
+        except:
+            pass
+        self.assertEquals(self.txn.action, &quot;aborted&quot;)
+
+
+
+class StubTransaction(object):
+
+    def __init__(self, label):
+        self.label = label
+        self.action = None
+
+    def commit(self):
+        self.action = &quot;committed&quot;
+        return succeed(None)
+
+    def abort(self):
+        self.action = &quot;aborted&quot;
+        return succeed(None)
+
</ins></span></pre></div>
<a id="CalendarServertrunktxdavwhodelegatespy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/delegates.py (13235 => 13236)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/delegates.py        2014-04-10 01:43:23 UTC (rev 13235)
+++ CalendarServer/trunk/txdav/who/delegates.py        2014-04-10 01:55:12 UTC (rev 13236)
</span><span class="lines">@@ -68,21 +68,26 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         parentUID, _ignore_proxyType = self.uid.split(u&quot;#&quot;)
</span><span class="cx"> 
</span><del>-        txn = self.service._store.newTransaction(label=&quot;DirectoryRecord.members&quot;)
</del><ins>+        @inlineCallbacks
+        def _members(txn):
+            if self.recordType in (
+                RecordType.readDelegateGroup, RecordType.writeDelegateGroup
+            ):  # Members are delegates of this record
+                readWrite = (self.recordType is RecordType.writeDelegateGroup)
+                delegateUIDs = (
+                    yield txn.delegates(parentUID, readWrite, expanded=expanded)
+                )
</ins><span class="cx"> 
</span><del>-        if self.recordType in (
-            RecordType.readDelegateGroup, RecordType.writeDelegateGroup
-        ):  # Members are delegates of this record
-            readWrite = (self.recordType is RecordType.writeDelegateGroup)
-            delegateUIDs = (
-                yield txn.delegates(parentUID, readWrite, expanded=expanded)
-            )
</del><ins>+            else:  # Members have delegated to this record
+                readWrite = (self.recordType is RecordType.writeDelegatorGroup)
+                delegateUIDs = (
+                    yield txn.delegators(parentUID, readWrite)
+                )
+            returnValue(delegateUIDs)
</ins><span class="cx"> 
</span><del>-        else:  # Members have delegated to this record
-            readWrite = (self.recordType is RecordType.writeDelegatorGroup)
-            delegateUIDs = (
-                yield txn.delegators(parentUID, readWrite)
-            )
</del><ins>+        delegateUIDs = yield self.service._store.inTransaction(
+            &quot;DirectoryRecord.members&quot;, _members
+        )
</ins><span class="cx"> 
</span><span class="cx">         records = []
</span><span class="cx">         for uid in delegateUIDs:
</span><span class="lines">@@ -90,7 +95,6 @@
</span><span class="cx">                 record = yield self.service._masterDirectory.recordWithUID(uid)
</span><span class="cx">                 if record is not None:
</span><span class="cx">                     records.append(record)
</span><del>-        yield txn.commit()
</del><span class="cx"> 
</span><span class="cx">         returnValue(records)
</span><span class="cx"> 
</span><span class="lines">@@ -117,22 +121,24 @@
</span><span class="cx">             m=[r.uid for r in memberRecords]
</span><span class="cx">         )
</span><span class="cx"> 
</span><del>-        txn = self.service._store.newTransaction(label=&quot;DirectoryRecord.setMembers&quot;)
</del><ins>+        @inlineCallbacks
+        def _setMembers(txn):
+            yield txn.removeDelegates(parentUID, readWrite)
+            yield txn.removeDelegateGroups(parentUID, readWrite)
</ins><span class="cx"> 
</span><del>-        yield txn.removeDelegates(parentUID, readWrite)
-        yield txn.removeDelegateGroups(parentUID, readWrite)
</del><ins>+            delegator = (
+                yield self.service._masterDirectory.recordWithUID(parentUID)
+            )
</ins><span class="cx"> 
</span><del>-        delegator = (
-            yield self.service._masterDirectory.recordWithUID(parentUID)
</del><ins>+            for delegate in memberRecords:
+                yield addDelegate(txn, delegator, delegate, readWrite)
+
+        yield self.service._store.inTransaction(
+            &quot;DirectoryRecord.setMembers&quot;, _setMembers
</ins><span class="cx">         )
</span><span class="cx"> 
</span><del>-        for delegate in memberRecords:
-            yield addDelegate(txn, delegator, delegate, readWrite)
</del><span class="cx"> 
</span><del>-        yield txn.commit()
</del><span class="cx"> 
</span><del>-
-
</del><span class="cx"> def recordTypeToProxyType(recordType):
</span><span class="cx">     return {
</span><span class="cx">         RecordType.readDelegateGroup: &quot;calendar-proxy-read&quot;,
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhoutilpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/util.py (13235 => 13236)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/util.py        2014-04-10 01:43:23 UTC (rev 13235)
+++ CalendarServer/trunk/txdav/who/util.py        2014-04-10 01:55:12 UTC (rev 13236)
</span><span class="lines">@@ -106,7 +106,6 @@
</span><span class="cx">                 params.rdnSchema.base,
</span><span class="cx">                 credentials=creds,
</span><span class="cx">                 fieldNameToAttributesMap=MappingProxyType({
</span><del>-                    # FieldName.dn: (LDAPAttribute.dn.value,),
</del><span class="cx">                     BaseFieldName.uid: (&quot;apple-generateduid&quot;,),
</span><span class="cx">                     BaseFieldName.guid: (&quot;apple-generateduid&quot;,),
</span><span class="cx">                     BaseFieldName.shortNames: (LDAPAttribute.uid.value,),
</span></span></pre>
</div>
</div>

</body>
</html>