[CalendarServer-changes] [4519] CalendarServer/branches/users/wsanchez/deployment/twistedcaldav/ directory

source_changes at macosforge.org source_changes at macosforge.org
Tue Sep 1 09:03:51 PDT 2009


Revision: 4519
          http://trac.macosforge.org/projects/calendarserver/changeset/4519
Author:   cdaboo at apple.com
Date:     2009-09-01 09:03:51 -0700 (Tue, 01 Sep 2009)
Log Message:
-----------
Cache cuaddrs->record mapping for fast lookups rather than iterating over all records.

Modified Paths:
--------------
    CalendarServer/branches/users/wsanchez/deployment/twistedcaldav/directory/appleopendirectory.py
    CalendarServer/branches/users/wsanchez/deployment/twistedcaldav/directory/test/test_opendirectoryrecords.py

Modified: CalendarServer/branches/users/wsanchez/deployment/twistedcaldav/directory/appleopendirectory.py
===================================================================
--- CalendarServer/branches/users/wsanchez/deployment/twistedcaldav/directory/appleopendirectory.py	2009-08-31 19:55:02 UTC (rev 4518)
+++ CalendarServer/branches/users/wsanchez/deployment/twistedcaldav/directory/appleopendirectory.py	2009-09-01 16:03:51 UTC (rev 4519)
@@ -476,6 +476,25 @@
 
         return record
 
+    def recordWithCalendarUserAddress(self, address):
+        address = address.lower()
+
+        def lookup():
+            for recordType in self.recordTypes():
+                record = self._storage(recordType)["cuaddrs"].get(address, None)
+                if record:
+                    return record
+            else:
+                return None
+
+        record = lookup()
+
+        if record is None:
+            # Nothing found
+            self.log_info("Unable to find any record with calendar user address %s" % (address,))
+
+        return record
+
     def groupsForGUID(self, guid):
         
         # Lookup in index
@@ -590,6 +609,7 @@
         if shortName is None and guid is None:
             records = {}
             guids   = {}
+            cuaddrs = {}
 
             disabledNames = set()
             disabledGUIDs = set()
@@ -604,6 +624,7 @@
 
             records = storage["records"]
             guids   = storage["guids"]
+            cuaddrs = storage["cuaddrs"]
 
             disabledNames = storage["disabled names"]
             disabledGUIDs = storage["disabled guids"]
@@ -728,6 +749,7 @@
 
                 shortName = record.shortName
                 guid      = record.guid
+                cuaddrset = record.calendarUserAddresses
 
                 disabledNames.add(shortName)
                 disabledGUIDs.add(guid)
@@ -736,6 +758,9 @@
                     del records[shortName]
                 if guid in guids:
                     del guids[guid]
+                for cuaddr in cuaddrset:
+                    if cuaddr in cuaddrs:
+                        del cuaddrs[cuaddr]
 
             # Check for disabled items
             if record.shortName in disabledNames or record.guid in disabledGUIDs:
@@ -756,6 +781,8 @@
 
                 if record.shortName not in disabledNames:
                     records[record.shortName] = guids[record.guid] = record
+                    for cuaddr in record.calendarUserAddresses:
+                        cuaddrs[cuaddr] = record
                     self.log_debug("Added record %s to OD record cache" % (record,))
 
                     # Do group indexing if needed
@@ -775,6 +802,7 @@
                 "status"        : "new",
                 "records"       : records,
                 "guids"         : guids,
+                "cuaddrs"       : cuaddrs,
                 "disabled names": disabledNames,
                 "disabled guids": disabledGUIDs,
             }

Modified: CalendarServer/branches/users/wsanchez/deployment/twistedcaldav/directory/test/test_opendirectoryrecords.py
===================================================================
--- CalendarServer/branches/users/wsanchez/deployment/twistedcaldav/directory/test/test_opendirectoryrecords.py	2009-08-31 19:55:02 UTC (rev 4518)
+++ CalendarServer/branches/users/wsanchez/deployment/twistedcaldav/directory/test/test_opendirectoryrecords.py	2009-09-01 16:03:51 UTC (rev 4519)
@@ -26,9 +26,9 @@
     from twistedcaldav.directory.util import uuidFromName
 
     class OpenDirectoryService (RealOpenDirectoryService):
-        def _queryDirectory(directory, recordType, shortName=None, guid=None):
+        def _queryDirectory(self, recordType, shortName=None, guid=None):
             if shortName is None and guid is None:
-                return directory.fakerecords[recordType]
+                return self.fakerecords[recordType]
 
             assert shortName is None or guid is None
             if guid is not None:
@@ -36,7 +36,7 @@
 
             records = []
 
-            for name, record in directory.fakerecords[recordType]:
+            for name, record in self.fakerecords[recordType]:
                 if name == shortName or record[dsattributes.kDS1AttrGeneratedUID] == guid:
                     records.append((name, record))
 
@@ -395,6 +395,60 @@
             self.assertTrue(user2 is not None)
             self.assertEqual(set((group2, group3)), user2.groups()) 
 
+        def test_calendaruseraddress(self):
+            self._service.fakerecords = {
+                DirectoryService.recordType_users: [
+                    fakeODRecord("User 01"),
+                    fakeODRecord("User 02"),
+                ],
+                DirectoryService.recordType_groups: [],
+                DirectoryService.recordType_resources: [],
+                DirectoryService.recordType_locations: [],
+            }
+
+            self._service.reloadCache(DirectoryService.recordType_users)
+
+            user1 = self._service.recordWithCalendarUserAddress("mailto:user01 at example.com")
+            self.assertTrue(user1 is not None)
+
+            user3 = self._service.recordWithCalendarUserAddress("mailto:user03 at example.com")
+            self.assertTrue(user3 is None)
+
+            self._service.fakerecords = {
+                DirectoryService.recordType_users: [
+                    fakeODRecord("User 01"),
+                    fakeODRecord("User 02"),
+                    fakeODRecord("User 03"),
+                ],
+                DirectoryService.recordType_groups: [],
+                DirectoryService.recordType_resources: [],
+                DirectoryService.recordType_locations: [],
+            }
+            self._service.reloadCache(DirectoryService.recordType_users)
+
+            user1 = self._service.recordWithCalendarUserAddress("mailto:user01 at example.com")
+            self.assertTrue(user1 is not None)
+
+            user3 = self._service.recordWithCalendarUserAddress("mailto:user03 at example.com")
+            self.assertTrue(user3 is not None)
+
+            self._service.fakerecords = {
+                DirectoryService.recordType_users: [
+                    fakeODRecord("User 02"),
+                    fakeODRecord("User 03"),
+                ],
+                DirectoryService.recordType_groups: [],
+                DirectoryService.recordType_resources: [],
+                DirectoryService.recordType_locations: [],
+            }
+            self._service.reloadCache(DirectoryService.recordType_users)
+
+            user1 = self._service.recordWithCalendarUserAddress("mailto:user01 at example.com")
+            self.assertTrue(user1 is None)
+
+            user3 = self._service.recordWithCalendarUserAddress("mailto:user03 at example.com")
+            self.assertTrue(user3 is not None)
+
 def fakeODRecord(fullName, shortName=None, guid=None, email=None, addLocator=True, members=None):
     if shortName is None:
         shortName = shortNameForFullName(fullName)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20090901/ef4be037/attachment.html>


More information about the calendarserver-changes mailing list