[CalendarServer-changes] [14458] CalendarServer/branches/users/cdaboo/pod2pod-migration

source_changes at macosforge.org source_changes at macosforge.org
Thu Feb 19 19:36:45 PST 2015


Revision: 14458
          http://trac.calendarserver.org//changeset/14458
Author:   cdaboo at apple.com
Date:     2015-02-19 19:36:45 -0800 (Thu, 19 Feb 2015)
Log Message:
-----------
Use DAL Record for APN subscriptions.

Modified Paths:
--------------
    CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/applepush.py
    CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/test/test_applepush.py
    CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/tools/push.py
    CalendarServer/branches/users/cdaboo/pod2pod-migration/requirements-stable.txt
    CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql.py
    CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/icommondatastore.py

Added Paths:
-----------
    CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql_apn.py

Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/applepush.py
===================================================================
--- CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/applepush.py	2015-02-20 03:35:37 UTC (rev 14457)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/applepush.py	2015-02-20 03:36:45 UTC (rev 14458)
@@ -234,10 +234,7 @@
                 self.log.debug(
                     "Sending %d APNS notifications for %s" %
                     (numSubscriptions, pushKey))
-                tokens = []
-                for token, uid in subscriptions:
-                    if token and uid:
-                        tokens.append(token)
+                tokens = [record.token for record in subscriptions if record.token and record.subscriberGUID]
                 if tokens:
                     provider.scheduleNotifications(
                         tokens, pushKey,
@@ -349,11 +346,11 @@
                     (token,))
                 txn = self.factory.store.newTransaction(label="APNProviderProtocol.processError")
                 subscriptions = (yield txn.apnSubscriptionsByToken(token))
-                for key, _ignore_modified, _ignore_uid in subscriptions:
+                for record in subscriptions:
                     self.log.debug(
                         "Removing subscription: %s %s" %
-                        (token, key))
-                    yield txn.removeAPNSubscription(token, key)
+                        (token, record.resourceKey))
+                    yield txn.removeAPNSubscription(token, record.resourceKey)
                 yield txn.commit()
 
 
@@ -746,12 +743,12 @@
         txn = self.factory.store.newTransaction(label="APNFeedbackProtocol.processFeedback")
         subscriptions = (yield txn.apnSubscriptionsByToken(token))
 
-        for key, modified, _ignore_uid in subscriptions:
-            if timestamp > modified:
+        for record in subscriptions:
+            if timestamp > record.modified:
                 self.log.debug(
                     "FeedbackProtocol removing subscription: %s %s" %
-                    (token, key))
-                yield txn.removeAPNSubscription(token, key)
+                    (token, record.resourceKey))
+                yield txn.removeAPNSubscription(token, record.resourceKey)
         yield txn.commit()
 
 

Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/test/test_applepush.py
===================================================================
--- CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/test/test_applepush.py	2015-02-20 03:35:37 UTC (rev 14457)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/test/test_applepush.py	2015-02-20 03:36:45 UTC (rev 14458)
@@ -88,6 +88,7 @@
         yield txn.addAPNSubscription(token, key2, timestamp2, uid, userAgent, ipAddr)
 
         subscriptions = (yield txn.apnSubscriptionsBySubscriber(uid))
+        subscriptions = [[record.token, record.resourceKey, record.modified, record.userAgent, record.ipAddr] for record in subscriptions]
         self.assertTrue([token, key1, timestamp1, userAgent, ipAddr] in subscriptions)
         self.assertTrue([token, key2, timestamp2, userAgent, ipAddr] in subscriptions)
         self.assertTrue([token2, key1, timestamp1, userAgent, ipAddr] in subscriptions)
@@ -98,9 +99,11 @@
         uid2 = "D8FFB335-9D36-4CE8-A3B9-D1859E38C0DA"
         yield txn.addAPNSubscription(token, key2, timestamp3, uid2, userAgent, ipAddr)
         subscriptions = (yield txn.apnSubscriptionsBySubscriber(uid))
+        subscriptions = [[record.token, record.resourceKey, record.modified, record.userAgent, record.ipAddr] for record in subscriptions]
         self.assertTrue([token, key1, timestamp1, userAgent, ipAddr] in subscriptions)
         self.assertFalse([token, key2, timestamp3, userAgent, ipAddr] in subscriptions)
         subscriptions = (yield txn.apnSubscriptionsBySubscriber(uid2))
+        subscriptions = [[record.token, record.resourceKey, record.modified, record.userAgent, record.ipAddr] for record in subscriptions]
         self.assertTrue([token, key2, timestamp3, userAgent, ipAddr] in subscriptions)
         # Change it back
         yield txn.addAPNSubscription(token, key2, timestamp2, uid, userAgent, ipAddr)
@@ -284,10 +287,10 @@
         txn = self._sqlCalendarStore.newTransaction()
         subscriptions = (yield txn.apnSubscriptionsByToken(token))
         yield txn.commit()
-        self.assertEquals(
-            subscriptions,
-            [["/CalDAV/calendars.example.com/user02/calendar/", 3000, "D2256BCC-48E2-42D1-BD89-CBA1E4CCDFFB"]]
-        )
+        self.assertEquals(len(subscriptions), 1)
+        self.assertEqual(subscriptions[0].resourceKey, "/CalDAV/calendars.example.com/user02/calendar/")
+        self.assertEqual(subscriptions[0].modified, 3000)
+        self.assertEqual(subscriptions[0].subscriberGUID, "D2256BCC-48E2-42D1-BD89-CBA1E4CCDFFB")
 
         # Verify processError removes associated subscriptions and history
         # First find the id corresponding to token2
@@ -326,7 +329,7 @@
         subscriptions = (yield txn.apnSubscriptionsByToken(token2))
         yield txn.commit()
         self.assertEquals(len(subscriptions), 1)
-        self.assertEquals(subscriptions[0][0], key2)
+        self.assertEquals(subscriptions[0].resourceKey, key2)
 
         service.stopService()
 

Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/tools/push.py
===================================================================
--- CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/tools/push.py	2015-02-20 03:35:37 UTC (rev 14457)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/tools/push.py	2015-02-20 03:36:45 UTC (rev 14458)
@@ -68,9 +68,9 @@
             (yield txn.commit())
             if subscriptions:
                 byKey = {}
-                for token, key, timestamp, userAgent, ipAddr in subscriptions:
-                    byKey.setdefault(key, []).append((token, timestamp, userAgent, ipAddr))
-                for key, tokens in byKey.iteritems():
+                for apnrecord in subscriptions:
+                    byKey.setdefault(apnrecord.resourceKey, []).append(apnrecord)
+                for key, apnsrecords in byKey.iteritems():
                     print
                     protocol, _ignore_host, path = key.strip("/").split("/", 2)
                     resource = {
@@ -89,13 +89,13 @@
                     else:
                         print("...is subscribed to %s's %s home" % (user, resource),)
                         # print("   (key: %s)\n" % (key,))
-                    print("with %d device(s):" % (len(tokens),))
-                    for token, timestamp, userAgent, ipAddr in tokens:
+                    print("with %d device(s):" % (len(apnsrecords),))
+                    for apnrecords in apnsrecords:
                         print(" %s\n   '%s' from %s\n   %s" % (
-                            token, userAgent, ipAddr,
+                            apnrecords.token, apnrecords.userAgent, apnrecords.ipAddr,
                             time.strftime(
                                 "on %a, %d %b %Y at %H:%M:%S %z(%Z)",
-                                time.localtime(timestamp)
+                                time.localtime(apnrecords.modified)
                             )
                         ))
             else:

Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/requirements-stable.txt
===================================================================
--- CalendarServer/branches/users/cdaboo/pod2pod-migration/requirements-stable.txt	2015-02-20 03:35:37 UTC (rev 14457)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/requirements-stable.txt	2015-02-20 03:36:45 UTC (rev 14458)
@@ -36,7 +36,7 @@
             #pyOpenSSL
         pycrypto==2.6.1
 
-    --editable svn+http://svn.calendarserver.org/repository/calendarserver/twext/branches/users/cdaboo/pod2pod-migration@14448#egg=twextpy
+    --editable svn+http://svn.calendarserver.org/repository/calendarserver/twext/branches/users/cdaboo/pod2pod-migration@14457#egg=twextpy
         cffi==0.8.6
             pycparser==2.10
         #twisted

Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql.py
===================================================================
--- CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql.py	2015-02-20 03:35:37 UTC (rev 14457)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql.py	2015-02-20 03:36:45 UTC (rev 14458)
@@ -14,7 +14,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 ##
-from txdav.common.datastore.sql_imip import imipAPIMixin
 
 """
 SQL data store.
@@ -62,8 +61,10 @@
 from txdav.carddav.iaddressbookstore import IAddressBookTransaction
 from txdav.common.datastore.common import HomeChildBase
 from txdav.common.datastore.podding.conduit import PoddingConduit
+from txdav.common.datastore.sql_apn import APNSubscriptionsMixin
 from txdav.common.datastore.sql_directory import DelegatesAPIMixin, \
     GroupsAPIMixin, GroupCacherAPIMixin
+from txdav.common.datastore.sql_imip import imipAPIMixin
 from txdav.common.datastore.sql_tables import _BIND_MODE_DIRECT, \
     _BIND_MODE_INDIRECT, _BIND_MODE_OWN, _BIND_STATUS_ACCEPTED, \
     _BIND_STATUS_DECLINED, _BIND_STATUS_DELETED, _BIND_STATUS_INVALID, \
@@ -75,7 +76,7 @@
 from txdav.common.icommondatastore import HomeChildNameNotAllowedError, \
     HomeChildNameAlreadyExistsError, NoSuchHomeChildError, \
     ObjectResourceNameNotAllowedError, ObjectResourceNameAlreadyExistsError, \
-    NoSuchObjectResourceError, AllRetriesFailed, InvalidSubscriptionValues, \
+    NoSuchObjectResourceError, AllRetriesFailed, \
     TooManyObjectResourcesError, SyncTokenValidException
 from txdav.common.idirectoryservice import IStoreDirectoryService, \
     DirectoryRecordNotFoundError
@@ -567,7 +568,7 @@
 
 class CommonStoreTransaction(
     GroupsAPIMixin, GroupCacherAPIMixin, DelegatesAPIMixin,
-    imipAPIMixin,
+    imipAPIMixin, APNSubscriptionsMixin,
 ):
     """
     Transaction implementation for SQL database.
@@ -785,145 +786,6 @@
         return NotificationCollection.notificationsWithResourceID(self, rid)
 
 
-    @classproperty
-    def _insertAPNSubscriptionQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Insert({
-            apn.TOKEN: Parameter("token"),
-            apn.RESOURCE_KEY: Parameter("resourceKey"),
-            apn.MODIFIED: Parameter("modified"),
-            apn.SUBSCRIBER_GUID: Parameter("subscriber"),
-            apn.USER_AGENT: Parameter("userAgent"),
-            apn.IP_ADDR: Parameter("ipAddr")
-        })
-
-
-    @classproperty
-    def _updateAPNSubscriptionQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Update(
-            {
-                apn.MODIFIED: Parameter("modified"),
-                apn.SUBSCRIBER_GUID: Parameter("subscriber"),
-                apn.USER_AGENT: Parameter("userAgent"),
-                apn.IP_ADDR: Parameter("ipAddr")
-            },
-            Where=(apn.TOKEN == Parameter("token")).And(
-                apn.RESOURCE_KEY == Parameter("resourceKey"))
-        )
-
-
-    @classproperty
-    def _selectAPNSubscriptionQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Select(
-            [apn.MODIFIED, apn.SUBSCRIBER_GUID],
-            From=apn,
-            Where=(apn.TOKEN == Parameter("token")).And(
-                apn.RESOURCE_KEY == Parameter("resourceKey")
-            )
-        )
-
-
-    @inlineCallbacks
-    def addAPNSubscription(
-        self, token, key, timestamp, subscriber,
-        userAgent, ipAddr
-    ):
-        if not (token and key and timestamp and subscriber):
-            raise InvalidSubscriptionValues()
-
-        # Cap these values at 255 characters
-        userAgent = userAgent[:255]
-        ipAddr = ipAddr[:255]
-
-        row = yield self._selectAPNSubscriptionQuery.on(
-            self,
-            token=token, resourceKey=key
-        )
-        if not row:  # Subscription does not yet exist
-            try:
-                yield self._insertAPNSubscriptionQuery.on(
-                    self,
-                    token=token, resourceKey=key, modified=timestamp,
-                    subscriber=subscriber, userAgent=userAgent,
-                    ipAddr=ipAddr)
-            except Exception:
-                # Subscription may have been added by someone else, which is fine
-                pass
-
-        else:  # Subscription exists, so update with new timestamp and subscriber
-            try:
-                yield self._updateAPNSubscriptionQuery.on(
-                    self,
-                    token=token, resourceKey=key, modified=timestamp,
-                    subscriber=subscriber, userAgent=userAgent,
-                    ipAddr=ipAddr)
-            except Exception:
-                # Subscription may have been added by someone else, which is fine
-                pass
-
-
-    @classproperty
-    def _removeAPNSubscriptionQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Delete(From=apn,
-                      Where=(apn.TOKEN == Parameter("token")).And(
-                          apn.RESOURCE_KEY == Parameter("resourceKey")))
-
-
-    def removeAPNSubscription(self, token, key):
-        return self._removeAPNSubscriptionQuery.on(
-            self,
-            token=token, resourceKey=key)
-
-
-    @classproperty
-    def _purgeOldAPNSubscriptionQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Delete(From=apn,
-                      Where=(apn.MODIFIED < Parameter("olderThan")))
-
-
-    def purgeOldAPNSubscriptions(self, olderThan):
-        return self._purgeOldAPNSubscriptionQuery.on(
-            self,
-            olderThan=olderThan)
-
-
-    @classproperty
-    def _apnSubscriptionsByTokenQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Select([apn.RESOURCE_KEY, apn.MODIFIED, apn.SUBSCRIBER_GUID],
-                      From=apn, Where=apn.TOKEN == Parameter("token"))
-
-
-    def apnSubscriptionsByToken(self, token):
-        return self._apnSubscriptionsByTokenQuery.on(self, token=token)
-
-
-    @classproperty
-    def _apnSubscriptionsByKeyQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Select([apn.TOKEN, apn.SUBSCRIBER_GUID],
-                      From=apn, Where=apn.RESOURCE_KEY == Parameter("resourceKey"))
-
-
-    def apnSubscriptionsByKey(self, key):
-        return self._apnSubscriptionsByKeyQuery.on(self, resourceKey=key)
-
-
-    @classproperty
-    def _apnSubscriptionsBySubscriberQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Select([apn.TOKEN, apn.RESOURCE_KEY, apn.MODIFIED, apn.USER_AGENT, apn.IP_ADDR],
-                      From=apn, Where=apn.SUBSCRIBER_GUID == Parameter("subscriberGUID"))
-
-
-    def apnSubscriptionsBySubscriber(self, guid):
-        return self._apnSubscriptionsBySubscriberQuery.on(self, subscriberGUID=guid)
-
-
     def preCommit(self, operation):
         """
         Run things before C{commit}.  (Note: only provided by SQL

Added: CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql_apn.py
===================================================================
--- CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql_apn.py	                        (rev 0)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql_apn.py	2015-02-20 03:36:45 UTC (rev 14458)
@@ -0,0 +1,121 @@
+# -*- test-case-name: twext.enterprise.dal.test.test_record -*-
+##
+# Copyright (c) 2015 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from twext.enterprise.dal.record import SerializableRecord, fromTable
+from twext.python.log import Logger
+from twisted.internet.defer import inlineCallbacks
+from txdav.common.datastore.sql_tables import schema
+from txdav.common.icommondatastore import InvalidSubscriptionValues
+
+log = Logger()
+
+"""
+Classes and methods that relate to APN objects in the SQL store.
+"""
+
+class APNSubscriptionsRecord(SerializableRecord, fromTable(schema.APN_SUBSCRIPTIONS)):
+    """
+    @DynamicAttrs
+    L{Record} for L{schema.APN_SUBSCRIPTIONS}.
+    """
+    pass
+
+
+
+class APNSubscriptionsMixin(object):
+    """
+    A mixin for L{CommonStoreTransaction} that covers the APN API.
+    """
+
+    @inlineCallbacks
+    def addAPNSubscription(
+        self, token, key, timestamp, subscriber,
+        userAgent, ipAddr
+    ):
+        if not (token and key and timestamp and subscriber):
+            raise InvalidSubscriptionValues()
+
+        # Cap these values at 255 characters
+        userAgent = userAgent[:255]
+        ipAddr = ipAddr[:255]
+
+        records = yield APNSubscriptionsRecord.querysimple(
+            self,
+            token=token, resourceKey=key
+        )
+        if not records:  # Subscription does not yet exist
+            try:
+                yield APNSubscriptionsRecord.create(
+                    self,
+                    token=token,
+                    resourceKey=key,
+                    modified=timestamp,
+                    subscriberGUID=subscriber,
+                    userAgent=userAgent,
+                    ipAddr=ipAddr
+                )
+            except Exception:
+                # Subscription may have been added by someone else, which is fine
+                pass
+
+        else:  # Subscription exists, so update with new timestamp and subscriber
+            try:
+                yield records[0].update(
+                    modified=timestamp,
+                    subscriberGUID=subscriber,
+                    userAgent=userAgent,
+                    ipAddr=ipAddr,
+                )
+            except Exception:
+                # Subscription may have been added by someone else, which is fine
+                pass
+
+
+    def removeAPNSubscription(self, token, key):
+        return APNSubscriptionsRecord.deletesimple(
+            self,
+            token=token,
+            resourceKey=key
+        )
+
+
+    def purgeOldAPNSubscriptions(self, olderThan):
+        return APNSubscriptionsRecord.deletesome(
+            self,
+            APNSubscriptionsRecord.modified < olderThan,
+        )
+
+
+    def apnSubscriptionsByToken(self, token):
+        return APNSubscriptionsRecord.querysimple(
+            self,
+            token=token,
+        )
+
+
+    def apnSubscriptionsByKey(self, key):
+        return APNSubscriptionsRecord.querysimple(
+            self,
+            resourceKey=key,
+        )
+
+
+    def apnSubscriptionsBySubscriber(self, guid):
+        return APNSubscriptionsRecord.querysimple(
+            self,
+            subscriberGUID=guid,
+        )

Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/icommondatastore.py
===================================================================
--- CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/icommondatastore.py	2015-02-20 03:35:37 UTC (rev 14457)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/icommondatastore.py	2015-02-20 03:36:45 UTC (rev 14458)
@@ -347,7 +347,7 @@
         @param token: The device token of the subscriber
         @type token: C{str}
 
-        @return: tuples of (key, timestamp, guid)
+        @return: list of L{Record}
         """
 
     def apnSubscriptionsByKey(key): #@NoSelf
@@ -357,7 +357,7 @@
         @param key: The push key
         @type key: C{str}
 
-        @return: tuples of (token, guid)
+        @return: list of L{Record}
         """
 
     def apnSubscriptionsBySubscriber(guid): #@NoSelf
@@ -367,7 +367,7 @@
         @param guid: The GUID of the subscribed principal
         @type guid: C{str}
 
-        @return: tuples of (token, key, timestamp, userAgent, ipAddr)
+        @return: list of L{Record}
         """
 
     def imipCreateToken(organizer, attendee, icaluid, token=None): #@NoSelf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20150219/ada43a76/attachment-0001.html>


More information about the calendarserver-changes mailing list